1/****************************************************************************
2**
3** Copyright (C) 2020 The Qt Company Ltd.
4** Copyright (C) 2019 Intel Corporation.
5** Copyright (C) 2019 Mail.ru Group.
6** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
7** Contact: https://www.qt.io/licensing/
8**
9** This file is part of the QtCore module of the Qt Toolkit.
10**
11** $QT_BEGIN_LICENSE:LGPL$
12** Commercial License Usage
13** Licensees holding valid commercial Qt licenses may use this file in
14** accordance with the commercial license agreement provided with the
15** Software or, alternatively, in accordance with the terms contained in
16** a written agreement between you and The Qt Company. For licensing terms
17** and conditions see https://www.qt.io/terms-conditions. For further
18** information use the contact form at https://www.qt.io/contact-us.
19**
20** GNU Lesser General Public License Usage
21** Alternatively, this file may be used under the terms of the GNU Lesser
22** General Public License version 3 as published by the Free Software
23** Foundation and appearing in the file LICENSE.LGPL3 included in the
24** packaging of this file. Please review the following information to
25** ensure the GNU Lesser General Public License version 3 requirements
26** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 2.0 or (at your option) the GNU General
31** Public license version 3 or any later version approved by the KDE Free
32** Qt Foundation. The licenses are as published by the Free Software
33** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
34** included in the packaging of this file. Please review the following
35** information to ensure the GNU General Public License requirements will
36** be met: https://www.gnu.org/licenses/gpl-2.0.html and
37** https://www.gnu.org/licenses/gpl-3.0.html.
38**
39** $QT_END_LICENSE$
40**
41****************************************************************************/
42
43#ifndef QSTRING_H
44#define QSTRING_H
45
46#if defined(QT_NO_CAST_FROM_ASCII) && defined(QT_RESTRICTED_CAST_FROM_ASCII)
47#error QT_NO_CAST_FROM_ASCII and QT_RESTRICTED_CAST_FROM_ASCII must not be defined at the same time
48#endif
49
50#include <QtCore/qchar.h>
51#include <QtCore/qbytearray.h>
52#include <QtCore/qarraydata.h>
53#include <QtCore/qnamespace.h>
54#include <QtCore/qstringliteral.h>
55#include <QtCore/qstringalgorithms.h>
56#include <QtCore/qanystringview.h>
57#include <QtCore/qstringtokenizer.h>
58
59#include <string>
60#include <iterator>
61
62#include <stdarg.h>
63
64#ifdef truncate
65#error qstring.h must be included before any header file that defines truncate
66#endif
67
68#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
69Q_FORWARD_DECLARE_CF_TYPE(CFString);
70Q_FORWARD_DECLARE_OBJC_CLASS(NSString);
71#endif
72
73QT_BEGIN_NAMESPACE
74
75class QRegularExpression;
76class QRegularExpressionMatch;
77class QString;
78
79namespace QtPrivate {
80template <bool...B> class BoolList;
81}
82
83class QLatin1String
84{
85public:
86 constexpr inline QLatin1String() noexcept : m_size(0), m_data(nullptr) {}
87 constexpr inline explicit QLatin1String(const char *s) noexcept : m_size(s ? qsizetype(strlen(s)) : 0), m_data(s) {}
88 constexpr explicit QLatin1String(const char *f, const char *l)
89 : QLatin1String(f, qsizetype(l - f)) {}
90 constexpr inline explicit QLatin1String(const char *s, qsizetype sz) noexcept : m_size(sz), m_data(s) {}
91 explicit QLatin1String(const QByteArray &s) noexcept : m_size(qsizetype(qstrnlen(s.constData(), s.size()))), m_data(s.constData()) {}
92
93 inline QString toString() const;
94
95 constexpr const char *latin1() const noexcept { return m_data; }
96 constexpr qsizetype size() const noexcept { return m_size; }
97 constexpr const char *data() const noexcept { return m_data; }
98
99 constexpr bool isNull() const noexcept { return !data(); }
100 constexpr bool isEmpty() const noexcept { return !size(); }
101
102 template <typename...Args>
103 [[nodiscard]] inline QString arg(Args &&...args) const;
104
105 [[nodiscard]] constexpr QLatin1Char at(qsizetype i) const
106 { return Q_ASSERT(i >= 0), Q_ASSERT(i < size()), QLatin1Char(m_data[i]); }
107 [[nodiscard]] constexpr QLatin1Char operator[](qsizetype i) const { return at(i); }
108
109 [[nodiscard]] constexpr QLatin1Char front() const { return at(0); }
110 [[nodiscard]] constexpr QLatin1Char back() const { return at(size() - 1); }
111
112 [[nodiscard]] int compare(QStringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
113 { return QtPrivate::compareStrings(*this, other, cs); }
114 [[nodiscard]] int compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
115 { return QtPrivate::compareStrings(*this, other, cs); }
116 [[nodiscard]] constexpr int compare(QChar c) const noexcept
117 { return isEmpty() || front() == c ? size() - 1 : uchar(m_data[0]) - c.unicode() ; }
118 [[nodiscard]] int compare(QChar c, Qt::CaseSensitivity cs) const noexcept
119 { return QtPrivate::compareStrings(*this, QStringView(&c, 1), cs); }
120
121 [[nodiscard]] bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
122 { return QtPrivate::startsWith(*this, s, cs); }
123 [[nodiscard]] bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
124 { return QtPrivate::startsWith(*this, s, cs); }
125 [[nodiscard]] constexpr bool startsWith(QChar c) const noexcept
126 { return !isEmpty() && front() == c; }
127 [[nodiscard]] inline bool startsWith(QChar c, Qt::CaseSensitivity cs) const noexcept
128 { return QtPrivate::startsWith(*this, QStringView(&c, 1), cs); }
129
130 [[nodiscard]] bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
131 { return QtPrivate::endsWith(*this, s, cs); }
132 [[nodiscard]] bool endsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
133 { return QtPrivate::endsWith(*this, s, cs); }
134 [[nodiscard]] constexpr bool endsWith(QChar c) const noexcept
135 { return !isEmpty() && back() == c; }
136 [[nodiscard]] inline bool endsWith(QChar c, Qt::CaseSensitivity cs) const noexcept
137 { return QtPrivate::endsWith(*this, QStringView(&c, 1), cs); }
138
139 [[nodiscard]] qsizetype indexOf(QStringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
140 { return QtPrivate::findString(*this, from, s, cs); }
141 [[nodiscard]] qsizetype indexOf(QLatin1String s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
142 { return QtPrivate::findString(*this, from, s, cs); }
143 [[nodiscard]] qsizetype indexOf(QChar c, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
144 { return QtPrivate::findString(*this, from, QStringView(&c, 1), cs); }
145
146 [[nodiscard]] bool contains(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
147 { return indexOf(s, 0, cs) != -1; }
148 [[nodiscard]] bool contains(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
149 { return indexOf(s, 0, cs) != -1; }
150 [[nodiscard]] inline bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
151 { return indexOf(QStringView(&c, 1), 0, cs) != -1; }
152
153 [[nodiscard]] qsizetype lastIndexOf(QStringView s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
154 { return QtPrivate::lastIndexOf(*this, from, s, cs); }
155 [[nodiscard]] qsizetype lastIndexOf(QLatin1String s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
156 { return QtPrivate::lastIndexOf(*this, from, s, cs); }
157 [[nodiscard]] qsizetype lastIndexOf(QChar c, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
158 { return QtPrivate::lastIndexOf(*this, from, QStringView(&c, 1), cs); }
159
160 using value_type = const char;
161 using reference = value_type&;
162 using const_reference = reference;
163 using iterator = value_type*;
164 using const_iterator = iterator;
165 using difference_type = qsizetype; // violates Container concept requirements
166 using size_type = qsizetype; // violates Container concept requirements
167
168 constexpr const_iterator begin() const noexcept { return data(); }
169 constexpr const_iterator cbegin() const noexcept { return data(); }
170 constexpr const_iterator end() const noexcept { return data() + size(); }
171 constexpr const_iterator cend() const noexcept { return data() + size(); }
172
173 using reverse_iterator = std::reverse_iterator<iterator>;
174 using const_reverse_iterator = reverse_iterator;
175
176 const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
177 const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); }
178 const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
179 const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); }
180
181 [[nodiscard]] constexpr QLatin1String mid(qsizetype pos, qsizetype n = -1) const
182 {
183 using namespace QtPrivate;
184 auto result = QContainerImplHelper::mid(size(), &pos, &n);
185 return result == QContainerImplHelper::Null ? QLatin1String() : QLatin1String(m_data + pos, n);
186 }
187 [[nodiscard]] constexpr QLatin1String left(qsizetype n) const
188 {
189 if (size_t(n) >= size_t(size()))
190 n = size();
191 return QLatin1String(m_data, n);
192 }
193 [[nodiscard]] constexpr QLatin1String right(qsizetype n) const
194 {
195 if (size_t(n) >= size_t(size()))
196 n = size();
197 return QLatin1String(m_data + m_size - n, n);
198 }
199
200 [[nodiscard]] constexpr QLatin1String sliced(qsizetype pos) const
201 { verify(pos); return QLatin1String(m_data + pos, m_size - pos); }
202 [[nodiscard]] constexpr QLatin1String sliced(qsizetype pos, qsizetype n) const
203 { verify(pos, n); return QLatin1String(m_data + pos, n); }
204 [[nodiscard]] constexpr QLatin1String first(qsizetype n) const
205 { verify(n); return QLatin1String(m_data, n); }
206 [[nodiscard]] constexpr QLatin1String last(qsizetype n) const
207 { verify(n); return QLatin1String(m_data + size() - n, n); }
208 [[nodiscard]] constexpr QLatin1String chopped(qsizetype n) const
209 { verify(n); return QLatin1String(m_data, size() - n); }
210
211 constexpr void chop(qsizetype n)
212 { verify(n); m_size -= n; }
213 constexpr void truncate(qsizetype n)
214 { verify(n); m_size = n; }
215
216 [[nodiscard]] QLatin1String trimmed() const noexcept { return QtPrivate::trimmed(*this); }
217
218 template <typename Needle, typename...Flags>
219 [[nodiscard]] inline constexpr auto tokenize(Needle &&needle, Flags...flags) const
220 noexcept(noexcept(qTokenize(std::declval<const QLatin1String &>(), std::forward<Needle>(needle), flags...)))
221 -> decltype(qTokenize(*this, std::forward<Needle>(needle), flags...))
222 { return qTokenize(*this, std::forward<Needle>(needle), flags...); }
223
224 inline bool operator==(const QString &s) const noexcept;
225 inline bool operator!=(const QString &s) const noexcept;
226 inline bool operator>(const QString &s) const noexcept;
227 inline bool operator<(const QString &s) const noexcept;
228 inline bool operator>=(const QString &s) const noexcept;
229 inline bool operator<=(const QString &s) const noexcept;
230
231#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
232 QT_ASCII_CAST_WARN inline bool operator==(const char *s) const;
233 QT_ASCII_CAST_WARN inline bool operator!=(const char *s) const;
234 QT_ASCII_CAST_WARN inline bool operator<(const char *s) const;
235 QT_ASCII_CAST_WARN inline bool operator>(const char *s) const;
236 QT_ASCII_CAST_WARN inline bool operator<=(const char *s) const;
237 QT_ASCII_CAST_WARN inline bool operator>=(const char *s) const;
238
239 QT_ASCII_CAST_WARN inline bool operator==(const QByteArray &s) const;
240 QT_ASCII_CAST_WARN inline bool operator!=(const QByteArray &s) const;
241 QT_ASCII_CAST_WARN inline bool operator<(const QByteArray &s) const;
242 QT_ASCII_CAST_WARN inline bool operator>(const QByteArray &s) const;
243 QT_ASCII_CAST_WARN inline bool operator<=(const QByteArray &s) const;
244 QT_ASCII_CAST_WARN inline bool operator>=(const QByteArray &s) const;
245#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
246
247private:
248 Q_ALWAYS_INLINE constexpr void verify(qsizetype pos, qsizetype n = 0) const
249 {
250 Q_ASSERT(pos >= 0);
251 Q_ASSERT(pos <= size());
252 Q_ASSERT(n >= 0);
253 Q_ASSERT(n <= size() - pos);
254 }
255 qsizetype m_size;
256 const char *m_data;
257};
258Q_DECLARE_TYPEINFO(QLatin1String, Q_MOVABLE_TYPE);
259
260// Qt 4.x compatibility
261
262//
263// QLatin1String inline implementations
264//
265constexpr bool QtPrivate::isLatin1(QLatin1String) noexcept
266{ return true; }
267
268//
269// QStringView members that require QLatin1String:
270//
271int QStringView::compare(QLatin1String s, Qt::CaseSensitivity cs) const noexcept
272{ return QtPrivate::compareStrings(*this, s, cs); }
273bool QStringView::startsWith(QLatin1String s, Qt::CaseSensitivity cs) const noexcept
274{ return QtPrivate::startsWith(*this, s, cs); }
275bool QStringView::endsWith(QLatin1String s, Qt::CaseSensitivity cs) const noexcept
276{ return QtPrivate::endsWith(*this, s, cs); }
277qsizetype QStringView::indexOf(QLatin1String s, qsizetype from, Qt::CaseSensitivity cs) const noexcept
278{ return QtPrivate::findString(*this, from, s, cs); }
279bool QStringView::contains(QLatin1String s, Qt::CaseSensitivity cs) const noexcept
280{ return indexOf(s, 0, cs) != qsizetype(-1); }
281qsizetype QStringView::lastIndexOf(QLatin1String s, qsizetype from, Qt::CaseSensitivity cs) const noexcept
282{ return QtPrivate::lastIndexOf(*this, from, s, cs); }
283
284//
285// QAnyStringView members that require QLatin1String
286//
287
288constexpr QAnyStringView::QAnyStringView(QLatin1String str) noexcept
289 : m_data{str.data()}, m_size{size_t(str.size()) | Tag::Latin1} {}
290
291constexpr QLatin1String QAnyStringView::asLatin1StringView() const
292{
293 Q_ASSERT(isLatin1());
294 return QLatin1String{m_data_utf8, int(size())};
295}
296
297template <typename Visitor>
298constexpr decltype(auto) QAnyStringView::visit(Visitor &&v) const
299{
300 if (isUtf16())
301 return std::forward<Visitor>(v)(asStringView());
302 else if (isLatin1())
303 return std::forward<Visitor>(v)(asLatin1StringView());
304 else
305 return std::forward<Visitor>(v)(asUtf8StringView());
306}
307
308//
309// QAnyStringView members that require QAnyStringView::visit()
310//
311
312constexpr QChar QAnyStringView::front() const
313{
314 return visit([] (auto that) { return QAnyStringView::toQChar(that.front()); });
315}
316constexpr QChar QAnyStringView::back() const
317{
318 return visit([] (auto that) { return QAnyStringView::toQChar(that.back()); });
319}
320
321
322class Q_CORE_EXPORT QString
323{
324 typedef QTypedArrayData<char16_t> Data;
325public:
326 typedef QStringPrivate DataPointer;
327
328 inline constexpr QString() noexcept;
329 explicit QString(const QChar *unicode, qsizetype size = -1);
330 QString(QChar c);
331 QString(qsizetype size, QChar c);
332 inline QString(QLatin1String latin1);
333 inline QString(const QString &) noexcept;
334 inline ~QString();
335 QString &operator=(QChar c);
336 QString &operator=(const QString &) noexcept;
337 QString &operator=(QLatin1String latin1);
338 inline QString(QString &&other) noexcept
339 { qSwap(d, other.d); }
340 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QString)
341 inline void swap(QString &other) noexcept { qSwap(d, other.d); }
342 inline qsizetype size() const { return d.size; }
343 inline qsizetype count() const { return d.size; }
344 inline qsizetype length() const { return d.size; }
345 inline bool isEmpty() const;
346 void resize(qsizetype size);
347 void resize(qsizetype size, QChar fillChar);
348
349 QString &fill(QChar c, qsizetype size = -1);
350 void truncate(qsizetype pos);
351 void chop(qsizetype n);
352
353 inline qsizetype capacity() const;
354 inline void reserve(qsizetype size);
355 inline void squeeze();
356
357 inline const QChar *unicode() const;
358 inline QChar *data();
359 inline const QChar *data() const;
360 inline const QChar *constData() const;
361
362 inline void detach();
363 inline bool isDetached() const;
364 inline bool isSharedWith(const QString &other) const { return d.isSharedWith(other.d); }
365 void clear();
366
367 inline const QChar at(qsizetype i) const;
368 const QChar operator[](qsizetype i) const;
369 [[nodiscard]] QChar &operator[](qsizetype i);
370
371 [[nodiscard]] inline QChar front() const { return at(0); }
372 [[nodiscard]] inline QChar &front();
373 [[nodiscard]] inline QChar back() const { return at(size() - 1); }
374 [[nodiscard]] inline QChar &back();
375
376 [[nodiscard]] QString arg(qlonglong a, int fieldwidth=0, int base=10,
377 QChar fillChar = QLatin1Char(' ')) const;
378 [[nodiscard]] QString arg(qulonglong a, int fieldwidth=0, int base=10,
379 QChar fillChar = QLatin1Char(' ')) const;
380 [[nodiscard]] QString arg(long a, int fieldwidth=0, int base=10,
381 QChar fillChar = QLatin1Char(' ')) const;
382 [[nodiscard]] QString arg(ulong a, int fieldwidth=0, int base=10,
383 QChar fillChar = QLatin1Char(' ')) const;
384 [[nodiscard]] QString arg(int a, int fieldWidth = 0, int base = 10,
385 QChar fillChar = QLatin1Char(' ')) const;
386 [[nodiscard]] QString arg(uint a, int fieldWidth = 0, int base = 10,
387 QChar fillChar = QLatin1Char(' ')) const;
388 [[nodiscard]] QString arg(short a, int fieldWidth = 0, int base = 10,
389 QChar fillChar = QLatin1Char(' ')) const;
390 [[nodiscard]] QString arg(ushort a, int fieldWidth = 0, int base = 10,
391 QChar fillChar = QLatin1Char(' ')) const;
392 [[nodiscard]] QString arg(double a, int fieldWidth = 0, char fmt = 'g', int prec = -1,
393 QChar fillChar = QLatin1Char(' ')) const;
394 [[nodiscard]] QString arg(char a, int fieldWidth = 0,
395 QChar fillChar = QLatin1Char(' ')) const;
396 [[nodiscard]] QString arg(QChar a, int fieldWidth = 0,
397 QChar fillChar = QLatin1Char(' ')) const;
398#if QT_STRINGVIEW_LEVEL < 2
399 [[nodiscard]] QString arg(const QString &a, int fieldWidth = 0,
400 QChar fillChar = QLatin1Char(' ')) const;
401#endif
402 [[nodiscard]] QString arg(QStringView a, int fieldWidth = 0,
403 QChar fillChar = QLatin1Char(' ')) const;
404 [[nodiscard]] QString arg(QLatin1String a, int fieldWidth = 0,
405 QChar fillChar = QLatin1Char(' ')) const;
406private:
407 template <typename T>
408 struct is_convertible_to_view_or_qstring_helper
409 : std::integral_constant<bool,
410 std::is_convertible<T, QString>::value ||
411 std::is_convertible<T, QStringView>::value ||
412 std::is_convertible<T, QLatin1String>::value> {};
413 template <typename T>
414 struct is_convertible_to_view_or_qstring
415 : is_convertible_to_view_or_qstring_helper<typename std::decay<T>::type> {};
416public:
417 template <typename...Args>
418 [[nodiscard]]
419#ifdef Q_CLANG_QDOC
420 QString
421#else
422 typename std::enable_if<
423 sizeof...(Args) >= 2 && std::is_same<
424 QtPrivate::BoolList<is_convertible_to_view_or_qstring<Args>::value..., true>,
425 QtPrivate::BoolList<true, is_convertible_to_view_or_qstring<Args>::value...>
426 >::value,
427 QString
428 >::type
429#endif
430 arg(Args &&...args) const
431 { return qToStringViewIgnoringNull(*this).arg(std::forward<Args>(args)...); }
432
433 static QString vasprintf(const char *format, va_list ap) Q_ATTRIBUTE_FORMAT_PRINTF(1, 0);
434 static QString asprintf(const char *format, ...) Q_ATTRIBUTE_FORMAT_PRINTF(1, 2);
435
436 qsizetype indexOf(QChar c, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
437 qsizetype indexOf(QLatin1String s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
438#if QT_STRINGVIEW_LEVEL < 2
439 qsizetype indexOf(const QString &s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
440#endif
441 [[nodiscard]] qsizetype indexOf(QStringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
442 { return QtPrivate::findString(*this, from, s, cs); }
443 qsizetype lastIndexOf(QChar c, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
444 qsizetype lastIndexOf(QLatin1String s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
445#if QT_STRINGVIEW_LEVEL < 2
446 qsizetype lastIndexOf(const QString &s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
447#endif
448
449 [[nodiscard]] qsizetype lastIndexOf(QStringView s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
450 { return QtPrivate::lastIndexOf(*this, from, s, cs); }
451
452 inline bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
453#if QT_STRINGVIEW_LEVEL < 2
454 inline bool contains(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
455#endif
456 inline bool contains(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
457 inline bool contains(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
458 qsizetype count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
459 qsizetype count(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
460 qsizetype count(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
461
462#if QT_CONFIG(regularexpression)
463 qsizetype indexOf(const QRegularExpression &re, qsizetype from = 0,
464 QRegularExpressionMatch *rmatch = nullptr) const;
465 qsizetype lastIndexOf(const QRegularExpression &re, qsizetype from = -1,
466 QRegularExpressionMatch *rmatch = nullptr) const;
467 bool contains(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const;
468 qsizetype count(const QRegularExpression &re) const;
469#endif
470
471 enum SectionFlag {
472 SectionDefault = 0x00,
473 SectionSkipEmpty = 0x01,
474 SectionIncludeLeadingSep = 0x02,
475 SectionIncludeTrailingSep = 0x04,
476 SectionCaseInsensitiveSeps = 0x08
477 };
478 Q_DECLARE_FLAGS(SectionFlags, SectionFlag)
479
480 QString section(QChar sep, qsizetype start, qsizetype end = -1, SectionFlags flags = SectionDefault) const;
481 QString section(const QString &in_sep, qsizetype start, qsizetype end = -1, SectionFlags flags = SectionDefault) const;
482#if QT_CONFIG(regularexpression)
483 QString section(const QRegularExpression &re, qsizetype start, qsizetype end = -1, SectionFlags flags = SectionDefault) const;
484#endif
485 [[nodiscard]] QString left(qsizetype n) const;
486 [[nodiscard]] QString right(qsizetype n) const;
487 [[nodiscard]] QString mid(qsizetype position, qsizetype n = -1) const;
488
489 [[nodiscard]] QString first(qsizetype n) const
490 { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QString(data(), n); }
491 [[nodiscard]] QString last(qsizetype n) const
492 { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QString(data() + size() - n, n); }
493 [[nodiscard]] QString sliced(qsizetype pos) const
494 { Q_ASSERT(pos >= 0); Q_ASSERT(pos <= size()); return QString(data() + pos, size() - pos); }
495 [[nodiscard]] QString sliced(qsizetype pos, qsizetype n) const
496 { Q_ASSERT(pos >= 0); Q_ASSERT(n >= 0); Q_ASSERT(size_t(pos) + size_t(n) <= size_t(size())); return QString(data() + pos, n); }
497 [[nodiscard]] QString chopped(qsizetype n) const
498 { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return first(size() - n); }
499
500
501#if QT_STRINGVIEW_LEVEL < 2
502 bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
503#endif
504 [[nodiscard]] bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
505 { return QtPrivate::startsWith(*this, s, cs); }
506 bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
507 bool startsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
508
509#if QT_STRINGVIEW_LEVEL < 2
510 bool endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
511#endif
512 [[nodiscard]] bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
513 { return QtPrivate::endsWith(*this, s, cs); }
514 bool endsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
515 bool endsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
516
517 bool isUpper() const;
518 bool isLower() const;
519
520 [[nodiscard]] QString leftJustified(qsizetype width, QChar fill = QLatin1Char(' '), bool trunc = false) const;
521 [[nodiscard]] QString rightJustified(qsizetype width, QChar fill = QLatin1Char(' '), bool trunc = false) const;
522
523#if !defined(Q_CLANG_QDOC)
524 [[nodiscard]] QString toLower() const &
525 { return toLower_helper(*this); }
526 [[nodiscard]] QString toLower() &&
527 { return toLower_helper(*this); }
528 [[nodiscard]] QString toUpper() const &
529 { return toUpper_helper(*this); }
530 [[nodiscard]] QString toUpper() &&
531 { return toUpper_helper(*this); }
532 [[nodiscard]] QString toCaseFolded() const &
533 { return toCaseFolded_helper(*this); }
534 [[nodiscard]] QString toCaseFolded() &&
535 { return toCaseFolded_helper(*this); }
536 [[nodiscard]] QString trimmed() const &
537 { return trimmed_helper(*this); }
538 [[nodiscard]] QString trimmed() &&
539 { return trimmed_helper(*this); }
540 [[nodiscard]] QString simplified() const &
541 { return simplified_helper(*this); }
542 [[nodiscard]] QString simplified() &&
543 { return simplified_helper(*this); }
544#else
545 [[nodiscard]] QString toLower() const;
546 [[nodiscard]] QString toUpper() const;
547 [[nodiscard]] QString toCaseFolded() const;
548 [[nodiscard]] QString trimmed() const;
549 [[nodiscard]] QString simplified() const;
550#endif
551 [[nodiscard]] QString toHtmlEscaped() const;
552
553 QString &insert(qsizetype i, QChar c);
554 QString &insert(qsizetype i, const QChar *uc, qsizetype len);
555#if QT_STRINGVIEW_LEVEL < 2
556 inline QString &insert(qsizetype i, const QString &s) { return insert(i, s.constData(), s.length()); }
557#endif
558 inline QString &insert(qsizetype i, QStringView v) { return insert(i, v.data(), v.length()); }
559 QString &insert(qsizetype i, QLatin1String s);
560
561 QString &append(QChar c);
562 QString &append(const QChar *uc, qsizetype len);
563#if QT_STRINGVIEW_LEVEL < 2
564 QString &append(const QString &s);
565#endif
566 inline QString &append(QStringView v) { return append(v.data(), v.length()); }
567 QString &append(QLatin1String s);
568
569 inline QString &prepend(QChar c) { return insert(0, c); }
570 inline QString &prepend(const QChar *uc, qsizetype len) { return insert(0, uc, len); }
571#if QT_STRINGVIEW_LEVEL < 2
572 inline QString &prepend(const QString &s) { return insert(0, s); }
573#endif
574 inline QString &prepend(QStringView v) { return prepend(v.data(), v.length()); }
575 inline QString &prepend(QLatin1String s) { return insert(0, s); }
576
577 inline QString &operator+=(QChar c) { return append(c); }
578
579#if QT_STRINGVIEW_LEVEL < 2
580 inline QString &operator+=(const QString &s) { return append(s); }
581#endif
582 inline QString &operator+=(QStringView v) { return append(v); }
583 inline QString &operator+=(QLatin1String s) { return append(s); }
584
585 QString &remove(qsizetype i, qsizetype len);
586 QString &remove(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive);
587 QString &remove(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive);
588 QString &remove(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive);
589 QString &replace(qsizetype i, qsizetype len, QChar after);
590 QString &replace(qsizetype i, qsizetype len, const QChar *s, qsizetype slen);
591 QString &replace(qsizetype i, qsizetype len, const QString &after);
592 QString &replace(QChar before, QChar after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
593 QString &replace(const QChar *before, qsizetype blen, const QChar *after, qsizetype alen, Qt::CaseSensitivity cs = Qt::CaseSensitive);
594 QString &replace(QLatin1String before, QLatin1String after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
595 QString &replace(QLatin1String before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
596 QString &replace(const QString &before, QLatin1String after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
597 QString &replace(const QString &before, const QString &after,
598 Qt::CaseSensitivity cs = Qt::CaseSensitive);
599 QString &replace(QChar c, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
600 QString &replace(QChar c, QLatin1String after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
601#if QT_CONFIG(regularexpression)
602 QString &replace(const QRegularExpression &re, const QString &after);
603 inline QString &remove(const QRegularExpression &re)
604 { return replace(re, QString()); }
605#endif
606
607public:
608 [[nodiscard]]
609 QStringList split(const QString &sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
610 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
611 [[nodiscard]]
612 QStringList split(QChar sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts,
613 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
614#ifndef QT_NO_REGULAREXPRESSION
615 [[nodiscard]]
616 QStringList split(const QRegularExpression &sep,
617 Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const;
618#endif
619
620 template <typename Needle, typename...Flags>
621 [[nodiscard]] inline auto tokenize(Needle &&needle, Flags...flags) const &
622 noexcept(noexcept(qTokenize(std::declval<const QString &>(), std::forward<Needle>(needle), flags...)))
623 -> decltype(qTokenize(*this, std::forward<Needle>(needle), flags...))
624 { return qTokenize(qToStringViewIgnoringNull(*this), std::forward<Needle>(needle), flags...); }
625
626 template <typename Needle, typename...Flags>
627 [[nodiscard]] inline auto tokenize(Needle &&needle, Flags...flags) const &&
628 noexcept(noexcept(qTokenize(std::declval<const QString>(), std::forward<Needle>(needle), flags...)))
629 -> decltype(qTokenize(std::move(*this), std::forward<Needle>(needle), flags...))
630 { return qTokenize(std::move(*this), std::forward<Needle>(needle), flags...); }
631
632 template <typename Needle, typename...Flags>
633 [[nodiscard]] inline auto tokenize(Needle &&needle, Flags...flags) &&
634 noexcept(noexcept(qTokenize(std::declval<QString>(), std::forward<Needle>(needle), flags...)))
635 -> decltype(qTokenize(std::move(*this), std::forward<Needle>(needle), flags...))
636 { return qTokenize(std::move(*this), std::forward<Needle>(needle), flags...); }
637
638
639 enum NormalizationForm {
640 NormalizationForm_D,
641 NormalizationForm_C,
642 NormalizationForm_KD,
643 NormalizationForm_KC
644 };
645 [[nodiscard]] QString normalized(NormalizationForm mode, QChar::UnicodeVersion version = QChar::Unicode_Unassigned) const;
646
647 [[nodiscard]] QString repeated(qsizetype times) const;
648
649 const ushort *utf16() const;
650
651#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QSTRING_COMPAT_CPP) && !defined(Q_CLANG_QDOC)
652 [[nodiscard]] QByteArray toLatin1() const &
653 { return toLatin1_helper(*this); }
654 [[nodiscard]] QByteArray toLatin1() &&
655 { return toLatin1_helper_inplace(*this); }
656 [[nodiscard]] QByteArray toUtf8() const &
657 { return toUtf8_helper(*this); }
658 [[nodiscard]] QByteArray toUtf8() &&
659 { return toUtf8_helper(*this); }
660 [[nodiscard]] QByteArray toLocal8Bit() const &
661 { return toLocal8Bit_helper(isNull() ? nullptr : constData(), size()); }
662 [[nodiscard]] QByteArray toLocal8Bit() &&
663 { return toLocal8Bit_helper(isNull() ? nullptr : constData(), size()); }
664#else
665 [[nodiscard]] QByteArray toLatin1() const;
666 [[nodiscard]] QByteArray toUtf8() const;
667 [[nodiscard]] QByteArray toLocal8Bit() const;
668#endif
669 [[nodiscard]] QList<uint> toUcs4() const;
670
671 // note - this are all inline so we can benefit from strlen() compile time optimizations
672 static QString fromLatin1(QByteArrayView ba);
673 Q_WEAK_OVERLOAD
674 static inline QString fromLatin1(const QByteArray &ba) { return fromLatin1(QByteArrayView(ba)); }
675 static inline QString fromLatin1(const char *str, qsizetype size)
676 {
677 return fromLatin1(QByteArrayView(str, !str || size < 0 ? qstrlen(str) : size));
678 }
679 static QString fromUtf8(QByteArrayView utf8);
680 Q_WEAK_OVERLOAD
681 static inline QString fromUtf8(const QByteArray &ba) { return fromUtf8(QByteArrayView(ba)); }
682 static inline QString fromUtf8(const char *utf8, qsizetype size)
683 {
684 return fromUtf8(QByteArrayView(utf8, !utf8 || size < 0 ? qstrlen(utf8) : size));
685 }
686#ifdef __cpp_char8_t
687 Q_WEAK_OVERLOAD
688 static inline QString fromUtf8(const char8_t *str, qsizetype size)
689 { return fromUtf8(reinterpret_cast<const char *>(str), int(size)); }
690#endif
691 static QString fromLocal8Bit(QByteArrayView ba);
692 Q_WEAK_OVERLOAD
693 static inline QString fromLocal8Bit(const QByteArray &ba) { return fromLocal8Bit(QByteArrayView(ba)); }
694 static inline QString fromLocal8Bit(const char *str, qsizetype size)
695 {
696 return fromLocal8Bit(QByteArrayView(str, !str || size < 0 ? qstrlen(str) : size));
697 }
698 static QString fromUtf16(const char16_t *, qsizetype size = -1);
699 static QString fromUcs4(const char32_t *, qsizetype size = -1);
700 static QString fromRawData(const QChar *, qsizetype size);
701
702#if QT_DEPRECATED_SINCE(6, 0)
703 QT_DEPRECATED_VERSION_X_6_0("Use char16_t* overload.")
704 static QString fromUtf16(const ushort *str, qsizetype size = -1)
705 { return fromUtf16(reinterpret_cast<const char16_t *>(str), size); }
706 QT_DEPRECATED_VERSION_X_6_0("Use char32_t* overload.")
707 static QString fromUcs4(const uint *str, qsizetype size = -1)
708 { return fromUcs4(reinterpret_cast<const char32_t *>(str), size); }
709#endif
710
711 inline qsizetype toWCharArray(wchar_t *array) const;
712 [[nodiscard]] static inline QString fromWCharArray(const wchar_t *string, qsizetype size = -1);
713
714 QString &setRawData(const QChar *unicode, qsizetype size);
715 QString &setUnicode(const QChar *unicode, qsizetype size);
716 inline QString &setUtf16(const ushort *utf16, qsizetype size);
717
718#if QT_STRINGVIEW_LEVEL < 2
719 int compare(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
720#endif
721 int compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
722 inline int compare(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
723 int compare(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
724 { return compare(QStringView{&ch, 1}, cs); }
725
726 static inline int compare(const QString &s1, const QString &s2,
727 Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept
728 { return s1.compare(s2, cs); }
729
730 static inline int compare(const QString &s1, QLatin1String s2,
731 Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept
732 { return s1.compare(s2, cs); }
733 static inline int compare(QLatin1String s1, const QString &s2,
734 Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept
735 { return -s2.compare(s1, cs); }
736 static int compare(const QString &s1, QStringView s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept
737 { return s1.compare(s2, cs); }
738 static int compare(QStringView s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept
739 { return -s2.compare(s1, cs); }
740
741 int localeAwareCompare(const QString& s) const;
742 int localeAwareCompare(QStringView s) const;
743 static int localeAwareCompare(const QString& s1, const QString& s2)
744 { return s1.localeAwareCompare(s2); }
745
746 static int localeAwareCompare(QStringView s1, QStringView s2);
747
748 short toShort(bool *ok=nullptr, int base=10) const
749 { return toIntegral_helper<short>(*this, ok, base); }
750 ushort toUShort(bool *ok=nullptr, int base=10) const
751 { return toIntegral_helper<ushort>(*this, ok, base); }
752 int toInt(bool *ok=nullptr, int base=10) const
753 { return toIntegral_helper<int>(*this, ok, base); }
754 uint toUInt(bool *ok=nullptr, int base=10) const
755 { return toIntegral_helper<uint>(*this, ok, base); }
756 long toLong(bool *ok=nullptr, int base=10) const
757 { return toIntegral_helper<long>(*this, ok, base); }
758 ulong toULong(bool *ok=nullptr, int base=10) const
759 { return toIntegral_helper<ulong>(*this, ok, base); }
760 qlonglong toLongLong(bool *ok=nullptr, int base=10) const;
761 qulonglong toULongLong(bool *ok=nullptr, int base=10) const;
762 float toFloat(bool *ok=nullptr) const;
763 double toDouble(bool *ok=nullptr) const;
764
765 QString &setNum(short, int base=10);
766 QString &setNum(ushort, int base=10);
767 QString &setNum(int, int base=10);
768 QString &setNum(uint, int base=10);
769 QString &setNum(long, int base=10);
770 QString &setNum(ulong, int base=10);
771 QString &setNum(qlonglong, int base=10);
772 QString &setNum(qulonglong, int base=10);
773 QString &setNum(float, char f='g', int prec=6);
774 QString &setNum(double, char f='g', int prec=6);
775
776 static QString number(int, int base=10);
777 static QString number(uint, int base=10);
778 static QString number(long, int base=10);
779 static QString number(ulong, int base=10);
780 static QString number(qlonglong, int base=10);
781 static QString number(qulonglong, int base=10);
782 static QString number(double, char f='g', int prec=6);
783
784 friend Q_CORE_EXPORT bool operator==(const QString &s1, const QString &s2) noexcept;
785 friend Q_CORE_EXPORT bool operator<(const QString &s1, const QString &s2) noexcept;
786 friend inline bool operator>(const QString &s1, const QString &s2) noexcept { return s2 < s1; }
787 friend inline bool operator!=(const QString &s1, const QString &s2) noexcept { return !(s1 == s2); }
788 friend inline bool operator<=(const QString &s1, const QString &s2) noexcept { return !(s1 > s2); }
789 friend inline bool operator>=(const QString &s1, const QString &s2) noexcept { return !(s1 < s2); }
790
791 bool operator==(QLatin1String s) const noexcept;
792 bool operator<(QLatin1String s) const noexcept;
793 bool operator>(QLatin1String s) const noexcept;
794 inline bool operator!=(QLatin1String s) const noexcept { return !operator==(s); }
795 inline bool operator<=(QLatin1String s) const noexcept { return !operator>(s); }
796 inline bool operator>=(QLatin1String s) const noexcept { return !operator<(s); }
797
798 // ASCII compatibility
799#if defined(QT_RESTRICTED_CAST_FROM_ASCII)
800 template <qsizetype N>
801 inline QString(const char (&ch)[N])
802 : QString(fromUtf8(ch))
803 {}
804 template <qsizetype N>
805 QString(char (&)[N]) = delete;
806 template <qsizetype N>
807 inline QString &operator=(const char (&ch)[N])
808 { return (*this = fromUtf8(ch, N - 1)); }
809 template <qsizetype N>
810 QString &operator=(char (&)[N]) = delete;
811#endif
812#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
813 QT_ASCII_CAST_WARN inline QString(const char *ch)
814 : QString(fromUtf8(ch))
815 {}
816 QT_ASCII_CAST_WARN inline QString(const QByteArray &a)
817 : QString(fromUtf8(a))
818 {}
819 QT_ASCII_CAST_WARN inline QString &operator=(const char *ch)
820 { return (*this = fromUtf8(ch)); }
821 QT_ASCII_CAST_WARN inline QString &operator=(const QByteArray &a)
822 { return (*this = fromUtf8(a)); }
823
824 // these are needed, so it compiles with STL support enabled
825 QT_ASCII_CAST_WARN inline QString &prepend(const char *s)
826 { return prepend(QString::fromUtf8(s)); }
827 QT_ASCII_CAST_WARN inline QString &prepend(const QByteArray &s)
828 { return prepend(QString::fromUtf8(s)); }
829 QT_ASCII_CAST_WARN inline QString &append(const char *s)
830 { return append(QString::fromUtf8(s)); }
831 QT_ASCII_CAST_WARN inline QString &append(const QByteArray &s)
832 { return append(QString::fromUtf8(s)); }
833 QT_ASCII_CAST_WARN inline QString &insert(qsizetype i, const char *s)
834 { return insert(i, QString::fromUtf8(s)); }
835 QT_ASCII_CAST_WARN inline QString &insert(qsizetype i, const QByteArray &s)
836 { return insert(i, QString::fromUtf8(s)); }
837 QT_ASCII_CAST_WARN inline QString &operator+=(const char *s)
838 { return append(QString::fromUtf8(s)); }
839 QT_ASCII_CAST_WARN inline QString &operator+=(const QByteArray &s)
840 { return append(QString::fromUtf8(s)); }
841
842 QT_ASCII_CAST_WARN inline bool operator==(const char *s) const;
843 QT_ASCII_CAST_WARN inline bool operator!=(const char *s) const;
844 QT_ASCII_CAST_WARN inline bool operator<(const char *s) const;
845 QT_ASCII_CAST_WARN inline bool operator<=(const char *s) const;
846 QT_ASCII_CAST_WARN inline bool operator>(const char *s) const;
847 QT_ASCII_CAST_WARN inline bool operator>=(const char *s) const;
848
849 QT_ASCII_CAST_WARN inline bool operator==(const QByteArray &s) const;
850 QT_ASCII_CAST_WARN inline bool operator!=(const QByteArray &s) const;
851 QT_ASCII_CAST_WARN inline bool operator<(const QByteArray &s) const;
852 QT_ASCII_CAST_WARN inline bool operator>(const QByteArray &s) const;
853 QT_ASCII_CAST_WARN inline bool operator<=(const QByteArray &s) const;
854 QT_ASCII_CAST_WARN inline bool operator>=(const QByteArray &s) const;
855
856 friend inline bool operator==(const char *s1, const QString &s2);
857 friend inline bool operator!=(const char *s1, const QString &s2);
858 friend inline bool operator<(const char *s1, const QString &s2);
859 friend inline bool operator>(const char *s1, const QString &s2);
860 friend inline bool operator<=(const char *s1, const QString &s2);
861 friend inline bool operator>=(const char *s1, const QString &s2);
862#endif
863
864 typedef QChar *iterator;
865 typedef const QChar *const_iterator;
866 typedef iterator Iterator;
867 typedef const_iterator ConstIterator;
868 typedef std::reverse_iterator<iterator> reverse_iterator;
869 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
870 inline iterator begin();
871 inline const_iterator begin() const;
872 inline const_iterator cbegin() const;
873 inline const_iterator constBegin() const;
874 inline iterator end();
875 inline const_iterator end() const;
876 inline const_iterator cend() const;
877 inline const_iterator constEnd() const;
878 reverse_iterator rbegin() { return reverse_iterator(end()); }
879 reverse_iterator rend() { return reverse_iterator(begin()); }
880 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
881 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
882 const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); }
883 const_reverse_iterator crend() const { return const_reverse_iterator(begin()); }
884
885 // STL compatibility
886 typedef qsizetype size_type;
887 typedef qptrdiff difference_type;
888 typedef const QChar & const_reference;
889 typedef QChar & reference;
890 typedef QChar *pointer;
891 typedef const QChar *const_pointer;
892 typedef QChar value_type;
893 inline void push_back(QChar c) { append(c); }
894 inline void push_back(const QString &s) { append(s); }
895 inline void push_front(QChar c) { prepend(c); }
896 inline void push_front(const QString &s) { prepend(s); }
897 void shrink_to_fit() { squeeze(); }
898
899 static inline QString fromStdString(const std::string &s);
900 inline std::string toStdString() const;
901 static inline QString fromStdWString(const std::wstring &s);
902 inline std::wstring toStdWString() const;
903
904 static inline QString fromStdU16String(const std::u16string &s);
905 inline std::u16string toStdU16String() const;
906 static inline QString fromStdU32String(const std::u32string &s);
907 inline std::u32string toStdU32String() const;
908
909#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
910 static QString fromCFString(CFStringRef string);
911 CFStringRef toCFString() const Q_DECL_CF_RETURNS_RETAINED;
912 static QString fromNSString(const NSString *string);
913 NSString *toNSString() const Q_DECL_NS_RETURNS_AUTORELEASED;
914#endif
915
916 inline bool isNull() const { return d->isNull(); }
917
918
919 bool isSimpleText() const;
920 bool isRightToLeft() const;
921 [[nodiscard]] bool isValidUtf16() const noexcept
922 { return QStringView(*this).isValidUtf16(); }
923
924 QString(qsizetype size, Qt::Initialization);
925 explicit QString(DataPointer &&dd) : d(std::move(dd)) {}
926
927private:
928#if defined(QT_NO_CAST_FROM_ASCII)
929 QString &operator+=(const char *s);
930 QString &operator+=(const QByteArray &s);
931 QString(const char *ch);
932 QString(const QByteArray &a);
933 QString &operator=(const char *ch);
934 QString &operator=(const QByteArray &a);
935#endif
936
937 DataPointer d;
938 static const char16_t _empty;
939
940 friend inline bool operator==(QChar, const QString &) noexcept;
941 friend inline bool operator< (QChar, const QString &) noexcept;
942 friend inline bool operator> (QChar, const QString &) noexcept;
943 friend inline bool operator==(QChar, QLatin1String) noexcept;
944 friend inline bool operator< (QChar, QLatin1String) noexcept;
945 friend inline bool operator> (QChar, QLatin1String) noexcept;
946
947 void reallocData(qsizetype alloc, Data::ArrayOptions options);
948 void reallocGrowData(qsizetype alloc, Data::ArrayOptions options);
949 static int compare_helper(const QChar *data1, qsizetype length1,
950 const QChar *data2, qsizetype length2,
951 Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
952 static int compare_helper(const QChar *data1, qsizetype length1,
953 const char *data2, qsizetype length2,
954 Qt::CaseSensitivity cs = Qt::CaseSensitive);
955 static int compare_helper(const QChar *data1, qsizetype length1,
956 QLatin1String s2,
957 Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
958 static int localeAwareCompare_helper(const QChar *data1, qsizetype length1,
959 const QChar *data2, qsizetype length2);
960 static QString toLower_helper(const QString &str);
961 static QString toLower_helper(QString &str);
962 static QString toUpper_helper(const QString &str);
963 static QString toUpper_helper(QString &str);
964 static QString toCaseFolded_helper(const QString &str);
965 static QString toCaseFolded_helper(QString &str);
966 static QString trimmed_helper(const QString &str);
967 static QString trimmed_helper(QString &str);
968 static QString simplified_helper(const QString &str);
969 static QString simplified_helper(QString &str);
970 static QByteArray toLatin1_helper(const QString &);
971 static QByteArray toLatin1_helper_inplace(QString &);
972 static QByteArray toUtf8_helper(const QString &);
973 static QByteArray toLocal8Bit_helper(const QChar *data, qsizetype size);
974 static qsizetype toUcs4_helper(const ushort *uc, qsizetype length, uint *out);
975 static qlonglong toIntegral_helper(QStringView string, bool *ok, int base);
976 static qulonglong toIntegral_helper(QStringView string, bool *ok, uint base);
977 void replace_helper(size_t *indices, qsizetype nIndices, qsizetype blen, const QChar *after, qsizetype alen);
978 friend class QStringView;
979 friend class QByteArray;
980 friend class QCollator;
981 friend struct QAbstractConcatenable;
982
983 template <typename T> static
984 T toIntegral_helper(QStringView string, bool *ok, int base)
985 {
986 using Int64 = typename std::conditional<std::is_unsigned<T>::value, qulonglong, qlonglong>::type;
987 using Int32 = typename std::conditional<std::is_unsigned<T>::value, uint, int>::type;
988
989 // we select the right overload by casting base to int or uint
990 Int64 val = toIntegral_helper(string, ok, Int32(base));
991 if (T(val) != val) {
992 if (ok)
993 *ok = false;
994 val = 0;
995 }
996 return T(val);
997 }
998
999public:
1000 inline DataPointer &data_ptr() { return d; }
1001 inline const DataPointer &data_ptr() const { return d; }
1002};
1003
1004//
1005// QLatin1String inline members that require QString:
1006//
1007
1008QString QLatin1String::toString() const { return *this; }
1009
1010//
1011// QStringView inline members that require QString:
1012//
1013
1014QString QStringView::toString() const
1015{ return Q_ASSERT(size() == length()), QString(data(), length()); }
1016
1017qint64 QStringView::toLongLong(bool *ok, int base) const
1018{ return QString::toIntegral_helper<qint64>(*this, ok, base); }
1019quint64 QStringView::toULongLong(bool *ok, int base) const
1020{ return QString::toIntegral_helper<quint64>(*this, ok, base); }
1021long QStringView::toLong(bool *ok, int base) const
1022{ return QString::toIntegral_helper<long>(*this, ok, base); }
1023ulong QStringView::toULong(bool *ok, int base) const
1024{ return QString::toIntegral_helper<ulong>(*this, ok, base); }
1025int QStringView::toInt(bool *ok, int base) const
1026{ return QString::toIntegral_helper<int>(*this, ok, base); }
1027uint QStringView::toUInt(bool *ok, int base) const
1028{ return QString::toIntegral_helper<uint>(*this, ok, base); }
1029short QStringView::toShort(bool *ok, int base) const
1030{ return QString::toIntegral_helper<short>(*this, ok, base); }
1031ushort QStringView::toUShort(bool *ok, int base) const
1032{ return QString::toIntegral_helper<ushort>(*this, ok, base); }
1033
1034//
1035// QUtf8StringView inline members that require QString:
1036//
1037
1038template <bool UseChar8T>
1039QString QBasicUtf8StringView<UseChar8T>::toString() const
1040{
1041 Q_ASSERT(size() == int(size()));
1042 return QString::fromUtf8(data(), int(size()));
1043}
1044
1045//
1046// QAnyStringView inline members that require QString:
1047//
1048
1049QAnyStringView::QAnyStringView(const QByteArray &str) noexcept
1050 : QAnyStringView{str.isNull() ? nullptr : str.data(), str.size()} {}
1051QAnyStringView::QAnyStringView(const QString &str) noexcept
1052 : QAnyStringView{str.isNull() ? nullptr : str.data(), str.size()} {}
1053
1054QString QAnyStringView::toString() const
1055{ return QtPrivate::convertToQString(*this); }
1056
1057//
1058// QString inline members
1059//
1060inline QString::QString(QLatin1String latin1)
1061{ *this = QString::fromLatin1(latin1.data(), latin1.size()); }
1062inline const QChar QString::at(qsizetype i) const
1063{ Q_ASSERT(size_t(i) < size_t(size())); return QChar(d.data()[i]); }
1064inline const QChar QString::operator[](qsizetype i) const
1065{ Q_ASSERT(size_t(i) < size_t(size())); return QChar(d.data()[i]); }
1066inline bool QString::isEmpty() const
1067{ return d.size == 0; }
1068inline const QChar *QString::unicode() const
1069{ return data(); }
1070inline const QChar *QString::data() const
1071{
1072#if QT5_NULL_STRINGS == 1
1073 return reinterpret_cast<const QChar *>(d.data() ? d.data() : &_empty);
1074#else
1075 return reinterpret_cast<const QChar *>(d.data());
1076#endif
1077}
1078inline QChar *QString::data()
1079{
1080 detach();
1081 Q_ASSERT(d.data());
1082 return reinterpret_cast<QChar *>(d.data());
1083}
1084inline const QChar *QString::constData() const
1085{ return data(); }
1086inline void QString::detach()
1087{ if (d->needsDetach()) reallocData(d.size, d->detachFlags()); }
1088inline bool QString::isDetached() const
1089{ return !d->isShared(); }
1090inline void QString::clear()
1091{ if (!isNull()) *this = QString(); }
1092inline QString::QString(const QString &other) noexcept : d(other.d)
1093{ }
1094inline qsizetype QString::capacity() const { return qsizetype(d->constAllocatedCapacity()); }
1095inline QString &QString::setNum(short n, int base)
1096{ return setNum(qlonglong(n), base); }
1097inline QString &QString::setNum(ushort n, int base)
1098{ return setNum(qulonglong(n), base); }
1099inline QString &QString::setNum(int n, int base)
1100{ return setNum(qlonglong(n), base); }
1101inline QString &QString::setNum(uint n, int base)
1102{ return setNum(qulonglong(n), base); }
1103inline QString &QString::setNum(long n, int base)
1104{ return setNum(qlonglong(n), base); }
1105inline QString &QString::setNum(ulong n, int base)
1106{ return setNum(qulonglong(n), base); }
1107inline QString &QString::setNum(float n, char f, int prec)
1108{ return setNum(double(n),f,prec); }
1109inline QString QString::arg(int a, int fieldWidth, int base, QChar fillChar) const
1110{ return arg(qlonglong(a), fieldWidth, base, fillChar); }
1111inline QString QString::arg(uint a, int fieldWidth, int base, QChar fillChar) const
1112{ return arg(qulonglong(a), fieldWidth, base, fillChar); }
1113inline QString QString::arg(long a, int fieldWidth, int base, QChar fillChar) const
1114{ return arg(qlonglong(a), fieldWidth, base, fillChar); }
1115inline QString QString::arg(ulong a, int fieldWidth, int base, QChar fillChar) const
1116{ return arg(qulonglong(a), fieldWidth, base, fillChar); }
1117inline QString QString::arg(short a, int fieldWidth, int base, QChar fillChar) const
1118{ return arg(qlonglong(a), fieldWidth, base, fillChar); }
1119inline QString QString::arg(ushort a, int fieldWidth, int base, QChar fillChar) const
1120{ return arg(qulonglong(a), fieldWidth, base, fillChar); }
1121
1122inline QString QString::section(QChar asep, qsizetype astart, qsizetype aend, SectionFlags aflags) const
1123{ return section(QString(asep), astart, aend, aflags); }
1124
1125QT_WARNING_PUSH
1126QT_WARNING_DISABLE_MSVC(4127) // "conditional expression is constant"
1127QT_WARNING_DISABLE_INTEL(111) // "statement is unreachable"
1128
1129inline qsizetype QString::toWCharArray(wchar_t *array) const
1130{
1131 return qToStringViewIgnoringNull(*this).toWCharArray(array);
1132}
1133
1134qsizetype QStringView::toWCharArray(wchar_t *array) const
1135{
1136 if (sizeof(wchar_t) == sizeof(QChar)) {
1137 if (auto src = data())
1138 memcpy(array, src, sizeof(QChar) * size());
1139 return size();
1140 } else {
1141 return QString::toUcs4_helper(reinterpret_cast<const ushort *>(data()), size(),
1142 reinterpret_cast<uint *>(array));
1143 }
1144}
1145
1146QT_WARNING_POP
1147
1148inline QString QString::fromWCharArray(const wchar_t *string, qsizetype size)
1149{
1150 return sizeof(wchar_t) == sizeof(QChar) ? fromUtf16(reinterpret_cast<const char16_t *>(string), size)
1151 : fromUcs4(reinterpret_cast<const char32_t *>(string), size);
1152}
1153
1154inline constexpr QString::QString() noexcept {}
1155inline QString::~QString() {}
1156
1157inline void QString::reserve(qsizetype asize)
1158{
1159 if (d->needsDetach() || asize >= capacity() - d.freeSpaceAtBegin()) {
1160 reallocData(qMax(asize, size()), d->detachFlags() | Data::CapacityReserved);
1161 } else {
1162 d->setFlag(Data::CapacityReserved);
1163 }
1164}
1165
1166inline void QString::squeeze()
1167{
1168 if (!d.isMutable())
1169 return;
1170 if (d->needsDetach() || size() < capacity()) {
1171 reallocData(d.size, d->detachFlags() & ~Data::CapacityReserved);
1172 } else {
1173 d->clearFlag(Data::CapacityReserved);
1174 }
1175}
1176
1177inline QString &QString::setUtf16(const ushort *autf16, qsizetype asize)
1178{ return setUnicode(reinterpret_cast<const QChar *>(autf16), asize); }
1179inline QChar &QString::operator[](qsizetype i)
1180{ Q_ASSERT(i >= 0 && i < size()); return data()[i]; }
1181inline QChar &QString::front() { return operator[](0); }
1182inline QChar &QString::back() { return operator[](size() - 1); }
1183inline QString::iterator QString::begin()
1184{ detach(); return reinterpret_cast<QChar*>(d.data()); }
1185inline QString::const_iterator QString::begin() const
1186{ return reinterpret_cast<const QChar*>(d.data()); }
1187inline QString::const_iterator QString::cbegin() const
1188{ return reinterpret_cast<const QChar*>(d.data()); }
1189inline QString::const_iterator QString::constBegin() const
1190{ return reinterpret_cast<const QChar*>(d.data()); }
1191inline QString::iterator QString::end()
1192{ detach(); return reinterpret_cast<QChar*>(d.data() + d.size); }
1193inline QString::const_iterator QString::end() const
1194{ return reinterpret_cast<const QChar*>(d.data() + d.size); }
1195inline QString::const_iterator QString::cend() const
1196{ return reinterpret_cast<const QChar*>(d.data() + d.size); }
1197inline QString::const_iterator QString::constEnd() const
1198{ return reinterpret_cast<const QChar*>(d.data() + d.size); }
1199#if QT_STRINGVIEW_LEVEL < 2
1200inline bool QString::contains(const QString &s, Qt::CaseSensitivity cs) const
1201{ return indexOf(s, 0, cs) != -1; }
1202#endif
1203inline bool QString::contains(QLatin1String s, Qt::CaseSensitivity cs) const
1204{ return indexOf(s, 0, cs) != -1; }
1205inline bool QString::contains(QChar c, Qt::CaseSensitivity cs) const
1206{ return indexOf(c, 0, cs) != -1; }
1207inline bool QString::contains(QStringView s, Qt::CaseSensitivity cs) const noexcept
1208{ return indexOf(s, 0, cs) != -1; }
1209
1210inline bool operator==(QLatin1String s1, QLatin1String s2) noexcept
1211{ return s1.size() == s2.size() && (!s1.size() || !memcmp(s1.latin1(), s2.latin1(), s1.size())); }
1212inline bool operator!=(QLatin1String s1, QLatin1String s2) noexcept
1213{ return !operator==(s1, s2); }
1214inline bool operator<(QLatin1String s1, QLatin1String s2) noexcept
1215{
1216 const qsizetype len = qMin(s1.size(), s2.size());
1217 const int r = len ? memcmp(s1.latin1(), s2.latin1(), len) : 0;
1218 return r < 0 || (r == 0 && s1.size() < s2.size());
1219}
1220inline bool operator>(QLatin1String s1, QLatin1String s2) noexcept
1221{ return operator<(s2, s1); }
1222inline bool operator<=(QLatin1String s1, QLatin1String s2) noexcept
1223{ return !operator>(s1, s2); }
1224inline bool operator>=(QLatin1String s1, QLatin1String s2) noexcept
1225{ return !operator<(s1, s2); }
1226
1227inline bool QLatin1String::operator==(const QString &s) const noexcept
1228{ return s == *this; }
1229inline bool QLatin1String::operator!=(const QString &s) const noexcept
1230{ return s != *this; }
1231inline bool QLatin1String::operator>(const QString &s) const noexcept
1232{ return s < *this; }
1233inline bool QLatin1String::operator<(const QString &s) const noexcept
1234{ return s > *this; }
1235inline bool QLatin1String::operator>=(const QString &s) const noexcept
1236{ return s <= *this; }
1237inline bool QLatin1String::operator<=(const QString &s) const noexcept
1238{ return s >= *this; }
1239
1240#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
1241inline bool QString::operator==(const char *s) const
1242{ return QString::compare_helper(constData(), size(), s, -1) == 0; }
1243inline bool QString::operator!=(const char *s) const
1244{ return QString::compare_helper(constData(), size(), s, -1) != 0; }
1245inline bool QString::operator<(const char *s) const
1246{ return QString::compare_helper(constData(), size(), s, -1) < 0; }
1247inline bool QString::operator>(const char *s) const
1248{ return QString::compare_helper(constData(), size(), s, -1) > 0; }
1249inline bool QString::operator<=(const char *s) const
1250{ return QString::compare_helper(constData(), size(), s, -1) <= 0; }
1251inline bool QString::operator>=(const char *s) const
1252{ return QString::compare_helper(constData(), size(), s, -1) >= 0; }
1253
1254QT_ASCII_CAST_WARN inline bool operator==(const char *s1, const QString &s2)
1255{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) == 0; }
1256QT_ASCII_CAST_WARN inline bool operator!=(const char *s1, const QString &s2)
1257{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) != 0; }
1258QT_ASCII_CAST_WARN inline bool operator<(const char *s1, const QString &s2)
1259{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) > 0; }
1260QT_ASCII_CAST_WARN inline bool operator>(const char *s1, const QString &s2)
1261{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) < 0; }
1262QT_ASCII_CAST_WARN inline bool operator<=(const char *s1, const QString &s2)
1263{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) >= 0; }
1264QT_ASCII_CAST_WARN inline bool operator>=(const char *s1, const QString &s2)
1265{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) <= 0; }
1266
1267QT_ASCII_CAST_WARN inline bool operator==(const char *s1, QLatin1String s2)
1268{ return QString::fromUtf8(s1) == s2; }
1269QT_ASCII_CAST_WARN inline bool operator!=(const char *s1, QLatin1String s2)
1270{ return QString::fromUtf8(s1) != s2; }
1271QT_ASCII_CAST_WARN inline bool operator<(const char *s1, QLatin1String s2)
1272{ return (QString::fromUtf8(s1) < s2); }
1273QT_ASCII_CAST_WARN inline bool operator>(const char *s1, QLatin1String s2)
1274{ return (QString::fromUtf8(s1) > s2); }
1275QT_ASCII_CAST_WARN inline bool operator<=(const char *s1, QLatin1String s2)
1276{ return (QString::fromUtf8(s1) <= s2); }
1277QT_ASCII_CAST_WARN inline bool operator>=(const char *s1, QLatin1String s2)
1278{ return (QString::fromUtf8(s1) >= s2); }
1279
1280QT_ASCII_CAST_WARN inline bool QLatin1String::operator==(const char *s) const
1281{ return QString::fromUtf8(s) == *this; }
1282QT_ASCII_CAST_WARN inline bool QLatin1String::operator!=(const char *s) const
1283{ return QString::fromUtf8(s) != *this; }
1284QT_ASCII_CAST_WARN inline bool QLatin1String::operator<(const char *s) const
1285{ return QString::fromUtf8(s) > *this; }
1286QT_ASCII_CAST_WARN inline bool QLatin1String::operator>(const char *s) const
1287{ return QString::fromUtf8(s) < *this; }
1288QT_ASCII_CAST_WARN inline bool QLatin1String::operator<=(const char *s) const
1289{ return QString::fromUtf8(s) >= *this; }
1290QT_ASCII_CAST_WARN inline bool QLatin1String::operator>=(const char *s) const
1291{ return QString::fromUtf8(s) <= *this; }
1292
1293QT_ASCII_CAST_WARN inline bool QLatin1String::operator==(const QByteArray &s) const
1294{ return QString::fromUtf8(s) == *this; }
1295QT_ASCII_CAST_WARN inline bool QLatin1String::operator!=(const QByteArray &s) const
1296{ return QString::fromUtf8(s) != *this; }
1297QT_ASCII_CAST_WARN inline bool QLatin1String::operator<(const QByteArray &s) const
1298{ return QString::fromUtf8(s) > *this; }
1299QT_ASCII_CAST_WARN inline bool QLatin1String::operator>(const QByteArray &s) const
1300{ return QString::fromUtf8(s) < *this; }
1301QT_ASCII_CAST_WARN inline bool QLatin1String::operator<=(const QByteArray &s) const
1302{ return QString::fromUtf8(s) >= *this; }
1303QT_ASCII_CAST_WARN inline bool QLatin1String::operator>=(const QByteArray &s) const
1304{ return QString::fromUtf8(s) <= *this; }
1305
1306QT_ASCII_CAST_WARN inline bool QString::operator==(const QByteArray &s) const
1307{ return QString::compare_helper(constData(), size(), s.constData(), s.size()) == 0; }
1308QT_ASCII_CAST_WARN inline bool QString::operator!=(const QByteArray &s) const
1309{ return QString::compare_helper(constData(), size(), s.constData(), s.size()) != 0; }
1310QT_ASCII_CAST_WARN inline bool QString::operator<(const QByteArray &s) const
1311{ return QString::compare_helper(constData(), size(), s.constData(), s.size()) < 0; }
1312QT_ASCII_CAST_WARN inline bool QString::operator>(const QByteArray &s) const
1313{ return QString::compare_helper(constData(), size(), s.constData(), s.size()) > 0; }
1314QT_ASCII_CAST_WARN inline bool QString::operator<=(const QByteArray &s) const
1315{ return QString::compare_helper(constData(), size(), s.constData(), s.size()) <= 0; }
1316QT_ASCII_CAST_WARN inline bool QString::operator>=(const QByteArray &s) const
1317{ return QString::compare_helper(constData(), size(), s.constData(), s.size()) >= 0; }
1318
1319inline bool QByteArray::operator==(const QString &s) const
1320{ return QString::compare_helper(s.constData(), s.size(), constData(), qstrnlen(constData(), size())) == 0; }
1321inline bool QByteArray::operator!=(const QString &s) const
1322{ return QString::compare_helper(s.constData(), s.size(), constData(), qstrnlen(constData(), size())) != 0; }
1323inline bool QByteArray::operator<(const QString &s) const
1324{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) > 0; }
1325inline bool QByteArray::operator>(const QString &s) const
1326{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) < 0; }
1327inline bool QByteArray::operator<=(const QString &s) const
1328{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) >= 0; }
1329inline bool QByteArray::operator>=(const QString &s) const
1330{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) <= 0; }
1331#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
1332
1333#if !defined(QT_USE_FAST_OPERATOR_PLUS) && !defined(QT_USE_QSTRINGBUILDER)
1334inline const QString operator+(const QString &s1, const QString &s2)
1335{ QString t(s1); t += s2; return t; }
1336inline const QString operator+(const QString &s1, QChar s2)
1337{ QString t(s1); t += s2; return t; }
1338inline const QString operator+(QChar s1, const QString &s2)
1339{ QString t(s1); t += s2; return t; }
1340# if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
1341QT_ASCII_CAST_WARN inline const QString operator+(const QString &s1, const char *s2)
1342{ QString t(s1); t += QString::fromUtf8(s2); return t; }
1343QT_ASCII_CAST_WARN inline const QString operator+(const char *s1, const QString &s2)
1344{ QString t = QString::fromUtf8(s1); t += s2; return t; }
1345QT_ASCII_CAST_WARN inline const QString operator+(const QByteArray &ba, const QString &s)
1346{ QString t = QString::fromUtf8(ba); t += s; return t; }
1347QT_ASCII_CAST_WARN inline const QString operator+(const QString &s, const QByteArray &ba)
1348{ QString t(s); t += QString::fromUtf8(ba); return t; }
1349# endif // QT_NO_CAST_FROM_ASCII
1350#endif // QT_USE_QSTRINGBUILDER
1351
1352inline std::string QString::toStdString() const
1353{ return toUtf8().toStdString(); }
1354
1355inline QString QString::fromStdString(const std::string &s)
1356{ return fromUtf8(s.data(), int(s.size())); }
1357
1358inline std::wstring QString::toStdWString() const
1359{
1360 std::wstring str;
1361 str.resize(length());
1362#if __cplusplus >= 201703L
1363 str.resize(toWCharArray(str.data()));
1364#else
1365 if (length())
1366 str.resize(toWCharArray(&str.front()));
1367#endif
1368 return str;
1369}
1370
1371inline QString QString::fromStdWString(const std::wstring &s)
1372{ return fromWCharArray(s.data(), int(s.size())); }
1373
1374inline QString QString::fromStdU16String(const std::u16string &s)
1375{ return fromUtf16(s.data(), int(s.size())); }
1376
1377inline std::u16string QString::toStdU16String() const
1378{ return std::u16string(reinterpret_cast<const char16_t*>(utf16()), length()); }
1379
1380inline QString QString::fromStdU32String(const std::u32string &s)
1381{ return fromUcs4(s.data(), int(s.size())); }
1382
1383inline std::u32string QString::toStdU32String() const
1384{
1385 std::u32string u32str(length(), char32_t(0));
1386 int len = toUcs4_helper(reinterpret_cast<const ushort *>(constData()), length(),
1387 reinterpret_cast<uint*>(&u32str[0]));
1388 u32str.resize(len);
1389 return u32str;
1390}
1391
1392#if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE))
1393Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QString &);
1394Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QString &);
1395#endif
1396
1397Q_DECLARE_SHARED(QString)
1398Q_DECLARE_OPERATORS_FOR_FLAGS(QString::SectionFlags)
1399
1400inline int QString::compare(QStringView s, Qt::CaseSensitivity cs) const noexcept
1401{ return -s.compare(*this, cs); }
1402
1403// QChar <> QString
1404inline bool operator==(QChar lhs, const QString &rhs) noexcept
1405{ return rhs.size() == 1 && lhs == rhs.front(); }
1406inline bool operator< (QChar lhs, const QString &rhs) noexcept
1407{ return QString::compare_helper(&lhs, 1, rhs.data(), rhs.size()) < 0; }
1408inline bool operator> (QChar lhs, const QString &rhs) noexcept
1409{ return QString::compare_helper(&lhs, 1, rhs.data(), rhs.size()) > 0; }
1410
1411inline bool operator!=(QChar lhs, const QString &rhs) noexcept { return !(lhs == rhs); }
1412inline bool operator<=(QChar lhs, const QString &rhs) noexcept { return !(lhs > rhs); }
1413inline bool operator>=(QChar lhs, const QString &rhs) noexcept { return !(lhs < rhs); }
1414
1415inline bool operator==(const QString &lhs, QChar rhs) noexcept { return rhs == lhs; }
1416inline bool operator!=(const QString &lhs, QChar rhs) noexcept { return !(rhs == lhs); }
1417inline bool operator< (const QString &lhs, QChar rhs) noexcept { return rhs > lhs; }
1418inline bool operator> (const QString &lhs, QChar rhs) noexcept { return rhs < lhs; }
1419inline bool operator<=(const QString &lhs, QChar rhs) noexcept { return !(rhs < lhs); }
1420inline bool operator>=(const QString &lhs, QChar rhs) noexcept { return !(rhs > lhs); }
1421
1422// QChar <> QLatin1String
1423inline bool operator==(QChar lhs, QLatin1String rhs) noexcept
1424{ return rhs.size() == 1 && lhs == rhs.front(); }
1425inline bool operator< (QChar lhs, QLatin1String rhs) noexcept
1426{ return QString::compare_helper(&lhs, 1, rhs) < 0; }
1427inline bool operator> (QChar lhs, QLatin1String rhs) noexcept
1428{ return QString::compare_helper(&lhs, 1, rhs) > 0; }
1429
1430inline bool operator!=(QChar lhs, QLatin1String rhs) noexcept { return !(lhs == rhs); }
1431inline bool operator<=(QChar lhs, QLatin1String rhs) noexcept { return !(lhs > rhs); }
1432inline bool operator>=(QChar lhs, QLatin1String rhs) noexcept { return !(lhs < rhs); }
1433
1434inline bool operator==(QLatin1String lhs, QChar rhs) noexcept { return rhs == lhs; }
1435inline bool operator!=(QLatin1String lhs, QChar rhs) noexcept { return !(rhs == lhs); }
1436inline bool operator< (QLatin1String lhs, QChar rhs) noexcept { return rhs > lhs; }
1437inline bool operator> (QLatin1String lhs, QChar rhs) noexcept { return rhs < lhs; }
1438inline bool operator<=(QLatin1String lhs, QChar rhs) noexcept { return !(rhs < lhs); }
1439inline bool operator>=(QLatin1String lhs, QChar rhs) noexcept { return !(rhs > lhs); }
1440
1441// QStringView <> QStringView
1442inline bool operator==(QStringView lhs, QStringView rhs) noexcept { return lhs.size() == rhs.size() && QtPrivate::equalStrings(lhs, rhs); }
1443inline bool operator!=(QStringView lhs, QStringView rhs) noexcept { return !(lhs == rhs); }
1444inline bool operator< (QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) < 0; }
1445inline bool operator<=(QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) <= 0; }
1446inline bool operator> (QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) > 0; }
1447inline bool operator>=(QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) >= 0; }
1448
1449// QStringView <> QChar
1450inline bool operator==(QStringView lhs, QChar rhs) noexcept { return lhs == QStringView(&rhs, 1); }
1451inline bool operator!=(QStringView lhs, QChar rhs) noexcept { return lhs != QStringView(&rhs, 1); }
1452inline bool operator< (QStringView lhs, QChar rhs) noexcept { return lhs < QStringView(&rhs, 1); }
1453inline bool operator<=(QStringView lhs, QChar rhs) noexcept { return lhs <= QStringView(&rhs, 1); }
1454inline bool operator> (QStringView lhs, QChar rhs) noexcept { return lhs > QStringView(&rhs, 1); }
1455inline bool operator>=(QStringView lhs, QChar rhs) noexcept { return lhs >= QStringView(&rhs, 1); }
1456
1457inline bool operator==(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) == rhs; }
1458inline bool operator!=(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) != rhs; }
1459inline bool operator< (QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) < rhs; }
1460inline bool operator<=(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) <= rhs; }
1461inline bool operator> (QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) > rhs; }
1462inline bool operator>=(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) >= rhs; }
1463
1464// QStringView <> QLatin1String
1465inline bool operator==(QStringView lhs, QLatin1String rhs) noexcept { return lhs.size() == rhs.size() && QtPrivate::equalStrings(lhs, rhs); }
1466inline bool operator!=(QStringView lhs, QLatin1String rhs) noexcept { return !(lhs == rhs); }
1467inline bool operator< (QStringView lhs, QLatin1String rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) < 0; }
1468inline bool operator<=(QStringView lhs, QLatin1String rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) <= 0; }
1469inline bool operator> (QStringView lhs, QLatin1String rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) > 0; }
1470inline bool operator>=(QStringView lhs, QLatin1String rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) >= 0; }
1471
1472inline bool operator==(QLatin1String lhs, QStringView rhs) noexcept { return lhs.size() == rhs.size() && QtPrivate::equalStrings(lhs, rhs); }
1473inline bool operator!=(QLatin1String lhs, QStringView rhs) noexcept { return !(lhs == rhs); }
1474inline bool operator< (QLatin1String lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) < 0; }
1475inline bool operator<=(QLatin1String lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) <= 0; }
1476inline bool operator> (QLatin1String lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) > 0; }
1477inline bool operator>=(QLatin1String lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) >= 0; }
1478
1479inline int QString::localeAwareCompare(QStringView s) const
1480{ return localeAwareCompare_helper(constData(), length(), s.constData(), s.length()); }
1481inline int QString::localeAwareCompare(QStringView s1, QStringView s2)
1482{ return localeAwareCompare_helper(s1.constData(), s1.length(), s2.constData(), s2.length()); }
1483
1484namespace QtPrivate {
1485// used by qPrintable() and qUtf8Printable() macros
1486inline const QString &asString(const QString &s) { return s; }
1487inline QString &&asString(QString &&s) { return std::move(s); }
1488}
1489
1490//
1491// QStringView::arg() implementation
1492//
1493
1494namespace QtPrivate {
1495
1496struct ArgBase {
1497 enum Tag : uchar { L1, U8, U16 } tag;
1498};
1499
1500struct QStringViewArg : ArgBase {
1501 QStringView string;
1502 QStringViewArg() = default;
1503 constexpr explicit QStringViewArg(QStringView v) noexcept : ArgBase{U16}, string{v} {}
1504};
1505
1506struct QLatin1StringArg : ArgBase {
1507 QLatin1String string;
1508 QLatin1StringArg() = default;
1509 constexpr explicit QLatin1StringArg(QLatin1String v) noexcept : ArgBase{L1}, string{v} {}
1510};
1511
1512[[nodiscard]] Q_CORE_EXPORT QString argToQString(QStringView pattern, size_t n, const ArgBase **args);
1513[[nodiscard]] Q_CORE_EXPORT QString argToQString(QLatin1String pattern, size_t n, const ArgBase **args);
1514
1515template <typename StringView, typename...Args>
1516[[nodiscard]] Q_ALWAYS_INLINE QString argToQStringDispatch(StringView pattern, const Args &...args)
1517{
1518 const ArgBase *argBases[] = {&args..., /* avoid zero-sized array */ nullptr};
1519 return QtPrivate::argToQString(pattern, sizeof...(Args), argBases);
1520}
1521
1522 inline QStringViewArg qStringLikeToArg(const QString &s) noexcept { return QStringViewArg{qToStringViewIgnoringNull(s)}; }
1523constexpr inline QStringViewArg qStringLikeToArg(QStringView s) noexcept { return QStringViewArg{s}; }
1524 inline QStringViewArg qStringLikeToArg(const QChar &c) noexcept { return QStringViewArg{QStringView{&c, 1}}; }
1525constexpr inline QLatin1StringArg qStringLikeToArg(QLatin1String s) noexcept { return QLatin1StringArg{s}; }
1526
1527} // namespace QtPrivate
1528
1529template <typename...Args>
1530Q_ALWAYS_INLINE
1531QString QStringView::arg(Args &&...args) const
1532{
1533 return QtPrivate::argToQStringDispatch(*this, QtPrivate::qStringLikeToArg(args)...);
1534}
1535
1536template <typename...Args>
1537Q_ALWAYS_INLINE
1538QString QLatin1String::arg(Args &&...args) const
1539{
1540 return QtPrivate::argToQStringDispatch(*this, QtPrivate::qStringLikeToArg(args)...);
1541}
1542
1543QT_END_NAMESPACE
1544
1545#if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER)
1546#include <QtCore/qstringbuilder.h>
1547#endif
1548
1549#endif // QSTRING_H
1550