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 Q_REQUIRED_RESULT QString leftJustified(int width, QChar fill = QLatin1Char(' '), bool trunc = false) const;
413 Q_REQUIRED_RESULT QString rightJustified(int width, QChar fill = QLatin1Char(' '), bool trunc = false) const;
414
415#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QSTRING_COMPAT_CPP) && !defined(Q_CLANG_QDOC)
416# if defined(Q_CC_GNU) && !defined(Q_CC_CLANG) && !defined(Q_CC_INTEL) && !QT_HAS_CPP_ATTRIBUTE(nodiscard)
417 // required due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61941
418# pragma push_macro("Q_REQUIRED_RESULT")
419# undef Q_REQUIRED_RESULT
420# define Q_REQUIRED_RESULT
421# define Q_REQUIRED_RESULT_pushed
422# endif
423 Q_REQUIRED_RESULT QString toLower() const &
424 { return toLower_helper(*this); }
425 Q_REQUIRED_RESULT QString toLower() &&
426 { return toLower_helper(*this); }
427 Q_REQUIRED_RESULT QString toUpper() const &
428 { return toUpper_helper(*this); }
429 Q_REQUIRED_RESULT QString toUpper() &&
430 { return toUpper_helper(*this); }
431 Q_REQUIRED_RESULT QString toCaseFolded() const &
432 { return toCaseFolded_helper(*this); }
433 Q_REQUIRED_RESULT QString toCaseFolded() &&
434 { return toCaseFolded_helper(*this); }
435 Q_REQUIRED_RESULT QString trimmed() const &
436 { return trimmed_helper(*this); }
437 Q_REQUIRED_RESULT QString trimmed() &&
438 { return trimmed_helper(*this); }
439 Q_REQUIRED_RESULT QString simplified() const &
440 { return simplified_helper(*this); }
441 Q_REQUIRED_RESULT QString simplified() &&
442 { return simplified_helper(*this); }
443# ifdef Q_REQUIRED_RESULT_pushed
444# pragma pop_macro("Q_REQUIRED_RESULT")
445# endif
446#else
447 Q_REQUIRED_RESULT QString toLower() const;
448 Q_REQUIRED_RESULT QString toUpper() const;
449 Q_REQUIRED_RESULT QString toCaseFolded() const;
450 Q_REQUIRED_RESULT QString trimmed() const;
451 Q_REQUIRED_RESULT QString simplified() const;
452#endif
453 Q_REQUIRED_RESULT QString toHtmlEscaped() const;
454
455 QString &insert(int i, QChar c);
456 QString &insert(int i, const QChar *uc, int len);
457 inline QString &insert(int i, const QString &s) { return insert(i, s.constData(), s.length()); }
458 inline QString &insert(int i, const QStringRef &s);
459 QString &insert(int i, QLatin1String s);
460 QString &append(QChar c);
461 QString &append(const QChar *uc, int len);
462 QString &append(const QString &s);
463 QString &append(const QStringRef &s);
464 QString &append(QLatin1String s);
465 inline QString &prepend(QChar c) { return insert(0, c); }
466 inline QString &prepend(const QChar *uc, int len) { return insert(0, uc, len); }
467 inline QString &prepend(const QString &s) { return insert(0, s); }
468 inline QString &prepend(const QStringRef &s) { return insert(0, s); }
469 inline QString &prepend(QLatin1String s) { return insert(0, s); }
470
471 inline QString &operator+=(QChar c) {
472 if (d->ref.isShared() || uint(d->size) + 2u > d->alloc)
473 reallocData(uint(d->size) + 2u, true);
474 d->data()[d->size++] = c.unicode();
475 d->data()[d->size] = '\0';
476 return *this;
477 }
478
479 inline QString &operator+=(QChar::SpecialCharacter c) { return append(QChar(c)); }
480 inline QString &operator+=(const QString &s) { return append(s); }
481 inline QString &operator+=(const QStringRef &s) { return append(s); }
482 inline QString &operator+=(QLatin1String s) { return append(s); }
483
484 QString &remove(int i, int len);
485 QString &remove(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive);
486 QString &remove(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive);
487 QString &remove(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive);
488 QString &replace(int i, int len, QChar after);
489 QString &replace(int i, int len, const QChar *s, int slen);
490 QString &replace(int i, int len, const QString &after);
491 QString &replace(QChar before, QChar after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
492 QString &replace(const QChar *before, int blen, const QChar *after, int alen, Qt::CaseSensitivity cs = Qt::CaseSensitive);
493 QString &replace(QLatin1String before, QLatin1String after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
494 QString &replace(QLatin1String before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
495 QString &replace(const QString &before, QLatin1String after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
496 QString &replace(const QString &before, const QString &after,
497 Qt::CaseSensitivity cs = Qt::CaseSensitive);
498 QString &replace(QChar c, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
499 QString &replace(QChar c, QLatin1String after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
500#ifndef QT_NO_REGEXP
501 QString &replace(const QRegExp &rx, const QString &after);
502 inline QString &remove(const QRegExp &rx)
503 { return replace(rx, QString()); }
504#endif
505#if QT_CONFIG(regularexpression)
506 QString &replace(const QRegularExpression &re, const QString &after);
507 inline QString &remove(const QRegularExpression &re)
508 { return replace(re, QString()); }
509#endif
510
511 enum SplitBehavior { KeepEmptyParts, SkipEmptyParts };
512
513 Q_REQUIRED_RESULT QStringList split(const QString &sep, SplitBehavior behavior = KeepEmptyParts,
514 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
515 Q_REQUIRED_RESULT QVector<QStringRef> splitRef(const QString &sep, SplitBehavior behavior = KeepEmptyParts,
516 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
517 Q_REQUIRED_RESULT QStringList split(QChar sep, SplitBehavior behavior = KeepEmptyParts,
518 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
519 Q_REQUIRED_RESULT QVector<QStringRef> splitRef(QChar sep, SplitBehavior behavior = KeepEmptyParts,
520 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
521#ifndef QT_NO_REGEXP
522 Q_REQUIRED_RESULT QStringList split(const QRegExp &sep, SplitBehavior behavior = KeepEmptyParts) const;
523 Q_REQUIRED_RESULT QVector<QStringRef> splitRef(const QRegExp &sep, SplitBehavior behavior = KeepEmptyParts) const;
524#endif
525#if QT_CONFIG(regularexpression)
526 Q_REQUIRED_RESULT QStringList split(const QRegularExpression &sep, SplitBehavior behavior = KeepEmptyParts) const;
527 Q_REQUIRED_RESULT QVector<QStringRef> splitRef(const QRegularExpression &sep, SplitBehavior behavior = KeepEmptyParts) const;
528#endif
529 enum NormalizationForm {
530 NormalizationForm_D,
531 NormalizationForm_C,
532 NormalizationForm_KD,
533 NormalizationForm_KC
534 };
535 Q_REQUIRED_RESULT QString normalized(NormalizationForm mode, QChar::UnicodeVersion version = QChar::Unicode_Unassigned) const;
536
537 Q_REQUIRED_RESULT QString repeated(int times) const;
538
539 const ushort *utf16() const;
540
541#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QSTRING_COMPAT_CPP) && !defined(Q_CLANG_QDOC)
542 Q_REQUIRED_RESULT QByteArray toLatin1() const &
543 { return toLatin1_helper(*this); }
544 Q_REQUIRED_RESULT QByteArray toLatin1() &&
545 { return toLatin1_helper_inplace(*this); }
546 Q_REQUIRED_RESULT QByteArray toUtf8() const &
547 { return toUtf8_helper(*this); }
548 Q_REQUIRED_RESULT QByteArray toUtf8() &&
549 { return toUtf8_helper(*this); }
550 Q_REQUIRED_RESULT QByteArray toLocal8Bit() const &
551 { return toLocal8Bit_helper(isNull() ? nullptr : constData(), size()); }
552 Q_REQUIRED_RESULT QByteArray toLocal8Bit() &&
553 { return toLocal8Bit_helper(isNull() ? nullptr : constData(), size()); }
554#else
555 Q_REQUIRED_RESULT QByteArray toLatin1() const;
556 Q_REQUIRED_RESULT QByteArray toUtf8() const;
557 Q_REQUIRED_RESULT QByteArray toLocal8Bit() const;
558#endif
559 Q_REQUIRED_RESULT QVector<uint> toUcs4() const;
560
561 // note - this are all inline so we can benefit from strlen() compile time optimizations
562 static inline QString fromLatin1(const char *str, int size = -1)
563 {
564 QStringDataPtr dataPtr = { fromLatin1_helper(str, (str && size == -1) ? int(strlen(str)) : size) };
565 return QString(dataPtr);
566 }
567 static inline QString fromUtf8(const char *str, int size = -1)
568 {
569 return fromUtf8_helper(str, (str && size == -1) ? int(strlen(str)) : size);
570 }
571 static inline QString fromLocal8Bit(const char *str, int size = -1)
572 {
573 return fromLocal8Bit_helper(str, (str && size == -1) ? int(strlen(str)) : size);
574 }
575 static inline QString fromLatin1(const QByteArray &str)
576 { return str.isNull() ? QString() : fromLatin1(str.data(), qstrnlen(str.constData(), str.size())); }
577 static inline QString fromUtf8(const QByteArray &str)
578 { return str.isNull() ? QString() : fromUtf8(str.data(), qstrnlen(str.constData(), str.size())); }
579 static inline QString fromLocal8Bit(const QByteArray &str)
580 { return str.isNull() ? QString() : fromLocal8Bit(str.data(), qstrnlen(str.constData(), str.size())); }
581 static QString fromUtf16(const ushort *, int size = -1);
582 static QString fromUcs4(const uint *, int size = -1);
583 static QString fromRawData(const QChar *, int size);
584
585#if defined(Q_COMPILER_UNICODE_STRINGS)
586 static QString fromUtf16(const char16_t *str, int size = -1)
587 { return fromUtf16(reinterpret_cast<const ushort *>(str), size); }
588 static QString fromUcs4(const char32_t *str, int size = -1)
589 { return fromUcs4(reinterpret_cast<const uint *>(str), size); }
590#endif
591
592#if QT_DEPRECATED_SINCE(5, 0)
593 QT_DEPRECATED static inline QString fromAscii(const char *str, int size = -1)
594 { return fromLatin1(str, size); }
595 QT_DEPRECATED static inline QString fromAscii(const QByteArray &str)
596 { return fromLatin1(str); }
597 Q_REQUIRED_RESULT QByteArray toAscii() const
598 { return toLatin1(); }
599#endif
600
601 inline int toWCharArray(wchar_t *array) const;
602 Q_REQUIRED_RESULT static inline QString fromWCharArray(const wchar_t *string, int size = -1);
603
604 QString &setRawData(const QChar *unicode, int size);
605 QString &setUnicode(const QChar *unicode, int size);
606 inline QString &setUtf16(const ushort *utf16, int size);
607
608 int compare(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW;
609 int compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW;
610
611 static inline int compare(const QString &s1, const QString &s2,
612 Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW
613 { return s1.compare(s2, cs); }
614
615 static inline int compare(const QString &s1, QLatin1String s2,
616 Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW
617 { return s1.compare(s2, cs); }
618 static inline int compare(QLatin1String s1, const QString &s2,
619 Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW
620 { return -s2.compare(s1, cs); }
621
622 inline int compare(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW;
623 static int compare(const QString &s1, const QStringRef &s2,
624 Qt::CaseSensitivity = Qt::CaseSensitive) Q_DECL_NOTHROW;
625
626 int localeAwareCompare(const QString& s) const;
627 static int localeAwareCompare(const QString& s1, const QString& s2)
628 { return s1.localeAwareCompare(s2); }
629
630 int localeAwareCompare(const QStringRef &s) const;
631 static int localeAwareCompare(const QString& s1, const QStringRef& s2);
632
633 // ### Qt6: make inline except for the long long versions
634 short toShort(bool *ok=nullptr, int base=10) const;
635 ushort toUShort(bool *ok=nullptr, int base=10) const;
636 int toInt(bool *ok=nullptr, int base=10) const;
637 uint toUInt(bool *ok=nullptr, int base=10) const;
638 long toLong(bool *ok=nullptr, int base=10) const;
639 ulong toULong(bool *ok=nullptr, int base=10) const;
640 qlonglong toLongLong(bool *ok=nullptr, int base=10) const;
641 qulonglong toULongLong(bool *ok=nullptr, int base=10) const;
642 float toFloat(bool *ok=nullptr) const;
643 double toDouble(bool *ok=nullptr) const;
644
645 QString &setNum(short, int base=10);
646 QString &setNum(ushort, int base=10);
647 QString &setNum(int, int base=10);
648 QString &setNum(uint, int base=10);
649 QString &setNum(long, int base=10);
650 QString &setNum(ulong, int base=10);
651 QString &setNum(qlonglong, int base=10);
652 QString &setNum(qulonglong, int base=10);
653 QString &setNum(float, char f='g', int prec=6);
654 QString &setNum(double, char f='g', int prec=6);
655
656 static QString number(int, int base=10);
657 static QString number(uint, int base=10);
658 static QString number(long, int base=10);
659 static QString number(ulong, int base=10);
660 static QString number(qlonglong, int base=10);
661 static QString number(qulonglong, int base=10);
662 static QString number(double, char f='g', int prec=6);
663
664 friend Q_CORE_EXPORT bool operator==(const QString &s1, const QString &s2) Q_DECL_NOTHROW;
665 friend Q_CORE_EXPORT bool operator<(const QString &s1, const QString &s2) Q_DECL_NOTHROW;
666 friend inline bool operator>(const QString &s1, const QString &s2) Q_DECL_NOTHROW { return s2 < s1; }
667 friend inline bool operator!=(const QString &s1, const QString &s2) Q_DECL_NOTHROW { return !(s1 == s2); }
668 friend inline bool operator<=(const QString &s1, const QString &s2) Q_DECL_NOTHROW { return !(s1 > s2); }
669 friend inline bool operator>=(const QString &s1, const QString &s2) Q_DECL_NOTHROW { return !(s1 < s2); }
670
671 bool operator==(QLatin1String s) const Q_DECL_NOTHROW;
672 bool operator<(QLatin1String s) const Q_DECL_NOTHROW;
673 bool operator>(QLatin1String s) const Q_DECL_NOTHROW;
674 inline bool operator!=(QLatin1String s) const Q_DECL_NOTHROW { return !operator==(s); }
675 inline bool operator<=(QLatin1String s) const Q_DECL_NOTHROW { return !operator>(s); }
676 inline bool operator>=(QLatin1String s) const Q_DECL_NOTHROW { return !operator<(s); }
677
678 // ASCII compatibility
679#if defined(QT_RESTRICTED_CAST_FROM_ASCII)
680 template <int N>
681 inline QString(const char (&ch)[N])
682 : d(fromAscii_helper(ch, N - 1))
683 {}
684 template <int N>
685 QString(char (&)[N]) = delete;
686 template <int N>
687 inline QString &operator=(const char (&ch)[N])
688 { return (*this = fromUtf8(ch, N - 1)); }
689 template <int N>
690 QString &operator=(char (&)[N]) = delete;
691#endif
692#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
693 inline QT_ASCII_CAST_WARN QString(const char *ch)
694 : d(fromAscii_helper(ch, ch ? int(strlen(ch)) : -1))
695 {}
696 inline QT_ASCII_CAST_WARN QString(const QByteArray &a)
697 : d(fromAscii_helper(a.constData(), qstrnlen(a.constData(), a.size())))
698 {}
699 inline QT_ASCII_CAST_WARN QString &operator=(const char *ch)
700 { return (*this = fromUtf8(ch)); }
701 inline QT_ASCII_CAST_WARN QString &operator=(const QByteArray &a)
702 { return (*this = fromUtf8(a)); }
703 inline QT_ASCII_CAST_WARN QString &operator=(char c)
704 { return (*this = QChar::fromLatin1(c)); }
705
706 // these are needed, so it compiles with STL support enabled
707 inline QT_ASCII_CAST_WARN QString &prepend(const char *s)
708 { return prepend(QString::fromUtf8(s)); }
709 inline QT_ASCII_CAST_WARN QString &prepend(const QByteArray &s)
710 { return prepend(QString::fromUtf8(s)); }
711 inline QT_ASCII_CAST_WARN QString &append(const char *s)
712 { return append(QString::fromUtf8(s)); }
713 inline QT_ASCII_CAST_WARN QString &append(const QByteArray &s)
714 { return append(QString::fromUtf8(s)); }
715 inline QT_ASCII_CAST_WARN QString &insert(int i, const char *s)
716 { return insert(i, QString::fromUtf8(s)); }
717 inline QT_ASCII_CAST_WARN QString &insert(int i, const QByteArray &s)
718 { return insert(i, QString::fromUtf8(s)); }
719 inline QT_ASCII_CAST_WARN QString &operator+=(const char *s)
720 { return append(QString::fromUtf8(s)); }
721 inline QT_ASCII_CAST_WARN QString &operator+=(const QByteArray &s)
722 { return append(QString::fromUtf8(s)); }
723 inline QT_ASCII_CAST_WARN QString &operator+=(char c)
724 { return append(QChar::fromLatin1(c)); }
725
726 inline QT_ASCII_CAST_WARN bool operator==(const char *s) const;
727 inline QT_ASCII_CAST_WARN bool operator!=(const char *s) const;
728 inline QT_ASCII_CAST_WARN bool operator<(const char *s) const;
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
733 inline QT_ASCII_CAST_WARN bool operator==(const QByteArray &s) const;
734 inline QT_ASCII_CAST_WARN bool operator!=(const QByteArray &s) const;
735 inline QT_ASCII_CAST_WARN bool operator<(const QByteArray &s) const;
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
740 friend inline QT_ASCII_CAST_WARN bool operator==(const char *s1, const QString &s2);
741 friend inline QT_ASCII_CAST_WARN bool operator!=(const char *s1, const QString &s2);
742 friend inline QT_ASCII_CAST_WARN bool operator<(const char *s1, const QString &s2);
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
747 friend inline QT_ASCII_CAST_WARN bool operator==(const char *s1, const QStringRef &s2);
748 friend inline QT_ASCII_CAST_WARN bool operator!=(const char *s1, const QStringRef &s2);
749 friend inline QT_ASCII_CAST_WARN bool operator<(const char *s1, const QStringRef &s2);
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#endif
754
755 typedef QChar *iterator;
756 typedef const QChar *const_iterator;
757 typedef iterator Iterator;
758 typedef const_iterator ConstIterator;
759 typedef std::reverse_iterator<iterator> reverse_iterator;
760 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
761 inline iterator begin();
762 inline const_iterator begin() const;
763 inline const_iterator cbegin() const;
764 inline const_iterator constBegin() const;
765 inline iterator end();
766 inline const_iterator end() const;
767 inline const_iterator cend() const;
768 inline const_iterator constEnd() const;
769 reverse_iterator rbegin() { return reverse_iterator(end()); }
770 reverse_iterator rend() { return reverse_iterator(begin()); }
771 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
772 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
773 const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); }
774 const_reverse_iterator crend() const { return const_reverse_iterator(begin()); }
775
776 // STL compatibility
777 typedef int size_type;
778 typedef qptrdiff difference_type;
779 typedef const QChar & const_reference;
780 typedef QChar & reference;
781 typedef QChar *pointer;
782 typedef const QChar *const_pointer;
783 typedef QChar value_type;
784 inline void push_back(QChar c) { append(c); }
785 inline void push_back(const QString &s) { append(s); }
786 inline void push_front(QChar c) { prepend(c); }
787 inline void push_front(const QString &s) { prepend(s); }
788 void shrink_to_fit() { squeeze(); }
789
790 static inline QString fromStdString(const std::string &s);
791 inline std::string toStdString() const;
792 static inline QString fromStdWString(const std::wstring &s);
793 inline std::wstring toStdWString() const;
794
795#if defined(Q_STDLIB_UNICODE_STRINGS) || defined(Q_QDOC)
796 static inline QString fromStdU16String(const std::u16string &s);
797 inline std::u16string toStdU16String() const;
798 static inline QString fromStdU32String(const std::u32string &s);
799 inline std::u32string toStdU32String() const;
800#endif
801
802#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
803 static QString fromCFString(CFStringRef string);
804 CFStringRef toCFString() const Q_DECL_CF_RETURNS_RETAINED;
805 static QString fromNSString(const NSString *string);
806 NSString *toNSString() const Q_DECL_NS_RETURNS_AUTORELEASED;
807#endif
808 // compatibility
809#if QT_DEPRECATED_SINCE(5, 9)
810 struct Null { };
811 QT_DEPRECATED_X("use QString()")
812 static const Null null;
813 inline QString(const Null &): d(Data::sharedNull()) {}
814 inline QString &operator=(const Null &) { *this = QString(); return *this; }
815#endif
816 inline bool isNull() const { return d == Data::sharedNull(); }
817
818
819 bool isSimpleText() const;
820 bool isRightToLeft() const;
821
822 QString(int size, Qt::Initialization);
823 Q_DECL_CONSTEXPR inline QString(QStringDataPtr dd) : d(dd.ptr) {}
824
825private:
826#if defined(QT_NO_CAST_FROM_ASCII)
827 QString &operator+=(const char *s);
828 QString &operator+=(const QByteArray &s);
829 QString(const char *ch);
830 QString(const QByteArray &a);
831 QString &operator=(const char *ch);
832 QString &operator=(const QByteArray &a);
833#endif
834
835 Data *d;
836
837 friend inline bool operator==(QChar, const QString &) Q_DECL_NOTHROW;
838 friend inline bool operator< (QChar, const QString &) Q_DECL_NOTHROW;
839 friend inline bool operator> (QChar, const QString &) Q_DECL_NOTHROW;
840 friend inline bool operator==(QChar, const QStringRef &) Q_DECL_NOTHROW;
841 friend inline bool operator< (QChar, const QStringRef &) Q_DECL_NOTHROW;
842 friend inline bool operator> (QChar, const QStringRef &) Q_DECL_NOTHROW;
843 friend inline bool operator==(QChar, QLatin1String) Q_DECL_NOTHROW;
844 friend inline bool operator< (QChar, QLatin1String) Q_DECL_NOTHROW;
845 friend inline bool operator> (QChar, QLatin1String) Q_DECL_NOTHROW;
846
847 void reallocData(uint alloc, bool grow = false);
848#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
849 void expand(int i);
850#endif
851 QString multiArg(int numArgs, const QString **args) const;
852 static int compare_helper(const QChar *data1, int length1,
853 const QChar *data2, int length2,
854 Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW;
855 static int compare_helper(const QChar *data1, int length1,
856 const char *data2, int length2,
857 Qt::CaseSensitivity cs = Qt::CaseSensitive);
858 static int compare_helper(const QChar *data1, int length1,
859 QLatin1String s2,
860 Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW;
861 static int localeAwareCompare_helper(const QChar *data1, int length1,
862 const QChar *data2, int length2);
863 static QString toLower_helper(const QString &str);
864 static QString toLower_helper(QString &str);
865 static QString toUpper_helper(const QString &str);
866 static QString toUpper_helper(QString &str);
867 static QString toCaseFolded_helper(const QString &str);
868 static QString toCaseFolded_helper(QString &str);
869 static QString trimmed_helper(const QString &str);
870 static QString trimmed_helper(QString &str);
871 static QString simplified_helper(const QString &str);
872 static QString simplified_helper(QString &str);
873 static Data *fromLatin1_helper(const char *str, int size = -1);
874 static Data *fromAscii_helper(const char *str, int size = -1);
875 static QString fromUtf8_helper(const char *str, int size);
876 static QString fromLocal8Bit_helper(const char *, int size);
877 static QByteArray toLatin1_helper(const QString &);
878 static QByteArray toLatin1_helper(const QChar *data, int size);
879 static QByteArray toLatin1_helper_inplace(QString &);
880 static QByteArray toUtf8_helper(const QString &);
881 static QByteArray toLocal8Bit_helper(const QChar *data, int size);
882 static int toUcs4_helper(const ushort *uc, int length, uint *out);
883 static qlonglong toIntegral_helper(const QChar *data, int len, bool *ok, int base);
884 static qulonglong toIntegral_helper(const QChar *data, uint len, bool *ok, int base);
885 void replace_helper(uint *indices, int nIndices, int blen, const QChar *after, int alen);
886 friend class QCharRef;
887 friend class QTextCodec;
888 friend class QStringRef;
889 friend class QStringView;
890 friend class QByteArray;
891 friend class QCollator;
892 friend struct QAbstractConcatenable;
893
894 template <typename T> static
895 T toIntegral_helper(const QChar *data, int len, bool *ok, int base)
896 {
897 // ### Qt6: use std::conditional<std::is_unsigned<T>::value, qulonglong, qlonglong>::type
898 const bool isUnsigned = T(0) < T(-1);
899 typedef typename QtPrivate::QConditional<isUnsigned, qulonglong, qlonglong>::Type Int64;
900 typedef typename QtPrivate::QConditional<isUnsigned, uint, int>::Type Int32;
901
902 // we select the right overload by casting size() to int or uint
903 Int64 val = toIntegral_helper(data, Int32(len), ok, base);
904 if (T(val) != val) {
905 if (ok)
906 *ok = false;
907 val = 0;
908 }
909 return T(val);
910 }
911
912public:
913 typedef Data * DataPtr;
914 inline DataPtr &data_ptr() { return d; }
915};
916
917//
918// QStringView inline members that require QString:
919//
920QString QStringView::toString() const
921{ return Q_ASSERT(size() == length()), QString(data(), length()); }
922
923//
924// QString inline members
925//
926inline QString::QString(QLatin1String aLatin1) : d(fromLatin1_helper(aLatin1.latin1(), aLatin1.size()))
927{ }
928inline int QString::length() const
929{ return d->size; }
930inline const QChar QString::at(int i) const
931{ Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; }
932inline const QChar QString::operator[](int i) const
933{ Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; }
934inline const QChar QString::operator[](uint i) const
935{ Q_ASSERT(i < uint(size())); return d->data()[i]; }
936inline bool QString::isEmpty() const
937{ return d->size == 0; }
938inline const QChar *QString::unicode() const
939{ return reinterpret_cast<const QChar*>(d->data()); }
940inline const QChar *QString::data() const
941{ return reinterpret_cast<const QChar*>(d->data()); }
942inline QChar *QString::data()
943{ detach(); return reinterpret_cast<QChar*>(d->data()); }
944inline const QChar *QString::constData() const
945{ return reinterpret_cast<const QChar*>(d->data()); }
946inline void QString::detach()
947{ if (d->ref.isShared() || (d->offset != sizeof(QStringData))) reallocData(uint(d->size) + 1u); }
948inline bool QString::isDetached() const
949{ return !d->ref.isShared(); }
950inline void QString::clear()
951{ if (!isNull()) *this = QString(); }
952inline QString::QString(const QString &other) Q_DECL_NOTHROW : d(other.d)
953{ Q_ASSERT(&other != this); d->ref.ref(); }
954inline int QString::capacity() const
955{ return d->alloc ? d->alloc - 1 : 0; }
956inline QString &QString::setNum(short n, int base)
957{ return setNum(qlonglong(n), base); }
958inline QString &QString::setNum(ushort n, int base)
959{ return setNum(qulonglong(n), base); }
960inline QString &QString::setNum(int n, int base)
961{ return setNum(qlonglong(n), base); }
962inline QString &QString::setNum(uint n, int base)
963{ return setNum(qulonglong(n), base); }
964inline QString &QString::setNum(long n, int base)
965{ return setNum(qlonglong(n), base); }
966inline QString &QString::setNum(ulong n, int base)
967{ return setNum(qulonglong(n), base); }
968inline QString &QString::setNum(float n, char f, int prec)
969{ return setNum(double(n),f,prec); }
970inline QString QString::arg(int a, int fieldWidth, int base, QChar fillChar) const
971{ return arg(qlonglong(a), fieldWidth, base, fillChar); }
972inline QString QString::arg(uint a, int fieldWidth, int base, QChar fillChar) const
973{ return arg(qulonglong(a), fieldWidth, base, fillChar); }
974inline QString QString::arg(long a, int fieldWidth, int base, QChar fillChar) const
975{ return arg(qlonglong(a), fieldWidth, base, fillChar); }
976inline QString QString::arg(ulong a, int fieldWidth, int base, QChar fillChar) const
977{ return arg(qulonglong(a), fieldWidth, base, fillChar); }
978inline QString QString::arg(short a, int fieldWidth, int base, QChar fillChar) const
979{ return arg(qlonglong(a), fieldWidth, base, fillChar); }
980inline QString QString::arg(ushort a, int fieldWidth, int base, QChar fillChar) const
981{ return arg(qulonglong(a), fieldWidth, base, fillChar); }
982inline QString QString::arg(const QString &a1, const QString &a2) const
983{ const QString *args[2] = { &a1, &a2 }; return multiArg(2, args); }
984inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3) const
985{ const QString *args[3] = { &a1, &a2, &a3 }; return multiArg(3, args); }
986inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
987 const QString &a4) const
988{ const QString *args[4] = { &a1, &a2, &a3, &a4 }; return multiArg(4, args); }
989inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
990 const QString &a4, const QString &a5) const
991{ const QString *args[5] = { &a1, &a2, &a3, &a4, &a5 }; return multiArg(5, args); }
992inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
993 const QString &a4, const QString &a5, const QString &a6) const
994{ const QString *args[6] = { &a1, &a2, &a3, &a4, &a5, &a6 }; return multiArg(6, args); }
995inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
996 const QString &a4, const QString &a5, const QString &a6,
997 const QString &a7) const
998{ const QString *args[7] = { &a1, &a2, &a3, &a4, &a5, &a6, &a7 }; return multiArg(7, args); }
999inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
1000 const QString &a4, const QString &a5, const QString &a6,
1001 const QString &a7, const QString &a8) const
1002{ const QString *args[8] = { &a1, &a2, &a3, &a4, &a5, &a6, &a7, &a8 }; return multiArg(8, args); }
1003inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
1004 const QString &a4, const QString &a5, const QString &a6,
1005 const QString &a7, const QString &a8, const QString &a9) const
1006{ const QString *args[9] = { &a1, &a2, &a3, &a4, &a5, &a6, &a7, &a8, &a9 }; return multiArg(9, args); }
1007
1008inline QString QString::section(QChar asep, int astart, int aend, SectionFlags aflags) const
1009{ return section(QString(asep), astart, aend, aflags); }
1010
1011QT_WARNING_PUSH
1012QT_WARNING_DISABLE_MSVC(4127) // "conditional expression is constant"
1013QT_WARNING_DISABLE_INTEL(111) // "statement is unreachable"
1014
1015inline int QString::toWCharArray(wchar_t *array) const
1016{
1017 if (sizeof(wchar_t) == sizeof(QChar)) {
1018 memcpy(array, d->data(), sizeof(QChar) * size());
1019 return size();
1020 } else {
1021 return toUcs4_helper(d->data(), size(), reinterpret_cast<uint *>(array));
1022 }
1023}
1024
1025QT_WARNING_POP
1026
1027inline QString QString::fromWCharArray(const wchar_t *string, int size)
1028{
1029 return sizeof(wchar_t) == sizeof(QChar) ? fromUtf16(reinterpret_cast<const ushort *>(string), size)
1030 : fromUcs4(reinterpret_cast<const uint *>(string), size);
1031}
1032
1033
1034class Q_CORE_EXPORT QCharRef {
1035 QString &s;
1036 int i;
1037 inline QCharRef(QString &str, int idx)
1038 : s(str),i(idx) {}
1039 friend class QString;
1040public:
1041
1042 // most QChar operations repeated here
1043
1044 // all this is not documented: We just say "like QChar" and let it be.
1045 inline operator QChar() const
1046 { return i < s.d->size ? s.d->data()[i] : 0; }
1047 inline QCharRef &operator=(QChar c)
1048 { if (i >= s.d->size) s.resize(i + 1, QLatin1Char(' ')); else s.detach();
1049 s.d->data()[i] = c.unicode(); return *this; }
1050
1051 // An operator= for each QChar cast constructors
1052#ifndef QT_NO_CAST_FROM_ASCII
1053 inline QT_ASCII_CAST_WARN QCharRef &operator=(char c)
1054 { return operator=(QChar::fromLatin1(c)); }
1055 inline QT_ASCII_CAST_WARN QCharRef &operator=(uchar c)
1056 { return operator=(QChar::fromLatin1(c)); }
1057#endif
1058 inline QCharRef &operator=(const QCharRef &c) { return operator=(QChar(c)); }
1059 inline QCharRef &operator=(ushort rc) { return operator=(QChar(rc)); }
1060 inline QCharRef &operator=(short rc) { return operator=(QChar(rc)); }
1061 inline QCharRef &operator=(uint rc) { return operator=(QChar(rc)); }
1062 inline QCharRef &operator=(int rc) { return operator=(QChar(rc)); }
1063
1064 // each function...
1065 inline bool isNull() const { return QChar(*this).isNull(); }
1066 inline bool isPrint() const { return QChar(*this).isPrint(); }
1067 inline bool isPunct() const { return QChar(*this).isPunct(); }
1068 inline bool isSpace() const { return QChar(*this).isSpace(); }
1069 inline bool isMark() const { return QChar(*this).isMark(); }
1070 inline bool isLetter() const { return QChar(*this).isLetter(); }
1071 inline bool isNumber() const { return QChar(*this).isNumber(); }
1072 inline bool isLetterOrNumber() { return QChar(*this).isLetterOrNumber(); }
1073 inline bool isDigit() const { return QChar(*this).isDigit(); }
1074 inline bool isLower() const { return QChar(*this).isLower(); }
1075 inline bool isUpper() const { return QChar(*this).isUpper(); }
1076 inline bool isTitleCase() const { return QChar(*this).isTitleCase(); }
1077
1078 inline int digitValue() const { return QChar(*this).digitValue(); }
1079 QChar toLower() const { return QChar(*this).toLower(); }
1080 QChar toUpper() const { return QChar(*this).toUpper(); }
1081 QChar toTitleCase () const { return QChar(*this).toTitleCase(); }
1082
1083 QChar::Category category() const { return QChar(*this).category(); }
1084 QChar::Direction direction() const { return QChar(*this).direction(); }
1085 QChar::JoiningType joiningType() const { return QChar(*this).joiningType(); }
1086#if QT_DEPRECATED_SINCE(5, 3)
1087 QT_DEPRECATED QChar::Joining joining() const
1088 {
1089 switch (QChar(*this).joiningType()) {
1090 case QChar::Joining_Causing: return QChar::Center;
1091 case QChar::Joining_Dual: return QChar::Dual;
1092 case QChar::Joining_Right: return QChar::Right;
1093 case QChar::Joining_None:
1094 case QChar::Joining_Left:
1095 case QChar::Joining_Transparent:
1096 default: return QChar::OtherJoining;
1097 }
1098 }
1099#endif
1100 bool hasMirrored() const { return QChar(*this).hasMirrored(); }
1101 QChar mirroredChar() const { return QChar(*this).mirroredChar(); }
1102 QString decomposition() const { return QChar(*this).decomposition(); }
1103 QChar::Decomposition decompositionTag() const { return QChar(*this).decompositionTag(); }
1104 uchar combiningClass() const { return QChar(*this).combiningClass(); }
1105
1106 inline QChar::Script script() const { return QChar(*this).script(); }
1107
1108 QChar::UnicodeVersion unicodeVersion() const { return QChar(*this).unicodeVersion(); }
1109
1110 inline uchar cell() const { return QChar(*this).cell(); }
1111 inline uchar row() const { return QChar(*this).row(); }
1112 inline void setCell(uchar cell);
1113 inline void setRow(uchar row);
1114
1115#if QT_DEPRECATED_SINCE(5, 0)
1116 QT_DEPRECATED char toAscii() const { return QChar(*this).toLatin1(); }
1117#endif
1118 char toLatin1() const { return QChar(*this).toLatin1(); }
1119 ushort unicode() const { return QChar(*this).unicode(); }
1120 ushort& unicode() { return s.data()[i].unicode(); }
1121
1122};
1123Q_DECLARE_TYPEINFO(QCharRef, Q_MOVABLE_TYPE);
1124
1125inline void QCharRef::setRow(uchar arow) { QChar(*this).setRow(arow); }
1126inline void QCharRef::setCell(uchar acell) { QChar(*this).setCell(acell); }
1127
1128
1129inline QString::QString() Q_DECL_NOTHROW : d(Data::sharedNull()) {}
1130inline QString::~QString() { if (!d->ref.deref()) Data::deallocate(d); }
1131
1132inline void QString::reserve(int asize)
1133{
1134 if (d->ref.isShared() || uint(asize) >= d->alloc)
1135 reallocData(qMax(asize, d->size) + 1u);
1136
1137 if (!d->capacityReserved) {
1138 // cannot set unconditionally, since d could be the shared_null/shared_empty (which is const)
1139 d->capacityReserved = true;
1140 }
1141}
1142
1143inline void QString::squeeze()
1144{
1145 if (d->ref.isShared() || uint(d->size) + 1u < d->alloc)
1146 reallocData(uint(d->size) + 1u);
1147
1148 if (d->capacityReserved) {
1149 // cannot set unconditionally, since d could be shared_null or
1150 // otherwise static.
1151 d->capacityReserved = false;
1152 }
1153}
1154
1155inline QString &QString::setUtf16(const ushort *autf16, int asize)
1156{ return setUnicode(reinterpret_cast<const QChar *>(autf16), asize); }
1157inline QCharRef QString::operator[](int i)
1158{ Q_ASSERT(i >= 0); return QCharRef(*this, i); }
1159inline QCharRef QString::operator[](uint i)
1160{ return QCharRef(*this, i); }
1161inline QCharRef QString::front() { return operator[](0); }
1162inline QCharRef QString::back() { return operator[](size() - 1); }
1163inline QString::iterator QString::begin()
1164{ detach(); return reinterpret_cast<QChar*>(d->data()); }
1165inline QString::const_iterator QString::begin() const
1166{ return reinterpret_cast<const QChar*>(d->data()); }
1167inline QString::const_iterator QString::cbegin() const
1168{ return reinterpret_cast<const QChar*>(d->data()); }
1169inline QString::const_iterator QString::constBegin() const
1170{ return reinterpret_cast<const QChar*>(d->data()); }
1171inline QString::iterator QString::end()
1172{ detach(); return reinterpret_cast<QChar*>(d->data() + d->size); }
1173inline QString::const_iterator QString::end() const
1174{ return reinterpret_cast<const QChar*>(d->data() + d->size); }
1175inline QString::const_iterator QString::cend() const
1176{ return reinterpret_cast<const QChar*>(d->data() + d->size); }
1177inline QString::const_iterator QString::constEnd() const
1178{ return reinterpret_cast<const QChar*>(d->data() + d->size); }
1179inline bool QString::contains(const QString &s, Qt::CaseSensitivity cs) const
1180{ return indexOf(s, 0, cs) != -1; }
1181inline bool QString::contains(const QStringRef &s, Qt::CaseSensitivity cs) const
1182{ return indexOf(s, 0, cs) != -1; }
1183inline bool QString::contains(QLatin1String s, Qt::CaseSensitivity cs) const
1184{ return indexOf(s, 0, cs) != -1; }
1185inline bool QString::contains(QChar c, Qt::CaseSensitivity cs) const
1186{ return indexOf(c, 0, cs) != -1; }
1187
1188#if QT_DEPRECATED_SINCE(5, 9)
1189inline bool operator==(QString::Null, QString::Null) { return true; }
1190QT_DEPRECATED_X("use QString::isNull()")
1191inline bool operator==(QString::Null, const QString &s) { return s.isNull(); }
1192QT_DEPRECATED_X("use QString::isNull()")
1193inline bool operator==(const QString &s, QString::Null) { return s.isNull(); }
1194inline bool operator!=(QString::Null, QString::Null) { return false; }
1195QT_DEPRECATED_X("use !QString::isNull()")
1196inline bool operator!=(QString::Null, const QString &s) { return !s.isNull(); }
1197QT_DEPRECATED_X("use !QString::isNull()")
1198inline bool operator!=(const QString &s, QString::Null) { return !s.isNull(); }
1199#endif
1200
1201inline bool operator==(QLatin1String s1, QLatin1String s2) Q_DECL_NOTHROW
1202{ return s1.size() == s2.size() && (!s1.size() || !memcmp(s1.latin1(), s2.latin1(), s1.size())); }
1203inline bool operator!=(QLatin1String s1, QLatin1String s2) Q_DECL_NOTHROW
1204{ return !operator==(s1, s2); }
1205inline bool operator<(QLatin1String s1, QLatin1String s2) Q_DECL_NOTHROW
1206{
1207 const int len = qMin(s1.size(), s2.size());
1208 const int r = len ? memcmp(s1.latin1(), s2.latin1(), len) : 0;
1209 return r < 0 || (r == 0 && s1.size() < s2.size());
1210}
1211inline bool operator>(QLatin1String s1, QLatin1String s2) Q_DECL_NOTHROW
1212{ return operator<(s2, s1); }
1213inline bool operator<=(QLatin1String s1, QLatin1String s2) Q_DECL_NOTHROW
1214{ return !operator>(s1, s2); }
1215inline bool operator>=(QLatin1String s1, QLatin1String s2) Q_DECL_NOTHROW
1216{ return !operator<(s1, s2); }
1217
1218inline bool QLatin1String::operator==(const QString &s) const Q_DECL_NOTHROW
1219{ return s == *this; }
1220inline bool QLatin1String::operator!=(const QString &s) const Q_DECL_NOTHROW
1221{ return s != *this; }
1222inline bool QLatin1String::operator>(const QString &s) const Q_DECL_NOTHROW
1223{ return s < *this; }
1224inline bool QLatin1String::operator<(const QString &s) const Q_DECL_NOTHROW
1225{ return s > *this; }
1226inline bool QLatin1String::operator>=(const QString &s) const Q_DECL_NOTHROW
1227{ return s <= *this; }
1228inline bool QLatin1String::operator<=(const QString &s) const Q_DECL_NOTHROW
1229{ return s >= *this; }
1230
1231#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
1232inline bool QString::operator==(const char *s) const
1233{ return QString::compare_helper(constData(), size(), s, -1) == 0; }
1234inline bool QString::operator!=(const char *s) const
1235{ return QString::compare_helper(constData(), size(), s, -1) != 0; }
1236inline bool QString::operator<(const char *s) const
1237{ return QString::compare_helper(constData(), size(), s, -1) < 0; }
1238inline bool QString::operator>(const char *s) const
1239{ return QString::compare_helper(constData(), size(), s, -1) > 0; }
1240inline bool QString::operator<=(const char *s) const
1241{ return QString::compare_helper(constData(), size(), s, -1) <= 0; }
1242inline bool QString::operator>=(const char *s) const
1243{ return QString::compare_helper(constData(), size(), s, -1) >= 0; }
1244
1245inline QT_ASCII_CAST_WARN bool operator==(const char *s1, const QString &s2)
1246{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) == 0; }
1247inline QT_ASCII_CAST_WARN bool operator!=(const char *s1, const QString &s2)
1248{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) != 0; }
1249inline QT_ASCII_CAST_WARN bool operator<(const char *s1, const QString &s2)
1250{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) > 0; }
1251inline QT_ASCII_CAST_WARN bool operator>(const char *s1, const QString &s2)
1252{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) < 0; }
1253inline QT_ASCII_CAST_WARN bool operator<=(const char *s1, const QString &s2)
1254{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) >= 0; }
1255inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, const QString &s2)
1256{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) <= 0; }
1257
1258inline QT_ASCII_CAST_WARN bool operator==(const char *s1, QLatin1String s2)
1259{ return QString::fromUtf8(s1) == s2; }
1260inline QT_ASCII_CAST_WARN bool operator!=(const char *s1, QLatin1String s2)
1261{ return QString::fromUtf8(s1) != s2; }
1262inline QT_ASCII_CAST_WARN bool operator<(const char *s1, QLatin1String s2)
1263{ return (QString::fromUtf8(s1) < s2); }
1264inline QT_ASCII_CAST_WARN bool operator>(const char *s1, QLatin1String s2)
1265{ return (QString::fromUtf8(s1) > s2); }
1266inline QT_ASCII_CAST_WARN bool operator<=(const char *s1, QLatin1String s2)
1267{ return (QString::fromUtf8(s1) <= s2); }
1268inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, QLatin1String s2)
1269{ return (QString::fromUtf8(s1) >= s2); }
1270
1271inline QT_ASCII_CAST_WARN bool QLatin1String::operator==(const char *s) const
1272{ return QString::fromUtf8(s) == *this; }
1273inline QT_ASCII_CAST_WARN bool QLatin1String::operator!=(const char *s) const
1274{ return QString::fromUtf8(s) != *this; }
1275inline QT_ASCII_CAST_WARN bool QLatin1String::operator<(const char *s) const
1276{ return QString::fromUtf8(s) > *this; }
1277inline QT_ASCII_CAST_WARN bool QLatin1String::operator>(const char *s) const
1278{ return QString::fromUtf8(s) < *this; }
1279inline QT_ASCII_CAST_WARN bool QLatin1String::operator<=(const char *s) const
1280{ return QString::fromUtf8(s) >= *this; }
1281inline QT_ASCII_CAST_WARN bool QLatin1String::operator>=(const char *s) const
1282{ return QString::fromUtf8(s) <= *this; }
1283
1284inline QT_ASCII_CAST_WARN bool QLatin1String::operator==(const QByteArray &s) const
1285{ return QString::fromUtf8(s) == *this; }
1286inline QT_ASCII_CAST_WARN bool QLatin1String::operator!=(const QByteArray &s) const
1287{ return QString::fromUtf8(s) != *this; }
1288inline QT_ASCII_CAST_WARN bool QLatin1String::operator<(const QByteArray &s) const
1289{ return QString::fromUtf8(s) > *this; }
1290inline QT_ASCII_CAST_WARN bool QLatin1String::operator>(const QByteArray &s) const
1291{ return QString::fromUtf8(s) < *this; }
1292inline QT_ASCII_CAST_WARN bool QLatin1String::operator<=(const QByteArray &s) const
1293{ return QString::fromUtf8(s) >= *this; }
1294inline QT_ASCII_CAST_WARN bool QLatin1String::operator>=(const QByteArray &s) const
1295{ return QString::fromUtf8(s) <= *this; }
1296
1297inline QT_ASCII_CAST_WARN bool QString::operator==(const QByteArray &s) const
1298{ return QString::compare_helper(constData(), size(), s.constData(), qstrnlen(s.constData(), s.size())) == 0; }
1299inline QT_ASCII_CAST_WARN bool QString::operator!=(const QByteArray &s) const
1300{ return QString::compare_helper(constData(), size(), s.constData(), qstrnlen(s.constData(), s.size())) != 0; }
1301inline QT_ASCII_CAST_WARN bool QString::operator<(const QByteArray &s) const
1302{ return QString::compare_helper(constData(), size(), s.constData(), s.size()) < 0; }
1303inline QT_ASCII_CAST_WARN bool QString::operator>(const QByteArray &s) const
1304{ return QString::compare_helper(constData(), size(), s.constData(), s.size()) > 0; }
1305inline QT_ASCII_CAST_WARN bool QString::operator<=(const QByteArray &s) const
1306{ return QString::compare_helper(constData(), size(), s.constData(), s.size()) <= 0; }
1307inline QT_ASCII_CAST_WARN bool QString::operator>=(const QByteArray &s) const
1308{ return QString::compare_helper(constData(), size(), s.constData(), s.size()) >= 0; }
1309
1310inline bool QByteArray::operator==(const QString &s) const
1311{ return QString::compare_helper(s.constData(), s.size(), constData(), qstrnlen(constData(), size())) == 0; }
1312inline bool QByteArray::operator!=(const QString &s) const
1313{ return QString::compare_helper(s.constData(), s.size(), constData(), qstrnlen(constData(), size())) != 0; }
1314inline bool QByteArray::operator<(const QString &s) const
1315{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) > 0; }
1316inline bool QByteArray::operator>(const QString &s) const
1317{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) < 0; }
1318inline bool QByteArray::operator<=(const QString &s) const
1319{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) >= 0; }
1320inline bool QByteArray::operator>=(const QString &s) const
1321{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) <= 0; }
1322#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
1323
1324#ifndef QT_NO_CAST_TO_ASCII
1325inline QByteArray &QByteArray::append(const QString &s)
1326{ return append(s.toUtf8()); }
1327inline QByteArray &QByteArray::insert(int i, const QString &s)
1328{ return insert(i, s.toUtf8()); }
1329inline QByteArray &QByteArray::replace(char c, const QString &after)
1330{ return replace(c, after.toUtf8()); }
1331inline QByteArray &QByteArray::replace(const QString &before, const char *after)
1332{ return replace(before.toUtf8(), after); }
1333inline QByteArray &QByteArray::replace(const QString &before, const QByteArray &after)
1334{ return replace(before.toUtf8(), after); }
1335inline QByteArray &QByteArray::operator+=(const QString &s)
1336{ return operator+=(s.toUtf8()); }
1337inline int QByteArray::indexOf(const QString &s, int from) const
1338{ return indexOf(s.toUtf8(), from); }
1339inline int QByteArray::lastIndexOf(const QString &s, int from) const
1340{ return lastIndexOf(s.toUtf8(), from); }
1341#endif // QT_NO_CAST_TO_ASCII
1342
1343#if !defined(QT_USE_FAST_OPERATOR_PLUS) && !defined(QT_USE_QSTRINGBUILDER)
1344inline const QString operator+(const QString &s1, const QString &s2)
1345{ QString t(s1); t += s2; return t; }
1346inline const QString operator+(const QString &s1, QChar s2)
1347{ QString t(s1); t += s2; return t; }
1348inline const QString operator+(QChar s1, const QString &s2)
1349{ QString t(s1); t += s2; return t; }
1350# if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
1351inline QT_ASCII_CAST_WARN const QString operator+(const QString &s1, const char *s2)
1352{ QString t(s1); t += QString::fromUtf8(s2); return t; }
1353inline QT_ASCII_CAST_WARN const QString operator+(const char *s1, const QString &s2)
1354{ QString t = QString::fromUtf8(s1); t += s2; return t; }
1355inline QT_ASCII_CAST_WARN const QString operator+(char c, const QString &s)
1356{ QString t = s; t.prepend(QChar::fromLatin1(c)); return t; }
1357inline QT_ASCII_CAST_WARN const QString operator+(const QString &s, char c)
1358{ QString t = s; t += QChar::fromLatin1(c); return t; }
1359inline QT_ASCII_CAST_WARN const QString operator+(const QByteArray &ba, const QString &s)
1360{ QString t = QString::fromUtf8(ba); t += s; return t; }
1361inline QT_ASCII_CAST_WARN const QString operator+(const QString &s, const QByteArray &ba)
1362{ QString t(s); t += QString::fromUtf8(ba); return t; }
1363# endif // QT_NO_CAST_FROM_ASCII
1364#endif // QT_USE_QSTRINGBUILDER
1365
1366inline std::string QString::toStdString() const
1367{ return toUtf8().toStdString(); }
1368
1369inline QString QString::fromStdString(const std::string &s)
1370{ return fromUtf8(s.data(), int(s.size())); }
1371
1372inline std::wstring QString::toStdWString() const
1373{
1374 std::wstring str;
1375 str.resize(length());
1376
1377#ifdef Q_CC_MSVC
1378 // VS2005 crashes if the string is empty
1379 if (!length())
1380 return str;
1381#endif
1382
1383 str.resize(toWCharArray(&(*str.begin())));
1384 return str;
1385}
1386
1387inline QString QString::fromStdWString(const std::wstring &s)
1388{ return fromWCharArray(s.data(), int(s.size())); }
1389
1390#if defined(Q_STDLIB_UNICODE_STRINGS)
1391inline QString QString::fromStdU16String(const std::u16string &s)
1392{ return fromUtf16(s.data(), int(s.size())); }
1393
1394inline std::u16string QString::toStdU16String() const
1395{ return std::u16string(reinterpret_cast<const char16_t*>(utf16()), length()); }
1396
1397inline QString QString::fromStdU32String(const std::u32string &s)
1398{ return fromUcs4(s.data(), int(s.size())); }
1399
1400inline std::u32string QString::toStdU32String() const
1401{
1402 std::u32string u32str(length(), char32_t(0));
1403 int len = toUcs4_helper(d->data(), length(), reinterpret_cast<uint*>(&u32str[0]));
1404 u32str.resize(len);
1405 return u32str;
1406}
1407#endif
1408
1409#if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE))
1410Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QString &);
1411Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QString &);
1412#endif
1413
1414Q_DECLARE_SHARED(QString)
1415Q_DECLARE_OPERATORS_FOR_FLAGS(QString::SectionFlags)
1416
1417
1418class Q_CORE_EXPORT QStringRef {
1419 const QString *m_string;
1420 int m_position;
1421 int m_size;
1422public:
1423 typedef QString::size_type size_type;
1424 typedef QString::value_type value_type;
1425 typedef const QChar *const_iterator;
1426 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
1427 typedef QString::const_pointer const_pointer;
1428 typedef QString::const_reference const_reference;
1429
1430 // ### Qt 6: make this constructor constexpr, after the destructor is made trivial
1431 inline QStringRef() : m_string(nullptr), m_position(0), m_size(0) {}
1432 inline QStringRef(const QString *string, int position, int size);
1433 inline QStringRef(const QString *string);
1434
1435#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
1436 // ### Qt 6: remove all of these, the implicit ones are fine
1437 QStringRef(const QStringRef &other) Q_DECL_NOTHROW
1438 :m_string(other.m_string), m_position(other.m_position), m_size(other.m_size)
1439 {}
1440#ifdef Q_COMPILER_RVALUE_REFS
1441 QStringRef(QStringRef &&other) Q_DECL_NOTHROW : m_string(other.m_string), m_position(other.m_position), m_size(other.m_size) {}
1442 QStringRef &operator=(QStringRef &&other) Q_DECL_NOTHROW { return *this = other; }
1443#endif
1444 QStringRef &operator=(const QStringRef &other) Q_DECL_NOTHROW
1445 {
1446 m_string = other.m_string; m_position = other.m_position;
1447 m_size = other.m_size; return *this;
1448 }
1449 inline ~QStringRef(){}
1450#endif // Qt < 6.0.0
1451
1452 inline const QString *string() const { return m_string; }
1453 inline int position() const { return m_position; }
1454 inline int size() const { return m_size; }
1455 inline int count() const { return m_size; }
1456 inline int length() const { return m_size; }
1457
1458 int indexOf(const QString &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1459 int indexOf(QChar ch, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1460 int indexOf(QLatin1String str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1461 int indexOf(const QStringRef &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1462 int lastIndexOf(const QString &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1463 int lastIndexOf(QChar ch, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1464 int lastIndexOf(QLatin1String str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1465 int lastIndexOf(const QStringRef &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1466
1467 inline bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1468 inline bool contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1469 inline bool contains(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1470 inline bool contains(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1471
1472 int count(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1473 int count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1474 int count(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1475
1476 Q_REQUIRED_RESULT QVector<QStringRef> split(const QString &sep, QString::SplitBehavior behavior = QString::KeepEmptyParts,
1477 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1478 Q_REQUIRED_RESULT QVector<QStringRef> split(QChar sep, QString::SplitBehavior behavior = QString::KeepEmptyParts,
1479 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1480
1481 Q_REQUIRED_RESULT QStringRef left(int n) const;
1482 Q_REQUIRED_RESULT QStringRef right(int n) const;
1483 Q_REQUIRED_RESULT QStringRef mid(int pos, int n = -1) const;
1484 Q_REQUIRED_RESULT QStringRef chopped(int n) const
1485 { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return left(size() - n); }
1486
1487 void truncate(int pos) Q_DECL_NOTHROW { m_size = qBound(0, pos, m_size); }
1488 void chop(int n) Q_DECL_NOTHROW
1489 {
1490 if (n >= m_size)
1491 m_size = 0;
1492 else if (n > 0)
1493 m_size -= n;
1494 }
1495
1496 bool isRightToLeft() const;
1497
1498 Q_REQUIRED_RESULT bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW
1499 { return QtPrivate::startsWith(*this, s, cs); }
1500 bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1501 bool startsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1502#if QT_STRINGVIEW_LEVEL < 2
1503 bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1504 bool startsWith(const QStringRef &c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1505#endif
1506
1507 Q_REQUIRED_RESULT bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW
1508 { return QtPrivate::endsWith(*this, s, cs); }
1509 bool endsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1510 bool endsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1511#if QT_STRINGVIEW_LEVEL < 2
1512 bool endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1513 bool endsWith(const QStringRef &c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1514#endif
1515
1516 inline QStringRef &operator=(const QString *string);
1517
1518 inline const QChar *unicode() const
1519 {
1520 if (!m_string)
1521 return reinterpret_cast<const QChar *>(QString::Data::sharedNull()->data());
1522 return m_string->unicode() + m_position;
1523 }
1524 inline const QChar *data() const { return unicode(); }
1525 inline const QChar *constData() const { return unicode(); }
1526
1527 inline const_iterator begin() const { return unicode(); }
1528 inline const_iterator cbegin() const { return unicode(); }
1529 inline const_iterator constBegin() const { return unicode(); }
1530 inline const_iterator end() const { return unicode() + size(); }
1531 inline const_iterator cend() const { return unicode() + size(); }
1532 inline const_iterator constEnd() const { return unicode() + size(); }
1533 inline const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
1534 inline const_reverse_iterator crbegin() const { return rbegin(); }
1535 inline const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
1536 inline const_reverse_iterator crend() const { return rend(); }
1537
1538#if QT_DEPRECATED_SINCE(5, 0)
1539 Q_REQUIRED_RESULT QT_DEPRECATED QByteArray toAscii() const
1540 { return toLatin1(); }
1541#endif
1542 Q_REQUIRED_RESULT QByteArray toLatin1() const;
1543 Q_REQUIRED_RESULT QByteArray toUtf8() const;
1544 Q_REQUIRED_RESULT QByteArray toLocal8Bit() const;
1545 Q_REQUIRED_RESULT QVector<uint> toUcs4() const;
1546
1547 inline void clear() { m_string = nullptr; m_position = m_size = 0; }
1548 QString toString() const;
1549 inline bool isEmpty() const { return m_size == 0; }
1550 inline bool isNull() const { return m_string == nullptr || m_string->isNull(); }
1551
1552 QStringRef appendTo(QString *string) const;
1553
1554 inline const QChar at(int i) const
1555 { Q_ASSERT(uint(i) < uint(size())); return m_string->at(i + m_position); }
1556 QChar operator[](int i) const { return at(i); }
1557 Q_REQUIRED_RESULT QChar front() const { return at(0); }
1558 Q_REQUIRED_RESULT QChar back() const { return at(size() - 1); }
1559
1560#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
1561 // ASCII compatibility
1562 inline QT_ASCII_CAST_WARN bool operator==(const char *s) const;
1563 inline QT_ASCII_CAST_WARN bool operator!=(const char *s) const;
1564 inline QT_ASCII_CAST_WARN bool operator<(const char *s) const;
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#endif
1569
1570 int compare(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW;
1571 int compare(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW;
1572 int compare(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW;
1573#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
1574 int compare(const QByteArray &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
1575 { return QString::compare_helper(unicode(), size(), s.data(), qstrnlen(s.data(), s.size()), cs); }
1576#endif
1577 static int compare(const QStringRef &s1, const QString &s2,
1578 Qt::CaseSensitivity = Qt::CaseSensitive) Q_DECL_NOTHROW;
1579 static int compare(const QStringRef &s1, const QStringRef &s2,
1580 Qt::CaseSensitivity = Qt::CaseSensitive) Q_DECL_NOTHROW;
1581 static int compare(const QStringRef &s1, QLatin1String s2,
1582 Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW;
1583
1584 int localeAwareCompare(const QString &s) const;
1585 int localeAwareCompare(const QStringRef &s) const;
1586 static int localeAwareCompare(const QStringRef &s1, const QString &s2);
1587 static int localeAwareCompare(const QStringRef &s1, const QStringRef &s2);
1588
1589 Q_REQUIRED_RESULT QStringRef trimmed() const;
1590 short toShort(bool *ok = nullptr, int base = 10) const;
1591 ushort toUShort(bool *ok = nullptr, int base = 10) const;
1592 int toInt(bool *ok = nullptr, int base = 10) const;
1593 uint toUInt(bool *ok = nullptr, int base = 10) const;
1594 long toLong(bool *ok = nullptr, int base = 10) const;
1595 ulong toULong(bool *ok = nullptr, int base = 10) const;
1596 qlonglong toLongLong(bool *ok = nullptr, int base = 10) const;
1597 qulonglong toULongLong(bool *ok = nullptr, int base = 10) const;
1598 float toFloat(bool *ok = nullptr) const;
1599 double toDouble(bool *ok = nullptr) const;
1600};
1601Q_DECLARE_TYPEINFO(QStringRef, Q_PRIMITIVE_TYPE);
1602
1603inline QStringRef &QStringRef::operator=(const QString *aString)
1604{ m_string = aString; m_position = 0; m_size = aString?aString->size():0; return *this; }
1605
1606inline QStringRef::QStringRef(const QString *aString, int aPosition, int aSize)
1607 :m_string(aString), m_position(aPosition), m_size(aSize){}
1608
1609inline QStringRef::QStringRef(const QString *aString)
1610 :m_string(aString), m_position(0), m_size(aString?aString->size() : 0){}
1611
1612// QStringRef <> QStringRef
1613Q_CORE_EXPORT bool operator==(const QStringRef &s1, const QStringRef &s2) Q_DECL_NOTHROW;
1614inline bool operator!=(const QStringRef &s1, const QStringRef &s2) Q_DECL_NOTHROW
1615{ return !(s1 == s2); }
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 s2 < s1; }
1619inline bool operator<=(const QStringRef &s1, const QStringRef &s2) Q_DECL_NOTHROW
1620{ return !(s1 > s2); }
1621inline bool operator>=(const QStringRef &s1, const QStringRef &s2) Q_DECL_NOTHROW
1622{ return !(s1 < s2); }
1623
1624// QString <> QStringRef
1625Q_CORE_EXPORT bool operator==(const QString &lhs, const QStringRef &rhs) Q_DECL_NOTHROW;
1626inline bool operator!=(const QString &lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return lhs.compare(rhs) != 0; }
1627inline bool operator< (const QString &lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return lhs.compare(rhs) < 0; }
1628inline bool operator> (const QString &lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return lhs.compare(rhs) > 0; }
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; }
1631
1632inline bool operator==(const QStringRef &lhs, const QString &rhs) Q_DECL_NOTHROW { return rhs == lhs; }
1633inline bool operator!=(const QStringRef &lhs, const QString &rhs) Q_DECL_NOTHROW { return rhs != lhs; }
1634inline bool operator< (const QStringRef &lhs, const QString &rhs) Q_DECL_NOTHROW { return rhs > lhs; }
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; }
1638
1639inline int QString::compare(const QStringRef &s, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW
1640{ return QString::compare_helper(constData(), length(), s.constData(), s.length(), cs); }
1641inline int QString::compare(const QString &s1, const QStringRef &s2, Qt::CaseSensitivity cs) Q_DECL_NOTHROW
1642{ return QString::compare_helper(s1.constData(), s1.length(), s2.constData(), s2.length(), cs); }
1643inline int QStringRef::compare(const QString &s, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW
1644{ return QString::compare_helper(constData(), length(), s.constData(), s.length(), cs); }
1645inline int QStringRef::compare(const QStringRef &s, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW
1646{ return QString::compare_helper(constData(), length(), s.constData(), s.length(), cs); }
1647inline int QStringRef::compare(QLatin1String s, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW
1648{ return QString::compare_helper(constData(), length(), s, cs); }
1649inline int QStringRef::compare(const QStringRef &s1, const QString &s2, Qt::CaseSensitivity cs) Q_DECL_NOTHROW
1650{ return QString::compare_helper(s1.constData(), s1.length(), s2.constData(), s2.length(), cs); }
1651inline int QStringRef::compare(const QStringRef &s1, const QStringRef &s2, Qt::CaseSensitivity cs) Q_DECL_NOTHROW
1652{ return QString::compare_helper(s1.constData(), s1.length(), s2.constData(), s2.length(), cs); }
1653inline int QStringRef::compare(const QStringRef &s1, QLatin1String s2, Qt::CaseSensitivity cs) Q_DECL_NOTHROW
1654{ return QString::compare_helper(s1.constData(), s1.length(), s2, cs); }
1655
1656// QLatin1String <> QStringRef
1657Q_CORE_EXPORT bool operator==(QLatin1String lhs, const QStringRef &rhs) Q_DECL_NOTHROW;
1658inline bool operator!=(QLatin1String lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return rhs.compare(lhs) != 0; }
1659inline bool operator< (QLatin1String lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return rhs.compare(lhs) > 0; }
1660inline bool operator> (QLatin1String lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return rhs.compare(lhs) < 0; }
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; }
1663
1664inline bool operator==(const QStringRef &lhs, QLatin1String rhs) Q_DECL_NOTHROW { return rhs == lhs; }
1665inline bool operator!=(const QStringRef &lhs, QLatin1String rhs) Q_DECL_NOTHROW { return rhs != lhs; }
1666inline bool operator< (const QStringRef &lhs, QLatin1String rhs) Q_DECL_NOTHROW { return rhs > lhs; }
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; }
1670
1671// QChar <> QString
1672inline bool operator==(QChar lhs, const QString &rhs) Q_DECL_NOTHROW
1673{ return rhs.size() == 1 && lhs == rhs.front(); }
1674inline bool operator< (QChar lhs, const QString &rhs) Q_DECL_NOTHROW
1675{ return QString::compare_helper(&lhs, 1, rhs.data(), rhs.size()) < 0; }
1676inline bool operator> (QChar lhs, const QString &rhs) Q_DECL_NOTHROW
1677{ return QString::compare_helper(&lhs, 1, rhs.data(), rhs.size()) > 0; }
1678
1679inline bool operator!=(QChar lhs, const QString &rhs) Q_DECL_NOTHROW { return !(lhs == rhs); }
1680inline bool operator<=(QChar lhs, const QString &rhs) Q_DECL_NOTHROW { return !(lhs > rhs); }
1681inline bool operator>=(QChar lhs, const QString &rhs) Q_DECL_NOTHROW { return !(lhs < rhs); }
1682
1683inline bool operator==(const QString &lhs, QChar rhs) Q_DECL_NOTHROW { return rhs == lhs; }
1684inline bool operator!=(const QString &lhs, QChar rhs) Q_DECL_NOTHROW { return !(rhs == lhs); }
1685inline bool operator< (const QString &lhs, QChar rhs) Q_DECL_NOTHROW { return rhs > lhs; }
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); }
1689
1690// QChar <> QStringRef
1691inline bool operator==(QChar lhs, const QStringRef &rhs) Q_DECL_NOTHROW
1692{ return rhs.size() == 1 && lhs == rhs.front(); }
1693inline bool operator< (QChar lhs, const QStringRef &rhs) Q_DECL_NOTHROW
1694{ return QString::compare_helper(&lhs, 1, rhs.data(), rhs.size()) < 0; }
1695inline bool operator> (QChar lhs, const QStringRef &rhs) Q_DECL_NOTHROW
1696{ return QString::compare_helper(&lhs, 1, rhs.data(), rhs.size()) > 0; }
1697
1698inline bool operator!=(QChar lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return !(lhs == rhs); }
1699inline bool operator<=(QChar lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return !(lhs > rhs); }
1700inline bool operator>=(QChar lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return !(lhs < rhs); }
1701
1702inline bool operator==(const QStringRef &lhs, QChar rhs) Q_DECL_NOTHROW { return rhs == lhs; }
1703inline bool operator!=(const QStringRef &lhs, QChar rhs) Q_DECL_NOTHROW { return !(rhs == lhs); }
1704inline bool operator< (const QStringRef &lhs, QChar rhs) Q_DECL_NOTHROW { return rhs > lhs; }
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); }
1708
1709// QChar <> QLatin1String
1710inline bool operator==(QChar lhs, QLatin1String rhs) Q_DECL_NOTHROW
1711{ return rhs.size() == 1 && lhs == rhs.front(); }
1712inline bool operator< (QChar lhs, QLatin1String rhs) Q_DECL_NOTHROW
1713{ return QString::compare_helper(&lhs, 1, rhs) < 0; }
1714inline bool operator> (QChar lhs, QLatin1String rhs) Q_DECL_NOTHROW
1715{ return QString::compare_helper(&lhs, 1, rhs) > 0; }
1716
1717inline bool operator!=(QChar lhs, QLatin1String rhs) Q_DECL_NOTHROW { return !(lhs == rhs); }
1718inline bool operator<=(QChar lhs, QLatin1String rhs) Q_DECL_NOTHROW { return !(lhs > rhs); }
1719inline bool