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