Remove constructors taking implicit string sizes
[profile/ivi/qtbase.git] / src / corelib / tools / qstring.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QSTRING_H
43 #define QSTRING_H
44
45 #include <QtCore/qchar.h>
46 #include <QtCore/qbytearray.h>
47 #include <QtCore/qrefcount.h>
48 #include <QtCore/qnamespace.h>
49
50 #ifndef QT_NO_STL
51 #  include <string>
52 #endif // QT_NO_STL
53
54 #include <stdarg.h>
55
56 #ifdef truncate
57 #error qstring.h must be included before any header file that defines truncate
58 #endif
59
60 QT_BEGIN_HEADER
61
62 QT_BEGIN_NAMESPACE
63
64
65 class QCharRef;
66 class QRegExp;
67 class QStringList;
68 class QTextCodec;
69 class QLatin1String;
70 class QStringRef;
71 template <typename T> class QVector;
72
73 struct QStringData {
74     QtPrivate::RefCount ref;
75     int size;
76     uint alloc : 31;
77     uint capacityReserved : 1;
78     union {
79         qptrdiff offset; // will always work as we add/subtract from a ushort ptr
80         ushort d[sizeof(qptrdiff)/sizeof(ushort)];
81     };
82     inline ushort *data() { return d + sizeof(qptrdiff)/sizeof(ushort) + offset; }
83     inline const ushort *data() const { return d + sizeof(qptrdiff)/sizeof(ushort) + offset; }
84 };
85
86 template<int N> struct QStaticStringData;
87 template<int N> struct QStaticStringDataPtr
88 {
89     const QStaticStringData<N> *ptr;
90 };
91
92 #if defined(Q_COMPILER_UNICODE_STRINGS)
93 template<int N> struct QStaticStringData
94 {
95     QStringData str;
96     char16_t data[N + 1];
97 };
98
99 #define QT_UNICODE_LITERAL_II(str) u"" str
100
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 QStaticStringData
104 {
105     QStringData str;
106     wchar_t data[N + 1];
107 };
108
109 #if defined(Q_CC_MSVC)
110 #    define QT_UNICODE_LITERAL_II(str) L##str
111 #else
112 #    define QT_UNICODE_LITERAL_II(str) L"" str
113 #endif
114
115 #else
116 template<int N> struct QStaticStringData
117 {
118     QStringData str;
119     ushort data[N + 1];
120 };
121 #endif
122
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) ([]() -> QStaticStringDataPtr<sizeof(QT_UNICODE_LITERAL(str))/2 - 1> { \
127         enum { Size = sizeof(QT_UNICODE_LITERAL(str))/2 - 1 }; \
128         static const QStaticStringData<Size> qstring_literal = \
129         { { Q_REFCOUNT_INITIALIZE_STATIC, Size, 0, 0, { 0 } }, QT_UNICODE_LITERAL(str) }; \
130         QStaticStringDataPtr<Size> holder = { &qstring_literal }; \
131     return holder; }())
132
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
137
138 #  define QStringLiteral(str) \
139     __extension__ ({ \
140         enum { Size = sizeof(QT_UNICODE_LITERAL(str))/2 - 1 }; \
141         static const QStaticStringData<Size> qstring_literal = \
142         { { Q_REFCOUNT_INITIALIZE_STATIC, Size, 0, 0, { 0 } }, QT_UNICODE_LITERAL(str) }; \
143         QStaticStringDataPtr<Size> holder = { &qstring_literal }; \
144         holder; })
145 # endif
146 #endif
147
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
151
152 # define QStringLiteral(str) QLatin1String(str)
153 #endif
154
155 class Q_CORE_EXPORT QString
156 {
157 public:
158     typedef QStringData Data;
159
160     inline QString();
161     explicit QString(const QChar *unicode, int size = -1);
162     QString(QChar c);
163     QString(int size, QChar c);
164     inline QString(const QLatin1String &latin1);
165     inline QString(const QString &);
166     inline ~QString();
167     QString &operator=(QChar c);
168     QString &operator=(const QString &);
169     inline QString &operator=(const QLatin1String &);
170 #ifdef Q_COMPILER_RVALUE_REFS
171     inline QString &operator=(QString &&other)
172     { qSwap(d, other.d); return *this; }
173 #endif
174     inline void swap(QString &other) { qSwap(d, other.d); }
175     inline int size() const { return d->size; }
176     inline int count() const { return d->size; }
177     inline int length() const;
178     inline bool isEmpty() const;
179     void resize(int size);
180
181     QString &fill(QChar c, int size = -1);
182     void truncate(int pos);
183     void chop(int n);
184
185     int capacity() const;
186     inline void reserve(int size);
187     inline void squeeze();
188
189     inline const QChar *unicode() const;
190     inline QChar *data();
191     inline const QChar *data() const;
192     inline const QChar *constData() const;
193
194     inline void detach();
195     inline bool isDetached() const;
196     inline bool isSharedWith(const QString &other) const { return d == other.d; }
197     void clear();
198
199     inline const QChar at(int i) const;
200     const QChar operator[](int i) const;
201     QCharRef operator[](int i);
202     const QChar operator[](uint i) const;
203     QCharRef operator[](uint i);
204
205     QString arg(qlonglong a, int fieldwidth=0, int base=10,
206                 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
207     QString arg(qulonglong a, int fieldwidth=0, int base=10,
208                 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
209     QString arg(long a, int fieldwidth=0, int base=10,
210                 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
211     QString arg(ulong a, int fieldwidth=0, int base=10,
212                 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
213     QString arg(int a, int fieldWidth = 0, int base = 10,
214                 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
215     QString arg(uint a, int fieldWidth = 0, int base = 10,
216                 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
217     QString arg(short a, int fieldWidth = 0, int base = 10,
218                 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
219     QString arg(ushort a, int fieldWidth = 0, int base = 10,
220                 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
221     QString arg(double a, int fieldWidth = 0, char fmt = 'g', int prec = -1,
222                 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
223     QString arg(char a, int fieldWidth = 0,
224                 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
225     QString arg(QChar a, int fieldWidth = 0,
226                 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
227     QString arg(const QString &a, int fieldWidth = 0,
228                 QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT;
229     QString arg(const QString &a1, const QString &a2) const Q_REQUIRED_RESULT;
230     QString arg(const QString &a1, const QString &a2, const QString &a3) const Q_REQUIRED_RESULT;
231     QString arg(const QString &a1, const QString &a2, const QString &a3,
232                 const QString &a4) const Q_REQUIRED_RESULT;
233     QString arg(const QString &a1, const QString &a2, const QString &a3,
234                 const QString &a4, const QString &a5) const Q_REQUIRED_RESULT;
235     QString arg(const QString &a1, const QString &a2, const QString &a3,
236                 const QString &a4, const QString &a5, const QString &a6) const Q_REQUIRED_RESULT;
237     QString arg(const QString &a1, const QString &a2, const QString &a3,
238                 const QString &a4, const QString &a5, const QString &a6,
239                 const QString &a7) const Q_REQUIRED_RESULT;
240     QString arg(const QString &a1, const QString &a2, const QString &a3,
241                 const QString &a4, const QString &a5, const QString &a6,
242                 const QString &a7, const QString &a8) const Q_REQUIRED_RESULT;
243     QString arg(const QString &a1, const QString &a2, const QString &a3,
244                 const QString &a4, const QString &a5, const QString &a6,
245                 const QString &a7, const QString &a8, const QString &a9) const Q_REQUIRED_RESULT;
246
247     QString    &vsprintf(const char *format, va_list ap)
248 #if defined(Q_CC_GNU) && !defined(__INSURE__)
249         __attribute__ ((format (printf, 2, 0)))
250 #endif
251         ;
252     QString    &sprintf(const char *format, ...)
253 #if defined(Q_CC_GNU) && !defined(__INSURE__)
254         __attribute__ ((format (printf, 2, 3)))
255 #endif
256         ;
257
258     int indexOf(QChar c, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
259     int indexOf(const QString &s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
260     int indexOf(const QLatin1String &s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
261     int indexOf(const QStringRef &s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
262     int lastIndexOf(QChar c, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
263     int lastIndexOf(const QString &s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
264     int lastIndexOf(const QLatin1String &s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
265     int lastIndexOf(const QStringRef &s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
266
267     inline bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
268     inline bool contains(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
269     inline bool contains(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
270     int count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
271     int count(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
272     int count(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
273
274 #ifndef QT_NO_REGEXP
275     int indexOf(const QRegExp &, int from = 0) const;
276     int lastIndexOf(const QRegExp &, int from = -1) const;
277     inline bool contains(const QRegExp &rx) const { return indexOf(rx) != -1; }
278     int count(const QRegExp &) const;
279
280     int indexOf(QRegExp &, int from = 0) const;
281     int lastIndexOf(QRegExp &, int from = -1) const;
282     inline bool contains(QRegExp &rx) const { return indexOf(rx) != -1; }
283 #endif
284
285     enum SectionFlag {
286         SectionDefault             = 0x00,
287         SectionSkipEmpty           = 0x01,
288         SectionIncludeLeadingSep   = 0x02,
289         SectionIncludeTrailingSep  = 0x04,
290         SectionCaseInsensitiveSeps = 0x08
291     };
292     Q_DECLARE_FLAGS(SectionFlags, SectionFlag)
293
294     QString section(QChar sep, int start, int end = -1, SectionFlags flags = SectionDefault) const;
295     QString section(const QString &in_sep, int start, int end = -1, SectionFlags flags = SectionDefault) const;
296 #ifndef QT_NO_REGEXP
297     QString section(const QRegExp &reg, int start, int end = -1, SectionFlags flags = SectionDefault) const;
298 #endif
299
300     QString left(int n) const Q_REQUIRED_RESULT;
301     QString right(int n) const Q_REQUIRED_RESULT;
302     QString mid(int position, int n = -1) const Q_REQUIRED_RESULT;
303     QStringRef leftRef(int n) const Q_REQUIRED_RESULT;
304     QStringRef rightRef(int n) const Q_REQUIRED_RESULT;
305     QStringRef midRef(int position, int n = -1) const Q_REQUIRED_RESULT;
306
307     bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
308     bool startsWith(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
309     bool startsWith(const QLatin1String &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
310     bool startsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
311     bool endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
312     bool endsWith(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
313     bool endsWith(const QLatin1String &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
314     bool endsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
315
316     QString leftJustified(int width, QChar fill = QLatin1Char(' '), bool trunc = false) const Q_REQUIRED_RESULT;
317     QString rightJustified(int width, QChar fill = QLatin1Char(' '), bool trunc = false) const Q_REQUIRED_RESULT;
318
319     QString toLower() const Q_REQUIRED_RESULT;
320     QString toUpper() const Q_REQUIRED_RESULT;
321     QString toCaseFolded() const Q_REQUIRED_RESULT;
322
323     QString trimmed() const Q_REQUIRED_RESULT;
324     QString simplified() const Q_REQUIRED_RESULT;
325     QString toHtmlEscaped() const Q_REQUIRED_RESULT;
326
327     QString &insert(int i, QChar c);
328     QString &insert(int i, const QChar *uc, int len);
329     inline QString &insert(int i, const QString &s) { return insert(i, s.constData(), s.length()); }
330     QString &insert(int i, const QLatin1String &s);
331     QString &append(QChar c);
332     QString &append(const QString &s);
333     QString &append(const QStringRef &s);
334     QString &append(const QLatin1String &s);
335     inline QString &prepend(QChar c) { return insert(0, c); }
336     inline QString &prepend(const QString &s) { return insert(0, s); }
337     inline QString &prepend(const QLatin1String &s) { return insert(0, s); }
338
339     inline QString &operator+=(QChar c) {
340         if (d->ref.isShared() || d->size + 1 > int(d->alloc))
341             realloc(grow(d->size + 1));
342         d->data()[d->size++] = c.unicode();
343         d->data()[d->size] = '\0';
344         return *this;
345     }
346
347     inline QString &operator+=(QChar::SpecialCharacter c) { return append(QChar(c)); }
348     inline QString &operator+=(const QString &s) { return append(s); }
349     inline QString &operator+=(const QStringRef &s) { return append(s); }
350     inline QString &operator+=(const QLatin1String &s) { return append(s); }
351
352     QString &remove(int i, int len);
353     QString &remove(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive);
354     QString &remove(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive);
355     QString &replace(int i, int len, QChar after);
356     QString &replace(int i, int len, const QChar *s, int slen);
357     QString &replace(int i, int len, const QString &after);
358     QString &replace(QChar before, QChar after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
359     QString &replace(const QChar *before, int blen, const QChar *after, int alen, Qt::CaseSensitivity cs = Qt::CaseSensitive);
360     QString &replace(const QLatin1String &before, const QLatin1String &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
361     QString &replace(const QLatin1String &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
362     QString &replace(const QString &before, const QLatin1String &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
363     QString &replace(const QString &before, const QString &after,
364                      Qt::CaseSensitivity cs = Qt::CaseSensitive);
365     QString &replace(QChar c, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
366     QString &replace(QChar c, const QLatin1String &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
367 #ifndef QT_NO_REGEXP
368     QString &replace(const QRegExp &rx, const QString &after);
369     inline QString &remove(const QRegExp &rx)
370     { return replace(rx, QString()); }
371 #endif
372
373     enum SplitBehavior { KeepEmptyParts, SkipEmptyParts };
374
375     QStringList split(const QString &sep, SplitBehavior behavior = KeepEmptyParts,
376                       Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_REQUIRED_RESULT;
377     QStringList split(QChar sep, SplitBehavior behavior = KeepEmptyParts,
378                       Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_REQUIRED_RESULT;
379 #ifndef QT_NO_REGEXP
380     QStringList split(const QRegExp &sep, SplitBehavior behavior = KeepEmptyParts) const Q_REQUIRED_RESULT;
381 #endif
382
383     enum NormalizationForm {
384         NormalizationForm_D,
385         NormalizationForm_C,
386         NormalizationForm_KD,
387         NormalizationForm_KC
388     };
389     QString normalized(NormalizationForm mode) const Q_REQUIRED_RESULT;
390     QString normalized(NormalizationForm mode, QChar::UnicodeVersion version) const Q_REQUIRED_RESULT;
391
392     QString repeated(int times) const;
393
394     const ushort *utf16() const;
395
396     QByteArray toAscii() const Q_REQUIRED_RESULT;
397     QByteArray toLatin1() const Q_REQUIRED_RESULT;
398     QByteArray toUtf8() const Q_REQUIRED_RESULT;
399     QByteArray toLocal8Bit() const Q_REQUIRED_RESULT;
400     QVector<uint> toUcs4() const Q_REQUIRED_RESULT;
401
402     // note - this are all inline so we can benefit from strlen() compile time optimizations
403     static inline QString fromAscii(const char *str, int size = -1)
404     {
405         return QString(fromAscii_helper(str, (str && size == -1) ? int(strlen(str)) : size), 0);
406     }
407     static inline QString fromLatin1(const char *str, int size = -1)
408     {
409         return QString(fromLatin1_helper(str, (str && size == -1) ? int(strlen(str)) : size), 0);
410     }
411     static inline QString fromUtf8(const char *str, int size = -1)
412     {
413         return fromUtf8_helper(str, (str && size == -1) ? int(strlen(str)) : size);
414     }
415     static inline QString fromLocal8Bit(const char *str, int size = -1)
416     {
417         return fromLocal8Bit_helper(str, (str && size == -1) ? int(strlen(str)) : size);
418     }
419     static QString fromUtf16(const ushort *, int size = -1);
420     static QString fromUcs4(const uint *, int size = -1);
421     static QString fromRawData(const QChar *, int size);
422
423     inline int toWCharArray(wchar_t *array) const;
424     static inline QString fromWCharArray(const wchar_t *string, int size = -1) Q_REQUIRED_RESULT;
425
426     QString &setRawData(const QChar *unicode, int size);
427     QString &setUnicode(const QChar *unicode, int size);
428     inline QString &setUtf16(const ushort *utf16, int size);
429
430     int compare(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
431     int compare(const QLatin1String &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
432
433     static inline int compare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
434     { return s1.compare(s2, cs); }
435
436     static inline int compare(const QString& s1, const QLatin1String &s2,
437                               Qt::CaseSensitivity cs = Qt::CaseSensitive)
438     { return s1.compare(s2, cs); }
439     static inline int compare(const QLatin1String& s1, const QString &s2,
440                               Qt::CaseSensitivity cs = Qt::CaseSensitive)
441     { return -s2.compare(s1, cs); }
442
443     int compare(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
444     static int compare(const QString &s1, const QStringRef &s2,
445                        Qt::CaseSensitivity = Qt::CaseSensitive);
446
447     int localeAwareCompare(const QString& s) const;
448     static int localeAwareCompare(const QString& s1, const QString& s2)
449     { return s1.localeAwareCompare(s2); }
450
451     int localeAwareCompare(const QStringRef &s) const;
452     static int localeAwareCompare(const QString& s1, const QStringRef& s2);
453
454     short  toShort(bool *ok=0, int base=10) const;
455     ushort toUShort(bool *ok=0, int base=10) const;
456     int toInt(bool *ok=0, int base=10) const;
457     uint toUInt(bool *ok=0, int base=10) const;
458     long toLong(bool *ok=0, int base=10) const;
459     ulong toULong(bool *ok=0, int base=10) const;
460     qlonglong toLongLong(bool *ok=0, int base=10) const;
461     qulonglong toULongLong(bool *ok=0, int base=10) const;
462     float toFloat(bool *ok=0) const;
463     double toDouble(bool *ok=0) const;
464
465     QString &setNum(short, int base=10);
466     QString &setNum(ushort, int base=10);
467     QString &setNum(int, int base=10);
468     QString &setNum(uint, int base=10);
469     QString &setNum(long, int base=10);
470     QString &setNum(ulong, int base=10);
471     QString &setNum(qlonglong, int base=10);
472     QString &setNum(qulonglong, int base=10);
473     QString &setNum(float, char f='g', int prec=6);
474     QString &setNum(double, char f='g', int prec=6);
475
476     static QString number(int, int base=10);
477     static QString number(uint, int base=10);
478     static QString number(long, int base=10);
479     static QString number(ulong, int base=10);
480     static QString number(qlonglong, int base=10);
481     static QString number(qulonglong, int base=10);
482     static QString number(double, char f='g', int prec=6);
483
484     bool operator==(const QString &s) const;
485     bool operator<(const QString &s) const;
486     inline bool operator>(const QString &s) const { return s < *this; }
487     inline bool operator!=(const QString &s) const { return !operator==(s); }
488     inline bool operator<=(const QString &s) const { return !operator>(s); }
489     inline bool operator>=(const QString &s) const { return !operator<(s); }
490
491     bool operator==(const QLatin1String &s) const;
492     bool operator<(const QLatin1String &s) const;
493     bool operator>(const QLatin1String &s) const;
494     inline bool operator!=(const QLatin1String &s) const { return !operator==(s); }
495     inline bool operator<=(const QLatin1String &s) const { return !operator>(s); }
496     inline bool operator>=(const QLatin1String &s) const { return !operator<(s); }
497
498     // ASCII compatibility
499 #ifndef QT_NO_CAST_FROM_ASCII
500     inline QT_ASCII_CAST_WARN_CONSTRUCTOR QString(const char *ch)
501         : d(fromAscii_helper(ch, ch ? int(strlen(ch)) : -1))
502     {}
503     inline QT_ASCII_CAST_WARN_CONSTRUCTOR QString(const QByteArray &a)
504         : d(fromAscii_helper(a.constData(), qstrnlen(a.constData(), a.size())))
505     {}
506     inline QT_ASCII_CAST_WARN QString &operator=(const char *ch)
507     { return (*this = fromAscii(ch, ch ? int(strlen(ch)) : -1)); }
508     inline QT_ASCII_CAST_WARN QString &operator=(const QByteArray &a)
509     { return (*this = fromAscii(a.constData(), qstrnlen(a.constData(), a.size()))); }
510     inline QT_ASCII_CAST_WARN QString &operator=(char c)
511     { return (*this = QChar::fromAscii(c)); }
512
513     // these are needed, so it compiles with STL support enabled
514     inline QT_ASCII_CAST_WARN QString &prepend(const char *s)
515     { return prepend(QString::fromAscii(s, s ? int(strlen(s)) : -1)); }
516     inline QT_ASCII_CAST_WARN QString &prepend(const QByteArray &s)
517     { return prepend(QString::fromAscii(s.constData(), qstrnlen(s.constData(), s.size()))); }
518     inline QT_ASCII_CAST_WARN QString &append(const char *s)
519     { return append(QString::fromAscii(s, s ? int(strlen(s)) : -1)); }
520     inline QT_ASCII_CAST_WARN QString &append(const QByteArray &s)
521     { return append(QString::fromAscii(s.constData(), qstrnlen(s.constData(), s.size()))); }
522     inline QT_ASCII_CAST_WARN QString &operator+=(const char *s)
523     { return append(QString::fromAscii(s, s ? int(strlen(s)) : -1)); }
524     inline QT_ASCII_CAST_WARN QString &operator+=(const QByteArray &s)
525     { return append(QString::fromAscii(s.constData(), qstrnlen(s.constData(), s.size()))); }
526     inline QT_ASCII_CAST_WARN QString &operator+=(char c)
527     { return append(QChar::fromAscii(c)); }
528
529     inline QT_ASCII_CAST_WARN bool operator==(const char *s) const;
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 *s2) 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
536     inline QT_ASCII_CAST_WARN bool operator==(const QByteArray &s) 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     { return *this < QString::fromAscii(s.constData(), s.size()); }
540     inline QT_ASCII_CAST_WARN bool operator>(const QByteArray &s) const
541     { return *this > QString::fromAscii(s.constData(), s.size()); }
542     inline QT_ASCII_CAST_WARN bool operator<=(const QByteArray &s) const
543     { return *this <= QString::fromAscii(s.constData(), s.size()); }
544     inline QT_ASCII_CAST_WARN bool operator>=(const QByteArray &s) const
545     { return *this >= QString::fromAscii(s.constData(), s.size()); }
546 #endif
547
548     typedef QChar *iterator;
549     typedef const QChar *const_iterator;
550     typedef iterator Iterator;
551     typedef const_iterator ConstIterator;
552     iterator begin();
553     const_iterator begin() const;
554     const_iterator constBegin() const;
555     iterator end();
556     const_iterator end() const;
557     const_iterator constEnd() const;
558
559     // STL compatibility
560     typedef const QChar & const_reference;
561     typedef QChar & reference;
562     typedef QChar value_type;
563     inline void push_back(QChar c) { append(c); }
564     inline void push_back(const QString &s) { append(s); }
565     inline void push_front(QChar c) { prepend(c); }
566     inline void push_front(const QString &s) { prepend(s); }
567
568 #ifndef QT_NO_STL
569     static inline QString fromStdString(const std::string &s);
570     inline std::string toStdString() const;
571     static inline QString fromStdWString(const std::wstring &s);
572     inline std::wstring toStdWString() const;
573 #endif
574
575     // compatibility
576     struct Null { };
577     static const Null null;
578     inline QString(const Null &): d(const_cast<Data *>(&shared_null.str)) {}
579     inline QString &operator=(const Null &) { *this = QString(); return *this; }
580     inline bool isNull() const { return d == &shared_null.str; }
581
582
583     bool isSimpleText() const;
584     bool isRightToLeft() const;
585
586     QString(int size, Qt::Initialization);
587     template <int n>
588     inline QString(const QStaticStringData<n> &dd) : d(const_cast<QStringData *>(&dd.str)) {}
589     template <int N>
590     Q_DECL_CONSTEXPR inline QString(QStaticStringDataPtr<N> dd) : d(const_cast<QStringData *>(&dd.ptr->str)) {}
591
592 private:
593 #if defined(QT_NO_CAST_FROM_ASCII) && !defined(Q_NO_DECLARED_NOT_DEFINED)
594     QString &operator+=(const char *s);
595     QString &operator+=(const QByteArray &s);
596     QString(const char *ch);
597     QString(const QByteArray &a);
598     QString &operator=(const char  *ch);
599     QString &operator=(const QByteArray &a);
600 #endif
601
602     static const QStaticStringData<1> shared_null;
603     static const QStaticStringData<1> shared_empty;
604     Data *d;
605     inline QString(Data *dd, int /*dummy*/) : d(dd) {}
606
607 #ifndef QT_NO_TEXTCODEC
608     static QTextCodec *codecForCStrings;
609 #endif
610     static int grow(int);
611     static void free(Data *);
612     void realloc();
613     void realloc(int alloc);
614     void expand(int i);
615     void updateProperties() const;
616     QString multiArg(int numArgs, const QString **args) const;
617     static int compare_helper(const QChar *data1, int length1,
618                               const QChar *data2, int length2,
619                               Qt::CaseSensitivity cs = Qt::CaseSensitive);
620     static int compare_helper(const QChar *data1, int length1,
621                               QLatin1String s2,
622                               Qt::CaseSensitivity cs = Qt::CaseSensitive);
623     static int localeAwareCompare_helper(const QChar *data1, int length1,
624                                          const QChar *data2, int length2);
625     static Data *fromLatin1_helper(const char *str, int size = -1);
626     static Data *fromAscii_helper(const char *str, int size = -1);
627     static QString fromUtf8_helper(const char *str, int size);
628     static QString fromLocal8Bit_helper(const char *, int size);
629     static int toUcs4_helper(const ushort *uc, int length, uint *out);
630     void replace_helper(uint *indices, int nIndices, int blen, const QChar *after, int alen);
631     friend class QCharRef;
632     friend class QTextCodec;
633     friend class QStringRef;
634     friend struct QAbstractConcatenable;
635     friend inline bool qStringComparisonHelper(const QString &s1, const char *s2);
636     friend inline bool qStringComparisonHelper(const QStringRef &s1, const char *s2);
637 public:
638     typedef Data * DataPtr;
639     inline DataPtr &data_ptr() { return d; }
640 };
641
642
643 class QLatin1String
644 {
645 public:
646     Q_DECL_CONSTEXPR inline explicit QLatin1String(const char *s) : m_size(s ? int(strlen(s)) : 0), m_data(s) {}
647     Q_DECL_CONSTEXPR inline explicit QLatin1String(const char *s, int sz) : m_size(sz), m_data(s) {}
648
649     inline const char *latin1() const { return m_data; }
650     inline int size() const { return m_size; }
651     inline const char *data() const { return m_data; }
652
653     inline bool operator==(const QString &s) const
654     { return s == *this; }
655     inline bool operator!=(const QString &s) const
656     { return s != *this; }
657     inline bool operator>(const QString &s) const
658     { return s < *this; }
659     inline bool operator<(const QString &s) const
660     { return s > *this; }
661     inline bool operator>=(const QString &s) const
662     { return s <= *this; }
663     inline bool operator<=(const QString &s) const
664     { return s >= *this; }
665
666     inline QT_ASCII_CAST_WARN bool operator==(const char *s) const
667         { return QString::fromAscii(s, s ? int(strlen(s)) : -1) == *this; }
668     inline QT_ASCII_CAST_WARN bool operator!=(const char *s) const
669         { return QString::fromAscii(s, s ? int(strlen(s)) : -1) != *this; }
670     inline QT_ASCII_CAST_WARN bool operator<(const char *s) const
671         { return QString::fromAscii(s, s ? int(strlen(s)) : -1) > *this; }
672     inline QT_ASCII_CAST_WARN bool operator>(const char *s) const
673         { return QString::fromAscii(s, s ? int(strlen(s)) : -1) < *this; }
674     inline QT_ASCII_CAST_WARN bool operator<=(const char *s) const
675         { return QString::fromAscii(s, s ? int(strlen(s)) : -1) >= *this; }
676     inline QT_ASCII_CAST_WARN bool operator>=(const char *s) const
677         { return QString::fromAscii(s, s ? int(strlen(s)) : -1) <= *this; }
678 private:
679     int m_size;
680     const char *m_data;
681 };
682
683 // Qt 4.x compatibility
684 typedef QLatin1String QLatin1Literal;
685
686
687 inline QString::QString(const QLatin1String &aLatin1) : d(fromLatin1_helper(aLatin1.latin1(), aLatin1.size()))
688 { }
689 inline int QString::length() const
690 { return d->size; }
691 inline const QChar QString::at(int i) const
692 { Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; }
693 inline const QChar QString::operator[](int i) const
694 { Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; }
695 inline const QChar QString::operator[](uint i) const
696 { Q_ASSERT(i < uint(size())); return d->data()[i]; }
697 inline bool QString::isEmpty() const
698 { return d->size == 0; }
699 inline const QChar *QString::unicode() const
700 { return reinterpret_cast<const QChar*>(d->data()); }
701 inline const QChar *QString::data() const
702 { return reinterpret_cast<const QChar*>(d->data()); }
703 inline QChar *QString::data()
704 { detach(); return reinterpret_cast<QChar*>(d->data()); }
705 inline const QChar *QString::constData() const
706 { return reinterpret_cast<const QChar*>(d->data()); }
707 inline void QString::detach()
708 { if (d->ref.isShared() || d->offset) realloc(); }
709 inline bool QString::isDetached() const
710 { return !d->ref.isShared(); }
711 inline QString &QString::operator=(const QLatin1String &s)
712 {
713     *this = fromLatin1(s.latin1(), s.size());
714     return *this;
715 }
716 inline void QString::clear()
717 { if (!isNull()) *this = QString(); }
718 inline QString::QString(const QString &other) : d(other.d)
719 { Q_ASSERT(&other != this); d->ref.ref(); }
720 inline int QString::capacity() const
721 { return d->alloc; }
722 inline QString &QString::setNum(short n, int base)
723 { return setNum(qlonglong(n), base); }
724 inline QString &QString::setNum(ushort n, int base)
725 { return setNum(qulonglong(n), base); }
726 inline QString &QString::setNum(int n, int base)
727 { return setNum(qlonglong(n), base); }
728 inline QString &QString::setNum(uint n, int base)
729 { return setNum(qulonglong(n), base); }
730 inline QString &QString::setNum(long n, int base)
731 { return setNum(qlonglong(n), base); }
732 inline QString &QString::setNum(ulong n, int base)
733 { return setNum(qulonglong(n), base); }
734 inline QString &QString::setNum(float n, char f, int prec)
735 { return setNum(double(n),f,prec); }
736 inline QString QString::arg(int a, int fieldWidth, int base, QChar fillChar) const
737 { return arg(qlonglong(a), fieldWidth, base, fillChar); }
738 inline QString QString::arg(uint a, int fieldWidth, int base, QChar fillChar) const
739 { return arg(qulonglong(a), fieldWidth, base, fillChar); }
740 inline QString QString::arg(long a, int fieldWidth, int base, QChar fillChar) const
741 { return arg(qlonglong(a), fieldWidth, base, fillChar); }
742 inline QString QString::arg(ulong a, int fieldWidth, int base, QChar fillChar) const
743 { return arg(qulonglong(a), fieldWidth, base, fillChar); }
744 inline QString QString::arg(short a, int fieldWidth, int base, QChar fillChar) const
745 { return arg(qlonglong(a), fieldWidth, base, fillChar); }
746 inline QString QString::arg(ushort a, int fieldWidth, int base, QChar fillChar) const
747 { return arg(qulonglong(a), fieldWidth, base, fillChar); }
748 inline QString QString::arg(const QString &a1, const QString &a2) const
749 { const QString *args[2] = { &a1, &a2 }; return multiArg(2, args); }
750 inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3) const
751 { const QString *args[3] = { &a1, &a2, &a3 }; return multiArg(3, args); }
752 inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
753                             const QString &a4) const
754 { const QString *args[4] = { &a1, &a2, &a3, &a4 }; return multiArg(4, args); }
755 inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
756                             const QString &a4, const QString &a5) const
757 { const QString *args[5] = { &a1, &a2, &a3, &a4, &a5 }; return multiArg(5, args); }
758 inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
759                             const QString &a4, const QString &a5, const QString &a6) const
760 { const QString *args[6] = { &a1, &a2, &a3, &a4, &a5, &a6 }; return multiArg(6, args); }
761 inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
762                             const QString &a4, const QString &a5, const QString &a6,
763                             const QString &a7) const
764 { const QString *args[7] = { &a1, &a2, &a3, &a4, &a5, &a6,  &a7 }; return multiArg(7, args); }
765 inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
766                             const QString &a4, const QString &a5, const QString &a6,
767                             const QString &a7, const QString &a8) const
768 { const QString *args[8] = { &a1, &a2, &a3, &a4, &a5, &a6,  &a7, &a8 }; return multiArg(8, args); }
769 inline QString QString::arg(const QString &a1, const QString &a2, const QString &a3,
770                             const QString &a4, const QString &a5, const QString &a6,
771                             const QString &a7, const QString &a8, const QString &a9) const
772 { const QString *args[9] = { &a1, &a2, &a3, &a4, &a5, &a6,  &a7, &a8, &a9 }; return multiArg(9, args); }
773
774 inline QString QString::section(QChar asep, int astart, int aend, SectionFlags aflags) const
775 { return section(QString(asep), astart, aend, aflags); }
776
777 inline int QString::toWCharArray(wchar_t *array) const
778 {
779     if (sizeof(wchar_t) == sizeof(QChar)) {
780         qMemCopy(array, d->data(), sizeof(QChar) * size());
781         return size();
782     }
783     return toUcs4_helper(d->data(), size(), reinterpret_cast<uint *>(array));
784 }
785 inline QString QString::fromWCharArray(const wchar_t *string, int size)
786 {
787     return sizeof(wchar_t) == sizeof(QChar) ? fromUtf16(reinterpret_cast<const ushort *>(string), size)
788                                             : fromUcs4(reinterpret_cast<const uint *>(string), size);
789 }
790
791
792 class Q_CORE_EXPORT QCharRef {
793     QString &s;
794     int i;
795     inline QCharRef(QString &str, int idx)
796         : s(str),i(idx) {}
797     friend class QString;
798 public:
799
800     // most QChar operations repeated here
801
802     // all this is not documented: We just say "like QChar" and let it be.
803     inline operator QChar() const
804     { return i < s.d->size ? s.d->data()[i] : 0; }
805     inline QCharRef &operator=(QChar c)
806     { if (i >= s.d->size) s.expand(i); else s.detach();
807       s.d->data()[i] = c.unicode(); return *this; }
808
809     // An operator= for each QChar cast constructors
810 #ifndef QT_NO_CAST_FROM_ASCII
811     inline QT_ASCII_CAST_WARN QCharRef &operator=(char c)
812     { return operator=(QChar::fromAscii(c)); }
813     inline QT_ASCII_CAST_WARN QCharRef &operator=(uchar c)
814     { return operator=(QChar::fromAscii(c)); }
815 #endif
816     inline QCharRef &operator=(const QCharRef &c) { return operator=(QChar(c)); }
817     inline QCharRef &operator=(ushort rc) { return operator=(QChar(rc)); }
818     inline QCharRef &operator=(short rc) { return operator=(QChar(rc)); }
819     inline QCharRef &operator=(uint rc) { return operator=(QChar(rc)); }
820     inline QCharRef &operator=(int rc) { return operator=(QChar(rc)); }
821
822     // each function...
823     inline bool isNull() const { return QChar(*this).isNull(); }
824     inline bool isPrint() const { return QChar(*this).isPrint(); }
825     inline bool isPunct() const { return QChar(*this).isPunct(); }
826     inline bool isSpace() const { return QChar(*this).isSpace(); }
827     inline bool isMark() const { return QChar(*this).isMark(); }
828     inline bool isLetter() const { return QChar(*this).isLetter(); }
829     inline bool isNumber() const { return QChar(*this).isNumber(); }
830     inline bool isLetterOrNumber() { return QChar(*this).isLetterOrNumber(); }
831     inline bool isDigit() const { return QChar(*this).isDigit(); }
832     inline bool isLower() const { return QChar(*this).isLower(); }
833     inline bool isUpper() const { return QChar(*this).isUpper(); }
834     inline bool isTitleCase() const { return QChar(*this).isTitleCase(); }
835
836     inline int digitValue() const { return QChar(*this).digitValue(); }
837     QChar toLower() const { return QChar(*this).toLower(); }
838     QChar toUpper() const { return QChar(*this).toUpper(); }
839     QChar toTitleCase () const { return QChar(*this).toTitleCase(); }
840
841     QChar::Category category() const { return QChar(*this).category(); }
842     QChar::Direction direction() const { return QChar(*this).direction(); }
843     QChar::Joining joining() const { return QChar(*this).joining(); }
844     bool hasMirrored() const { return QChar(*this).hasMirrored(); }
845     QChar mirroredChar() const { return QChar(*this).mirroredChar(); }
846     QString decomposition() const { return QChar(*this).decomposition(); }
847     QChar::Decomposition decompositionTag() const { return QChar(*this).decompositionTag(); }
848     uchar combiningClass() const { return QChar(*this).combiningClass(); }
849
850     QChar::UnicodeVersion unicodeVersion() const { return QChar(*this).unicodeVersion(); }
851
852     inline uchar cell() const { return QChar(*this).cell(); }
853     inline uchar row() const { return QChar(*this).row(); }
854     inline void setCell(uchar cell);
855     inline void setRow(uchar row);
856
857     char toAscii() const { return QChar(*this).toAscii(); }
858     char toLatin1() const { return QChar(*this).toLatin1(); }
859     ushort unicode() const { return QChar(*this).unicode(); }
860     ushort& unicode() { return s.data()[i].unicode(); }
861
862 };
863
864 inline void QCharRef::setRow(uchar arow) { QChar(*this).setRow(arow); }
865 inline void QCharRef::setCell(uchar acell) { QChar(*this).setCell(acell); }
866
867
868 inline QString::QString() : d(const_cast<Data *>(&shared_null.str)) {}
869 inline QString::~QString() { if (!d->ref.deref()) free(d); }
870
871 inline void QString::reserve(int asize)
872 {
873     if (d->ref.isShared() || asize > int(d->alloc))
874         realloc(asize);
875
876     if (!d->capacityReserved) {
877         // cannot set unconditionally, since d could be the shared_null/shared_empty (which is const)
878         d->capacityReserved = true;
879     }
880 }
881
882 inline void QString::squeeze()
883 {
884     if (d->ref.isShared() || d->size < int(d->alloc))
885         realloc();
886
887     if (d->capacityReserved) {
888         // cannot set unconditionally, since d could be shared_null or
889         // otherwise static.
890         d->capacityReserved = false;
891     }
892 }
893
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; }
918
919
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(); }
926
927 #ifndef QT_NO_CAST_FROM_ASCII
928 inline bool qStringComparisonHelper(const QString &s1, const char *s2)
929 {
930 #  ifndef QT_NO_TEXTCODEC
931     if (QString::codecForCStrings) return (s1 == QString::fromAscii(s2, s2 ? int(strlen(s2)) : -1));
932 #  endif
933     return (s1 == QLatin1String(s2));
934 }
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); }
947
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); }
960
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); }
973
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()); }
990
991
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()); }
996
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
1010
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
1029
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
1052
1053 #ifndef QT_NO_STL
1054 inline std::string QString::toStdString() const
1055 { const QByteArray asc = toAscii(); return std::string(asc.constData(), asc.length()); }
1056
1057 inline QString QString::fromStdString(const std::string &s)
1058 { return fromAscii(s.data(), int(s.size())); }
1059
1060 inline std::wstring QString::toStdWString() const
1061 {
1062     std::wstring str;
1063     str.resize(length());
1064
1065 #if defined(_MSC_VER) && _MSC_VER >= 1400
1066     // VS2005 crashes if the string is empty
1067     if (!length())
1068         return str;
1069 #endif
1070
1071     str.resize(toWCharArray(&(*str.begin())));
1072     return str;
1073 }
1074 inline QString QString::fromStdWString(const std::wstring &s)
1075 { return fromWCharArray(s.data(), int(s.size())); }
1076 #endif
1077
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 &);
1081 #endif
1082
1083 Q_DECLARE_TYPEINFO(QString, Q_MOVABLE_TYPE);
1084 Q_DECLARE_SHARED(QString)
1085 Q_DECLARE_OPERATORS_FOR_FLAGS(QString::SectionFlags)
1086
1087
1088 class Q_CORE_EXPORT QStringRef {
1089     const QString *m_string;
1090     int m_position;
1091     int m_size;
1092 public:
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)
1098         {}
1099
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; }
1106
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;
1110     }
1111
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;
1120
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;
1125
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;
1129
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;
1134
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;
1139
1140     inline QStringRef &operator=(const QString *string);
1141
1142     inline const QChar *unicode() const {
1143         if (!m_string)
1144             return reinterpret_cast<const QChar *>(QString::shared_null.str.data());
1145         return m_string->unicode() + m_position;
1146     }
1147     inline const QChar *data() const { return unicode(); }
1148     inline const QChar *constData() const {  return unicode(); }
1149
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;
1155
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(); }
1160
1161     QStringRef appendTo(QString *string) const;
1162
1163     inline const QChar at(int i) const
1164         { Q_ASSERT(i >= 0 && i < size()); return m_string->at(i + m_position); }
1165
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);
1175
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);
1180 };
1181
1182 inline QStringRef &QStringRef::operator=(const QString *aString)
1183 { m_string = aString; m_position = 0; m_size = aString?aString->size():0; return *this; }
1184
1185 inline QStringRef::QStringRef(const QString *aString, int aPosition, int aSize)
1186         :m_string(aString), m_position(aPosition), m_size(aSize){}
1187
1188 inline QStringRef::QStringRef(const QString *aString)
1189     :m_string(aString), m_position(0), m_size(aString?aString->size() : 0){}
1190
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; }
1208
1209 Q_CORE_EXPORT bool operator<(const QStringRef &s1,const QStringRef &s2);
1210 inline bool operator>(const QStringRef &s1, const QStringRef &s2)
1211 { return s2 < s1; }
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); }
1216
1217 inline bool qStringComparisonHelper(const QStringRef &s1, const char *s2)
1218 {
1219 #  ifndef QT_NO_TEXTCODEC
1220     if (QString::codecForCStrings) return (s1 == QString::fromAscii(s2, s2 ? int(strlen(s2)) : -1));
1221 #  endif
1222     return (s1 == QLatin1String(s2));
1223 }
1224
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); }
1233
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); }
1250
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()); }
1263
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; }
1272
1273 namespace Qt {
1274 #if QT_DEPRECATED_SINCE(5, 0)
1275 QT_DEPRECATED inline QString escape(const QString &plain) {
1276     return plain.toHtmlEscaped();
1277 }
1278 #endif
1279 }
1280
1281 QT_END_NAMESPACE
1282
1283 QT_END_HEADER
1284
1285 #if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER)
1286 #include <QtCore/qstringbuilder.h>
1287 #endif
1288
1289 #endif // QSTRING_H