1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Copyright (C) 2016 Intel Corporation.
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#ifndef QSTRING_H
42#define QSTRING_H
43
44#if defined(QT_NO_CAST_FROM_ASCII) && defined(QT_RESTRICTED_CAST_FROM_ASCII)
45#error QT_NO_CAST_FROM_ASCII and QT_RESTRICTED_CAST_FROM_ASCII must not be defined at the same time
46#endif
47
48#include <QtCore/qchar.h>
49#include <QtCore/qbytearray.h>
50#include <QtCore/qrefcount.h>
51#include <QtCore/qnamespace.h>
52#include <QtCore/qstringliteral.h>
53#include <QtCore/qstringalgorithms.h>
54#include <QtCore/qstringview.h>
55
56#include <string>
57#include <iterator>
58
59#if defined(Q_OS_ANDROID) && !defined(ANDROID_HAS_WSTRING)
60// std::wstring is disabled on android's glibc, as bionic lacks certain features
61// that libstdc++ checks for (like mbcslen).
62namespace std
63{
64 typedef basic_string<wchar_t> wstring;
65}
66#endif
67
68#include <stdarg.h>
69
70#ifdef truncate
71#error qstring.h must be included before any header file that defines truncate
72#endif
73
74#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
75Q_FORWARD_DECLARE_CF_TYPE(CFString);
76Q_FORWARD_DECLARE_OBJC_CLASS(NSString);
77#endif
78
79QT_BEGIN_NAMESPACE
80
81class QCharRef;
82class QRegExp;
83class QRegularExpression;
84class QRegularExpressionMatch;
85class QString;
86class QStringList;
87class QTextCodec;
88class QStringRef;
89template <typename T> class QVector;
90
91class QLatin1String
92{
93public:
94 Q_DECL_CONSTEXPR inline QLatin1String() Q_DECL_NOTHROW : m_size(0), m_data(nullptr) {}
95 Q_DECL_CONSTEXPR inline explicit QLatin1String(const char *s) Q_DECL_NOTHROW : m_size(s ? int(strlen(s)) : 0), m_data(s) {}
96 Q_DECL_CONSTEXPR explicit QLatin1String(const char *f, const char *l)
97 : QLatin1String(f, int(l - f)) {}
98 Q_DECL_CONSTEXPR inline explicit QLatin1String(const char *s, int sz) Q_DECL_NOTHROW : m_size(sz), m_data(s) {}
99 inline explicit QLatin1String(const QByteArray &s) Q_DECL_NOTHROW : m_size(int(qstrnlen(s.constData(), s.size()))), m_data(s.constData()) {}
100
101 Q_DECL_CONSTEXPR const char *latin1() const Q_DECL_NOTHROW { return m_data; }
102 Q_DECL_CONSTEXPR int size() const Q_DECL_NOTHROW { return m_size; }
103 Q_DECL_CONSTEXPR const char *data() const Q_DECL_NOTHROW { return m_data; }
104
105 Q_DECL_CONSTEXPR bool isNull() const Q_DECL_NOTHROW { return !data(); }
106 Q_DECL_CONSTEXPR bool isEmpty() const Q_DECL_NOTHROW { return !size(); }
107
108 Q_DECL_CONSTEXPR QLatin1Char at(int i) const
109 { return Q_ASSERT(i >= 0), Q_ASSERT(i < size()), QLatin1Char(m_data[i]); }
110 Q_DECL_CONSTEXPR QLatin1Char operator[](int i) const { return at(i); }
111
112 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QLatin1Char front() const { return at(0); }
113 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QLatin1Char back() const { return at(size() - 1); }
114
115 Q_REQUIRED_RESULT bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW
116 { return QtPrivate::startsWith(*this, s, cs); }
117 Q_REQUIRED_RESULT bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW
118 { return QtPrivate::startsWith(*this, s, cs); }
119 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR bool startsWith(QChar c) const Q_DECL_NOTHROW
120 { return !isEmpty() && front() == c; }
121 Q_REQUIRED_RESULT inline bool startsWith(QChar c, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW
122 { return QtPrivate::startsWith(*this, QStringView(&c, 1), cs); }
123
124 Q_REQUIRED_RESULT bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW
125 { return QtPrivate::endsWith(*this, s, cs); }
126 Q_REQUIRED_RESULT bool endsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW
127 { return QtPrivate::endsWith(*this, s, cs); }
128 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR bool endsWith(QChar c) const Q_DECL_NOTHROW
129 { return !isEmpty() && back() == c; }
130 Q_REQUIRED_RESULT inline bool endsWith(QChar c, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW
131 { return QtPrivate::endsWith(*this, QStringView(&c, 1), cs); }
132
133 using value_type = const char;
134 using reference = value_type&;
135 using const_reference = reference;
136 using iterator = value_type*;
137 using const_iterator = iterator;
138 using difference_type = int; // violates Container concept requirements
139 using size_type = int; // violates Container concept requirements
140
141 Q_DECL_CONSTEXPR const_iterator begin() const Q_DECL_NOTHROW { return data(); }
142 Q_DECL_CONSTEXPR const_iterator cbegin() const Q_DECL_NOTHROW { return data(); }
143 Q_DECL_CONSTEXPR const_iterator end() const Q_DECL_NOTHROW { return data() + size(); }
144 Q_DECL_CONSTEXPR const_iterator cend() const Q_DECL_NOTHROW { return data() + size(); }
145
146 using reverse_iterator = std::reverse_iterator<iterator>;
147 using const_reverse_iterator = reverse_iterator;
148
149 const_reverse_iterator rbegin() const Q_DECL_NOTHROW { return const_reverse_iterator(end()); }
150 const_reverse_iterator crbegin() const Q_DECL_NOTHROW { return const_reverse_iterator(end()); }
151 const_reverse_iterator rend() const Q_DECL_NOTHROW { return const_reverse_iterator(begin()); }
152 const_reverse_iterator crend() const Q_DECL_NOTHROW { return const_reverse_iterator(begin()); }
153
154 Q_DECL_CONSTEXPR QLatin1String mid(int pos) const
155 { return Q_ASSERT(pos >= 0), Q_ASSERT(pos <= size()), QLatin1String(m_data + pos, m_size - pos); }
156 Q_DECL_CONSTEXPR QLatin1String mid(int pos, int n) const
157 { return Q_ASSERT(pos >= 0), Q_ASSERT(n >= 0), Q_ASSERT(pos + n <= size()), QLatin1String(m_data + pos, n); }
158 Q_DECL_CONSTEXPR QLatin1String left(int n) const
159 { return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QLatin1String(m_data, n); }
160 Q_DECL_CONSTEXPR QLatin1String right(int n) const
161 { return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QLatin1String(m_data + m_size - n, n); }
162 Q_REQUIRED_RESULT Q_DECL_CONSTEXPR QLatin1String chopped(int n) const
163 { return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QLatin1String(m_data, m_size - n); }
164
165 Q_DECL_RELAXED_CONSTEXPR void chop(int n)
166 { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size -= n; }
167 Q_DECL_RELAXED_CONSTEXPR void truncate(int n)
168 { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size = n; }
169
170 Q_REQUIRED_RESULT QLatin1String trimmed() const Q_DECL_NOTHROW { return QtPrivate::trimmed(*this); }
171
172 inline bool operator==(const QString &s) const Q_DECL_NOTHROW;
173 inline bool operator!=(const QString &s) const Q_DECL_NOTHROW;
174 inline bool operator>(const QString &s) const Q_DECL_NOTHROW;
175 inline bool operator<(const QString &s) const Q_DECL_NOTHROW;
176 inline bool operator>=(const QString &s) const Q_DECL_NOTHROW;
177 inline bool operator<=(const QString &s) const Q_DECL_NOTHROW;
178
179#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
180 inline QT_ASCII_CAST_WARN bool operator==(const char *s) const;
181 inline QT_ASCII_CAST_WARN bool operator!=(const char *s) const;
182 inline QT_ASCII_CAST_WARN bool operator<(const char *s) const;
183 inline QT_ASCII_CAST_WARN bool operator>(const char *s) const;
184 inline QT_ASCII_CAST_WARN bool operator<=(const char *s) const;
185 inline QT_ASCII_CAST_WARN bool operator>=(const char *s) const;
186
187 inline QT_ASCII_CAST_WARN bool operator==(const QByteArray &s) const;
188 inline QT_ASCII_CAST_WARN bool operator!=(const QByteArray &s) const;
189 inline QT_ASCII_CAST_WARN bool operator<(const QByteArray &s) const;
190 inline QT_ASCII_CAST_WARN bool operator>(const QByteArray &s) const;
191 inline QT_ASCII_CAST_WARN bool operator<=(const QByteArray &s) const;
192 inline QT_ASCII_CAST_WARN bool operator>=(const QByteArray &s) const;
193#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
194
195private:
196 int m_size;
197 const char *m_data;
198};
199Q_DECLARE_TYPEINFO(QLatin1String, Q_MOVABLE_TYPE);
200
201// Qt 4.x compatibility
202typedef QLatin1String QLatin1Literal;
203
204//
205// QLatin1String inline implementations
206//
207inline bool QtPrivate::isLatin1(QLatin1String) Q_DECL_NOTHROW
208{ return true; }
209
210//
211// QStringView members that require QLatin1String:
212//
213bool QStringView::startsWith(QLatin1String s, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW
214{ return QtPrivate::startsWith(*this, s, cs); }
215bool QStringView::endsWith(QLatin1String s, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW
216{ return QtPrivate::endsWith(*this, s, cs); }
217
218class Q_CORE_EXPORT QString
219{
220public:
221 typedef QStringData Data;
222
223 inline QString() Q_DECL_NOTHROW;
224 explicit QString(const QChar *unicode, int size = -1);
225 QString(QChar c);
226 QString(int size, QChar c);
227 inline QString(QLatin1String latin1);
228 inline QString(const QString &) Q_DECL_NOTHROW;
229 inline ~QString();
230 QString &operator=(QChar c);
231 QString &operator=(const QString &) Q_DECL_NOTHROW;
232 QString &operator=(QLatin1String latin1);
233#ifdef Q_COMPILER_RVALUE_REFS
234 inline QString(QString && other) Q_DECL_NOTHROW : d(other.d) { other.d = Data::sharedNull(); }
235 inline QString &operator=(QString &&other) Q_DECL_NOTHROW
236 { qSwap(d, other.d); return *this; }
237#endif
238 inline void swap(QString &other) Q_DECL_NOTHROW { qSwap(d, other.d); }
239 inline int size() const { return d->size; }
240 inline int count() const { return d->size; }
241 inline int length() const;
242 inline bool isEmpty() const;
243 void resize(int size);
244 void resize(int size, QChar fillChar);
245
246 QString &fill(QChar c, int size = -1);
247 void truncate(int pos);
248 void chop(int n);
249
250 int capacity() const;
251 inline void reserve(int size);
252 inline void squeeze();
253
254 inline const QChar *unicode() const;
255 inline QChar *data();
256 inline const QChar *data() const;
257 inline const QChar *constData() const;
258
259 inline void detach();
260 inline bool isDetached() const;
261 inline bool isSharedWith(const QString &other) const { return d == other.d; }
262 void clear();
263
264 inline const QChar at(int i) const;
265 const QChar operator[](int i) const;
266 QCharRef operator[](int i);
267 const QChar operator[](uint i) const;
268 QCharRef operator[](uint i);
269
270 Q_REQUIRED_RESULT inline QChar front() const { return at(0); }
271 Q_REQUIRED_RESULT inline QCharRef front();
272 Q_REQUIRED_RESULT inline QChar back() const { return at(size() - 1); }
273 Q_REQUIRED_RESULT inline QCharRef back();
274
275 Q_REQUIRED_RESULT QString arg(qlonglong a, int fieldwidth=0, int base=10,
276 QChar fillChar = QLatin1Char(' ')) const;
277 Q_REQUIRED_RESULT QString arg(qulonglong a, int fieldwidth=0, int base=10,
278 QChar fillChar = QLatin1Char(' ')) const;
279 Q_REQUIRED_RESULT QString arg(long a, int fieldwidth=0, int base=10,
280 QChar fillChar = QLatin1Char(' ')) const;
281 Q_REQUIRED_RESULT QString arg(ulong a, int fieldwidth=0, int base=10,
282 QChar fillChar = QLatin1Char(' ')) const;
283 Q_REQUIRED_RESULT QString arg(int a, int fieldWidth = 0, int base = 10,
284 QChar fillChar = QLatin1Char(' ')) const;
285 Q_REQUIRED_RESULT QString arg(uint a, int fieldWidth = 0, int base = 10,
286 QChar fillChar = QLatin1Char(' ')) const;
287 Q_REQUIRED_RESULT QString arg(short a, int fieldWidth = 0, int base = 10,
288 QChar fillChar = QLatin1Char(' ')) const;
289 Q_REQUIRED_RESULT QString arg(ushort a, int fieldWidth = 0, int base = 10,
290 QChar fillChar = QLatin1Char(' ')) const;
291 Q_REQUIRED_RESULT QString arg(double a, int fieldWidth = 0, char fmt = 'g', int prec = -1,
292 QChar fillChar = QLatin1Char(' ')) const;
293 Q_REQUIRED_RESULT QString arg(char a, int fieldWidth = 0,
294 QChar fillChar = QLatin1Char(' ')) const;
295 Q_REQUIRED_RESULT QString arg(QChar a, int fieldWidth = 0,
296 QChar fillChar = QLatin1Char(' ')) const;
297#if QT_STRINGVIEW_LEVEL < 2
298 Q_REQUIRED_RESULT QString arg(const QString &a, int fieldWidth = 0,
299 QChar fillChar = QLatin1Char(' ')) const;
300#endif
301 Q_REQUIRED_RESULT QString arg(QStringView a, int fieldWidth = 0,
302 QChar fillChar = QLatin1Char(' ')) const;
303 Q_REQUIRED_RESULT QString arg(QLatin1String a, int fieldWidth = 0,
304 QChar fillChar = QLatin1Char(' ')) const;
305 Q_REQUIRED_RESULT QString arg(const QString &a1, const QString &a2) const;
306 Q_REQUIRED_RESULT QString arg(const QString &a1, const QString &a2, const QString &a3) const;
307 Q_REQUIRED_RESULT QString arg(const QString &a1, const QString &a2, const QString &a3,
308 const QString &a4) const;
309 Q_REQUIRED_RESULT QString arg(const QString &a1, const QString &a2, const QString &a3,
310 const QString &a4, const QString &a5) const;
311 Q_REQUIRED_RESULT QString arg(const QString &a1, const QString &a2, const QString &a3,
312 const QString &a4, const QString &a5, const QString &a6) const;
313 Q_REQUIRED_RESULT QString arg(const QString &a1, const QString &a2, const QString &a3,
314 const QString &a4, const QString &a5, const QString &a6,
315 const QString &a7) const;
316 Q_REQUIRED_RESULT QString arg(const QString &a1, const QString &a2, const QString &a3,
317 const QString &a4, const QString &a5, const QString &a6,
318 const QString &a7, const QString &a8) const;
319 Q_REQUIRED_RESULT QString arg(const QString &a1, const QString &a2, const QString &a3,
320 const QString &a4, const QString &a5, const QString &a6,
321 const QString &a7, const QString &a8, const QString &a9) const;
322
323 QString &vsprintf(const char *format, va_list ap) Q_ATTRIBUTE_FORMAT_PRINTF(2, 0);
324 QString &sprintf(const char *format, ...) Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
325 static QString vasprintf(const char *format, va_list ap) Q_ATTRIBUTE_FORMAT_PRINTF(1, 0);
326 static QString asprintf(const char *format, ...) Q_ATTRIBUTE_FORMAT_PRINTF(1, 2);
327
328 int indexOf(QChar c, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
329 int indexOf(const QString &s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
330 int indexOf(QLatin1String s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
331 int indexOf(const QStringRef &s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
332 int lastIndexOf(QChar c, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
333 int lastIndexOf(const QString &s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
334 int lastIndexOf(QLatin1String s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
335 int lastIndexOf(const QStringRef &s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
336
337 inline bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
338 inline bool contains(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
339 inline bool contains(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
340 inline bool contains(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
341 int count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
342 int count(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
343 int count(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
344
345#ifndef QT_NO_REGEXP
346 int indexOf(const QRegExp &, int from = 0) const;
347 int lastIndexOf(const QRegExp &, int from = -1) const;
348 inline bool contains(const QRegExp &rx) const { return indexOf(rx) != -1; }
349 int count(const QRegExp &) const;
350
351 int indexOf(QRegExp &, int from = 0) const;
352 int lastIndexOf(QRegExp &, int from = -1) const;
353 inline bool contains(QRegExp &rx) const { return indexOf(rx) != -1; }
354#endif
355
356#if QT_CONFIG(regularexpression)
357 int indexOf(const QRegularExpression &re, int from = 0) const;
358 int indexOf(const QRegularExpression &re, int from, QRegularExpressionMatch *rmatch) const; // ### Qt 6: merge overloads
359 int lastIndexOf(const QRegularExpression &re, int from = -1) const;
360 int lastIndexOf(const QRegularExpression &re, int from, QRegularExpressionMatch *rmatch) const; // ### Qt 6: merge overloads
361 bool contains(const QRegularExpression &re) const;
362 bool contains(const QRegularExpression &re, QRegularExpressionMatch *match) const; // ### Qt 6: merge overloads
363 int count(const QRegularExpression &re) const;
364#endif
365
366 enum SectionFlag {
367 SectionDefault = 0x00,
368 SectionSkipEmpty = 0x01,
369 SectionIncludeLeadingSep = 0x02,
370 SectionIncludeTrailingSep = 0x04,
371 SectionCaseInsensitiveSeps = 0x08
372 };
373 Q_DECLARE_FLAGS(SectionFlags, SectionFlag)
374
375 QString section(QChar sep, int start, int end = -1, SectionFlags flags = SectionDefault) const;
376 QString section(const QString &in_sep, int start, int end = -1, SectionFlags flags = SectionDefault) const;
377#ifndef QT_NO_REGEXP
378 QString section(const QRegExp &reg, int start, int end = -1, SectionFlags flags = SectionDefault) const;
379#endif
380#if QT_CONFIG(regularexpression)
381 QString section(const QRegularExpression &re, int start, int end = -1, SectionFlags flags = SectionDefault) const;
382#endif
383 Q_REQUIRED_RESULT QString left(int n) const;
384 Q_REQUIRED_RESULT QString right(int n) const;
385 Q_REQUIRED_RESULT QString mid(int position, int n = -1) const;
386 Q_REQUIRED_RESULT QString chopped(int n) const
387 { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return left(size() - n); }
388
389
390 Q_REQUIRED_RESULT QStringRef leftRef(int n) const;
391 Q_REQUIRED_RESULT QStringRef rightRef(int n) const;
392 Q_REQUIRED_RESULT QStringRef midRef(int position, int n = -1) const;
393
394#if QT_STRINGVIEW_LEVEL < 2
395 bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
396 bool startsWith(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
397#endif
398 Q_REQUIRED_RESULT bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW
399 { return QtPrivate::startsWith(*this, s, cs); }
400 bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
401 bool startsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
402
403#if QT_STRINGVIEW_LEVEL < 2
404 bool endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
405 bool endsWith(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
406#endif
407 Q_REQUIRED_RESULT bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW
408 { return QtPrivate::endsWith(*this, s, cs); }
409 bool endsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
410 bool endsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
411
412 bool isUpper() const;
413 bool isLower() const;
414
415 Q_REQUIRED_RESULT QString leftJustified(int width, QChar fill = QLatin1Char(' '), bool trunc = false) const;
416 Q_REQUIRED_RESULT QString rightJustified(int width, QChar fill = QLatin1Char(' '), bool trunc = false) const;
417
418#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QSTRING_COMPAT_CPP) && !defined(Q_CLANG_QDOC)
419# if defined(Q_CC_GNU) && !defined(Q_CC_CLANG) && !defined(Q_CC_INTEL) && !QT_HAS_CPP_ATTRIBUTE(nodiscard)
420 // required due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61941
421# pragma push_macro("Q_REQUIRED_RESULT")
422# undef Q_REQUIRED_RESULT
423# define Q_REQUIRED_RESULT
424# define Q_REQUIRED_RESULT_pushed
425# endif
426 Q_REQUIRED_RESULT QString toLower() const &
427 { return toLower_helper(*this); }
428 Q_REQUIRED_RESULT QString toLower() &&
429 { return toLower_helper(*this); }
430 Q_REQUIRED_RESULT QString toUpper() const &
431 { return toUpper_helper(*this); }
432 Q_REQUIRED_RESULT QString toUpper() &&
433 { return toUpper_helper(*this); }
434 Q_REQUIRED_RESULT QString toCaseFolded() const &
435 { return toCaseFolded_helper(*this); }
436 Q_REQUIRED_RESULT QString toCaseFolded() &&
437 { return toCaseFolded_helper(*this); }
438 Q_REQUIRED_RESULT QString trimmed() const &
439 { return trimmed_helper(*this); }
440 Q_REQUIRED_RESULT QString trimmed() &&
441 { return trimmed_helper(*this); }
442 Q_REQUIRED_RESULT QString simplified() const &
443 { return simplified_helper(*this); }
444 Q_REQUIRED_RESULT QString simplified() &&
445 { return simplified_helper(*this); }
446# ifdef Q_REQUIRED_RESULT_pushed
447# pragma pop_macro("Q_REQUIRED_RESULT")
448# endif
449#else
450 Q_REQUIRED_RESULT QString toLower() const;
451 Q_REQUIRED_RESULT QString toUpper() const;
452 Q_REQUIRED_RESULT QString toCaseFolded() const;
453 Q_REQUIRED_RESULT QString trimmed() const;
454 Q_REQUIRED_RESULT QString simplified() const;
455#endif
456 Q_REQUIRED_RESULT QString toHtmlEscaped() const;
457
458 QString &insert(int i, QChar c);
459 QString &insert(int i, const QChar *uc, int len);
460 inline QString &insert(int i, const QString &s) { return insert(i, s.constData(), s.length()); }
461 inline QString &insert(int i, const QStringRef &s);
462 QString &insert(int i, QLatin1String s);
463 QString &append(QChar c);
464 QString &append(const QChar *uc, int len);
465 QString &append(const QString &s);
466 QString &append(const QStringRef &s);
467 QString &append(QLatin1String s);
468 inline QString &prepend(QChar c) { return insert(0, c); }
469 inline QString &prepend(const QChar *uc, int len) { return insert(0, uc, len); }
470 inline QString &prepend(const QString &s) { return insert(0, s); }
471 inline QString &prepend(const QStringRef &s) { return insert(0, s); }
472 inline QString &prepend(QLatin1String s) { return insert(0, s); }
473
474 inline QString &operator+=(QChar c) {
475 if (d->ref.isShared() || uint(d->size) + 2u > d->alloc)
476 reallocData(uint(d->size) + 2u, true);
477 d->data()[d->size++] = c.unicode();
478 d->data()[d->size] = '\0';
479 return *this;
480 }
481
482 inline QString &operator+=(QChar::SpecialCharacter c) { return append(QChar(c)); }
483 inline QString &operator+=(const QString &s) { return append(s); }
484 inline QString &operator+=(const QStringRef &s) { return append(s); }
485 inline QString &operator+=(QLatin1String s) { return append(s); }
486
487 QString &remove(int i, int len);
488 QString &remove(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive);
489 QString &remove(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive);
490 QString &remove(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive);
491 QString &replace(int i, int len, QChar after);
492 QString &replace(int i, int len, const QChar *s, int slen);
493 QString &replace(int i, int len, const QString &after);
494 QString &replace(QChar before, QChar after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
495 QString &replace(const QChar *before, int blen, const QChar *after, int alen, Qt::CaseSensitivity cs = Qt::CaseSensitive);
496 QString &replace(QLatin1String before, QLatin1String after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
497 QString &replace(QLatin1String before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
498 QString &replace(const QString &before, QLatin1String after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
499 QString &replace(const QString &before, const QString &after,
500 Qt::CaseSensitivity cs = Qt::CaseSensitive);
501 QString &replace(QChar c, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
502 QString &replace(QChar c, QLatin1String after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
503#ifndef QT_NO_REGEXP
504 QString &replace(const QRegExp &rx, const QString &after);
505 inline QString &remove(const QRegExp &rx)
506 { return replace(rx, QString()); }
507#endif
508#if QT_CONFIG(regularexpression)
509 QString &replace(const QRegularExpression &re, const QString &after);
510 inline QString &remove(const QRegularExpression &re)
511 { return replace(re, QString()); }
512#endif
513
514 enum SplitBehavior { KeepEmptyParts, SkipEmptyParts };
515
516 Q_REQUIRED_RESULT QStringList split(const QString &sep, SplitBehavior behavior = KeepEmptyParts,
517 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
518 Q_REQUIRED_RESULT QVector<QStringRef> splitRef(const QString &sep, SplitBehavior behavior = KeepEmptyParts,
519 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
520 Q_REQUIRED_RESULT QStringList split(QChar sep, SplitBehavior behavior = KeepEmptyParts,
521 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
522 Q_REQUIRED_RESULT QVector<QStringRef> splitRef(QChar sep, SplitBehavior behavior = KeepEmptyParts,
523 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
524#ifndef QT_NO_REGEXP
525 Q_REQUIRED_RESULT QStringList split(const QRegExp &sep, SplitBehavior behavior = KeepEmptyParts) const;
526 Q_REQUIRED_RESULT QVector<QStringRef> splitRef(const QRegExp &sep, SplitBehavior behavior = KeepEmptyParts) const;
527#endif
528#if QT_CONFIG(regularexpression)
529 Q_REQUIRED_RESULT QStringList split(const QRegularExpression &sep, SplitBehavior behavior = KeepEmptyParts) const;
530 Q_REQUIRED_RESULT QVector<QStringRef> splitRef(const QRegularExpression &sep, SplitBehavior behavior = KeepEmptyParts) const;
531#endif
532 enum NormalizationForm {
533 NormalizationForm_D,
534 NormalizationForm_C,
535 NormalizationForm_KD,
536 NormalizationForm_KC
537 };
538 Q_REQUIRED_RESULT QString normalized(NormalizationForm mode, QChar::UnicodeVersion version = QChar::Unicode_Unassigned) const;
539
540 Q_REQUIRED_RESULT QString repeated(int times) const;
541
542 const ushort *utf16() const;
543
544#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QSTRING_COMPAT_CPP) && !defined(Q_CLANG_QDOC)
545 Q_REQUIRED_RESULT QByteArray toLatin1() const &
546 { return toLatin1_helper(*this); }
547 Q_REQUIRED_RESULT QByteArray toLatin1() &&
548 { return toLatin1_helper_inplace(*this); }
549 Q_REQUIRED_RESULT QByteArray toUtf8() const &
550 { return toUtf8_helper(*this); }
551 Q_REQUIRED_RESULT QByteArray toUtf8() &&
552 { return toUtf8_helper(*this); }
553 Q_REQUIRED_RESULT QByteArray toLocal8Bit() const &
554 { return toLocal8Bit_helper(isNull() ? nullptr : constData(), size()); }
555 Q_REQUIRED_RESULT QByteArray toLocal8Bit() &&
556 { return toLocal8Bit_helper(isNull() ? nullptr : constData(), size()); }
557#else
558 Q_REQUIRED_RESULT QByteArray toLatin1() const;
559 Q_REQUIRED_RESULT QByteArray toUtf8() const;
560 Q_REQUIRED_RESULT QByteArray toLocal8Bit() const;
561#endif
562 Q_REQUIRED_RESULT QVector<uint> toUcs4() const;
563
564 // note - this are all inline so we can benefit from strlen() compile time optimizations
565 static inline QString fromLatin1(const char *str, int size = -1)
566 {
567 QStringDataPtr dataPtr = { fromLatin1_helper(str, (str && size == -1) ? int(strlen(str)) : size) };
568 return QString(dataPtr);
569 }
570 static inline QString fromUtf8(const char *str, int size = -1)
571 {
572 return fromUtf8_helper(str, (str && size == -1) ? int(strlen(str)) : size);
573 }
574 static inline QString fromLocal8Bit(const char *str, int size = -1)
575 {
576 return fromLocal8Bit_helper(str, (str && size == -1) ? int(strlen(str)) : size);
577 }
578 static inline QString fromLatin1(const QByteArray &str)
579 { return str.isNull() ? QString() : fromLatin1(str.data(), qstrnlen(str.constData(), str.size())); }
580 static inline QString fromUtf8(const QByteArray &str)
581 { return str.isNull() ? QString() : fromUtf8(str.data(), qstrnlen(str.constData(), str.size())); }
582 static inline QString fromLocal8Bit(const QByteArray &str)
583 { return str.isNull() ? QString() : fromLocal8Bit(str.data(), qstrnlen(str.constData(), str.size())); }
584 static QString fromUtf16(const ushort *, int size = -1);
585 static QString fromUcs4(const uint *, int size = -1);
586 static QString fromRawData(const QChar *, int size);
587
588#if defined(Q_COMPILER_UNICODE_STRINGS)
589 static QString fromUtf16(const char16_t *str, int size = -1)
590 { return fromUtf16(reinterpret_cast<const ushort *>(str), size); }
591 static QString fromUcs4(const char32_t *str, int size = -1)
592 { return fromUcs4(reinterpret_cast<const uint *>(str), size); }
593#endif
594
595#if QT_DEPRECATED_SINCE(5, 0)
596 QT_DEPRECATED static inline QString fromAscii(const char *str, int size = -1)
597 { return fromLatin1(str, size); }
598 QT_DEPRECATED static inline QString fromAscii(const QByteArray &str)
599 { return fromLatin1(str); }
600 Q_REQUIRED_RESULT QByteArray toAscii() const
601 { return toLatin1(); }
602#endif
603
604 inline int toWCharArray(wchar_t *array) const;
605 Q_REQUIRED_RESULT static inline QString fromWCharArray(const wchar_t *string, int size = -1);
606
607 QString &setRawData(const QChar *unicode, int size);
608 QString &setUnicode(const QChar *unicode, int size);
609 inline QString &setUtf16(const ushort *utf16, int size);
610
611 int compare(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW;
612 int compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW;
613
614 static inline int compare(const QString &s1, const QString &s2,
615 Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW
616 { return s1.compare(s2, cs); }
617
618 static inline int compare(const QString &s1, QLatin1String s2,
619 Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW
620 { return s1.compare(s2, cs); }
621 static inline int compare(QLatin1String s1, const QString &s2,
622 Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW
623 { return -s2.compare(s1, cs); }
624
625 inline int compare(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW;
626 static int compare(const QString &s1, const QStringRef &s2,
627 Qt::CaseSensitivity = Qt::CaseSensitive) Q_DECL_NOTHROW;
628
629 int localeAwareCompare(const QString& s) const;
630 static int localeAwareCompare(const QString& s1, const QString& s2)
631 { return s1.localeAwareCompare(s2); }
632
633 int localeAwareCompare(const QStringRef &s) const;
634 static int localeAwareCompare(const QString& s1, const QStringRef& s2);
635
636 // ### Qt6: make inline except for the long long versions
637 short toShort(bool *ok=nullptr, int base=10) const;
638 ushort toUShort(bool *ok=nullptr, int base=10) const;
639 int toInt(bool *ok=nullptr, int base=10) const;
640 uint toUInt(bool *ok=nullptr, int base=10) const;
641 long toLong(bool *ok=nullptr, int base=10) const;
642 ulong toULong(bool *ok=nullptr, int base=10) const;
643 qlonglong toLongLong(bool *ok=nullptr, int base=10) const;
644 qulonglong toULongLong(bool *ok=nullptr, int base=10) const;
645 float toFloat(bool *ok=nullptr) const;
646 double toDouble(bool *ok=nullptr) const;
647
648 QString &setNum(short, int base=10);
649 QString &setNum(ushort, int base=10);
650 QString &setNum(int, int base=10);
651 QString &setNum(uint, int base=10);
652 QString &setNum(long, int base=10);
653 QString &setNum(ulong, int base=10);
654 QString &setNum(qlonglong, int base=10);
655 QString &setNum(qulonglong, int base=10);
656 QString &setNum(float, char f='g', int prec=6);
657 QString &setNum(double, char f='g', int prec=6);
658
659 static QString number(int, int base=10);
660 static QString number(uint, int base=10);
661 static QString number(long, int base=10);
662 static QString number(ulong, int base=10);
663 static QString number(qlonglong, int base=10);
664 static QString number(qulonglong, int base=10);
665 static QString number(double, char f='g', int prec=6);
666
667 friend Q_CORE_EXPORT bool operator==(const QString &s1, const QString &s2) Q_DECL_NOTHROW;
668 friend Q_CORE_EXPORT bool operator<(const QString &s1, const QString &s2) Q_DECL_NOTHROW;
669 friend inline bool operator>(const QString &s1, const QString &s2) Q_DECL_NOTHROW { return s2 < s1; }
670 friend inline bool operator!=(const QString &s1, const QString &s2) Q_DECL_NOTHROW { return !(s1 == s2); }
671 friend inline bool operator<=(const QString &s1, const QString &s2) Q_DECL_NOTHROW { return !(s1 > s2); }
672 friend inline bool operator>=(const QString &s1, const QString &s2) Q_DECL_NOTHROW { return !(s1 < s2); }
673
674 bool operator==(QLatin1String s) const Q_DECL_NOTHROW;
675 bool operator<(QLatin1String s) const Q_DECL_NOTHROW;
676 bool operator>(QLatin1String s) const Q_DECL_NOTHROW;
677 inline bool operator!=(QLatin1String s) const Q_DECL_NOTHROW { return !operator==(s); }
678 inline bool operator<=(QLatin1String s) const Q_DECL_NOTHROW { return !operator>(s); }
679 inline bool operator>=(QLatin1String s) const Q_DECL_NOTHROW { return !operator<(s); }
680
681 // ASCII compatibility
682#if defined(QT_RESTRICTED_CAST_FROM_ASCII)
683 template <int N>
684 inline QString(const char (&ch)[N])
685 : d(fromAscii_helper(ch, N - 1))
686 {}
687 template <int N>
688 QString(char (&)[N]) = delete;
689 template <int N>
690 inline QString &operator=(const char (&ch)[N])
691 { return (*this = fromUtf8(ch, N - 1)); }
692 template <int N>
693 QString &operator=(char (&)[N]) = delete;
694#endif
695#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
696 inline QT_ASCII_CAST_WARN QString(const char *ch)
697 : d(fromAscii_helper(ch, ch ? int(strlen(ch)) : -1))
698 {}
699 inline QT_ASCII_CAST_WARN QString(const QByteArray &a)
700 : d(fromAscii_helper(a.constData(), qstrnlen(a.constData(), a.size())))
701 {}
702 inline QT_ASCII_CAST_WARN QString &operator=(const char *ch)
703 { return (*this = fromUtf8(ch)); }
704 inline QT_ASCII_CAST_WARN QString &operator=(const QByteArray &a)
705 { return (*this = fromUtf8(a)); }
706 inline QT_ASCII_CAST_WARN QString &operator=(char c)
707 { return (*this = QChar::fromLatin1(c)); }
708
709 // these are needed, so it compiles with STL support enabled
710 inline QT_ASCII_CAST_WARN QString &prepend(const char *s)
711 { return prepend(QString::fromUtf8(s)); }
712 inline QT_ASCII_CAST_WARN QString &prepend(const QByteArray &s)
713 { return prepend(QString::fromUtf8(s)); }
714 inline QT_ASCII_CAST_WARN QString &append(const char *s)
715 { return append(QString::fromUtf8(s)); }
716 inline QT_ASCII_CAST_WARN QString &append(const QByteArray &s)
717 { return append(QString::fromUtf8(s)); }
718 inline QT_ASCII_CAST_WARN QString &insert(int i, const char *s)
719 { return insert(i, QString::fromUtf8(s)); }
720 inline QT_ASCII_CAST_WARN QString &insert(int i, const QByteArray &s)
721 { return insert(i, QString::fromUtf8(s)); }
722 inline QT_ASCII_CAST_WARN QString &operator+=(const char *s)
723 { return append(QString::fromUtf8(s)); }
724 inline QT_ASCII_CAST_WARN QString &operator+=(const QByteArray &s)
725 { return append(QString::fromUtf8(s)); }
726 inline QT_ASCII_CAST_WARN QString &operator+=(char c)
727 { return append(QChar::fromLatin1(c)); }
728
729 inline QT_ASCII_CAST_WARN bool operator==(const char *s) const;
730 inline QT_ASCII_CAST_WARN bool operator!=(const char *s) const;
731 inline QT_ASCII_CAST_WARN bool operator<(const char *s) const;
732 inline QT_ASCII_CAST_WARN bool operator<=(const char *s) const;
733 inline QT_ASCII_CAST_WARN bool operator>(const char *s) const;
734 inline QT_ASCII_CAST_WARN bool operator>=(const char *s) const;
735
736 inline QT_ASCII_CAST_WARN bool operator==(const QByteArray &s) const;
737 inline QT_ASCII_CAST_WARN bool operator!=(const QByteArray &s) const;
738 inline QT_ASCII_CAST_WARN bool operator<(const QByteArray &s) const;
739 inline QT_ASCII_CAST_WARN bool operator>(const QByteArray &s) const;
740 inline QT_ASCII_CAST_WARN bool operator<=(const QByteArray &s) const;
741 inline QT_ASCII_CAST_WARN bool operator>=(const QByteArray &s) const;
742
743 friend inline QT_ASCII_CAST_WARN bool operator==(const char *s1, const QString &s2);
744 friend inline QT_ASCII_CAST_WARN bool operator!=(const char *s1, const QString &s2);
745 friend inline QT_ASCII_CAST_WARN bool operator<(const char *s1, const QString &s2);
746 friend inline QT_ASCII_CAST_WARN bool operator>(const char *s1, const QString &s2);
747 friend inline QT_ASCII_CAST_WARN bool operator<=(const char *s1, const QString &s2);
748 friend inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, const QString &s2);
749
750 friend inline QT_ASCII_CAST_WARN bool operator==(const char *s1, const QStringRef &s2);
751 friend inline QT_ASCII_CAST_WARN bool operator!=(const char *s1, const QStringRef &s2);
752 friend inline QT_ASCII_CAST_WARN bool operator<(const char *s1, const QStringRef &s2);
753 friend inline QT_ASCII_CAST_WARN bool operator>(const char *s1, const QStringRef &s2);
754 friend inline QT_ASCII_CAST_WARN bool operator<=(const char *s1, const QStringRef &s2);
755 friend inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, const QStringRef &s2);
756#endif
757
758 typedef QChar *iterator;
759 typedef const QChar *const_iterator;
760 typedef iterator Iterator;
761 typedef const_iterator ConstIterator;
762 typedef std::reverse_iterator<iterator> reverse_iterator;
763 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
764 inline iterator begin();
765 inline const_iterator begin() const;
766 inline const_iterator cbegin() const;
767 inline const_iterator constBegin() const;
768 inline iterator end();
769 inline const_iterator end() const;
770 inline const_iterator cend() const;
771 inline const_iterator constEnd() const;
772 reverse_iterator rbegin() { return reverse_iterator(end()); }
773 reverse_iterator rend() { return reverse_iterator(begin()); }
774 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
775 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
776 const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); }
777 const_reverse_iterator crend() const { return const_reverse_iterator(begin()); }
778
779 // STL compatibility
780 typedef int size_type;
781 typedef qptrdiff difference_type;
782 typedef const QChar & const_reference;
783 typedef QChar & reference;
784 typedef QChar *pointer;
785 typedef const QChar *const_pointer;
786 typedef QChar value_type;
787 inline void push_back(QChar c) { append(c); }
788 inline void push_back(const QString &s) { append(s); }
789 inline void push_front(QChar c) { prepend(c); }
790 inline void push_front(const QString &s) { prepend(s); }
791 void shrink_to_fit() { squeeze(); }
792
793 static inline QString fromStdString(const std::string &s);
794 inline std::string toStdString() const;
795 static inline QString fromStdWString(const std::wstring &s);
796 inline std::wstring toStdWString() const;
797
798#if defined(Q_STDLIB_UNICODE_STRINGS) || defined(Q_QDOC)
799 static inline QString fromStdU16String(const std::u16string &s);
800 inline std::u16string toStdU16String() const;
801 static inline QString fromStdU32String(const std::u32string &s);
802 inline std::u32string toStdU32String() const;
803#endif
804
805#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
806 static QString fromCFString(CFStringRef string);
807 CFStringRef toCFString() const Q_DECL_CF_RETURNS_RETAINED;
808 static QString fromNSString(const NSString *string);
809 NSString *toNSString() const Q_DECL_NS_RETURNS_AUTORELEASED;
810#endif
811 // compatibility
812#if QT_DEPRECATED_SINCE(5, 9)
813 struct Null { };
814 QT_DEPRECATED_X("use QString()")
815 static const Null null;
816 inline QString(const Null &): d(Data::sharedNull()) {}
817 inline QString &operator=(const Null &) { *this = QString(); return *this; }
818#endif
819 inline bool isNull() const { return d == Data::sharedNull(); }
820
821
822 bool isSimpleText() const;
823 bool isRightToLeft() const;
824
825 QString(int size, Qt::Initialization);
826 Q_DECL_CONSTEXPR inline QString(QStringDataPtr dd) : d(dd.ptr) {}
827
828private:
829#if defined(QT_NO_CAST_FROM_ASCII)
830 QString &operator+=(const char *s);
831 QString &operator+=(const QByteArray &s);
832 QString(const char *ch);
833 QString(const QByteArray &a);
834 QString &operator=(const char *ch);
835 QString &operator=(const QByteArray &a);
836#endif
837
838 Data *d;
839
840 friend inline bool operator==(QChar, const QString &) Q_DECL_NOTHROW;
841 friend inline bool operator< (QChar, const QString &) Q_DECL_NOTHROW;
842 friend inline bool operator> (QChar, const QString &) Q_DECL_NOTHROW;
843 friend inline bool operator==(QChar, const QStringRef &) Q_DECL_NOTHROW;
844 friend inline bool operator< (QChar, const QStringRef &) Q_DECL_NOTHROW;
845 friend inline bool operator> (QChar, const QStringRef &) Q_DECL_NOTHROW;
846 friend inline bool operator==(QChar, QLatin1String) Q_DECL_NOTHROW;
847 friend inline bool operator< (QChar, QLatin1String) Q_DECL_NOTHROW;
848 friend inline bool operator> (QChar, QLatin1String) Q_DECL_NOTHROW;
849
850 void reallocData(uint alloc, bool grow = false);
851#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
852 void expand(int i);
853#endif
854 QString multiArg(int numArgs, const QString **args) const;
855 static int compare_helper(const QChar *data1, int length1,
856 const QChar *data2, int length2,
857 Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW;
858 static int compare_helper(const QChar *data1, int length1,
859 const char *data2, int length2,
860 Qt::CaseSensitivity cs = Qt::CaseSensitive);
861 static int compare_helper(const QChar *data1, int length1,
862 QLatin1String s2,
863 Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW;
864 static int localeAwareCompare_helper(const QChar *data1, int length1,
865 const QChar *data2, int length2);
866 static QString toLower_helper(const QString &str);
867 static QString toLower_helper(QString &str);
868 static QString toUpper_helper(const QString &str);
869 static QString toUpper_helper(QString &str);
870 static QString toCaseFolded_helper(const QString &str);
871 static QString toCaseFolded_helper(QString &str);
872 static QString trimmed_helper(const QString &str);
873 static QString trimmed_helper(QString &str);
874 static QString simplified_helper(const QString &str);
875 static QString simplified_helper(QString &str);
876 static Data *fromLatin1_helper(const char *str, int size = -1);
877 static Data *fromAscii_helper(const char *str, int size = -1);
878 static QString fromUtf8_helper(const char *str, int size);
879 static QString fromLocal8Bit_helper(const char *, int size);
880 static QByteArray toLatin1_helper(const QString &);
881 static QByteArray toLatin1_helper(const QChar *data, int size);
882 static QByteArray toLatin1_helper_inplace(QString &);
883 static QByteArray toUtf8_helper(const QString &);
884 static QByteArray toLocal8Bit_helper(const QChar *data, int size);
885 static int toUcs4_helper(const ushort *uc, int length, uint *out);
886 static qlonglong toIntegral_helper(const QChar *data, int len, bool *ok, int base);
887 static qulonglong toIntegral_helper(const QChar *data, uint len, bool *ok, int base);
888 void replace_helper(uint *indices, int nIndices, int blen, const QChar *after, int alen);
889 friend class QCharRef;
890 friend class QTextCodec;
891 friend class QStringRef;
892 friend class QStringView;
893 friend class QByteArray;
894 friend class QCollator;
895 friend struct QAbstractConcatenable;
896
897 template <typename T> static
898 T toIntegral_helper(const QChar *data, int len, bool *ok, int base)
899 {
900 // ### Qt6: use std::conditional<std::is_unsigned<T>::value, qulonglong, qlonglong>::type
901 const bool isUnsigned = T(0) < T(-1);
902 typedef typename QtPrivate::QConditional<isUnsigned, qulonglong, qlonglong>::Type Int64;
903 typedef typename QtPrivate::QConditional<isUnsigned, uint, int>::Type Int32;
904
905 // we select the right overload by casting size() to int or uint
906 Int64 val = toIntegral_helper(data, Int32(len), ok, base);
907 if (T(val) != val) {
908 if (ok)
909 *ok = false;
910 val = 0;
911 }
912 return T(val);
913 }
914
915public:
916 typedef Data * DataPtr;
917 inline DataPtr &data_ptr() { return d; }
918};
919
920//
921// QStringView inline members that require QString:
922//
923QString QStringView::toString() const
924{ return Q_ASSERT(size() == length()), QString(data(), length()); }
925
926//
927// QString inline members
928//
929inline QString::QString(QLatin1String aLatin1) : d(fromLatin1_helper(aLatin1.latin1(), aLatin1.size()))
930{ }
931inline int QString::length() const
932{ return d->size; }
933inline const QChar QString::at(int i) const
934{ Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; }
935inline const QChar QString::operator[](int i) const
936{ Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; }
937inline const QChar QString::operator[](uint i) const
938{ Q_ASSERT(i < uint(size())); return d->data()[i]; }
939inline bool QString::isEmpty() const
940{ return d->size == 0; }
941inline const QChar *QString::unicode() const
942{ return reinterpret_cast<const QChar*>(d->data()); }
943inline const QChar *QString::data() const
944{ return reinterpret_cast<const QChar*>(d->data()); }
945inline QChar *QString::data()
946{ detach(); return reinterpret_cast<QChar*>(d->data()); }
947inline const QChar *QString::constData() const
948{ return reinterpret_cast<const QChar*>(d->data()); }
949inline void QString::detach()
950{ if (d->ref.isShared() || (d->offset != sizeof(QStringData))) reallocData(uint(d->size) + 1u); }
951inline bool QString::isDetached() const
952{ return !d->ref.isShared(); }
953inline void QString::clear()
954{ if (!isNull()) *this = QString(); }
955inline QString::QString(const QString &other) Q_DECL_NOTHROW : d(other.d)
956{ Q_ASSERT(&other != this); d->ref.ref(); }
957inline int QString::capacity() const
958{ return d->alloc ? d->alloc - 1 : 0; }
959inline QString &QString::setNum(short n, int base)
960{ return setNum(qlonglong(n), base); }
961inline QString &QString::setNum(ushort n, int base)
962{ return setNum(qulonglong(n), base); }
963inline QString &QString::setNum(int n, int base)
964{ return setNum(qlonglong(n), base); }
965inline QString &QString::setNum(uint n, int base)
966{ return setNum(qulonglong(n), base); }
967inline QString &QString::setNum(long n, int base)
968{ return setNum(qlonglong(n), base); }
969inline QString &QString::setNum(ulong n, int base)
970{ return setNum(qulonglong(n), base); }
971inline QString &QString::setNum(float n, char f, int prec)
972{ return setNum(double(n),f,prec); }
973inline QString QString::arg(int a, int fieldWidth, int base, QChar fillChar) const
974{ return arg(qlonglong(a), fieldWidth, base, fillChar); }
975inline QString QString::arg(uint a, int fieldWidth, int base, QChar fillChar) const
976{ return arg(qulonglong(a), fieldWidth, base, fillChar); }
977inline QString QString::arg(long a, int fieldWidth, int base, QChar fillChar) const
978{ return arg(qlonglong(a), fieldWidth, base, fillChar); }
979inline QString QString::arg(ulong a, int fieldWidth, int base, QChar fillChar) const
980{ return arg(qulonglong(a), fieldWidth, base, fillChar); }
981inline QString QString::arg(short a, int fieldWidth, int base, QChar fillChar) const
982{ return arg(qlonglong(a), fieldWidth, base, fillChar); }
983inline QString QString::arg(ushort a, int fieldWidth, int base, QChar fillChar) const
984{ return arg(qulonglong(a), fieldWidth, base, fillChar); }
985inline QString QString::arg(const QString &a1, const QString &a2) const
986{ const QString *args[2] = { &a1, &a2 }; return multiArg(2, args); }
987inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3) const
988{ const QString *args[3] = { &a1, &a2, &a3 }; return multiArg(3, args); }
989inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
990 const QString &a4) const
991{ const QString *args[4] = { &a1, &a2, &a3, &a4 }; return multiArg(4, args); }
992inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
993 const QString &a4, const QString &a5) const
994{ const QString *args[5] = { &a1, &a2, &a3, &a4, &a5 }; return multiArg(5, args); }
995inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
996 const QString &a4, const QString &a5, const QString &a6) const
997{ const QString *args[6] = { &a1, &a2, &a3, &a4, &a5, &a6 }; return multiArg(6, args); }
998inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
999 const QString &a4, const QString &a5, const QString &a6,
1000 const QString &a7) const
1001{ const QString *args[7] = { &a1, &a2, &a3, &a4, &a5, &a6, &a7 }; return multiArg(7, args); }
1002inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
1003 const QString &a4, const QString &a5, const QString &a6,
1004 const QString &a7, const QString &a8) const
1005{ const QString *args[8] = { &a1, &a2, &a3, &a4, &a5, &a6, &a7, &a8 }; return multiArg(8, args); }
1006inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
1007 const QString &a4, const QString &a5, const QString &a6,
1008 const QString &a7, const QString &a8, const QString &a9) const
1009{ const QString *args[9] = { &a1, &a2, &a3, &a4, &a5, &a6, &a7, &a8, &a9 }; return multiArg(9, args); }
1010
1011inline QString QString::section(QChar asep, int astart, int aend, SectionFlags aflags) const
1012{ return section(QString(asep), astart, aend, aflags); }
1013
1014QT_WARNING_PUSH
1015QT_WARNING_DISABLE_MSVC(4127) // "conditional expression is constant"
1016QT_WARNING_DISABLE_INTEL(111) // "statement is unreachable"
1017
1018inline int QString::toWCharArray(wchar_t *array) const
1019{
1020 if (sizeof(wchar_t) == sizeof(QChar)) {
1021 memcpy(array, d->data(), sizeof(QChar) * size());
1022 return size();
1023 } else {
1024 return toUcs4_helper(d->data(), size(), reinterpret_cast<uint *>(array));
1025 }
1026}
1027
1028QT_WARNING_POP
1029
1030inline QString QString::fromWCharArray(const wchar_t *string, int size)
1031{
1032 return sizeof(wchar_t) == sizeof(QChar) ? fromUtf16(reinterpret_cast<const ushort *>(string), size)
1033 : fromUcs4(reinterpret_cast<const uint *>(string), size);
1034}
1035
1036
1037class Q_CORE_EXPORT QCharRef {
1038 QString &s;
1039 int i;
1040 inline QCharRef(QString &str, int idx)
1041 : s(str),i(idx) {}
1042 friend class QString;
1043public:
1044
1045 // most QChar operations repeated here
1046
1047 // all this is not documented: We just say "like QChar" and let it be.
1048 inline operator QChar() const
1049 { return i < s.d->size ? s.d->data()[i] : 0; }
1050 inline QCharRef &operator=(QChar c)
1051 { if (i >= s.d->size) s.resize(i + 1, QLatin1Char(' ')); else s.detach();
1052 s.d->data()[i] = c.unicode(); return *this; }
1053
1054 // An operator= for each QChar cast constructors
1055#ifndef QT_NO_CAST_FROM_ASCII
1056 inline QT_ASCII_CAST_WARN QCharRef &operator=(char c)
1057 { return operator=(QChar::fromLatin1(c)); }
1058 inline QT_ASCII_CAST_WARN QCharRef &operator=(uchar c)
1059 { return operator=(QChar::fromLatin1(c)); }
1060#endif
1061 inline QCharRef &operator=(const QCharRef &c) { return operator=(QChar(c)); }
1062 inline QCharRef &operator=(ushort rc) { return operator=(QChar(rc)); }
1063 inline QCharRef &operator=(short rc) { return operator=(QChar(rc)); }
1064 inline QCharRef &operator=(uint rc) { return operator=(QChar(rc)); }
1065 inline QCharRef &operator=(int rc) { return operator=(QChar(rc)); }
1066
1067 // each function...
1068 inline bool isNull() const { return QChar(*this).isNull(); }
1069 inline bool isPrint() const { return QChar(*this).isPrint(); }
1070 inline bool isPunct() const { return QChar(*this).isPunct(); }
1071 inline bool isSpace() const { return QChar(*this).isSpace(); }
1072 inline bool isMark() const { return QChar(*this).isMark(); }
1073 inline bool isLetter() const { return QChar(*this).isLetter(); }
1074 inline bool isNumber() const { return QChar(*this).isNumber(); }
1075 inline bool isLetterOrNumber() { return QChar(*this).isLetterOrNumber(); }
1076 inline bool isDigit() const { return QChar(*this).isDigit(); }
1077 inline bool isLower() const { return QChar(*this).isLower(); }
1078 inline bool isUpper() const { return QChar(*this).isUpper(); }
1079 inline bool isTitleCase() const { return QChar(*this).isTitleCase(); }
1080
1081 inline int digitValue() const { return QChar(*this).digitValue(); }
1082 QChar toLower() const { return QChar(*this).toLower(); }
1083 QChar toUpper() const { return QChar(*this).toUpper(); }
1084 QChar toTitleCase () const { return QChar(*this).toTitleCase(); }
1085
1086 QChar::Category category() const { return QChar(*this).category(); }
1087 QChar::Direction direction() const { return QChar(*this).direction(); }
1088 QChar::JoiningType joiningType() const { return QChar(*this).joiningType(); }
1089#if QT_DEPRECATED_SINCE(5, 3)
1090 QT_DEPRECATED QChar::Joining joining() const
1091 {
1092 switch (QChar(*this).joiningType()) {
1093 case QChar::Joining_Causing: return QChar::Center;
1094 case QChar::Joining_Dual: return QChar::Dual;
1095 case QChar::Joining_Right: return QChar::Right;
1096 case QChar::Joining_None:
1097 case QChar::Joining_Left:
1098 case QChar::Joining_Transparent:
1099 default: return QChar::OtherJoining;
1100 }
1101 }
1102#endif
1103 bool hasMirrored() const { return QChar(*this).hasMirrored(); }
1104 QChar mirroredChar() const { return QChar(*this).mirroredChar(); }
1105 QString decomposition() const { return QChar(*this).decomposition(); }
1106 QChar::Decomposition decompositionTag() const { return QChar(*this).decompositionTag(); }
1107 uchar combiningClass() const { return QChar(*this).combiningClass(); }
1108
1109 inline QChar::Script script() const { return QChar(*this).script(); }
1110
1111 QChar::UnicodeVersion unicodeVersion() const { return QChar(*this).unicodeVersion(); }
1112
1113 inline uchar cell() const { return QChar(*this).cell(); }
1114 inline uchar row() const { return QChar(*this).row(); }
1115 inline void setCell(uchar cell);
1116 inline void setRow(uchar row);
1117
1118#if QT_DEPRECATED_SINCE(5, 0)
1119 QT_DEPRECATED char toAscii() const { return QChar(*this).toLatin1(); }
1120#endif
1121 char toLatin1() const { return QChar(*this).toLatin1(); }
1122 ushort unicode() const { return QChar(*this).unicode(); }
1123 ushort& unicode() { return s.data()[i].unicode(); }
1124
1125};
1126Q_DECLARE_TYPEINFO(QCharRef, Q_MOVABLE_TYPE);
1127
1128inline void QCharRef::setRow(uchar arow) { QChar(*this).setRow(arow); }
1129inline void QCharRef::setCell(uchar acell) { QChar(*this).setCell(acell); }
1130
1131
1132inline QString::QString() Q_DECL_NOTHROW : d(Data::sharedNull()) {}
1133inline QString::~QString() { if (!d->ref.deref()) Data::deallocate(d); }
1134
1135inline void QString::reserve(int asize)
1136{
1137 if (d->ref.isShared() || uint(asize) >= d->alloc)
1138 reallocData(qMax(asize, d->size) + 1u);
1139
1140 if (!d->capacityReserved) {
1141 // cannot set unconditionally, since d could be the shared_null/shared_empty (which is const)
1142 d->capacityReserved = true;
1143 }
1144}
1145
1146inline void QString::squeeze()
1147{
1148 if (d->ref.isShared() || uint(d->size) + 1u < d->alloc)
1149 reallocData(uint(d->size) + 1u);
1150
1151 if (d->capacityReserved) {
1152 // cannot set unconditionally, since d could be shared_null or
1153 // otherwise static.
1154 d->capacityReserved = false;
1155 }
1156}
1157
1158inline QString &QString::setUtf16(const ushort *autf16, int asize)
1159{ return setUnicode(reinterpret_cast<const QChar *>(autf16), asize); }
1160inline QCharRef QString::operator[](int i)
1161{ Q_ASSERT(i >= 0); return QCharRef(*this, i); }
1162inline QCharRef QString::operator[](uint i)
1163{ return QCharRef(*this, i); }
1164inline QCharRef QString::front() { return operator[](0); }
1165inline QCharRef QString::back() { return operator[](size() - 1); }
1166inline QString::iterator QString::begin()
1167{ detach(); return reinterpret_cast<QChar*>(d->data()); }
1168inline QString::const_iterator QString::begin() const
1169{ return reinterpret_cast<const QChar*>(d->data()); }
1170inline QString::const_iterator QString::cbegin() const
1171{ return reinterpret_cast<const QChar*>(d->data()); }
1172inline QString::const_iterator QString::constBegin() const
1173{ return reinterpret_cast<const QChar*>(d->data()); }
1174inline QString::iterator QString::end()
1175{ detach(); return reinterpret_cast<QChar*>(d->data() + d->size); }
1176inline QString::const_iterator QString::end() const
1177{ return reinterpret_cast<const QChar*>(d->data() + d->size); }
1178inline QString::const_iterator QString::cend() const
1179{ return reinterpret_cast<const QChar*>(d->data() + d->size); }
1180inline QString::const_iterator QString::constEnd() const
1181{ return reinterpret_cast<const QChar*>(d->data() + d->size); }
1182inline bool QString::contains(const QString &s, Qt::CaseSensitivity cs) const
1183{ return indexOf(s, 0, cs) != -1; }
1184inline bool QString::contains(const QStringRef &s, Qt::CaseSensitivity cs) const
1185{ return indexOf(s, 0, cs) != -1; }
1186inline bool QString::contains(QLatin1String s, Qt::CaseSensitivity cs) const
1187{ return indexOf(s, 0, cs) != -1; }
1188inline bool QString::contains(QChar c, Qt::CaseSensitivity cs) const
1189{ return indexOf(c, 0, cs) != -1; }
1190
1191#if QT_DEPRECATED_SINCE(5, 9)
1192inline bool operator==(QString::Null, QString::Null) { return true; }
1193QT_DEPRECATED_X("use QString::isNull()")
1194inline bool operator==(QString::Null, const QString &s) { return s.isNull(); }
1195QT_DEPRECATED_X("use QString::isNull()")
1196inline bool operator==(const QString &s, QString::Null) { return s.isNull(); }
1197inline bool operator!=(QString::Null, QString::Null) { return false; }
1198QT_DEPRECATED_X("use !QString::isNull()")
1199inline bool operator!=(QString::Null, const QString &s) { return !s.isNull(); }
1200QT_DEPRECATED_X("use !QString::isNull()")
1201inline bool operator!=(const QString &s, QString::Null) { return !s.isNull(); }
1202#endif
1203
1204inline bool operator==(QLatin1String s1, QLatin1String s2) Q_DECL_NOTHROW
1205{ return s1.size() == s2.size() && (!s1.size() || !memcmp(s1.latin1(), s2.latin1(), s1.size())); }
1206inline bool operator!=(QLatin1String s1, QLatin1String s2) Q_DECL_NOTHROW
1207{ return !operator==(s1, s2); }
1208inline bool operator<(QLatin1String s1, QLatin1String s2) Q_DECL_NOTHROW
1209{
1210 const int len = qMin(s1.size(), s2.size());
1211 const int r = len ? memcmp(s1.latin1(), s2.latin1(), len) : 0;
1212 return r < 0 || (r == 0 && s1.size() < s2.size());
1213}
1214inline bool operator>(QLatin1String s1, QLatin1String s2) Q_DECL_NOTHROW
1215{ return operator<(s2, s1); }
1216inline bool operator<=(QLatin1String s1, QLatin1String s2) Q_DECL_NOTHROW
1217{ return !operator>(s1, s2); }
1218inline bool operator>=(QLatin1String s1, QLatin1String s2) Q_DECL_NOTHROW
1219{ return !operator<(s1, s2); }
1220
1221inline bool QLatin1String::operator==(const QString &s) const Q_DECL_NOTHROW
1222{ return s == *this; }
1223inline bool QLatin1String::operator!=(const QString &s) const Q_DECL_NOTHROW
1224{ return s != *this; }
1225inline bool QLatin1String::operator>(const QString &s) const Q_DECL_NOTHROW
1226{ return s < *this; }
1227inline bool QLatin1String::operator<(const QString &s) const Q_DECL_NOTHROW
1228{ return s > *this; }
1229inline bool QLatin1String::operator>=(const QString &s) const Q_DECL_NOTHROW
1230{ return s <= *this; }
1231inline bool QLatin1String::operator<=(const QString &s) const Q_DECL_NOTHROW
1232{ return s >= *this; }
1233
1234#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
1235inline bool QString::operator==(const char *s) const
1236{ return QString::compare_helper(constData(), size(), s, -1) == 0; }
1237inline bool QString::operator!=(const char *s) const
1238{ return QString::compare_helper(constData(), size(), s, -1) != 0; }
1239inline bool QString::operator<(const char *s) const
1240{ return QString::compare_helper(constData(), size(), s, -1) < 0; }
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; }
1247
1248inline QT_ASCII_CAST_WARN bool operator==(const char *s1, const QString &s2)
1249{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) == 0; }
1250inline QT_ASCII_CAST_WARN bool operator!=(const char *s1, const QString &s2)
1251{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) != 0; }
1252inline QT_ASCII_CAST_WARN bool operator<(const char *s1, const QString &s2)
1253{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) > 0; }
1254inline QT_ASCII_CAST_WARN bool operator>(const char *s1, const QString &s2)
1255{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) < 0; }
1256inline QT_ASCII_CAST_WARN bool operator<=(const char *s1, const QString &s2)
1257{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) >= 0; }
1258inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, const QString &s2)
1259{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) <= 0; }
1260
1261inline QT_ASCII_CAST_WARN bool operator==(const char *s1, QLatin1String s2)
1262{ return QString::fromUtf8(s1) == s2; }
1263inline QT_ASCII_CAST_WARN bool operator!=(const char *s1, QLatin1String s2)
1264{ return QString::fromUtf8(s1) != s2; }
1265inline QT_ASCII_CAST_WARN bool operator<(const char *s1, QLatin1String s2)
1266{ return (QString::fromUtf8(s1) < s2); }
1267inline QT_ASCII_CAST_WARN bool operator>(const char *s1, QLatin1String s2)
1268{ return (QString::fromUtf8(s1) > s2); }
1269inline QT_ASCII_CAST_WARN bool operator<=(const char *s1, QLatin1String s2)
1270{ return (QString::fromUtf8(s1) <= s2); }
1271inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, QLatin1String s2)
1272{ return (QString::fromUtf8(s1) >= s2); }
1273
1274inline QT_ASCII_CAST_WARN bool QLatin1String::operator==(const char *s) const
1275{ return QString::fromUtf8(s) == *this; }
1276inline QT_ASCII_CAST_WARN bool QLatin1String::operator!=(const char *s) const
1277{ return QString::fromUtf8(s) != *this; }
1278inline QT_ASCII_CAST_WARN bool QLatin1String::operator<(const char *s) const
1279{ return QString::fromUtf8(s) > *this; }
1280inline QT_ASCII_CAST_WARN bool QLatin1String::operator>(const char *s) const
1281{ return QString::fromUtf8(s) < *this; }
1282inline QT_ASCII_CAST_WARN bool QLatin1String::operator<=(const char *s) const
1283{ return QString::fromUtf8(s) >= *this; }
1284inline QT_ASCII_CAST_WARN bool QLatin1String::operator>=(const char *s) const
1285{ return QString::fromUtf8(s) <= *this; }
1286
1287inline QT_ASCII_CAST_WARN bool QLatin1String::operator==(const QByteArray &s) const
1288{ return QString::fromUtf8(s) == *this; }
1289inline QT_ASCII_CAST_WARN bool QLatin1String::operator!=(const QByteArray &s) const
1290{ return QString::fromUtf8(s) != *this; }
1291inline QT_ASCII_CAST_WARN bool QLatin1String::operator<(const QByteArray &s) const
1292{ return QString::fromUtf8(s) > *this; }
1293inline QT_ASCII_CAST_WARN bool QLatin1String::operator>(const QByteArray &s) const
1294{ return QString::fromUtf8(s) < *this; }
1295inline QT_ASCII_CAST_WARN bool QLatin1String::operator<=(const QByteArray &s) const
1296{ return QString::fromUtf8(s) >= *this; }
1297inline QT_ASCII_CAST_WARN bool QLatin1String::operator>=(const QByteArray &s) const
1298{ return QString::fromUtf8(s) <= *this; }
1299
1300inline QT_ASCII_CAST_WARN bool QString::operator==(const QByteArray &s) const
1301{ return QString::compare_helper(constData(), size(), s.constData(), qstrnlen(s.constData(), s.size())) == 0; }
1302inline QT_ASCII_CAST_WARN bool QString::operator!=(const QByteArray &s) const
1303{ return QString::compare_helper(constData(), size(), s.constData(), qstrnlen(s.constData(), s.size())) != 0; }
1304inline QT_ASCII_CAST_WARN bool QString::operator<(const QByteArray &s) const
1305{ return QString::compare_helper(constData(), size(), s.constData(), s.size()) < 0; }
1306inline QT_ASCII_CAST_WARN bool QString::operator>(const QByteArray &s) const
1307{ return QString::compare_helper(constData(), size(), s.constData(), s.size()) > 0; }
1308inline QT_ASCII_CAST_WARN bool QString::operator<=(const QByteArray &s) const
1309{ return QString::compare_helper(constData(), size(), s.constData(), s.size()) <= 0; }
1310inline QT_ASCII_CAST_WARN bool QString::operator>=(const QByteArray &s) const
1311{ return QString::compare_helper(constData(), size(), s.constData(), s.size()) >= 0; }
1312
1313inline bool QByteArray::operator==(const QString &s) const
1314{ return QString::compare_helper(s.constData(), s.size(), constData(), qstrnlen(constData(), size())) == 0; }
1315inline bool QByteArray::operator!=(const QString &s) const
1316{ return QString::compare_helper(s.constData(), s.size(), constData(), qstrnlen(constData(), size())) != 0; }
1317inline bool QByteArray::operator<(const QString &s) const
1318{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) > 0; }
1319inline bool QByteArray::operator>(const QString &s) const
1320{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) < 0; }
1321inline bool QByteArray::operator<=(const QString &s) const
1322{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) >= 0; }
1323inline bool QByteArray::operator>=(const QString &s) const
1324{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) <= 0; }
1325#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
1326
1327#ifndef QT_NO_CAST_TO_ASCII
1328inline QByteArray &QByteArray::append(const QString &s)
1329{ return append(s.toUtf8()); }
1330inline QByteArray &QByteArray::insert(int i, const QString &s)
1331{ return insert(i, s.toUtf8()); }
1332inline QByteArray &QByteArray::replace(char c, const QString &after)
1333{ return replace(c, after.toUtf8()); }
1334inline QByteArray &QByteArray::replace(const QString &before, const char *after)
1335{ return replace(before.toUtf8(), after); }
1336inline QByteArray &QByteArray::replace(const QString &before, const QByteArray &after)
1337{ return replace(before.toUtf8(), after); }
1338inline QByteArray &QByteArray::operator+=(const QString &s)
1339{ return operator+=(s.toUtf8()); }
1340inline int QByteArray::indexOf(const QString &s, int from) const
1341{ return indexOf(s.toUtf8(), from); }
1342inline int QByteArray::lastIndexOf(const QString &s, int from) const
1343{ return lastIndexOf(s.toUtf8(), from); }
1344#endif // QT_NO_CAST_TO_ASCII
1345
1346#if !defined(QT_USE_FAST_OPERATOR_PLUS) && !defined(QT_USE_QSTRINGBUILDER)
1347inline const QString operator+(const QString &s1, const QString &s2)
1348{ QString t(s1); t += s2; return t; }
1349inline const QString operator+(const QString &s1, QChar s2)
1350{ QString t(s1); t += s2; return t; }
1351inline const QString operator+(QChar s1, const QString &s2)
1352{ QString t(s1); t += s2; return t; }
1353# if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
1354inline QT_ASCII_CAST_WARN const QString operator+(const QString &s1, const char *s2)
1355{ QString t(s1); t += QString::fromUtf8(s2); return t; }
1356inline QT_ASCII_CAST_WARN const QString operator+(const char *s1, const QString &s2)
1357{ QString t = QString::fromUtf8(s1); t += s2; return t; }
1358inline QT_ASCII_CAST_WARN const QString operator+(char c, const QString &s)
1359{ QString t = s; t.prepend(QChar::fromLatin1(c)); return t; }
1360inline QT_ASCII_CAST_WARN const QString operator+(const QString &s, char c)
1361{ QString t = s; t += QChar::fromLatin1(c); return t; }
1362inline QT_ASCII_CAST_WARN const QString operator+(const QByteArray &ba, const QString &s)
1363{ QString t = QString::fromUtf8(ba); t += s; return t; }
1364inline QT_ASCII_CAST_WARN const QString operator+(const QString &s, const QByteArray &ba)
1365{ QString t(s); t += QString::fromUtf8(ba); return t; }
1366# endif // QT_NO_CAST_FROM_ASCII
1367#endif // QT_USE_QSTRINGBUILDER
1368
1369inline std::string QString::toStdString() const
1370{ return toUtf8().toStdString(); }
1371
1372inline QString QString::fromStdString(const std::string &s)
1373{ return fromUtf8(s.data(), int(s.size())); }
1374
1375inline std::wstring QString::toStdWString() const
1376{
1377 std::wstring str;
1378 str.resize(length());
1379
1380#ifdef Q_CC_MSVC
1381 // VS2005 crashes if the string is empty
1382 if (!length())
1383 return str;
1384#endif
1385
1386 str.resize(toWCharArray(&(*str.begin())));
1387 return str;
1388}
1389
1390inline QString QString::fromStdWString(const std::wstring &s)
1391{ return fromWCharArray(s.data(), int(s.size())); }
1392
1393#if defined(Q_STDLIB_UNICODE_STRINGS)
1394inline QString QString::fromStdU16String(const std::u16string &s)
1395{ return fromUtf16(s.data(), int(s.size())); }
1396
1397inline std::u16string QString::toStdU16String() const
1398{ return std::u16string(reinterpret_cast<const char16_t*>(utf16()), length()); }
1399
1400inline QString QString::fromStdU32String(const std::u32string &s)
1401{ return fromUcs4(s.data(), int(s.size())); }
1402
1403inline std::u32string QString::toStdU32String() const
1404{
1405 std::u32string u32str(length(), char32_t(0));
1406 int len = toUcs4_helper(d->data(), length(), reinterpret_cast<uint*>(&u32str[0]));
1407 u32str.resize(len);
1408 return u32str;
1409}
1410#endif
1411
1412#if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE))
1413Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QString &);
1414Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QString &);
1415#endif
1416
1417Q_DECLARE_SHARED(QString)
1418Q_DECLARE_OPERATORS_FOR_FLAGS(QString::SectionFlags)
1419
1420
1421class Q_CORE_EXPORT QStringRef {
1422 const QString *m_string;
1423 int m_position;
1424 int m_size;
1425public:
1426 typedef QString::size_type size_type;
1427 typedef QString::value_type value_type;
1428 typedef const QChar *const_iterator;
1429 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
1430 typedef QString::const_pointer const_pointer;
1431 typedef QString::const_reference const_reference;
1432
1433 // ### Qt 6: make this constructor constexpr, after the destructor is made trivial
1434 inline QStringRef() : m_string(nullptr), m_position(0), m_size(0) {}
1435 inline QStringRef(const QString *string, int position, int size);
1436 inline QStringRef(const QString *string);
1437
1438#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
1439 // ### Qt 6: remove all of these, the implicit ones are fine
1440 QStringRef(const QStringRef &other) Q_DECL_NOTHROW
1441 :m_string(other.m_string), m_position(other.m_position), m_size(other.m_size)
1442 {}
1443#ifdef Q_COMPILER_RVALUE_REFS
1444 QStringRef(QStringRef &&other) Q_DECL_NOTHROW : m_string(other.m_string), m_position(other.m_position), m_size(other.m_size) {}
1445 QStringRef &operator=(QStringRef &&other) Q_DECL_NOTHROW { return *this = other; }
1446#endif
1447 QStringRef &operator=(const QStringRef &other) Q_DECL_NOTHROW
1448 {
1449 m_string = other.m_string; m_position = other.m_position;
1450 m_size = other.m_size; return *this;
1451 }
1452 inline ~QStringRef(){}
1453#endif // Qt < 6.0.0
1454
1455 inline const QString *string() const { return m_string; }
1456 inline int position() const { return m_position; }
1457 inline int size() const { return m_size; }
1458 inline int count() const { return m_size; }
1459 inline int length() const { return m_size; }
1460
1461 int indexOf(const QString &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1462 int indexOf(QChar ch, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1463 int indexOf(QLatin1String str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1464 int indexOf(const QStringRef &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1465 int lastIndexOf(const QString &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1466 int lastIndexOf(QChar ch, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1467 int lastIndexOf(QLatin1String str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1468 int lastIndexOf(const QStringRef &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1469
1470 inline bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1471 inline bool contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1472 inline bool contains(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1473 inline bool contains(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1474
1475 int count(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1476 int count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1477 int count(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1478
1479 Q_REQUIRED_RESULT QVector<QStringRef> split(const QString &sep, QString::SplitBehavior behavior = QString::KeepEmptyParts,
1480 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1481 Q_REQUIRED_RESULT QVector<QStringRef> split(QChar sep, QString::SplitBehavior behavior = QString::KeepEmptyParts,
1482 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1483
1484 Q_REQUIRED_RESULT QStringRef left(int n) const;
1485 Q_REQUIRED_RESULT QStringRef right(int n) const;
1486 Q_REQUIRED_RESULT QStringRef mid(int pos, int n = -1) const;
1487 Q_REQUIRED_RESULT QStringRef chopped(int n) const
1488 { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return left(size() - n); }
1489
1490 void truncate(int pos) Q_DECL_NOTHROW { m_size = qBound(0, pos, m_size); }
1491 void chop(int n) Q_DECL_NOTHROW
1492 {
1493 if (n >= m_size)
1494 m_size = 0;
1495 else if (n > 0)
1496 m_size -= n;
1497 }
1498
1499 bool isRightToLeft() const;
1500
1501 Q_REQUIRED_RESULT bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW
1502 { return QtPrivate::startsWith(*this, s, cs); }
1503 bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1504 bool startsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1505#if QT_STRINGVIEW_LEVEL < 2
1506 bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1507 bool startsWith(const QStringRef &c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1508#endif
1509
1510 Q_REQUIRED_RESULT bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW
1511 { return QtPrivate::endsWith(*this, s, cs); }
1512 bool endsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1513 bool endsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1514#if QT_STRINGVIEW_LEVEL < 2
1515 bool endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1516 bool endsWith(const QStringRef &c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1517#endif
1518
1519 inline QStringRef &operator=(const QString *string);
1520
1521 inline const QChar *unicode() const
1522 {
1523 if (!m_string)
1524 return reinterpret_cast<const QChar *>(QString::Data::sharedNull()->data());
1525 return m_string->unicode() + m_position;
1526 }
1527 inline const QChar *data() const { return unicode(); }
1528 inline const QChar *constData() const { return unicode(); }
1529
1530 inline const_iterator begin() const { return unicode(); }
1531 inline const_iterator cbegin() const { return unicode(); }
1532 inline const_iterator constBegin() const { return unicode(); }
1533 inline const_iterator end() const { return unicode() + size(); }
1534 inline const_iterator cend() const { return unicode() + size(); }
1535 inline const_iterator constEnd() const { return unicode() + size(); }
1536 inline const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
1537 inline const_reverse_iterator crbegin() const { return rbegin(); }
1538 inline const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
1539 inline const_reverse_iterator crend() const { return rend(); }
1540
1541#if QT_DEPRECATED_SINCE(5, 0)
1542 Q_REQUIRED_RESULT QT_DEPRECATED QByteArray toAscii() const
1543 { return toLatin1(); }
1544#endif
1545 Q_REQUIRED_RESULT QByteArray toLatin1() const;
1546 Q_REQUIRED_RESULT QByteArray toUtf8() const;
1547 Q_REQUIRED_RESULT QByteArray toLocal8Bit() const;
1548 Q_REQUIRED_RESULT QVector<uint> toUcs4() const;
1549
1550 inline void clear() { m_string = nullptr; m_position = m_size = 0; }
1551 QString toString() const;
1552 inline bool isEmpty() const { return m_size == 0; }
1553 inline bool isNull() const { return m_string == nullptr || m_string->isNull(); }
1554
1555 QStringRef appendTo(QString *string) const;
1556
1557 inline const QChar at(int i) const
1558 { Q_ASSERT(uint(i) < uint(size())); return m_string->at(i + m_position); }
1559 QChar operator[](int i) const { return at(i); }
1560 Q_REQUIRED_RESULT QChar front() const { return at(0); }
1561 Q_REQUIRED_RESULT QChar back() const { return at(size() - 1); }
1562
1563#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
1564 // ASCII compatibility
1565 inline QT_ASCII_CAST_WARN bool operator==(const char *s) const;
1566 inline QT_ASCII_CAST_WARN bool operator!=(const char *s) const;
1567 inline QT_ASCII_CAST_WARN bool operator<(const char *s) const;
1568 inline QT_ASCII_CAST_WARN bool operator<=(const char *s) const;
1569 inline QT_ASCII_CAST_WARN bool operator>(const char *s) const;
1570 inline QT_ASCII_CAST_WARN bool operator>=(const char *s) const;
1571#endif
1572
1573 int compare(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW;
1574 int compare(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW;
1575 int compare(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW;
1576#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
1577 int compare(const QByteArray &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
1578 { return QString::compare_helper(unicode(), size(), s.data(), qstrnlen(s.data(), s.size()), cs); }
1579#endif
1580 static int compare(const QStringRef &s1, const QString &s2,
1581 Qt::CaseSensitivity = Qt::CaseSensitive) Q_DECL_NOTHROW;
1582 static int compare(const QStringRef &s1, const QStringRef &s2,
1583 Qt::CaseSensitivity = Qt::CaseSensitive) Q_DECL_NOTHROW;
1584 static int compare(const QStringRef &s1, QLatin1String s2,
1585 Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW;
1586
1587 int localeAwareCompare(const QString &s) const;
1588 int localeAwareCompare(const QStringRef &s) const;
1589 static int localeAwareCompare(const QStringRef &s1, const QString &s2);
1590 static int localeAwareCompare(const QStringRef &s1, const QStringRef &s2);
1591
1592 Q_REQUIRED_RESULT QStringRef trimmed() const;
1593 short toShort(bool *ok = nullptr, int base = 10) const;
1594 ushort toUShort(bool *ok = nullptr, int base = 10) const;
1595 int toInt(bool *ok = nullptr, int base = 10) const;
1596 uint toUInt(bool *ok = nullptr, int base = 10) const;
1597 long toLong(bool *ok = nullptr, int base = 10) const;
1598 ulong toULong(bool *ok = nullptr, int base = 10) const;
1599 qlonglong toLongLong(bool *ok = nullptr, int base = 10) const;
1600 qulonglong toULongLong(bool *ok = nullptr, int base = 10) const;
1601 float toFloat(bool *ok = nullptr) const;
1602 double toDouble(bool *ok = nullptr) const;
1603};
1604Q_DECLARE_TYPEINFO(QStringRef, Q_PRIMITIVE_TYPE);
1605
1606inline QStringRef &QStringRef::operator=(const QString *aString)
1607{ m_string = aString; m_position = 0; m_size = aString?aString->size():0; return *this; }
1608
1609inline QStringRef::QStringRef(const QString *aString, int aPosition, int aSize)
1610 :m_string(aString), m_position(aPosition), m_size(aSize){}
1611
1612inline QStringRef::QStringRef(const QString *aString)
1613 :m_string(aString), m_position(0), m_size(aString?aString->size() : 0){}
1614
1615// QStringRef <> QStringRef
1616Q_CORE_EXPORT bool operator==(const QStringRef &s1, const QStringRef &s2) Q_DECL_NOTHROW;
1617inline bool operator!=(const QStringRef &s1, const QStringRef &s2) Q_DECL_NOTHROW
1618{ return !(s1 == s2); }
1619Q_CORE_EXPORT bool operator<(const QStringRef &s1, const QStringRef &s2) Q_DECL_NOTHROW;
1620inline bool operator>(const QStringRef &s1, const QStringRef &s2) Q_DECL_NOTHROW
1621{ return s2 < s1; }
1622inline bool operator<=(const QStringRef &s1, const QStringRef &s2) Q_DECL_NOTHROW
1623{ return !(s1 > s2); }
1624inline bool operator>=(const QStringRef &s1, const QStringRef &s2) Q_DECL_NOTHROW
1625{ return !(s1 < s2); }
1626
1627// QString <> QStringRef
1628Q_CORE_EXPORT bool operator==(const QString &lhs, const QStringRef &rhs) Q_DECL_NOTHROW;
1629inline bool operator!=(const QString &lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return lhs.compare(rhs) != 0; }
1630inline bool operator< (const QString &lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return lhs.compare(rhs) < 0; }
1631inline bool operator> (const QString &lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return lhs.compare(rhs) > 0; }
1632inline bool operator<=(const QString &lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return lhs.compare(rhs) <= 0; }
1633inline bool operator>=(const QString &lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return lhs.compare(rhs) >= 0; }
1634
1635inline bool operator==(const QStringRef &lhs, const QString &rhs) Q_DECL_NOTHROW { return rhs == lhs; }
1636inline bool operator!=(const QStringRef &lhs, const QString &rhs) Q_DECL_NOTHROW { return rhs != lhs; }
1637inline bool operator< (const QStringRef &lhs, const QString &rhs) Q_DECL_NOTHROW { return rhs > lhs; }
1638inline bool operator> (const QStringRef &lhs, const QString &rhs) Q_DECL_NOTHROW { return rhs < lhs; }
1639inline bool operator<=(const QStringRef &lhs, const QString &rhs) Q_DECL_NOTHROW { return rhs >= lhs; }
1640inline bool operator>=(const QStringRef &lhs, const QString &rhs) Q_DECL_NOTHROW { return rhs <= lhs; }
1641
1642inline int QString::compare(const QStringRef &s, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW
1643{ return QString::compare_helper(constData(), length(), s.constData(), s.length(), cs); }
1644inline int QString::compare(const QString &s1, const QStringRef &s2, Qt::CaseSensitivity cs) Q_DECL_NOTHROW
1645{ return QString::compare_helper(s1.constData(), s1.length(), s2.constData(), s2.length(), cs); }
1646inline int QStringRef::compare(const QString &s, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW
1647{ return QString::compare_helper(constData(), length(), s.constData(), s.length(), cs); }
1648inline int QStringRef::compare(const QStringRef &s, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW
1649{ return QString::compare_helper(constData(), length(), s.constData(), s.length(), cs); }
1650inline int QStringRef::compare(QLatin1String s, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW
1651{ return QString::compare_helper(constData(), length(), s, cs); }
1652inline int QStringRef::compare(const QStringRef &s1, const QString &s2, Qt::CaseSensitivity cs) Q_DECL_NOTHROW
1653{ return QString::compare_helper(s1.constData(), s1.length(), s2.constData(), s2.length(), cs); }
1654inline int QStringRef::compare(const QStringRef &s1, const QStringRef &s2, Qt::CaseSensitivity cs) Q_DECL_NOTHROW
1655{ return QString::compare_helper(s1.constData(), s1.length(), s2.constData(), s2.length(), cs); }
1656inline int QStringRef::compare(const QStringRef &s1, QLatin1String s2, Qt::CaseSensitivity cs) Q_DECL_NOTHROW
1657{ return QString::compare_helper(s1.constData(), s1.length(), s2, cs); }
1658
1659// QLatin1String <> QStringRef
1660Q_CORE_EXPORT bool operator==(QLatin1String lhs, const QStringRef &rhs) Q_DECL_NOTHROW;
1661inline bool operator!=(QLatin1String lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return rhs.compare(lhs) != 0; }
1662inline bool operator< (QLatin1String lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return rhs.compare(lhs) > 0; }
1663inline bool operator> (QLatin1String lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return rhs.compare(lhs) < 0; }
1664inline bool operator<=(QLatin1String lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return rhs.compare(lhs) >= 0; }
1665inline bool operator>=(QLatin1String lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return rhs.compare(lhs) <= 0; }
1666
1667inline bool operator==(const QStringRef &lhs, QLatin1String rhs) Q_DECL_NOTHROW { return rhs == lhs; }
1668inline bool operator!=(const QStringRef &lhs, QLatin1String rhs) Q_DECL_NOTHROW { return rhs != lhs; }
1669inline bool operator< (const QStringRef &lhs, QLatin1String rhs) Q_DECL_NOTHROW { return rhs > lhs; }
1670inline bool operator> (const QStringRef &lhs, QLatin1String rhs) Q_DECL_NOTHROW { return rhs < lhs; }
1671inline bool operator<=(const QStringRef &lhs, QLatin1String rhs) Q_DECL_NOTHROW { return rhs >= lhs; }
1672inline bool operator>=(const QStringRef &lhs, QLatin1String rhs) Q_DECL_NOTHROW { return rhs <= lhs; }
1673
1674// QChar <> QString
1675inline bool operator==(QChar lhs, const QString &rhs) Q_DECL_NOTHROW
1676{ return rhs.size() == 1 && lhs == rhs.front(); }
1677inline bool operator< (QChar lhs, const QString &rhs) Q_DECL_NOTHROW
1678{ return QString::compare_helper(&lhs, 1, rhs.data(), rhs.size()) < 0; }
1679inline bool operator> (QChar lhs, const QString &rhs) Q_DECL_NOTHROW
1680{ return QString::compare_helper(&lhs, 1, rhs.data(), rhs.size()) > 0; }
1681
1682inline bool operator!=(QChar lhs, const QString &rhs) Q_DECL_NOTHROW { return !(lhs == rhs); }
1683inline bool operator<=(QChar lhs, const QString &rhs) Q_DECL_NOTHROW { return !(lhs > rhs); }
1684inline bool operator>=(QChar lhs, const QString &rhs) Q_DECL_NOTHROW { return !(lhs < rhs); }
1685
1686inline bool operator==(const QString &lhs, QChar rhs) Q_DECL_NOTHROW { return rhs == lhs; }
1687inline bool operator!=(const QString &lhs, QChar rhs) Q_DECL_NOTHROW { return !(rhs == lhs); }
1688inline bool operator< (const QString &lhs, QChar rhs) Q_DECL_NOTHROW { return rhs > lhs; }
1689inline bool operator> (const QString &lhs, QChar rhs) Q_DECL_NOTHROW { return rhs < lhs; }
1690inline bool operator<=(const QString &lhs, QChar rhs) Q_DECL_NOTHROW { return !(rhs < lhs); }
1691inline bool operator>=(const QString &lhs, QChar rhs) Q_DECL_NOTHROW { return !(rhs > lhs); }
1692
1693// QChar <> QStringRef
1694inline bool operator==(QChar lhs, const QStringRef &rhs) Q_DECL_NOTHROW
1695{ return rhs.size() == 1 && lhs == rhs.front(); }
1696inline bool operator< (QChar lhs, const QStringRef &rhs) Q_DECL_NOTHROW
1697{ return QString::compare_helper(&lhs, 1, rhs.data(), rhs.size()) < 0; }
1698inline bool operator> (QChar lhs, const QStringRef &rhs) Q_DECL_NOTHROW
1699{ return QString::compare_helper(&lhs, 1, rhs.data(), rhs.size()) > 0; }
1700
1701inline bool operator!=(QChar lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return !(lhs == rhs); }
1702inline bool operator<=(QChar lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return !(lhs > rhs); }
1703inline bool operator>=(QChar lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return !(lhs < rhs); }
1704
1705inline bool operator==(const QStringRef &lhs, QChar rhs) Q_DECL_NOTHROW { return rhs == lhs; }
1706inline bool operator!=(const QStringRef &lhs, QChar rhs) Q_DECL_NOTHROW { return !(rhs == lhs); }
1707inline bool operator< (const QStringRef &lhs, QChar rhs) Q_DECL_NOTHROW { return rhs > lhs; }
1708inline bool operator> (const QStringRef &lhs, QChar rhs) Q_DECL_NOTHROW { return rhs < lhs; }
1709inline bool operator<=(const QStringRef &lhs, QChar rhs) Q_DECL_NOTHROW { return !(rhs < lhs); }
1710inline bool operator>=(const QStringRef &lhs, QChar rhs) Q_DECL_NOTHROW { return !(rhs > lhs); }
1711
1712// QChar <> QLatin1String
1713inline bool operator==(QChar lhs, QLatin1String rhs) Q_DECL_NOTHROW
1714{ return rhs.size() == 1 && lhs == rhs.front(); }
1715inline bool operator< (QChar lhs, QLatin1String rhs) Q_DECL_NOTHROW
1716{ return QString::compare_helper(&lhs, 1, rhs) < 0; }
1717inline bool operator> (QChar lhs, QLatin1String rhs) Q_DECL_NOTHROW
1718{ return QString::compare_helper(&lhs, 1, rhs) > 0; }
1719
1720inline bool operator!=(QChar lhs, QLatin1String rhs) Q_DECL_NOTHROW { return !(lhs == rhs); }
1721inline bool operator<=(QChar lhs, QLatin1String rhs) Q_DECL_NOTHROW { return !(lhs > rhs); }
1722inline bool operator>=(QChar lhs, QLatin1String rhs) Q_DECL_NOTHROW { return !(lhs < rhs); }
1723
1724inline bool operator==(QLatin1String lhs, QChar rhs) Q_DECL_NOTHROW { return rhs == lhs; }
1725inline bool operator!=(QLatin1String lhs, QChar rhs) Q_DECL_NOTHROW { return !(rhs == lhs); }
1726inline bool operator< (QLatin1String lhs, QChar rhs) Q_DECL_NOTHROW { return rhs > lhs; }
1727inline bool operator> (QLatin1String lhs, QChar rhs) Q_DECL_NOTHROW { return rhs < lhs; }
1728inline bool operator<=(QLatin1String lhs, QChar rhs) Q_DECL_NOTHROW { return !(rhs < lhs); }
1729inline bool operator>=(QLatin1String lhs, QChar rhs) Q_DECL_NOTHROW { return !(rhs > lhs); }
1730
1731// QStringView <> QStringView
1732inline bool operator==(QStringView lhs, QStringView rhs) Q_DECL_NOTHROW { return lhs.size() == rhs.size() && QtPrivate::compareStrings(lhs, rhs) == 0; }
1733inline bool operator!=(QStringView lhs, QStringView rhs) Q_DECL_NOTHROW { return !(lhs == rhs); }
1734inline bool operator< (QStringView lhs, QStringView rhs) Q_DECL_NOTHROW { return QtPrivate::compareStrings(lhs, rhs) < 0; }
1735inline bool operator<=(QStringView lhs, QStringView rhs) Q_DECL_NOTHROW { return QtPrivate::compareStrings(lhs, rhs) <= 0; }
1736inline bool operator> (QStringView lhs, QStringView rhs) Q_DECL_NOTHROW { return QtPrivate::compareStrings(lhs, rhs) > 0; }
1737inline bool operator>=(QStringView lhs, QStringView rhs) Q_DECL_NOTHROW { return QtPrivate::compareStrings(lhs, rhs) >= 0; }
1738
1739// QStringView <> QChar
1740inline bool operator==(QStringView lhs, QChar rhs) Q_DECL_NOTHROW { return lhs == QStringView(&rhs, 1); }
1741inline bool operator!=(QStringView lhs, QChar rhs) Q_DECL_NOTHROW { return lhs != QStringView(&rhs, 1); }
1742inline bool operator< (QStringView lhs, QChar rhs) Q_DECL_NOTHROW { return lhs < QStringView(&rhs, 1); }
1743inline bool operator<=(QStringView lhs, QChar rhs) Q_DECL_NOTHROW { return lhs <= QStringView(&rhs, 1); }
1744inline bool operator> (QStringView lhs, QChar rhs) Q_DECL_NOTHROW { return lhs > QStringView(&rhs, 1); }
1745inline bool operator>=(QStringView lhs, QChar rhs) Q_DECL_NOTHROW { return lhs >= QStringView(&rhs, 1); }
1746
1747inline bool operator==(QChar lhs, QStringView rhs) Q_DECL_NOTHROW { return QStringView(&lhs, 1) == rhs; }
1748inline bool operator!=(QChar lhs, QStringView rhs) Q_DECL_NOTHROW { return QStringView(&lhs, 1) != rhs; }
1749inline bool operator< (QChar lhs, QStringView rhs) Q_DECL_NOTHROW { return QStringView(&lhs, 1) < rhs; }
1750inline bool operator<=(QChar lhs, QStringView rhs) Q_DECL_NOTHROW { return QStringView(&lhs, 1) <= rhs; }
1751inline bool operator> (QChar lhs, QStringView rhs) Q_DECL_NOTHROW { return QStringView(&lhs, 1) > rhs; }
1752inline bool operator>=(QChar lhs, QStringView rhs) Q_DECL_NOTHROW { return QStringView(&lhs, 1) >= rhs; }
1753
1754// QStringView <> QLatin1String
1755inline bool operator==(QStringView lhs, QLatin1String rhs) Q_DECL_NOTHROW { return lhs.size() == rhs.size() && QtPrivate::compareStrings(lhs, rhs) == 0; }
1756inline bool operator!=(QStringView lhs, QLatin1String rhs) Q_DECL_NOTHROW { return !(lhs == rhs); }
1757inline bool operator< (QStringView lhs, QLatin1String rhs) Q_DECL_NOTHROW { return QtPrivate::compareStrings(lhs, rhs) < 0; }
1758inline bool operator<=(QStringView lhs, QLatin1String rhs) Q_DECL_NOTHROW { return QtPrivate::compareStrings(lhs, rhs) <= 0; }
1759inline bool operator> (QStringView lhs, QLatin1String rhs) Q_DECL_NOTHROW { return QtPrivate::compareStrings(lhs, rhs) > 0; }
1760inline bool operator>=(QStringView lhs, QLatin1String rhs) Q_DECL_NOTHROW { return QtPrivate::compareStrings(lhs, rhs) >= 0; }
1761
1762inline bool operator==(QLatin1String lhs, QStringView rhs) Q_DECL_NOTHROW { return lhs.size() == rhs.size() && QtPrivate::compareStrings(lhs, rhs) == 0; }
1763inline bool operator!=(QLatin1String lhs, QStringView rhs) Q_DECL_NOTHROW { return !(lhs == rhs); }
1764inline bool operator< (QLatin1String lhs, QStringView rhs) Q_DECL_NOTHROW { return QtPrivate::compareStrings(lhs, rhs) < 0; }
1765inline bool operator<=(QLatin1String lhs, QStringView rhs) Q_DECL_NOTHROW { return QtPrivate::compareStrings(lhs, rhs) <= 0; }
1766inline bool operator> (QLatin1String lhs, QStringView rhs) Q_DECL_NOTHROW { return QtPrivate::compareStrings(lhs, rhs) > 0; }
1767inline bool operator>=(QLatin1String lhs, QStringView rhs) Q_DECL_NOTHROW { return QtPrivate::compareStrings(lhs, rhs) >= 0; }
1768
1769#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
1770// QStringRef <> QByteArray
1771inline QT_ASCII_CAST_WARN bool operator==(const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(rhs) == 0; }
1772inline QT_ASCII_CAST_WARN bool operator!=(const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(rhs) != 0; }
1773inline QT_ASCII_CAST_WARN bool operator< (const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(rhs) < 0; }
1774inline QT_ASCII_CAST_WARN bool operator> (const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(rhs) > 0; }
1775inline QT_ASCII_CAST_WARN bool operator<=(const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(rhs) <= 0; }
1776inline QT_ASCII_CAST_WARN bool operator>=(const QStringRef &lhs, const QByteArray &rhs) { return lhs.compare(rhs) >= 0; }
1777
1778inline QT_ASCII_CAST_WARN bool operator==(const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(lhs) == 0; }
1779inline QT_ASCII_CAST_WARN bool operator!=(const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(lhs) != 0; }
1780inline QT_ASCII_CAST_WARN bool operator< (const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(lhs) > 0; }
1781inline QT_ASCII_CAST_WARN bool operator> (const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(lhs) < 0; }
1782inline QT_ASCII_CAST_WARN bool operator<=(const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(lhs) >= 0; }
1783inline QT_ASCII_CAST_WARN bool operator>=(const QByteArray &lhs, const QStringRef &rhs) { return rhs.compare(lhs) <= 0; }
1784
1785// QStringRef <> const char *
1786inline QT_ASCII_CAST_WARN bool QStringRef::operator==(const char *s) const
1787{ return QString::compare_helper(constData(), size(), s, -1) == 0; }
1788inline QT_ASCII_CAST_WARN bool QStringRef::operator!=(const char *s) const
1789{ return QString::compare_helper(constData(), size(), s, -1) != 0; }
1790inline QT_ASCII_CAST_WARN bool QStringRef::operator<(const char *s) const
1791{ return QString::compare_helper(constData(), size(), s, -1) < 0; }
1792inline QT_ASCII_CAST_WARN bool QStringRef::operator<=(const char *s) const
1793{ return QString::compare_helper(constData(), size(), s, -1) <= 0; }
1794inline QT_ASCII_CAST_WARN bool QStringRef::operator>(const char *s) const
1795{ return QString::compare_helper(constData(), size(), s, -1) > 0; }
1796inline QT_ASCII_CAST_WARN bool QStringRef::operator>=(const char *s) const
1797{ return QString::compare_helper(constData(), size(), s, -1) >= 0; }
1798
1799inline QT_ASCII_CAST_WARN bool operator==(const char *s1, const QStringRef &s2)
1800{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) == 0; }
1801inline QT_ASCII_CAST_WARN bool operator!=(const char *s1, const QStringRef &s2)
1802{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) != 0; }
1803inline QT_ASCII_CAST_WARN bool operator<(const char *s1, const QStringRef &s2)
1804{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) > 0; }
1805inline QT_ASCII_CAST_WARN bool operator<=(const char *s1, const QStringRef &s2)
1806{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) >= 0; }
1807inline QT_ASCII_CAST_WARN bool operator>(const char *s1, const QStringRef &s2)
1808{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) < 0; }
1809inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, const QStringRef &s2)
1810{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) <= 0; }
1811#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
1812
1813inline int QString::localeAwareCompare(const QStringRef &s) const
1814{ return localeAwareCompare_helper(constData(), length(), s.constData(), s.length()); }
1815inline int QString::localeAwareCompare(const QString& s1, const QStringRef& s2)
1816{ return localeAwareCompare_helper(s1.constData(), s1.length(), s2.constData(), s2.length()); }
1817inline int QStringRef::localeAwareCompare(const QString &s) const
1818{ return QString::localeAwareCompare_helper(constData(), length(), s.constData(), s.length()); }
1819inline int QStringRef::localeAwareCompare(const QStringRef &s) const
1820{ return QString::localeAwareCompare_helper(constData(), length(), s.constData(), s.length()); }
1821inline int QStringRef::localeAwareCompare(const QStringRef &s1, const QString &s2)
1822{ return QString::localeAwareCompare_helper(s1.constData(), s1.length(), s2.constData(), s2.length()); }
1823inline int QStringRef::localeAwareCompare(const QStringRef &s1, const QStringRef &s2)
1824{ return QString::localeAwareCompare_helper(s1.constData(), s1.length(), s2.constData(), s2.length()); }
1825
1826inline bool QStringRef::contains(const QString &s, Qt::CaseSensitivity cs) const
1827{ return indexOf(s, 0, cs) != -1; }
1828inline bool QStringRef::contains(QLatin1String s, Qt::CaseSensitivity cs) const
1829{ return indexOf(s, 0, cs) != -1; }
1830inline bool QStringRef::contains(QChar c, Qt::CaseSensitivity cs) const
1831{ return indexOf(c, 0, cs) != -1; }
1832inline bool QStringRef::contains(const QStringRef &s, Qt::CaseSensitivity cs) const
1833{ return indexOf(s, 0, cs) != -1; }
1834
1835inline QString &QString::insert(int i, const QStringRef &s)
1836{ return insert(i, s.constData(), s.length()); }
1837
1838#if !defined(QT_USE_FAST_OPERATOR_PLUS) && !defined(QT_USE_QSTRINGBUILDER)
1839inline QString operator+(const QString &s1, const QStringRef &s2)
1840{ QString t; t.reserve(s1.size() + s2.size()); t += s1; t += s2; return t; }
1841inline QString operator+(const QStringRef &s1, const QString &s2)
1842{ QString t; t.reserve(s1.size() + s2.size()); t += s1; t += s2; return t; }
1843inline QString operator+(const QStringRef &s1, QLatin1String s2)
1844{ QString t; t.reserve(s1.size() + s2.size()); t += s1; t += s2; return t; }
1845inline QString operator+(QLatin1String s1, const QStringRef &s2)
1846{ QString t; t.reserve(s1.size() + s2.size()); t += s1; t += s2; return t; }
1847inline QString operator+(const QStringRef &s1, const QStringRef &s2)
1848{ QString t; t.reserve(s1.size() + s2.size()); t += s1; t += s2; return t; }
1849inline QString operator+(const QStringRef &s1, QChar s2)
1850{ QString t; t.reserve(s1.size() + 1); t += s1; t += s2; return t; }
1851inline QString operator+(QChar s1, const QStringRef &s2)
1852{ QString t; t.reserve(1 + s2.size()); t += s1; t += s2; return t; }
1853#endif // !(QT_USE_FAST_OPERATOR_PLUS || QT_USE_QSTRINGBUILDER)
1854
1855namespace Qt {
1856#if QT_DEPRECATED_SINCE(5, 0)
1857QT_DEPRECATED inline QString escape(const QString &plain) {
1858 return plain.toHtmlEscaped();
1859}
1860#endif
1861}
1862
1863namespace QtPrivate {
1864// used by qPrintable() and qUtf8Printable() macros
1865inline const QString &asString(const QString &s) { return s; }
1866inline QString &&asString(QString &&s) { return std::move(s); }
1867}
1868
1869QT_END_NAMESPACE
1870
1871#if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER)
1872#include <QtCore/qstringbuilder.h>
1873#endif
1874
1875#endif // QSTRING_H
1876