1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
45 #include <QtCore/qchar.h>
46 #include <QtCore/qbytearray.h>
47 #include <QtCore/qrefcount.h>
48 #include <QtCore/qnamespace.h>
57 #error qstring.h must be included before any header file that defines truncate
71 template <typename T> class QVector;
74 QtPrivate::RefCount ref;
77 uint capacityReserved : 1;
79 qptrdiff offset; // will always work as we add/subtract from a ushort ptr
80 ushort d[sizeof(qptrdiff)/sizeof(ushort)];
82 inline ushort *data() { return d + sizeof(qptrdiff)/sizeof(ushort) + offset; }
83 inline const ushort *data() const { return d + sizeof(qptrdiff)/sizeof(ushort) + offset; }
86 template<int N> struct QConstStringData;
87 template<int N> struct QConstStringDataPtr
89 const QConstStringData<N> *ptr;
92 #if defined(Q_COMPILER_UNICODE_STRINGS)
93 template<int N> struct QConstStringData
95 const QStringData str;
96 const char16_t data[N + 1];
99 #define QT_UNICODE_LITERAL_II(str) u"" str
101 #elif defined(Q_OS_WIN) || (defined(__SIZEOF_WCHAR_T__) && __SIZEOF_WCHAR_T__ == 2) || defined(WCHAR_MAX) && (WCHAR_MAX - 0 < 65536)
102 // wchar_t is 2 bytes
103 template<int N> struct QConstStringData
105 const QStringData str;
106 const wchar_t data[N + 1];
109 #if defined(Q_CC_MSVC)
110 # define QT_UNICODE_LITERAL_II(str) L##str
112 # define QT_UNICODE_LITERAL_II(str) L"" str
116 template<int N> struct QConstStringData
118 const QStringData str;
119 const ushort data[N + 1];
123 #if defined(QT_UNICODE_LITERAL_II)
124 # define QT_UNICODE_LITERAL(str) QT_UNICODE_LITERAL_II(str)
125 # if defined(Q_COMPILER_LAMBDA)
126 # define QStringLiteral(str) ([]() -> QConstStringDataPtr<sizeof(QT_UNICODE_LITERAL(str))/2 - 1> { \
127 enum { Size = sizeof(QT_UNICODE_LITERAL(str))/2 - 1 }; \
128 static const QConstStringData<Size> qstring_literal = \
129 { { Q_REFCOUNT_INITIALIZER(-1), Size, 0, 0, { 0 } }, QT_UNICODE_LITERAL(str) }; \
130 QConstStringDataPtr<Size> holder = { &qstring_literal }; \
133 # elif defined(Q_CC_GNU)
134 // We need to create a QStringData in the .rodata section of memory
135 // and the only way to do that is to create a "static const" variable.
136 // To do that, we need the __extension__ {( )} trick which only GCC supports
138 # define QStringLiteral(str) \
140 enum { Size = sizeof(QT_UNICODE_LITERAL(str))/2 - 1 }; \
141 static const QConstStringData<Size> qstring_literal = \
142 { { Q_REFCOUNT_INITIALIZER(-1), Size, 0, 0, { 0 } }, QT_UNICODE_LITERAL(str) }; \
143 QConstStringDataPtr<Size> holder = { &qstring_literal }; \
148 #ifndef QStringLiteral
149 // no lambdas, not GCC, or GCC in C++98 mode with 4-byte wchar_t
150 // fallback, uses QLatin1String as next best options
152 # define QStringLiteral(str) QLatin1String(str)
155 class Q_CORE_EXPORT QString
158 typedef QStringData Data;
161 QString(const QChar *unicode, int size); // Qt5: don't cap size < 0
162 explicit QString(const QChar *unicode); // Qt5: merge with the above
164 QString(int size, QChar c);
165 inline QString(const QLatin1String &latin1);
166 inline QString(const QString &);
168 QString &operator=(QChar c);
169 QString &operator=(const QString &);
170 inline QString &operator=(const QLatin1String &);
171 #ifdef Q_COMPILER_RVALUE_REFS
172 inline QString &operator=(QString &&other)
173 { qSwap(d, other.d); return *this; }
175 inline void swap(QString &other) { qSwap(d, other.d); }
176 inline int size() const { return d->size; }
177 inline int count() const { return d->size; }
178 inline int length() const;
179 inline bool isEmpty() const;
180 void resize(int size);
182 QString &fill(QChar c, int size = -1);
183 void truncate(int pos);
186 int capacity() const;
187 inline void reserve(int size);
188 inline void squeeze();
190 inline const QChar *unicode() const;
191 inline QChar *data();
192 inline const QChar *data() const;
193 inline const QChar *constData() const;
195 inline void detach();
196 inline bool isDetached() const;
197 inline bool isSharedWith(const QString &other) const { return d == other.d; }
200 inline const QChar at(int i) const;
201 const QChar operator[](int i) const;
202 QCharRef operator[](int i);
203 const QChar operator[](uint i) const;
204 QCharRef operator[](uint i);
206 QString arg(qlonglong a, int fieldwidth=0, int base=10,
207 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
208 QString arg(qulonglong a, int fieldwidth=0, int base=10,
209 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
210 QString arg(long a, int fieldwidth=0, int base=10,
211 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
212 QString arg(ulong a, int fieldwidth=0, int base=10,
213 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
214 QString arg(int a, int fieldWidth = 0, int base = 10,
215 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
216 QString arg(uint a, int fieldWidth = 0, int base = 10,
217 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
218 QString arg(short a, int fieldWidth = 0, int base = 10,
219 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
220 QString arg(ushort a, int fieldWidth = 0, int base = 10,
221 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
222 QString arg(double a, int fieldWidth = 0, char fmt = 'g', int prec = -1,
223 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
224 QString arg(char a, int fieldWidth = 0,
225 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
226 QString arg(QChar a, int fieldWidth = 0,
227 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
228 QString arg(const QString &a, int fieldWidth = 0,
229 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
230 QString arg(const QString &a1, const QString &a2) const Q_REQUIRED_RESULT;
231 QString arg(const QString &a1, const QString &a2, const QString &a3) const Q_REQUIRED_RESULT;
232 QString arg(const QString &a1, const QString &a2, const QString &a3,
233 const QString &a4) const Q_REQUIRED_RESULT;
234 QString arg(const QString &a1, const QString &a2, const QString &a3,
235 const QString &a4, const QString &a5) const Q_REQUIRED_RESULT;
236 QString arg(const QString &a1, const QString &a2, const QString &a3,
237 const QString &a4, const QString &a5, const QString &a6) const Q_REQUIRED_RESULT;
238 QString arg(const QString &a1, const QString &a2, const QString &a3,
239 const QString &a4, const QString &a5, const QString &a6,
240 const QString &a7) const Q_REQUIRED_RESULT;
241 QString arg(const QString &a1, const QString &a2, const QString &a3,
242 const QString &a4, const QString &a5, const QString &a6,
243 const QString &a7, const QString &a8) const Q_REQUIRED_RESULT;
244 QString arg(const QString &a1, const QString &a2, const QString &a3,
245 const QString &a4, const QString &a5, const QString &a6,
246 const QString &a7, const QString &a8, const QString &a9) const Q_REQUIRED_RESULT;
248 QString &vsprintf(const char *format, va_list ap)
249 #if defined(Q_CC_GNU) && !defined(__INSURE__)
250 __attribute__ ((format (printf, 2, 0)))
253 QString &sprintf(const char *format, ...)
254 #if defined(Q_CC_GNU) && !defined(__INSURE__)
255 __attribute__ ((format (printf, 2, 3)))
259 int indexOf(QChar c, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
260 int indexOf(const QString &s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
261 int indexOf(const QLatin1String &s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
262 int indexOf(const QStringRef &s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
263 int lastIndexOf(QChar c, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
264 int lastIndexOf(const QString &s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
265 int lastIndexOf(const QLatin1String &s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
266 int lastIndexOf(const QStringRef &s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
268 inline bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
269 inline bool contains(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
270 inline bool contains(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
271 int count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
272 int count(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
273 int count(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
276 int indexOf(const QRegExp &, int from = 0) const;
277 int lastIndexOf(const QRegExp &, int from = -1) const;
278 inline bool contains(const QRegExp &rx) const { return indexOf(rx) != -1; }
279 int count(const QRegExp &) const;
281 int indexOf(QRegExp &, int from = 0) const;
282 int lastIndexOf(QRegExp &, int from = -1) const;
283 inline bool contains(QRegExp &rx) const { return indexOf(rx) != -1; }
287 SectionDefault = 0x00,
288 SectionSkipEmpty = 0x01,
289 SectionIncludeLeadingSep = 0x02,
290 SectionIncludeTrailingSep = 0x04,
291 SectionCaseInsensitiveSeps = 0x08
293 Q_DECLARE_FLAGS(SectionFlags, SectionFlag)
295 QString section(QChar sep, int start, int end = -1, SectionFlags flags = SectionDefault) const;
296 QString section(const QString &in_sep, int start, int end = -1, SectionFlags flags = SectionDefault) const;
298 QString section(const QRegExp ®, int start, int end = -1, SectionFlags flags = SectionDefault) const;
301 QString left(int n) const Q_REQUIRED_RESULT;
302 QString right(int n) const Q_REQUIRED_RESULT;
303 QString mid(int position, int n = -1) const Q_REQUIRED_RESULT;
304 QStringRef leftRef(int n) const Q_REQUIRED_RESULT;
305 QStringRef rightRef(int n) const Q_REQUIRED_RESULT;
306 QStringRef midRef(int position, int n = -1) const Q_REQUIRED_RESULT;
308 bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
309 bool startsWith(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
310 bool startsWith(const QLatin1String &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
311 bool startsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
312 bool endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
313 bool endsWith(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
314 bool endsWith(const QLatin1String &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
315 bool endsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
317 QString leftJustified(int width, QChar fill = QLatin1Char(' '), bool trunc = false) const Q_REQUIRED_RESULT;
318 QString rightJustified(int width, QChar fill = QLatin1Char(' '), bool trunc = false) const Q_REQUIRED_RESULT;
320 QString toLower() const Q_REQUIRED_RESULT;
321 QString toUpper() const Q_REQUIRED_RESULT;
322 QString toCaseFolded() const Q_REQUIRED_RESULT;
324 QString trimmed() const Q_REQUIRED_RESULT;
325 QString simplified() const Q_REQUIRED_RESULT;
326 QString toHtmlEscaped() const Q_REQUIRED_RESULT;
328 QString &insert(int i, QChar c);
329 QString &insert(int i, const QChar *uc, int len);
330 inline QString &insert(int i, const QString &s) { return insert(i, s.constData(), s.length()); }
331 QString &insert(int i, const QLatin1String &s);
332 QString &append(QChar c);
333 QString &append(const QString &s);
334 QString &append(const QStringRef &s);
335 QString &append(const QLatin1String &s);
336 inline QString &prepend(QChar c) { return insert(0, c); }
337 inline QString &prepend(const QString &s) { return insert(0, s); }
338 inline QString &prepend(const QLatin1String &s) { return insert(0, s); }
340 inline QString &operator+=(QChar c) {
341 if (d->ref != 1 || d->size + 1 > int(d->alloc))
342 realloc(grow(d->size + 1));
343 d->data()[d->size++] = c.unicode();
344 d->data()[d->size] = '\0';
348 inline QString &operator+=(QChar::SpecialCharacter c) { return append(QChar(c)); }
349 inline QString &operator+=(const QString &s) { return append(s); }
350 inline QString &operator+=(const QStringRef &s) { return append(s); }
351 inline QString &operator+=(const QLatin1String &s) { return append(s); }
353 QString &remove(int i, int len);
354 QString &remove(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive);
355 QString &remove(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive);
356 QString &replace(int i, int len, QChar after);
357 QString &replace(int i, int len, const QChar *s, int slen);
358 QString &replace(int i, int len, const QString &after);
359 QString &replace(QChar before, QChar after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
360 QString &replace(const QChar *before, int blen, const QChar *after, int alen, Qt::CaseSensitivity cs = Qt::CaseSensitive);
361 QString &replace(const QLatin1String &before, const QLatin1String &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
362 QString &replace(const QLatin1String &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
363 QString &replace(const QString &before, const QLatin1String &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
364 QString &replace(const QString &before, const QString &after,
365 Qt::CaseSensitivity cs = Qt::CaseSensitive);
366 QString &replace(QChar c, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
367 QString &replace(QChar c, const QLatin1String &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
369 QString &replace(const QRegExp &rx, const QString &after);
370 inline QString &remove(const QRegExp &rx)
371 { return replace(rx, QString()); }
374 enum SplitBehavior { KeepEmptyParts, SkipEmptyParts };
376 QStringList split(const QString &sep, SplitBehavior behavior = KeepEmptyParts,
377 Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_REQUIRED_RESULT;
378 QStringList split(QChar sep, SplitBehavior behavior = KeepEmptyParts,
379 Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_REQUIRED_RESULT;
381 QStringList split(const QRegExp &sep, SplitBehavior behavior = KeepEmptyParts) const Q_REQUIRED_RESULT;
384 enum NormalizationForm {
387 NormalizationForm_KD,
390 QString normalized(NormalizationForm mode) const Q_REQUIRED_RESULT;
391 QString normalized(NormalizationForm mode, QChar::UnicodeVersion version) const Q_REQUIRED_RESULT;
393 QString repeated(int times) const;
395 const ushort *utf16() const;
397 QByteArray toAscii() const Q_REQUIRED_RESULT;
398 QByteArray toLatin1() const Q_REQUIRED_RESULT;
399 QByteArray toUtf8() const Q_REQUIRED_RESULT;
400 QByteArray toLocal8Bit() const Q_REQUIRED_RESULT;
401 QVector<uint> toUcs4() const Q_REQUIRED_RESULT;
403 // note - this are all inline so we can benefit from strlen() compile time optimizations
404 static inline QString fromAscii(const char *str, int size = -1)
406 return QString(fromAscii_helper(str, (str && size == -1) ? int(strlen(str)) : size), 0);
408 static inline QString fromLatin1(const char *str, int size = -1)
410 return QString(fromLatin1_helper(str, (str && size == -1) ? int(strlen(str)) : size), 0);
412 static inline QString fromUtf8(const char *str, int size = -1)
414 return fromUtf8_helper(str, (str && size == -1) ? int(strlen(str)) : size);
416 static inline QString fromLocal8Bit(const char *str, int size = -1)
418 return fromLocal8Bit_helper(str, (str && size == -1) ? int(strlen(str)) : size);
420 static QString fromUtf16(const ushort *, int size = -1);
421 static QString fromUcs4(const uint *, int size = -1);
422 static QString fromRawData(const QChar *, int size);
424 inline int toWCharArray(wchar_t *array) const;
425 static inline QString fromWCharArray(const wchar_t *string, int size = -1) Q_REQUIRED_RESULT;
427 QString &setRawData(const QChar *unicode, int size);
428 QString &setUnicode(const QChar *unicode, int size);
429 inline QString &setUtf16(const ushort *utf16, int size);
431 int compare(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
432 int compare(const QLatin1String &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
434 static inline int compare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
435 { return s1.compare(s2, cs); }
437 static inline int compare(const QString& s1, const QLatin1String &s2,
438 Qt::CaseSensitivity cs = Qt::CaseSensitive)
439 { return s1.compare(s2, cs); }
440 static inline int compare(const QLatin1String& s1, const QString &s2,
441 Qt::CaseSensitivity cs = Qt::CaseSensitive)
442 { return -s2.compare(s1, cs); }
444 int compare(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
445 static int compare(const QString &s1, const QStringRef &s2,
446 Qt::CaseSensitivity = Qt::CaseSensitive);
448 int localeAwareCompare(const QString& s) const;
449 static int localeAwareCompare(const QString& s1, const QString& s2)
450 { return s1.localeAwareCompare(s2); }
452 int localeAwareCompare(const QStringRef &s) const;
453 static int localeAwareCompare(const QString& s1, const QStringRef& s2);
455 short toShort(bool *ok=0, int base=10) const;
456 ushort toUShort(bool *ok=0, int base=10) const;
457 int toInt(bool *ok=0, int base=10) const;
458 uint toUInt(bool *ok=0, int base=10) const;
459 long toLong(bool *ok=0, int base=10) const;
460 ulong toULong(bool *ok=0, int base=10) const;
461 qlonglong toLongLong(bool *ok=0, int base=10) const;
462 qulonglong toULongLong(bool *ok=0, int base=10) const;
463 float toFloat(bool *ok=0) const;
464 double toDouble(bool *ok=0) const;
466 QString &setNum(short, int base=10);
467 QString &setNum(ushort, int base=10);
468 QString &setNum(int, int base=10);
469 QString &setNum(uint, int base=10);
470 QString &setNum(long, int base=10);
471 QString &setNum(ulong, int base=10);
472 QString &setNum(qlonglong, int base=10);
473 QString &setNum(qulonglong, int base=10);
474 QString &setNum(float, char f='g', int prec=6);
475 QString &setNum(double, char f='g', int prec=6);
477 static QString number(int, int base=10);
478 static QString number(uint, int base=10);
479 static QString number(long, int base=10);
480 static QString number(ulong, int base=10);
481 static QString number(qlonglong, int base=10);
482 static QString number(qulonglong, int base=10);
483 static QString number(double, char f='g', int prec=6);
485 bool operator==(const QString &s) const;
486 bool operator<(const QString &s) const;
487 inline bool operator>(const QString &s) const { return s < *this; }
488 inline bool operator!=(const QString &s) const { return !operator==(s); }
489 inline bool operator<=(const QString &s) const { return !operator>(s); }
490 inline bool operator>=(const QString &s) const { return !operator<(s); }
492 bool operator==(const QLatin1String &s) const;
493 bool operator<(const QLatin1String &s) const;
494 bool operator>(const QLatin1String &s) const;
495 inline bool operator!=(const QLatin1String &s) const { return !operator==(s); }
496 inline bool operator<=(const QLatin1String &s) const { return !operator>(s); }
497 inline bool operator>=(const QLatin1String &s) const { return !operator<(s); }
499 // ASCII compatibility
500 #ifndef QT_NO_CAST_FROM_ASCII
501 inline QT_ASCII_CAST_WARN_CONSTRUCTOR QString(const char *ch)
502 : d(fromAscii_helper(ch, ch ? int(strlen(ch)) : -1))
504 inline QT_ASCII_CAST_WARN_CONSTRUCTOR QString(const QByteArray &a)
505 : d(fromAscii_helper(a.constData(), qstrnlen(a.constData(), a.size())))
507 inline QT_ASCII_CAST_WARN QString &operator=(const char *ch)
508 { return (*this = fromAscii(ch, ch ? int(strlen(ch)) : -1)); }
509 inline QT_ASCII_CAST_WARN QString &operator=(const QByteArray &a)
510 { return (*this = fromAscii(a.constData(), qstrnlen(a.constData(), a.size()))); }
511 inline QT_ASCII_CAST_WARN QString &operator=(char c)
512 { return (*this = QChar::fromAscii(c)); }
514 // these are needed, so it compiles with STL support enabled
515 inline QT_ASCII_CAST_WARN QString &prepend(const char *s)
516 { return prepend(QString::fromAscii(s, s ? int(strlen(s)) : -1)); }
517 inline QT_ASCII_CAST_WARN QString &prepend(const QByteArray &s)
518 { return prepend(QString::fromAscii(s.constData(), qstrnlen(s.constData(), s.size()))); }
519 inline QT_ASCII_CAST_WARN QString &append(const char *s)
520 { return append(QString::fromAscii(s, s ? int(strlen(s)) : -1)); }
521 inline QT_ASCII_CAST_WARN QString &append(const QByteArray &s)
522 { return append(QString::fromAscii(s.constData(), qstrnlen(s.constData(), s.size()))); }
523 inline QT_ASCII_CAST_WARN QString &operator+=(const char *s)
524 { return append(QString::fromAscii(s, s ? int(strlen(s)) : -1)); }
525 inline QT_ASCII_CAST_WARN QString &operator+=(const QByteArray &s)
526 { return append(QString::fromAscii(s.constData(), qstrnlen(s.constData(), s.size()))); }
527 inline QT_ASCII_CAST_WARN QString &operator+=(char c)
528 { return append(QChar::fromAscii(c)); }
530 inline QT_ASCII_CAST_WARN bool operator==(const char *s) const;
531 inline QT_ASCII_CAST_WARN bool operator!=(const char *s) const;
532 inline QT_ASCII_CAST_WARN bool operator<(const char *s) const;
533 inline QT_ASCII_CAST_WARN bool operator<=(const char *s2) const;
534 inline QT_ASCII_CAST_WARN bool operator>(const char *s2) const;
535 inline QT_ASCII_CAST_WARN bool operator>=(const char *s2) const;
537 inline QT_ASCII_CAST_WARN bool operator==(const QByteArray &s) const;
538 inline QT_ASCII_CAST_WARN bool operator!=(const QByteArray &s) const;
539 inline QT_ASCII_CAST_WARN bool operator<(const QByteArray &s) const
540 { return *this < QString::fromAscii(s.constData(), s.size()); }
541 inline QT_ASCII_CAST_WARN bool operator>(const QByteArray &s) const
542 { return *this > QString::fromAscii(s.constData(), s.size()); }
543 inline QT_ASCII_CAST_WARN bool operator<=(const QByteArray &s) const
544 { return *this <= QString::fromAscii(s.constData(), s.size()); }
545 inline QT_ASCII_CAST_WARN bool operator>=(const QByteArray &s) const
546 { return *this >= QString::fromAscii(s.constData(), s.size()); }
549 typedef QChar *iterator;
550 typedef const QChar *const_iterator;
551 typedef iterator Iterator;
552 typedef const_iterator ConstIterator;
554 const_iterator begin() const;
555 const_iterator constBegin() const;
557 const_iterator end() const;
558 const_iterator constEnd() const;
561 typedef const QChar & const_reference;
562 typedef QChar & reference;
563 typedef QChar value_type;
564 inline void push_back(QChar c) { append(c); }
565 inline void push_back(const QString &s) { append(s); }
566 inline void push_front(QChar c) { prepend(c); }
567 inline void push_front(const QString &s) { prepend(s); }
570 static inline QString fromStdString(const std::string &s);
571 inline std::string toStdString() const;
572 static inline QString fromStdWString(const std::wstring &s);
573 inline std::wstring toStdWString() const;
578 static const Null null;
579 inline QString(const Null &): d(const_cast<Data *>(&shared_null.str)) {}
580 inline QString &operator=(const Null &) { *this = QString(); return *this; }
581 inline bool isNull() const { return d == &shared_null.str; }
584 bool isSimpleText() const;
585 bool isRightToLeft() const;
587 QString(int size, Qt::Initialization);
589 inline QString(const QConstStringData<n> &dd) : d(const_cast<QStringData *>(&dd.str)) {}
591 Q_DECL_CONSTEXPR inline QString(QConstStringDataPtr<N> dd) : d(const_cast<QStringData *>(&dd.ptr->str)) {}
594 #if defined(QT_NO_CAST_FROM_ASCII) && !defined(Q_NO_DECLARED_NOT_DEFINED)
595 QString &operator+=(const char *s);
596 QString &operator+=(const QByteArray &s);
597 QString(const char *ch);
598 QString(const QByteArray &a);
599 QString &operator=(const char *ch);
600 QString &operator=(const QByteArray &a);
603 static const QConstStringData<1> shared_null;
604 static const QConstStringData<1> shared_empty;
606 inline QString(Data *dd, int /*dummy*/) : d(dd) {}
608 #ifndef QT_NO_TEXTCODEC
609 static QTextCodec *codecForCStrings;
611 static int grow(int);
612 static void free(Data *);
614 void realloc(int alloc);
616 void updateProperties() const;
617 QString multiArg(int numArgs, const QString **args) const;
618 static int compare_helper(const QChar *data1, int length1,
619 const QChar *data2, int length2,
620 Qt::CaseSensitivity cs = Qt::CaseSensitive);
621 static int compare_helper(const QChar *data1, int length1,
623 Qt::CaseSensitivity cs = Qt::CaseSensitive);
624 static int localeAwareCompare_helper(const QChar *data1, int length1,
625 const QChar *data2, int length2);
626 static Data *fromLatin1_helper(const char *str, int size = -1);
627 static Data *fromAscii_helper(const char *str, int size = -1);
628 static QString fromUtf8_helper(const char *str, int size);
629 static QString fromLocal8Bit_helper(const char *, int size);
630 static int toUcs4_helper(const ushort *uc, int length, uint *out);
631 void replace_helper(uint *indices, int nIndices, int blen, const QChar *after, int alen);
632 friend class QCharRef;
633 friend class QTextCodec;
634 friend class QStringRef;
635 friend struct QAbstractConcatenable;
636 friend inline bool qStringComparisonHelper(const QString &s1, const char *s2);
637 friend inline bool qStringComparisonHelper(const QStringRef &s1, const char *s2);
639 typedef Data * DataPtr;
640 inline DataPtr &data_ptr() { return d; }
647 Q_DECL_CONSTEXPR inline explicit QLatin1String(const char *s) : m_size(s ? int(strlen(s)) : 0), m_data(s) {}
648 Q_DECL_CONSTEXPR inline explicit QLatin1String(const char *s, int sz) : m_size(sz), m_data(s) {}
650 inline const char *latin1() const { return m_data; }
651 inline int size() const { return m_size; }
652 inline const char *data() const { return m_data; }
654 inline bool operator==(const QString &s) const
655 { return s == *this; }
656 inline bool operator!=(const QString &s) const
657 { return s != *this; }
658 inline bool operator>(const QString &s) const
659 { return s < *this; }
660 inline bool operator<(const QString &s) const
661 { return s > *this; }
662 inline bool operator>=(const QString &s) const
663 { return s <= *this; }
664 inline bool operator<=(const QString &s) const
665 { return s >= *this; }
667 inline QT_ASCII_CAST_WARN bool operator==(const char *s) const
668 { return QString::fromAscii(s, s ? int(strlen(s)) : -1) == *this; }
669 inline QT_ASCII_CAST_WARN bool operator!=(const char *s) const
670 { return QString::fromAscii(s, s ? int(strlen(s)) : -1) != *this; }
671 inline QT_ASCII_CAST_WARN bool operator<(const char *s) const
672 { return QString::fromAscii(s, s ? int(strlen(s)) : -1) > *this; }
673 inline QT_ASCII_CAST_WARN bool operator>(const char *s) const
674 { return QString::fromAscii(s, s ? int(strlen(s)) : -1) < *this; }
675 inline QT_ASCII_CAST_WARN bool operator<=(const char *s) const
676 { return QString::fromAscii(s, s ? int(strlen(s)) : -1) >= *this; }
677 inline QT_ASCII_CAST_WARN bool operator>=(const char *s) const
678 { return QString::fromAscii(s, s ? int(strlen(s)) : -1) <= *this; }
684 // Qt 4.x compatibility
685 typedef QLatin1String QLatin1Literal;
688 inline QString::QString(const QLatin1String &aLatin1) : d(fromLatin1_helper(aLatin1.latin1(), aLatin1.size()))
690 inline int QString::length() const
692 inline const QChar QString::at(int i) const
693 { Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; }
694 inline const QChar QString::operator[](int i) const
695 { Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; }
696 inline const QChar QString::operator[](uint i) const
697 { Q_ASSERT(i < uint(size())); return d->data()[i]; }
698 inline bool QString::isEmpty() const
699 { return d->size == 0; }
700 inline const QChar *QString::unicode() const
701 { return reinterpret_cast<const QChar*>(d->data()); }
702 inline const QChar *QString::data() const
703 { return reinterpret_cast<const QChar*>(d->data()); }
704 inline QChar *QString::data()
705 { detach(); return reinterpret_cast<QChar*>(d->data()); }
706 inline const QChar *QString::constData() const
707 { return reinterpret_cast<const QChar*>(d->data()); }
708 inline void QString::detach()
709 { if (d->ref != 1 || d->offset) realloc(); }
710 inline bool QString::isDetached() const
711 { return d->ref == 1; }
712 inline QString &QString::operator=(const QLatin1String &s)
714 *this = fromLatin1(s.latin1(), s.size());
717 inline void QString::clear()
718 { if (!isNull()) *this = QString(); }
719 inline QString::QString(const QString &other) : d(other.d)
720 { Q_ASSERT(&other != this); d->ref.ref(); }
721 inline int QString::capacity() const
723 inline QString &QString::setNum(short n, int base)
724 { return setNum(qlonglong(n), base); }
725 inline QString &QString::setNum(ushort n, int base)
726 { return setNum(qulonglong(n), base); }
727 inline QString &QString::setNum(int n, int base)
728 { return setNum(qlonglong(n), base); }
729 inline QString &QString::setNum(uint n, int base)
730 { return setNum(qulonglong(n), base); }
731 inline QString &QString::setNum(long n, int base)
732 { return setNum(qlonglong(n), base); }
733 inline QString &QString::setNum(ulong n, int base)
734 { return setNum(qulonglong(n), base); }
735 inline QString &QString::setNum(float n, char f, int prec)
736 { return setNum(double(n),f,prec); }
737 inline QString QString::arg(int a, int fieldWidth, int base, QChar fillChar) const
738 { return arg(qlonglong(a), fieldWidth, base, fillChar); }
739 inline QString QString::arg(uint a, int fieldWidth, int base, QChar fillChar) const
740 { return arg(qulonglong(a), fieldWidth, base, fillChar); }
741 inline QString QString::arg(long a, int fieldWidth, int base, QChar fillChar) const
742 { return arg(qlonglong(a), fieldWidth, base, fillChar); }
743 inline QString QString::arg(ulong a, int fieldWidth, int base, QChar fillChar) const
744 { return arg(qulonglong(a), fieldWidth, base, fillChar); }
745 inline QString QString::arg(short a, int fieldWidth, int base, QChar fillChar) const
746 { return arg(qlonglong(a), fieldWidth, base, fillChar); }
747 inline QString QString::arg(ushort a, int fieldWidth, int base, QChar fillChar) const
748 { return arg(qulonglong(a), fieldWidth, base, fillChar); }
749 inline QString QString::arg(const QString &a1, const QString &a2) const
750 { const QString *args[2] = { &a1, &a2 }; return multiArg(2, args); }
751 inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3) const
752 { const QString *args[3] = { &a1, &a2, &a3 }; return multiArg(3, args); }
753 inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
754 const QString &a4) const
755 { const QString *args[4] = { &a1, &a2, &a3, &a4 }; return multiArg(4, args); }
756 inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
757 const QString &a4, const QString &a5) const
758 { const QString *args[5] = { &a1, &a2, &a3, &a4, &a5 }; return multiArg(5, args); }
759 inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
760 const QString &a4, const QString &a5, const QString &a6) const
761 { const QString *args[6] = { &a1, &a2, &a3, &a4, &a5, &a6 }; return multiArg(6, args); }
762 inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
763 const QString &a4, const QString &a5, const QString &a6,
764 const QString &a7) const
765 { const QString *args[7] = { &a1, &a2, &a3, &a4, &a5, &a6, &a7 }; return multiArg(7, args); }
766 inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
767 const QString &a4, const QString &a5, const QString &a6,
768 const QString &a7, const QString &a8) const
769 { const QString *args[8] = { &a1, &a2, &a3, &a4, &a5, &a6, &a7, &a8 }; return multiArg(8, args); }
770 inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
771 const QString &a4, const QString &a5, const QString &a6,
772 const QString &a7, const QString &a8, const QString &a9) const
773 { const QString *args[9] = { &a1, &a2, &a3, &a4, &a5, &a6, &a7, &a8, &a9 }; return multiArg(9, args); }
775 inline QString QString::section(QChar asep, int astart, int aend, SectionFlags aflags) const
776 { return section(QString(asep), astart, aend, aflags); }
778 inline int QString::toWCharArray(wchar_t *array) const
780 if (sizeof(wchar_t) == sizeof(QChar)) {
781 qMemCopy(array, d->data(), sizeof(QChar) * size());
784 return toUcs4_helper(d->data(), size(), reinterpret_cast<uint *>(array));
786 inline QString QString::fromWCharArray(const wchar_t *string, int size)
788 return sizeof(wchar_t) == sizeof(QChar) ? fromUtf16(reinterpret_cast<const ushort *>(string), size)
789 : fromUcs4(reinterpret_cast<const uint *>(string), size);
793 class Q_CORE_EXPORT QCharRef {
796 inline QCharRef(QString &str, int idx)
798 friend class QString;
801 // most QChar operations repeated here
803 // all this is not documented: We just say "like QChar" and let it be.
804 inline operator QChar() const
805 { return i < s.d->size ? s.d->data()[i] : 0; }
806 inline QCharRef &operator=(QChar c)
807 { if (i >= s.d->size) s.expand(i); else s.detach();
808 s.d->data()[i] = c.unicode(); return *this; }
810 // An operator= for each QChar cast constructors
811 #ifndef QT_NO_CAST_FROM_ASCII
812 inline QT_ASCII_CAST_WARN QCharRef &operator=(char c)
813 { return operator=(QChar::fromAscii(c)); }
814 inline QT_ASCII_CAST_WARN QCharRef &operator=(uchar c)
815 { return operator=(QChar::fromAscii(c)); }
817 inline QCharRef &operator=(const QCharRef &c) { return operator=(QChar(c)); }
818 inline QCharRef &operator=(ushort rc) { return operator=(QChar(rc)); }
819 inline QCharRef &operator=(short rc) { return operator=(QChar(rc)); }
820 inline QCharRef &operator=(uint rc) { return operator=(QChar(rc)); }
821 inline QCharRef &operator=(int rc) { return operator=(QChar(rc)); }
824 inline bool isNull() const { return QChar(*this).isNull(); }
825 inline bool isPrint() const { return QChar(*this).isPrint(); }
826 inline bool isPunct() const { return QChar(*this).isPunct(); }
827 inline bool isSpace() const { return QChar(*this).isSpace(); }
828 inline bool isMark() const { return QChar(*this).isMark(); }
829 inline bool isLetter() const { return QChar(*this).isLetter(); }
830 inline bool isNumber() const { return QChar(*this).isNumber(); }
831 inline bool isLetterOrNumber() { return QChar(*this).isLetterOrNumber(); }
832 inline bool isDigit() const { return QChar(*this).isDigit(); }
833 inline bool isLower() const { return QChar(*this).isLower(); }
834 inline bool isUpper() const { return QChar(*this).isUpper(); }
835 inline bool isTitleCase() const { return QChar(*this).isTitleCase(); }
837 inline int digitValue() const { return QChar(*this).digitValue(); }
838 QChar toLower() const { return QChar(*this).toLower(); }
839 QChar toUpper() const { return QChar(*this).toUpper(); }
840 QChar toTitleCase () const { return QChar(*this).toTitleCase(); }
842 QChar::Category category() const { return QChar(*this).category(); }
843 QChar::Direction direction() const { return QChar(*this).direction(); }
844 QChar::Joining joining() const { return QChar(*this).joining(); }
845 bool hasMirrored() const { return QChar(*this).hasMirrored(); }
846 QChar mirroredChar() const { return QChar(*this).mirroredChar(); }
847 QString decomposition() const { return QChar(*this).decomposition(); }
848 QChar::Decomposition decompositionTag() const { return QChar(*this).decompositionTag(); }
849 uchar combiningClass() const { return QChar(*this).combiningClass(); }
851 QChar::UnicodeVersion unicodeVersion() const { return QChar(*this).unicodeVersion(); }
853 inline uchar cell() const { return QChar(*this).cell(); }
854 inline uchar row() const { return QChar(*this).row(); }
855 inline void setCell(uchar cell);
856 inline void setRow(uchar row);
858 char toAscii() const { return QChar(*this).toAscii(); }
859 char toLatin1() const { return QChar(*this).toLatin1(); }
860 ushort unicode() const { return QChar(*this).unicode(); }
861 ushort& unicode() { return s.data()[i].unicode(); }
865 inline void QCharRef::setRow(uchar arow) { QChar(*this).setRow(arow); }
866 inline void QCharRef::setCell(uchar acell) { QChar(*this).setCell(acell); }
869 inline QString::QString() : d(const_cast<Data *>(&shared_null.str)) {}
870 inline QString::~QString() { if (!d->ref.deref()) free(d); }
872 inline void QString::reserve(int asize)
874 if (d->ref != 1 || asize > int(d->alloc))
877 if (!d->capacityReserved) {
878 // cannot set unconditionally, since d could be the shared_null/shared_empty (which is const)
879 d->capacityReserved = true;
883 inline void QString::squeeze()
885 if (d->ref > 1 || d->size < int(d->alloc))
888 if (d->capacityReserved) {
889 // cannot set unconditionally, since d could be the shared_null/shared_empty (which is const)
890 d->capacityReserved = false;
894 inline QString &QString::setUtf16(const ushort *autf16, int asize)
895 { return setUnicode(reinterpret_cast<const QChar *>(autf16), asize); }
896 inline QCharRef QString::operator[](int i)
897 { Q_ASSERT(i >= 0); return QCharRef(*this, i); }
898 inline QCharRef QString::operator[](uint i)
899 { return QCharRef(*this, i); }
900 inline QString::iterator QString::begin()
901 { detach(); return reinterpret_cast<QChar*>(d->data()); }
902 inline QString::const_iterator QString::begin() const
903 { return reinterpret_cast<const QChar*>(d->data()); }
904 inline QString::const_iterator QString::constBegin() const
905 { return reinterpret_cast<const QChar*>(d->data()); }
906 inline QString::iterator QString::end()
907 { detach(); return reinterpret_cast<QChar*>(d->data() + d->size); }
908 inline QString::const_iterator QString::end() const
909 { return reinterpret_cast<const QChar*>(d->data() + d->size); }
910 inline QString::const_iterator QString::constEnd() const
911 { return reinterpret_cast<const QChar*>(d->data() + d->size); }
912 inline bool QString::contains(const QString &s, Qt::CaseSensitivity cs) const
913 { return indexOf(s, 0, cs) != -1; }
914 inline bool QString::contains(const QStringRef &s, Qt::CaseSensitivity cs) const
915 { return indexOf(s, 0, cs) != -1; }
916 inline bool QString::contains(QChar c, Qt::CaseSensitivity cs) const
917 { return indexOf(c, 0, cs) != -1; }
920 inline bool operator==(QString::Null, QString::Null) { return true; }
921 inline bool operator==(QString::Null, const QString &s) { return s.isNull(); }
922 inline bool operator==(const QString &s, QString::Null) { return s.isNull(); }
923 inline bool operator!=(QString::Null, QString::Null) { return false; }
924 inline bool operator!=(QString::Null, const QString &s) { return !s.isNull(); }
925 inline bool operator!=(const QString &s, QString::Null) { return !s.isNull(); }
927 #ifndef QT_NO_CAST_FROM_ASCII
928 inline bool qStringComparisonHelper(const QString &s1, const char *s2)
930 # ifndef QT_NO_TEXTCODEC
931 if (QString::codecForCStrings) return (s1 == QString::fromAscii(s2, s2 ? int(strlen(s2)) : -1));
933 return (s1 == QLatin1String(s2));
935 inline bool QString::operator==(const char *s) const
936 { return qStringComparisonHelper(*this, s); }
937 inline bool QString::operator!=(const char *s) const
938 { return !qStringComparisonHelper(*this, s); }
939 inline bool QString::operator<(const char *s) const
940 { return *this < QString::fromAscii(s, s ? int(strlen(s)) : -1); }
941 inline bool QString::operator>(const char *s) const
942 { return *this > QString::fromAscii(s, s ? int(strlen(s)) : -1); }
943 inline bool QString::operator<=(const char *s) const
944 { return *this <= QString::fromAscii(s, s ? int(strlen(s)) : -1); }
945 inline bool QString::operator>=(const char *s) const
946 { return *this >= QString::fromAscii(s, s ? int(strlen(s)) : -1); }
948 inline QT_ASCII_CAST_WARN bool operator==(const char *s1, const QString &s2)
949 { return qStringComparisonHelper(s2, s1); }
950 inline QT_ASCII_CAST_WARN bool operator!=(const char *s1, const QString &s2)
951 { return !qStringComparisonHelper(s2, s1); }
952 inline QT_ASCII_CAST_WARN bool operator<(const char *s1, const QString &s2)
953 { return (QString::fromAscii(s1, s1 ? int(strlen(s1)) : -1) < s2); }
954 inline QT_ASCII_CAST_WARN bool operator>(const char *s1, const QString &s2)
955 { return (QString::fromAscii(s1, s1 ? int(strlen(s1)) : -1) > s2); }
956 inline QT_ASCII_CAST_WARN bool operator<=(const char *s1, const QString &s2)
957 { return (QString::fromAscii(s1, s1 ? int(strlen(s1)) : -1) <= s2); }
958 inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, const QString &s2)
959 { return (QString::fromAscii(s1, s1 ? int(strlen(s1)) : -1) >= s2); }
961 inline QT_ASCII_CAST_WARN bool operator==(const char *s1, const QLatin1String &s2)
962 { return QString::fromAscii(s1, s1 ? int(strlen(s1)) : -1) == s2; }
963 inline QT_ASCII_CAST_WARN bool operator!=(const char *s1, const QLatin1String &s2)
964 { return QString::fromAscii(s1, s1 ? int(strlen(s1)) : -1) != s2; }
965 inline QT_ASCII_CAST_WARN bool operator<(const char *s1, const QLatin1String &s2)
966 { return (QString::fromAscii(s1, s1 ? int(strlen(s1)) : -1) < s2); }
967 inline QT_ASCII_CAST_WARN bool operator>(const char *s1, const QLatin1String &s2)
968 { return (QString::fromAscii(s1, s1 ? int(strlen(s1)) : -1) > s2); }
969 inline QT_ASCII_CAST_WARN bool operator<=(const char *s1, const QLatin1String &s2)
970 { return (QString::fromAscii(s1, s1 ? int(strlen(s1)) : -1) <= s2); }
971 inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, const QLatin1String &s2)
972 { return (QString::fromAscii(s1, s1 ? int(strlen(s1)) : -1) >= s2); }
974 inline bool operator==(const QLatin1String &s1, const QLatin1String &s2)
975 { return (s1.size() == s2.size() && !memcmp(s1.latin1(), s2.latin1(), s1.size())); }
976 inline bool operator!=(const QLatin1String &s1, const QLatin1String &s2)
977 { return (s1.size() != s2.size() || memcmp(s1.latin1(), s2.latin1(), s1.size())); }
978 inline bool operator<(const QLatin1String &s1, const QLatin1String &s2)
979 { int r = memcmp(s1.latin1(), s2.latin1(), qMin(s1.size(), s2.size()));
980 return (r < 0) || (r == 0 && s1.size() < s2.size()); }
981 inline bool operator<=(const QLatin1String &s1, const QLatin1String &s2)
982 { int r = memcmp(s1.latin1(), s2.latin1(), qMin(s1.size(), s2.size()));
983 return (r < 0) || (r == 0 && s1.size() <= s2.size()); }
984 inline bool operator>(const QLatin1String &s1, const QLatin1String &s2)
985 { int r = memcmp(s1.latin1(), s2.latin1(), qMin(s1.size(), s2.size()));
986 return (r > 0) || (r == 0 && s1.size() > s2.size()); }
987 inline bool operator>=(const QLatin1String &s1, const QLatin1String &s2)
988 { int r = memcmp(s1.latin1(), s2.latin1(), qMin(s1.size(), s2.size()));
989 return (r > 0) || (r == 0 && s1.size() >= s2.size()); }
992 inline bool QString::operator==(const QByteArray &s) const
993 { return qStringComparisonHelper(*this, s.constData()); }
994 inline bool QString::operator!=(const QByteArray &s) const
995 { return !qStringComparisonHelper(*this, s.constData()); }
997 inline bool QByteArray::operator==(const QString &s) const
998 { return qStringComparisonHelper(s, constData()); }
999 inline bool QByteArray::operator!=(const QString &s) const
1000 { return !qStringComparisonHelper(s, constData()); }
1001 inline bool QByteArray::operator<(const QString &s) const
1002 { return QString::fromAscii(constData(), size()) < s; }
1003 inline bool QByteArray::operator>(const QString &s) const
1004 { return QString::fromAscii(constData(), size()) > s; }
1005 inline bool QByteArray::operator<=(const QString &s) const
1006 { return QString::fromAscii(constData(), size()) <= s; }
1007 inline bool QByteArray::operator>=(const QString &s) const
1008 { return QString::fromAscii(constData(), size()) >= s; }
1009 #endif // QT_NO_CAST_FROM_ASCII
1011 #ifndef QT_NO_CAST_TO_ASCII
1012 inline QByteArray &QByteArray::append(const QString &s)
1013 { return append(s.toAscii()); }
1014 inline QByteArray &QByteArray::insert(int i, const QString &s)
1015 { return insert(i, s.toAscii()); }
1016 inline QByteArray &QByteArray::replace(char c, const QString &after)
1017 { return replace(c, after.toAscii()); }
1018 inline QByteArray &QByteArray::replace(const QString &before, const char *after)
1019 { return replace(before.toAscii(), after); }
1020 inline QByteArray &QByteArray::replace(const QString &before, const QByteArray &after)
1021 { return replace(before.toAscii(), after); }
1022 inline QByteArray &QByteArray::operator+=(const QString &s)
1023 { return operator+=(s.toAscii()); }
1024 inline int QByteArray::indexOf(const QString &s, int from) const
1025 { return indexOf(s.toAscii(), from); }
1026 inline int QByteArray::lastIndexOf(const QString &s, int from) const
1027 { return lastIndexOf(s.toAscii(), from); }
1028 #endif // QT_NO_CAST_TO_ASCII
1030 #if !defined(QT_USE_FAST_OPERATOR_PLUS) && !defined(QT_USE_QSTRINGBUILDER)
1031 inline const QString operator+(const QString &s1, const QString &s2)
1032 { QString t(s1); t += s2; return t; }
1033 inline const QString operator+(const QString &s1, QChar s2)
1034 { QString t(s1); t += s2; return t; }
1035 inline const QString operator+(QChar s1, const QString &s2)
1036 { QString t(s1); t += s2; return t; }
1037 # ifndef QT_NO_CAST_FROM_ASCII
1038 inline QT_ASCII_CAST_WARN const QString operator+(const QString &s1, const char *s2)
1039 { QString t(s1); t += QString::fromAscii(s2, s2 ? int(strlen(s2)) : -1); return t; }
1040 inline QT_ASCII_CAST_WARN const QString operator+(const char *s1, const QString &s2)
1041 { QString t = QString::fromAscii(s1, s1 ? int(strlen(s1)) : -1); t += s2; return t; }
1042 inline QT_ASCII_CAST_WARN const QString operator+(char c, const QString &s)
1043 { QString t = s; t.prepend(QChar::fromAscii(c)); return t; }
1044 inline QT_ASCII_CAST_WARN const QString operator+(const QString &s, char c)
1045 { QString t = s; t += QChar::fromAscii(c); return t; }
1046 inline QT_ASCII_CAST_WARN const QString operator+(const QByteArray &ba, const QString &s)
1047 { QString t = QString::fromAscii(ba.constData(), qstrnlen(ba.constData(), ba.size())); t += s; return t; }
1048 inline QT_ASCII_CAST_WARN const QString operator+(const QString &s, const QByteArray &ba)
1049 { QString t(s); t += QString::fromAscii(ba.constData(), qstrnlen(ba.constData(), ba.size())); return t; }
1050 # endif // QT_NO_CAST_FROM_ASCII
1051 #endif // QT_USE_QSTRINGBUILDER
1054 inline std::string QString::toStdString() const
1055 { const QByteArray asc = toAscii(); return std::string(asc.constData(), asc.length()); }
1057 inline QString QString::fromStdString(const std::string &s)
1058 { return fromAscii(s.data(), int(s.size())); }
1060 inline std::wstring QString::toStdWString() const
1063 str.resize(length());
1065 #if defined(_MSC_VER) && _MSC_VER >= 1400
1066 // VS2005 crashes if the string is empty
1071 str.resize(toWCharArray(&(*str.begin())));
1074 inline QString QString::fromStdWString(const std::wstring &s)
1075 { return fromWCharArray(s.data(), int(s.size())); }
1078 #if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE))
1079 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QString &);
1080 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QString &);
1083 Q_DECLARE_TYPEINFO(QString, Q_MOVABLE_TYPE);
1084 Q_DECLARE_SHARED(QString)
1085 Q_DECLARE_OPERATORS_FOR_FLAGS(QString::SectionFlags)
1088 class Q_CORE_EXPORT QStringRef {
1089 const QString *m_string;
1093 inline QStringRef():m_string(0), m_position(0), m_size(0){}
1094 inline QStringRef(const QString *string, int position, int size);
1095 inline QStringRef(const QString *string);
1096 inline QStringRef(const QStringRef &other)
1097 :m_string(other.m_string), m_position(other.m_position), m_size(other.m_size)
1100 inline ~QStringRef(){}
1101 inline const QString *string() const { return m_string; }
1102 inline int position() const { return m_position; }
1103 inline int size() const { return m_size; }
1104 inline int count() const { return m_size; }
1105 inline int length() const { return m_size; }
1107 inline QStringRef &operator=(const QStringRef &other) {
1108 m_string = other.m_string; m_position = other.m_position;
1109 m_size = other.m_size; return *this;
1112 int indexOf(const QString &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1113 int indexOf(QChar ch, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1114 int indexOf(QLatin1String str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1115 int indexOf(const QStringRef &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1116 int lastIndexOf(const QString &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1117 int lastIndexOf(QChar ch, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1118 int lastIndexOf(QLatin1String str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1119 int lastIndexOf(const QStringRef &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1121 inline bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1122 inline bool contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1123 inline bool contains(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1124 inline bool contains(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1126 int count(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1127 int count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1128 int count(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1130 bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1131 bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1132 bool startsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1133 bool startsWith(const QStringRef &c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1135 bool endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1136 bool endsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1137 bool endsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1138 bool endsWith(const QStringRef &c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1140 inline QStringRef &operator=(const QString *string);
1142 inline const QChar *unicode() const {
1144 return reinterpret_cast<const QChar *>(QString::shared_null.str.data());
1145 return m_string->unicode() + m_position;
1147 inline const QChar *data() const { return unicode(); }
1148 inline const QChar *constData() const { return unicode(); }
1150 QByteArray toAscii() const Q_REQUIRED_RESULT;
1151 QByteArray toLatin1() const Q_REQUIRED_RESULT;
1152 QByteArray toUtf8() const Q_REQUIRED_RESULT;
1153 QByteArray toLocal8Bit() const Q_REQUIRED_RESULT;
1154 QVector<uint> toUcs4() const Q_REQUIRED_RESULT;
1156 inline void clear() { m_string = 0; m_position = m_size = 0; }
1157 QString toString() const;
1158 inline bool isEmpty() const { return m_size == 0; }
1159 inline bool isNull() const { return m_string == 0 || m_string->isNull(); }
1161 QStringRef appendTo(QString *string) const;
1163 inline const QChar at(int i) const
1164 { Q_ASSERT(i >= 0 && i < size()); return m_string->at(i + m_position); }
1166 int compare(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1167 int compare(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1168 int compare(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
1169 static int compare(const QStringRef &s1, const QString &s2,
1170 Qt::CaseSensitivity = Qt::CaseSensitive);
1171 static int compare(const QStringRef &s1, const QStringRef &s2,
1172 Qt::CaseSensitivity = Qt::CaseSensitive);
1173 static int compare(const QStringRef &s1, QLatin1String s2,
1174 Qt::CaseSensitivity cs = Qt::CaseSensitive);
1176 int localeAwareCompare(const QString &s) const;
1177 int localeAwareCompare(const QStringRef &s) const;
1178 static int localeAwareCompare(const QStringRef &s1, const QString &s2);
1179 static int localeAwareCompare(const QStringRef &s1, const QStringRef &s2);
1182 inline QStringRef &QStringRef::operator=(const QString *aString)
1183 { m_string = aString; m_position = 0; m_size = aString?aString->size():0; return *this; }
1185 inline QStringRef::QStringRef(const QString *aString, int aPosition, int aSize)
1186 :m_string(aString), m_position(aPosition), m_size(aSize){}
1188 inline QStringRef::QStringRef(const QString *aString)
1189 :m_string(aString), m_position(0), m_size(aString?aString->size() : 0){}
1191 Q_CORE_EXPORT bool operator==(const QStringRef &s1,const QStringRef &s2);
1192 inline bool operator!=(const QStringRef &s1,const QStringRef &s2)
1193 { return !(s1 == s2); }
1194 Q_CORE_EXPORT bool operator==(const QString &s1,const QStringRef &s2);
1195 inline bool operator!=(const QString &s1,const QStringRef &s2)
1196 { return !(s1 == s2); }
1197 inline bool operator==(const QStringRef &s1,const QString &s2)
1198 { return s2 == s1; }
1199 inline bool operator!=(const QStringRef &s1,const QString &s2)
1200 { return s2 != s1; }
1201 Q_CORE_EXPORT bool operator==(const QLatin1String &s1, const QStringRef &s2);
1202 inline bool operator!=(const QLatin1String &s1,const QStringRef &s2)
1203 { return !(s1 == s2); }
1204 inline bool operator==(const QStringRef &s1,const QLatin1String &s2)
1205 { return s2 == s1; }
1206 inline bool operator!=(const QStringRef &s1,const QLatin1String &s2)
1207 { return s2 != s1; }
1209 Q_CORE_EXPORT bool operator<(const QStringRef &s1,const QStringRef &s2);
1210 inline bool operator>(const QStringRef &s1, const QStringRef &s2)
1212 inline bool operator<=(const QStringRef &s1, const QStringRef &s2)
1213 { return !(s1 > s2); }
1214 inline bool operator>=(const QStringRef &s1, const QStringRef &s2)
1215 { return !(s1 < s2); }
1217 inline bool qStringComparisonHelper(const QStringRef &s1, const char *s2)
1219 # ifndef QT_NO_TEXTCODEC
1220 if (QString::codecForCStrings) return (s1 == QString::fromAscii(s2, s2 ? int(strlen(s2)) : -1));
1222 return (s1 == QLatin1String(s2));
1225 inline QT_ASCII_CAST_WARN bool operator==(const char *s1, const QStringRef &s2)
1226 { return qStringComparisonHelper(s2, s1); }
1227 inline QT_ASCII_CAST_WARN bool operator==(const QStringRef &s1, const char *s2)
1228 { return qStringComparisonHelper(s1, s2); }
1229 inline QT_ASCII_CAST_WARN bool operator!=(const char *s1, const QStringRef &s2)
1230 { return !qStringComparisonHelper(s2, s1); }
1231 inline QT_ASCII_CAST_WARN bool operator!=(const QStringRef &s1, const char *s2)
1232 { return !qStringComparisonHelper(s1, s2); }
1234 inline int QString::compare(const QStringRef &s, Qt::CaseSensitivity cs) const
1235 { return QString::compare_helper(constData(), length(), s.constData(), s.length(), cs); }
1236 inline int QString::compare(const QString &s1, const QStringRef &s2, Qt::CaseSensitivity cs)
1237 { return QString::compare_helper(s1.constData(), s1.length(), s2.constData(), s2.length(), cs); }
1238 inline int QStringRef::compare(const QString &s, Qt::CaseSensitivity cs) const
1239 { return QString::compare_helper(constData(), length(), s.constData(), s.length(), cs); }
1240 inline int QStringRef::compare(const QStringRef &s, Qt::CaseSensitivity cs) const
1241 { return QString::compare_helper(constData(), length(), s.constData(), s.length(), cs); }
1242 inline int QStringRef::compare(QLatin1String s, Qt::CaseSensitivity cs) const
1243 { return QString::compare_helper(constData(), length(), s, cs); }
1244 inline int QStringRef::compare(const QStringRef &s1, const QString &s2, Qt::CaseSensitivity cs)
1245 { return QString::compare_helper(s1.constData(), s1.length(), s2.constData(), s2.length(), cs); }
1246 inline int QStringRef::compare(const QStringRef &s1, const QStringRef &s2, Qt::CaseSensitivity cs)
1247 { return QString::compare_helper(s1.constData(), s1.length(), s2.constData(), s2.length(), cs); }
1248 inline int QStringRef::compare(const QStringRef &s1, QLatin1String s2, Qt::CaseSensitivity cs)
1249 { return QString::compare_helper(s1.constData(), s1.length(), s2, cs); }
1251 inline int QString::localeAwareCompare(const QStringRef &s) const
1252 { return localeAwareCompare_helper(constData(), length(), s.constData(), s.length()); }
1253 inline int QString::localeAwareCompare(const QString& s1, const QStringRef& s2)
1254 { return localeAwareCompare_helper(s1.constData(), s1.length(), s2.constData(), s2.length()); }
1255 inline int QStringRef::localeAwareCompare(const QString &s) const
1256 { return QString::localeAwareCompare_helper(constData(), length(), s.constData(), s.length()); }
1257 inline int QStringRef::localeAwareCompare(const QStringRef &s) const
1258 { return QString::localeAwareCompare_helper(constData(), length(), s.constData(), s.length()); }
1259 inline int QStringRef::localeAwareCompare(const QStringRef &s1, const QString &s2)
1260 { return QString::localeAwareCompare_helper(s1.constData(), s1.length(), s2.constData(), s2.length()); }
1261 inline int QStringRef::localeAwareCompare(const QStringRef &s1, const QStringRef &s2)
1262 { return QString::localeAwareCompare_helper(s1.constData(), s1.length(), s2.constData(), s2.length()); }
1264 inline bool QStringRef::contains(const QString &s, Qt::CaseSensitivity cs) const
1265 { return indexOf(s, 0, cs) != -1; }
1266 inline bool QStringRef::contains(QLatin1String s, Qt::CaseSensitivity cs) const
1267 { return indexOf(s, 0, cs) != -1; }
1268 inline bool QStringRef::contains(QChar c, Qt::CaseSensitivity cs) const
1269 { return indexOf(c, 0, cs) != -1; }
1270 inline bool QStringRef::contains(const QStringRef &s, Qt::CaseSensitivity cs) const
1271 { return indexOf(s, 0, cs) != -1; }
1274 #if QT_DEPRECATED_SINCE(5, 0)
1275 QT_DEPRECATED inline QString escape(const QString &plain) {
1276 return plain.toHtmlEscaped();
1285 #if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER)
1286 #include <QtCore/qstringbuilder.h>