4f8d0e5614c47c29445c711fea21202405d02f5e
[profile/ivi/qtbase.git] / src / corelib / tools / qstring.cpp
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 #include "qstringlist.h"
43 #include "qregexp.h"
44 #include "qregularexpression.h"
45 #include "qunicodetables_p.h"
46 #ifndef QT_NO_TEXTCODEC
47 #include <qtextcodec.h>
48 #endif
49 #include <private/qutfcodec_p.h>
50 #include "qsimd_p.h"
51 #include <qnumeric.h>
52 #include <qdatastream.h>
53 #include <qlist.h>
54 #include "qlocale.h"
55 #include "qlocale_p.h"
56 #include "qstringmatcher.h"
57 #include "qvarlengtharray.h"
58 #include "qtools_p.h"
59 #include "qhash.h"
60 #include "qdebug.h"
61 #include "qendian.h"
62
63 #ifdef Q_OS_MAC
64 #include <private/qcore_mac_p.h>
65 #endif
66
67 #include <private/qfunctions_p.h>
68
69 #include <limits.h>
70 #include <string.h>
71 #include <stdlib.h>
72 #include <stdio.h>
73 #include <stdarg.h>
74
75 #include "qchar.cpp"
76 #include "qstringmatcher.cpp"
77
78 #ifdef Q_OS_WIN
79 #  include <qt_windows.h>
80 #  ifdef Q_OS_WINCE
81 #    include <winnls.h>
82 #  endif
83 #endif
84
85 #ifdef truncate
86 #  undef truncate
87 #endif
88
89 #ifndef LLONG_MAX
90 #define LLONG_MAX qint64_C(9223372036854775807)
91 #endif
92 #ifndef LLONG_MIN
93 #define LLONG_MIN (-LLONG_MAX - qint64_C(1))
94 #endif
95 #ifndef ULLONG_MAX
96 #define ULLONG_MAX quint64_C(18446744073709551615)
97 #endif
98
99 #define IS_RAW_DATA(d) ((d)->offset != sizeof(QStringData))
100
101 QT_BEGIN_NAMESPACE
102
103 // internal
104 int qFindString(const QChar *haystack, int haystackLen, int from,
105     const QChar *needle, int needleLen, Qt::CaseSensitivity cs);
106 int qFindStringBoyerMoore(const QChar *haystack, int haystackLen, int from,
107     const QChar *needle, int needleLen, Qt::CaseSensitivity cs);
108 static inline int qt_last_index_of(const QChar *haystack, int haystackLen, QChar needle,
109                                    int from, Qt::CaseSensitivity cs);
110 static inline int qt_string_count(const QChar *haystack, int haystackLen,
111                                   const QChar *needle, int needleLen,
112                                   Qt::CaseSensitivity cs);
113 static inline int qt_string_count(const QChar *haystack, int haystackLen,
114                                   QChar needle, Qt::CaseSensitivity cs);
115 static inline int qt_find_latin1_string(const QChar *hay, int size, QLatin1String needle,
116                                         int from, Qt::CaseSensitivity cs);
117 static inline bool qt_starts_with(const QChar *haystack, int haystackLen,
118                                   const QChar *needle, int needleLen, Qt::CaseSensitivity cs);
119 static inline bool qt_starts_with(const QChar *haystack, int haystackLen,
120                                   QLatin1String needle, Qt::CaseSensitivity cs);
121 static inline bool qt_ends_with(const QChar *haystack, int haystackLen,
122                                 const QChar *needle, int needleLen, Qt::CaseSensitivity cs);
123 static inline bool qt_ends_with(const QChar *haystack, int haystackLen,
124                                 QLatin1String needle, Qt::CaseSensitivity cs);
125
126 // Unicode case-insensitive comparison
127 static int ucstricmp(const ushort *a, const ushort *ae, const ushort *b, const ushort *be)
128 {
129     if (a == b)
130         return (ae - be);
131     if (a == 0)
132         return 1;
133     if (b == 0)
134         return -1;
135
136     const ushort *e = ae;
137     if (be - b < ae - a)
138         e = a + (be - b);
139
140     uint alast = 0;
141     uint blast = 0;
142     while (a < e) {
143 //         qDebug() << hex << alast << blast;
144 //         qDebug() << hex << "*a=" << *a << "alast=" << alast << "folded=" << foldCase (*a, alast);
145 //         qDebug() << hex << "*b=" << *b << "blast=" << blast << "folded=" << foldCase (*b, blast);
146         int diff = foldCase(*a, alast) - foldCase(*b, blast);
147         if ((diff))
148             return diff;
149         ++a;
150         ++b;
151     }
152     if (a == ae) {
153         if (b == be)
154             return 0;
155         return -1;
156     }
157     return 1;
158 }
159
160 // Case-insensitive comparison between a Unicode string and a QLatin1String
161 static int ucstricmp(const ushort *a, const ushort *ae, const uchar *b, const uchar *be)
162 {
163     if (a == 0) {
164         if (b == 0)
165             return 0;
166         return 1;
167     }
168     if (b == 0)
169         return -1;
170
171     const ushort *e = ae;
172     if (be - b < ae - a)
173         e = a + (be - b);
174
175     while (a < e) {
176         int diff = foldCase(*a) - foldCase(*b);
177         if ((diff))
178             return diff;
179         ++a;
180         ++b;
181     }
182     if (a == ae) {
183         if (b == be)
184             return 0;
185         return -1;
186     }
187     return 1;
188 }
189
190 // Unicode case-sensitive compare two same-sized strings
191 static int ucstrncmp(const QChar *a, const QChar *b, int l)
192 {
193     while (l-- && *a == *b)
194         a++,b++;
195     if (l==-1)
196         return 0;
197     return a->unicode() - b->unicode();
198 }
199
200 // Unicode case-sensitive comparison
201 static int ucstrcmp(const QChar *a, int alen, const QChar *b, int blen)
202 {
203     if (a == b && alen == blen)
204         return 0;
205     int l = qMin(alen, blen);
206     int cmp = ucstrncmp(a, b, l);
207     return cmp ? cmp : (alen-blen);
208 }
209
210 // Unicode case-insensitive compare two same-sized strings
211 static int ucstrnicmp(const ushort *a, const ushort *b, int l)
212 {
213     return ucstricmp(a, a + l, b, b + l);
214 }
215
216 // Benchmarking indicates that doing memcmp is much slower than
217 // executing the comparison ourselves.
218 //
219 // The profiling was done on a population of calls to qMemEquals, generated
220 // during a run of the demo browser. The profile of the data (32-bit x86
221 // Linux) was:
222 //
223 //  total number of comparisons: 21353
224 //  longest string compared: 95
225 //  average comparison length: 14.8786
226 //  cache-line crosses: 5661 (13.3%)
227 //  alignment histogram:
228 //   0xXXX0 = 512 (1.2%) strings, 0 (0.0%) of which same-aligned
229 //   0xXXX2 = 15087 (35.3%) strings, 5145 (34.1%) of which same-aligned
230 //   0xXXX4 = 525 (1.2%) strings, 0 (0.0%) of which same-aligned
231 //   0xXXX6 = 557 (1.3%) strings, 6 (1.1%) of which same-aligned
232 //   0xXXX8 = 509 (1.2%) strings, 0 (0.0%) of which same-aligned
233 //   0xXXXa = 24358 (57.0%) strings, 9901 (40.6%) of which same-aligned
234 //   0xXXXc = 557 (1.3%) strings, 0 (0.0%) of which same-aligned
235 //   0xXXXe = 601 (1.4%) strings, 15 (2.5%) of which same-aligned
236 //   total  = 42706 (100%) strings, 15067 (35.3%) of which same-aligned
237 //
238 // 92% of the strings have alignment of 2 or 10, which is due to malloc on
239 // 32-bit Linux returning values aligned to 8 bytes, and offsetof(array, QString::Data) == 18.
240 //
241 // The profile on 64-bit will be different since offsetof(array, QString::Data) == 26.
242 //
243 // The benchmark results were, for a Core-i7 @ 2.67 GHz 32-bit, compiled with -O3 -funroll-loops:
244 //   16-bit loads only:           872,301 CPU ticks [Qt 4.5 / memcmp]
245 //   32- and 16-bit loads:        773,362 CPU ticks [Qt 4.6]
246 //   SSE2 "movdqu" 128-bit loads: 618,736 CPU ticks
247 //   SSE3 "lddqu" 128-bit loads:  619,954 CPU ticks
248 //   SSSE3 "palignr" corrections: 852,147 CPU ticks
249 //   SSE4.2 "pcmpestrm":          738,702 CPU ticks
250 //
251 // The same benchmark on an Atom N450 @ 1.66 GHz, is:
252 //  16-bit loads only:            2,185,882 CPU ticks
253 //  32- and 16-bit loads:         1,805,060 CPU ticks
254 //  SSE2 "movdqu" 128-bit loads:  2,529,843 CPU ticks
255 //  SSE3 "lddqu" 128-bit loads:   2,514,858 CPU ticks
256 //  SSSE3 "palignr" corrections:  2,160,325 CPU ticks
257 //  SSE4.2 not available
258 //
259 // The conclusion we reach is that alignment the SSE2 unaligned code can gain
260 // 20% improvement in performance in some systems, but suffers a penalty due
261 // to the unaligned loads on others.
262
263 static bool qMemEquals(const quint16 *a, const quint16 *b, int length)
264 {
265     if (a == b || !length)
266         return true;
267
268     register union {
269         const quint16 *w;
270         const quint32 *d;
271         quintptr value;
272     } sa, sb;
273     sa.w = a;
274     sb.w = b;
275
276     // check alignment
277     if ((sa.value & 2) == (sb.value & 2)) {
278         // both addresses have the same alignment
279         if (sa.value & 2) {
280             // both addresses are not aligned to 4-bytes boundaries
281             // compare the first character
282             if (*sa.w != *sb.w)
283                 return false;
284             --length;
285             ++sa.w;
286             ++sb.w;
287
288             // now both addresses are 4-bytes aligned
289         }
290
291         // both addresses are 4-bytes aligned
292         // do a fast 32-bit comparison
293         register const quint32 *e = sa.d + (length >> 1);
294         for ( ; sa.d != e; ++sa.d, ++sb.d) {
295             if (*sa.d != *sb.d)
296                 return false;
297         }
298
299         // do we have a tail?
300         return (length & 1) ? *sa.w == *sb.w : true;
301     } else {
302         // one of the addresses isn't 4-byte aligned but the other is
303         register const quint16 *e = sa.w + length;
304         for ( ; sa.w != e; ++sa.w, ++sb.w) {
305             if (*sa.w != *sb.w)
306                 return false;
307         }
308     }
309     return true;
310 }
311
312 /*!
313     \internal
314
315     Returns the index position of the first occurrence of the
316     character \a ch in the string given by \a str and \a len,
317     searching forward from index
318     position \a from. Returns -1 if \a ch could not be found.
319 */
320 static int findChar(const QChar *str, int len, QChar ch, int from,
321     Qt::CaseSensitivity cs)
322 {
323     const ushort *s = (const ushort *)str;
324     ushort c = ch.unicode();
325     if (from < 0)
326         from = qMax(from + len, 0);
327     if (from < len) {
328         const ushort *n = s + from - 1;
329         const ushort *e = s + len;
330         if (cs == Qt::CaseSensitive) {
331             while (++n != e)
332                 if (*n == c)
333                     return  n - s;
334         } else {
335             c = foldCase(c);
336             while (++n != e)
337                 if (foldCase(*n) == c)
338                     return  n - s;
339         }
340     }
341     return -1;
342 }
343
344 #define REHASH(a) \
345     if (sl_minus_1 < (int)sizeof(int) * CHAR_BIT)       \
346         hashHaystack -= (a) << sl_minus_1; \
347     hashHaystack <<= 1
348
349 inline bool qIsUpper(char ch)
350 {
351     return ch >= 'A' && ch <= 'Z';
352 }
353
354 inline bool qIsDigit(char ch)
355 {
356     return ch >= '0' && ch <= '9';
357 }
358
359 inline char qToLower(char ch)
360 {
361     if (ch >= 'A' && ch <= 'Z')
362         return ch - 'A' + 'a';
363     else
364         return ch;
365 }
366
367
368 const QString::Null QString::null = { };
369
370 /*!
371   \macro QT_NO_CAST_FROM_ASCII
372   \relates QString
373
374   Disables automatic conversions from 8-bit strings (char *) to unicode QStrings
375
376   \sa QT_NO_CAST_TO_ASCII, QT_NO_CAST_FROM_BYTEARRAY
377 */
378
379 /*!
380   \macro QT_NO_CAST_TO_ASCII
381   \relates QString
382
383   disables automatic conversion from QString to 8-bit strings (char *)
384
385   \sa QT_NO_CAST_FROM_ASCII, QT_NO_CAST_FROM_BYTEARRAY
386 */
387
388 /*!
389   \macro QT_ASCII_CAST_WARNINGS
390   \internal
391   \relates QString
392
393   This macro can be defined to force a warning whenever a function is
394   called that automatically converts between unicode and 8-bit encodings.
395
396   Note: This only works for compilers that support warnings for
397   deprecated API.
398
399   \sa QT_NO_CAST_TO_ASCII, QT_NO_CAST_FROM_ASCII
400 */
401
402 /*!
403     \class QCharRef
404     \reentrant
405     \brief The QCharRef class is a helper class for QString.
406
407     \internal
408
409     \ingroup string-processing
410
411     When you get an object of type QCharRef, if you can assign to it,
412     the assignment will apply to the character in the string from
413     which you got the reference. That is its whole purpose in life.
414     The QCharRef becomes invalid once modifications are made to the
415     string: if you want to keep the character, copy it into a QChar.
416
417     Most of the QChar member functions also exist in QCharRef.
418     However, they are not explicitly documented here.
419
420     \sa QString::operator[](), QString::at(), QChar
421 */
422
423 /*!
424     \class QString
425     \reentrant
426
427     \brief The QString class provides a Unicode character string.
428
429     \ingroup tools
430     \ingroup shared
431     \ingroup string-processing
432
433     QString stores a string of 16-bit \l{QChar}s, where each QChar
434     corresponds one Unicode 4.0 character. (Unicode characters
435     with code values above 65535 are stored using surrogate pairs,
436     i.e., two consecutive \l{QChar}s.)
437
438     \l{Unicode} is an international standard that supports most of the
439     writing systems in use today. It is a superset of US-ASCII (ANSI
440     X3.4-1986) and Latin-1 (ISO 8859-1), and all the US-ASCII/Latin-1
441     characters are available at the same code positions.
442
443     Behind the scenes, QString uses \l{implicit sharing}
444     (copy-on-write) to reduce memory usage and to avoid the needless
445     copying of data. This also helps reduce the inherent overhead of
446     storing 16-bit characters instead of 8-bit characters.
447
448     In addition to QString, Qt also provides the QByteArray class to
449     store raw bytes and traditional 8-bit '\\0'-terminated strings.
450     For most purposes, QString is the class you want to use. It is
451     used throughout the Qt API, and the Unicode support ensures that
452     your applications will be easy to translate if you want to expand
453     your application's market at some point. The two main cases where
454     QByteArray is appropriate are when you need to store raw binary
455     data, and when memory conservation is critical (e.g., with
456     \l{Qt for Embedded Linux}).
457
458     \tableofcontents
459
460     \section1 Initializing a String
461
462     One way to initialize a QString is simply to pass a \c{const char
463     *} to its constructor. For example, the following code creates a
464     QString of size 5 containing the data "Hello":
465
466     \snippet qstring/main.cpp 0
467
468     QString converts the \c{const char *} data into Unicode using the
469     fromUtf8() function.
470
471     In all of the QString functions that take \c{const char *}
472     parameters, the \c{const char *} is interpreted as a classic
473     C-style '\\0'-terminated string encoded in UTF-8. It is legal for
474     the \c{const char *} parameter to be 0.
475
476     You can also provide string data as an array of \l{QChar}s:
477
478     \snippet qstring/main.cpp 1
479
480     QString makes a deep copy of the QChar data, so you can modify it
481     later without experiencing side effects. (If for performance
482     reasons you don't want to take a deep copy of the character data,
483     use QString::fromRawData() instead.)
484
485     Another approach is to set the size of the string using resize()
486     and to initialize the data character per character. QString uses
487     0-based indexes, just like C++ arrays. To access the character at
488     a particular index position, you can use \l operator[](). On
489     non-const strings, \l operator[]() returns a reference to a
490     character that can be used on the left side of an assignment. For
491     example:
492
493     \snippet qstring/main.cpp 2
494
495     For read-only access, an alternative syntax is to use the at()
496     function:
497
498     \snippet qstring/main.cpp 3
499
500     The at() function can be faster than \l operator[](), because it
501     never causes a \l{deep copy} to occur. Alternatively, use the
502     left(), right(), or mid() functions to extract several characters
503     at a time.
504
505     A QString can embed '\\0' characters (QChar::Null). The size()
506     function always returns the size of the whole string, including
507     embedded '\\0' characters.
508
509     After a call to the resize() function, newly allocated characters
510     have undefined values. To set all the characters in the string to
511     a particular value, use the fill() function.
512
513     QString provides dozens of overloads designed to simplify string
514     usage. For example, if you want to compare a QString with a string
515     literal, you can write code like this and it will work as expected:
516
517     \snippet qstring/main.cpp 4
518
519     You can also pass string literals to functions that take QStrings
520     as arguments, invoking the QString(const char *)
521     constructor. Similarly, you can pass a QString to a function that
522     takes a \c{const char *} argument using the \l qPrintable() macro
523     which returns the given QString as a \c{const char *}. This is
524     equivalent to calling <QString>.toLocal8Bit().constData().
525
526     \section1 Manipulating String Data
527
528     QString provides the following basic functions for modifying the
529     character data: append(), prepend(), insert(), replace(), and
530     remove(). For example:
531
532     \snippet qstring/main.cpp 5
533
534     If you are building a QString gradually and know in advance
535     approximately how many characters the QString will contain, you
536     can call reserve(), asking QString to preallocate a certain amount
537     of memory. You can also call capacity() to find out how much
538     memory QString actually allocated.
539
540     The replace() and remove() functions' first two arguments are the
541     position from which to start erasing and the number of characters
542     that should be erased.  If you want to replace all occurrences of
543     a particular substring with another, use one of the two-parameter
544     replace() overloads.
545
546     A frequent requirement is to remove whitespace characters from a
547     string ('\\n', '\\t', ' ', etc.). If you want to remove whitespace
548     from both ends of a QString, use the trimmed() function. If you
549     want to remove whitespace from both ends and replace multiple
550     consecutive whitespaces with a single space character within the
551     string, use simplified().
552
553     If you want to find all occurrences of a particular character or
554     substring in a QString, use the indexOf() or lastIndexOf()
555     functions. The former searches forward starting from a given index
556     position, the latter searches backward. Both return the index
557     position of the character or substring if they find it; otherwise,
558     they return -1.  For example, here's a typical loop that finds all
559     occurrences of a particular substring:
560
561     \snippet qstring/main.cpp 6
562
563     QString provides many functions for converting numbers into
564     strings and strings into numbers. See the arg() functions, the
565     setNum() functions, the number() static functions, and the
566     toInt(), toDouble(), and similar functions.
567
568     To get an upper- or lowercase version of a string use toUpper() or
569     toLower().
570
571     Lists of strings are handled by the QStringList class. You can
572     split a string into a list of strings using the split() function,
573     and join a list of strings into a single string with an optional
574     separator using QStringList::join(). You can obtain a list of
575     strings from a string list that contain a particular substring or
576     that match a particular QRegExp using the QStringList::filter()
577     function.
578
579     \section1 Querying String Data
580
581     If you want to see if a QString starts or ends with a particular
582     substring use startsWith() or endsWith(). If you simply want to
583     check whether a QString contains a particular character or
584     substring, use the contains() function. If you want to find out
585     how many times a particular character or substring occurs in the
586     string, use count().
587
588     QStrings can be compared using overloaded operators such as \l
589     operator<(), \l operator<=(), \l operator==(), \l operator>=(),
590     and so on.  Note that the comparison is based exclusively on the
591     numeric Unicode values of the characters. It is very fast, but is
592     not what a human would expect; the QString::localeAwareCompare()
593     function is a better choice for sorting user-interface strings.
594
595     To obtain a pointer to the actual character data, call data() or
596     constData(). These functions return a pointer to the beginning of
597     the QChar data. The pointer is guaranteed to remain valid until a
598     non-const function is called on the QString.
599
600     \section1 Converting Between 8-Bit Strings and Unicode Strings
601
602     QString provides the following three functions that return a
603     \c{const char *} version of the string as QByteArray: toUtf8(),
604     toLatin1(), and toLocal8Bit().
605
606     \list
607     \li toLatin1() returns a Latin-1 (ISO 8859-1) encoded 8-bit string.
608     \li toUtf8() returns a UTF-8 encoded 8-bit string. UTF-8 is a
609        superset of US-ASCII (ANSI X3.4-1986) that supports the entire
610        Unicode character set through multibyte sequences.
611     \li toLocal8Bit() returns an 8-bit string using the system's local
612        encoding.
613     \endlist
614
615     To convert from one of these encodings, QString provides
616     fromLatin1(), fromUtf8(), and fromLocal8Bit(). Other
617     encodings are supported through the QTextCodec class.
618
619     As mentioned above, QString provides a lot of functions and
620     operators that make it easy to interoperate with \c{const char *}
621     strings. But this functionality is a double-edged sword: It makes
622     QString more convenient to use if all strings are US-ASCII or
623     Latin-1, but there is always the risk that an implicit conversion
624     from or to \c{const char *} is done using the wrong 8-bit
625     encoding. To minimize these risks, you can turn off these implicit
626     conversions by defining the following two preprocessor symbols:
627
628     \list
629     \li \c QT_NO_CAST_FROM_ASCII disables automatic conversions from
630        C string literals and pointers to Unicode.
631     \li \c QT_NO_CAST_TO_ASCII disables automatic conversion from QString
632        to C strings.
633     \endlist
634
635     One way to define these preprocessor symbols globally for your
636     application is to add the following entry to your
637     \l{qmake Project Files}{qmake project file}:
638
639     \snippet code/src_corelib_tools_qstring.cpp 0
640
641     You then need to explicitly call fromUtf8(), fromLatin1(),
642     or fromLocal8Bit() to construct a QString from an
643     8-bit string, or use the lightweight QLatin1String class, for
644     example:
645
646     \snippet code/src_corelib_tools_qstring.cpp 1
647
648     Similarly, you must call toLatin1(), toUtf8(), or
649     toLocal8Bit() explicitly to convert the QString to an 8-bit
650     string.  (Other encodings are supported through the QTextCodec
651     class.)
652
653     \table 100 %
654     \header
655     \li Note for C Programmers
656
657     \row
658     \li
659     Due to C++'s type system and the fact that QString is
660     \l{implicitly shared}, QStrings may be treated like \c{int}s or
661     other basic types. For example:
662
663     \snippet qstring/main.cpp 7
664
665     The \c result variable, is a normal variable allocated on the
666     stack. When \c return is called, and because we're returning by
667     value, the copy constructor is called and a copy of the string is
668     returned. No actual copying takes place thanks to the implicit
669     sharing.
670
671     \endtable
672
673     \section1 Distinction Between Null and Empty Strings
674
675     For historical reasons, QString distinguishes between a null
676     string and an empty string. A \e null string is a string that is
677     initialized using QString's default constructor or by passing
678     (const char *)0 to the constructor. An \e empty string is any
679     string with size 0. A null string is always empty, but an empty
680     string isn't necessarily null:
681
682     \snippet qstring/main.cpp 8
683
684     All functions except isNull() treat null strings the same as empty
685     strings. For example, toUtf8().constData() returns a pointer to a
686     '\\0' character for a null string (\e not a null pointer), and
687     QString() compares equal to QString(""). We recommend that you
688     always use the isEmpty() function and avoid isNull().
689
690     \section1 Argument Formats
691
692     In member functions where an argument \e format can be specified
693     (e.g., arg(), number()), the argument \e format can be one of the
694     following:
695
696     \table
697     \header \li Format \li Meaning
698     \row \li \c e \li format as [-]9.9e[+|-]999
699     \row \li \c E \li format as [-]9.9E[+|-]999
700     \row \li \c f \li format as [-]9.9
701     \row \li \c g \li use \c e or \c f format, whichever is the most concise
702     \row \li \c G \li use \c E or \c f format, whichever is the most concise
703     \endtable
704
705     A \e precision is also specified with the argument \e format. For
706     the 'e', 'E', and 'f' formats, the \e precision represents the
707     number of digits \e after the decimal point. For the 'g' and 'G'
708     formats, the \e precision represents the maximum number of
709     significant digits (trailing zeroes are omitted).
710
711     \section1 More Efficient String Construction
712
713     Many strings are known at compile time. But the trivial
714     constructor QString("Hello"), will copy the contents of the string,
715     treating the contents as Latin-1. To avoid this one can use the
716     QStringLiteral macro to directly create the required data at compile
717     time. Constructing a QString out of the literal does then not cause
718     any overhead at runtime.
719
720     A slightly less efficient way is to use QLatin1String. This class wraps
721     a C string literal, precalculates it length at compile time and can
722     then be used for faster comparison with QStrings and conversion to
723     QStrings than a regular C string literal.
724
725     Using the QString \c{'+'} operator, it is easy to construct a
726     complex string from multiple substrings. You will often write code
727     like this:
728
729     \snippet qstring/stringbuilder.cpp 0
730
731     There is nothing wrong with either of these string constructions,
732     but there are a few hidden inefficiencies. Beginning with Qt 4.6,
733     you can eliminate them.
734
735     First, multiple uses of the \c{'+'} operator usually means
736     multiple memory allocations. When concatenating \e{n} substrings,
737     where \e{n > 2}, there can be as many as \e{n - 1} calls to the
738     memory allocator.
739
740     In 4.6, an internal template class \c{QStringBuilder} has been
741     added along with a few helper functions. This class is marked
742     internal and does not appear in the documentation, because you
743     aren't meant to instantiate it in your code. Its use will be
744     automatic, as described below. The class is found in
745     \c {src/corelib/tools/qstringbuilder.cpp} if you want to have a
746     look at it.
747
748     \c{QStringBuilder} uses expression templates and reimplements the
749     \c{'%'} operator so that when you use \c{'%'} for string
750     concatenation instead of \c{'+'}, multiple substring
751     concatenations will be postponed until the final result is about
752     to be assigned to a QString. At this point, the amount of memory
753     required for the final result is known. The memory allocator is
754     then called \e{once} to get the required space, and the substrings
755     are copied into it one by one.
756
757     Additional efficiency is gained by inlining and reduced reference
758     counting (the QString created from a \c{QStringBuilder} typically
759     has a ref count of 1, whereas QString::append() needs an extra
760     test).
761
762     There are three ways you can access this improved method of string
763     construction. The straightforward way is to include
764     \c{QStringBuilder} wherever you want to use it, and use the
765     \c{'%'} operator instead of \c{'+'} when concatenating strings:
766
767     \snippet qstring/stringbuilder.cpp 5
768
769     A more global approach which is the most convenient but
770     not entirely source compatible, is to this define in your
771     .pro file:
772
773     \snippet qstring/stringbuilder.cpp 3
774
775     and the \c{'+'} will automatically be performed as the
776     \c{QStringBuilder} \c{'%'} everywhere.
777
778     \sa fromRawData(), QChar, QLatin1String, QByteArray, QStringRef
779 */
780
781 /*!
782     \enum QString::SplitBehavior
783
784     This enum specifies how the split() function should behave with
785     respect to empty strings.
786
787     \value KeepEmptyParts  If a field is empty, keep it in the result.
788     \value SkipEmptyParts  If a field is empty, don't include it in the result.
789
790     \sa split()
791 */
792
793 /*! \typedef QString::ConstIterator
794
795     Qt-style synonym for QString::const_iterator.
796 */
797
798 /*! \typedef QString::Iterator
799
800     Qt-style synonym for QString::iterator.
801 */
802
803 /*! \typedef QString::const_iterator
804
805     The QString::const_iterator typedef provides an STL-style const
806     iterator for QString.
807
808     \sa QString::iterator
809 */
810
811 /*! \typedef QString::iterator
812
813     The QString::iterator typedef provides an STL-style non-const
814     iterator for QString.
815
816     \sa QString::const_iterator
817 */
818
819 /*!
820     \typedef QString::const_reference
821
822     The QString::const_reference typedef provides an STL-style
823     const reference for QString.
824 */
825 /*!
826     \typedef QString::reference
827
828     The QString::const_reference typedef provides an STL-style
829     reference for QString.
830 */
831 /*!
832     \typedef QString::value_type
833
834     The QString::const_reference typedef provides an STL-style
835     value type for QString.
836 */
837
838 /*! \fn QString::iterator QString::begin()
839
840     Returns an \l{STL-style iterator} pointing to the first character in
841     the string.
842
843     \sa constBegin(), end()
844 */
845
846 /*! \fn QString::const_iterator QString::begin() const
847
848     \overload begin()
849 */
850
851 /*! \fn QString::const_iterator QString::cbegin() const
852     \since 5.0
853
854     Returns a const \l{STL-style iterator} pointing to the first character
855     in the string.
856
857     \sa begin(), cend()
858 */
859
860 /*! \fn QString::const_iterator QString::constBegin() const
861
862     Returns a const \l{STL-style iterator} pointing to the first character
863     in the string.
864
865     \sa begin(), constEnd()
866 */
867
868 /*! \fn QString::iterator QString::end()
869
870     Returns an \l{STL-style iterator} pointing to the imaginary character
871     after the last character in the string.
872
873     \sa begin(), constEnd()
874 */
875
876 /*! \fn QString::const_iterator QString::end() const
877
878     \overload end()
879 */
880
881 /*! \fn QString::const_iterator QString::cend() const
882     \since 5.0
883
884     Returns a const \l{STL-style iterator} pointing to the imaginary
885     item after the last item in the list.
886
887     \sa cbegin(), end()
888 */
889
890 /*! \fn QString::const_iterator QString::constEnd() const
891
892     Returns a const \l{STL-style iterator} pointing to the imaginary
893     item after the last item in the list.
894
895     \sa constBegin(), end()
896 */
897
898 /*!
899     \fn QString::QString()
900
901     Constructs a null string. Null strings are also empty.
902
903     \sa isEmpty()
904 */
905
906 /*! \fn QString::QString(const char *str)
907
908     Constructs a string initialized with the 8-bit string \a str. The
909     given const char pointer is converted to Unicode using the
910     fromUtf8() function.
911
912     You can disable this constructor by defining \c
913     QT_NO_CAST_FROM_ASCII when you compile your applications. This
914     can be useful if you want to ensure that all user-visible strings
915     go through QObject::tr(), for example.
916
917     \sa fromLatin1(), fromLocal8Bit(), fromUtf8()
918 */
919
920 /*! \fn QString QString::fromStdString(const std::string &str)
921
922     Returns a copy of the \a str string. The given string is converted
923     to Unicode using the fromUtf8() function.
924
925     This constructor is only available if Qt is configured with STL
926     compatibility enabled.
927
928     \sa fromLatin1(), fromLocal8Bit(), fromUtf8()
929 */
930
931 /*! \fn QString QString::fromStdWString(const std::wstring &str)
932
933     Returns a copy of the \a str string. The given string is assumed
934     to be encoded in utf16 if the size of wchar_t is 2 bytes (e.g. on
935     windows) and ucs4 if the size of wchar_t is 4 bytes (most Unix
936     systems).
937
938     \sa fromUtf16(), fromLatin1(), fromLocal8Bit(), fromUtf8(), fromUcs4()
939 */
940
941 /*! \fn QString QString::fromWCharArray(const wchar_t *string, int size)
942     \since 4.2
943
944     Returns a copy of the \a string, where the encoding of \a string depends on
945     the size of wchar. If wchar is 4 bytes, the \a string is interpreted as ucs-4,
946     if wchar is 2 bytes it is interpreted as ucs-2.
947
948     If \a size is -1 (default), the \a string has to be 0 terminated.
949
950     \sa fromUtf16(), fromLatin1(), fromLocal8Bit(), fromUtf8(), fromUcs4(), fromStdWString()
951 */
952
953 /*! \fn std::wstring QString::toStdWString() const
954
955     Returns a std::wstring object with the data contained in this
956     QString. The std::wstring is encoded in utf16 on platforms where
957     wchar_t is 2 bytes wide (e.g. windows) and in ucs4 on platforms
958     where wchar_t is 4 bytes wide (most Unix systems).
959
960     This operator is mostly useful to pass a QString to a function
961     that accepts a std::wstring object.
962
963     \sa utf16(), toLatin1(), toUtf8(), toLocal8Bit()
964 */
965
966 // ### replace with QCharIterator
967 int QString::toUcs4_helper(const ushort *uc, int length, uint *out)
968 {
969     int i = 0;
970     for (; i < length; ++i) {
971         uint u = uc[i];
972         if (QChar::isHighSurrogate(u) && i + 1 < length) {
973             ushort low = uc[i+1];
974             if (QChar::isLowSurrogate(low)) {
975                 ++i;
976                 u = QChar::surrogateToUcs4(u, low);
977             }
978         }
979         *out++ = u;
980     }
981     return i;
982 }
983
984 /*! \fn int QString::toWCharArray(wchar_t *array) const
985   \since 4.2
986
987   Fills the \a array with the data contained in this QString object.
988   The array is encoded in utf16 on platforms where
989   wchar_t is 2 bytes wide (e.g. windows) and in ucs4 on platforms
990   where wchar_t is 4 bytes wide (most Unix systems).
991
992   \a array has to be allocated by the caller and contain enough space to
993   hold the complete string (allocating the array with the same length as the
994   string is always sufficient).
995
996   This function returns the actual length of the string in \a array.
997
998   \note This function does not append a null character to the array.
999
1000   \sa utf16(), toUcs4(), toLatin1(), toUtf8(), toLocal8Bit(), toStdWString()
1001 */
1002
1003 /*! \fn QString::QString(const QString &other)
1004
1005     Constructs a copy of \a other.
1006
1007     This operation takes \l{constant time}, because QString is
1008     \l{implicitly shared}. This makes returning a QString from a
1009     function very fast. If a shared instance is modified, it will be
1010     copied (copy-on-write), and that takes \l{linear time}.
1011
1012     \sa operator=()
1013 */
1014
1015 /*!
1016     Constructs a string initialized with the first \a size characters
1017     of the QChar array \a unicode.
1018
1019     If \a unicode is 0, a null string is constructed.
1020
1021     If \a size is negative, \a unicode is assumed to point to a nul-terminated
1022     array and its length is determined dynamically. The terminating
1023     nul-character is not considered part of the string.
1024
1025     QString makes a deep copy of the string data. The unicode data is copied as
1026     is and the Byte Order Mark is preserved if present.
1027
1028     \sa fromRawData()
1029 */
1030 QString::QString(const QChar *unicode, int size)
1031 {
1032    if (!unicode) {
1033         d = Data::sharedNull();
1034     } else {
1035         if (size < 0) {
1036             size = 0;
1037             while (unicode[size] != 0)
1038                 ++size;
1039         }
1040         if (!size) {
1041             d = Data::allocate(0);
1042         } else {
1043             d = Data::allocate(size + 1);
1044             Q_CHECK_PTR(d);
1045             d->size = size;
1046             memcpy(d->data(), unicode, size * sizeof(QChar));
1047             d->data()[size] = '\0';
1048         }
1049     }
1050 }
1051
1052 /*!
1053     Constructs a string of the given \a size with every character set
1054     to \a ch.
1055
1056     \sa fill()
1057 */
1058 QString::QString(int size, QChar ch)
1059 {
1060    if (size <= 0) {
1061         d = Data::allocate(0);
1062     } else {
1063         d = Data::allocate(size + 1);
1064         Q_CHECK_PTR(d);
1065         d->size = size;
1066         d->data()[size] = '\0';
1067         ushort *i = d->data() + size;
1068         ushort *b = d->data();
1069         const ushort value = ch.unicode();
1070         while (i != b)
1071            *--i = value;
1072     }
1073 }
1074
1075 /*! \fn QString::QString(int size, Qt::Initialization)
1076   \internal
1077
1078   Constructs a string of the given \a size without initializing the
1079   characters. This is only used in \c QStringBuilder::toString().
1080 */
1081 QString::QString(int size, Qt::Initialization)
1082 {
1083     d = Data::allocate(size + 1);
1084     Q_CHECK_PTR(d);
1085     d->size = size;
1086     d->data()[size] = '\0';
1087 }
1088
1089 /*! \fn QString::QString(QLatin1String str)
1090
1091     Constructs a copy of the Latin-1 string \a str.
1092
1093     \sa fromLatin1()
1094 */
1095
1096 /*!
1097     Constructs a string of size 1 containing the character \a ch.
1098 */
1099 QString::QString(QChar ch)
1100 {
1101     d = Data::allocate(2);
1102     Q_CHECK_PTR(d);
1103     d->size = 1;
1104     d->data()[0] = ch.unicode();
1105     d->data()[1] = '\0';
1106 }
1107
1108 /*! \fn QString::QString(const QByteArray &ba)
1109
1110     Constructs a string initialized with the byte array \a ba. The
1111     given byte array is converted to Unicode using fromUtf8(). Stops
1112     copying at the first 0 character, otherwise copies the entire byte
1113     array.
1114
1115     You can disable this constructor by defining \c
1116     QT_NO_CAST_FROM_ASCII when you compile your applications. This
1117     can be useful if you want to ensure that all user-visible strings
1118     go through QObject::tr(), for example.
1119
1120     \sa fromLatin1(), fromLocal8Bit(), fromUtf8()
1121 */
1122
1123 /*! \fn QString::QString(const Null &)
1124     \internal
1125 */
1126
1127 /*! \fn QString &QString::operator=(const Null &)
1128     \internal
1129 */
1130
1131 /*!
1132   \fn QString::~QString()
1133
1134     Destroys the string.
1135 */
1136
1137
1138 /*! \fn void QString::swap(QString &other)
1139     \since 4.8
1140
1141     Swaps string \a other with this string. This operation is very fast and
1142     never fails.
1143 */
1144
1145 /*! \fn void QString::detach()
1146
1147     \internal
1148 */
1149
1150 /*! \fn bool QString::isDetached() const
1151
1152     \internal
1153 */
1154
1155 /*! \fn bool QString::isSharedWith(const QString &other) const
1156
1157     \internal
1158 */
1159
1160 /*!
1161     Sets the size of the string to \a size characters.
1162
1163     If \a size is greater than the current size, the string is
1164     extended to make it \a size characters long with the extra
1165     characters added to the end. The new characters are uninitialized.
1166
1167     If \a size is less than the current size, characters are removed
1168     from the end.
1169
1170     Example:
1171
1172     \snippet qstring/main.cpp 45
1173
1174     If you want to append a certain number of identical characters to
1175     the string, use \l operator+=() as follows rather than resize():
1176
1177     \snippet qstring/main.cpp 46
1178
1179     If you want to expand the string so that it reaches a certain
1180     width and fill the new positions with a particular character, use
1181     the leftJustified() function:
1182
1183     If \a size is negative, it is equivalent to passing zero.
1184
1185     \snippet qstring/main.cpp 47
1186
1187     \sa truncate(), reserve()
1188 */
1189
1190 void QString::resize(int size)
1191 {
1192     if (size < 0)
1193         size = 0;
1194
1195     if (IS_RAW_DATA(d) && !d->ref.isShared() && size < d->size) {
1196         d->size = size;
1197         return;
1198     }
1199
1200     if (size == 0 && !d->capacityReserved) {
1201         Data *x = Data::allocate(0);
1202         if (!d->ref.deref())
1203             Data::deallocate(d);
1204         d = x;
1205     } else {
1206         if (d->ref.isShared() || uint(size) + 1u > d->alloc
1207                 || (!d->capacityReserved && size < d->size
1208                     && uint(size) + 1u < uint(d->alloc >> 1)))
1209             reallocData(uint(size) + 1u, true);
1210         if (d->alloc) {
1211             d->size = size;
1212             d->data()[size] = '\0';
1213         }
1214     }
1215 }
1216
1217 /*! \fn int QString::capacity() const
1218
1219     Returns the maximum number of characters that can be stored in
1220     the string without forcing a reallocation.
1221
1222     The sole purpose of this function is to provide a means of fine
1223     tuning QString's memory usage. In general, you will rarely ever
1224     need to call this function. If you want to know how many
1225     characters are in the string, call size().
1226
1227     \sa reserve(), squeeze()
1228 */
1229
1230 /*!
1231     \fn void QString::reserve(int size)
1232
1233     Attempts to allocate memory for at least \a size characters. If
1234     you know in advance how large the string will be, you can call
1235     this function, and if you resize the string often you are likely
1236     to get better performance. If \a size is an underestimate, the
1237     worst that will happen is that the QString will be a bit slower.
1238
1239     The sole purpose of this function is to provide a means of fine
1240     tuning QString's memory usage. In general, you will rarely ever
1241     need to call this function. If you want to change the size of the
1242     string, call resize().
1243
1244     This function is useful for code that needs to build up a long
1245     string and wants to avoid repeated reallocation. In this example,
1246     we want to add to the string until some condition is true, and
1247     we're fairly sure that size is large enough to make a call to
1248     reserve() worthwhile:
1249
1250     \snippet qstring/main.cpp 44
1251
1252     \sa squeeze(), capacity()
1253 */
1254
1255 /*!
1256     \fn void QString::squeeze()
1257
1258     Releases any memory not required to store the character data.
1259
1260     The sole purpose of this function is to provide a means of fine
1261     tuning QString's memory usage. In general, you will rarely ever
1262     need to call this function.
1263
1264     \sa reserve(), capacity()
1265 */
1266
1267 void QString::reallocData(uint alloc, bool grow)
1268 {
1269     if (grow)
1270         alloc = qAllocMore(alloc * sizeof(QChar), sizeof(Data)) / sizeof(QChar);
1271
1272     if (d->ref.isShared() || IS_RAW_DATA(d)) {
1273         Data::AllocationOptions allocOptions(d->capacityReserved ? Data::CapacityReserved : 0);
1274         Data *x = Data::allocate(alloc, allocOptions);
1275         Q_CHECK_PTR(x);
1276         x->size = qMin(int(alloc) - 1, d->size);
1277         ::memcpy(x->data(), d->data(), x->size * sizeof(QChar));
1278         x->data()[x->size] = 0;
1279         if (!d->ref.deref())
1280             Data::deallocate(d);
1281         d = x;
1282     } else {
1283         Data *p = static_cast<Data *>(::realloc(d, sizeof(Data) + alloc * sizeof(QChar)));
1284         Q_CHECK_PTR(p);
1285         d = p;
1286         d->alloc = alloc;
1287         d->offset = sizeof(QStringData);
1288     }
1289 }
1290
1291 void QString::expand(int i)
1292 {
1293     int sz = d->size;
1294     resize(qMax(i + 1, sz));
1295     if (d->size - 1 > sz) {
1296         ushort *n = d->data() + d->size - 1;
1297         ushort *e = d->data() + sz;
1298         while (n != e)
1299            * --n = ' ';
1300     }
1301 }
1302
1303 /*! \fn void QString::clear()
1304
1305     Clears the contents of the string and makes it empty.
1306
1307     \sa resize(), isEmpty()
1308 */
1309
1310 /*! \fn QString &QString::operator=(const QString &other)
1311
1312     Assigns \a other to this string and returns a reference to this
1313     string.
1314 */
1315
1316 QString &QString::operator=(const QString &other)
1317 {
1318     other.d->ref.ref();
1319     if (!d->ref.deref())
1320         Data::deallocate(d);
1321     d = other.d;
1322     return *this;
1323 }
1324
1325
1326 /*! \fn QString &QString::operator=(QLatin1String str)
1327
1328     \overload operator=()
1329
1330     Assigns the Latin-1 string \a str to this string.
1331 */
1332
1333 /*! \fn QString &QString::operator=(const QByteArray &ba)
1334
1335     \overload operator=()
1336
1337     Assigns \a ba to this string. The byte array is converted to Unicode
1338     using the fromUtf8() function. This function stops conversion at the
1339     first NUL character found, or the end of the \a ba byte array.
1340
1341     You can disable this operator by defining \c
1342     QT_NO_CAST_FROM_ASCII when you compile your applications. This
1343     can be useful if you want to ensure that all user-visible strings
1344     go through QObject::tr(), for example.
1345 */
1346
1347 /*! \fn QString &QString::operator=(const char *str)
1348
1349     \overload operator=()
1350
1351     Assigns \a str to this string. The const char pointer is converted
1352     to Unicode using the fromUtf8() function.
1353
1354     You can disable this operator by defining \c
1355     QT_NO_CAST_FROM_ASCII when you compile your applications. This
1356     can be useful if you want to ensure that all user-visible strings
1357     go through QObject::tr(), for example.
1358 */
1359
1360 /*! \fn QString &QString::operator=(char ch)
1361
1362     \overload operator=()
1363
1364     Assigns character \a ch to this string. Note that the character is
1365     converted to Unicode using the fromLatin1() function, unlike other 8-bit
1366     functions that operate on UTF-8 data.
1367
1368     You can disable this operator by defining \c
1369     QT_NO_CAST_FROM_ASCII when you compile your applications. This
1370     can be useful if you want to ensure that all user-visible strings
1371     go through QObject::tr(), for example.
1372 */
1373
1374 /*!
1375     \overload operator=()
1376
1377     Sets the string to contain the single character \a ch.
1378 */
1379 QString &QString::operator=(QChar ch)
1380 {
1381     return operator=(QString(ch));
1382 }
1383
1384 /*!
1385      \fn QString& QString::insert(int position, const QString &str)
1386
1387     Inserts the string \a str at the given index \a position and
1388     returns a reference to this string.
1389
1390     Example:
1391
1392     \snippet qstring/main.cpp 26
1393
1394     If the given \a position is greater than size(), the array is
1395     first extended using resize().
1396
1397     \sa append(), prepend(), replace(), remove()
1398 */
1399
1400
1401 /*!
1402     \fn QString &QString::insert(int position, QLatin1String str)
1403     \overload insert()
1404
1405     Inserts the Latin-1 string \a str at the given index \a position.
1406 */
1407 QString &QString::insert(int i, QLatin1String str)
1408 {
1409     const uchar *s = (const uchar *)str.latin1();
1410     if (i < 0 || !s || !(*s))
1411         return *this;
1412
1413     int len = str.size();
1414     expand(qMax(d->size, i) + len - 1);
1415
1416     ::memmove(d->data() + i + len, d->data() + i, (d->size - i - len) * sizeof(QChar));
1417     for (int j = 0; j < len; ++j)
1418         d->data()[i + j] = s[j];
1419     return *this;
1420 }
1421
1422 /*!
1423     \fn QString& QString::insert(int position, const QChar *unicode, int size)
1424     \overload insert()
1425
1426     Inserts the first \a size characters of the QChar array \a unicode
1427     at the given index \a position in the string.
1428 */
1429 QString& QString::insert(int i, const QChar *unicode, int size)
1430 {
1431     if (i < 0 || size <= 0)
1432         return *this;
1433
1434     const ushort *s = (const ushort *)unicode;
1435     if (s >= d->data() && s < d->data() + d->alloc) {
1436         // Part of me - take a copy
1437         ushort *tmp = static_cast<ushort *>(::malloc(size * sizeof(QChar)));
1438         Q_CHECK_PTR(tmp);
1439         memcpy(tmp, s, size * sizeof(QChar));
1440         insert(i, reinterpret_cast<const QChar *>(tmp), size);
1441         ::free(tmp);
1442         return *this;
1443     }
1444
1445     expand(qMax(d->size, i) + size - 1);
1446
1447     ::memmove(d->data() + i + size, d->data() + i, (d->size - i - size) * sizeof(QChar));
1448     memcpy(d->data() + i, s, size * sizeof(QChar));
1449     return *this;
1450 }
1451
1452 /*!
1453     \fn QString& QString::insert(int position, QChar ch)
1454     \overload insert()
1455
1456     Inserts \a ch at the given index \a position in the string.
1457 */
1458
1459 QString& QString::insert(int i, QChar ch)
1460 {
1461     if (i < 0)
1462         i += d->size;
1463     if (i < 0)
1464         return *this;
1465     expand(qMax(i, d->size));
1466     ::memmove(d->data() + i + 1, d->data() + i, (d->size - i - 1) * sizeof(QChar));
1467     d->data()[i] = ch.unicode();
1468     return *this;
1469 }
1470
1471 /*!
1472     Appends the string \a str onto the end of this string.
1473
1474     Example:
1475
1476     \snippet qstring/main.cpp 9
1477
1478     This is the same as using the insert() function:
1479
1480     \snippet qstring/main.cpp 10
1481
1482     The append() function is typically very fast (\l{constant time}),
1483     because QString preallocates extra space at the end of the string
1484     data so it can grow without reallocating the entire string each
1485     time.
1486
1487     \sa operator+=(), prepend(), insert()
1488 */
1489 QString &QString::append(const QString &str)
1490 {
1491     if (str.d != Data::sharedNull()) {
1492         if (d == Data::sharedNull()) {
1493             operator=(str);
1494         } else {
1495             if (d->ref.isShared() || uint(d->size + str.d->size) + 1u > d->alloc)
1496                 reallocData(uint(d->size + str.d->size) + 1u, true);
1497             memcpy(d->data() + d->size, str.d->data(), str.d->size * sizeof(QChar));
1498             d->size += str.d->size;
1499             d->data()[d->size] = '\0';
1500         }
1501     }
1502     return *this;
1503 }
1504
1505 /*!
1506   \overload append()
1507
1508   Appends the Latin-1 string \a str to this string.
1509 */
1510 QString &QString::append(QLatin1String str)
1511 {
1512     const uchar *s = (const uchar *)str.latin1();
1513     if (s) {
1514         int len = str.size();
1515         if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc)
1516             reallocData(uint(d->size + len) + 1u, true);
1517         ushort *i = d->data() + d->size;
1518         while ((*i++ = *s++))
1519             ;
1520         d->size += len;
1521     }
1522     return *this;
1523 }
1524
1525 /*! \fn QString &QString::append(const QByteArray &ba)
1526
1527     \overload append()
1528
1529     Appends the byte array \a ba to this string. The given byte array
1530     is converted to Unicode using the fromUtf8() function.
1531
1532     You can disable this function by defining \c QT_NO_CAST_FROM_ASCII
1533     when you compile your applications. This can be useful if you want
1534     to ensure that all user-visible strings go through QObject::tr(),
1535     for example.
1536 */
1537
1538 /*! \fn QString &QString::append(const char *str)
1539
1540     \overload append()
1541
1542     Appends the string \a str to this string. The given const char
1543     pointer is converted to Unicode using the fromUtf8() function.
1544
1545     You can disable this function by defining \c QT_NO_CAST_FROM_ASCII
1546     when you compile your applications. This can be useful if you want
1547     to ensure that all user-visible strings go through QObject::tr(),
1548     for example.
1549 */
1550
1551 /*!
1552     \overload append()
1553
1554     Appends the character \a ch to this string.
1555 */
1556 QString &QString::append(QChar ch)
1557 {
1558     if (d->ref.isShared() || uint(d->size) + 2u > d->alloc)
1559         reallocData(uint(d->size) + 2u, true);
1560     d->data()[d->size++] = ch.unicode();
1561     d->data()[d->size] = '\0';
1562     return *this;
1563 }
1564
1565 /*! \fn QString &QString::prepend(const QString &str)
1566
1567     Prepends the string \a str to the beginning of this string and
1568     returns a reference to this string.
1569
1570     Example:
1571
1572     \snippet qstring/main.cpp 36
1573
1574     \sa append(), insert()
1575 */
1576
1577 /*! \fn QString &QString::prepend(QLatin1String str)
1578
1579     \overload prepend()
1580
1581     Prepends the Latin-1 string \a str to this string.
1582 */
1583
1584 /*! \fn QString &QString::prepend(const QByteArray &ba)
1585
1586     \overload prepend()
1587
1588     Prepends the byte array \a ba to this string. The byte array is
1589     converted to Unicode using the fromUtf8() function.
1590
1591     You can disable this function by defining \c
1592     QT_NO_CAST_FROM_ASCII when you compile your applications. This
1593     can be useful if you want to ensure that all user-visible strings
1594     go through QObject::tr(), for example.
1595 */
1596
1597 /*! \fn QString &QString::prepend(const char *str)
1598
1599     \overload prepend()
1600
1601     Prepends the string \a str to this string. The const char pointer
1602     is converted to Unicode using the fromUtf8() function.
1603
1604     You can disable this function by defining \c
1605     QT_NO_CAST_FROM_ASCII when you compile your applications. This
1606     can be useful if you want to ensure that all user-visible strings
1607     go through QObject::tr(), for example.
1608 */
1609
1610 /*! \fn QString &QString::prepend(QChar ch)
1611
1612     \overload prepend()
1613
1614     Prepends the character \a ch to this string.
1615 */
1616
1617 /*!
1618   \fn QString &QString::remove(int position, int n)
1619
1620   Removes \a n characters from the string, starting at the given \a
1621   position index, and returns a reference to the string.
1622
1623   If the specified \a position index is within the string, but \a
1624   position + \a n is beyond the end of the string, the string is
1625   truncated at the specified \a position.
1626
1627   \snippet qstring/main.cpp 37
1628
1629   \sa insert(), replace()
1630 */
1631 QString &QString::remove(int pos, int len)
1632 {
1633     if (pos < 0)  // count from end of string
1634         pos += d->size;
1635     if (pos < 0 || pos >= d->size) {
1636         // range problems
1637     } else if (len >= d->size - pos) {
1638         resize(pos); // truncate
1639     } else if (len > 0) {
1640         detach();
1641         memmove(d->data() + pos, d->data() + pos + len,
1642                 (d->size - pos - len + 1) * sizeof(ushort));
1643         d->size -= len;
1644     }
1645     return *this;
1646 }
1647
1648 /*!
1649   Removes every occurrence of the given \a str string in this
1650   string, and returns a reference to this string.
1651
1652   If \a cs is Qt::CaseSensitive (default), the search is
1653   case sensitive; otherwise the search is case insensitive.
1654
1655   This is the same as \c replace(str, "", cs).
1656
1657   \sa replace()
1658 */
1659 QString &QString::remove(const QString &str, Qt::CaseSensitivity cs)
1660 {
1661     if (str.d->size) {
1662         int i = 0;
1663         while ((i = indexOf(str, i, cs)) != -1)
1664             remove(i, str.d->size);
1665     }
1666     return *this;
1667 }
1668
1669 /*!
1670   Removes every occurrence of the character \a ch in this string, and
1671   returns a reference to this string.
1672
1673   If \a cs is Qt::CaseSensitive (default), the search is case
1674   sensitive; otherwise the search is case insensitive.
1675
1676   Example:
1677
1678   \snippet qstring/main.cpp 38
1679
1680   This is the same as \c replace(ch, "", cs).
1681
1682   \sa replace()
1683 */
1684 QString &QString::remove(QChar ch, Qt::CaseSensitivity cs)
1685 {
1686     int i = 0;
1687     ushort c = ch.unicode();
1688     if (cs == Qt::CaseSensitive) {
1689         while (i < d->size)
1690             if (d->data()[i] == ch)
1691                 remove(i, 1);
1692             else
1693                 i++;
1694     } else {
1695         c = foldCase(c);
1696         while (i < d->size)
1697             if (foldCase(d->data()[i]) == c)
1698                 remove(i, 1);
1699             else
1700                 i++;
1701     }
1702     return *this;
1703 }
1704
1705 /*!
1706   \fn QString &QString::remove(const QRegExp &rx)
1707
1708   Removes every occurrence of the regular expression \a rx in the
1709   string, and returns a reference to the string. For example:
1710
1711   \snippet qstring/main.cpp 39
1712
1713   \sa indexOf(), lastIndexOf(), replace()
1714 */
1715
1716 /*!
1717   \fn QString &QString::remove(const QRegularExpression &re)
1718   \since 5.0
1719
1720   Removes every occurrence of the regular expression \a re in the
1721   string, and returns a reference to the string. For example:
1722
1723   \snippet qstring/main.cpp 96
1724
1725   \sa indexOf(), lastIndexOf(), replace()
1726 */
1727
1728 /*!
1729   \fn QString &QString::replace(int position, int n, const QString &after)
1730
1731   Replaces \a n characters beginning at index \a position with
1732   the string \a after and returns a reference to this string.
1733
1734   Example:
1735
1736   \snippet qstring/main.cpp 40
1737
1738   \sa insert(), remove()
1739 */
1740 QString &QString::replace(int pos, int len, const QString &after)
1741 {
1742     QString copy = after;
1743     return replace(pos, len, copy.constData(), copy.length());
1744 }
1745
1746 /*!
1747   \fn QString &QString::replace(int position, int n, const QChar *unicode, int size)
1748   \overload replace()
1749   Replaces \a n characters beginning at index \a position with the
1750   first \a size characters of the QChar array \a unicode and returns a
1751   reference to this string.
1752 */
1753 QString &QString::replace(int pos, int len, const QChar *unicode, int size)
1754 {
1755     if (pos < 0 || pos > d->size)
1756         return *this;
1757     if (pos + len > d->size)
1758         len = d->size - pos;
1759
1760     uint index = pos;
1761     replace_helper(&index, 1, len, unicode, size);
1762     return *this;
1763 }
1764
1765 /*!
1766   \fn QString &QString::replace(int position, int n, QChar after)
1767   \overload replace()
1768
1769   Replaces \a n characters beginning at index \a position with the
1770   character \a after and returns a reference to this string.
1771 */
1772 QString &QString::replace(int pos, int len, QChar after)
1773 {
1774     return replace(pos, len, &after, 1);
1775 }
1776
1777 /*!
1778   \overload replace()
1779   Replaces every occurrence of the string \a before with the string \a
1780   after and returns a reference to this string.
1781
1782   If \a cs is Qt::CaseSensitive (default), the search is case
1783   sensitive; otherwise the search is case insensitive.
1784
1785   Example:
1786
1787   \snippet qstring/main.cpp 41
1788
1789   \note The replacement text is not rescanned after it is inserted.
1790
1791   Example:
1792
1793   \snippet qstring/main.cpp 86
1794 */
1795 QString &QString::replace(const QString &before, const QString &after, Qt::CaseSensitivity cs)
1796 {
1797     return replace(before.constData(), before.size(), after.constData(), after.size(), cs);
1798 }
1799
1800 /*!
1801   \internal
1802  */
1803 void QString::replace_helper(uint *indices, int nIndices, int blen, const QChar *after, int alen)
1804 {
1805     // copy *after in case it lies inside our own d->data() area
1806     // (which we could possibly invalidate via a realloc or corrupt via memcpy operations.)
1807     QChar *afterBuffer = const_cast<QChar *>(after);
1808     if (after >= reinterpret_cast<QChar *>(d->data()) && after < reinterpret_cast<QChar *>(d->data()) + d->size) {
1809         afterBuffer = static_cast<QChar *>(::malloc(alen*sizeof(QChar)));
1810         Q_CHECK_PTR(afterBuffer);
1811         ::memcpy(afterBuffer, after, alen*sizeof(QChar));
1812     }
1813
1814     QT_TRY {
1815         if (blen == alen) {
1816             // replace in place
1817             detach();
1818             for (int i = 0; i < nIndices; ++i)
1819                 memcpy(d->data() + indices[i], afterBuffer, alen * sizeof(QChar));
1820         } else if (alen < blen) {
1821             // replace from front
1822             detach();
1823             uint to = indices[0];
1824             if (alen)
1825                 memcpy(d->data()+to, after, alen*sizeof(QChar));
1826             to += alen;
1827             uint movestart = indices[0] + blen;
1828             for (int i = 1; i < nIndices; ++i) {
1829                 int msize = indices[i] - movestart;
1830                 if (msize > 0) {
1831                     memmove(d->data() + to, d->data() + movestart, msize * sizeof(QChar));
1832                     to += msize;
1833                 }
1834                 if (alen) {
1835                     memcpy(d->data() + to, afterBuffer, alen*sizeof(QChar));
1836                     to += alen;
1837                 }
1838                 movestart = indices[i] + blen;
1839             }
1840             int msize = d->size - movestart;
1841             if (msize > 0)
1842                 memmove(d->data() + to, d->data() + movestart, msize * sizeof(QChar));
1843             resize(d->size - nIndices*(blen-alen));
1844         } else {
1845             // replace from back
1846             int adjust = nIndices*(alen-blen);
1847             int newLen = d->size + adjust;
1848             int moveend = d->size;
1849             resize(newLen);
1850
1851             while (nIndices) {
1852                 --nIndices;
1853                 int movestart = indices[nIndices] + blen;
1854                 int insertstart = indices[nIndices] + nIndices*(alen-blen);
1855                 int moveto = insertstart + alen;
1856                 memmove(d->data() + moveto, d->data() + movestart,
1857                         (moveend - movestart)*sizeof(QChar));
1858                 memcpy(d->data() + insertstart, afterBuffer, alen*sizeof(QChar));
1859                 moveend = movestart-blen;
1860             }
1861         }
1862     } QT_CATCH(const std::bad_alloc &) {
1863         if (afterBuffer != after)
1864             ::free(afterBuffer);
1865         QT_RETHROW;
1866     }
1867     if (afterBuffer != after)
1868         ::free(afterBuffer);
1869 }
1870
1871 /*!
1872   \since 4.5
1873   \overload replace()
1874
1875   Replaces each occurrence in this string of the first \a blen
1876   characters of \a before with the first \a alen characters of \a
1877   after and returns a reference to this string.
1878
1879   If \a cs is Qt::CaseSensitive (default), the search is case
1880   sensitive; otherwise the search is case insensitive.
1881 */
1882 QString &QString::replace(const QChar *before, int blen,
1883                           const QChar *after, int alen,
1884                           Qt::CaseSensitivity cs)
1885 {
1886     if (d->size == 0) {
1887         if (blen)
1888             return *this;
1889     } else {
1890         if (cs == Qt::CaseSensitive && before == after && blen == alen)
1891             return *this;
1892     }
1893     if (alen == 0 && blen == 0)
1894         return *this;
1895
1896     QStringMatcher matcher(before, blen, cs);
1897
1898     int index = 0;
1899     while (1) {
1900         uint indices[1024];
1901         uint pos = 0;
1902         while (pos < 1023) {
1903             index = matcher.indexIn(*this, index);
1904             if (index == -1)
1905                 break;
1906             indices[pos++] = index;
1907             index += blen;
1908             // avoid infinite loop
1909             if (!blen)
1910                 index++;
1911         }
1912         if (!pos)
1913             break;
1914
1915         replace_helper(indices, pos, blen, after, alen);
1916
1917         if (index == -1)
1918             break;
1919         // index has to be adjusted in case we get back into the loop above.
1920         index += pos*(alen-blen);
1921     }
1922
1923     return *this;
1924 }
1925
1926 /*!
1927   \overload replace()
1928   Replaces every occurrence of the character \a ch in the string with
1929   \a after and returns a reference to this string.
1930
1931   If \a cs is Qt::CaseSensitive (default), the search is case
1932   sensitive; otherwise the search is case insensitive.
1933 */
1934 QString& QString::replace(QChar ch, const QString &after, Qt::CaseSensitivity cs)
1935 {
1936     if (after.d->size == 0)
1937         return remove(ch, cs);
1938
1939     if (after.d->size == 1)
1940         return replace(ch, after.d->data()[0], cs);
1941
1942     if (d->size == 0)
1943         return *this;
1944
1945     ushort cc = (cs == Qt::CaseSensitive ? ch.unicode() : ch.toCaseFolded().unicode());
1946
1947     int index = 0;
1948     while (1) {
1949         uint indices[1024];
1950         uint pos = 0;
1951         if (cs == Qt::CaseSensitive) {
1952             while (pos < 1023 && index < d->size) {
1953                 if (d->data()[index] == cc)
1954                     indices[pos++] = index;
1955                 index++;
1956             }
1957         } else {
1958             while (pos < 1023 && index < d->size) {
1959                 if (QChar::toCaseFolded(d->data()[index]) == cc)
1960                     indices[pos++] = index;
1961                 index++;
1962             }
1963         }
1964         if (!pos)
1965             break;
1966
1967         replace_helper(indices, pos, 1, after.constData(), after.d->size);
1968
1969         if (index == -1)
1970             break;
1971         // index has to be adjusted in case we get back into the loop above.
1972         index += pos*(after.d->size - 1);
1973     }
1974     return *this;
1975 }
1976
1977 /*!
1978   \overload replace()
1979   Replaces every occurrence of the character \a before with the
1980   character \a after and returns a reference to this string.
1981
1982   If \a cs is Qt::CaseSensitive (default), the search is case
1983   sensitive; otherwise the search is case insensitive.
1984 */
1985 QString& QString::replace(QChar before, QChar after, Qt::CaseSensitivity cs)
1986 {
1987     ushort a = after.unicode();
1988     ushort b = before.unicode();
1989     if (d->size) {
1990         detach();
1991         ushort *i = d->data();
1992         const ushort *e = i + d->size;
1993         if (cs == Qt::CaseSensitive) {
1994             for (; i != e; ++i)
1995                 if (*i == b)
1996                     *i = a;
1997         } else {
1998             b = foldCase(b);
1999             for (; i != e; ++i)
2000                 if (foldCase(*i) == b)
2001                     *i = a;
2002         }
2003     }
2004     return *this;
2005 }
2006
2007 /*!
2008   \since 4.5
2009   \overload replace()
2010
2011   Replaces every occurrence of the string \a before with the string \a
2012   after and returns a reference to this string.
2013
2014   If \a cs is Qt::CaseSensitive (default), the search is case
2015   sensitive; otherwise the search is case insensitive.
2016
2017   \note The text is not rescanned after a replacement.
2018 */
2019 QString &QString::replace(QLatin1String before, QLatin1String after, Qt::CaseSensitivity cs)
2020 {
2021     int alen = after.size();
2022     QVarLengthArray<ushort> a(alen);
2023     for (int i = 0; i < alen; ++i)
2024         a[i] = (uchar)after.latin1()[i];
2025     int blen = before.size();
2026     QVarLengthArray<ushort> b(blen);
2027     for (int i = 0; i < blen; ++i)
2028         b[i] = (uchar)before.latin1()[i];
2029     return replace((const QChar *)b.data(), blen, (const QChar *)a.data(), alen, cs);
2030 }
2031
2032 /*!
2033   \since 4.5
2034   \overload replace()
2035
2036   Replaces every occurrence of the string \a before with the string \a
2037   after and returns a reference to this string.
2038
2039   If \a cs is Qt::CaseSensitive (default), the search is case
2040   sensitive; otherwise the search is case insensitive.
2041
2042   \note The text is not rescanned after a replacement.
2043 */
2044 QString &QString::replace(QLatin1String before, const QString &after, Qt::CaseSensitivity cs)
2045 {
2046     int blen = before.size();
2047     QVarLengthArray<ushort> b(blen);
2048     for (int i = 0; i < blen; ++i)
2049         b[i] = (uchar)before.latin1()[i];
2050     return replace((const QChar *)b.data(), blen, after.constData(), after.d->size, cs);
2051 }
2052
2053 /*!
2054   \since 4.5
2055   \overload replace()
2056
2057   Replaces every occurrence of the string \a before with the string \a
2058   after and returns a reference to this string.
2059
2060   If \a cs is Qt::CaseSensitive (default), the search is case
2061   sensitive; otherwise the search is case insensitive.
2062
2063   \note The text is not rescanned after a replacement.
2064 */
2065 QString &QString::replace(const QString &before, QLatin1String after, Qt::CaseSensitivity cs)
2066 {
2067     int alen = after.size();
2068     QVarLengthArray<ushort> a(alen);
2069     for (int i = 0; i < alen; ++i)
2070         a[i] = (uchar)after.latin1()[i];
2071     return replace(before.constData(), before.d->size, (const QChar *)a.data(), alen, cs);
2072 }
2073
2074 /*!
2075   \since 4.5
2076   \overload replace()
2077
2078   Replaces every occurrence of the character \a c with the string \a
2079   after and returns a reference to this string.
2080
2081   If \a cs is Qt::CaseSensitive (default), the search is case
2082   sensitive; otherwise the search is case insensitive.
2083
2084   \note The text is not rescanned after a replacement.
2085 */
2086 QString &QString::replace(QChar c, QLatin1String after, Qt::CaseSensitivity cs)
2087 {
2088     int alen = after.size();
2089     QVarLengthArray<ushort> a(alen);
2090     for (int i = 0; i < alen; ++i)
2091         a[i] = (uchar)after.latin1()[i];
2092     return replace(&c, 1, (const QChar *)a.data(), alen, cs);
2093 }
2094
2095
2096 /*!
2097   \relates QString
2098   Returns true if string \a s1 is equal to string \a s2; otherwise
2099   returns false.
2100
2101   The comparison is based exclusively on the numeric Unicode values of
2102   the characters and is very fast, but is not what a human would
2103   expect. Consider sorting user-interface strings with
2104   localeAwareCompare().
2105 */
2106 bool operator==(const QString &s1, const QString &s2)
2107 {
2108     if (s1.d->size != s2.d->size)
2109         return false;
2110
2111     return qMemEquals(s1.d->data(), s2.d->data(), s1.d->size);
2112 }
2113
2114 /*!
2115     \overload operator==()
2116 */
2117 bool QString::operator==(QLatin1String other) const
2118 {
2119     if (d->size != other.size())
2120         return false;
2121
2122     if (!other.size())
2123         return isEmpty();
2124
2125     const ushort *uc = d->data();
2126     const ushort *e = uc + d->size;
2127     const uchar *c = (uchar *)other.latin1();
2128
2129     while (uc < e) {
2130         if (*uc != *c)
2131             return false;
2132         ++uc;
2133         ++c;
2134     }
2135     return true;
2136 }
2137
2138 /*! \fn bool QString::operator==(const QByteArray &other) const
2139
2140     \overload operator==()
2141
2142     The \a other byte array is converted to a QString using the
2143     fromUtf8() function. This function stops conversion at the
2144     first NUL character found, or the end of the byte array.
2145
2146     You can disable this operator by defining \c
2147     QT_NO_CAST_FROM_ASCII when you compile your applications. This
2148     can be useful if you want to ensure that all user-visible strings
2149     go through QObject::tr(), for example.
2150 */
2151
2152 /*! \fn bool QString::operator==(const char *other) const
2153
2154     \overload operator==()
2155
2156     The \a other const char pointer is converted to a QString using
2157     the fromUtf8() function.
2158
2159     You can disable this operator by defining \c
2160     QT_NO_CAST_FROM_ASCII when you compile your applications. This
2161     can be useful if you want to ensure that all user-visible strings
2162     go through QObject::tr(), for example.
2163 */
2164
2165 /*!
2166    \relates QString
2167     Returns true if string \a s1 is lexically less than string
2168     \a s2; otherwise returns false.
2169
2170     The comparison is based exclusively on the numeric Unicode values
2171     of the characters and is very fast, but is not what a human would
2172     expect. Consider sorting user-interface strings using the
2173     QString::localeAwareCompare() function.
2174 */
2175 bool operator<(const QString &s1, const QString &s2)
2176 {
2177     return ucstrcmp(s1.constData(), s1.length(), s2.constData(), s2.length()) < 0;
2178 }
2179
2180 /*!
2181     \overload operator<()
2182 */
2183 bool QString::operator<(QLatin1String other) const
2184 {
2185     const uchar *c = (uchar *) other.latin1();
2186     if (!c || *c == 0)
2187         return false;
2188
2189     const ushort *uc = d->data();
2190     const ushort *e = uc + qMin(d->size, other.size());
2191
2192     while (uc < e) {
2193         if (*uc != *c)
2194             break;
2195         ++uc;
2196         ++c;
2197     }
2198     return (uc == e ? d->size < other.size() : *uc < *c);
2199 }
2200
2201 /*! \fn bool QString::operator<(const QByteArray &other) const
2202
2203     \overload operator<()
2204
2205     The \a other byte array is converted to a QString using the
2206     fromUtf8() function. If any NUL characters ('\\0') are embedded
2207     in the byte array, they will be included in the transformation.
2208
2209     You can disable this operator by defining \c
2210     QT_NO_CAST_FROM_ASCII when you compile your applications. This
2211     can be useful if you want to ensure that all user-visible strings
2212     go through QObject::tr(), for example.
2213 */
2214
2215 /*! \fn bool QString::operator<(const char *other) const
2216
2217     \overload operator<()
2218
2219     The \a other const char pointer is converted to a QString using
2220     the fromUtf8() function.
2221
2222     You can disable this operator by defining \c
2223     QT_NO_CAST_FROM_ASCII when you compile your applications. This
2224     can be useful if you want to ensure that all user-visible strings
2225     go through QObject::tr(), for example.
2226 */
2227
2228 /*! \fn bool QString::operator<=(const QString &s1, const QString &s2)
2229
2230     Returns true if string \a s1 is lexically less than or equal to
2231     string \a s2; otherwise returns false.
2232
2233     The comparison is based exclusively on the numeric Unicode values
2234     of the characters and is very fast, but is not what a human would
2235     expect. Consider sorting user-interface strings with
2236     localeAwareCompare().
2237 */
2238
2239 /*! \fn bool QString::operator<=(QLatin1String other) const
2240
2241     \overload operator<=()
2242 */
2243
2244 /*! \fn bool QString::operator<=(const QByteArray &other) const
2245
2246     \overload operator<=()
2247
2248     The \a other byte array is converted to a QString using the
2249     fromUtf8() function. If any NUL characters ('\\0') are embedded
2250     in the byte array, they will be included in the transformation.
2251
2252     You can disable this operator by defining \c
2253     QT_NO_CAST_FROM_ASCII when you compile your applications. This
2254     can be useful if you want to ensure that all user-visible strings
2255     go through QObject::tr(), for example.
2256 */
2257
2258 /*! \fn bool QString::operator<=(const char *other) const
2259
2260     \overload operator<=()
2261
2262     The \a other const char pointer is converted to a QString using
2263     the fromUtf8() function.
2264
2265     You can disable this operator by defining \c
2266     QT_NO_CAST_FROM_ASCII when you compile your applications. This
2267     can be useful if you want to ensure that all user-visible strings
2268     go through QObject::tr(), for example.
2269 */
2270
2271 /*! \fn bool QString::operator>(const QString &s1, const QString &s2)
2272
2273     Returns true if string \a s1 is lexically greater than string \a
2274     s2; otherwise returns false.
2275
2276     The comparison is based exclusively on the numeric Unicode values
2277     of the characters and is very fast, but is not what a human would
2278     expect. Consider sorting user-interface strings with
2279     localeAwareCompare().
2280 */
2281
2282 /*!
2283     \overload operator>()
2284 */
2285 bool QString::operator>(QLatin1String other) const
2286 {
2287     const uchar *c = (uchar *) other.latin1();
2288     if (!c || *c == '\0')
2289         return !isEmpty();
2290
2291     const ushort *uc = d->data();
2292     const ushort *e = uc + qMin(d->size, other.size());
2293
2294     while (uc < e) {
2295         if (*uc != *c)
2296             break;
2297         ++uc;
2298         ++c;
2299     }
2300     return (uc == e) ? d->size > other.size() : *uc > *c;
2301 }
2302
2303 /*! \fn bool QString::operator>(const QByteArray &other) const
2304
2305     \overload operator>()
2306
2307     The \a other byte array is converted to a QString using the
2308     fromUtf8() function. If any NUL characters ('\\0') are embedded
2309     in the byte array, they will be included in the transformation.
2310
2311     You can disable this operator by defining \c
2312     QT_NO_CAST_FROM_ASCII when you compile your applications. This
2313     can be useful if you want to ensure that all user-visible strings
2314     go through QObject::tr(), for example.
2315 */
2316
2317 /*! \fn bool QString::operator>(const char *other) const
2318
2319     \overload operator>()
2320
2321     The \a other const char pointer is converted to a QString using
2322     the fromUtf8() function.
2323
2324     You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII
2325     when you compile your applications. This can be useful if you want
2326     to ensure that all user-visible strings go through QObject::tr(),
2327     for example.
2328 */
2329
2330 /*! \fn bool operator>=(const QString &s1, const QString &s2)
2331     \relates QString
2332
2333     Returns true if string \a s1 is lexically greater than or equal to
2334     string \a s2; otherwise returns false.
2335
2336     The comparison is based exclusively on the numeric Unicode values
2337     of the characters and is very fast, but is not what a human would
2338     expect. Consider sorting user-interface strings with
2339     localeAwareCompare().
2340 */
2341
2342 /*! \fn bool QString::operator>=(QLatin1String other) const
2343
2344     \overload operator>=()
2345 */
2346
2347 /*! \fn bool QString::operator>=(const QByteArray &other) const
2348
2349     \overload operator>=()
2350
2351     The \a other byte array is converted to a QString using the
2352     fromUtf8() function. If any NUL characters ('\\0') are embedded in
2353     the byte array, they will be included in the transformation.
2354
2355     You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII
2356     when you compile your applications. This can be useful if you want
2357     to ensure that all user-visible strings go through QObject::tr(),
2358     for example.
2359 */
2360
2361 /*! \fn bool QString::operator>=(const char *other) const
2362
2363     \overload operator>=()
2364
2365     The \a other const char pointer is converted to a QString using
2366     the fromUtf8() function.
2367
2368     You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII
2369     when you compile your applications. This can be useful if you want
2370     to ensure that all user-visible strings go through QObject::tr(),
2371     for example.
2372 */
2373
2374 /*! \fn bool operator!=(const QString &s1, const QString &s2)
2375     \relates QString
2376
2377     Returns true if string \a s1 is not equal to string \a s2;
2378     otherwise returns false.
2379
2380     The comparison is based exclusively on the numeric Unicode values
2381     of the characters and is very fast, but is not what a human would
2382     expect. Consider sorting user-interface strings with
2383     localeAwareCompare().
2384 */
2385
2386 /*! \fn bool QString::operator!=(QLatin1String other) const
2387
2388     \overload operator!=()
2389 */
2390
2391 /*! \fn bool QString::operator!=(const QByteArray &other) const
2392
2393     \overload operator!=()
2394
2395     The \a other byte array is converted to a QString using the
2396     fromUtf8() function. If any NUL characters ('\\0') are embedded
2397     in the byte array, they will be included in the transformation.
2398
2399     You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII
2400     when you compile your applications. This can be useful if you want
2401     to ensure that all user-visible strings go through QObject::tr(),
2402     for example.
2403 */
2404
2405 /*! \fn bool QString::operator!=(const char *other) const
2406
2407     \overload operator!=()
2408
2409     The \a other const char pointer is converted to a QString using
2410     the fromUtf8() function.
2411
2412     You can disable this operator by defining \c
2413     QT_NO_CAST_FROM_ASCII when you compile your applications. This
2414     can be useful if you want to ensure that all user-visible strings
2415     go through QObject::tr(), for example.
2416 */
2417
2418 /*!
2419   Returns the index position of the first occurrence of the string \a
2420   str in this string, searching forward from index position \a
2421   from. Returns -1 if \a str is not found.
2422
2423   If \a cs is Qt::CaseSensitive (default), the search is case
2424   sensitive; otherwise the search is case insensitive.
2425
2426   Example:
2427
2428   \snippet qstring/main.cpp 24
2429
2430   If \a from is -1, the search starts at the last character; if it is
2431   -2, at the next to last character and so on.
2432
2433   \sa lastIndexOf(), contains(), count()
2434 */
2435 int QString::indexOf(const QString &str, int from, Qt::CaseSensitivity cs) const
2436 {
2437     return qFindString(unicode(), length(), from, str.unicode(), str.length(), cs);
2438 }
2439
2440 /*!
2441   \since 4.5
2442   Returns the index position of the first occurrence of the string \a
2443   str in this string, searching forward from index position \a
2444   from. Returns -1 if \a str is not found.
2445
2446   If \a cs is Qt::CaseSensitive (default), the search is case
2447   sensitive; otherwise the search is case insensitive.
2448
2449   Example:
2450
2451   \snippet qstring/main.cpp 24
2452
2453   If \a from is -1, the search starts at the last character; if it is
2454   -2, at the next to last character and so on.
2455
2456   \sa lastIndexOf(), contains(), count()
2457 */
2458
2459 int QString::indexOf(QLatin1String str, int from, Qt::CaseSensitivity cs) const
2460 {
2461     return qt_find_latin1_string(unicode(), size(), str, from, cs);
2462 }
2463
2464 int qFindString(
2465     const QChar *haystack0, int haystackLen, int from,
2466     const QChar *needle0, int needleLen, Qt::CaseSensitivity cs)
2467 {
2468     const int l = haystackLen;
2469     const int sl = needleLen;
2470     if (from < 0)
2471         from += l;
2472     if (uint(sl + from) > (uint)l)
2473         return -1;
2474     if (!sl)
2475         return from;
2476     if (!l)
2477         return -1;
2478
2479     if (sl == 1)
2480         return findChar(haystack0, haystackLen, needle0[0], from, cs);
2481
2482     /*
2483         We use the Boyer-Moore algorithm in cases where the overhead
2484         for the skip table should pay off, otherwise we use a simple
2485         hash function.
2486     */
2487     if (l > 500 && sl > 5)
2488         return qFindStringBoyerMoore(haystack0, haystackLen, from,
2489             needle0, needleLen, cs);
2490
2491     /*
2492         We use some hashing for efficiency's sake. Instead of
2493         comparing strings, we compare the hash value of str with that
2494         of a part of this QString. Only if that matches, we call
2495         ucstrncmp() or ucstrnicmp().
2496     */
2497     const ushort *needle = (const ushort *)needle0;
2498     const ushort *haystack = (const ushort *)haystack0 + from;
2499     const ushort *end = (const ushort *)haystack0 + (l-sl);
2500     const int sl_minus_1 = sl-1;
2501     int hashNeedle = 0, hashHaystack = 0, idx;
2502
2503     if (cs == Qt::CaseSensitive) {
2504         for (idx = 0; idx < sl; ++idx) {
2505             hashNeedle = ((hashNeedle<<1) + needle[idx]);
2506             hashHaystack = ((hashHaystack<<1) + haystack[idx]);
2507         }
2508         hashHaystack -= haystack[sl_minus_1];
2509
2510         while (haystack <= end) {
2511             hashHaystack += haystack[sl_minus_1];
2512             if (hashHaystack == hashNeedle
2513                  && ucstrncmp((const QChar *)needle, (const QChar *)haystack, sl) == 0)
2514                 return haystack - (const ushort *)haystack0;
2515
2516             REHASH(*haystack);
2517             ++haystack;
2518         }
2519     } else {
2520         const ushort *haystack_start = (const ushort *)haystack0;
2521         for (idx = 0; idx < sl; ++idx) {
2522             hashNeedle = (hashNeedle<<1) + foldCase(needle + idx, needle);
2523             hashHaystack = (hashHaystack<<1) + foldCase(haystack + idx, haystack_start);
2524         }
2525         hashHaystack -= foldCase(haystack + sl_minus_1, haystack_start);
2526
2527         while (haystack <= end) {
2528             hashHaystack += foldCase(haystack + sl_minus_1, haystack_start);
2529             if (hashHaystack == hashNeedle && ucstrnicmp(needle, haystack, sl) == 0)
2530                 return haystack - (const ushort *)haystack0;
2531
2532             REHASH(foldCase(haystack, haystack_start));
2533             ++haystack;
2534         }
2535     }
2536     return -1;
2537 }
2538
2539 /*!
2540     \overload indexOf()
2541
2542     Returns the index position of the first occurrence of the
2543     character \a ch in the string, searching forward from index
2544     position \a from. Returns -1 if \a ch could not be found.
2545 */
2546 int QString::indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
2547 {
2548     return findChar(unicode(), length(), ch, from, cs);
2549 }
2550
2551 /*!
2552     \since 4.8
2553
2554     \overload indexOf()
2555
2556     Returns the index position of the first occurrence of the string
2557     reference \a str in this string, searching forward from index
2558     position \a from. Returns -1 if \a str is not found.
2559
2560     If \a cs is Qt::CaseSensitive (default), the search is case
2561     sensitive; otherwise the search is case insensitive.
2562 */
2563 int QString::indexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) const
2564 {
2565     return qFindString(unicode(), length(), from, str.unicode(), str.length(), cs);
2566 }
2567
2568 static int lastIndexOfHelper(const ushort *haystack, int from, const ushort *needle, int sl, Qt::CaseSensitivity cs)
2569 {
2570     /*
2571         See indexOf() for explanations.
2572     */
2573
2574     const ushort *end = haystack;
2575     haystack += from;
2576     const int sl_minus_1 = sl-1;
2577     const ushort *n = needle+sl_minus_1;
2578     const ushort *h = haystack+sl_minus_1;
2579     int hashNeedle = 0, hashHaystack = 0, idx;
2580
2581     if (cs == Qt::CaseSensitive) {
2582         for (idx = 0; idx < sl; ++idx) {
2583             hashNeedle = ((hashNeedle<<1) + *(n-idx));
2584             hashHaystack = ((hashHaystack<<1) + *(h-idx));
2585         }
2586         hashHaystack -= *haystack;
2587
2588         while (haystack >= end) {
2589             hashHaystack += *haystack;
2590             if (hashHaystack == hashNeedle
2591                  && ucstrncmp((const QChar *)needle, (const QChar *)haystack, sl) == 0)
2592                 return haystack - end;
2593             --haystack;
2594             REHASH(haystack[sl]);
2595         }
2596     } else {
2597         for (idx = 0; idx < sl; ++idx) {
2598             hashNeedle = ((hashNeedle<<1) + foldCase(n-idx, needle));
2599             hashHaystack = ((hashHaystack<<1) + foldCase(h-idx, end));
2600         }
2601         hashHaystack -= foldCase(haystack, end);
2602
2603         while (haystack >= end) {
2604             hashHaystack += foldCase(haystack, end);
2605             if (hashHaystack == hashNeedle && ucstrnicmp(needle, haystack, sl) == 0)
2606                 return haystack - end;
2607             --haystack;
2608             REHASH(foldCase(haystack + sl, end));
2609         }
2610     }
2611     return -1;
2612 }
2613
2614 /*!
2615   Returns the index position of the last occurrence of the string \a
2616   str in this string, searching backward from index position \a
2617   from. If \a from is -1 (default), the search starts at the last
2618   character; if \a from is -2, at the next to last character and so
2619   on. Returns -1 if \a str is not found.
2620
2621   If \a cs is Qt::CaseSensitive (default), the search is case
2622   sensitive; otherwise the search is case insensitive.
2623
2624   Example:
2625
2626   \snippet qstring/main.cpp 29
2627
2628   \sa indexOf(), contains(), count()
2629 */
2630 int QString::lastIndexOf(const QString &str, int from, Qt::CaseSensitivity cs) const
2631 {
2632     const int sl = str.d->size;
2633     if (sl == 1)
2634         return lastIndexOf(QChar(str.d->data()[0]), from, cs);
2635
2636     const int l = d->size;
2637     if (from < 0)
2638         from += l;
2639     int delta = l-sl;
2640     if (from == l && sl == 0)
2641         return from;
2642     if (from < 0 || from >= l || delta < 0)
2643         return -1;
2644     if (from > delta)
2645         from = delta;
2646
2647     return lastIndexOfHelper(d->data(), from, str.d->data(), str.d->size, cs);
2648 }
2649
2650 /*!
2651   \since 4.5
2652   \overload lastIndexOf()
2653
2654   Returns the index position of the last occurrence of the string \a
2655   str in this string, searching backward from index position \a
2656   from. If \a from is -1 (default), the search starts at the last
2657   character; if \a from is -2, at the next to last character and so
2658   on. Returns -1 if \a str is not found.
2659
2660   If \a cs is Qt::CaseSensitive (default), the search is case
2661   sensitive; otherwise the search is case insensitive.
2662
2663   Example:
2664
2665   \snippet qstring/main.cpp 29
2666
2667   \sa indexOf(), contains(), count()
2668 */
2669 int QString::lastIndexOf(QLatin1String str, int from, Qt::CaseSensitivity cs) const
2670 {
2671     const int sl = str.size();
2672     if (sl == 1)
2673         return lastIndexOf(QLatin1Char(str.latin1()[0]), from, cs);
2674
2675     const int l = d->size;
2676     if (from < 0)
2677         from += l;
2678     int delta = l-sl;
2679     if (from == l && sl == 0)
2680         return from;
2681     if (from < 0 || from >= l || delta < 0)
2682         return -1;
2683     if (from > delta)
2684         from = delta;
2685
2686     QVarLengthArray<ushort> s(sl);
2687     for (int i = 0; i < sl; ++i)
2688         s[i] = str.latin1()[i];
2689
2690     return lastIndexOfHelper(d->data(), from, s.data(), sl, cs);
2691 }
2692
2693 /*!
2694   \overload lastIndexOf()
2695
2696   Returns the index position of the last occurrence of the character
2697   \a ch, searching backward from position \a from.
2698 */
2699 int QString::lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
2700 {
2701     return qt_last_index_of(unicode(), size(), ch, from, cs);
2702 }
2703
2704 /*!
2705   \since 4.8
2706   \overload lastIndexOf()
2707
2708   Returns the index position of the last occurrence of the string
2709   reference \a str in this string, searching backward from index
2710   position \a from. If \a from is -1 (default), the search starts at
2711   the last character; if \a from is -2, at the next to last character
2712   and so on. Returns -1 if \a str is not found.
2713
2714   If \a cs is Qt::CaseSensitive (default), the search is case
2715   sensitive; otherwise the search is case insensitive.
2716
2717   \sa indexOf(), contains(), count()
2718 */
2719 int QString::lastIndexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) const
2720 {
2721     const int sl = str.size();
2722     if (sl == 1)
2723         return lastIndexOf(str.at(0), from, cs);
2724
2725     const int l = d->size;
2726     if (from < 0)
2727         from += l;
2728     int delta = l - sl;
2729     if (from == l && sl == 0)
2730         return from;
2731     if (from < 0 || from >= l || delta < 0)
2732     return -1;
2733     if (from > delta)
2734         from = delta;
2735
2736     return lastIndexOfHelper(d->data(), from, reinterpret_cast<const ushort*>(str.unicode()),
2737                              str.size(), cs);
2738 }
2739
2740 #ifndef QT_NO_REGEXP
2741 struct QStringCapture
2742 {
2743     int pos;
2744     int len;
2745     int no;
2746 };
2747
2748 /*!
2749   \overload replace()
2750
2751   Replaces every occurrence of the regular expression \a rx in the
2752   string with \a after. Returns a reference to the string. For
2753   example:
2754
2755   \snippet qstring/main.cpp 42
2756
2757   For regular expressions containing \l{capturing parentheses},
2758   occurrences of \b{\\1}, \b{\\2}, ..., in \a after are replaced
2759   with \a{rx}.cap(1), cap(2), ...
2760
2761   \snippet qstring/main.cpp 43
2762
2763   \sa indexOf(), lastIndexOf(), remove(), QRegExp::cap()
2764 */
2765 QString& QString::replace(const QRegExp &rx, const QString &after)
2766 {
2767     QRegExp rx2(rx);
2768
2769     if (isEmpty() && rx2.indexIn(*this) == -1)
2770         return *this;
2771
2772     reallocData(uint(d->size) + 1u);
2773
2774     int index = 0;
2775     int numCaptures = rx2.captureCount();
2776     int al = after.length();
2777     QRegExp::CaretMode caretMode = QRegExp::CaretAtZero;
2778
2779     if (numCaptures > 0) {
2780         const QChar *uc = after.unicode();
2781         int numBackRefs = 0;
2782
2783         for (int i = 0; i < al - 1; i++) {
2784             if (uc[i] == QLatin1Char('\\')) {
2785                 int no = uc[i + 1].digitValue();
2786                 if (no > 0 && no <= numCaptures)
2787                     numBackRefs++;
2788             }
2789         }
2790
2791         /*
2792             This is the harder case where we have back-references.
2793         */
2794         if (numBackRefs > 0) {
2795             QVarLengthArray<QStringCapture, 16> captures(numBackRefs);
2796             int j = 0;
2797
2798             for (int i = 0; i < al - 1; i++) {
2799                 if (uc[i] == QLatin1Char('\\')) {
2800                     int no = uc[i + 1].digitValue();
2801                     if (no > 0 && no <= numCaptures) {
2802                         QStringCapture capture;
2803                         capture.pos = i;
2804                         capture.len = 2;
2805
2806                         if (i < al - 2) {
2807                             int secondDigit = uc[i + 2].digitValue();
2808                             if (secondDigit != -1 && ((no * 10) + secondDigit) <= numCaptures) {
2809                                 no = (no * 10) + secondDigit;
2810                                 ++capture.len;
2811                             }
2812                         }
2813
2814                         capture.no = no;
2815                         captures[j++] = capture;
2816                     }
2817                 }
2818             }
2819
2820             while (index <= length()) {
2821                 index = rx2.indexIn(*this, index, caretMode);
2822                 if (index == -1)
2823                     break;
2824
2825                 QString after2(after);
2826                 for (j = numBackRefs - 1; j >= 0; j--) {
2827                     const QStringCapture &capture = captures[j];
2828                     after2.replace(capture.pos, capture.len, rx2.cap(capture.no));
2829                 }
2830
2831                 replace(index, rx2.matchedLength(), after2);
2832                 index += after2.length();
2833
2834                 // avoid infinite loop on 0-length matches (e.g., QRegExp("[a-z]*"))
2835                 if (rx2.matchedLength() == 0)
2836                     ++index;
2837
2838                 caretMode = QRegExp::CaretWontMatch;
2839             }
2840             return *this;
2841         }
2842     }
2843
2844     /*
2845         This is the simple and optimized case where we don't have
2846         back-references.
2847     */
2848     while (index != -1) {
2849         struct {
2850             int pos;
2851             int length;
2852         } replacements[2048];
2853
2854         int pos = 0;
2855         int adjust = 0;
2856         while (pos < 2047) {
2857             index = rx2.indexIn(*this, index, caretMode);
2858             if (index == -1)
2859                 break;
2860             int ml = rx2.matchedLength();
2861             replacements[pos].pos = index;
2862             replacements[pos++].length = ml;
2863             index += ml;
2864             adjust += al - ml;
2865             // avoid infinite loop
2866             if (!ml)
2867                 index++;
2868         }
2869         if (!pos)
2870             break;
2871         replacements[pos].pos = d->size;
2872         int newlen = d->size + adjust;
2873
2874         // to continue searching at the right position after we did
2875         // the first round of replacements
2876         if (index != -1)
2877             index += adjust;
2878         QString newstring;
2879         newstring.reserve(newlen + 1);
2880         QChar *newuc = newstring.data();
2881         QChar *uc = newuc;
2882         int copystart = 0;
2883         int i = 0;
2884         while (i < pos) {
2885             int copyend = replacements[i].pos;
2886             int size = copyend - copystart;
2887             memcpy(uc, d->data() + copystart, size * sizeof(QChar));
2888             uc += size;
2889             memcpy(uc, after.d->data(), al * sizeof(QChar));
2890             uc += al;
2891             copystart = copyend + replacements[i].length;
2892             i++;
2893         }
2894         memcpy(uc, d->data() + copystart, (d->size - copystart) * sizeof(QChar));
2895         newstring.resize(newlen);
2896         *this = newstring;
2897         caretMode = QRegExp::CaretWontMatch;
2898     }
2899     return *this;
2900 }
2901 #endif
2902
2903 #ifndef QT_NO_REGEXP
2904 #ifndef QT_BOOTSTRAPPED
2905 /*!
2906   \overload replace()
2907   \since 5.0
2908
2909   Replaces every occurrence of the regular expression \a re in the
2910   string with \a after. Returns a reference to the string. For
2911   example:
2912
2913   \snippet qstring/main.cpp 87
2914
2915   For regular expressions containing capturing groups,
2916   occurrences of \b{\\1}, \b{\\2}, ..., in \a after are replaced
2917   with the string captured by the corresponding capturing group.
2918
2919   \snippet qstring/main.cpp 88
2920
2921   \sa indexOf(), lastIndexOf(), remove(), QRegularExpression, QRegularExpressionMatch
2922 */
2923 QString &QString::replace(const QRegularExpression &re, const QString &after)
2924 {
2925     if (!re.isValid()) {
2926         qWarning("QString::replace: invalid QRegularExpresssion object");
2927         return *this;
2928     }
2929
2930     const QString copy(*this);
2931     QRegularExpressionMatchIterator iterator = re.globalMatch(copy);
2932     if (!iterator.hasNext()) // no matches at all
2933         return *this;
2934
2935     reallocData(uint(d->size) + 1u);
2936
2937     int numCaptures = re.captureCount();
2938
2939     // 1. build the backreferences vector, holding where the backreferences
2940     // are in the replacement string
2941     QVector<QStringCapture> backReferences;
2942     const int al = after.length();
2943     const QChar *ac = after.unicode();
2944
2945     for (int i = 0; i < al - 1; i++) {
2946         if (ac[i] == QLatin1Char('\\')) {
2947             int no = ac[i + 1].digitValue();
2948             if (no > 0 && no <= numCaptures) {
2949                 QStringCapture backReference;
2950                 backReference.pos = i;
2951                 backReference.len = 2;
2952
2953                 if (i < al - 2) {
2954                     int secondDigit = ac[i + 2].digitValue();
2955                     if (secondDigit != -1 && ((no * 10) + secondDigit) <= numCaptures) {
2956                         no = (no * 10) + secondDigit;
2957                         ++backReference.len;
2958                     }
2959                 }
2960
2961                 backReference.no = no;
2962                 backReferences.append(backReference);
2963             }
2964         }
2965     }
2966
2967     // 2. iterate on the matches. For every match, copy in chunks
2968     // - the part before the match
2969     // - the after string, with the proper replacements for the backreferences
2970
2971     int newLength = 0; // length of the new string, with all the replacements
2972     int lastEnd = 0;
2973     QVector<QStringRef> chunks;
2974     while (iterator.hasNext()) {
2975         QRegularExpressionMatch match = iterator.next();
2976         int len;
2977         // add the part before the match
2978         len = match.capturedStart() - lastEnd;
2979         if (len > 0) {
2980             chunks << copy.midRef(lastEnd, len);
2981             newLength += len;
2982         }
2983
2984         lastEnd = 0;
2985         // add the after string, with replacements for the backreferences
2986         foreach (const QStringCapture &backReference, backReferences) {
2987             // part of "after" before the backreference
2988             len = backReference.pos - lastEnd;
2989             if (len > 0) {
2990                 chunks << after.midRef(lastEnd, len);
2991                 newLength += len;
2992             }
2993
2994             // backreference itself
2995             len = match.capturedLength(backReference.no);
2996             if (len > 0) {
2997                 chunks << copy.midRef(match.capturedStart(backReference.no), len);
2998                 newLength += len;
2999             }
3000
3001             lastEnd = backReference.pos + backReference.len;
3002         }
3003
3004         // add the last part of the after string
3005         len = after.length() - lastEnd;
3006         if (len > 0) {
3007             chunks << after.midRef(lastEnd, len);
3008             newLength += len;
3009         }
3010
3011         lastEnd = match.capturedEnd();
3012     }
3013
3014     // 3. trailing string after the last match
3015     if (copy.length() > lastEnd) {
3016         chunks << copy.midRef(lastEnd);
3017         newLength += copy.length() - lastEnd;
3018     }
3019
3020     // 4. assemble the chunks together
3021     resize(newLength);
3022     int i = 0;
3023     QChar *uc = data();
3024     foreach (const QStringRef &chunk, chunks) {
3025         int len = chunk.length();
3026         memcpy(uc + i, chunk.unicode(), len * sizeof(QChar));
3027         i += len;
3028     }
3029
3030     return *this;
3031 }
3032 #endif // QT_BOOTSTRAPPED
3033 #endif // QT_NO_REGEXP
3034
3035 /*!
3036     Returns the number of (potentially overlapping) occurrences of
3037     the string \a str in this string.
3038
3039     If \a cs is Qt::CaseSensitive (default), the search is
3040     case sensitive; otherwise the search is case insensitive.
3041
3042     \sa contains(), indexOf()
3043 */
3044
3045 int QString::count(const QString &str, Qt::CaseSensitivity cs) const
3046 {
3047     return qt_string_count(unicode(), size(), str.unicode(), str.size(), cs);
3048 }
3049
3050 /*!
3051   \overload count()
3052
3053   Returns the number of occurrences of character \a ch in the string.
3054 */
3055
3056 int QString::count(QChar ch, Qt::CaseSensitivity cs) const
3057 {
3058     return qt_string_count(unicode(), size(), ch, cs);
3059     }
3060
3061 /*!
3062     \since 4.8
3063     \overload count()
3064     Returns the number of (potentially overlapping) occurrences of the
3065     string reference \a str in this string.
3066
3067     If \a cs is Qt::CaseSensitive (default), the search is
3068     case sensitive; otherwise the search is case insensitive.
3069
3070     \sa contains(), indexOf()
3071 */
3072 int QString::count(const QStringRef &str, Qt::CaseSensitivity cs) const
3073 {
3074     return qt_string_count(unicode(), size(), str.unicode(), str.size(), cs);
3075 }
3076
3077
3078 /*! \fn bool QString::contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
3079
3080     Returns true if this string contains an occurrence of the string
3081     \a str; otherwise returns false.
3082
3083     If \a cs is Qt::CaseSensitive (default), the search is
3084     case sensitive; otherwise the search is case insensitive.
3085
3086     Example:
3087     \snippet qstring/main.cpp 17
3088
3089     \sa indexOf(), count()
3090 */
3091
3092 /*! \fn bool QString::contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
3093
3094     \overload contains()
3095
3096     Returns true if this string contains an occurrence of the
3097     character \a ch; otherwise returns false.
3098 */
3099
3100 /*! \fn bool QString::contains(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
3101     \since 4.8
3102
3103     Returns true if this string contains an occurrence of the string
3104     reference \a str; otherwise returns false.
3105
3106     If \a cs is Qt::CaseSensitive (default), the search is
3107     case sensitive; otherwise the search is case insensitive.
3108
3109     \sa indexOf(), count()
3110 */
3111
3112 /*! \fn bool QString::contains(const QRegExp &rx) const
3113
3114     \overload contains()
3115
3116     Returns true if the regular expression \a rx matches somewhere in
3117     this string; otherwise returns false.
3118 */
3119
3120 /*! \fn bool QString::contains(QRegExp &rx) const
3121     \overload contains()
3122     \since 4.5
3123
3124     Returns true if the regular expression \a rx matches somewhere in
3125     this string; otherwise returns false.
3126
3127     If there is a match, the \a rx regular expression will contain the
3128     matched captures (see QRegExp::matchedLength, QRegExp::cap).
3129 */
3130
3131 #ifndef QT_NO_REGEXP
3132 /*!
3133     \overload indexOf()
3134
3135     Returns the index position of the first match of the regular
3136     expression \a rx in the string, searching forward from index
3137     position \a from. Returns -1 if \a rx didn't match anywhere.
3138
3139     Example:
3140
3141     \snippet qstring/main.cpp 25
3142 */
3143 int QString::indexOf(const QRegExp& rx, int from) const
3144 {
3145     QRegExp rx2(rx);
3146     return rx2.indexIn(*this, from);
3147 }
3148
3149 /*!
3150     \overload indexOf()
3151     \since 4.5
3152
3153     Returns the index position of the first match of the regular
3154     expression \a rx in the string, searching forward from index
3155     position \a from. Returns -1 if \a rx didn't match anywhere.
3156
3157     If there is a match, the \a rx regular expression will contain the
3158     matched captures (see QRegExp::matchedLength, QRegExp::cap).
3159
3160     Example:
3161
3162     \snippet qstring/main.cpp 25
3163 */
3164 int QString::indexOf(QRegExp& rx, int from) const
3165 {
3166     return rx.indexIn(*this, from);
3167 }
3168
3169 /*!
3170     \overload lastIndexOf()
3171
3172     Returns the index position of the last match of the regular
3173     expression \a rx in the string, searching backward from index
3174     position \a from. Returns -1 if \a rx didn't match anywhere.
3175
3176     Example:
3177
3178     \snippet qstring/main.cpp 30
3179 */
3180 int QString::lastIndexOf(const QRegExp& rx, int from) const
3181 {
3182     QRegExp rx2(rx);
3183     return rx2.lastIndexIn(*this, from);
3184 }
3185
3186 /*!
3187     \overload lastIndexOf()
3188     \since 4.5
3189
3190     Returns the index position of the last match of the regular
3191     expression \a rx in the string, searching backward from index
3192     position \a from. Returns -1 if \a rx didn't match anywhere.
3193
3194     If there is a match, the \a rx regular expression will contain the
3195     matched captures (see QRegExp::matchedLength, QRegExp::cap).
3196
3197     Example:
3198
3199     \snippet qstring/main.cpp 30
3200 */
3201 int QString::lastIndexOf(QRegExp& rx, int from) const
3202 {
3203     return rx.lastIndexIn(*this, from);
3204 }
3205
3206 /*!
3207     \overload count()
3208
3209     Returns the number of times the regular expression \a rx matches
3210     in the string.
3211
3212     This function counts overlapping matches, so in the example
3213     below, there are four instances of "ana" or "ama":
3214
3215     \snippet qstring/main.cpp 18
3216
3217 */
3218 int QString::count(const QRegExp& rx) const
3219 {
3220     QRegExp rx2(rx);
3221     int count = 0;
3222     int index = -1;
3223     int len = length();
3224     while (index < len - 1) {                 // count overlapping matches
3225         index = rx2.indexIn(*this, index + 1);
3226         if (index == -1)
3227             break;
3228         count++;
3229     }
3230     return count;
3231 }
3232 #endif // QT_NO_REGEXP
3233
3234 #ifndef QT_NO_REGEXP
3235 #ifndef QT_BOOTSTRAPPED
3236 /*!
3237     \overload indexOf()
3238     \since 5.0
3239
3240     Returns the index position of the first match of the regular
3241     expression \a re in the string, searching forward from index
3242     position \a from. Returns -1 if \a re didn't match anywhere.
3243
3244     Example:
3245
3246     \snippet qstring/main.cpp 93
3247 */
3248 int QString::indexOf(const QRegularExpression& re, int from) const
3249 {
3250     if (!re.isValid()) {
3251         qWarning("QString::indexOf: invalid QRegularExpresssion object");
3252         return -1;
3253     }
3254
3255     QRegularExpressionMatch match = re.match(*this, from);
3256     if (match.hasMatch())
3257         return match.capturedStart();
3258
3259     return -1;
3260 }
3261
3262 /*!
3263     \overload lastIndexOf()
3264     \since 5.0
3265
3266     Returns the index position of the last match of the regular
3267     expression \a re in the string, which starts before the index
3268     position \a from. Returns -1 if \a re didn't match anywhere.
3269
3270     Example:
3271
3272     \snippet qstring/main.cpp 94
3273 */
3274 int QString::lastIndexOf(const QRegularExpression &re, int from) const
3275 {
3276     if (!re.isValid()) {
3277         qWarning("QString::lastIndexOf: invalid QRegularExpresssion object");
3278         return -1;
3279     }
3280
3281     int endpos = (from < 0) ? (size() + from + 1) : (from + 1);
3282
3283     QRegularExpressionMatchIterator iterator = re.globalMatch(*this);
3284     int lastIndex = -1;
3285     while (iterator.hasNext()) {
3286         QRegularExpressionMatch match = iterator.next();
3287         int start = match.capturedStart();
3288         if (start < endpos)
3289             lastIndex = start;
3290         else
3291             break;
3292     }
3293
3294     return lastIndex;
3295 }
3296
3297 /*! \overload contains()
3298     \since 5.0
3299
3300     Returns true if the regular expression \a re matches somewhere in
3301     this string; otherwise returns false.
3302 */
3303 bool QString::contains(const QRegularExpression &re) const
3304 {
3305     if (!re.isValid()) {
3306         qWarning("QString::contains: invalid QRegularExpresssion object");
3307         return false;
3308     }
3309     QRegularExpressionMatch match = re.match(*this);
3310     return match.hasMatch();
3311 }
3312
3313 /*!
3314     \overload count()
3315     \since 5.0
3316
3317     Returns the number of times the regular expression \a re matches
3318     in the string.
3319
3320     This function counts overlapping matches, so in the example
3321     below, there are four instances of "ana" or "ama":
3322
3323     \snippet qstring/main.cpp 95
3324 */
3325 int QString::count(const QRegularExpression &re) const
3326 {
3327     if (!re.isValid()) {
3328         qWarning("QString::count: invalid QRegularExpresssion object");
3329         return 0;
3330     }
3331     int count = 0;
3332     int index = -1;
3333     int len = length();
3334     while (index < len - 1) {
3335         QRegularExpressionMatch match = re.match(*this, index + 1);
3336         if (!match.hasMatch())
3337             break;
3338         index = match.capturedStart();
3339         count++;
3340     }
3341     return count;
3342 }
3343 #endif // QT_BOOTSTRAPPED
3344 #endif // QT_NO_REGEXP
3345
3346 /*! \fn int QString::count() const
3347
3348     \overload count()
3349
3350     Same as size().
3351 */
3352
3353
3354 /*!
3355     \enum QString::SectionFlag
3356
3357     This enum specifies flags that can be used to affect various
3358     aspects of the section() function's behavior with respect to
3359     separators and empty fields.
3360
3361     \value SectionDefault Empty fields are counted, leading and
3362     trailing separators are not included, and the separator is
3363     compared case sensitively.
3364
3365     \value SectionSkipEmpty Treat empty fields as if they don't exist,
3366     i.e. they are not considered as far as \e start and \e end are
3367     concerned.
3368
3369     \value SectionIncludeLeadingSep Include the leading separator (if
3370     any) in the result string.
3371
3372     \value SectionIncludeTrailingSep Include the trailing separator
3373     (if any) in the result string.
3374
3375     \value SectionCaseInsensitiveSeps Compare the separator
3376     case-insensitively.
3377
3378     \sa section()
3379 */
3380
3381 /*!
3382     \fn QString QString::section(QChar sep, int start, int end = -1, SectionFlags flags) const
3383
3384     This function returns a section of the string.
3385
3386     This string is treated as a sequence of fields separated by the
3387     character, \a sep. The returned string consists of the fields from
3388     position \a start to position \a end inclusive. If \a end is not
3389     specified, all fields from position \a start to the end of the
3390     string are included. Fields are numbered 0, 1, 2, etc., counting
3391     from the left, and -1, -2, etc., counting from right to left.
3392
3393     The \a flags argument can be used to affect some aspects of the
3394     function's behavior, e.g. whether to be case sensitive, whether
3395     to skip empty fields and how to deal with leading and trailing
3396     separators; see \l{SectionFlags}.
3397
3398     \snippet qstring/main.cpp 52
3399
3400     If \a start or \a end is negative, we count fields from the right
3401     of the string, the right-most field being -1, the one from
3402     right-most field being -2, and so on.
3403
3404     \snippet qstring/main.cpp 53
3405
3406     \sa split()
3407 */
3408
3409 /*!
3410     \overload section()
3411
3412     \snippet qstring/main.cpp 51
3413     \snippet qstring/main.cpp 54
3414
3415     \sa split()
3416 */
3417
3418 QString QString::section(const QString &sep, int start, int end, SectionFlags flags) const
3419 {
3420     QStringList sections = split(sep, KeepEmptyParts,
3421                                  (flags & SectionCaseInsensitiveSeps) ? Qt::CaseInsensitive : Qt::CaseSensitive);
3422     if (sections.isEmpty())
3423         return QString();
3424     if (!(flags & SectionSkipEmpty)) {
3425         if (start < 0)
3426             start += sections.count();
3427         if (end < 0)
3428             end += sections.count();
3429     } else {
3430         int skip = 0;
3431         for (int k=0; k<sections.size(); ++k) {
3432             if (sections.at(k).isEmpty())
3433                 skip++;
3434         }
3435         if (start < 0)
3436             start += sections.count() - skip;
3437         if (end < 0)
3438             end += sections.count() - skip;
3439     }
3440     int x = 0;
3441     QString ret;
3442     int first_i = start, last_i = end;
3443     for (int i = 0; x <= end && i < sections.size(); ++i) {
3444         QString section = sections.at(i);
3445         const bool empty = section.isEmpty();
3446         if (x >= start) {
3447             if(x == start)
3448                 first_i = i;
3449             if(x == end)
3450                 last_i = i;
3451             if(x > start)
3452                 ret += sep;
3453             ret += section;
3454         }
3455         if (!empty || !(flags & SectionSkipEmpty))
3456             x++;
3457     }
3458     if((flags & SectionIncludeLeadingSep) && first_i)
3459         ret.prepend(sep);
3460     if((flags & SectionIncludeTrailingSep) && last_i < sections.size()-1)
3461         ret += sep;
3462     return ret;
3463 }
3464
3465 #ifndef QT_NO_REGEXP
3466 class qt_section_chunk {
3467 public:
3468     qt_section_chunk(int l, QString s) { length = l; string = s; }
3469     int length;
3470     QString string;
3471 };
3472
3473 static QString extractSections(const QList<qt_section_chunk> &sections,
3474                                int start,
3475                                int end,
3476                                QString::SectionFlags flags)
3477 {
3478     if (start < 0)
3479         start += sections.count();
3480     if (end < 0)
3481         end += sections.count();
3482
3483     QString ret;
3484     int x = 0;
3485     int first_i = start, last_i = end;
3486     for (int i = 0; x <= end && i < sections.size(); ++i) {
3487         const qt_section_chunk &section = sections.at(i);
3488         const bool empty = (section.length == section.string.length());
3489         if (x >= start) {
3490             if (x == start)
3491                 first_i = i;
3492             if (x == end)
3493                 last_i = i;
3494             if (x != start)
3495                 ret += section.string;
3496             else
3497                 ret += section.string.mid(section.length);
3498         }
3499         if (!empty || !(flags & QString::SectionSkipEmpty))
3500             x++;
3501     }
3502
3503     if ((flags & QString::SectionIncludeLeadingSep) && first_i < sections.size()) {
3504         const qt_section_chunk &section = sections.at(first_i);
3505         ret.prepend(section.string.left(section.length));
3506     }
3507
3508     if ((flags & QString::SectionIncludeTrailingSep) && last_i+1 <= sections.size()-1) {
3509         const qt_section_chunk &section = sections.at(last_i+1);
3510         ret += section.string.left(section.length);
3511     }
3512
3513     return ret;
3514 }
3515
3516 /*!
3517     \overload section()
3518
3519     This string is treated as a sequence of fields separated by the
3520     regular expression, \a reg.
3521
3522     \snippet qstring/main.cpp 55
3523
3524     \warning Using this QRegExp version is much more expensive than
3525     the overloaded string and character versions.
3526
3527     \sa split(), simplified()
3528 */
3529 QString QString::section(const QRegExp &reg, int start, int end, SectionFlags flags) const
3530 {
3531     const QChar *uc = unicode();
3532     if(!uc)
3533         return QString();
3534
3535     QRegExp sep(reg);
3536     sep.setCaseSensitivity((flags & SectionCaseInsensitiveSeps) ? Qt::CaseInsensitive
3537                                                                 : Qt::CaseSensitive);
3538
3539     QList<qt_section_chunk> sections;
3540     int n = length(), m = 0, last_m = 0, last_len = 0;
3541     while ((m = sep.indexIn(*this, m)) != -1) {
3542         sections.append(qt_section_chunk(last_len, QString(uc + last_m, m - last_m)));
3543         last_m = m;
3544         last_len = sep.matchedLength();
3545         m += qMax(sep.matchedLength(), 1);
3546     }
3547     sections.append(qt_section_chunk(last_len, QString(uc + last_m, n - last_m)));
3548
3549     return extractSections(sections, start, end, flags);
3550 }
3551 #endif
3552
3553 #ifndef QT_NO_REGEXP
3554 #ifndef QT_BOOTSTRAPPED
3555 /*!
3556     \overload section()
3557     \since 5.0
3558
3559     This string is treated as a sequence of fields separated by the
3560     regular expression, \a re.
3561
3562     \snippet qstring/main.cpp 89
3563
3564     \warning Using this QRegularExpression version is much more expensive than
3565     the overloaded string and character versions.
3566
3567     \sa split(), simplified()
3568 */
3569 QString QString::section(const QRegularExpression &re, int start, int end, SectionFlags flags) const
3570 {
3571     if (!re.isValid()) {
3572         qWarning("QString::section: invalid QRegularExpression object");
3573         return QString();
3574     }
3575
3576     const QChar *uc = unicode();
3577     if (!uc)
3578         return QString();
3579
3580     QRegularExpression sep(re);
3581     if (flags & SectionCaseInsensitiveSeps)
3582         sep.setPatternOptions(sep.patternOptions() | QRegularExpression::CaseInsensitiveOption);
3583
3584     QList<qt_section_chunk> sections;
3585     int n = length(), m = 0, last_m = 0, last_len = 0;
3586     QRegularExpressionMatchIterator iterator = sep.globalMatch(*this);
3587     while (iterator.hasNext()) {
3588         QRegularExpressionMatch match = iterator.next();
3589         m = match.capturedStart();
3590         sections.append(qt_section_chunk(last_len, QString(uc + last_m, m - last_m)));
3591         last_m = m;
3592         last_len = match.capturedLength();
3593     }
3594     sections.append(qt_section_chunk(last_len, QString(uc + last_m, n - last_m)));
3595
3596     return extractSections(sections, start, end, flags);
3597 }
3598 #endif // QT_BOOTSTRAPPED
3599 #endif // QT_NO_REGEXP
3600
3601 /*!
3602     Returns a substring that contains the \a n leftmost characters
3603     of the string.
3604
3605     The entire string is returned if \a n is greater than size() or
3606     less than zero.
3607
3608     \snippet qstring/main.cpp 31
3609
3610     \sa right(), mid(), startsWith()
3611 */
3612 QString QString::left(int n)  const
3613 {
3614     if (n >= d->size || n < 0)
3615         return *this;
3616     return QString((const QChar*) d->data(), n);
3617 }
3618
3619 /*!
3620     Returns a substring that contains the \a n rightmost characters
3621     of the string.
3622
3623     The entire string is returned if \a n is greater than size() or
3624     less than zero.
3625
3626     \snippet qstring/main.cpp 48
3627
3628     \sa left(), mid(), endsWith()
3629 */
3630 QString QString::right(int n) const
3631 {
3632     if (n >= d->size || n < 0)
3633         return *this;
3634     return QString((const QChar*) d->data() + d->size - n, n);
3635 }
3636
3637 /*!
3638     Returns a string that contains \a n characters of this string,
3639     starting at the specified \a position index.
3640
3641     Returns a null string if the \a position index exceeds the
3642     length of the string. If there are less than \a n characters
3643     available in the string starting at the given \a position, or if
3644     \a n is -1 (default), the function returns all characters that
3645     are available from the specified \a position.
3646
3647     Example:
3648
3649     \snippet qstring/main.cpp 34
3650
3651     \sa left(), right()
3652 */
3653
3654 QString QString::mid(int position, int n) const
3655 {
3656     if (position > d->size)
3657         return QString();
3658     if (position < 0) {
3659         if (n < 0 || n + position >= d->size)
3660             return *this;
3661         if (n + position <= 0)
3662             return QString();
3663
3664         n += position;
3665         position = 0;
3666     } else if (n < 0 || n > d->size - position)
3667         n = d->size - position;
3668     if (position == 0 && n == d->size)
3669         return *this;
3670     return QString((const QChar*) d->data() + position, n);
3671 }
3672
3673 /*!
3674     Returns true if the string starts with \a s; otherwise returns
3675     false.
3676
3677     If \a cs is Qt::CaseSensitive (default), the search is
3678     case sensitive; otherwise the search is case insensitive.
3679
3680     \snippet qstring/main.cpp 65
3681
3682     \sa endsWith()
3683 */
3684 bool QString::startsWith(const QString& s, Qt::CaseSensitivity cs) const
3685 {
3686     return qt_starts_with(isNull() ? 0 : unicode(), size(),
3687                           s.isNull() ? 0 : s.unicode(), s.size(), cs);
3688 }
3689
3690 /*!
3691   \overload startsWith()
3692  */
3693 bool QString::startsWith(QLatin1String s, Qt::CaseSensitivity cs) const
3694 {
3695     return qt_starts_with(isNull() ? 0 : unicode(), size(), s, cs);
3696 }
3697
3698 /*!
3699   \overload startsWith()
3700
3701   Returns true if the string starts with \a c; otherwise returns
3702   false.
3703 */
3704 bool QString::startsWith(QChar c, Qt::CaseSensitivity cs) const
3705 {
3706     return d->size
3707            && (cs == Qt::CaseSensitive
3708                ? d->data()[0] == c
3709                : foldCase(d->data()[0]) == foldCase(c.unicode()));
3710 }
3711
3712 /*!
3713     \since 4.8
3714     \overload
3715     Returns true if the string starts with the string reference \a s;
3716     otherwise returns false.
3717
3718     If \a cs is Qt::CaseSensitive (default), the search is case
3719     sensitive; otherwise the search is case insensitive.
3720
3721     \sa endsWith()
3722 */
3723 bool QString::startsWith(const QStringRef &s, Qt::CaseSensitivity cs) const
3724 {
3725     return qt_starts_with(isNull() ? 0 : unicode(), size(),
3726                           s.isNull() ? 0 : s.unicode(), s.size(), cs);
3727 }
3728
3729 /*!
3730     Returns true if the string ends with \a s; otherwise returns
3731     false.
3732
3733     If \a cs is Qt::CaseSensitive (default), the search is case
3734     sensitive; otherwise the search is case insensitive.
3735
3736     \snippet qstring/main.cpp 20
3737
3738     \sa startsWith()
3739 */
3740 bool QString::endsWith(const QString& s, Qt::CaseSensitivity cs) const
3741 {
3742     return qt_ends_with(isNull() ? 0 : unicode(), size(),
3743                         s.isNull() ? 0 : s.unicode(), s.size(), cs);
3744     }
3745
3746 /*!
3747     \since 4.8
3748     \overload endsWith()
3749     Returns true if the string ends with the string reference \a s;
3750     otherwise returns false.
3751
3752     If \a cs is Qt::CaseSensitive (default), the search is case
3753     sensitive; otherwise the search is case insensitive.
3754
3755     \sa startsWith()
3756 */
3757 bool QString::endsWith(const QStringRef &s, Qt::CaseSensitivity cs) const
3758 {
3759     return qt_ends_with(isNull() ? 0 : unicode(), size(),
3760                         s.isNull() ? 0 : s.unicode(), s.size(), cs);
3761 }
3762
3763
3764 /*!
3765     \overload endsWith()
3766 */
3767 bool QString::endsWith(QLatin1String s, Qt::CaseSensitivity cs) const
3768 {
3769     return qt_ends_with(isNull() ? 0 : unicode(), size(), s, cs);
3770 }
3771
3772 /*!
3773   Returns true if the string ends with \a c; otherwise returns
3774   false.
3775
3776   \overload endsWith()
3777  */
3778 bool QString::endsWith(QChar c, Qt::CaseSensitivity cs) const
3779 {
3780     return d->size
3781            && (cs == Qt::CaseSensitive
3782                ? d->data()[d->size - 1] == c
3783                : foldCase(d->data()[d->size - 1]) == foldCase(c.unicode()));
3784 }
3785
3786
3787 #if defined(__SSE2__)
3788 static inline __m128i mergeQuestionMarks(__m128i chunk)
3789 {
3790     const __m128i questionMark = _mm_set1_epi16('?');
3791
3792 # ifdef __SSE4_2__
3793     // compare the unsigned shorts for the range 0x0100-0xFFFF
3794     // note on the use of _mm_cmpestrm:
3795     //  The MSDN documentation online (http://technet.microsoft.com/en-us/library/bb514080.aspx)
3796     //  says for range search the following:
3797     //    For each character c in a, determine whether b0 <= c <= b1 or b2 <= c <= b3
3798     //
3799     //  However, all examples on the Internet, including from Intel
3800     //  (see http://software.intel.com/en-us/articles/xml-parsing-accelerator-with-intel-streaming-simd-extensions-4-intel-sse4/)
3801     //  put the range to be searched first
3802     //
3803     //  Disassembly and instruction-level debugging with GCC and ICC show
3804     //  that they are doing the right thing. Inverting the arguments in the
3805     //  instruction does cause a bunch of test failures.
3806
3807     const int mode = _SIDD_UWORD_OPS | _SIDD_CMP_RANGES | _SIDD_UNIT_MASK;
3808     const __m128i rangeMatch = _mm_cvtsi32_si128(0xffff0100);
3809     const __m128i offLimitMask = _mm_cmpestrm(rangeMatch, 2, chunk, 8, mode);
3810
3811     // replace the non-Latin 1 characters in the chunk with question marks
3812     chunk = _mm_blendv_epi8(chunk, questionMark, offLimitMask);
3813 # else
3814     // SSE has no compare instruction for unsigned comparison.
3815     // The variables must be shiffted + 0x8000 to be compared
3816     const __m128i signedBitOffset = _mm_set1_epi16(0x8000);
3817     const __m128i thresholdMask = _mm_set1_epi16(0xff + 0x8000);
3818
3819     const __m128i signedChunk = _mm_add_epi16(chunk, signedBitOffset);
3820     const __m128i offLimitMask = _mm_cmpgt_epi16(signedChunk, thresholdMask);
3821
3822 #  ifdef __SSE4_1__
3823     // replace the non-Latin 1 characters in the chunk with question marks
3824     chunk = _mm_blendv_epi8(chunk, questionMark, offLimitMask);
3825 #  else
3826     // offLimitQuestionMark contains '?' for each 16 bits that was off-limit
3827     // the 16 bits that were correct contains zeros
3828     const __m128i offLimitQuestionMark = _mm_and_si128(offLimitMask, questionMark);
3829
3830     // correctBytes contains the bytes that were in limit
3831     // the 16 bits that were off limits contains zeros
3832     const __m128i correctBytes = _mm_andnot_si128(offLimitMask, chunk);
3833
3834     // merge offLimitQuestionMark and correctBytes to have the result
3835     chunk = _mm_or_si128(correctBytes, offLimitQuestionMark);
3836 #  endif
3837 # endif
3838     return chunk;
3839 }
3840 #endif
3841
3842 static QByteArray toLatin1_helper(const QChar *data, int length)
3843 {
3844     QByteArray ba;
3845     if (length) {
3846         ba.resize(length);
3847         const ushort *src = reinterpret_cast<const ushort *>(data);
3848         uchar *dst = (uchar*) ba.data();
3849 #if defined(__SSE2__)
3850         if (length >= 16) {
3851             const int chunkCount = length >> 4; // divided by 16
3852
3853             for (int i = 0; i < chunkCount; ++i) {
3854                 __m128i chunk1 = _mm_loadu_si128((__m128i*)src); // load
3855                 chunk1 = mergeQuestionMarks(chunk1);
3856                 src += 8;
3857
3858                 __m128i chunk2 = _mm_loadu_si128((__m128i*)src); // load
3859                 chunk2 = mergeQuestionMarks(chunk2);
3860                 src += 8;
3861
3862                 // pack the two vector to 16 x 8bits elements
3863                 const __m128i result = _mm_packus_epi16(chunk1, chunk2);
3864
3865                 _mm_storeu_si128((__m128i*)dst, result); // store
3866                 dst += 16;
3867             }
3868             length = length % 16;
3869         }
3870 #elif defined(__ARM_NEON__)
3871         // Refer to the documentation of the SSE2 implementation
3872         // this use eactly the same method as for SSE except:
3873         // 1) neon has unsigned comparison
3874         // 2) packing is done to 64 bits (8 x 8bits component).
3875         if (length >= 16) {
3876             const int chunkCount = length >> 3; // divided by 8
3877             const uint16x8_t questionMark = vdupq_n_u16('?'); // set
3878             const uint16x8_t thresholdMask = vdupq_n_u16(0xff); // set
3879             for (int i = 0; i < chunkCount; ++i) {
3880                 uint16x8_t chunk = vld1q_u16((uint16_t *)src); // load
3881                 src += 8;
3882
3883                 const uint16x8_t offLimitMask = vcgtq_u16(chunk, thresholdMask); // chunk > thresholdMask
3884                 const uint16x8_t offLimitQuestionMark = vandq_u16(offLimitMask, questionMark); // offLimitMask & questionMark
3885                 const uint16x8_t correctBytes = vbicq_u16(chunk, offLimitMask); // !offLimitMask & chunk
3886                 chunk = vorrq_u16(correctBytes, offLimitQuestionMark); // correctBytes | offLimitQuestionMark
3887                 const uint8x8_t result = vmovn_u16(chunk); // narrowing move->packing
3888                 vst1_u8(dst, result); // store
3889                 dst += 8;
3890             }
3891             length = length % 8;
3892         }
3893 #endif
3894         while (length--) {
3895             *dst++ = (*src>0xff) ? '?' : (uchar) *src;
3896             ++src;
3897         }
3898     }
3899     return ba;
3900 }
3901
3902 /*!
3903     Returns a Latin-1 representation of the string as a QByteArray.
3904
3905     The returned byte array is undefined if the string contains non-Latin1
3906     characters. Those characters may be suppressed or replaced with a
3907     question mark.
3908
3909     \sa fromLatin1(), toUtf8(), toLocal8Bit(), QTextCodec
3910 */
3911 QByteArray QString::toLatin1() const
3912 {
3913     return toLatin1_helper(unicode(), length());
3914 }
3915
3916 /*!
3917     \fn QByteArray QString::toAscii() const
3918     \deprecated
3919     Returns an 8-bit representation of the string as a QByteArray.
3920
3921     This function does the same as toLatin1().
3922
3923     Note that, despite the name, this function does not necessarily return an US-ASCII
3924     (ANSI X3.4-1986) string and its result may not be US-ASCII compatible.
3925
3926     \sa fromAscii(), toLatin1(), toUtf8(), toLocal8Bit(), QTextCodec
3927 */
3928
3929 #if !defined(Q_OS_MAC) && defined(Q_OS_UNIX)
3930 static QByteArray toLocal8Bit_helper(const QChar *data, int length)
3931 {
3932 #ifndef QT_NO_TEXTCODEC
3933     if (QTextCodec::codecForLocale())
3934         return QTextCodec::codecForLocale()->fromUnicode(data, length);
3935 #endif // QT_NO_TEXTCODEC
3936     return toLatin1_helper(data, length);
3937 }
3938 #endif
3939
3940 /*!
3941     Returns the local 8-bit representation of the string as a
3942     QByteArray. The returned byte array is undefined if the string
3943     contains characters not supported by the local 8-bit encoding.
3944
3945     QTextCodec::codecForLocale() is used to perform the conversion from
3946     Unicode. If the locale encoding could not be determined, this function
3947     does the same as toLatin1().
3948
3949     If this string contains any characters that cannot be encoded in the
3950     locale, the returned byte array is undefined. Those characters may be
3951     suppressed or replaced by another.
3952
3953     \sa fromLocal8Bit(), toLatin1(), toUtf8(), QTextCodec
3954 */
3955 QByteArray QString::toLocal8Bit() const
3956 {
3957 #ifndef QT_NO_TEXTCODEC
3958     if (QTextCodec::codecForLocale())
3959         return QTextCodec::codecForLocale()->fromUnicode(*this);
3960 #endif // QT_NO_TEXTCODEC
3961     return toLatin1();
3962 }
3963
3964 /*!
3965     Returns a UTF-8 representation of the string as a QByteArray.
3966
3967     UTF-8 is a Unicode codec and can represent all characters in a Unicode
3968     string like QString.
3969
3970     However, in the Unicode range, there are certain codepoints that are not
3971     considered characters. The Unicode standard reserves the last two
3972     codepoints in each Unicode Plane (U+FFFE, U+FFFF, U+1FFFE, U+1FFFF,
3973     U+2FFFE, etc.), as well as 32 codepoints in the range U+FDD0..U+FDEF,
3974     inclusive, as non-characters. If any of those appear in the string, they
3975     may be discarded and will not appear in the UTF-8 representation, or they
3976     may be replaced by one or more replacement characters.
3977
3978     \sa fromUtf8(), toLatin1(), toLocal8Bit(), QTextCodec
3979 */
3980 QByteArray QString::toUtf8() const
3981 {
3982     if (isNull())
3983         return QByteArray();
3984
3985     return QUtf8::convertFromUnicode(constData(), length(), 0);
3986 }
3987
3988 /*!
3989     \since 4.2
3990
3991     Returns a UCS-4/UTF-32 representation of the string as a QVector<uint>.
3992
3993     UCS-4 is a Unicode codec and is lossless. All characters from this string
3994     can be encoded in UCS-4. The vector is not null terminated.
3995
3996     \sa fromUtf8(), toUtf8(), toLatin1(), toLocal8Bit(), QTextCodec, fromUcs4(), toWCharArray()
3997 */
3998 QVector<uint> QString::toUcs4() const
3999 {
4000     QVector<uint> v(length());
4001     uint *a = v.data();
4002     int len = toUcs4_helper(d->data(), length(), a);
4003     v.resize(len);
4004     return v;
4005 }
4006
4007 QString::Data *QString::fromLatin1_helper(const char *str, int size)
4008 {
4009     Data *d;
4010     if (!str) {
4011         d = Data::sharedNull();
4012     } else if (size == 0 || (!*str && size < 0)) {
4013         d = Data::allocate(0);
4014     } else {
4015         if (size < 0)
4016             size = qstrlen(str);
4017         d = Data::allocate(size + 1);
4018         Q_CHECK_PTR(d);
4019         d->size = size;
4020         d->data()[size] = '\0';
4021         ushort *dst = d->data();
4022         /* SIMD:
4023          * Unpacking with SSE has been shown to improve performance on recent CPUs
4024          * The same method gives no improvement with NEON.
4025          */
4026 #if defined(__SSE2__)
4027         if (size >= 16) {
4028             int chunkCount = size >> 4; // divided by 16
4029             const __m128i nullMask = _mm_set1_epi32(0);
4030             for (int i = 0; i < chunkCount; ++i) {
4031                 const __m128i chunk = _mm_loadu_si128((__m128i*)str); // load
4032                 str += 16;
4033
4034                 // unpack the first 8 bytes, padding with zeros
4035                 const __m128i firstHalf = _mm_unpacklo_epi8(chunk, nullMask);
4036                 _mm_storeu_si128((__m128i*)dst, firstHalf); // store
4037                 dst += 8;
4038
4039                 // unpack the last 8 bytes, padding with zeros
4040                 const __m128i secondHalf = _mm_unpackhi_epi8 (chunk, nullMask);
4041                 _mm_storeu_si128((__m128i*)dst, secondHalf); // store
4042                 dst += 8;
4043             }
4044             size = size % 16;
4045         }
4046 #endif
4047         while (size--)
4048             *dst++ = (uchar)*str++;
4049     }
4050     return d;
4051 }
4052
4053 QString::Data *QString::fromAscii_helper(const char *str, int size)
4054 {
4055     QString s = fromUtf8(str, size);
4056     s.d->ref.ref();
4057     return s.d;
4058 }
4059
4060 /*! \fn QString QString::fromLatin1(const char *str, int size)
4061     Returns a QString initialized with the first \a size characters
4062     of the Latin-1 string \a str.
4063
4064     If \a size is -1 (default), it is taken to be strlen(\a
4065     str).
4066
4067     \sa toLatin1(), fromUtf8(), fromLocal8Bit()
4068 */
4069
4070
4071 /*! \fn QString QString::fromLocal8Bit(const char *str, int size)
4072     Returns a QString initialized with the first \a size characters
4073     of the 8-bit string \a str.
4074
4075     If \a size is -1 (default), it is taken to be strlen(\a
4076     str).
4077
4078     QTextCodec::codecForLocale() is used to perform the conversion.
4079
4080     \sa toLocal8Bit(), fromLatin1(), fromUtf8()
4081 */
4082 QString QString::fromLocal8Bit_helper(const char *str, int size)
4083 {
4084     if (!str)
4085         return QString();
4086     if (size == 0 || (!*str && size < 0)) {
4087         QStringDataPtr empty = { Data::allocate(0) };
4088         return QString(empty);
4089     }
4090 #if !defined(QT_NO_TEXTCODEC)
4091     if (size < 0)
4092         size = qstrlen(str);
4093     QTextCodec *codec = QTextCodec::codecForLocale();
4094     if (codec)
4095         return codec->toUnicode(str, size);
4096 #endif // !QT_NO_TEXTCODEC
4097     return fromLatin1(str, size);
4098 }
4099
4100 /*! \fn QString QString::fromAscii(const char *, int size);
4101     \deprecated
4102
4103     Returns a QString initialized with the first \a size characters
4104     from the string \a str.
4105
4106     If \a size is -1 (default), it is taken to be strlen(\a
4107     str).
4108
4109     This function does the same as fromLatin1().
4110
4111     \sa toAscii(), fromLatin1(), fromUtf8(), fromLocal8Bit()
4112 */
4113
4114 /*! \fn QString QString::fromUtf8(const char *str, int size)
4115     Returns a QString initialized with the first \a size bytes
4116     of the UTF-8 string \a str.
4117
4118     If \a size is -1 (default), it is taken to be strlen(\a
4119     str).
4120
4121     UTF-8 is a Unicode codec and can represent all characters in a Unicode
4122     string like QString. However, invalid sequences are possible with UTF-8
4123     and, if any such are found, they will be replaced with one or more
4124     "replacement characters", or suppressed. These include non-Unicode
4125     sequences, non-characters, overlong sequences or surrogate codepoints
4126     encoded into UTF-8.
4127
4128     Non-characters are codepoints that the Unicode standard reserves and must
4129     not be used in text interchange. They are the last two codepoints in each
4130     Unicode Plane (U+FFFE, U+FFFF, U+1FFFE, U+1FFFF, U+2FFFE, etc.), as well
4131     as 32 codepoints in the range U+FDD0..U+FDEF, inclusive.
4132
4133     \sa toUtf8(), fromLatin1(), fromLocal8Bit()
4134 */
4135 QString QString::fromUtf8_helper(const char *str, int size)
4136 {
4137     if (!str)
4138         return QString();
4139
4140     Q_ASSERT(size != -1);
4141     return QUtf8::convertToUnicode(str, size, 0);
4142 }
4143
4144 /*!
4145     Returns a QString initialized with the first \a size characters
4146     of the Unicode string \a unicode (ISO-10646-UTF-16 encoded).
4147
4148     If \a size is -1 (default), \a unicode must be terminated
4149     with a 0.
4150
4151     This function checks for a Byte Order Mark (BOM). If it is missing,
4152     host byte order is assumed.
4153
4154     This function is slow compared to the other Unicode conversions.
4155     Use QString(const QChar *, int) or QString(const QChar *) if possible.
4156
4157     QString makes a deep copy of the Unicode data.
4158
4159     \sa utf16(), setUtf16()
4160 */
4161 QString QString::fromUtf16(const ushort *unicode, int size)
4162 {
4163     if (!unicode)
4164         return QString();
4165     if (size < 0) {
4166         size = 0;
4167         while (unicode[size] != 0)
4168             ++size;
4169     }
4170     return QUtf16::convertToUnicode((const char *)unicode, size*2, 0);
4171 }
4172
4173
4174 /*!
4175     \since 4.2
4176
4177     Returns a QString initialized with the first \a size characters
4178     of the Unicode string \a unicode (ISO-10646-UCS-4 encoded).
4179
4180     If \a size is -1 (default), \a unicode must be terminated
4181     with a 0.
4182
4183     \sa toUcs4(), fromUtf16(), utf16(), setUtf16(), fromWCharArray()
4184 */
4185 QString QString::fromUcs4(const uint *unicode, int size)
4186 {
4187     if (!unicode)
4188         return QString();
4189     if (size < 0) {
4190         size = 0;
4191         while (unicode[size] != 0)
4192             ++size;
4193     }
4194     return QUtf32::convertToUnicode((const char *)unicode, size*4, 0);
4195 }
4196
4197 /*!
4198     Resizes the string to \a size characters and copies \a unicode
4199     into the string.
4200
4201     If \a unicode is 0, nothing is copied, but the string is still
4202     resized to \a size.
4203
4204     \sa unicode(), setUtf16()
4205 */
4206 QString& QString::setUnicode(const QChar *unicode, int size)
4207 {
4208      resize(size);
4209      if (unicode && size)
4210          memcpy(d->data(), unicode, size * sizeof(QChar));
4211      return *this;
4212 }
4213
4214 /*!
4215     \fn QString &QString::setUtf16(const ushort *unicode, int size)
4216
4217     Resizes the string to \a size characters and copies \a unicode
4218     into the string.
4219
4220     If \a unicode is 0, nothing is copied, but the string is still
4221     resized to \a size.
4222
4223     Note that unlike fromUtf16(), this function does not consider BOMs and
4224     possibly differing byte ordering.
4225
4226     \sa utf16(), setUnicode()
4227 */
4228
4229 /*!
4230     Returns a string that has whitespace removed from the start
4231     and the end, and that has each sequence of internal whitespace
4232     replaced with a single space.
4233
4234     Whitespace means any character for which QChar::isSpace() returns
4235     true. This includes the ASCII characters '\\t', '\\n', '\\v',
4236     '\\f', '\\r', and ' '.
4237
4238     Example:
4239
4240     \snippet qstring/main.cpp 57
4241
4242     \sa trimmed()
4243 */
4244 QString QString::simplified() const
4245 {
4246     if (d->size == 0)
4247         return *this;
4248
4249     const QChar * const start = reinterpret_cast<QChar *>(d->data());
4250     const QChar *from = start;
4251     const QChar *fromEnd = start + d->size;
4252     forever {
4253         QChar ch = *from;
4254         if (!ch.isSpace())
4255             break;
4256         if (++from == fromEnd) {
4257             // All-whitespace string
4258             QStringDataPtr empty = { Data::allocate(0) };
4259             return QString(empty);
4260         }
4261     }
4262     // This loop needs no underflow check, as we already determined that
4263     // the string contains non-whitespace. If the string has exactly one
4264     // non-whitespace, it will be checked twice - we can live with that.
4265     while (fromEnd[-1].isSpace())
4266         fromEnd--;
4267     // The rest of the function depends on the fact that we already know
4268     // that the last character in the source is no whitespace.
4269     const QChar *copyFrom = from;
4270     int copyCount;
4271     forever {
4272         if (++from == fromEnd) {
4273             // Only leading and/or trailing whitespace, if any at all
4274             return mid(copyFrom - start, from - copyFrom);
4275         }
4276         QChar ch = *from;
4277         if (!ch.isSpace())
4278             continue;
4279         if (ch != QLatin1Char(' ')) {
4280             copyCount = from - copyFrom;
4281             break;
4282         }
4283         ch = *++from;
4284         if (ch.isSpace()) {
4285             copyCount = from - copyFrom - 1;
4286             break;
4287         }
4288     }
4289     // 'from' now points at the non-trailing whitespace which made the
4290     // string not simplified in the first place. 'copyCount' is the number
4291     // of already simplified characters - at least one, obviously -
4292     // without a trailing space.
4293     QString result((fromEnd - from) + copyCount, Qt::Uninitialized);
4294     QChar *to = reinterpret_cast<QChar *>(result.d->data());
4295     ::memcpy(to, copyFrom, copyCount * 2);
4296     to += copyCount;
4297     fromEnd--;
4298     QChar ch;
4299     forever {
4300         *to++ = QLatin1Char(' ');
4301         do {
4302             ch = *++from;
4303         } while (ch.isSpace());
4304         if (from == fromEnd)
4305             break;
4306         do {
4307             *to++ = ch;
4308             ch = *++from;
4309             if (from == fromEnd)
4310                 goto done;
4311         } while (!ch.isSpace());
4312
4313     }
4314   done:
4315     *to++ = ch;
4316     result.truncate(to - reinterpret_cast<QChar *>(result.d->data()));
4317     return result;
4318 }
4319
4320 /*!
4321     Returns a string that has whitespace removed from the start and
4322     the end.
4323
4324     Whitespace means any character for which QChar::isSpace() returns
4325     true. This includes the ASCII characters '\\t', '\\n', '\\v',
4326     '\\f', '\\r', and ' '.
4327
4328     Example:
4329
4330     \snippet qstring/main.cpp 82
4331
4332     Unlike simplified(), trimmed() leaves internal whitespace alone.
4333
4334     \sa simplified()
4335 */
4336 QString QString::trimmed() const
4337 {
4338     if (d->size == 0)
4339         return *this;
4340     const QChar *s = (const QChar*)d->data();
4341     if (!s->isSpace() && !s[d->size-1].isSpace())
4342         return *this;
4343     int start = 0;
4344     int end = d->size - 1;
4345     while (start<=end && s[start].isSpace())  // skip white space from start
4346         start++;
4347     if (start <= end) {                          // only white space
4348         while (end && s[end].isSpace())           // skip white space from end
4349             end--;
4350     }
4351     int l = end - start + 1;
4352     if (l <= 0) {
4353         QStringDataPtr empty = { Data::allocate(0) };
4354         return QString(empty);
4355     }
4356     return QString(s + start, l);
4357 }
4358
4359 /*! \fn const QChar QString::at(int position) const
4360
4361     Returns the character at the given index \a position in the
4362     string.
4363
4364     The \a position must be a valid index position in the string
4365     (i.e., 0 <= \a position < size()).
4366
4367     \sa operator[]()
4368 */
4369
4370 /*!
4371     \fn QCharRef QString::operator[](int position)
4372
4373     Returns the character at the specified \a position in the string as a
4374     modifiable reference.
4375
4376     Example:
4377
4378     \snippet qstring/main.cpp 85
4379
4380     The return value is of type QCharRef, a helper class for QString.
4381     When you get an object of type QCharRef, you can use it as if it
4382     were a QChar &. If you assign to it, the assignment will apply to
4383     the character in the QString from which you got the reference.
4384
4385     \sa at()
4386 */
4387
4388 /*!
4389     \fn const QChar QString::operator[](int position) const
4390
4391     \overload operator[]()
4392 */
4393
4394 /*! \fn QCharRef QString::operator[](uint position)
4395
4396 \overload operator[]()
4397
4398 Returns the character at the specified \a position in the string as a
4399 modifiable reference. Equivalent to \c at(position).
4400 */
4401
4402 /*! \fn const QChar QString::operator[](uint position) const
4403
4404 \overload operator[]()
4405 */
4406
4407 /*!
4408     \fn void QString::truncate(int position)
4409
4410     Truncates the string at the given \a position index.
4411
4412     If the specified \a position index is beyond the end of the
4413     string, nothing happens.
4414
4415     Example:
4416
4417     \snippet qstring/main.cpp 83
4418
4419     If \a position is negative, it is equivalent to passing zero.
4420
4421     \sa chop(), resize(), left()
4422 */
4423
4424 void QString::truncate(int pos)
4425 {
4426     if (pos < d->size)
4427         resize(pos);
4428 }
4429
4430
4431 /*!
4432     Removes \a n characters from the end of the string.
4433
4434     If \a n is greater than size(), the result is an empty string.
4435
4436     Example:
4437     \snippet qstring/main.cpp 15
4438
4439     If you want to remove characters from the \e beginning of the
4440     string, use remove() instead.
4441
4442     \sa truncate(), resize(), remove()
4443 */
4444 void QString::chop(int n)
4445 {
4446     if (n > 0)
4447         resize(d->size - n);
4448 }
4449
4450 /*!
4451     Sets every character in the string to character \a ch. If \a size
4452     is different from -1 (default), the string is resized to \a
4453     size beforehand.
4454
4455     Example:
4456
4457     \snippet qstring/main.cpp 21
4458
4459     \sa resize()
4460 */
4461
4462 QString& QString::fill(QChar ch, int size)
4463 {
4464     resize(size < 0 ? d->size : size);
4465     if (d->size) {
4466         QChar *i = (QChar*)d->data() + d->size;
4467         QChar *b = (QChar*)d->data();
4468         while (i != b)
4469            *--i = ch;
4470     }
4471     return *this;
4472 }
4473
4474 /*!
4475     \fn int QString::length() const
4476
4477     Returns the number of characters in this string.  Equivalent to
4478     size().
4479
4480     \sa resize()
4481 */
4482
4483 /*!
4484     \fn int QString::size() const
4485
4486     Returns the number of characters in this string.
4487
4488     The last character in the string is at position size() - 1. In
4489     addition, QString ensures that the character at position size()
4490     is always '\\0', so that you can use the return value of data()
4491     and constData() as arguments to functions that expect
4492     '\\0'-terminated strings.
4493
4494     Example:
4495
4496     \snippet qstring/main.cpp 58
4497
4498     \sa isEmpty(), resize()
4499 */
4500
4501 /*! \fn bool QString::isNull() const
4502
4503     Returns true if this string is null; otherwise returns false.
4504
4505     Example:
4506
4507     \snippet qstring/main.cpp 28
4508
4509     Qt makes a distinction between null strings and empty strings for
4510     historical reasons. For most applications, what matters is
4511     whether or not a string contains any data, and this can be
4512     determined using the isEmpty() function.
4513
4514     \sa isEmpty()
4515 */
4516
4517 /*! \fn bool QString::isEmpty() const
4518
4519     Returns true if the string has no characters; otherwise returns
4520     false.
4521
4522     Example:
4523
4524     \snippet qstring/main.cpp 27
4525
4526     \sa size()
4527 */
4528
4529 /*! \fn QString &QString::operator+=(const QString &other)
4530
4531     Appends the string \a other onto the end of this string and
4532     returns a reference to this string.
4533
4534     Example:
4535
4536     \snippet qstring/main.cpp 84
4537
4538     This operation is typically very fast (\l{constant time}),
4539     because QString preallocates extra space at the end of the string
4540     data so it can grow without reallocating the entire string each
4541     time.
4542
4543     \sa append(), prepend()
4544 */
4545
4546 /*! \fn QString &QString::operator+=(QLatin1String str)
4547
4548     \overload operator+=()
4549
4550     Appends the Latin-1 string \a str to this string.
4551 */
4552
4553 /*! \fn QString &QString::operator+=(const QByteArray &ba)
4554
4555     \overload operator+=()
4556
4557     Appends the byte array \a ba to this string. The byte array is converted
4558     to Unicode using the fromUtf8() function. If any NUL characters ('\\0')
4559     are embedded in the \a ba byte array, they will be included in the
4560     transformation.
4561
4562     You can disable this function by defining \c
4563     QT_NO_CAST_FROM_ASCII when you compile your applications. This
4564     can be useful if you want to ensure that all user-visible strings
4565     go through QObject::tr(), for example.
4566 */
4567
4568 /*! \fn QString &QString::operator+=(const char *str)
4569
4570     \overload operator+=()
4571
4572     Appends the string \a str to this string. The const char pointer
4573     is converted to Unicode using the fromUtf8() function.
4574
4575     You can disable this function by defining \c QT_NO_CAST_FROM_ASCII
4576     when you compile your applications. This can be useful if you want
4577     to ensure that all user-visible strings go through QObject::tr(),
4578     for example.
4579 */
4580
4581 /*! \fn QString &QString::operator+=(const QStringRef &str)
4582
4583     \overload operator+=()
4584
4585     Appends the string section referenced by \a str to this string.
4586 */
4587
4588 /*! \fn QString &QString::operator+=(char ch)
4589
4590     \overload operator+=()
4591
4592     Appends the character \a ch to this string. Note that the character is
4593     converted to Unicode using the fromLatin1() function, unlike other 8-bit
4594     functions that operate on UTF-8 data.
4595
4596     You can disable this function by defining \c QT_NO_CAST_FROM_ASCII
4597     when you compile your applications. This can be useful if you want
4598     to ensure that all user-visible strings go through QObject::tr(),
4599     for example.
4600 */
4601
4602 /*! \fn QString &QString::operator+=(QChar ch)
4603
4604     \overload operator+=()
4605
4606     Appends the character \a ch to the string.
4607 */
4608
4609 /*! \fn QString &QString::operator+=(QChar::SpecialCharacter c)
4610
4611     \overload operator+=()
4612
4613     \internal
4614 */
4615
4616 /*!
4617     \fn bool operator==(const char *s1, const QString &s2)
4618
4619     \overload  operator==()
4620     \relates QString
4621
4622     Returns true if \a s1 is equal to \a s2; otherwise returns false.
4623     Note that no string is equal to \a s1 being 0.
4624
4625     Equivalent to \c {s1 != 0 && compare(s1, s2) == 0}.
4626
4627     \sa QString::compare()
4628 */
4629
4630 /*!
4631     \fn bool operator!=(const char *s1, const QString &s2)
4632     \relates QString
4633
4634     Returns true if \a s1 is not equal to \a s2; otherwise returns
4635     false.
4636
4637     For \a s1 != 0, this is equivalent to \c {compare(} \a s1, \a s2
4638     \c {) != 0}. Note that no string is equal to \a s1 being 0.
4639
4640     \sa QString::compare()
4641 */
4642
4643 /*!
4644     \fn bool operator<(const char *s1, const QString &s2)
4645     \relates QString
4646
4647     Returns true if \a s1 is lexically less than \a s2; otherwise
4648     returns false.  For \a s1 != 0, this is equivalent to \c
4649     {compare(s1, s2) < 0}.
4650
4651     The comparison is based exclusively on the numeric Unicode values
4652     of the characters and is very fast, but is not what a human would
4653     expect. Consider sorting user-interface strings using the
4654     QString::localeAwareCompare() function.
4655
4656     \sa QString::compare()
4657 */
4658
4659 /*!
4660     \fn bool operator<=(const char *s1, const QString &s2)
4661     \relates QString
4662
4663     Returns true if \a s1 is lexically less than or equal to \a s2;
4664     otherwise returns false.  For \a s1 != 0, this is equivalent to \c
4665     {compare(s1, s2) <= 0}.
4666
4667     The comparison is based exclusively on the numeric Unicode values
4668     of the characters and is very fast, but is not what a human would
4669     expect. Consider sorting user-interface strings with
4670     QString::localeAwareCompare().
4671
4672     \sa QString::compare()
4673 */
4674
4675 /*!
4676     \fn bool operator>(const char *s1, const QString &s2)
4677     \relates QString
4678
4679     Returns true if \a s1 is lexically greater than \a s2; otherwise
4680     returns false.  Equivalent to \c {compare(s1, s2) > 0}.
4681
4682     The comparison is based exclusively on the numeric Unicode values
4683     of the characters and is very fast, but is not what a human would
4684     expect. Consider sorting user-interface strings using the
4685     QString::localeAwareCompare() function.
4686
4687     \sa QString::compare()
4688 */
4689
4690 /*!
4691     \fn bool operator>=(const char *s1, const QString &s2)
4692     \relates QString
4693
4694     Returns true if \a s1 is lexically greater than or equal to \a s2;
4695     otherwise returns false.  For \a s1 != 0, this is equivalent to \c
4696     {compare(s1, s2) >= 0}.
4697
4698     The comparison is based exclusively on the numeric Unicode values
4699     of the characters and is very fast, but is not what a human would
4700     expect. Consider sorting user-interface strings using the
4701     QString::localeAwareCompare() function.
4702 */
4703
4704 /*!
4705     \fn const QString operator+(const QString &s1, const QString &s2)
4706     \relates QString
4707
4708     Returns a string which is the result of concatenating \a s1 and \a
4709     s2.
4710 */
4711
4712 /*!
4713     \fn const QString operator+(const QString &s1, const char *s2)
4714     \relates QString
4715
4716     Returns a string which is the result of concatenating \a s1 and \a
4717     s2 (\a s2 is converted to Unicode using the QString::fromUtf8()
4718     function).
4719
4720     \sa QString::fromUtf8()
4721 */
4722
4723 /*!
4724     \fn const QString operator+(const char *s1, const QString &s2)
4725     \relates QString
4726
4727     Returns a string which is the result of concatenating \a s1 and \a
4728     s2 (\a s1 is converted to Unicode using the QString::fromUtf8()
4729     function).
4730
4731     \sa QString::fromUtf8()
4732 */
4733
4734 /*!
4735     \fn const QString operator+(const QString &s, char ch)
4736     \relates QString
4737
4738     Returns a string which is the result of concatenating the string
4739     \a s and the character \a ch.
4740 */
4741
4742 /*!
4743     \fn const QString operator+(char ch, const QString &s)
4744     \relates QString
4745
4746     Returns a string which is the result of concatenating the
4747     character \a ch and the string \a s.
4748 */
4749
4750 /*!
4751     \fn int QString::compare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs)
4752     \since 4.2
4753
4754     Compares \a s1 with \a s2 and returns an integer less than, equal
4755     to, or greater than zero if \a s1 is less than, equal to, or
4756     greater than \a s2.
4757
4758     If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
4759     otherwise the comparison is case insensitive.
4760
4761     Case sensitive comparison is based exclusively on the numeric
4762     Unicode values of the characters and is very fast, but is not what
4763     a human would expect.  Consider sorting user-visible strings with
4764     localeAwareCompare().
4765
4766     \snippet qstring/main.cpp 16
4767
4768     \sa operator==(), operator<(), operator>()
4769 */
4770
4771 /*!
4772     \fn int QString::compare(const QString &s1, QLatin1String s2, Qt::CaseSensitivity cs)
4773     \since 4.2
4774     \overload compare()
4775
4776     Performs a comparison of \a s1 and \a s2, using the case
4777     sensitivity setting \a cs.
4778 */
4779
4780 /*!
4781     \fn int QString::compare(QLatin1String s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
4782
4783     \since 4.2
4784     \overload compare()
4785
4786     Performs a comparison of \a s1 and \a s2, using the case
4787     sensitivity setting \a cs.
4788 */
4789
4790
4791 /*!
4792     \overload compare()
4793     \since 4.2
4794
4795     Lexically compares this string with the \a other string and
4796     returns an integer less than, equal to, or greater than zero if
4797     this string is less than, equal to, or greater than the other
4798     string.
4799
4800     Same as compare(*this, \a other, \a cs).
4801 */
4802 int QString::compare(const QString &other, Qt::CaseSensitivity cs) const
4803 {
4804     if (cs == Qt::CaseSensitive)
4805         return ucstrcmp(constData(), length(), other.constData(), other.length());
4806     return ucstricmp(d->data(), d->data() + d->size, other.d->data(), other.d->data() + other.d->size);
4807 }
4808
4809 /*!
4810     \internal
4811     \since 4.5
4812 */
4813 int QString::compare_helper(const QChar *data1, int length1, const QChar *data2, int length2,
4814                             Qt::CaseSensitivity cs)
4815 {
4816     if (cs == Qt::CaseSensitive)
4817         return ucstrcmp(data1, length1, data2, length2);
4818     register const ushort *s1 = reinterpret_cast<const ushort *>(data1);
4819     register const ushort *s2 = reinterpret_cast<const ushort *>(data2);
4820     return ucstricmp(s1, s1 + length1, s2, s2 + length2);
4821 }
4822
4823 /*!
4824     \overload compare()
4825     \since 4.2
4826
4827     Same as compare(*this, \a other, \a cs).
4828 */
4829 int QString::compare(QLatin1String other, Qt::CaseSensitivity cs) const
4830 {
4831     return compare_helper(unicode(), length(), other, cs);
4832 }
4833
4834 /*!
4835   \fn int QString::compare(const QStringRef &ref, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
4836   \overload compare()
4837
4838   Compares the string reference, \a ref, with the string and returns
4839   an integer less than, equal to, or greater than zero if the string
4840   is less than, equal to, or greater than \a ref.
4841 */
4842
4843 /*!
4844     \internal
4845     \since 5.0
4846 */
4847 int QString::compare_helper(const QChar *data1, int length1, const char *data2, int length2,
4848                             Qt::CaseSensitivity cs)
4849 {
4850     // ### optimize me
4851     const QString s2 = QString::fromUtf8(data2, length2 == -1 ? (data2 ? int(strlen(data2)) : -1) : length2);
4852     return compare_helper(data1, length1, s2.constData(), s2.size(), cs);
4853 }
4854
4855 /*!
4856   \fn int QString::compare(const QString &s1, const QStringRef &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
4857   \overload compare()
4858 */
4859
4860 /*!
4861     \internal
4862     \since 4.5
4863 */
4864 int QString::compare_helper(const QChar *data1, int length1, QLatin1String s2,
4865                             Qt::CaseSensitivity cs)
4866 {
4867     const ushort *uc = reinterpret_cast<const ushort *>(data1);
4868     const ushort *uce = uc + length1;
4869     const uchar *c = (uchar *)s2.latin1();
4870
4871     if (!c)
4872         return length1;
4873
4874     if (cs == Qt::CaseSensitive) {
4875         const ushort *e = uc + length1;
4876         if (s2.size() < length1)
4877             e = uc + s2.size();
4878         while (uc < e) {
4879             int diff = *uc - *c;
4880             if (diff)
4881                 return diff;
4882             uc++, c++;
4883         }
4884
4885         if (uc == uce) {
4886             if (c == (const uchar *)s2.latin1() + s2.size())
4887                 return 0;
4888             return -1;
4889         }
4890         return 1;
4891     } else {
4892         return ucstricmp(uc, uce, c, c + s2.size());
4893     }
4894 }
4895
4896 /*!
4897     \fn int QString::localeAwareCompare(const QString & s1, const QString & s2)
4898
4899     Compares \a s1 with \a s2 and returns an integer less than, equal
4900     to, or greater than zero if \a s1 is less than, equal to, or
4901     greater than \a s2.
4902
4903     The comparison is performed in a locale- and also
4904     platform-dependent manner. Use this function to present sorted
4905     lists of strings to the user.
4906
4907     On Mac OS X since Qt 4.3, this function compares according the
4908     "Order for sorted lists" setting in the International prefereces panel.
4909
4910     \sa compare(), QTextCodec::locale()
4911 */
4912
4913 /*!
4914     \fn int QString::localeAwareCompare(const QStringRef &other) const
4915     \since 4.5
4916     \overload localeAwareCompare()
4917
4918     Compares this string with the \a other string and returns an
4919     integer less than, equal to, or greater than zero if this string
4920     is less than, equal to, or greater than the \a other string.
4921
4922     The comparison is performed in a locale- and also
4923     platform-dependent manner. Use this function to present sorted
4924     lists of strings to the user.
4925
4926     Same as \c {localeAwareCompare(*this, other)}.
4927 */
4928
4929 /*!
4930     \fn int QString::localeAwareCompare(const QString &s1, const QStringRef &s2)
4931     \since 4.5
4932     \overload localeAwareCompare()
4933
4934     Compares \a s1 with \a s2 and returns an integer less than, equal
4935     to, or greater than zero if \a s1 is less than, equal to, or
4936     greater than \a s2.
4937
4938     The comparison is performed in a locale- and also
4939     platform-dependent manner. Use this function to present sorted
4940     lists of strings to the user.
4941 */
4942
4943
4944 #if !defined(CSTR_LESS_THAN)
4945 #define CSTR_LESS_THAN    1
4946 #define CSTR_EQUAL        2
4947 #define CSTR_GREATER_THAN 3
4948 #endif
4949
4950 /*!
4951     \overload localeAwareCompare()
4952
4953     Compares this string with the \a other string and returns an
4954     integer less than, equal to, or greater than zero if this string
4955     is less than, equal to, or greater than the \a other string.
4956
4957     The comparison is performed in a locale- and also
4958     platform-dependent manner. Use this function to present sorted
4959     lists of strings to the user.
4960
4961     Same as \c {localeAwareCompare(*this, other)}.
4962 */
4963 int QString::localeAwareCompare(const QString &other) const
4964 {
4965     return localeAwareCompare_helper(constData(), length(), other.constData(), other.length());
4966 }
4967
4968 /*!
4969     \internal
4970     \since 4.5
4971 */
4972 int QString::localeAwareCompare_helper(const QChar *data1, int length1,
4973                                        const QChar *data2, int length2)
4974 {
4975     // do the right thing for null and empty
4976     if (length1 == 0 || length2 == 0)
4977         return ucstrcmp(data1, length1, data2, length2);
4978
4979 #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
4980     int res = CompareString(GetUserDefaultLCID(), 0, (wchar_t*)data1, length1, (wchar_t*)data2, length2);
4981
4982     switch (res) {
4983     case CSTR_LESS_THAN:
4984         return -1;
4985     case CSTR_GREATER_THAN:
4986         return 1;
4987     default:
4988         return 0;
4989     }
4990 #elif defined (Q_OS_MAC)
4991     // Use CFStringCompare for comparing strings on Mac. This makes Qt order
4992     // strings the same way as native applications do, and also respects
4993     // the "Order for sorted lists" setting in the International preferences
4994     // panel.
4995     const CFStringRef thisString =
4996         CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault,
4997             reinterpret_cast<const UniChar *>(data1), length1, kCFAllocatorNull);
4998     const CFStringRef otherString =
4999         CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault,
5000             reinterpret_cast<const UniChar *>(data2), length2, kCFAllocatorNull);
5001
5002     const int result = CFStringCompare(thisString, otherString, kCFCompareLocalized);
5003     CFRelease(thisString);
5004     CFRelease(otherString);
5005     return result;
5006 #elif defined(Q_OS_UNIX)
5007 #  if defined(QT_USE_ICU)
5008     QLocale locale;
5009     int res;
5010     if (QIcu::strcoll(locale.d.constData()->m_localeID, data1, length1, data2, length2, &res))
5011         return res;
5012     // else fall through
5013 #  endif
5014     // declared in <string.h>
5015     int delta = strcoll(toLocal8Bit_helper(data1, length1).constData(), toLocal8Bit_helper(data2, length2).constData());
5016     if (delta == 0)
5017         delta = ucstrcmp(data1, length1, data2, length2);
5018     return delta;
5019 #else
5020     return ucstrcmp(data1, length1, data2, length2);
5021 #endif
5022 }
5023
5024
5025 /*!
5026     \fn const QChar *QString::unicode() const
5027
5028     Returns a '\\0'-terminated Unicode representation of the string.
5029     The result remains valid until the string is modified.
5030
5031     \sa utf16()
5032 */
5033
5034 /*!
5035     \fn const ushort *QString::utf16() const
5036
5037     Returns the QString as a '\\0\'-terminated array of unsigned
5038     shorts. The result remains valid until the string is modified.
5039
5040     The returned string is in host byte order.
5041
5042     \sa unicode()
5043 */
5044
5045 const ushort *QString::utf16() const
5046 {
5047     if (IS_RAW_DATA(d)) {
5048         // ensure '\0'-termination for ::fromRawData strings
5049         const_cast<QString*>(this)->reallocData(uint(d->size) + 1u);
5050     }
5051     return d->data();
5052 }
5053
5054 /*!
5055     Returns a string of size \a width that contains this string
5056     padded by the \a fill character.
5057
5058     If \a truncate is false and the size() of the string is more than
5059     \a width, then the returned string is a copy of the string.
5060
5061     \snippet qstring/main.cpp 32
5062
5063     If \a truncate is true and the size() of the string is more than
5064     \a width, then any characters in a copy of the string after
5065     position \a width are removed, and the copy is returned.
5066
5067     \snippet qstring/main.cpp 33
5068
5069     \sa rightJustified()
5070 */
5071
5072 QString QString::leftJustified(int width, QChar fill, bool truncate) const
5073 {
5074     QString result;
5075     int len = length();
5076     int padlen = width - len;
5077     if (padlen > 0) {
5078         result.resize(len+padlen);
5079         if (len)
5080             memcpy(result.d->data(), d->data(), sizeof(QChar)*len);
5081         QChar *uc = (QChar*)result.d->data() + len;
5082         while (padlen--)
5083            * uc++ = fill;
5084     } else {
5085         if (truncate)
5086             result = left(width);
5087         else
5088             result = *this;
5089     }
5090     return result;
5091 }
5092
5093 /*!
5094     Returns a string of size() \a width that contains the \a fill
5095     character followed by the string. For example:
5096
5097     \snippet qstring/main.cpp 49
5098
5099     If \a truncate is false and the size() of the string is more than
5100     \a width, then the returned string is a copy of the string.
5101
5102     If \a truncate is true and the size() of the string is more than
5103     \a width, then the resulting string is truncated at position \a
5104     width.
5105
5106     \snippet qstring/main.cpp 50
5107
5108     \sa leftJustified()
5109 */
5110
5111 QString QString::rightJustified(int width, QChar fill, bool truncate) const
5112 {
5113     QString result;
5114     int len = length();
5115     int padlen = width - len;
5116     if (padlen > 0) {
5117         result.resize(len+padlen);
5118         QChar *uc = (QChar*)result.d->data();
5119         while (padlen--)
5120            * uc++ = fill;
5121         if (len)
5122             memcpy(uc, d->data(), sizeof(QChar)*len);
5123     } else {
5124         if (truncate)
5125             result = left(width);
5126         else
5127             result = *this;
5128     }
5129     return result;
5130 }
5131
5132 /*!
5133     Returns a lowercase copy of the string.
5134
5135     \snippet qstring/main.cpp 75
5136
5137     The case conversion will always happen in the 'C' locale. For locale dependent
5138     case folding use QLocale::toLower()
5139
5140     \sa toUpper(), QLocale::toLower()
5141 */
5142
5143 QString QString::toLower() const
5144 {
5145     const ushort *p = d->data();
5146     if (!p)
5147         return *this;
5148
5149     const ushort *e = p + d->size;
5150     // this avoids out of bounds check in the loop
5151     while (e != p && QChar::isHighSurrogate(*(e - 1)))
5152         --e;
5153
5154     const QUnicodeTables::Properties *prop;
5155     while (p != e) {
5156         if (QChar::isHighSurrogate(*p) && QChar::isLowSurrogate(p[1])) {
5157             ushort high = *p++;
5158             prop = qGetProp(QChar::surrogateToUcs4(high, *p));
5159         } else {
5160             prop = qGetProp(*p);
5161         }
5162         if (prop->lowerCaseDiff) {
5163             if (QChar::isLowSurrogate(*p))
5164                 --p; // safe; diff is 0 for surrogates
5165             QString s(d->size, Qt::Uninitialized);
5166             memcpy(s.d->data(), d->data(), (p - d->data())*sizeof(ushort));
5167             ushort *pp = s.d->data() + (p - d->data());
5168             while (p != e) {
5169                 if (QChar::isHighSurrogate(*p) && QChar::isLowSurrogate(p[1])) {
5170                     *pp = *p++;
5171                     prop = qGetProp(QChar::surrogateToUcs4(*pp++, *p));
5172                 } else {
5173                     prop = qGetProp(*p);
5174                 }
5175                 if (prop->lowerCaseSpecial) {
5176                     const ushort *specialCase = specialCaseMap + prop->lowerCaseDiff;
5177                     ushort length = *specialCase++;
5178                     int pos = pp - s.d->data();
5179                     s.resize(s.d->size + length - 1);
5180                     pp = s.d->data() + pos;
5181                     while (length--)
5182                         *pp++ = *specialCase++;
5183                 } else {
5184                     *pp++ = *p + prop->lowerCaseDiff;
5185                 }
5186                 ++p;
5187             }
5188
5189             // this restores high surrogate parts eaten above, if any
5190             while (e != d->data() + d->size)
5191                 *pp++ = *e++;
5192
5193             return s;
5194         }
5195         ++p;
5196     }
5197     return *this;
5198 }
5199
5200 /*!
5201     Returns the case folded equivalent of the string. For most Unicode
5202     characters this is the same as toLower().
5203 */
5204 QString QString::toCaseFolded() const
5205 {
5206     const ushort *p = d->data();
5207     if (!p)
5208         return *this;
5209
5210     const ushort *e = p + d->size;
5211     // this avoids out of bounds check in the loop
5212     while (e != p && QChar::isHighSurrogate(*(e - 1)))
5213         --e;
5214
5215     const QUnicodeTables::Properties *prop;
5216     while (p != e) {
5217         if (QChar::isHighSurrogate(*p) && QChar::isLowSurrogate(p[1])) {
5218             ushort high = *p++;
5219             prop = qGetProp(QChar::surrogateToUcs4(high, *p));
5220         } else {
5221             prop = qGetProp(*p);
5222         }
5223         if (prop->caseFoldDiff) {
5224             if (QChar::isLowSurrogate(*p))
5225                 --p; // safe; diff is 0 for surrogates
5226             QString s(d->size, Qt::Uninitialized);
5227             memcpy(s.d->data(), d->data(), (p - d->data())*sizeof(ushort));
5228             ushort *pp = s.d->data() + (p - d->data());
5229             while (p != e) {
5230                 if (QChar::isHighSurrogate(*p) && QChar::isLowSurrogate(p[1])) {
5231                     *pp = *p++;
5232                     prop = qGetProp(QChar::surrogateToUcs4(*pp++, *p));
5233                 } else {
5234                     prop = qGetProp(*p);
5235                 }
5236                 if (prop->caseFoldSpecial) {
5237                     const ushort *specialCase = specialCaseMap + prop->caseFoldDiff;
5238                     ushort length = *specialCase++;
5239 #if 0
5240                     int pos = pp - s.d->data;
5241                     s.resize(s.d->size + length - 1);
5242                     pp = s.d->data + pos;
5243                     while (length--)
5244                         *pp++ = *specialCase++;
5245 #else
5246                     //### we currently don't support full case foldings
5247                     Q_ASSERT(length == 1);
5248                     Q_UNUSED(length)
5249                     *pp++ = *specialCase;
5250 #endif
5251                 } else {
5252                     *pp++ = *p + prop->caseFoldDiff;
5253                 }
5254                 ++p;
5255             }
5256
5257             // this restores high surrogate parts eaten above, if any
5258             while (e != d->data() + d->size)
5259                 *pp++ = *e++;
5260
5261             return s;
5262         }
5263         ++p;
5264     }
5265     return *this;
5266 }
5267
5268 /*!
5269     Returns an uppercase copy of the string.
5270
5271     \snippet qstring/main.cpp 81
5272
5273     The case conversion will always happen in the 'C' locale. For locale dependent
5274     case folding use QLocale::toUpper()
5275
5276     \sa toLower(), QLocale::toLower()
5277 */
5278 QString QString::toUpper() const
5279 {
5280     const ushort *p = d->data();
5281     if (!p)
5282         return *this;
5283
5284     const ushort *e = p + d->size;
5285     // this avoids out of bounds check in the loop
5286     while (e != p && QChar::isHighSurrogate(*(e - 1)))
5287         --e;
5288
5289     const QUnicodeTables::Properties *prop;
5290     while (p != e) {
5291         if (QChar::isHighSurrogate(*p) && QChar::isLowSurrogate(p[1])) {
5292             ushort high = *p++;
5293             prop = qGetProp(QChar::surrogateToUcs4(high, *p));
5294         } else {
5295             prop = qGetProp(*p);
5296         }
5297         if (prop->upperCaseDiff) {
5298             if (QChar::isLowSurrogate(*p))
5299                 --p; // safe; diff is 0 for surrogates
5300             QString s(d->size, Qt::Uninitialized);
5301             memcpy(s.d->data(), d->data(), (p - d->data())*sizeof(ushort));
5302             ushort *pp = s.d->data() + (p - d->data());
5303             while (p != e) {
5304                 if (QChar::isHighSurrogate(*p) && QChar::isLowSurrogate(p[1])) {
5305                     *pp = *p++;
5306                     prop = qGetProp(QChar::surrogateToUcs4(*pp++, *p));
5307                 } else {
5308                     prop = qGetProp(*p);
5309                 }
5310                 if (prop->upperCaseSpecial) {
5311                     const ushort *specialCase = specialCaseMap + prop->upperCaseDiff;
5312                     ushort length = *specialCase++;
5313                     int pos = pp - s.d->data();
5314                     s.resize(s.d->size + length - 1);
5315                     pp = s.d->data() + pos;
5316                     while (length--)
5317                         *pp++ = *specialCase++;
5318                 } else {
5319                     *pp++ = *p + prop->upperCaseDiff;
5320                 }
5321                 ++p;
5322             }
5323
5324             // this restores high surrogate parts eaten above, if any
5325             while (e != d->data() + d->size)
5326                 *pp++ = *e++;
5327
5328             return s;
5329         }
5330         ++p;
5331     }
5332     return *this;
5333 }
5334
5335 // ### Qt 6: Consider whether this function shouldn't be removed See task 202871.
5336 /*!
5337     Safely builds a formatted string from the format string \a cformat
5338     and an arbitrary list of arguments.
5339
5340     The %lc escape sequence expects a unicode character of type ushort
5341     (as returned by QChar::unicode()). The %ls escape sequence expects
5342     a pointer to a zero-terminated array of unicode characters of type
5343     ushort (as returned by QString::utf16()).
5344
5345     \note This function expects a UTF-8 string for %s and Latin-1 for
5346     the format string.
5347
5348     The format string supports most of the conversion specifiers
5349     provided by printf() in the standard C++ library. It doesn't
5350     honor the length modifiers (e.g. \c h for \c short, \c ll for
5351     \c{long long}). If you need those, use the standard snprintf()
5352     function instead:
5353
5354     \snippet qstring/main.cpp 63
5355
5356     \warning We do not recommend using QString::sprintf() in new Qt
5357     code. Instead, consider using QTextStream or arg(), both of
5358     which support Unicode strings seamlessly and are type-safe.
5359     Here's an example that uses QTextStream:
5360
5361     \snippet qstring/main.cpp 64
5362
5363     For \l {QObject::tr()}{translations}, especially if the strings
5364     contains more than one escape sequence, you should consider using
5365     the arg() function instead. This allows the order of the
5366     replacements to be controlled by the translator.
5367
5368     \sa arg()
5369 */
5370
5371 QString &QString::sprintf(const char *cformat, ...)
5372 {
5373     va_list ap;
5374     va_start(ap, cformat);
5375     QString &s = vsprintf(cformat, ap);
5376     va_end(ap);
5377     return s;
5378 }
5379
5380 /*!
5381     Equivalent method to sprintf(), but takes a va_list \a ap
5382     instead a list of variable arguments. See the sprintf()
5383     documentation for an explanation of \a cformat.
5384
5385     This method does not call the va_end macro, the caller
5386     is responsible to call va_end on \a ap.
5387
5388     \sa sprintf()
5389 */
5390
5391 QString &QString::vsprintf(const char* cformat, va_list ap)
5392 {
5393     QLocale locale(QLocale::C);
5394
5395     if (!cformat || !*cformat) {
5396         // Qt 1.x compat
5397         *this = fromLatin1("");
5398         return *this;
5399     }
5400
5401     // Parse cformat
5402
5403     QString result;
5404     const char *c = cformat;
5405     for (;;) {
5406         // Copy non-escape chars to result
5407         while (*c != '\0' && *c != '%')
5408             result.append(QLatin1Char(*c++));
5409
5410         if (*c == '\0')
5411             break;
5412
5413         // Found '%'
5414         const char *escape_start = c;
5415         ++c;
5416
5417         if (*c == '\0') {
5418             result.append(QLatin1Char('%')); // a % at the end of the string - treat as non-escape text
5419             break;
5420         }
5421         if (*c == '%') {
5422             result.append(QLatin1Char('%')); // %%
5423             ++c;
5424             continue;
5425         }
5426
5427         // Parse flag characters
5428         uint flags = 0;
5429         bool no_more_flags = false;
5430         do {
5431             switch (*c) {
5432                 case '#': flags |= QLocalePrivate::Alternate; break;
5433                 case '0': flags |= QLocalePrivate::ZeroPadded; break;
5434                 case '-': flags |= QLocalePrivate::LeftAdjusted; break;
5435                 case ' ': flags |= QLocalePrivate::BlankBeforePositive; break;
5436                 case '+': flags |= QLocalePrivate::AlwaysShowSign; break;
5437                 case '\'': flags |= QLocalePrivate::ThousandsGroup; break;
5438                 default: no_more_flags = true; break;
5439             }
5440
5441             if (!no_more_flags)
5442                 ++c;
5443         } while (!no_more_flags);
5444
5445         if (*c == '\0') {
5446             result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text
5447             break;
5448         }
5449
5450         // Parse field width
5451         int width = -1; // -1 means unspecified
5452         if (qIsDigit(*c)) {
5453             QString width_str;
5454             while (*c != '\0' && qIsDigit(*c))
5455                 width_str.append(QLatin1Char(*c++));
5456
5457             // can't be negative - started with a digit
5458             // contains at least one digit
5459             width = width_str.toInt();
5460         }
5461         else if (*c == '*') {
5462             width = va_arg(ap, int);
5463             if (width < 0)
5464                 width = -1; // treat all negative numbers as unspecified
5465             ++c;
5466         }
5467
5468         if (*c == '\0') {
5469             result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text
5470             break;
5471         }
5472
5473         // Parse precision
5474         int precision = -1; // -1 means unspecified
5475         if (*c == '.') {
5476             ++c;
5477             if (qIsDigit(*c)) {
5478                 QString precision_str;
5479                 while (*c != '\0' && qIsDigit(*c))
5480                     precision_str.append(QLatin1Char(*c++));
5481
5482                 // can't be negative - started with a digit
5483                 // contains at least one digit
5484                 precision = precision_str.toInt();
5485             }
5486             else if (*c == '*') {
5487                 precision = va_arg(ap, int);
5488                 if (precision < 0)
5489                     precision = -1; // treat all negative numbers as unspecified
5490                 ++c;
5491             }
5492         }
5493
5494         if (*c == '\0') {
5495             result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text
5496             break;
5497         }
5498
5499         // Parse the length modifier
5500         enum LengthMod { lm_none, lm_hh, lm_h, lm_l, lm_ll, lm_L, lm_j, lm_z, lm_t };
5501         LengthMod length_mod = lm_none;
5502         switch (*c) {
5503             case 'h':
5504                 ++c;
5505                 if (*c == 'h') {
5506                     length_mod = lm_hh;
5507                     ++c;
5508                 }
5509                 else
5510                     length_mod = lm_h;
5511                 break;
5512
5513             case 'l':
5514                 ++c;
5515                 if (*c == 'l') {
5516                     length_mod = lm_ll;
5517                     ++c;
5518                 }
5519                 else
5520                     length_mod = lm_l;
5521                 break;
5522
5523             case 'L':
5524                 ++c;
5525                 length_mod = lm_L;
5526                 break;
5527
5528             case 'j':
5529                 ++c;
5530                 length_mod = lm_j;
5531                 break;
5532
5533             case 'z':
5534             case 'Z':
5535                 ++c;
5536                 length_mod = lm_z;
5537                 break;
5538
5539             case 't':
5540                 ++c;
5541                 length_mod = lm_t;
5542                 break;
5543
5544             default: break;
5545         }
5546
5547         if (*c == '\0') {
5548             result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text
5549             break;
5550         }
5551
5552         // Parse the conversion specifier and do the conversion
5553         QString subst;
5554         switch (*c) {
5555             case 'd':
5556             case 'i': {
5557                 qint64 i;
5558                 switch (length_mod) {
5559                     case lm_none: i = va_arg(ap, int); break;
5560                     case lm_hh: i = va_arg(ap, int); break;
5561                     case lm_h: i = va_arg(ap, int); break;
5562                     case lm_l: i = va_arg(ap, long int); break;
5563                     case lm_ll: i = va_arg(ap, qint64); break;
5564                     case lm_j: i = va_arg(ap, long int); break;
5565                     case lm_z: i = va_arg(ap, size_t); break;
5566                     case lm_t: i = va_arg(ap, int); break;
5567                     default: i = 0; break;
5568                 }
5569                 subst = locale.d->longLongToString(i, precision, 10, width, flags);
5570                 ++c;
5571                 break;
5572             }
5573             case 'o':
5574             case 'u':
5575             case 'x':
5576             case 'X': {
5577                 quint64 u;
5578                 switch (length_mod) {
5579                     case lm_none: u = va_arg(ap, uint); break;
5580                     case lm_hh: u = va_arg(ap, uint); break;
5581                     case lm_h: u = va_arg(ap, uint); break;
5582                     case lm_l: u = va_arg(ap, ulong); break;
5583                     case lm_ll: u = va_arg(ap, quint64); break;
5584                     case lm_z: u = va_arg(ap, size_t); break;
5585                     default: u = 0; break;
5586                 }
5587
5588                 if (qIsUpper(*c))
5589                     flags |= QLocalePrivate::CapitalEorX;
5590
5591                 int base = 10;
5592                 switch (qToLower(*c)) {
5593                     case 'o':
5594                         base = 8; break;
5595                     case 'u':
5596                         base = 10; break;
5597                     case 'x':
5598                         base = 16; break;
5599                     default: break;
5600                 }
5601                 subst = locale.d->unsLongLongToString(u, precision, base, width, flags);
5602                 ++c;
5603                 break;
5604             }
5605             case 'E':
5606             case 'e':
5607             case 'F':
5608             case 'f':
5609             case 'G':
5610             case 'g':
5611             case 'A':
5612             case 'a': {
5613                 double d;
5614                 if (length_mod == lm_L)
5615                     d = va_arg(ap, long double); // not supported - converted to a double
5616                 else
5617                     d = va_arg(ap, double);
5618
5619                 if (qIsUpper(*c))
5620                     flags |= QLocalePrivate::CapitalEorX;
5621
5622                 QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
5623                 switch (qToLower(*c)) {
5624                     case 'e': form = QLocalePrivate::DFExponent; break;
5625                     case 'a':                             // not supported - decimal form used instead
5626                     case 'f': form = QLocalePrivate::DFDecimal; break;
5627                     case 'g': form = QLocalePrivate::DFSignificantDigits; break;
5628                     default: break;
5629                 }
5630                 subst = locale.d->doubleToString(d, precision, form, width, flags);
5631                 ++c;
5632                 break;
5633             }
5634             case 'c': {
5635                 if (length_mod == lm_l)
5636                     subst = QChar((ushort) va_arg(ap, int));
5637                 else
5638                     subst = QLatin1Char((uchar) va_arg(ap, int));
5639                 ++c;
5640                 break;
5641             }
5642             case 's': {
5643                 if (length_mod == lm_l) {
5644                     const ushort *buff = va_arg(ap, const ushort*);
5645                     const ushort *ch = buff;
5646                     while (*ch != 0)
5647                         ++ch;
5648                     subst.setUtf16(buff, ch - buff);
5649                 } else
5650                     subst = QString::fromUtf8(va_arg(ap, const char*));
5651                 if (precision != -1)
5652                     subst.truncate(precision);
5653                 ++c;
5654                 break;
5655             }
5656             case 'p': {
5657                 void *arg = va_arg(ap, void*);
5658 #ifdef Q_OS_WIN64
5659                 quint64 i = reinterpret_cast<quint64>(arg);
5660 #else
5661                 quint64 i = reinterpret_cast<unsigned long>(arg);
5662 #endif
5663                 flags |= QLocalePrivate::Alternate;
5664                 subst = locale.d->unsLongLongToString(i, precision, 16, width, flags);
5665                 ++c;
5666                 break;
5667             }
5668             case 'n':
5669                 switch (length_mod) {
5670                     case lm_hh: {
5671                         signed char *n = va_arg(ap, signed char*);
5672                         *n = result.length();
5673                         break;
5674                     }
5675                     case lm_h: {
5676                         short int *n = va_arg(ap, short int*);
5677                         *n = result.length();
5678                             break;
5679                     }
5680                     case lm_l: {
5681                         long int *n = va_arg(ap, long int*);
5682                         *n = result.length();
5683                         break;
5684                     }
5685                     case lm_ll: {
5686                         qint64 *n = va_arg(ap, qint64*);
5687                         volatile uint tmp = result.length(); // egcs-2.91.66 gets internal
5688                         *n = tmp;                             // compiler error without volatile
5689                         break;
5690                     }
5691                     default: {
5692                         int *n = va_arg(ap, int*);
5693                         *n = result.length();
5694                         break;
5695                     }
5696                 }
5697                 ++c;
5698                 break;
5699
5700             default: // bad escape, treat as non-escape text
5701                 for (const char *cc = escape_start; cc != c; ++cc)
5702                     result.append(QLatin1Char(*cc));
5703                 continue;
5704         }
5705
5706         if (flags & QLocalePrivate::LeftAdjusted)
5707             result.append(subst.leftJustified(width));
5708         else
5709             result.append(subst.rightJustified(width));
5710     }
5711
5712     *this = result;
5713
5714     return *this;
5715 }
5716
5717 /*!
5718     Returns the string converted to a \c{long long} using base \a
5719     base, which is 10 by default and must be between 2 and 36, or 0.
5720     Returns 0 if the conversion fails.
5721
5722     If a conversion error occurs, *\a{ok} is set to false; otherwise
5723     *\a{ok} is set to true.
5724
5725     If \a base is 0, the C language convention is used: If the string
5726     begins with "0x", base 16 is used; if the string begins with "0",
5727     base 8 is used; otherwise, base 10 is used.
5728
5729     The string conversion will always happen in the 'C' locale. For locale
5730     dependent conversion use QLocale::toLongLong()
5731
5732     Example:
5733
5734     \snippet qstring/main.cpp 74
5735
5736     \sa number(), toULongLong(), toInt(), QLocale::toLongLong()
5737 */
5738
5739 qint64 QString::toLongLong(bool *ok, int base) const
5740 {
5741 #if defined(QT_CHECK_RANGE)
5742     if (base != 0 && (base < 2 || base > 36)) {
5743         qWarning("QString::toLongLong: Invalid base (%d)", base);
5744         base = 10;
5745     }
5746 #endif
5747
5748     QLocale c_locale(QLocale::C);
5749     return c_locale.d->stringToLongLong(*this, base, ok, QLocalePrivate::FailOnGroupSeparators);
5750 }
5751
5752 /*!
5753     Returns the string converted to an \c{unsigned long long} using base \a
5754     base, which is 10 by default and must be between 2 and 36, or 0.
5755     Returns 0 if the conversion fails.
5756
5757     If a conversion error occurs, *\a{ok} is set to false; otherwise
5758     *\a{ok} is set to true.
5759
5760     If \a base is 0, the C language convention is used: If the string
5761     begins with "0x", base 16 is used; if the string begins with "0",
5762     base 8 is used; otherwise, base 10 is used.
5763
5764     The string conversion will always happen in the 'C' locale. For locale
5765     dependent conversion use QLocale::toULongLong()
5766
5767     Example:
5768
5769     \snippet qstring/main.cpp 79
5770
5771     \sa number(), toLongLong(), QLocale::toULongLong()
5772 */
5773
5774 quint64 QString::toULongLong(bool *ok, int base) const
5775 {
5776 #if defined(QT_CHECK_RANGE)
5777     if (base != 0 && (base < 2 || base > 36)) {
5778         qWarning("QString::toULongLong: Invalid base (%d)", base);
5779         base = 10;
5780     }
5781 #endif
5782
5783     QLocale c_locale(QLocale::C);
5784     return c_locale.d->stringToUnsLongLong(*this, base, ok, QLocalePrivate::FailOnGroupSeparators);
5785 }
5786
5787 /*!
5788     \fn long QString::toLong(bool *ok, int base) const
5789
5790     Returns the string converted to a \c long using base \a
5791     base, which is 10 by default and must be between 2 and 36, or 0.
5792     Returns 0 if the conversion fails.
5793
5794     If a conversion error occurs, *\a{ok} is set to false; otherwise
5795     *\a{ok} is set to true.
5796
5797     If \a base is 0, the C language convention is used: If the string
5798     begins with "0x", base 16 is used; if the string begins with "0",
5799     base 8 is used; otherwise, base 10 is used.
5800
5801     The string conversion will always happen in the 'C' locale. For locale
5802     dependent conversion use QLocale::toLong()
5803
5804     Example:
5805
5806     \snippet qstring/main.cpp 73
5807
5808     \sa number(), toULong(), toInt(), QLocale::toLong()
5809 */
5810
5811 long QString::toLong(bool *ok, int base) const
5812 {
5813     qint64 v = toLongLong(ok, base);
5814     if (v < LONG_MIN || v > LONG_MAX) {
5815         if (ok)
5816             *ok = false;
5817         v = 0;
5818     }
5819     return (long)v;
5820 }
5821
5822 /*!
5823     \fn ulong QString::toULong(bool *ok, int base) const
5824
5825     Returns the string converted to an \c{unsigned long} using base \a
5826     base, which is 10 by default and must be between 2 and 36, or 0.
5827     Returns 0 if the conversion fails.
5828
5829     If a conversion error occurs, *\a{ok} is set to false; otherwise
5830     *\a{ok} is set to true.
5831
5832     If \a base is 0, the C language convention is used: If the string
5833     begins with "0x", base 16 is used; if the string begins with "0",
5834     base 8 is used; otherwise, base 10 is used.
5835
5836     The string conversion will always happen in the 'C' locale. For locale
5837     dependent conversion use QLocale::toULong()
5838
5839     Example:
5840
5841     \snippet qstring/main.cpp 78
5842
5843     \sa number(), QLocale::toULong()
5844 */
5845
5846 ulong QString::toULong(bool *ok, int base) const
5847 {
5848     quint64 v = toULongLong(ok, base);
5849     if (v > ULONG_MAX) {
5850         if (ok)
5851             *ok = false;
5852         v = 0;
5853     }
5854     return (ulong)v;
5855 }
5856
5857
5858 /*!
5859     Returns the string converted to an \c int using base \a
5860     base, which is 10 by default and must be between 2 and 36, or 0.
5861     Returns 0 if the conversion fails.
5862
5863     If a conversion error occurs, *\a{ok} is set to false; otherwise
5864     *\a{ok} is set to true.
5865
5866     If \a base is 0, the C language convention is used: If the string
5867     begins with "0x", base 16 is used; if the string begins with "0",
5868     base 8 is used; otherwise, base 10 is used.
5869
5870     The string conversion will always happen in the 'C' locale. For locale
5871     dependent conversion use QLocale::toInt()
5872
5873     Example:
5874
5875     \snippet qstring/main.cpp 72
5876
5877     \sa number(), toUInt(), toDouble(), QLocale::toInt()
5878 */
5879
5880 int QString::toInt(bool *ok, int base) const
5881 {
5882     qint64 v = toLongLong(ok, base);
5883     if (v < INT_MIN || v > INT_MAX) {
5884         if (ok)
5885             *ok = false;
5886         v = 0;
5887     }
5888     return v;
5889 }
5890
5891 /*!
5892     Returns the string converted to an \c{unsigned int} using base \a
5893     base, which is 10 by default and must be between 2 and 36, or 0.
5894     Returns 0 if the conversion fails.
5895
5896     If a conversion error occurs, *\a{ok} is set to false; otherwise
5897     *\a{ok} is set to true.
5898
5899     If \a base is 0, the C language convention is used: If the string
5900     begins with "0x", base 16 is used; if the string begins with "0",
5901     base 8 is used; otherwise, base 10 is used.
5902
5903     The string conversion will always happen in the 'C' locale. For locale
5904     dependent conversion use QLocale::toUInt()
5905
5906     Example:
5907
5908     \snippet qstring/main.cpp 77
5909
5910     \sa number(), toInt(), QLocale::toUInt()
5911 */
5912
5913 uint QString::toUInt(bool *ok, int base) const
5914 {
5915     quint64 v = toULongLong(ok, base);
5916     if (v > UINT_MAX) {
5917         if (ok)
5918             *ok = false;
5919         v = 0;
5920     }
5921     return (uint)v;
5922 }
5923
5924 /*!
5925     Returns the string converted to a \c short using base \a
5926     base, which is 10 by default and must be between 2 and 36, or 0.
5927     Returns 0 if the conversion fails.
5928
5929     If a conversion error occurs, *\a{ok} is set to false; otherwise
5930     *\a{ok} is set to true.
5931
5932     If \a base is 0, the C language convention is used: If the string
5933     begins with "0x", base 16 is used; if the string begins with "0",
5934     base 8 is used; otherwise, base 10 is used.
5935
5936     The string conversion will always happen in the 'C' locale. For locale
5937     dependent conversion use QLocale::toShort()
5938
5939     Example:
5940
5941     \snippet qstring/main.cpp 76
5942
5943     \sa number(), toUShort(), toInt(), QLocale::toShort()
5944 */
5945
5946 short QString::toShort(bool *ok, int base) const
5947 {
5948     long v = toLongLong(ok, base);
5949     if (v < SHRT_MIN || v > SHRT_MAX) {
5950         if (ok)
5951             *ok = false;
5952         v = 0;
5953     }
5954     return (short)v;
5955 }
5956
5957 /*!
5958     Returns the string converted to an \c{unsigned short} using base \a
5959     base, which is 10 by default and must be between 2 and 36, or 0.
5960     Returns 0 if the conversion fails.
5961
5962     If a conversion error occurs, *\a{ok} is set to false; otherwise
5963     *\a{ok} is set to true.
5964
5965     If \a base is 0, the C language convention is used: If the string
5966     begins with "0x", base 16 is used; if the string begins with "0",
5967     base 8 is used; otherwise, base 10 is used.
5968
5969     The string conversion will always happen in the 'C' locale. For locale
5970     dependent conversion use QLocale::toUShort()
5971
5972     Example:
5973
5974     \snippet qstring/main.cpp 80
5975
5976     \sa number(), toShort(), QLocale::toUShort()
5977 */
5978
5979 ushort QString::toUShort(bool *ok, int base) const
5980 {
5981     ulong v = toULongLong(ok, base);
5982     if (v > USHRT_MAX) {
5983         if (ok)
5984             *ok = false;
5985         v = 0;
5986     }
5987     return (ushort)v;
5988 }
5989
5990
5991 /*!
5992     Returns the string converted to a \c double value.
5993
5994     Returns 0.0 if the conversion fails.
5995
5996     If a conversion error occurs, \c{*}\a{ok} is set to false;
5997     otherwise \c{*}\a{ok} is set to true.
5998
5999     \snippet qstring/main.cpp 66
6000
6001     Various string formats for floating point numbers can be converted
6002     to double values:
6003
6004     \snippet qstring/main.cpp 67
6005
6006     The string conversion will always happen in the 'C' locale. For locale
6007     dependent conversion use QLocale::toDouble()
6008
6009     \snippet qstring/main.cpp 68
6010
6011     For historic reasons, this function does not handle
6012     thousands group separators. If you need to convert such numbers,
6013     use QLocale::toDouble().
6014
6015     \snippet qstring/main.cpp 69
6016
6017     \sa number(), QLocale::setDefault(), QLocale::toDouble(), trimmed()
6018 */
6019
6020 double QString::toDouble(bool *ok) const
6021 {
6022     QLocale c_locale(QLocale::C);
6023     return c_locale.d->stringToDouble(*this, ok, QLocalePrivate::FailOnGroupSeparators);
6024 }
6025
6026 /*!
6027     Returns the string converted to a \c float value.
6028
6029     If a conversion error occurs, *\a{ok} is set to false; otherwise
6030     *\a{ok} is set to true. Returns 0.0 if the conversion fails.
6031
6032     The string conversion will always happen in the 'C' locale. For locale
6033     dependent conversion use QLocale::toFloat()
6034
6035     Example:
6036
6037     \snippet qstring/main.cpp 71
6038
6039     \sa number(), toDouble(), toInt(), QLocale::toFloat()
6040 */
6041
6042 #define QT_MAX_FLOAT 3.4028234663852886e+38
6043
6044 float QString::toFloat(bool *ok) const
6045 {
6046     bool myOk;
6047     double d = toDouble(&myOk);
6048     if (!myOk) {
6049         if (ok != 0)
6050             *ok = false;
6051         return 0.0;
6052     }
6053     if (qIsInf(d))
6054         return float(d);
6055     if (d > QT_MAX_FLOAT || d < -QT_MAX_FLOAT) {
6056         if (ok != 0)
6057             *ok = false;
6058         return 0.0;
6059     }
6060     if (ok != 0)
6061         *ok = true;
6062     return float(d);
6063 }
6064
6065 /*! \fn QString &QString::setNum(int n, int base)
6066
6067     Sets the string to the printed value of \a n in the specified \a
6068     base, and returns a reference to the string.
6069
6070     The base is 10 by default and must be between 2 and 36. For bases
6071     other than 10, \a n is treated as an unsigned integer.
6072
6073     \snippet qstring/main.cpp 56
6074
6075    The formatting always uses QLocale::C, i.e., English/UnitedStates.
6076    To get a localized string representation of a number, use
6077    QLocale::toString() with the appropriate locale.
6078 */
6079
6080 /*! \fn QString &QString::setNum(uint n, int base)
6081
6082     \overload
6083 */
6084
6085 /*! \fn QString &QString::setNum(long n, int base)
6086
6087     \overload
6088 */
6089
6090 /*! \fn QString &QString::setNum(ulong n, int base)
6091
6092     \overload
6093 */
6094
6095 /*!
6096     \overload
6097 */
6098 QString &QString::setNum(qlonglong n, int base)
6099 {
6100 #if defined(QT_CHECK_RANGE)
6101     if (base < 2 || base > 36) {
6102         qWarning("QString::setNum: Invalid base (%d)", base);
6103         base = 10;
6104     }
6105 #endif
6106     QLocale locale(QLocale::C);
6107     *this = locale.d->longLongToString(n, -1, base);
6108     return *this;
6109 }
6110
6111 /*!
6112     \overload
6113 */
6114 QString &QString::setNum(qulonglong n, int base)
6115 {
6116 #if defined(QT_CHECK_RANGE)
6117     if (base < 2 || base > 36) {
6118         qWarning("QString::setNum: Invalid base (%d)", base);
6119         base = 10;
6120     }
6121 #endif
6122     QLocale locale(QLocale::C);
6123     *this = locale.d->unsLongLongToString(n, -1, base);
6124     return *this;
6125 }
6126
6127 /*! \fn QString &QString::setNum(short n, int base)
6128
6129     \overload
6130 */
6131
6132 /*! \fn QString &QString::setNum(ushort n, int base)
6133
6134     \overload
6135 */
6136
6137 /*!
6138     \fn QString &QString::setNum(double n, char format, int precision)
6139     \overload
6140
6141     Sets the string to the printed value of \a n, formatted according
6142     to the given \a format and \a precision, and returns a reference
6143     to the string.
6144
6145     The \a format can be 'f', 'F', 'e', 'E', 'g' or 'G' (see the
6146     arg() function documentation for an explanation of the formats).
6147
6148     The formatting always uses QLocale::C, i.e., English/UnitedStates.
6149     To get a localized string representation of a number, use
6150     QLocale::toString() with the appropriate locale.
6151 */
6152
6153 QString &QString::setNum(double n, char f, int prec)
6154 {
6155     QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
6156     uint flags = 0;
6157
6158     if (qIsUpper(f))
6159         flags = QLocalePrivate::CapitalEorX;
6160     f = qToLower(f);
6161
6162     switch (f) {
6163         case 'f':
6164             form = QLocalePrivate::DFDecimal;
6165             break;
6166         case 'e':
6167             form = QLocalePrivate::DFExponent;
6168             break;
6169         case 'g':
6170             form = QLocalePrivate::DFSignificantDigits;
6171             break;
6172         default:
6173 #if defined(QT_CHECK_RANGE)
6174             qWarning("QString::setNum: Invalid format char '%c'", f);
6175 #endif
6176             break;
6177     }
6178
6179     QLocale locale(QLocale::C);
6180     *this = locale.d->doubleToString(n, prec, form, -1, flags);
6181     return *this;
6182 }
6183
6184 /*!
6185     \fn QString &QString::setNum(float n, char format, int precision)
6186     \overload
6187
6188     Sets the string to the printed value of \a n, formatted according
6189     to the given \a format and \a precision, and returns a reference
6190     to the string.
6191
6192     The formatting always uses QLocale::C, i.e., English/UnitedStates.
6193     To get a localized string representation of a number, use
6194     QLocale::toString() with the appropriate locale.
6195 */
6196
6197
6198 /*!
6199     \fn QString QString::number(long n, int base)
6200
6201     Returns a string equivalent of the number \a n according to the
6202     specified \a base.
6203
6204     The base is 10 by default and must be between 2
6205     and 36. For bases other than 10, \a n is treated as an
6206     unsigned integer.
6207
6208     The formatting always uses QLocale::C, i.e., English/UnitedStates.
6209     To get a localized string representation of a number, use
6210     QLocale::toString() with the appropriate locale.
6211
6212     \snippet qstring/main.cpp 35
6213
6214     \sa setNum()
6215 */
6216
6217 QString QString::number(long n, int base)
6218 {
6219     QString s;
6220     s.setNum(n, base);
6221     return s;
6222 }
6223
6224 /*!
6225   \fn QString QString::number(ulong n, int base)
6226
6227     \overload
6228 */
6229 QString QString::number(ulong n, int base)
6230 {
6231     QString s;
6232     s.setNum(n, base);
6233     return s;
6234 }
6235
6236 /*!
6237     \overload
6238 */
6239 QString QString::number(int n, int base)
6240 {
6241     QString s;
6242     s.setNum(n, base);
6243     return s;
6244 }
6245
6246 /*!
6247     \overload
6248 */
6249 QString QString::number(uint n, int base)
6250 {
6251     QString s;
6252     s.setNum(n, base);
6253     return s;
6254 }
6255
6256 /*!
6257     \overload
6258 */
6259 QString QString::number(qlonglong n, int base)
6260 {
6261     QString s;
6262     s.setNum(n, base);
6263     return s;
6264 }
6265
6266 /*!
6267     \overload
6268 */
6269 QString QString::number(qulonglong n, int base)
6270 {
6271     QString s;
6272     s.setNum(n, base);
6273     return s;
6274 }
6275
6276
6277 /*!
6278     \fn QString QString::number(double n, char format, int precision)
6279
6280     Returns a string equivalent of the number \a n, formatted
6281     according to the specified \a format and \a precision. See
6282     \l{Argument Formats} for details.
6283
6284     Unlike QLocale::toString(), this function does not honor the
6285     user's locale settings.
6286
6287     \sa setNum(), QLocale::toString()
6288 */
6289 QString QString::number(double n, char f, int prec)
6290 {
6291     QString s;
6292     s.setNum(n, f, prec);
6293     return s;
6294 }
6295
6296 /*!
6297     Splits the string into substrings wherever \a sep occurs, and
6298     returns the list of those strings. If \a sep does not match
6299     anywhere in the string, split() returns a single-element list
6300     containing this string.
6301
6302     \a cs specifies whether \a sep should be matched case
6303     sensitively or case insensitively.
6304
6305     If \a behavior is QString::SkipEmptyParts, empty entries don't
6306     appear in the result. By default, empty entries are kept.
6307
6308     Example:
6309
6310     \snippet qstring/main.cpp 62
6311
6312     \sa QStringList::join(), section()
6313 */
6314 QStringList QString::split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
6315 {
6316     QStringList list;
6317     int start = 0;
6318     int extra = 0;
6319     int end;
6320     while ((end = indexOf(sep, start + extra, cs)) != -1) {
6321         if (start != end || behavior == KeepEmptyParts)
6322             list.append(mid(start, end - start));
6323         start = end + sep.size();
6324         extra = (sep.size() == 0 ? 1 : 0);
6325     }
6326     if (start != size() || behavior == KeepEmptyParts)
6327         list.append(mid(start));
6328     return list;
6329 }
6330
6331 /*!
6332     \overload
6333 */
6334 QStringList QString::split(QChar sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
6335 {
6336     QStringList list;
6337     int start = 0;
6338     int end;
6339     while ((end = indexOf(sep, start, cs)) != -1) {
6340         if (start != end || behavior == KeepEmptyParts)
6341             list.append(mid(start, end - start));
6342         start = end + 1;
6343     }
6344     if (start != size() || behavior == KeepEmptyParts)
6345         list.append(mid(start));
6346     return list;
6347 }
6348
6349 #ifndef QT_NO_REGEXP
6350 /*!
6351     \overload
6352
6353     Splits the string into substrings wherever the regular expression
6354     \a rx matches, and returns the list of those strings. If \a rx
6355     does not match anywhere in the string, split() returns a
6356     single-element list containing this string.
6357
6358     Here's an example where we extract the words in a sentence
6359     using one or more whitespace characters as the separator:
6360
6361     \snippet qstring/main.cpp 59
6362
6363     Here's a similar example, but this time we use any sequence of
6364     non-word characters as the separator:
6365
6366     \snippet qstring/main.cpp 60
6367
6368     Here's a third example where we use a zero-length assertion,
6369     \b{\\b} (word boundary), to split the string into an
6370     alternating sequence of non-word and word tokens:
6371
6372     \snippet qstring/main.cpp 61
6373
6374     \sa QStringList::join(), section()
6375 */
6376 QStringList QString::split(const QRegExp &rx, SplitBehavior behavior) const
6377 {
6378     QRegExp rx2(rx);
6379     QStringList list;
6380     int start = 0;
6381     int extra = 0;
6382     int end;
6383     while ((end = rx2.indexIn(*this, start + extra)) != -1) {
6384         int matchedLen = rx2.matchedLength();
6385         if (start != end || behavior == KeepEmptyParts)
6386             list.append(mid(start, end - start));
6387         start = end + matchedLen;
6388         extra = (matchedLen == 0) ? 1 : 0;
6389     }
6390     if (start != size() || behavior == KeepEmptyParts)
6391         list.append(mid(start));
6392     return list;
6393 }
6394 #endif
6395
6396 #ifndef QT_NO_REGEXP
6397 #ifndef QT_BOOTSTRAPPED
6398 /*!
6399     \overload
6400     \since 5.0
6401
6402     Splits the string into substrings wherever the regular expression
6403     \a re matches, and returns the list of those strings. If \a re
6404     does not match anywhere in the string, split() returns a
6405     single-element list containing this string.
6406
6407     Here's an example where we extract the words in a sentence
6408     using one or more whitespace characters as the separator:
6409
6410     \snippet qstring/main.cpp 90
6411
6412     Here's a similar example, but this time we use any sequence of
6413     non-word characters as the separator:
6414
6415     \snippet qstring/main.cpp 91
6416
6417     Here's a third example where we use a zero-length assertion,
6418     \b{\\b} (word boundary), to split the string into an
6419     alternating sequence of non-word and word tokens:
6420
6421     \snippet qstring/main.cpp 92
6422
6423     \sa QStringList::join(), section()
6424 */
6425 QStringList QString::split(const QRegularExpression &re, SplitBehavior behavior) const
6426 {
6427     QStringList list;
6428     if (!re.isValid()) {
6429         qWarning("QString::split: invalid QRegularExpression object");
6430         return list;
6431     }
6432
6433     int start = 0;
6434     int end = 0;
6435     QRegularExpressionMatchIterator iterator = re.globalMatch(*this);
6436     while (iterator.hasNext()) {
6437         QRegularExpressionMatch match = iterator.next();
6438         end = match.capturedStart();
6439         if (start != end || behavior == KeepEmptyParts)
6440             list.append(mid(start, end - start));
6441         start = match.capturedEnd();
6442     }
6443
6444     if (start != size() || behavior == KeepEmptyParts)
6445         list.append(mid(start));
6446
6447     return list;
6448 }
6449 #endif // QT_BOOTSTRAPPED
6450 #endif // QT_NO_REGEXP
6451
6452 /*!
6453     \enum QString::NormalizationForm
6454
6455     This enum describes the various normalized forms of Unicode text.
6456
6457     \value NormalizationForm_D  Canonical Decomposition
6458     \value NormalizationForm_C  Canonical Decomposition followed by Canonical Composition
6459     \value NormalizationForm_KD  Compatibility Decomposition
6460     \value NormalizationForm_KC  Compatibility Decomposition followed by Canonical Composition
6461
6462     \sa normalized(),
6463         {http://www.unicode.org/reports/tr15/}{Unicode Standard Annex #15}
6464 */
6465
6466 /*!
6467     \since 4.5
6468
6469     Returns a copy of this string repeated the specified number of \a times.
6470
6471     If \a times is less than 1, an empty string is returned.
6472
6473     Example:
6474
6475     \code
6476         QString str("ab");
6477         str.repeated(4);            // returns "abababab"
6478     \endcode
6479 */
6480 QString QString::repeated(int times) const
6481 {
6482     if (d->size == 0)
6483         return *this;
6484
6485     if (times <= 1) {
6486         if (times == 1)
6487             return *this;
6488         return QString();
6489     }
6490
6491     const int resultSize = times * d->size;
6492
6493     QString result;
6494     result.reserve(resultSize);
6495     if (result.d->alloc != uint(resultSize) + 1u)
6496         return QString(); // not enough memory
6497
6498     memcpy(result.d->data(), d->data(), d->size * sizeof(ushort));
6499
6500     int sizeSoFar = d->size;
6501     ushort *end = result.d->data() + sizeSoFar;
6502
6503     const int halfResultSize = resultSize >> 1;
6504     while (sizeSoFar <= halfResultSize) {
6505         memcpy(end, result.d->data(), sizeSoFar * sizeof(ushort));
6506         end += sizeSoFar;
6507         sizeSoFar <<= 1;
6508     }
6509     memcpy(end, result.d->data(), (resultSize - sizeSoFar) * sizeof(ushort));
6510     result.d->data()[resultSize] = '\0';
6511     result.d->size = resultSize;
6512     return result;
6513 }
6514
6515 void qt_string_normalize(QString *data, QString::NormalizationForm mode, QChar::UnicodeVersion version, int from)
6516 {
6517     bool simple = true;
6518     const QChar *p = data->constData();
6519     int len = data->length();
6520     for (int i = from; i < len; ++i) {
6521         if (p[i].unicode() >= 0x80) {
6522             simple = false;
6523             break;
6524         }
6525     }
6526     if (simple)
6527         return;
6528
6529     if (version == QChar::Unicode_Unassigned) {
6530         version = QChar::currentUnicodeVersion();
6531     } else if (int(version) <= NormalizationCorrectionsVersionMax) {
6532         const QString &s = *data;
6533         QChar *d = 0;
6534         for (int i = 0; i < NumNormalizationCorrections; ++i) {
6535             const NormalizationCorrection &n = uc_normalization_corrections[i];
6536             if (n.version > version) {
6537                 int pos = from;
6538                 if (QChar::requiresSurrogates(n.ucs4)) {
6539                     ushort ucs4High = QChar::highSurrogate(n.ucs4);
6540                     ushort ucs4Low = QChar::lowSurrogate(n.ucs4);
6541                     ushort oldHigh = QChar::highSurrogate(n.old_mapping);
6542                     ushort oldLow = QChar::lowSurrogate(n.old_mapping);
6543                     while (pos < s.length() - 1) {
6544                         if (s.at(pos).unicode() == ucs4High && s.at(pos + 1).unicode() == ucs4Low) {
6545                             if (!d)
6546                                 d = data->data();
6547                             d[pos] = QChar(oldHigh);
6548                             d[++pos] = QChar(oldLow);
6549                         }
6550                         ++pos;
6551                     }
6552                 } else {
6553                     while (pos < s.length()) {
6554                         if (s.at(pos).unicode() == n.ucs4) {
6555                             if (!d)
6556                                 d = data->data();
6557                             d[pos] = QChar(n.old_mapping);
6558                         }
6559                         ++pos;
6560                     }
6561                 }
6562             }
6563         }
6564     }
6565     decomposeHelper(data, mode < QString::NormalizationForm_KD, version, from);
6566
6567     canonicalOrderHelper(data, version, from);
6568
6569     if (mode == QString::NormalizationForm_D || mode == QString::NormalizationForm_KD)
6570         return;
6571
6572     composeHelper(data, version, from);
6573 }
6574
6575 /*!
6576     Returns the string in the given Unicode normalization \a mode,
6577     according to the given \a version of the Unicode standard.
6578 */
6579 QString QString::normalized(QString::NormalizationForm mode, QChar::UnicodeVersion version) const
6580 {
6581     QString copy = *this;
6582     qt_string_normalize(&copy, mode, version, 0);
6583     return copy;
6584 }
6585
6586
6587 struct ArgEscapeData
6588 {
6589     int min_escape;            // lowest escape sequence number
6590     int occurrences;           // number of occurrences of the lowest escape sequence number
6591     int locale_occurrences;    // number of occurrences of the lowest escape sequence number that
6592                                // contain 'L'
6593     int escape_len;            // total length of escape sequences which will be replaced
6594 };
6595
6596 static ArgEscapeData findArgEscapes(const QString &s)
6597 {
6598     const QChar *uc_begin = s.unicode();
6599     const QChar *uc_end = uc_begin + s.length();
6600
6601     ArgEscapeData d;
6602
6603     d.min_escape = INT_MAX;
6604     d.occurrences = 0;
6605     d.escape_len = 0;
6606     d.locale_occurrences = 0;
6607
6608     const QChar *c = uc_begin;
6609     while (c != uc_end) {
6610         while (c != uc_end && c->unicode() != '%')
6611             ++c;
6612
6613         if (c == uc_end)
6614             break;
6615         const QChar *escape_start = c;
6616         if (++c == uc_end)
6617             break;
6618
6619         bool locale_arg = false;
6620         if (c->unicode() == 'L') {
6621             locale_arg = true;
6622             if (++c == uc_end)
6623                 break;
6624         }
6625
6626         if (c->digitValue() == -1)
6627             continue;
6628
6629         int escape = c->digitValue();
6630         ++c;
6631
6632         if (c != uc_end && c->digitValue() != -1) {
6633             escape = (10 * escape) + c->digitValue();
6634             ++c;
6635         }
6636
6637         if (escape > d.min_escape)
6638             continue;
6639
6640         if (escape < d.min_escape) {
6641             d.min_escape = escape;
6642             d.occurrences = 0;
6643             d.escape_len = 0;
6644             d.locale_occurrences = 0;
6645         }
6646
6647         ++d.occurrences;
6648         if (locale_arg)
6649             ++d.locale_occurrences;
6650         d.escape_len += c - escape_start;
6651     }
6652     return d;
6653 }
6654
6655 static QString replaceArgEscapes(const QString &s, const ArgEscapeData &d, int field_width,
6656                                  const QString &arg, const QString &larg, QChar fillChar = QLatin1Char(' '))
6657 {
6658     const QChar *uc_begin = s.unicode();
6659     const QChar *uc_end = uc_begin + s.length();
6660
6661     int abs_field_width = qAbs(field_width);
6662     int result_len = s.length()
6663                      - d.escape_len
6664                      + (d.occurrences - d.locale_occurrences)
6665                      *qMax(abs_field_width, arg.length())
6666                      + d.locale_occurrences
6667                      *qMax(abs_field_width, larg.length());
6668
6669     QString result(result_len, Qt::Uninitialized);
6670     QChar *result_buff = (QChar*) result.unicode();
6671
6672     QChar *rc = result_buff;
6673     const QChar *c = uc_begin;
6674     int repl_cnt = 0;
6675     while (c != uc_end) {
6676         /* We don't have to check if we run off the end of the string with c,
6677            because as long as d.occurrences > 0 we KNOW there are valid escape
6678            sequences. */
6679
6680         const QChar *text_start = c;
6681
6682         while (c->unicode() != '%')
6683             ++c;
6684
6685         const QChar *escape_start = c++;
6686
6687         bool locale_arg = false;
6688         if (c->unicode() == 'L') {
6689             locale_arg = true;
6690             ++c;
6691         }
6692
6693         int escape = c->digitValue();
6694         if (escape != -1) {
6695             if (c + 1 != uc_end && (c + 1)->digitValue() != -1) {
6696                 escape = (10 * escape) + (c + 1)->digitValue();
6697                 ++c;
6698             }
6699         }
6700
6701         if (escape != d.min_escape) {
6702             memcpy(rc, text_start, (c - text_start)*sizeof(QChar));
6703             rc += c - text_start;
6704         }
6705         else {
6706             ++c;
6707
6708             memcpy(rc, text_start, (escape_start - text_start)*sizeof(QChar));
6709             rc += escape_start - text_start;
6710
6711             uint pad_chars;
6712             if (locale_arg)
6713                 pad_chars = qMax(abs_field_width, larg.length()) - larg.length();
6714             else
6715                 pad_chars = qMax(abs_field_width, arg.length()) - arg.length();
6716
6717             if (field_width > 0) { // left padded
6718                 for (uint i = 0; i < pad_chars; ++i)
6719                     (rc++)->unicode() = fillChar.unicode();
6720             }
6721
6722             if (locale_arg) {
6723                 memcpy(rc, larg.unicode(), larg.length()*sizeof(QChar));
6724                 rc += larg.length();
6725             }
6726             else {
6727                 memcpy(rc, arg.unicode(), arg.length()*sizeof(QChar));
6728                 rc += arg.length();
6729             }
6730
6731             if (field_width < 0) { // right padded
6732                 for (uint i = 0; i < pad_chars; ++i)
6733                     (rc++)->unicode() = fillChar.unicode();
6734             }
6735
6736             if (++repl_cnt == d.occurrences) {
6737                 memcpy(rc, c, (uc_end - c)*sizeof(QChar));
6738                 rc += uc_end - c;
6739                 Q_ASSERT(rc - result_buff == result_len);
6740                 c = uc_end;
6741             }
6742         }
6743     }
6744     Q_ASSERT(rc == result_buff + result_len);
6745
6746     return result;
6747 }
6748
6749 /*!
6750   Returns a copy of this string with the lowest numbered place marker
6751   replaced by string \a a, i.e., \c %1, \c %2, ..., \c %99.
6752
6753   \a fieldWidth specifies the minimum amount of space that argument \a
6754   a shall occupy. If \a a requires less space than \a fieldWidth, it
6755   is padded to \a fieldWidth with character \a fillChar.  A positive
6756   \a fieldWidth produces right-aligned text. A negative \a fieldWidth
6757   produces left-aligned text.
6758
6759   This example shows how we might create a \c status string for
6760   reporting progress while processing a list of files:
6761
6762   \snippet qstring/main.cpp 11
6763
6764   First, \c arg(i) replaces \c %1. Then \c arg(total) replaces \c
6765   %2. Finally, \c arg(fileName) replaces \c %3.
6766
6767   One advantage of using arg() over sprintf() is that the order of the
6768   numbered place markers can change, if the application's strings are
6769   translated into other languages, but each arg() will still replace
6770   the lowest numbered unreplaced place marker, no matter where it
6771   appears. Also, if place marker \c %i appears more than once in the
6772   string, the arg() replaces all of them.
6773
6774   If there is no unreplaced place marker remaining, a warning message
6775   is output and the result is undefined. Place marker numbers must be
6776   in the range 1 to 99.
6777 */
6778 QString QString::arg(const QString &a, int fieldWidth, QChar fillChar) const
6779 {
6780     ArgEscapeData d = findArgEscapes(*this);
6781
6782     if (d.occurrences == 0) {
6783         qWarning("QString::arg: Argument missing: %s, %s", toLocal8Bit().data(),
6784                   a.toLocal8Bit().data());
6785         return *this;
6786     }
6787     return replaceArgEscapes(*this, d, fieldWidth, a, a, fillChar);
6788 }
6789
6790 /*!
6791   \fn QString QString::arg(const QString& a1, const QString& a2) const
6792   \overload arg()
6793
6794   This is the same as \c {str.arg(a1).arg(a2)}, except that the
6795   strings \a a1 and \a a2 are replaced in one pass. This can make a
6796   difference if \a a1 contains e.g. \c{%1}:
6797
6798   \snippet qstring/main.cpp 13
6799 */
6800
6801 /*!
6802   \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3) const
6803   \overload arg()
6804
6805   This is the same as calling \c str.arg(a1).arg(a2).arg(a3), except
6806   that the strings \a a1, \a a2 and \a a3 are replaced in one pass.
6807 */
6808
6809 /*!
6810   \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4) const
6811   \overload arg()
6812
6813   This is the same as calling \c
6814   {str.arg(a1).arg(a2).arg(a3).arg(a4)}, except that the strings \a
6815   a1, \a a2, \a a3 and \a a4 are replaced in one pass.
6816 */
6817
6818 /*!
6819   \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4, const QString& a5) const
6820   \overload arg()
6821
6822   This is the same as calling \c
6823   {str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5)}, except that the strings
6824   \a a1, \a a2, \a a3, \a a4, and \a a5 are replaced in one pass.
6825 */
6826
6827 /*!
6828   \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4, const QString& a5, const QString& a6) const
6829   \overload arg()
6830
6831   This is the same as calling \c
6832   {str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5).arg(a6))}, except that
6833   the strings \a a1, \a a2, \a a3, \a a4, \a a5, and \a a6 are
6834   replaced in one pass.
6835 */
6836
6837 /*!
6838   \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4, const QString& a5, const QString& a6, const QString& a7) const
6839   \overload arg()
6840
6841   This is the same as calling \c
6842   {str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5).arg(a6).arg(a7)},
6843   except that the strings \a a1, \a a2, \a a3, \a a4, \a a5, \a a6,
6844   and \a a7 are replaced in one pass.
6845 */
6846
6847 /*!
6848   \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4, const QString& a5, const QString& a6, const QString& a7, const QString& a8) const
6849   \overload arg()
6850
6851   This is the same as calling \c
6852   {str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5).arg(a6).arg(a7).arg(a8)},
6853   except that the strings \a a1, \a a2, \a a3, \a a4, \a a5, \a a6, \a
6854   a7, and \a a8 are replaced in one pass.
6855 */
6856
6857 /*!
6858   \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4, const QString& a5, const QString& a6, const QString& a7, const QString& a8, const QString& a9) const
6859   \overload arg()
6860
6861   This is the same as calling \c
6862   {str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5).arg(a6).arg(a7).arg(a8).arg(a9)},
6863   except that the strings \a a1, \a a2, \a a3, \a a4, \a a5, \a a6, \a
6864   a7, \a a8, and \a a9 are replaced in one pass.
6865 */
6866
6867 /*! \fn QString QString::arg(int a, int fieldWidth, int base, QChar fillChar) const
6868   \overload arg()
6869
6870   The \a a argument is expressed in base \a base, which is 10 by
6871   default and must be between 2 and 36. For bases other than 10, \a a
6872   is treated as an unsigned integer.
6873
6874   \a fieldWidth specifies the minimum amount of space that \a a is
6875   padded to and filled with the character \a fillChar. A positive
6876   value produces right-aligned text; a negative value produces
6877   left-aligned text.
6878
6879   The '%' can be followed by an 'L', in which case the sequence is
6880   replaced with a localized representation of \a a. The conversion
6881   uses the default locale, set by QLocale::setDefault(). If no default
6882   locale was specified, the "C" locale is used. The 'L' flag is
6883   ignored if \a base is not 10.
6884
6885   \snippet qstring/main.cpp 12
6886   \snippet qstring/main.cpp 14
6887
6888   If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
6889   used. For negative numbers, zero padding might appear before the
6890   minus sign.
6891 */
6892
6893 /*! \fn QString QString::arg(uint a, int fieldWidth, int base, QChar fillChar) const
6894   \overload arg()
6895
6896   The \a base argument specifies the base to use when converting the
6897   integer \a a into a string. The base must be between 2 and 36.
6898
6899   If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
6900   used. For negative numbers, zero padding might appear before the
6901   minus sign.
6902 */
6903
6904 /*! \fn QString QString::arg(long a, int fieldWidth, int base, QChar fillChar) const
6905   \overload arg()
6906
6907   \a fieldWidth specifies the minimum amount of space that \a a is
6908   padded to and filled with the character \a fillChar. A positive
6909   value produces right-aligned text; a negative value produces
6910   left-aligned text.
6911
6912   The \a a argument is expressed in the given \a base, which is 10 by
6913   default and must be between 2 and 36.
6914
6915   The '%' can be followed by an 'L', in which case the sequence is
6916   replaced with a localized representation of \a a. The conversion
6917   uses the default locale. The default locale is determined from the
6918   system's locale settings at application startup. It can be changed
6919   using QLocale::setDefault(). The 'L' flag is ignored if \a base is
6920   not 10.
6921
6922   \snippet qstring/main.cpp 12
6923   \snippet qstring/main.cpp 14
6924
6925   If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
6926   used. For negative numbers, zero padding might appear before the
6927   minus sign.
6928 */
6929
6930 /*! \fn QString QString::arg(ulong a, int fieldWidth, int base, QChar fillChar) const
6931   \overload arg()
6932
6933   \a fieldWidth specifies the minimum amount of space that \a a is
6934   padded to and filled with the character \a fillChar. A positive
6935   value produces right-aligned text; a negative value produces
6936   left-aligned text.
6937
6938   The \a base argument specifies the base to use when converting the
6939   integer \a a to a string. The base must be between 2 and 36, with 8
6940   giving octal, 10 decimal, and 16 hexadecimal numbers.
6941
6942   If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
6943   used. For negative numbers, zero padding might appear before the
6944   minus sign.
6945 */
6946
6947 /*!
6948   \overload arg()
6949
6950   \a fieldWidth specifies the minimum amount of space that \a a is
6951   padded to and filled with the character \a fillChar. A positive
6952   value produces right-aligned text; a negative value produces
6953   left-aligned text.
6954
6955   The \a base argument specifies the base to use when converting the
6956   integer \a a into a string. The base must be between 2 and 36, with
6957   8 giving octal, 10 decimal, and 16 hexadecimal numbers.
6958
6959   If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
6960   used. For negative numbers, zero padding might appear before the
6961   minus sign.
6962 */
6963 QString QString::arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const
6964 {
6965     ArgEscapeData d = findArgEscapes(*this);
6966
6967     if (d.occurrences == 0) {
6968         qWarning() << "QString::arg: Argument missing:" << *this << ',' << a;
6969         return *this;
6970     }
6971
6972     unsigned flags = QLocalePrivate::NoFlags;
6973     if (fillChar == QLatin1Char('0'))
6974         flags = QLocalePrivate::ZeroPadded;
6975
6976     QString arg;
6977     if (d.occurrences > d.locale_occurrences)
6978         arg = QLocale::c().d->longLongToString(a, -1, base, fieldWidth, flags);
6979
6980     QString locale_arg;
6981     if (d.locale_occurrences > 0) {
6982         QLocale locale;
6983         if (!locale.numberOptions() & QLocale::OmitGroupSeparator)
6984             flags |= QLocalePrivate::ThousandsGroup;
6985         locale_arg = locale.d->longLongToString(a, -1, base, fieldWidth, flags);
6986     }
6987
6988     return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg, fillChar);
6989 }
6990
6991 /*!
6992   \overload arg()
6993
6994   \a fieldWidth specifies the minimum amount of space that \a a is
6995   padded to and filled with the character \a fillChar. A positive
6996   value produces right-aligned text; a negative value produces
6997   left-aligned text.
6998
6999   The \a base argument specifies the base to use when converting the
7000   integer \a a into a string. \a base must be between 2 and 36, with 8
7001   giving octal, 10 decimal, and 16 hexadecimal numbers.
7002
7003   If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
7004   used. For negative numbers, zero padding might appear before the
7005   minus sign.
7006 */
7007 QString QString::arg(qulonglong a, int fieldWidth, int base, QChar fillChar) const
7008 {
7009     ArgEscapeData d = findArgEscapes(*this);
7010
7011     if (d.occurrences == 0) {
7012         qWarning() << "QString::arg: Argument missing:" << *this << ',' << a;
7013         return *this;
7014     }
7015
7016     unsigned flags = QLocalePrivate::NoFlags;
7017     if (fillChar == QLatin1Char('0'))
7018         flags = QLocalePrivate::ZeroPadded;
7019
7020     QString arg;
7021     if (d.occurrences > d.locale_occurrences)
7022         arg = QLocale::c().d->unsLongLongToString(a, -1, base, fieldWidth, flags);
7023
7024     QString locale_arg;
7025     if (d.locale_occurrences > 0) {
7026         QLocale locale;
7027         if (!locale.numberOptions() & QLocale::OmitGroupSeparator)
7028             flags |= QLocalePrivate::ThousandsGroup;
7029         locale_arg = locale.d->unsLongLongToString(a, -1, base, fieldWidth, flags);
7030     }
7031
7032     return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg, fillChar);
7033 }
7034
7035 /*!
7036   \overload arg()
7037
7038   \fn QString QString::arg(short a, int fieldWidth, int base, QChar fillChar) const
7039
7040   \a fieldWidth specifies the minimum amount of space that \a a is
7041   padded to and filled with the character \a fillChar. A positive
7042   value produces right-aligned text; a negative value produces
7043   left-aligned text.
7044
7045   The \a base argument specifies the base to use when converting the
7046   integer \a a into a string. The base must be between 2 and 36, with
7047   8 giving octal, 10 decimal, and 16 hexadecimal numbers.
7048
7049   If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
7050   used. For negative numbers, zero padding might appear before the
7051   minus sign.
7052 */
7053
7054 /*!
7055   \fn QString QString::arg(ushort a, int fieldWidth, int base, QChar fillChar) const
7056   \overload arg()
7057
7058   \a fieldWidth specifies the minimum amount of space that \a a is
7059   padded to and filled with the character \a fillChar. A positive
7060   value produces right-aligned text; a negative value produces
7061   left-aligned text.
7062
7063   The \a base argument specifies the base to use when converting the
7064   integer \a a into a string. The base must be between 2 and 36, with
7065   8 giving octal, 10 decimal, and 16 hexadecimal numbers.
7066
7067   If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
7068   used. For negative numbers, zero padding might appear before the
7069   minus sign.
7070 */
7071
7072 /*!
7073     \overload arg()
7074 */
7075 QString QString::arg(QChar a, int fieldWidth, QChar fillChar) const
7076 {
7077     QString c;
7078     c += a;
7079     return arg(c, fieldWidth, fillChar);
7080 }
7081
7082 /*!
7083   \overload arg()
7084
7085   The \a a argument is interpreted as a Latin-1 character.
7086 */
7087 QString QString::arg(char a, int fieldWidth, QChar fillChar) const
7088 {
7089     QString c;
7090     c += QLatin1Char(a);
7091     return arg(c, fieldWidth, fillChar);
7092 }
7093
7094 /*!
7095   \fn QString QString::arg(double a, int fieldWidth, char format, int precision, QChar fillChar) const
7096   \overload arg()
7097
7098   Argument \a a is formatted according to the specified \a format and
7099   \a precision. See \l{Argument Formats} for details.
7100
7101   \a fieldWidth specifies the minimum amount of space that \a a is
7102   padded to and filled with the character \a fillChar.  A positive
7103   value produces right-aligned text; a negative value produces
7104   left-aligned text.
7105
7106   \snippet code/src_corelib_tools_qstring.cpp 2
7107
7108   The '%' can be followed by an 'L', in which case the sequence is
7109   replaced with a localized representation of \a a. The conversion
7110   uses the default locale, set by QLocale::setDefaultLocale(). If no
7111   default locale was specified, the "C" locale is used.
7112
7113   If \a fillChar is '0' (the number 0, ASCII 48), this function will
7114   use the locale's zero to pad. For negative numbers, the zero padding
7115   will probably appear before the minus sign.
7116
7117   \sa QLocale::toString()
7118 */
7119 QString QString::arg(double a, int fieldWidth, char fmt, int prec, QChar fillChar) const
7120 {
7121     ArgEscapeData d = findArgEscapes(*this);
7122
7123     if (d.occurrences == 0) {
7124         qWarning("QString::arg: Argument missing: %s, %g", toLocal8Bit().data(), a);
7125         return *this;
7126     }
7127
7128     unsigned flags = QLocalePrivate::NoFlags;
7129     if (fillChar == QLatin1Char('0'))
7130         flags = QLocalePrivate::ZeroPadded;
7131
7132     if (qIsUpper(fmt))
7133         flags |= QLocalePrivate::CapitalEorX;
7134     fmt = qToLower(fmt);
7135
7136     QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
7137     switch (fmt) {
7138     case 'f':
7139         form = QLocalePrivate::DFDecimal;
7140         break;
7141     case 'e':
7142         form = QLocalePrivate::DFExponent;
7143         break;
7144     case 'g':
7145         form = QLocalePrivate::DFSignificantDigits;
7146         break;
7147     default:
7148 #if defined(QT_CHECK_RANGE)
7149         qWarning("QString::arg: Invalid format char '%c'", fmt);
7150 #endif
7151         break;
7152     }
7153
7154     QString arg;
7155     if (d.occurrences > d.locale_occurrences)
7156         arg = QLocale::c().d->doubleToString(a, prec, form, fieldWidth, flags);
7157
7158     QString locale_arg;
7159     if (d.locale_occurrences > 0) {
7160         QLocale locale;
7161
7162         if (!locale.numberOptions() & QLocale::OmitGroupSeparator)
7163             flags |= QLocalePrivate::ThousandsGroup;
7164         locale_arg = locale.d->doubleToString(a, prec, form, fieldWidth, flags);
7165     }
7166
7167     return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg, fillChar);
7168 }
7169
7170 static int getEscape(const QChar *uc, int *pos, int len, int maxNumber = 999)
7171 {
7172     int i = *pos;
7173     ++i;
7174     if (i < len && uc[i] == QLatin1Char('L'))
7175         ++i;
7176     if (i < len) {
7177         int escape = uc[i].unicode() - '0';
7178         if (uint(escape) >= 10U)
7179             return -1;
7180         ++i;
7181         while (i < len) {
7182             int digit = uc[i].unicode() - '0';
7183             if (uint(digit) >= 10U)
7184                 break;
7185             escape = (escape * 10) + digit;
7186             ++i;
7187         }
7188         if (escape <= maxNumber) {
7189             *pos = i;
7190             return escape;
7191         }
7192     }
7193     return -1;
7194 }
7195
7196 QString QString::multiArg(int numArgs, const QString **args) const
7197 {
7198     QString result;
7199     QMap<int, int> numbersUsed;
7200     const QChar *uc = (const QChar *) d->data();
7201     const int len = d->size;
7202     const int end = len - 1;
7203     int lastNumber = -1;
7204     int i = 0;
7205
7206     // populate the numbersUsed map with the %n's that actually occur in the string
7207     while (i < end) {
7208         if (uc[i] == QLatin1Char('%')) {
7209             int number = getEscape(uc, &i, len);
7210             if (number != -1) {
7211                 numbersUsed.insert(number, -1);
7212                 continue;
7213             }
7214         }
7215         ++i;
7216     }
7217
7218     // assign an argument number to each of the %n's
7219     QMap<int, int>::iterator j = numbersUsed.begin();
7220     QMap<int, int>::iterator jend = numbersUsed.end();
7221     int arg = 0;
7222     while (j != jend && arg < numArgs) {
7223         *j = arg++;
7224         lastNumber = j.key();
7225         ++j;
7226     }
7227
7228     // sanity
7229     if (numArgs > arg) {
7230         qWarning("QString::arg: %d argument(s) missing in %s", numArgs - arg, toLocal8Bit().data());
7231         numArgs = arg;
7232     }
7233
7234     i = 0;
7235     while (i < len) {
7236         if (uc[i] == QLatin1Char('%') && i != end) {
7237             int number = getEscape(uc, &i, len, lastNumber);
7238             int arg = numbersUsed[number];
7239             if (number != -1 && arg != -1) {
7240                 result += *args[arg];
7241                 continue;
7242             }
7243         }
7244         result += uc[i++];
7245     }
7246     return result;
7247 }
7248
7249
7250 /*! \fn bool QString::isSimpleText() const
7251
7252     \internal
7253 */
7254 bool QString::isSimpleText() const
7255 {
7256     const ushort *p = d->data();
7257     const ushort * const end = p + d->size;
7258     while (p < end) {
7259         ushort uc = *p;
7260         // sort out regions of complex text formatting
7261         if (uc > 0x058f && (uc < 0x1100 || uc > 0xfb0f)) {
7262             return false;
7263         }
7264         p++;
7265     }
7266
7267     return true;
7268 }
7269
7270 /*! \fn bool QString::isRightToLeft() const
7271
7272     Returns true if the string is read right to left.
7273 */
7274 bool QString::isRightToLeft() const
7275 {
7276     const ushort *p = d->data();
7277     const ushort * const end = p + d->size;
7278     while (p < end) {
7279         uint ucs4 = *p;
7280         if (QChar::isHighSurrogate(ucs4) && p < end - 1) {
7281             ushort low = p[1];
7282             if (QChar::isLowSurrogate(low)) {
7283                 ucs4 = QChar::surrogateToUcs4(ucs4, low);
7284                 ++p;
7285             }
7286         }
7287         switch (QChar::direction(ucs4))
7288         {
7289         case QChar::DirL:
7290             return false;
7291         case QChar::DirR:
7292         case QChar::DirAL:
7293             return true;
7294         default:
7295             break;
7296         }
7297         ++p;
7298     }
7299     return false;
7300 }
7301
7302 /*! \fn QChar *QString::data()
7303
7304     Returns a pointer to the data stored in the QString. The pointer
7305     can be used to access and modify the characters that compose the
7306     string. For convenience, the data is '\\0'-terminated.
7307
7308     Example:
7309
7310     \snippet qstring/main.cpp 19
7311
7312     Note that the pointer remains valid only as long as the string is
7313     not modified by other means. For read-only access, constData() is
7314     faster because it never causes a \l{deep copy} to occur.
7315
7316     \sa constData(), operator[]()
7317 */
7318
7319 /*! \fn const QChar *QString::data() const
7320
7321     \overload
7322 */
7323
7324 /*! \fn const QChar *QString::constData() const
7325
7326     Returns a pointer to the data stored in the QString. The pointer
7327     can be used to access the characters that compose the string. For
7328     convenience, the data is '\\0'-terminated.
7329
7330     Note that the pointer remains valid only as long as the string is
7331     not modified.
7332
7333     \sa data(), operator[]()
7334 */
7335
7336 /*! \fn void QString::push_front(const QString &other)
7337
7338     This function is provided for STL compatibility, prepending the
7339     given \a other string to the beginning of this string. It is
7340     equivalent to \c prepend(other).
7341
7342     \sa prepend()
7343 */
7344
7345 /*! \fn void QString::push_front(QChar ch)
7346
7347     \overload
7348
7349     Prepends the given \a ch character to the beginning of this string.
7350 */
7351
7352 /*! \fn void QString::push_back(const QString &other)
7353
7354     This function is provided for STL compatibility, appending the
7355     given \a other string onto the end of this string. It is
7356     equivalent to \c append(other).
7357
7358     \sa append()
7359 */
7360
7361 /*! \fn void QString::push_back(QChar ch)
7362
7363     \overload
7364
7365     Appends the given \a ch character onto the end of this string.
7366 */
7367
7368 /*!
7369     \fn std::string QString::toStdString() const
7370
7371     Returns a std::string object with the data contained in this
7372     QString. The Unicode data is converted into 8-bit characters using
7373     the toUtf8() function.
7374
7375     This operator is mostly useful to pass a QString to a function
7376     that accepts a std::string object.
7377
7378     If the QString contains non-Latin1 Unicode characters, using this
7379     can lead to loss of information.
7380
7381     \sa toLatin1(), toUtf8(), toLocal8Bit()
7382 */
7383
7384 /*!
7385     Constructs a QString that uses the first \a size Unicode characters
7386     in the array \a unicode. The data in \a unicode is \e not
7387     copied. The caller must be able to guarantee that \a unicode will
7388     not be deleted or modified as long as the QString (or an
7389     unmodified copy of it) exists.
7390
7391     Any attempts to modify the QString or copies of it will cause it
7392     to create a deep copy of the data, ensuring that the raw data
7393     isn't modified.
7394
7395     Here's an example of how we can use a QRegExp on raw data in
7396     memory without requiring to copy the data into a QString:
7397
7398     \snippet qstring/main.cpp 22
7399     \snippet qstring/main.cpp 23
7400
7401     \warning A string created with fromRawData() is \e not
7402     '\\0'-terminated, unless the raw data contains a '\\0' character
7403     at position \a size. This means unicode() will \e not return a
7404     '\\0'-terminated string (although utf16() does, at the cost of
7405     copying the raw data).
7406
7407     \sa fromUtf16(), setRawData()
7408 */
7409 QString QString::fromRawData(const QChar *unicode, int size)
7410 {
7411     Data *x;
7412     if (!unicode) {
7413         x = Data::sharedNull();
7414     } else if (!size) {
7415         x = Data::allocate(0);
7416     } else {
7417         x = Data::fromRawData(reinterpret_cast<const ushort *>(unicode), size);
7418         Q_CHECK_PTR(x);
7419     }
7420     QStringDataPtr dataPtr = { x };
7421     return QString(dataPtr);
7422 }
7423
7424 /*!
7425     \since 4.7
7426
7427     Resets the QString to use the first \a size Unicode characters
7428     in the array \a unicode. The data in \a unicode is \e not
7429     copied. The caller must be able to guarantee that \a unicode will
7430     not be deleted or modified as long as the QString (or an
7431     unmodified copy of it) exists.
7432
7433     This function can be used instead of fromRawData() to re-use
7434     existings QString objects to save memory re-allocations.
7435
7436     \sa fromRawData()
7437 */
7438 QString &QString::setRawData(const QChar *unicode, int size)
7439 {
7440     if (d->ref.isShared() || d->alloc) {
7441         *this = fromRawData(unicode, size);
7442     } else {
7443         if (unicode) {
7444             d->size = size;
7445             d->offset = reinterpret_cast<const char *>(unicode) - reinterpret_cast<char *>(d);
7446         } else {
7447             d->offset = sizeof(QStringData);
7448             d->size = 0;
7449         }
7450     }
7451     return *this;
7452 }
7453
7454 /*! \class QLatin1String
7455     \brief The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal.
7456
7457     \ingroup string-processing
7458     \reentrant
7459
7460     Many of QString's member functions are overloaded to accept
7461     \c{const char *} instead of QString. This includes the copy
7462     constructor, the assignment operator, the comparison operators,
7463     and various other functions such as \l{QString::insert()}{insert()}, \l{QString::replace()}{replace()},
7464     and \l{QString::indexOf()}{indexOf()}. These functions
7465     are usually optimized to avoid constructing a QString object for
7466     the \c{const char *} data. For example, assuming \c str is a
7467     QString,
7468
7469     \snippet code/src_corelib_tools_qstring.cpp 3
7470
7471     is much faster than
7472
7473     \snippet code/src_corelib_tools_qstring.cpp 4
7474
7475     because it doesn't construct four temporary QString objects and
7476     make a deep copy of the character data.
7477
7478     Applications that define \c QT_NO_CAST_FROM_ASCII (as explained
7479     in the QString documentation) don't have access to QString's
7480     \c{const char *} API. To provide an efficient way of specifying
7481     constant Latin-1 strings, Qt provides the QLatin1String, which is
7482     just a very thin wrapper around a \c{const char *}. Using
7483     QLatin1String, the example code above becomes
7484
7485     \snippet code/src_corelib_tools_qstring.cpp 5
7486
7487     This is a bit longer to type, but it provides exactly the same
7488     benefits as the first version of the code, and is faster than
7489     converting the Latin-1 strings using QString::fromLatin1().
7490
7491     Thanks to the QString(QLatin1String) constructor,
7492     QLatin1String can be used everywhere a QString is expected. For
7493     example:
7494
7495     \snippet code/src_corelib_tools_qstring.cpp 6
7496
7497     \sa QString, QLatin1Char, QStringLiteral
7498 */
7499
7500 /*! \fn QLatin1String::QLatin1String(const char *str)
7501
7502     Constructs a QLatin1String object that stores \a str. Note that if
7503     \a str is 0, an empty string is created; this case is handled by
7504     QString.
7505
7506     The string data is \e not copied. The caller must be able to
7507     guarantee that \a str will not be deleted or modified as long as
7508     the QLatin1String object exists.
7509
7510     \sa latin1()
7511 */
7512
7513 /*! \fn QLatin1String::QLatin1String(const char *str, int size)
7514
7515     Constructs a QLatin1String object that stores \a str with \a size.
7516     Note that if \a str is 0, an empty string is created; this case
7517     is handled by QString.
7518
7519     The string data is \e not copied. The caller must be able to
7520     guarantee that \a str will not be deleted or modified as long as
7521     the QLatin1String object exists.
7522
7523     \sa latin1()
7524 */
7525
7526 /*! \fn QLatin1String::QLatin1String(const QByteArray &str)
7527
7528     Constructs a QLatin1String object that stores \a str.
7529
7530     The string data is \e not copied. The caller must be able to
7531     guarantee that \a str will not be deleted or modified as long as
7532     the QLatin1String object exists.
7533
7534     \sa latin1()
7535 */
7536
7537 /*! \fn const char *QLatin1String::latin1() const
7538
7539     Returns the Latin-1 string stored in this object.
7540 */
7541
7542 /*! \fn int QLatin1String::size() const
7543
7544     Returns the size of the Latin-1 string stored in this object.
7545 */
7546
7547 /*! \fn bool QLatin1String::operator==(const QString &other) const
7548
7549     Returns true if this string is equal to string \a other;
7550     otherwise returns false.
7551
7552     The comparison is based exclusively on the numeric Unicode values
7553     of the characters and is very fast, but is not what a human would
7554     expect. Consider sorting user-interface strings with
7555     QString::localeAwareCompare().
7556 */
7557
7558 /*!
7559     \fn bool QLatin1String::operator==(const char *other) const
7560     \since 4.3
7561     \overload
7562
7563     The \a other const char pointer is converted to a QString using
7564     the QString::fromUtf8() function.
7565
7566     You can disable this operator by defining \c
7567     QT_NO_CAST_FROM_ASCII when you compile your applications. This
7568     can be useful if you want to ensure that all user-visible strings
7569     go through QObject::tr(), for example.
7570 */
7571
7572 /*! \fn bool QLatin1String::operator!=(const QString &other) const
7573
7574     Returns true if this string is not equal to string \a other;
7575     otherwise returns false.
7576
7577     The comparison is based exclusively on the numeric Unicode values
7578     of the characters and is very fast, but is not what a human would
7579     expect. Consider sorting user-interface strings with
7580     QString::localeAwareCompare().
7581 */
7582
7583 /*!
7584     \fn bool QLatin1String::operator!=(const char *other) const
7585     \since 4.3
7586     \overload operator!=()
7587
7588     The \a other const char pointer is converted to a QString using
7589     the QString::fromUtf8() function.
7590
7591     You can disable this operator by defining \c
7592     QT_NO_CAST_FROM_ASCII when you compile your applications. This
7593     can be useful if you want to ensure that all user-visible strings
7594     go through QObject::tr(), for example.
7595 */
7596
7597 /*!
7598     \fn bool QLatin1String::operator>(const QString &other) const
7599
7600     Returns true if this string is lexically greater than string \a
7601     other; otherwise returns false.
7602
7603     The comparison is based exclusively on the numeric Unicode values
7604     of the characters and is very fast, but is not what a human would
7605     expect. Consider sorting user-interface strings with
7606     QString::localeAwareCompare().
7607 */
7608
7609 /*!
7610     \fn bool QLatin1String::operator>(const char *other) const
7611     \since 4.3
7612     \overload
7613
7614     The \a other const char pointer is converted to a QString using
7615     the QString::fromUtf8() function.
7616
7617     You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII
7618     when you compile your applications. This can be useful if you want
7619     to ensure that all user-visible strings go through QObject::tr(),
7620     for example.
7621 */
7622
7623 /*!
7624     \fn bool QLatin1String::operator<(const QString &other) const
7625
7626     Returns true if this string is lexically less than the \a other
7627     string; otherwise returns false.
7628
7629     The comparison is based exclusively on the numeric Unicode values
7630     of the characters and is very fast, but is not what a human would
7631     expect. Consider sorting user-interface strings using the
7632     QString::localeAwareCompare() function.
7633 */
7634
7635 /*!
7636     \fn bool QLatin1String::operator<(const char *other) const
7637     \since 4.3
7638     \overload
7639
7640     The \a other const char pointer is converted to a QString using
7641     the QString::fromUtf8() function.
7642
7643     You can disable this operator by defining \c
7644     QT_NO_CAST_FROM_ASCII when you compile your applications. This
7645     can be useful if you want to ensure that all user-visible strings
7646     go through QObject::tr(), for example.
7647 */
7648
7649 /*!
7650     \fn bool QLatin1String::operator>=(const QString &other) const
7651
7652     Returns true if this string is lexically greater than or equal
7653     to string \a other; otherwise returns false.
7654
7655     The comparison is based exclusively on the numeric Unicode values
7656     of the characters and is very fast, but is not what a human would
7657     expect. Consider sorting user-interface strings with
7658     QString::localeAwareCompare().
7659 */
7660
7661 /*!
7662     \fn bool QLatin1String::operator>=(const char *other) const
7663     \since 4.3
7664     \overload
7665
7666     The \a other const char pointer is converted to a QString using
7667     the QString::fromUtf8() function.
7668
7669     You can disable this operator by defining \c
7670     QT_NO_CAST_FROM_ASCII when you compile your applications. This
7671     can be useful if you want to ensure that all user-visible strings
7672     go through QObject::tr(), for example.
7673 */
7674
7675 /*! \fn bool QLatin1String::operator<=(const QString &other) const
7676
7677     Returns true if this string is lexically less than or equal
7678     to string \a other; otherwise returns false.
7679
7680     The comparison is based exclusively on the numeric Unicode values
7681     of the characters and is very fast, but is not what a human would
7682     expect. Consider sorting user-interface strings with
7683     QString::localeAwareCompare().
7684 */
7685
7686 /*!
7687     \fn bool QLatin1String::operator<=(const char *other) const
7688     \since 4.3
7689     \overload
7690
7691     The \a other const char pointer is converted to a QString using
7692     the QString::fromUtf8() function.
7693
7694     You can disable this operator by defining \c
7695     QT_NO_CAST_FROM_ASCII when you compile your applications. This
7696     can be useful if you want to ensure that all user-visible strings
7697     go through QObject::tr(), for example.
7698 */
7699
7700
7701
7702 /*! \fn bool operator==(QLatin1String s1, QLatin1String s2)
7703    \relates QLatin1String
7704
7705    Returns true if string \a s1 is lexically equal to string \a s2; otherwise
7706    returns false.
7707 */
7708 /*! \fn bool operator!=(QLatin1String s1, QLatin1String s2)
7709    \relates QLatin1String
7710
7711    Returns true if string \a s1 is lexically unequal to string \a s2; otherwise
7712    returns false.
7713 */
7714 /*! \fn bool operator<(QLatin1String s1, QLatin1String s2)
7715    \relates QLatin1String
7716
7717    Returns true if string \a s1 is lexically smaller than string \a s2; otherwise
7718    returns false.
7719 */
7720 /*! \fn bool operator<=(QLatin1String s1, QLatin1String s2)
7721    \relates QLatin1String
7722
7723    Returns true if string \a s1 is lexically smaller than or equal to string \a s2; otherwise
7724    returns false.
7725 */
7726 /*! \fn bool operator>(QLatin1String s1, QLatin1String s2)
7727    \relates QLatin1String
7728
7729    Returns true if string \a s1 is lexically greater than string \a s2; otherwise
7730    returns false.
7731 */
7732 /*! \fn bool operator>=(QLatin1String s1, QLatin1String s2)
7733    \relates QLatin1String
7734
7735    Returns true if string \a s1 is lexically greater than or equal to
7736    string \a s2; otherwise returns false.
7737 */
7738
7739
7740 #if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE))
7741 /*!
7742     \fn QDataStream &operator<<(QDataStream &stream, const QString &string)
7743     \relates QString
7744
7745     Writes the given \a string to the specified \a stream.
7746
7747     \sa {Serializing Qt Data Types}
7748 */
7749
7750 QDataStream &operator<<(QDataStream &out, const QString &str)
7751 {
7752     if (out.version() == 1) {
7753         out << str.toLatin1();
7754     } else {
7755         if (!str.isNull() || out.version() < 3) {
7756             if ((out.byteOrder() == QDataStream::BigEndian) == (QSysInfo::ByteOrder == QSysInfo::BigEndian)) {
7757                 out.writeBytes(reinterpret_cast<const char *>(str.unicode()), sizeof(QChar) * str.length());
7758             } else {
7759                 QVarLengthArray<ushort> buffer(str.length());
7760                 const ushort *data = reinterpret_cast<const ushort *>(str.constData());
7761                 for (int i = 0; i < str.length(); i++) {
7762                     buffer[i] = qbswap(*data);
7763                     ++data;
7764                 }
7765                 out.writeBytes(reinterpret_cast<const char *>(buffer.data()), sizeof(ushort) * buffer.size());
7766             }
7767         } else {
7768             // write null marker
7769             out << (quint32)0xffffffff;
7770         }
7771     }
7772     return out;
7773 }
7774
7775 /*!
7776     \fn QDataStream &operator>>(QDataStream &stream, QString &string)
7777     \relates QString
7778
7779     Reads a string from the specified \a stream into the given \a string.
7780
7781     \sa {Serializing Qt Data Types}
7782 */
7783
7784 QDataStream &operator>>(QDataStream &in, QString &str)
7785 {
7786 #ifdef QT_QSTRING_UCS_4
7787 #if defined(Q_CC_GNU)
7788 #warning "operator>> not working properly"
7789 #endif
7790 #endif
7791
7792     if (in.version() == 1) {
7793         QByteArray l;
7794         in >> l;
7795         str = QString::fromLatin1(l);
7796     } else {
7797         quint32 bytes = 0;
7798         in >> bytes;                                  // read size of string
7799         if (bytes == 0xffffffff) {                    // null string
7800             str.clear();
7801         } else if (bytes > 0) {                       // not empty
7802             if (bytes & 0x1) {
7803                 str.clear();
7804                 in.setStatus(QDataStream::ReadCorruptData);
7805                 return in;
7806             }
7807
7808             const quint32 Step = 1024 * 1024;
7809             quint32 len = bytes / 2;
7810             quint32 allocated = 0;
7811
7812             while (allocated < len) {
7813                 int blockSize = qMin(Step, len - allocated);
7814                 str.resize(allocated + blockSize);
7815                 if (in.readRawData(reinterpret_cast<char *>(str.data()) + allocated * 2,
7816                                    blockSize * 2) != blockSize * 2) {
7817                     str.clear();
7818                     in.setStatus(QDataStream::ReadPastEnd);
7819                     return in;
7820                 }
7821                 allocated += blockSize;
7822             }
7823
7824             if ((in.byteOrder() == QDataStream::BigEndian)
7825                     != (QSysInfo::ByteOrder == QSysInfo::BigEndian)) {
7826                 ushort *data = reinterpret_cast<ushort *>(str.data());
7827                 while (len--) {
7828                     *data = qbswap(*data);
7829                     ++data;
7830                 }
7831             }
7832         } else {
7833             str = QString(QLatin1String(""));
7834         }
7835     }
7836     return in;
7837 }
7838 #endif // QT_NO_DATASTREAM
7839
7840
7841
7842
7843 /*!
7844     \class QStringRef
7845     \since 4.3
7846     \brief The QStringRef class provides a thin wrapper around QString substrings.
7847     \reentrant
7848     \ingroup tools
7849     \ingroup string-processing
7850
7851     QStringRef provides a read-only subset of the QString API.
7852
7853     A string reference explicitly references a portion of a string()
7854     with a given size(), starting at a specific position(). Calling
7855     toString() returns a copy of the data as a real QString instance.
7856
7857     This class is designed to improve the performance of substring
7858     handling when manipulating substrings obtained from existing QString
7859     instances. QStringRef avoids the memory allocation and reference
7860     counting overhead of a standard QString by simply referencing a
7861     part of the original string. This can prove to be advantageous in
7862     low level code, such as that used in a parser, at the expense of
7863     potentially more complex code.
7864
7865     For most users, there are no semantic benefits to using QStringRef
7866     instead of QString since QStringRef requires attention to be paid
7867     to memory management issues, potentially making code more complex
7868     to write and maintain.
7869
7870     \warning A QStringRef is only valid as long as the referenced
7871     string exists. If the original string is deleted, the string
7872     reference points to an invalid memory location.
7873
7874     We suggest that you only use this class in stable code where profiling
7875     has clearly identified that performance improvements can be made by
7876     replacing standard string operations with the optimized substring
7877     handling provided by this class.
7878
7879     \sa {Implicitly Shared Classes}
7880 */
7881
7882
7883 /*!
7884  \fn QStringRef::QStringRef()
7885
7886  Constructs an empty string reference.
7887 */
7888
7889 /*! \fn QStringRef::QStringRef(const QString *string, int position, int length)
7890
7891 Constructs a string reference to the range of characters in the given
7892 \a string specified by the starting \a position and \a length in characters.
7893
7894 \warning This function exists to improve performance as much as possible,
7895 and performs no bounds checking. For program correctness, \a position and
7896 \a length must describe a valid substring of \a string.
7897
7898 This means that the starting \a position must be positive or 0 and smaller
7899 than \a string's length, and \a length must be positive or 0 but smaller than
7900 the string's length minus the starting \a position;
7901 i.e, 0 <= position < string->length() and
7902 0 <= length <= string->length() - position must both be satisfied.
7903 */
7904
7905 /*! \fn QStringRef::QStringRef(const QString *string)
7906
7907 Constructs a string reference to the given \a string.
7908 */
7909
7910 /*! \fn QStringRef::QStringRef(const QStringRef &other)
7911
7912 Constructs a copy of the \a other string reference.
7913  */
7914 /*!
7915 \fn QStringRef::~QStringRef()
7916
7917 Destroys the string reference.
7918
7919 Since this class is only used to refer to string data, and does not take
7920 ownership of it, no memory is freed when instances are destroyed.
7921 */
7922
7923
7924 /*!
7925     \fn int QStringRef::position() const
7926
7927     Returns the starting position in the referenced string that is referred to
7928     by the string reference.
7929
7930     \sa size(), string()
7931 */
7932
7933 /*!
7934     \fn int QStringRef::size() const
7935
7936     Returns the number of characters referred to by the string reference.
7937     Equivalent to length() and count().
7938
7939     \sa position(), string()
7940 */
7941 /*!
7942     \fn int QStringRef::count() const
7943     Returns the number of characters referred to by the string reference.
7944     Equivalent to size() and length().
7945
7946     \sa position(), string()
7947 */
7948 /*!
7949     \fn int QStringRef::length() const
7950     Returns the number of characters referred to by the string reference.
7951     Equivalent to size() and count().
7952
7953     \sa position(), string()
7954 */
7955
7956
7957 /*!
7958     \fn bool QStringRef::isEmpty() const
7959
7960     Returns true if the string reference has no characters; otherwise returns
7961     false.
7962
7963     A string reference is empty if its size is zero.
7964
7965     \sa size()
7966 */
7967
7968 /*!
7969     \fn bool QStringRef::isNull() const
7970
7971     Returns true if string() returns a null pointer or a pointer to a
7972     null string; otherwise returns true.
7973
7974     \sa size()
7975 */
7976
7977 /*!
7978     \fn const QString *QStringRef::string() const
7979
7980     Returns a pointer to the string referred to by the string reference, or
7981     0 if it does not reference a string.
7982
7983     \sa unicode()
7984 */
7985
7986
7987 /*!
7988     \fn const QChar *QStringRef::unicode() const
7989
7990     Returns a Unicode representation of the string reference. Since
7991     the data stems directly from the referenced string, it is not
7992     null-terminated unless the string reference includes the string's
7993     null terminator.
7994
7995     \sa string()
7996 */
7997
7998 /*!
7999     \fn const QChar *QStringRef::data() const
8000
8001     Same as unicode().
8002 */
8003
8004 /*!
8005     \fn const QChar *QStringRef::constData() const
8006
8007     Same as unicode().
8008 */
8009
8010 /*!
8011     Returns a copy of the string reference as a QString object.
8012
8013     If the string reference is not a complete reference of the string
8014     (meaning that position() is 0 and size() equals string()->size()),
8015     this function will allocate a new string to return.
8016
8017     \sa string()
8018 */
8019
8020 QString QStringRef::toString() const {
8021     if (!m_string)
8022         return QString();
8023     if (m_size && m_position == 0 && m_size == m_string->size())
8024         return *m_string;
8025     return QString(m_string->unicode() + m_position, m_size);
8026 }
8027
8028
8029 /*! \relates QStringRef
8030
8031    Returns true if string reference \a s1 is lexically equal to string reference \a s2; otherwise
8032    returns false.
8033 */
8034 bool operator==(const QStringRef &s1,const QStringRef &s2)
8035 { return (s1.size() == s2.size() &&
8036           qMemEquals((const ushort *)s1.unicode(), (const ushort *)s2.unicode(), s1.size()));
8037 }
8038
8039 /*! \relates QStringRef
8040
8041    Returns true if string \a s1 is lexically equal to string reference \a s2; otherwise
8042    returns false.
8043 */
8044 bool operator==(const QString &s1,const QStringRef &s2)
8045 { return (s1.size() == s2.size() &&
8046           qMemEquals((const ushort *)s1.unicode(), (const ushort *)s2.unicode(), s1.size()));
8047 }
8048
8049 /*! \relates QStringRef
8050
8051    Returns true if string  \a s1 is lexically equal to string reference \a s2; otherwise
8052    returns false.
8053 */
8054 bool operator==(QLatin1String s1, const QStringRef &s2)
8055 {
8056     if (s1.size() != s2.size())
8057         return false;
8058
8059     const ushort *uc = reinterpret_cast<const ushort *>(s2.unicode());
8060     const ushort *e = uc + s2.size();
8061     const uchar *c = reinterpret_cast<const uchar *>(s1.latin1());
8062     if (!c)
8063         return s2.isEmpty();
8064
8065     while (*c) {
8066         if (uc == e || *uc != *c)
8067             return false;
8068         ++uc;
8069         ++c;
8070     }
8071     return (uc == e);
8072 }
8073
8074 /*!
8075    \relates QStringRef
8076
8077     Returns true if string reference \a s1 is lexically less than
8078     string reference \a s2; otherwise returns false.
8079
8080     The comparison is based exclusively on the numeric Unicode values
8081     of the characters and is very fast, but is not what a human would
8082     expect. Consider sorting user-interface strings using the
8083     QString::localeAwareCompare() function.
8084 */
8085 bool operator<(const QStringRef &s1,const QStringRef &s2)
8086 {
8087     return ucstrcmp(s1.constData(), s1.length(), s2.constData(), s2.length()) < 0;
8088 }
8089
8090 /*!\fn bool operator<=(const QStringRef &s1,const QStringRef &s2)
8091
8092    \relates QStringRef
8093
8094     Returns true if string reference \a s1 is lexically less than
8095     or equal to string reference \a s2; otherwise returns false.
8096
8097     The comparison is based exclusively on the numeric Unicode values
8098     of the characters and is very fast, but is not what a human would
8099     expect. Consider sorting user-interface strings using the
8100     QString::localeAwareCompare() function.
8101 */
8102
8103 /*!\fn bool operator>=(const QStringRef &s1,const QStringRef &s2)
8104
8105    \relates QStringRef
8106
8107     Returns true if string reference \a s1 is lexically greater than
8108     or equal to string reference \a s2; otherwise returns false.
8109
8110     The comparison is based exclusively on the numeric Unicode values
8111     of the characters and is very fast, but is not what a human would
8112     expect. Consider sorting user-interface strings using the
8113     QString::localeAwareCompare() function.
8114 */
8115
8116 /*!\fn bool operator>(const QStringRef &s1,const QStringRef &s2)
8117
8118    \relates QStringRef
8119
8120     Returns true if string reference \a s1 is lexically greater than
8121     string reference \a s2; otherwise returns false.
8122
8123     The comparison is based exclusively on the numeric Unicode values
8124     of the characters and is very fast, but is not what a human would
8125     expect. Consider sorting user-interface strings using the
8126     QString::localeAwareCompare() function.
8127 */
8128
8129
8130 /*!
8131     \fn const QChar QStringRef::at(int position) const
8132
8133     Returns the character at the given index \a position in the
8134     string reference.
8135
8136     The \a position must be a valid index position in the string
8137     (i.e., 0 <= \a position < size()).
8138 */
8139
8140 /*!
8141     \fn void QStringRef::clear()
8142
8143     Clears the contents of the string reference by making it null and empty.
8144
8145     \sa isEmpty(), isNull()
8146 */
8147
8148 /*!
8149     \fn QStringRef &QStringRef::operator=(const QStringRef &other)
8150
8151     Assigns the \a other string reference to this string reference, and
8152     returns the result.
8153 */
8154
8155 /*!
8156     \fn QStringRef &QStringRef::operator=(const QString *string)
8157
8158     Constructs a string reference to the given \a string and assigns it to
8159     this string reference, returning the result.
8160 */
8161
8162 /*!
8163     \typedef QString::DataPtr
8164     \internal
8165 */
8166
8167 /*!
8168     \fn DataPtr & QString::data_ptr()
8169     \internal
8170 */
8171
8172
8173
8174 /*!  Appends the string reference to \a string, and returns a new
8175 reference to the combined string data.
8176  */
8177 QStringRef QStringRef::appendTo(QString *string) const
8178 {
8179     if (!string)
8180         return QStringRef();
8181     int pos = string->size();
8182     string->insert(pos, unicode(), size());
8183     return QStringRef(string, pos, size());
8184 }
8185
8186 /*!
8187     \fn int QStringRef::compare(const QStringRef &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
8188     \since 4.5
8189
8190     Compares the string \a s1 with the string \a s2 and returns an
8191     integer less than, equal to, or greater than zero if \a s1
8192     is less than, equal to, or greater than \a s2.
8193
8194     If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
8195     otherwise the comparison is case insensitive.
8196 */
8197
8198 /*!
8199     \fn int QStringRef::compare(const QStringRef &s1, const QStringRef &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
8200     \since 4.5
8201     \overload
8202
8203     Compares the string \a s1 with the string \a s2 and returns an
8204     integer less than, equal to, or greater than zero if \a s1
8205     is less than, equal to, or greater than \a s2.
8206
8207     If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
8208     otherwise the comparison is case insensitive.
8209 */
8210
8211 /*!
8212     \fn int QStringRef::compare(const QStringRef &s1, QLatin1String s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
8213     \since 4.5
8214     \overload
8215
8216     Compares the string \a s1 with the string \a s2 and returns an
8217     integer less than, equal to, or greater than zero if \a s1
8218     is less than, equal to, or greater than \a s2.
8219
8220     If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
8221     otherwise the comparison is case insensitive.
8222 */
8223
8224 /*!
8225     \overload
8226     \fn int QStringRef::compare(const QString &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
8227     \since 4.5
8228
8229     Compares this string with the \a other string and returns an
8230     integer less than, equal to, or greater than zero if this string
8231     is less than, equal to, or greater than the \a other string.
8232
8233     If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
8234     otherwise the comparison is case insensitive.
8235
8236     Equivalent to \c {compare(*this, other, cs)}.
8237
8238     \sa QString::compare()
8239 */
8240
8241 /*!
8242     \overload
8243     \fn int QStringRef::compare(const QStringRef &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
8244     \since 4.5
8245
8246     Compares this string with the \a other string and returns an
8247     integer less than, equal to, or greater than zero if this string
8248     is less than, equal to, or greater than the \a other string.
8249
8250     If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
8251     otherwise the comparison is case insensitive.
8252
8253     Equivalent to \c {compare(*this, other, cs)}.
8254
8255     \sa QString::compare()
8256 */
8257
8258 /*!
8259     \overload
8260     \fn int QStringRef::compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
8261     \since 4.5
8262
8263     Compares this string with the \a other string and returns an
8264     integer less than, equal to, or greater than zero if this string
8265     is less than, equal to, or greater than the \a other string.
8266
8267     If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
8268     otherwise the comparison is case insensitive.
8269
8270     Equivalent to \c {compare(*this, other, cs)}.
8271
8272     \sa QString::compare()
8273 */
8274
8275 /*!
8276     \fn int QStringRef::localeAwareCompare(const QStringRef &s1, const QString & s2)
8277     \since 4.5
8278
8279     Compares \a s1 with \a s2 and returns an integer less than, equal
8280     to, or greater than zero if \a s1 is less than, equal to, or
8281     greater than \a s2.
8282
8283     The comparison is performed in a locale- and also
8284     platform-dependent manner. Use this function to present sorted
8285     lists of strings to the user.
8286
8287     On Mac OS X, this function compares according the
8288     "Order for sorted lists" setting in the International prefereces panel.
8289
8290     \sa compare(), QTextCodec::locale()
8291 */
8292
8293 /*!
8294     \fn int QStringRef::localeAwareCompare(const QStringRef &s1, const QStringRef & s2)
8295     \since 4.5
8296     \overload
8297
8298     Compares \a s1 with \a s2 and returns an integer less than, equal
8299     to, or greater than zero if \a s1 is less than, equal to, or
8300     greater than \a s2.
8301
8302     The comparison is performed in a locale- and also
8303     platform-dependent manner. Use this function to present sorted
8304     lists of strings to the user.
8305
8306 */
8307
8308 /*!
8309     \fn int QStringRef::localeAwareCompare(const QString &other) const
8310     \since 4.5
8311     \overload
8312
8313     Compares this string with the \a other string and returns an
8314     integer less than, equal to, or greater than zero if this string
8315     is less than, equal to, or greater than the \a other string.
8316
8317     The comparison is performed in a locale- and also
8318     platform-dependent manner. Use this function to present sorted
8319     lists of strings to the user.
8320 */
8321
8322 /*!
8323     \fn int QStringRef::localeAwareCompare(const QStringRef &other) const
8324     \since 4.5
8325     \overload
8326
8327     Compares this string with the \a other string and returns an
8328     integer less than, equal to, or greater than zero if this string
8329     is less than, equal to, or greater than the \a other string.
8330
8331     The comparison is performed in a locale- and also
8332     platform-dependent manner. Use this function to present sorted
8333     lists of strings to the user.
8334 */
8335
8336 /*!
8337     \fn QString &QString::append(const QStringRef &reference)
8338     \since 4.4
8339
8340     Appends the given string \a reference to this string and returns the result.
8341  */
8342 QString &QString::append(const QStringRef &str)
8343 {
8344     if (str.string() == this) {
8345         str.appendTo(this);
8346     } else if (str.string()) {
8347         int oldSize = size();
8348         resize(oldSize + str.size());
8349         memcpy(data() + oldSize, str.unicode(), str.size() * sizeof(QChar));
8350     }
8351     return *this;
8352 }
8353
8354 /*!
8355     \since 4.4
8356
8357     Returns a substring reference to the \a n leftmost characters
8358     of the string.
8359
8360     If \a n is greater than size() or less than zero, a reference to the entire
8361     string is returned.
8362
8363     \snippet qstring/main.cpp leftRef
8364
8365     \sa left(), rightRef(), midRef(), startsWith()
8366 */
8367 QStringRef QString::leftRef(int n)  const
8368 {
8369     if (n >= d->size || n < 0)
8370         n = d->size;
8371     return QStringRef(this, 0, n);
8372 }
8373
8374 /*!
8375     \since 4.4
8376
8377     Returns a substring reference to the \a n rightmost characters
8378     of the string.
8379
8380     If \a n is greater than size() or less than zero, a reference to the entire
8381     string is returned.
8382
8383     \snippet qstring/main.cpp rightRef
8384
8385     \sa right(), leftRef(), midRef(), endsWith()
8386 */
8387 QStringRef QString::rightRef(int n) const
8388 {
8389     if (n >= d->size || n < 0)
8390         n = d->size;
8391     return QStringRef(this, d->size - n, n);
8392 }
8393
8394 /*!
8395     \since 4.4
8396
8397     Returns a substring reference to \a n characters of this string,
8398     starting at the specified \a position.
8399
8400     If the \a position exceeds the length of the string, a null
8401     reference is returned.
8402
8403     If there are less than \a n characters available in the string,
8404     starting at the given \a position, or if \a n is -1 (default), the
8405     function returns all characters from the specified \a position
8406     onwards.
8407
8408     Example:
8409
8410     \snippet qstring/main.cpp midRef
8411
8412     \sa mid(), leftRef(), rightRef()
8413 */
8414
8415 QStringRef QString::midRef(int position, int n) const
8416 {
8417     if (position > d->size)
8418         return QStringRef();
8419     if (position < 0) {
8420         if (n < 0 || n + position >= d->size)
8421             return QStringRef(this, 0, d->size);
8422         if (n + position <= 0)
8423             return QStringRef();
8424
8425         n += position;
8426         position = 0;
8427     } else if (n < 0 || n > d->size - position)
8428         n = d->size - position;
8429     return QStringRef(this, position, n);
8430 }
8431
8432 /*!
8433   \since 4.8
8434
8435   Returns the index position of the first occurrence of the string \a
8436   str in this string reference, searching forward from index position
8437   \a from. Returns -1 if \a str is not found.
8438
8439   If \a cs is Qt::CaseSensitive (default), the search is case
8440   sensitive; otherwise the search is case insensitive.
8441
8442   If \a from is -1, the search starts at the last character; if it is
8443   -2, at the next to last character and so on.
8444
8445   \sa QString::indexOf(), lastIndexOf(), contains(), count()
8446 */
8447 int QStringRef::indexOf(const QString &str, int from, Qt::CaseSensitivity cs) const
8448 {
8449     return qFindString(unicode(), length(), from, str.unicode(), str.length(), cs);
8450 }
8451
8452 /*!
8453     \since 4.8
8454     \overload indexOf()
8455
8456     Returns the index position of the first occurrence of the
8457     character \a ch in the string reference, searching forward from
8458     index position \a from. Returns -1 if \a ch could not be found.
8459
8460     \sa QString::indexOf(), lastIndexOf(), contains(), count()
8461 */
8462 int QStringRef::indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
8463 {
8464     return findChar(unicode(), length(), ch, from, cs);
8465 }
8466
8467 /*!
8468   \since 4.8
8469
8470   Returns the index position of the first occurrence of the string \a
8471   str in this string reference, searching forward from index position
8472   \a from. Returns -1 if \a str is not found.
8473
8474   If \a cs is Qt::CaseSensitive (default), the search is case
8475   sensitive; otherwise the search is case insensitive.
8476
8477   If \a from is -1, the search starts at the last character; if it is
8478   -2, at the next to last character and so on.
8479
8480   \sa QString::indexOf(), lastIndexOf(), contains(), count()
8481 */
8482 int QStringRef::indexOf(QLatin1String str, int from, Qt::CaseSensitivity cs) const
8483 {
8484     return qt_find_latin1_string(unicode(), size(), str, from, cs);
8485 }
8486
8487 /*!
8488     \since 4.8
8489
8490     \overload indexOf()
8491
8492     Returns the index position of the first occurrence of the string
8493     reference \a str in this string reference, searching forward from
8494     index position \a from. Returns -1 if \a str is not found.
8495
8496     If \a cs is Qt::CaseSensitive (default), the search is case
8497     sensitive; otherwise the search is case insensitive.
8498
8499     \sa QString::indexOf(), lastIndexOf(), contains(), count()
8500 */
8501 int QStringRef::indexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) const
8502 {
8503     return qFindString(unicode(), size(), from, str.unicode(), str.size(), cs);
8504 }
8505
8506 /*!
8507   \since 4.8
8508
8509   Returns the index position of the last occurrence of the string \a
8510   str in this string reference, searching backward from index position
8511   \a from. If \a from is -1 (default), the search starts at the last
8512   character; if \a from is -2, at the next to last character and so
8513   on. Returns -1 if \a str is not found.
8514
8515   If \a cs is Qt::CaseSensitive (default), the search is case
8516   sensitive; otherwise the search is case insensitive.
8517
8518   \sa QString::lastIndexOf(), indexOf(), contains(), count()
8519 */
8520 int QStringRef::lastIndexOf(const QString &str, int from, Qt::CaseSensitivity cs) const
8521 {
8522     const int sl = str.size();
8523     if (sl == 1)
8524         return lastIndexOf(str.at(0), from, cs);
8525
8526     const int l = size();;
8527     if (from < 0)
8528         from += l;
8529     int delta = l - sl;
8530     if (from == l && sl == 0)
8531         return from;
8532     if (from < 0 || from >= l || delta < 0)
8533         return -1;
8534     if (from > delta)
8535         from = delta;
8536
8537     return lastIndexOfHelper(reinterpret_cast<const ushort*>(unicode()), from,
8538                              reinterpret_cast<const ushort*>(str.unicode()), str.size(), cs);
8539 }
8540
8541 /*!
8542   \since 4.8
8543   \overload lastIndexOf()
8544
8545   Returns the index position of the last occurrence of the character
8546   \a ch, searching backward from position \a from.
8547
8548   \sa QString::lastIndexOf(), indexOf(), contains(), count()
8549 */
8550 int QStringRef::lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
8551 {
8552     return qt_last_index_of(unicode(), size(), ch, from, cs);
8553 }
8554
8555 /*!
8556   \since 4.8
8557   \overload lastIndexOf()
8558
8559   Returns the index position of the last occurrence of the string \a
8560   str in this string reference, searching backward from index position
8561   \a from. If \a from is -1 (default), the search starts at the last
8562   character; if \a from is -2, at the next to last character and so
8563   on. Returns -1 if \a str is not found.
8564
8565   If \a cs is Qt::CaseSensitive (default), the search is case
8566   sensitive; otherwise the search is case insensitive.
8567
8568   \sa QString::lastIndexOf(), indexOf(), contains(), count()
8569 */
8570 int QStringRef::lastIndexOf(QLatin1String str, int from, Qt::CaseSensitivity cs) const
8571 {
8572     const int sl = str.size();
8573     if (sl == 1)
8574         return lastIndexOf(QLatin1Char(str.latin1()[0]), from, cs);
8575
8576     const int l = size();
8577     if (from < 0)
8578         from += l;
8579     int delta = l - sl;
8580     if (from == l && sl == 0)
8581         return from;
8582     if (from < 0 || from >= l || delta < 0)
8583         return -1;
8584     if (from > delta)
8585         from = delta;
8586
8587     QVarLengthArray<ushort> s(sl);
8588     for (int i = 0; i < sl; ++i)
8589         s[i] = str.latin1()[i];
8590
8591     return lastIndexOfHelper(reinterpret_cast<const ushort*>(unicode()), from, s.data(), sl, cs);
8592 }
8593
8594 /*!
8595   \since 4.8
8596   \overload lastIndexOf()
8597
8598   Returns the index position of the last occurrence of the string
8599   reference \a str in this string reference, searching backward from
8600   index position \a from. If \a from is -1 (default), the search
8601   starts at the last character; if \a from is -2, at the next to last
8602   character and so on. Returns -1 if \a str is not found.
8603
8604   If \a cs is Qt::CaseSensitive (default), the search is case
8605   sensitive; otherwise the search is case insensitive.
8606
8607   \sa QString::lastIndexOf(), indexOf(), contains(), count()
8608 */
8609 int QStringRef::lastIndexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) const
8610 {
8611     const int sl = str.size();
8612     if (sl == 1)
8613         return lastIndexOf(str.at(0), from, cs);
8614
8615     const int l = size();
8616     if (from < 0)
8617         from += l;
8618     int delta = l - sl;
8619     if (from == l && sl == 0)
8620         return from;
8621     if (from < 0 || from >= l || delta < 0)
8622         return -1;
8623     if (from > delta)
8624         from = delta;
8625
8626     return lastIndexOfHelper(reinterpret_cast<const ushort*>(unicode()), from,
8627                              reinterpret_cast<const ushort*>(str.unicode()),
8628                              str.size(), cs);
8629 }
8630
8631 /*!
8632     \since 4.8
8633     Returns the number of (potentially overlapping) occurrences of
8634     the string \a str in this string reference.
8635
8636     If \a cs is Qt::CaseSensitive (default), the search is
8637     case sensitive; otherwise the search is case insensitive.
8638
8639     \sa QString::count(), contains(), indexOf()
8640 */
8641 int QStringRef::count(const QString &str, Qt::CaseSensitivity cs) const
8642 {
8643     return qt_string_count(unicode(), size(), str.unicode(), str.size(), cs);
8644 }
8645
8646 /*!
8647     \since 4.8
8648     \overload count()
8649
8650     Returns the number of occurrences of the character \a ch in the
8651     string reference.
8652
8653     If \a cs is Qt::CaseSensitive (default), the search is
8654     case sensitive; otherwise the search is case insensitive.
8655
8656     \sa QString::count(), contains(), indexOf()
8657 */
8658 int QStringRef::count(QChar ch, Qt::CaseSensitivity cs) const
8659 {
8660     return qt_string_count(unicode(), size(), ch, cs);
8661 }
8662
8663 /*!
8664     \since 4.8
8665     \overload count()
8666
8667     Returns the number of (potentially overlapping) occurrences of the
8668     string reference \a str in this string reference.
8669
8670     If \a cs is Qt::CaseSensitive (default), the search is
8671     case sensitive; otherwise the search is case insensitive.
8672
8673     \sa QString::count(), contains(), indexOf()
8674 */
8675 int QStringRef::count(const QStringRef &str, Qt::CaseSensitivity cs) const
8676 {
8677     return qt_string_count(unicode(), size(), str.unicode(), str.size(), cs);
8678 }
8679
8680 /*!
8681     \since 4.8
8682
8683     Returns true if the string reference starts with \a str; otherwise
8684     returns false.
8685
8686     If \a cs is Qt::CaseSensitive (default), the search is
8687     case sensitive; otherwise the search is case insensitive.
8688
8689     \sa QString::startsWith(), endsWith()
8690 */
8691 bool QStringRef::startsWith(const QString &str, Qt::CaseSensitivity cs) const
8692 {
8693     return qt_starts_with(isNull() ? 0 : unicode(), size(),
8694                           str.isNull() ? 0 : str.unicode(), str.size(), cs);
8695 }
8696
8697 /*!
8698     \since 4.8
8699     \overload startsWith()
8700     \sa QString::startsWith(), endsWith()
8701 */
8702 bool QStringRef::startsWith(QLatin1String str, Qt::CaseSensitivity cs) const
8703 {
8704     return qt_starts_with(isNull() ? 0 : unicode(), size(), str, cs);
8705 }
8706
8707 /*!
8708     \since 4.8
8709     \overload startsWith()
8710     \sa QString::startsWith(), endsWith()
8711 */
8712 bool QStringRef::startsWith(const QStringRef &str, Qt::CaseSensitivity cs) const
8713 {
8714     return qt_starts_with(isNull() ? 0 : unicode(), size(),
8715                           str.isNull() ? 0 : str.unicode(), str.size(), cs);
8716 }
8717
8718 /*!
8719     \since 4.8
8720     \overload startsWith()
8721
8722     Returns true if the string reference starts with \a ch; otherwise
8723     returns false.
8724
8725     If \a cs is Qt::CaseSensitive (default), the search is case
8726     sensitive; otherwise the search is case insensitive.
8727
8728     \sa QString::startsWith(), endsWith()
8729 */
8730 bool QStringRef::startsWith(QChar ch, Qt::CaseSensitivity cs) const
8731 {
8732     if (!isEmpty()) {
8733         const ushort *data = reinterpret_cast<const ushort*>(unicode());
8734         return (cs == Qt::CaseSensitive
8735                 ? data[0] == ch
8736                 : foldCase(data[0]) == foldCase(ch.unicode()));
8737     } else {
8738         return false;
8739     }
8740 }
8741
8742 /*!
8743     \since 4.8
8744     Returns true if the string reference ends with \a str; otherwise
8745     returns false.
8746
8747     If \a cs is Qt::CaseSensitive (default), the search is case
8748     sensitive; otherwise the search is case insensitive.
8749
8750     \sa QString::endsWith(), startsWith()
8751 */
8752 bool QStringRef::endsWith(const QString &str, Qt::CaseSensitivity cs) const
8753 {
8754     return qt_ends_with(isNull() ? 0 : unicode(), size(),
8755                         str.isNull() ? 0 : str.unicode(), str.size(), cs);
8756 }
8757
8758 /*!
8759     \since 4.8
8760     \overload endsWith()
8761
8762     Returns true if the string reference ends with \a ch; otherwise
8763     returns false.
8764
8765     If \a cs is Qt::CaseSensitive (default), the search is case
8766     sensitive; otherwise the search is case insensitive.
8767
8768     \sa QString::endsWith(), endsWith()
8769 */
8770 bool QStringRef::endsWith(QChar ch, Qt::CaseSensitivity cs) const
8771 {
8772     if (!isEmpty()) {
8773         const ushort *data = reinterpret_cast<const ushort*>(unicode());
8774         const int size = length();
8775         return (cs == Qt::CaseSensitive
8776                 ? data[size - 1] == ch
8777                 : foldCase(data[size - 1]) == foldCase(ch.unicode()));
8778     } else {
8779         return false;
8780     }
8781 }
8782
8783 /*!
8784     \since 4.8
8785     \overload endsWith()
8786     \sa QString::endsWith(), endsWith()
8787 */
8788 bool QStringRef::endsWith(QLatin1String str, Qt::CaseSensitivity cs) const
8789 {
8790     return qt_ends_with(isNull() ? 0 : unicode(), size(), str, cs);
8791 }
8792
8793 /*!
8794     \since 4.8
8795     \overload endsWith()
8796     \sa QString::endsWith(), endsWith()
8797 */
8798 bool QStringRef::endsWith(const QStringRef &str, Qt::CaseSensitivity cs) const
8799 {
8800     return qt_ends_with(isNull() ? 0 : unicode(), size(),
8801                         str.isNull() ? 0 : str.unicode(), str.size(), cs);
8802 }
8803
8804
8805 /*! \fn bool QStringRef::contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
8806
8807     \since 4.8
8808     Returns true if this string reference contains an occurrence of
8809     the string \a str; otherwise returns false.
8810
8811     If \a cs is Qt::CaseSensitive (default), the search is
8812     case sensitive; otherwise the search is case insensitive.
8813
8814     \sa indexOf(), count()
8815 */
8816
8817 /*! \fn bool QStringRef::contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
8818
8819     \overload contains()
8820     \since 4.8
8821
8822     Returns true if this string contains an occurrence of the
8823     character \a ch; otherwise returns false.
8824
8825     If \a cs is Qt::CaseSensitive (default), the search is
8826     case sensitive; otherwise the search is case insensitive.
8827
8828 */
8829
8830 /*! \fn bool QStringRef::contains(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
8831     \overload contains()
8832     \since 4.8
8833
8834     Returns true if this string reference contains an occurrence of
8835     the string reference \a str; otherwise returns false.
8836
8837     If \a cs is Qt::CaseSensitive (default), the search is
8838     case sensitive; otherwise the search is case insensitive.
8839
8840     \sa indexOf(), count()
8841 */
8842
8843 /*! \fn bool QStringRef::contains(QLatin1String str, Qt::CaseSensitivity cs) const
8844     \since 4,8
8845     \overload contains()
8846
8847     Returns true if this string reference contains an occurrence of
8848     the string \a str; otherwise returns false.
8849
8850     If \a cs is Qt::CaseSensitive (default), the search is
8851     case sensitive; otherwise the search is case insensitive.
8852
8853     \sa indexOf(), count()
8854 */
8855
8856 static inline int qt_last_index_of(const QChar *haystack, int haystackLen, QChar needle,
8857                                    int from, Qt::CaseSensitivity cs)
8858 {
8859     ushort c = needle.unicode();
8860     if (from < 0)
8861         from += haystackLen;
8862     if (from < 0 || from >= haystackLen)
8863         return -1;
8864     if (from >= 0) {
8865         const ushort *b = reinterpret_cast<const ushort*>(haystack);
8866         const ushort *n = b + from;
8867         if (cs == Qt::CaseSensitive) {
8868             for (; n >= b; --n)
8869                 if (*n == c)
8870                     return n - b;
8871         } else {
8872             c = foldCase(c);
8873             for (; n >= b; --n)
8874                 if (foldCase(*n) == c)
8875                     return n - b;
8876         }
8877     }
8878     return -1;
8879
8880
8881 }
8882
8883 static inline int qt_string_count(const QChar *haystack, int haystackLen,
8884                                   const QChar *needle, int needleLen,
8885                                   Qt::CaseSensitivity cs)
8886 {
8887     int num = 0;
8888     int i = -1;
8889     if (haystackLen > 500 && needleLen > 5) {
8890         QStringMatcher matcher(needle, needleLen, cs);
8891         while ((i = matcher.indexIn(haystack, haystackLen, i + 1)) != -1)
8892             ++num;
8893     } else {
8894         while ((i = qFindString(haystack, haystackLen, i + 1, needle, needleLen, cs)) != -1)
8895             ++num;
8896     }
8897     return num;
8898 }
8899
8900 static inline int qt_string_count(const QChar *unicode, int size, QChar ch,
8901                                   Qt::CaseSensitivity cs)
8902 {
8903     ushort c = ch.unicode();
8904     int num = 0;
8905     const ushort *b = reinterpret_cast<const ushort*>(unicode);
8906     const ushort *i = b + size;
8907     if (cs == Qt::CaseSensitive) {
8908         while (i != b)
8909             if (*--i == c)
8910                 ++num;
8911     } else {
8912         c = foldCase(c);
8913         while (i != b)
8914             if (foldCase(*(--i)) == c)
8915                 ++num;
8916     }
8917     return num;
8918 }
8919
8920 static inline int qt_find_latin1_string(const QChar *haystack, int size,
8921                                         QLatin1String needle,
8922                                         int from, Qt::CaseSensitivity cs)
8923 {
8924     const char *latin1 = needle.latin1();
8925     int len = needle.size();
8926     QVarLengthArray<ushort> s(len);
8927     for (int i = 0; i < len; ++i)
8928         s[i] = latin1[i];
8929
8930     return qFindString(haystack, size, from,
8931                        reinterpret_cast<const QChar*>(s.constData()), len, cs);
8932 }
8933
8934 static inline bool qt_starts_with(const QChar *haystack, int haystackLen,
8935                                   const QChar *needle, int needleLen, Qt::CaseSensitivity cs)
8936 {
8937     if (!haystack)
8938         return !needle;
8939     if (haystackLen == 0)
8940         return needleLen == 0;
8941     if (needleLen > haystackLen)
8942         return false;
8943
8944     const ushort *h = reinterpret_cast<const ushort*>(haystack);
8945     const ushort *n = reinterpret_cast<const ushort*>(needle);
8946
8947     if (cs == Qt::CaseSensitive) {
8948         return qMemEquals(h, n, needleLen);
8949     } else {
8950         uint last = 0;
8951         uint olast = 0;
8952         for (int i = 0; i < needleLen; ++i)
8953             if (foldCase(h[i], last) != foldCase(n[i], olast))
8954                 return false;
8955     }
8956     return true;
8957 }
8958
8959 static inline bool qt_starts_with(const QChar *haystack, int haystackLen,
8960                                   QLatin1String needle, Qt::CaseSensitivity cs)
8961 {
8962     if (!haystack)
8963         return !needle.latin1();
8964     if (haystackLen == 0)
8965         return !needle.latin1() || *needle.latin1() == 0;
8966     const int slen = needle.size();
8967     if (slen > haystackLen)
8968         return false;
8969     const ushort *data = reinterpret_cast<const ushort*>(haystack);
8970     const uchar *latin = reinterpret_cast<const uchar*>(needle.latin1());
8971     if (cs == Qt::CaseSensitive) {
8972         for (int i = 0; i < slen; ++i)
8973             if (data[i] != latin[i])
8974                 return false;
8975     } else {
8976         for (int i = 0; i < slen; ++i)
8977             if (foldCase(data[i]) != foldCase((ushort)latin[i]))
8978                 return false;
8979     }
8980     return true;
8981 }
8982
8983 static inline bool qt_ends_with(const QChar *haystack, int haystackLen,
8984                                 const QChar *needle, int needleLen, Qt::CaseSensitivity cs)
8985 {
8986     if (!haystack)
8987         return !needle;
8988     if (haystackLen == 0)
8989         return needleLen == 0;
8990     const int pos = haystackLen - needleLen;
8991     if (pos < 0)
8992         return false;
8993
8994     const ushort *h = reinterpret_cast<const ushort*>(haystack);
8995     const ushort *n = reinterpret_cast<const ushort*>(needle);
8996
8997     if (cs == Qt::CaseSensitive) {
8998         return qMemEquals(h + pos, n, needleLen);
8999     } else {
9000         uint last = 0;
9001         uint olast = 0;
9002         for (int i = 0; i < needleLen; i++)
9003             if (foldCase(h[pos+i], last) != foldCase(n[i], olast))
9004                 return false;
9005     }
9006     return true;
9007 }
9008
9009
9010 static inline bool qt_ends_with(const QChar *haystack, int haystackLen,
9011                                 QLatin1String needle, Qt::CaseSensitivity cs)
9012 {
9013     if (!haystack)
9014         return !needle.latin1();
9015     if (haystackLen == 0)
9016         return !needle.latin1() || *needle.latin1() == 0;
9017     const int slen = needle.size();
9018     int pos = haystackLen - slen;
9019     if (pos < 0)
9020         return false;
9021     const uchar *latin = reinterpret_cast<const uchar*>(needle.latin1());
9022     const ushort *data = reinterpret_cast<const ushort*>(haystack);
9023     if (cs == Qt::CaseSensitive) {
9024         for (int i = 0; i < slen; i++)
9025             if (data[pos+i] != latin[i])
9026                 return false;
9027     } else {
9028         for (int i = 0; i < slen; i++)
9029             if (foldCase(data[pos+i]) != foldCase((ushort)latin[i]))
9030                 return false;
9031     }
9032     return true;
9033 }
9034
9035 /*!
9036     \since 4.8
9037
9038     Returns a Latin-1 representation of the string as a QByteArray.
9039
9040     The returned byte array is undefined if the string contains non-Latin1
9041     characters. Those characters may be suppressed or replaced with a
9042     question mark.
9043
9044     \sa toUtf8(), toLocal8Bit(), QTextCodec
9045 */
9046 QByteArray QStringRef::toLatin1() const
9047 {
9048     return toLatin1_helper(unicode(), length());
9049 }
9050
9051 /*!
9052     \fn QByteArray QStringRef::toAscii() const
9053     \since 4.8
9054     \deprecated
9055
9056     Returns an 8-bit representation of the string as a QByteArray.
9057
9058     This function does the same as toLatin1().
9059
9060     Note that, despite the name, this function does not necessarily return an US-ASCII
9061     (ANSI X3.4-1986) string and its result may not be US-ASCII compatible.
9062
9063     \sa toLatin1(), toUtf8(), toLocal8Bit(), QTextCodec
9064 */
9065
9066 /*!
9067     \since 4.8
9068
9069     Returns the local 8-bit representation of the string as a
9070     QByteArray. The returned byte array is undefined if the string
9071     contains characters not supported by the local 8-bit encoding.
9072
9073     QTextCodec::codecForLocale() is used to perform the conversion from
9074     Unicode. If the locale encoding could not be determined, this function
9075     does the same as toLatin1().
9076
9077     If this string contains any characters that cannot be encoded in the
9078     locale, the returned byte array is undefined. Those characters may be
9079     suppressed or replaced by another.
9080
9081     \sa toLatin1(), toUtf8(), QTextCodec
9082 */
9083 QByteArray QStringRef::toLocal8Bit() const
9084 {
9085 #ifndef QT_NO_TEXTCODEC
9086     if (QTextCodec::codecForLocale())
9087         return QTextCodec::codecForLocale()->fromUnicode(unicode(), length());
9088 #endif // QT_NO_TEXTCODEC
9089     return toLatin1();
9090 }
9091
9092 /*!
9093     \since 4.8
9094
9095     Returns a UTF-8 representation of the string as a QByteArray.
9096
9097     UTF-8 is a Unicode codec and can represent all characters in a Unicode
9098     string like QString.
9099
9100     However, in the Unicode range, there are certain codepoints that are not
9101     considered characters. The Unicode standard reserves the last two
9102     codepoints in each Unicode Plane (U+FFFE, U+FFFF, U+1FFFE, U+1FFFF,
9103     U+2FFFE, etc.), as well as 16 codepoints in the range U+FDD0..U+FDDF,
9104     inclusive, as non-characters. If any of those appear in the string, they
9105     may be discarded and will not appear in the UTF-8 representation, or they
9106     may be replaced by one or more replacement characters.
9107
9108     \sa toLatin1(), toLocal8Bit(), QTextCodec
9109 */
9110 QByteArray QStringRef::toUtf8() const
9111 {
9112     if (isNull())
9113         return QByteArray();
9114
9115     return QUtf8::convertFromUnicode(constData(), length(), 0);
9116 }
9117
9118 /*!
9119     \since 4.8
9120
9121     Returns a UCS-4/UTF-32 representation of the string as a QVector<uint>.
9122
9123     UCS-4 is a Unicode codec and is lossless. All characters from this string
9124     can be encoded in UCS-4.
9125
9126     \sa toUtf8(), toLatin1(), toLocal8Bit(), QTextCodec
9127 */
9128 QVector<uint> QStringRef::toUcs4() const
9129 {
9130     QVector<uint> v(length());
9131     uint *a = v.data();
9132     int len = QString::toUcs4_helper(reinterpret_cast<const ushort *>(unicode()), length(), a);
9133     v.resize(len);
9134     return v;
9135 }
9136
9137
9138 /*!
9139     \obsolete
9140     \fn QString Qt::escape(const QString &plain)
9141
9142     \sa QString::toHtmlEscaped()
9143 */
9144
9145 /*!
9146     Converts the plain text string \a plain to a HTML string with
9147     HTML metacharacters \c{<}, \c{>}, \c{&}, and \c{"} replaced by HTML
9148     entities.
9149
9150     Example:
9151
9152     \snippet code/src_corelib_tools_qstring.cpp 7
9153 */
9154 QString QString::toHtmlEscaped() const
9155 {
9156     QString rich;
9157     const int len = length();
9158     rich.reserve(int(len * 1.1));
9159     for (int i = 0; i < len; ++i) {
9160         if (at(i) == QLatin1Char('<'))
9161             rich += QLatin1String("&lt;");
9162         else if (at(i) == QLatin1Char('>'))
9163             rich += QLatin1String("&gt;");
9164         else if (at(i) == QLatin1Char('&'))
9165             rich += QLatin1String("&amp;");
9166         else if (at(i) == QLatin1Char('"'))
9167             rich += QLatin1String("&quot;");
9168         else
9169             rich += at(i);
9170     }
9171     rich.squeeze();
9172     return rich;
9173 }
9174
9175 /*!
9176   \macro QStringLiteral(str)
9177   \relates QString
9178
9179   The macro generates the data for a QString out of \a str at compile time if the compiler supports it.
9180   Creating a QString from it is free in this case, and the generated string data is stored in
9181   the read-only segment of the compiled object file.
9182
9183   For compilers not supporting the creation of compile time strings, QStringLiteral will fall back to
9184   QLatin1String.
9185
9186   The result of the QStringLiteral expression can be cast into a QString.
9187
9188   If you have code looking like:
9189   \code
9190   if (node.hasAttribute("http-contents-length")) //...
9191   \endcode
9192   One temporary QString will be created to be passed as the hasAttribute function parameter.
9193   This can be quite expensive, as it involves a memory allocation and the copy and the conversion
9194   of the data into QString's internal encoding.
9195
9196   This can be avoided by doing
9197   \code
9198   if (node.hasAttribute(QStringLiteral("http-contents-length"))) //...
9199   \endcode
9200   Then the QString's internal data will be generated at compile time and no conversion or allocation
9201   will occur at runtime
9202
9203   Using QStringLiteral instead of a double quoted ascii literal can significantly speed up creation
9204   of QString's from data known at compile time.
9205
9206   If the compiler is C++11 enabled the string \a str can actually contain unicode data.
9207
9208   \note There are still a few cases in which QLatin1String is more efficient than QStringLiteral:
9209   If it is passed to a function that has an overload that takes the QLatin1String directly, without
9210   conversion to QString. For instance, this is the case of QString::operator==
9211   \code
9212   if (attribute.name() == QLatin1String("http-contents-length")) //...
9213   \endcode
9214 */
9215
9216 QT_END_NAMESPACE