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#if QT_DEPRECATED_SINCE(5, 14)
324 QT_DEPRECATED_X("Use vasprintf(), arg() or QTextStream instead")
325 QString &vsprintf(const char *format, va_list ap) Q_ATTRIBUTE_FORMAT_PRINTF(2, 0);
326 QT_DEPRECATED_X("Use asprintf(), arg() or QTextStream instead")
327 QString &sprintf(const char *format, ...) Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
328#endif
329 static QString vasprintf(const char *format, va_list ap) Q_ATTRIBUTE_FORMAT_PRINTF(1, 0);
330 static QString asprintf(const char *format, ...) Q_ATTRIBUTE_FORMAT_PRINTF(1, 2);
331
332 int indexOf(QChar c, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
333 int indexOf(const QString &s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
334 int indexOf(QLatin1String s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
335 int indexOf(const QStringRef &s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
336 int lastIndexOf(QChar c, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
337 int lastIndexOf(const QString &s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
338 int lastIndexOf(QLatin1String s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
339 int lastIndexOf(const QStringRef &s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
340
341 inline bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
342 inline bool contains(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
343 inline bool contains(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
344 inline bool contains(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
345 int count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
346 int count(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
347 int count(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
348
349#ifndef QT_NO_REGEXP
350 int indexOf(const QRegExp &, int from = 0) const;
351 int lastIndexOf(const QRegExp &, int from = -1) const;
352 inline bool contains(const QRegExp &rx) const { return indexOf(rx) != -1; }
353 int count(const QRegExp &) const;
354
355 int indexOf(QRegExp &, int from = 0) const;
356 int lastIndexOf(QRegExp &, int from = -1) const;
357 inline bool contains(QRegExp &rx) const { return indexOf(rx) != -1; }
358#endif
359
360#if QT_CONFIG(regularexpression)
361 int indexOf(const QRegularExpression &re, int from = 0) const;
362 int indexOf(const QRegularExpression &re, int from, QRegularExpressionMatch *rmatch) const; // ### Qt 6: merge overloads
363 int lastIndexOf(const QRegularExpression &re, int from = -1) const;
364 int lastIndexOf(const QRegularExpression &re, int from, QRegularExpressionMatch *rmatch) const; // ### Qt 6: merge overloads
365 bool contains(const QRegularExpression &re) const;
366 bool contains(const QRegularExpression &re, QRegularExpressionMatch *match) const; // ### Qt 6: merge overloads
367 int count(const QRegularExpression &re) const;
368#endif
369
370 enum SectionFlag {
371 SectionDefault = 0x00,
372 SectionSkipEmpty = 0x01,
373 SectionIncludeLeadingSep = 0x02,
374 SectionIncludeTrailingSep = 0x04,
375 SectionCaseInsensitiveSeps = 0x08
376 };
377 Q_DECLARE_FLAGS(SectionFlags, SectionFlag)
378
379 QString section(QChar sep, int start, int end = -1, SectionFlags flags = SectionDefault) const;
380 QString section(const QString &in_sep, int start, int end = -1, SectionFlags flags = SectionDefault) const;
381#ifndef QT_NO_REGEXP
382 QString section(const QRegExp &reg, int start, int end = -1, SectionFlags flags = SectionDefault) const;
383#endif
384#if QT_CONFIG(regularexpression)
385 QString section(const QRegularExpression &re, int start, int end = -1, SectionFlags flags = SectionDefault) const;
386#endif
387 Q_REQUIRED_RESULT QString left(int n) const;
388 Q_REQUIRED_RESULT QString right(int n) const;
389 Q_REQUIRED_RESULT QString mid(int position, int n = -1) const;
390 Q_REQUIRED_RESULT QString chopped(int n) const
391 { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return left(size() - n); }
392
393
394 Q_REQUIRED_RESULT QStringRef leftRef(int n) const;
395 Q_REQUIRED_RESULT QStringRef rightRef(int n) const;
396 Q_REQUIRED_RESULT QStringRef midRef(int position, int n = -1) const;
397
398#if QT_STRINGVIEW_LEVEL < 2
399 bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
400 bool startsWith(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
401#endif
402 Q_REQUIRED_RESULT bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW
403 { return QtPrivate::startsWith(*this, s, cs); }
404 bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
405 bool startsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
406
407#if QT_STRINGVIEW_LEVEL < 2
408 bool endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
409 bool endsWith(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
410#endif
411 Q_REQUIRED_RESULT bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW
412 { return QtPrivate::endsWith(*this, s, cs); }
413 bool endsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
414 bool endsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
415
416 bool isUpper() const;
417 bool isLower() const;
418
419 Q_REQUIRED_RESULT QString leftJustified(int width, QChar fill = QLatin1Char(' '), bool trunc = false) const;
420 Q_REQUIRED_RESULT QString rightJustified(int width, QChar fill = QLatin1Char(' '), bool trunc = false) const;
421
422#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QSTRING_COMPAT_CPP) && !defined(Q_CLANG_QDOC)
423# if defined(Q_CC_GNU) && !defined(Q_CC_CLANG) && !defined(Q_CC_INTEL) && !QT_HAS_CPP_ATTRIBUTE(nodiscard)
424 // required due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61941
425# pragma push_macro("Q_REQUIRED_RESULT")
426# undef Q_REQUIRED_RESULT
427# define Q_REQUIRED_RESULT
428# define Q_REQUIRED_RESULT_pushed
429# endif
430 Q_REQUIRED_RESULT QString toLower() const &
431 { return toLower_helper(*this); }
432 Q_REQUIRED_RESULT QString toLower() &&
433 { return toLower_helper(*this); }
434 Q_REQUIRED_RESULT QString toUpper() const &
435 { return toUpper_helper(*this); }
436 Q_REQUIRED_RESULT QString toUpper() &&
437 { return toUpper_helper(*this); }
438 Q_REQUIRED_RESULT QString toCaseFolded() const &
439 { return toCaseFolded_helper(*this); }
440 Q_REQUIRED_RESULT QString toCaseFolded() &&
441 { return toCaseFolded_helper(*this); }
442 Q_REQUIRED_RESULT QString trimmed() const &
443 { return trimmed_helper(*this); }
444 Q_REQUIRED_RESULT QString trimmed() &&
445 { return trimmed_helper(*this); }
446 Q_REQUIRED_RESULT QString simplified() const &
447 { return simplified_helper(*this); }
448 Q_REQUIRED_RESULT QString simplified() &&
449 { return simplified_helper(*this); }
450# ifdef Q_REQUIRED_RESULT_pushed
451# pragma pop_macro("Q_REQUIRED_RESULT")
452# endif
453#else
454 Q_REQUIRED_RESULT QString toLower() const;
455 Q_REQUIRED_RESULT QString toUpper() const;
456 Q_REQUIRED_RESULT QString toCaseFolded() const;
457 Q_REQUIRED_RESULT QString trimmed() const;
458 Q_REQUIRED_RESULT QString simplified() const;
459#endif
460 Q_REQUIRED_RESULT QString toHtmlEscaped() const;
461
462 QString &insert(int i, QChar c);
463 QString &insert(int i, const QChar *uc, int len);
464 inline QString &insert(int i, const QString &s) { return insert(i, s.constData(), s.length()); }
465 inline QString &insert(int i, const QStringRef &s);
466 QString &insert(int i, QLatin1String s);
467 QString &append(QChar c);
468 QString &append(const QChar *uc, int len);
469 QString &append(const QString &s);
470 QString &append(const QStringRef &s);
471 QString &append(QLatin1String s);
472 inline QString &prepend(QChar c) { return insert(0, c); }
473 inline QString &prepend(const QChar *uc, int len) { return insert(0, uc, len); }
474 inline QString &prepend(const QString &s) { return insert(0, s); }
475 inline QString &prepend(const QStringRef &s) { return insert(0, s); }
476 inline QString &prepend(QLatin1String s) { return insert(0, s); }
477
478 inline QString &operator+=(QChar c) {
479 if (d->ref.isShared() || uint(d->size) + 2u > d->alloc)
480 reallocData(uint(d->size) + 2u, true);
481 d->data()[d->size++] = c.unicode();
482 d->data()[d->size] = '\0';
483 return *this;
484 }
485
486 inline QString &operator+=(QChar::SpecialCharacter c) { return append(QChar(c)); }
487 inline QString &operator+=(const QString &s) { return append(s); }
488 inline QString &operator+=(const QStringRef &s) { return append(s); }
489 inline QString &operator+=(QLatin1String s) { return append(s); }
490
491 QString &remove(int i, int len);
492 QString &remove(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive);
493 QString &remove(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive);
494 QString &remove(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive);
495 QString &replace(int i, int len, QChar after);
496 QString &replace(int i, int len, const QChar *s, int slen);
497 QString &replace(int i, int len, const QString &after);
498 QString &replace(QChar before, QChar after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
499 QString &replace(const QChar *before, int blen, const QChar *after, int alen, Qt::CaseSensitivity cs = Qt::CaseSensitive);
500 QString &replace(QLatin1String before, QLatin1String after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
501 QString &replace(QLatin1String before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
502 QString &replace(const QString &before, QLatin1String after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
503 QString &replace(const QString &before, const QString &after,
504 Qt::CaseSensitivity cs = Qt::CaseSensitive);
505 QString &replace(QChar c, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
506 QString &replace(QChar c, QLatin1String after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
507#ifndef QT_NO_REGEXP
508 QString &replace(const QRegExp &rx, const QString &after);
509 inline QString &remove(const QRegExp &rx)
510 { return replace(rx, QString()); }
511#endif
512#if QT_CONFIG(regularexpression)
513 QString &replace(const QRegularExpression &re, const QString &after);
514 inline QString &remove(const QRegularExpression &re)
515 { return replace(re, QString()); }
516#endif
517
518 enum SplitBehavior { KeepEmptyParts, SkipEmptyParts };
519
520 Q_REQUIRED_RESULT QStringList split(const QString &sep, SplitBehavior behavior = KeepEmptyParts,
521 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
522 Q_REQUIRED_RESULT QVector<QStringRef> splitRef(const QString &sep, SplitBehavior behavior = KeepEmptyParts,
523 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
524 Q_REQUIRED_RESULT QStringList split(QChar sep, SplitBehavior behavior = KeepEmptyParts,
525 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
526 Q_REQUIRED_RESULT QVector<QStringRef> splitRef(QChar sep, SplitBehavior behavior = KeepEmptyParts,
527 Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
528#ifndef QT_NO_REGEXP
529 Q_REQUIRED_RESULT QStringList split(const QRegExp &sep, SplitBehavior behavior = KeepEmptyParts) const;
530 Q_REQUIRED_RESULT QVector<QStringRef> splitRef(const QRegExp &sep, SplitBehavior behavior = KeepEmptyParts) const;
531#endif
532#if QT_CONFIG(regularexpression)
533 Q_REQUIRED_RESULT QStringList split(const QRegularExpression &sep, SplitBehavior behavior = KeepEmptyParts) const;
534 Q_REQUIRED_RESULT QVector<QStringRef> splitRef(const QRegularExpression &sep, SplitBehavior behavior = KeepEmptyParts) const;
535#endif
536 enum NormalizationForm {
537 NormalizationForm_D,
538 NormalizationForm_C,
539 NormalizationForm_KD,
540 NormalizationForm_KC
541 };
542 Q_REQUIRED_RESULT QString normalized(NormalizationForm mode, QChar::UnicodeVersion version = QChar::Unicode_Unassigned) const;
543
544 Q_REQUIRED_RESULT QString repeated(int times) const;
545
546 const ushort *utf16() const;
547
548#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QSTRING_COMPAT_CPP) && !defined(Q_CLANG_QDOC)
549 Q_REQUIRED_RESULT QByteArray toLatin1() const &
550 { return toLatin1_helper(*this); }
551 Q_REQUIRED_RESULT QByteArray toLatin1() &&
552 { return toLatin1_helper_inplace(*this); }
553 Q_REQUIRED_RESULT QByteArray toUtf8() const &
554 { return toUtf8_helper(*this); }
555 Q_REQUIRED_RESULT QByteArray toUtf8() &&
556 { return toUtf8_helper(*this); }
557 Q_REQUIRED_RESULT QByteArray toLocal8Bit() const &
558 { return toLocal8Bit_helper(isNull() ? nullptr : constData(), size()); }
559 Q_REQUIRED_RESULT QByteArray toLocal8Bit() &&
560 { return toLocal8Bit_helper(isNull() ? nullptr : constData(), size()); }
561#else
562 Q_REQUIRED_RESULT QByteArray toLatin1() const;
563 Q_REQUIRED_RESULT QByteArray toUtf8() const;
564 Q_REQUIRED_RESULT QByteArray toLocal8Bit() const;
565#endif
566 Q_REQUIRED_RESULT QVector<uint> toUcs4() const;
567
568 // note - this are all inline so we can benefit from strlen() compile time optimizations
569 static inline QString fromLatin1(const char *str, int size = -1)
570 {
571 QStringDataPtr dataPtr = { fromLatin1_helper(str, (str && size == -1) ? int(strlen(str)) : size) };
572 return QString(dataPtr);
573 }
574 static inline QString fromUtf8(const char *str, int size = -1)
575 {
576 return fromUtf8_helper(str, (str && size == -1) ? int(strlen(str)) : size);
577 }
578 static inline QString fromLocal8Bit(const char *str, int size = -1)
579 {
580 return fromLocal8Bit_helper(str, (str && size == -1) ? int(strlen(str)) : size);
581 }
582 static inline QString fromLatin1(const QByteArray &str)
583 { return str.isNull() ? QString() : fromLatin1(str.data(), qstrnlen(str.constData(), str.size())); }
584 static inline QString fromUtf8(const QByteArray &str)
585 { return str.isNull() ? QString() : fromUtf8(str.data(), qstrnlen(str.constData(), str.size())); }
586 static inline QString fromLocal8Bit(const QByteArray &str)
587 { return str.isNull() ? QString() : fromLocal8Bit(str.data(), qstrnlen(str.constData(), str.size())); }
588 static QString fromUtf16(const ushort *, int size = -1);
589 static QString fromUcs4(const uint *, int size = -1);
590 static QString fromRawData(const QChar *, int size);
591
592#if defined(Q_COMPILER_UNICODE_STRINGS)
593 static QString fromUtf16(const char16_t *str, int size = -1)
594 { return fromUtf16(reinterpret_cast<const ushort *>(str), size); }
595 static QString fromUcs4(const char32_t *str, int size = -1)
596 { return fromUcs4(reinterpret_cast<const uint *>(str), size); }
597#endif
598
599#if QT_DEPRECATED_SINCE(5, 0)
600 QT_DEPRECATED static inline QString fromAscii(const char *str, int size = -1)
601 { return fromLatin1(str, size); }
602 QT_DEPRECATED static inline QString fromAscii(const QByteArray &str)
603 { return fromLatin1(str); }
604 Q_REQUIRED_RESULT QByteArray toAscii() const
605 { return toLatin1(); }
606#endif
607
608 inline int toWCharArray(wchar_t *array) const;
609 Q_REQUIRED_RESULT static inline QString fromWCharArray(const wchar_t *string, int size = -1);
610
611 QString &setRawData(const QChar *unicode, int size);
612 QString &setUnicode(const QChar *unicode, int size);
613 inline QString &setUtf16(const ushort *utf16, int size);
614
615#if QT_STRINGVIEW_LEVEL < 2
616 int compare(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW;
617 inline int compare(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW;
618#endif
619 int compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW;
620 inline int compare(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW;
621
622 static inline int compare(const QString &s1, const QString &s2,
623 Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW
624 { return s1.compare(s2, cs); }
625
626 static inline int compare(const QString &s1, QLatin1String s2,
627 Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW
628 { return s1.compare(s2, cs); }
629 static inline int compare(QLatin1String s1, const QString &s2,
630 Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW
631 { return -s2.compare(s1, cs); }
632
633 static int compare(const QString &s1, const QStringRef &s2,
634 Qt::CaseSensitivity = Qt::CaseSensitive) Q_DECL_NOTHROW;
635
636 int localeAwareCompare(const QString& s) const;
637 static int localeAwareCompare(const QString& s1, const QString& s2)
638 { return s1.localeAwareCompare(s2); }
639
640 int localeAwareCompare(const QStringRef &s) const;
641 static int localeAwareCompare(const QString& s1, const QStringRef& s2);
642
643 // ### Qt6: make inline except for the long long versions
644 short toShort(bool *ok=nullptr, int base=10) const;
645 ushort toUShort(bool *ok=nullptr, int base=10) const;
646 int toInt(bool *ok=nullptr, int base=10) const;
647 uint toUInt(bool *ok=nullptr, int base=10) const;
648 long toLong(bool *ok=nullptr, int base=10) const;
649 ulong toULong(bool *ok=nullptr, int base=10) const;
650 qlonglong toLongLong(bool *ok=nullptr, int base=10) const;
651 qulonglong toULongLong(bool *ok=nullptr, int base=10) const;
652 float toFloat(bool *ok=nullptr) const;
653 double toDouble(bool *ok=nullptr) const;
654
655 QString &setNum(short, int base=10);
656 QString &setNum(ushort, int base=10);
657 QString &setNum(int, int base=10);
658 QString &setNum(uint, int base=10);
659 QString &setNum(long, int base=10);
660 QString &setNum(ulong, int base=10);
661 QString &setNum(qlonglong, int base=10);
662 QString &setNum(qulonglong, int base=10);
663 QString &setNum(float, char f='g', int prec=6);
664 QString &setNum(double, char f='g', int prec=6);
665
666 static QString number(int, int base=10);
667 static QString number(uint, int base=10);
668 static QString number(long, int base=10);
669 static QString number(ulong, int base=10);
670 static QString number(qlonglong, int base=10);
671 static QString number(qulonglong, int base=10);
672 static QString number(double, char f='g', int prec=6);
673
674 friend Q_CORE_EXPORT bool operator==(const QString &s1, const QString &s2) Q_DECL_NOTHROW;
675 friend Q_CORE_EXPORT bool operator<(const QString &s1, const QString &s2) Q_DECL_NOTHROW;
676 friend inline bool operator>(const QString &s1, const QString &s2) Q_DECL_NOTHROW { return s2 < s1; }
677 friend inline bool operator!=(const QString &s1, const QString &s2) Q_DECL_NOTHROW { return !(s1 == s2); }
678 friend inline bool operator<=(const QString &s1, const QString &s2) Q_DECL_NOTHROW { return !(s1 > s2); }
679 friend inline bool operator>=(const QString &s1, const QString &s2) Q_DECL_NOTHROW { return !(s1 < s2); }
680
681 bool operator==(QLatin1String s) const Q_DECL_NOTHROW;
682 bool operator<(QLatin1String s) const Q_DECL_NOTHROW;
683 bool operator>(QLatin1String s) const Q_DECL_NOTHROW;
684 inline bool operator!=(QLatin1String s) const Q_DECL_NOTHROW { return !operator==(s); }
685 inline bool operator<=(QLatin1String s) const Q_DECL_NOTHROW { return !operator>(s); }
686 inline bool operator>=(QLatin1String s) const Q_DECL_NOTHROW { return !operator<(s); }
687
688 // ASCII compatibility
689#if defined(QT_RESTRICTED_CAST_FROM_ASCII)
690 template <int N>
691 inline QString(const char (&ch)[N])
692 : d(fromAscii_helper(ch, N - 1))
693 {}
694 template <int N>
695 QString(char (&)[N]) = delete;
696 template <int N>
697 inline QString &operator=(const char (&ch)[N])
698 { return (*this = fromUtf8(ch, N - 1)); }
699 template <int N>
700 QString &operator=(char (&)[N]) = delete;
701#endif
702#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
703 inline QT_ASCII_CAST_WARN QString(const char *ch)
704 : d(fromAscii_helper(ch, ch ? int(strlen(ch)) : -1))
705 {}
706 inline QT_ASCII_CAST_WARN QString(const QByteArray &a)
707 : d(fromAscii_helper(a.constData(), qstrnlen(a.constData(), a.size())))
708 {}
709 inline QT_ASCII_CAST_WARN QString &operator=(const char *ch)
710 { return (*this = fromUtf8(ch)); }
711 inline QT_ASCII_CAST_WARN QString &operator=(const QByteArray &a)
712 { return (*this = fromUtf8(a)); }
713 inline QT_ASCII_CAST_WARN QString &operator=(char c)
714 { return (*this = QChar::fromLatin1(c)); }
715
716 // these are needed, so it compiles with STL support enabled
717 inline QT_ASCII_CAST_WARN QString &prepend(const char *s)
718 { return prepend(QString::fromUtf8(s)); }
719 inline QT_ASCII_CAST_WARN QString &prepend(const QByteArray &s)
720 { return prepend(QString::fromUtf8(s)); }
721 inline QT_ASCII_CAST_WARN QString &append(const char *s)
722 { return append(QString::fromUtf8(s)); }
723 inline QT_ASCII_CAST_WARN QString &append(const QByteArray &s)
724 { return append(QString::fromUtf8(s)); }
725 inline QT_ASCII_CAST_WARN QString &insert(int i, const char *s)
726 { return insert(i, QString::fromUtf8(s)); }
727 inline QT_ASCII_CAST_WARN QString &insert(int i, const QByteArray &s)
728 { return insert(i, QString::fromUtf8(s)); }
729 inline QT_ASCII_CAST_WARN QString &operator+=(const char *s)
730 { return append(QString::fromUtf8(s)); }
731 inline QT_ASCII_CAST_WARN QString &operator+=(const QByteArray &s)
732 { return append(QString::fromUtf8(s)); }
733 inline QT_ASCII_CAST_WARN QString &operator+=(char c)
734 { return append(QChar::fromLatin1(c)); }
735
736 inline QT_ASCII_CAST_WARN bool operator==(const char *s) const;
737 inline QT_ASCII_CAST_WARN bool operator!=(const char *s) const;
738 inline QT_ASCII_CAST_WARN bool operator<(const char *s) const;
739 inline QT_ASCII_CAST_WARN bool operator<=(const char *s) const;
740 inline QT_ASCII_CAST_WARN bool operator>(const char *s) const;
741 inline QT_ASCII_CAST_WARN bool operator>=(const char *s) const;
742
743 inline QT_ASCII_CAST_WARN bool operator==(const QByteArray &s) const;
744 inline QT_ASCII_CAST_WARN bool operator!=(const QByteArray &s) const;
745 inline QT_ASCII_CAST_WARN bool operator<(const QByteArray &s) const;
746 inline QT_ASCII_CAST_WARN bool operator>(const QByteArray &s) const;
747 inline QT_ASCII_CAST_WARN bool operator<=(const QByteArray &s) const;
748 inline QT_ASCII_CAST_WARN bool operator>=(const QByteArray &s) const;
749
750 friend inline QT_ASCII_CAST_WARN bool operator==(const char *s1, const QString &s2);
751 friend inline QT_ASCII_CAST_WARN bool operator!=(const char *s1, const QString &s2);
752 friend inline QT_ASCII_CAST_WARN bool operator<(const char *s1, const QString &s2);
753 friend inline QT_ASCII_CAST_WARN bool operator>(const char *s1, const QString &s2);
754 friend inline QT_ASCII_CAST_WARN bool operator<=(const char *s1, const QString &s2);
755 friend inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, const QString &s2);
756
757 friend inline QT_ASCII_CAST_WARN bool operator==(const char *s1, const QStringRef &s2);
758 friend inline QT_ASCII_CAST_WARN bool operator!=(const char *s1, const QStringRef &s2);
759 friend inline QT_ASCII_CAST_WARN bool operator<(const char *s1, const QStringRef &s2);
760 friend inline QT_ASCII_CAST_WARN bool operator>(const char *s1, const QStringRef &s2);
761 friend inline QT_ASCII_CAST_WARN bool operator<=(const char *s1, const QStringRef &s2);
762 friend inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, const QStringRef &s2);
763#endif
764
765 typedef QChar *iterator;
766 typedef const QChar *const_iterator;
767 typedef iterator Iterator;
768 typedef const_iterator ConstIterator;
769 typedef std::reverse_iterator<iterator> reverse_iterator;
770 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
771 inline iterator begin();
772 inline const_iterator begin() const;
773 inline const_iterator cbegin() const;
774 inline const_iterator constBegin() const;
775 inline iterator end();
776 inline const_iterator end() const;
777 inline const_iterator cend() const;
778 inline const_iterator constEnd() const;
779 reverse_iterator rbegin() { return reverse_iterator(end()); }
780 reverse_iterator rend() { return reverse_iterator(begin()); }
781 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
782 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
783 const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); }
784 const_reverse_iterator crend() const { return const_reverse_iterator(begin()); }
785
786 // STL compatibility
787 typedef int size_type;
788 typedef qptrdiff difference_type;
789 typedef const QChar & const_reference;
790 typedef QChar & reference;
791 typedef QChar *pointer;
792 typedef const QChar *const_pointer;
793 typedef QChar value_type;
794 inline void push_back(QChar c) { append(c); }
795 inline void push_back(const QString &s) { append(s); }
796 inline void push_front(QChar c) { prepend(c); }
797 inline void push_front(const QString &s) { prepend(s); }
798 void shrink_to_fit() { squeeze(); }
799
800 static inline QString fromStdString(const std::string &s);
801 inline std::string toStdString() const;
802 static inline QString fromStdWString(const std::wstring &s);
803 inline std::wstring toStdWString() const;
804
805#if defined(Q_STDLIB_UNICODE_STRINGS) || defined(Q_QDOC)
806 static inline QString fromStdU16String(const std::u16string &s);
807 inline std::u16string toStdU16String() const;
808 static inline QString fromStdU32String(const std::u32string &s);
809 inline std::u32string toStdU32String() const;
810#endif
811
812#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
813 static QString fromCFString(CFStringRef string);
814 CFStringRef toCFString() const Q_DECL_CF_RETURNS_RETAINED;
815 static QString fromNSString(const NSString *string);
816 NSString *toNSString() const Q_DECL_NS_RETURNS_AUTORELEASED;
817#endif
818 // compatibility
819#if QT_DEPRECATED_SINCE(5, 9)
820 struct Null { };
821 QT_DEPRECATED_X("use QString()")
822 static const Null null;
823 inline QString(const Null &): d(Data::sharedNull()) {}
824 inline QString &operator=(const Null &) { *this = QString(); return *this; }
825#endif
826 inline bool isNull() const { return d == Data::sharedNull(); }
827
828
829 bool isSimpleText() const;
830 bool isRightToLeft() const;
831
832 QString(int size, Qt::Initialization);
833 Q_DECL_CONSTEXPR inline QString(QStringDataPtr dd) : d(dd.ptr) {}
834
835private:
836#if defined(QT_NO_CAST_FROM_ASCII)
837 QString &operator+=(const char *s);
838 QString &operator+=(const QByteArray &s);
839 QString(const char *ch);
840 QString(const QByteArray &a);
841 QString &operator=(const char *ch);
842 QString &operator=(const QByteArray &a);
843#endif
844
845 Data *d;
846
847 friend inline bool operator==(QChar, const QString &) Q_DECL_NOTHROW;
848 friend inline bool operator< (QChar, const QString &) Q_DECL_NOTHROW;
849 friend inline bool operator> (QChar, const QString &) Q_DECL_NOTHROW;
850 friend inline bool operator==(QChar, const QStringRef &) Q_DECL_NOTHROW;
851 friend inline bool operator< (QChar, const QStringRef &) Q_DECL_NOTHROW;
852 friend inline bool operator> (QChar, const QStringRef &) Q_DECL_NOTHROW;
853 friend inline bool operator==(QChar, QLatin1String) Q_DECL_NOTHROW;
854 friend inline bool operator< (QChar, QLatin1String) Q_DECL_NOTHROW;
855 friend inline bool operator> (QChar, QLatin1String) Q_DECL_NOTHROW;
856
857 void reallocData(uint alloc, bool grow = false);
858#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
859 void expand(int i);
860#endif
861 QString multiArg(int numArgs, const QString **args) const;
862 static int compare_helper(const QChar *data1, int length1,
863 const QChar *data2, int length2,
864 Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW;
865 static int compare_helper(const QChar *data1, int length1,
866 const char *data2, int length2,
867 Qt::CaseSensitivity cs = Qt::CaseSensitive);
868 static int compare_helper(const QChar *data1, int length1,
869 QLatin1String s2,
870 Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW;
871 static int localeAwareCompare_helper(const QChar *data1, int length1,
872 const QChar *data2, int length2);
873 static QString toLower_helper(const QString &str);
874 static QString toLower_helper(QString &str);
875 static QString toUpper_helper(const QString &str);
876 static QString toUpper_helper(QString &str);
877 static QString toCaseFolded_helper(const QString &str);
878 static QString toCaseFolded_helper(QString &str);
879 static QString trimmed_helper(const QString &str);
880 static QString trimmed_helper(QString &str);
881 static QString simplified_helper(const QString &str);
882 static QString simplified_helper(QString &str);
883 static Data *fromLatin1_helper(const char *str, int size = -1);
884 static Data *fromAscii_helper(const char *str, int size = -1);
885 static QString fromUtf8_helper(const char *str, int size);
886 static QString fromLocal8Bit_helper(const char *, int size);
887 static QByteArray toLatin1_helper(const QString &);
888 static QByteArray toLatin1_helper_inplace(QString &);
889 static QByteArray toUtf8_helper(const QString &);
890 static QByteArray toLocal8Bit_helper(const QChar *data, int size);
891 static int toUcs4_helper(const ushort *uc, int length, uint *out);
892 static qlonglong toIntegral_helper(const QChar *data, int len, bool *ok, int base);
893 static qulonglong toIntegral_helper(const QChar *data, uint len, bool *ok, int base);
894 void replace_helper(uint *indices, int nIndices, int blen, const QChar *after, int alen);
895 friend class QCharRef;
896 friend class QTextCodec;
897 friend class QStringRef;
898 friend class QStringView;
899 friend class QByteArray;
900 friend class QCollator;
901 friend struct QAbstractConcatenable;
902
903 template <typename T> static
904 T toIntegral_helper(const QChar *data, int len, bool *ok, int base)
905 {
906 using Int64 = typename std::conditional<std::is_unsigned<T>::value, qulonglong, qlonglong>::type;
907 using Int32 = typename std::conditional<std::is_unsigned<T>::value, uint, int>::type;
908
909 // we select the right overload by casting size() to int or uint
910 Int64 val = toIntegral_helper(data, Int32(len), ok, base);
911 if (T(val) != val) {
912 if (ok)
913 *ok = false;
914 val = 0;
915 }
916 return T(val);
917 }
918
919public:
920 typedef Data * DataPtr;
921 inline DataPtr &data_ptr() { return d; }
922};
923
924//
925// QStringView inline members that require QString:
926//
927QString QStringView::toString() const
928{ return Q_ASSERT(size() == length()), QString(data(), length()); }
929
930//
931// QString inline members
932//
933inline QString::QString(QLatin1String aLatin1) : d(fromLatin1_helper(aLatin1.latin1(), aLatin1.size()))
934{ }
935inline int QString::length() const
936{ return d->size; }
937inline const QChar QString::at(int i) const
938{ Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; }
939inline const QChar QString::operator[](int i) const
940{ Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; }
941inline const QChar QString::operator[](uint i) const
942{ Q_ASSERT(i < uint(size())); return d->data()[i]; }
943inline bool QString::isEmpty() const
944{ return d->size == 0; }
945inline const QChar *QString::unicode() const
946{ return reinterpret_cast<const QChar*>(d->data()); }
947inline const QChar *QString::data() const
948{ return reinterpret_cast<const QChar*>(d->data()); }
949inline QChar *QString::data()
950{ detach(); return reinterpret_cast<QChar*>(d->data()); }
951inline const QChar *QString::constData() const
952{ return reinterpret_cast<const QChar*>(d->data()); }
953inline void QString::detach()
954{ if (d->ref.isShared() || (d->offset != sizeof(QStringData))) reallocData(uint(d->size) + 1u); }
955inline bool QString::isDetached() const
956{ return !d->ref.isShared(); }
957inline void QString::clear()
958{ if (!isNull()) *this = QString(); }
959inline QString::QString(const QString &other) Q_DECL_NOTHROW : d(other.d)
960{ Q_ASSERT(&other != this); d->ref.ref(); }
961inline int QString::capacity() const
962{ return d->alloc ? d->alloc - 1 : 0; }
963inline QString &QString::setNum(short n, int base)
964{ return setNum(qlonglong(n), base); }
965inline QString &QString::setNum(ushort n, int base)
966{ return setNum(qulonglong(n), base); }
967inline QString &QString::setNum(int n, int base)
968{ return setNum(qlonglong(n), base); }
969inline QString &QString::setNum(uint n, int base)
970{ return setNum(qulonglong(n), base); }
971inline QString &QString::setNum(long n, int base)
972{ return setNum(qlonglong(n), base); }
973inline QString &QString::setNum(ulong n, int base)
974{ return setNum(qulonglong(n), base); }
975inline QString &QString::setNum(float n, char f, int prec)
976{ return setNum(double(n),f,prec); }
977inline QString QString::arg(int a, int fieldWidth, int base, QChar fillChar) const
978{ return arg(qlonglong(a), fieldWidth, base, fillChar); }
979inline QString QString::arg(uint a, int fieldWidth, int base, QChar fillChar) const
980{ return arg(qulonglong(a), fieldWidth, base, fillChar); }
981inline QString QString::arg(long a, int fieldWidth, int base, QChar fillChar) const
982{ return arg(qlonglong(a), fieldWidth, base, fillChar); }
983inline QString QString::arg(ulong a, int fieldWidth, int base, QChar fillChar) const
984{ return arg(qulonglong(a), fieldWidth, base, fillChar); }
985inline QString QString::arg(short a, int fieldWidth, int base, QChar fillChar) const
986{ return arg(qlonglong(a), fieldWidth, base, fillChar); }
987inline QString QString::arg(ushort a, int fieldWidth, int base, QChar fillChar) const
988{ return arg(qulonglong(a), fieldWidth, base, fillChar); }
989inline QString QString::arg(const QString &a1, const QString &a2) const
990{ const QString *args[2] = { &a1, &a2 }; return multiArg(2, args); }
991inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3) const
992{ const QString *args[3] = { &a1, &a2, &a3 }; return multiArg(3, args); }
993inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
994 const QString &a4) const
995{ const QString *args[4] = { &a1, &a2, &a3, &a4 }; return multiArg(4, args); }
996inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
997 const QString &a4, const QString &a5) const
998{ const QString *args[5] = { &a1, &a2, &a3, &a4, &a5 }; return multiArg(5, args); }
999inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
1000 const QString &a4, const QString &a5, const QString &a6) const
1001{ const QString *args[6] = { &a1, &a2, &a3, &a4, &a5, &a6 }; return multiArg(6, 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
1005{ const QString *args[7] = { &a1, &a2, &a3, &a4, &a5, &a6, &a7 }; return multiArg(7, 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
1009{ const QString *args[8] = { &a1, &a2, &a3, &a4, &a5, &a6, &a7, &a8 }; return multiArg(8, args); }
1010inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
1011 const QString &a4, const QString &a5, const QString &a6,
1012 const QString &a7, const QString &a8, const QString &a9) const
1013{ const QString *args[9] = { &a1, &a2, &a3, &a4, &a5, &a6, &a7, &a8, &a9 }; return multiArg(9, args); }
1014
1015inline QString QString::section(QChar asep, int astart, int aend, SectionFlags aflags) const
1016{ return section(QString(asep), astart, aend, aflags); }
1017
1018QT_WARNING_PUSH
1019QT_WARNING_DISABLE_MSVC(4127) // "conditional expression is constant"
1020QT_WARNING_DISABLE_INTEL(111) // "statement is unreachable"
1021
1022inline int QString::toWCharArray(wchar_t *array) const
1023{
1024 return QStringView(*this).toWCharArray(array);
1025}
1026
1027QT_WARNING_POP
1028
1029inline QString QString::fromWCharArray(const wchar_t *string, int size)
1030{
1031 return sizeof(wchar_t) == sizeof(QChar) ? fromUtf16(reinterpret_cast<const ushort *>(string), size)
1032 : fromUcs4(reinterpret_cast<const uint *>(string), size);
1033}
1034
1035
1036class Q_CORE_EXPORT QCharRef {
1037 QString &s;
1038 int i;
1039 inline QCharRef(QString &str, int idx)
1040 : s(str),i(idx) {}
1041 friend class QString;
1042public:
1043
1044 // most QChar operations repeated here
1045
1046 // all this is not documented: We just say "like QChar" and let it be.
1047 inline operator QChar() const
1048 { return i < s.d->size ? s.d->data()[i] : 0; }
1049 inline QCharRef &operator=(QChar c)
1050 { if (i >= s.d->size) s.resize(i + 1, QLatin1Char(' ')); else s.detach();
1051 s.d->data()[i] = c.unicode(); return *this; }
1052
1053 // An operator= for each QChar cast constructors
1054#ifndef QT_NO_CAST_FROM_ASCII
1055 inline QT_ASCII_CAST_WARN QCharRef &operator=(char c)
1056 { return operator=(QChar::fromLatin1(c)); }
1057 inline QT_ASCII_CAST_WARN QCharRef &operator=(uchar c)
1058 { return operator=(QChar::fromLatin1(c)); }
1059#endif
1060 inline QCharRef &operator=(const QCharRef &c) { return operator=(QChar(c)); }
1061 inline QCharRef &operator=(ushort rc) { return operator=(QChar(rc)); }
1062 inline QCharRef &operator=(short rc) { return operator=(QChar(rc)); }
1063 inline QCharRef &operator=(uint rc) { return operator=(QChar(rc)); }
1064 inline QCharRef &operator=(int rc) { return operator=(QChar(rc)); }
1065
1066 // each function...
1067 inline bool isNull() const { return QChar(*this).isNull(); }
1068 inline bool isPrint() const { return QChar(*this).isPrint(); }
1069 inline bool isPunct() const { return QChar(*this).isPunct(); }
1070 inline bool isSpace() const { return QChar(*this).isSpace(); }
1071 inline bool isMark() const { return QChar(*this).isMark(); }
1072 inline bool isLetter() const { return QChar(*this).isLetter(); }
1073 inline bool isNumber() const { return QChar(*this).isNumber(); }
1074 inline bool isLetterOrNumber() { return QChar(*this).isLetterOrNumber(); }
1075 inline bool isDigit() const { return QChar(*this).isDigit(); }
1076 inline bool isLower() const { return QChar(*this).isLower(); }
1077 inline bool isUpper() const { return QChar(*this).isUpper(); }
1078 inline bool isTitleCase() const { return QChar(*this).isTitleCase(); }
1079
1080 inline int digitValue() const { return QChar(*this).digitValue(); }
1081 QChar toLower() const { return QChar(*this).toLower(); }
1082 QChar toUpper() const { return QChar(*this).toUpper(); }
1083 QChar toTitleCase () const { return QChar(*this).toTitleCase(); }
1084
1085 QChar::Category category() const { return QChar(*this).category(); }
1086 QChar::Direction direction() const { return QChar(*this).direction(); }
1087 QChar::JoiningType joiningType() const { return QChar(*this).joiningType(); }
1088#if QT_DEPRECATED_SINCE(5, 3)
1089 QT_DEPRECATED QChar::Joining joining() const
1090 {
1091 switch (QChar(*this).joiningType()) {
1092 case QChar::Joining_Causing: return QChar::Center;
1093 case QChar::Joining_Dual: return QChar::Dual;
1094 case QChar::Joining_Right: return QChar::Right;
1095 case QChar::Joining_None:
1096 case QChar::Joining_Left:
1097 case QChar::Joining_Transparent:
1098 default: return QChar::OtherJoining;
1099 }
1100 }
1101#endif
1102 bool hasMirrored() const { return QChar(*this).hasMirrored(); }
1103 QChar mirroredChar() const { return QChar(*this).mirroredChar(); }
1104 QString decomposition() const { return QChar(*this).decomposition(); }
1105 QChar::Decomposition decompositionTag() const { return QChar(*this).decompositionTag(); }
1106 uchar combiningClass() const { return QChar(*this).combiningClass(); }
1107
1108 inline QChar::Script script() const { return QChar(*this).script(); }
1109
1110 QChar::UnicodeVersion unicodeVersion() const { return QChar(*this).unicodeVersion(); }
1111
1112 inline uchar cell() const { return QChar(*this).cell(); }
1113 inline uchar row() const { return QChar(*this).row(); }
1114 inline void setCell(uchar cell);
1115 inline void setRow(uchar row);
1116
1117#if QT_DEPRECATED_SINCE(5, 0)
1118 QT_DEPRECATED char toAscii() const { return QChar(*this).toLatin1(); }
1119#endif
1120 char toLatin1() const { return QChar(*this).toLatin1(); }
1121 ushort unicode() const { return QChar(*this).unicode(); }
1122 ushort& unicode() { return s.data()[i].unicode(); }
1123
1124};
1125Q_DECLARE_TYPEINFO(QCharRef, Q_MOVABLE_TYPE);
1126
1127inline void QCharRef::setRow(uchar arow) { QChar(*this).setRow(arow); }
1128inline void QCharRef::setCell(uchar acell) { QChar(*this).setCell(acell); }
1129
1130
1131inline QString::QString() Q_DECL_NOTHROW : d(Data::sharedNull()) {}
1132inline QString::~QString() { if (!d->ref.deref()) Data::deallocate(d); }
1133
1134inline void QString::reserve(int asize)
1135{
1136 if (d->ref.isShared() || uint(asize) >= d->alloc)
1137 reallocData(qMax(asize, d->size) + 1u);
1138
1139 if (!d->capacityReserved) {
1140 // cannot set unconditionally, since d could be the shared_null/shared_empty (which is const)
1141 d->capacityReserved = true;
1142 }
1143}
1144
1145inline void QString::squeeze()
1146{
1147 if (d->ref.isShared() || uint(d->size) + 1u < d->alloc)
1148 reallocData(uint(d->size) + 1u);
1149
1150 if (d->capacityReserved) {
1151 // cannot set unconditionally, since d could be shared_null or
1152 // otherwise static.
1153 d->capacityReserved = false;
1154 }
1155}
1156
1157inline QString &QString::setUtf16(const ushort *autf16, int asize)
1158{ return setUnicode(reinterpret_cast<const QChar *>(autf16), asize); }
1159inline QCharRef QString::operator[](int i)
1160{ Q_ASSERT(i >= 0); return QCharRef(*this, i); }
1161inline QCharRef QString::operator[](uint i)
1162{ return QCharRef(*this, i); }
1163inline QCharRef QString::front() { return operator[](0); }
1164inline QCharRef QString::back() { return operator[](size() - 1); }
1165inline QString::iterator QString::begin()
1166{ detach(); return reinterpret_cast<QChar*>(d->data()); }
1167inline QString::const_iterator QString::begin() const
1168{ return reinterpret_cast<const QChar*>(d->data()); }
1169inline QString::const_iterator QString::cbegin() const
1170{ return reinterpret_cast<const QChar*>(d->data()); }
1171inline QString::const_iterator QString::constBegin() const
1172{ return reinterpret_cast<const QChar*>(d->data()); }
1173inline QString::iterator QString::end()
1174{ detach(); return reinterpret_cast<QChar*>(d->data() + d->size); }
1175inline QString::const_iterator QString::end() const
1176{ return reinterpret_cast<const QChar*>(d->data() + d->size); }
1177inline QString::const_iterator QString::cend() const
1178{ return reinterpret_cast<const QChar*>(d->data() + d->size); }
1179inline QString::const_iterator QString::constEnd() const
1180{ return reinterpret_cast<const QChar*>(d->data() + d->size); }
1181inline bool QString::contains(const QString &s, Qt::CaseSensitivity cs) const
1182{ return indexOf(s, 0, cs) != -1; }
1183inline bool QString::contains(const QStringRef &s, Qt::CaseSensitivity cs) const
1184{ return indexOf(s, 0, cs) != -1; }
1185inline bool QString::contains(QLatin1String s, Qt::CaseSensitivity cs) const
1186{ return indexOf(s, 0, cs) != -1; }
1187inline bool QString::contains(QChar c, Qt::CaseSensitivity cs) const
1188{ return indexOf(c, 0, cs) != -1; }
1189
1190#if QT_DEPRECATED_SINCE(5, 9)
1191inline bool operator==(QString::Null, QString::Null) { return true; }
1192QT_DEPRECATED_X("use QString::isNull()")
1193inline bool operator==(QString::Null, const QString &s) { return s.isNull(); }
1194QT_DEPRECATED_X("use QString::isNull()")
1195inline bool operator==(const QString &s, QString::Null) { return s.isNull(); }
1196inline bool operator!=(QString::Null, QString::Null) { return false; }
1197QT_DEPRECATED_X("use !QString::isNull()")
1198inline bool operator!=(QString::Null, const QString &s) { return !s.isNull(); }
1199QT_DEPRECATED_X("use !QString::isNull()")
1200inline bool operator!=(const QString &s, QString::Null) { return !s.isNull(); }
1201#endif
1202
1203inline bool operator==(QLatin1String s1, QLatin1String s2) Q_DECL_NOTHROW
1204{ return s1.size() == s2.size() && (!s1.size() || !memcmp(s1.latin1(), s2.latin1(), s1.size())); }
1205inline bool operator!=(QLatin1String s1, QLatin1String s2) Q_DECL_NOTHROW
1206{ return !operator==(s1, s2); }
1207inline bool operator<(QLatin1String s1, QLatin1String s2) Q_DECL_NOTHROW
1208{
1209 const int len = qMin(s1.size(), s2.size());
1210 const int r = len ? memcmp(s1.latin1(), s2.latin1(), len) : 0;
1211 return r < 0 || (r == 0 && s1.size() < s2.size());
1212}
1213inline bool operator>(QLatin1String s1, QLatin1String s2) Q_DECL_NOTHROW
1214{ return operator<(s2, s1); }
1215inline bool operator<=(QLatin1String s1, QLatin1String s2) Q_DECL_NOTHROW
1216{ return !operator>(s1, s2); }
1217inline bool operator>=(QLatin1String s1, QLatin1String s2) Q_DECL_NOTHROW
1218{ return !operator<(s1, s2); }
1219
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; }
1230inline bool QLatin1String::operator<=(const QString &s) const Q_DECL_NOTHROW
1231{ return s >= *this; }
1232
1233#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
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; }
1244inline bool QString::operator>=(const char *s) const
1245{ return QString::compare_helper(constData(), size(), s, -1) >= 0; }
1246
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; }
1257inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, const QString &s2)
1258{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) <= 0; }
1259
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); }
1270inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, QLatin1String s2)
1271{ return (QString::fromUtf8(s1) >= s2); }
1272
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; }
1283inline QT_ASCII_CAST_WARN bool QLatin1String::operator>=(const char *s) const
1284{ return QString::fromUtf8(s) <= *this; }
1285
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; }
1296inline QT_ASCII_CAST_WARN bool QLatin1String::operator>=(const QByteArray &s) const
1297{ return QString::fromUtf8(s) <= *this; }
1298
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(), qstrnlen(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; }
1309inline QT_ASCII_CAST_WARN bool QString::operator>=(const QByteArray &s) const
1310{ return QString::compare_helper(constData(), size(), s.constData(), s.size()) >= 0; }
1311
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(), qstrnlen(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; }
1322inline bool QByteArray::operator>=(const QString &s) const
1323{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) <= 0; }
1324#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
1325
1326#ifndef QT_NO_CAST_TO_ASCII
1327inline QByteArray &QByteArray::append(const QString &s)
1328{ return append(s.toUtf8()); }
1329inline QByteArray &QByteArray::insert(int i, const QString &s)
1330{ return insert(i, s.toUtf8()); }
1331inline QByteArray &QByteArray::replace(char c, const QString &after)
1332{ return replace(c, after.toUtf8()); }
1333inline QByteArray &QByteArray::replace(const QString &before, const char *after)
1334{ return replace(before.toUtf8(), after); }
1335inline QByteArray &QByteArray::replace(const QString &before, const QByteArray &after)
1336{ return replace(before.toUtf8(), after); }
1337inline QByteArray &QByteArray::operator+=(const QString &s)
1338{ return operator+=(s.toUtf8()); }
1339inline int QByteArray::indexOf(const QString &s, int from) const
1340{ return indexOf(s.toUtf8(), from); }
1341inline int QByteArray::lastIndexOf(const QString &s, int from) const
1342{ return lastIndexOf(s.toUtf8(), from); }
1343#endif // QT_NO_CAST_TO_ASCII
1344
1345#if !defined(QT_USE_FAST_OPERATOR_PLUS) && !defined(QT_USE_QSTRINGBUILDER)
1346inline const QString operator+(const QString &s1, const QString &s2)
1347{ QString t(s1); t += s2; return t; }
1348inline const QString operator+(const QString &s1, QChar s2)
1349{ QString t(s1); t += s2; return t; }
1350inline const QString operator+(QChar s1, const QString &s2)
1351{ QString t(s1); t += s2; return t; }
1352# if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
1353inline QT_ASCII_CAST_WARN const QString operator+(const QString &s1, const char *s2)
1354{ QString t(s1); t += QString::fromUtf8(s2); return t; }
1355inline QT_ASCII_CAST_WARN const QString operator+(const char *s1, const QString &s2)
1356{ QString t = QString::fromUtf8(s1); t += s2; return t; }
1357inline QT_ASCII_CAST_WARN const QString operator+(char c, const QString &s)
1358{ QString t = s; t.prepend(QChar::fromLatin1(c)); return t; }
1359inline QT_ASCII_CAST_WARN const QString operator+(const QString &s, char c)
1360{ QString t = s; t += QChar::fromLatin1(c); return t; }
1361inline QT_ASCII_CAST_WARN const QString operator+(const QByteArray &ba, const QString &s)
1362{ QString t = QString::fromUtf8(ba); t += s; return t; }
1363inline QT_ASCII_CAST_WARN const QString operator+(const QString &s, const QByteArray &ba)
1364{ QString t(s); t += QString::fromUtf8(ba); return t; }
1365# endif // QT_NO_CAST_FROM_ASCII
1366#endif // QT_USE_QSTRINGBUILDER
1367
1368inline std::string QString::toStdString() const
1369{ return toUtf8().toStdString(); }
1370
1371inline QString QString::fromStdString(const std::string &s)
1372{ return fromUtf8(s.data(), int(s.size())); }
1373
1374inline std::wstring QString::toStdWString() const
1375{
1376 std::wstring str;
1377 str.resize(length());
1378#if __cplusplus >= 201703L
1379 str.resize(toWCharArray(str.data()));
1380#else
1381 if (length())
1382 str.resize(toWCharArray(&str.front()));
1383#endif
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
1639#if QT_STRINGVIEW_LEVEL < 2
1640inline int QString::compare(const QStringRef &s, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW
1641{ return QString::compare_helper(constData(), length(), s.constData(), s.length(), cs); }
1642#endif
1643inline int QString::compare(QStringView s, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW
1644{ return -s.compare(*this, cs); }
1645inline int QString::compare(const QString &s1, const QStringRef &s2, Qt::CaseSensitivity cs) Q_DECL_NOTHROW
1646{ return QString::compare_helper(s1.constData(), s1.length(), s2.constData(), s2.length(), cs); }
1647inline int QStringRef::compare(const QString &s, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW
1648{ return QString::compare_helper(constData(), length(), s.constData(), s.length(), cs); }
1649inline int QStringRef::compare(const QStringRef &s, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW
1650{ return QString::compare_helper(constData(), length(), s.constData(), s.length(), cs); }
1651inline int QStringRef::compare(QLatin1String s, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW
1652{ return QString::compare_helper(constData(), length(), s, cs); }
1653inline int QStringRef::compare(const QStringRef &s1, const QString &s2, Qt::CaseSensitivity cs) Q_DECL_NOTHROW
1654{ return QString::compare_helper(s1.constData(), s1.length(), s2.constData(), s2.length(), cs); }
1655inline int QStringRef::compare(const QStringRef &s1, const QStringRef &s2, Qt::CaseSensitivity cs) Q_DECL_NOTHROW
1656{ return QString::compare_helper(s1.constData(), s1.length(), s2.constData(), s2.length(), cs); }
1657inline int QStringRef::compare(const QStringRef &s1, QLatin1String s2, Qt::CaseSensitivity cs) Q_DECL_NOTHROW
1658{ return QString::compare_helper(s1.constData(), s1.length(), s2, cs); }
1659
1660// QLatin1String <> QStringRef
1661Q_CORE_EXPORT bool operator==(QLatin1String lhs, const QStringRef &rhs) Q_DECL_NOTHROW;
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; }
1666inline bool operator>=(QLatin1String lhs, const QStringRef &rhs) Q_DECL_NOTHROW { return rhs.compare(lhs) <= 0; }
1667
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; }
1673inline bool operator>=(const QStringRef &lhs, QLatin1String rhs) Q_DECL_NOTHROW { return rhs <= lhs; }
1674
1675// QChar <> QString
1676inline bool operator==(QChar lhs, const QString &rhs) Q_DECL_NOTHROW
1677{ return rhs.size() == 1 && lhs == rhs.front(); }
1678inline bool operator< (QChar lhs, const QString &rhs) Q_DECL_NOTHROW
1679{ return QString::compare_helper(&lhs, 1, rhs.data(), rhs.size()) < 0; }
1680inline bool operator> (QChar lhs, const QString &rhs) Q_DECL_NOTHROW
1681{ return QString::compare_helper(&lhs, 1, rhs.data(), rhs.size()) > 0; }
1682
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); }
1685inline bool operator>=(QChar lhs, const QString &rhs) Q_DECL_NOTHROW { return !(lhs < rhs); }
1686
1687inline bool operator==(const QString &lhs, QChar rhs) Q_DECL_NOTHROW { return rhs == lhs; }
1688inline bool operator!=(const QString &lhs, QChar