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