~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/corelib/tools/qstring.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the core module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "qstringlist.h"
 
30
#include "qregexp.h"
 
31
#include "qunicodetables_p.h"
 
32
#ifndef QT_NO_TEXTCODEC
 
33
#include <qtextcodec.h>
 
34
#endif
 
35
#include <qdatastream.h>
 
36
#include <qlist.h>
 
37
#include "qlocale.h"
 
38
#include "qlocale_p.h"
 
39
#include "qstringmatcher.h"
 
40
#include "qtools_p.h"
 
41
#include "qhash.h"
 
42
#include "qdebug.h"
 
43
 
 
44
#include <limits.h>
 
45
#include <string.h>
 
46
#include <stdlib.h>
 
47
#include <stdio.h>
 
48
#include <stdarg.h>
 
49
 
 
50
#ifndef QT_NO_STL
 
51
# if defined (Q_CC_GNU) && (__GNUC__ - 0 >= 3)
 
52
#  include <string>
 
53
# endif
 
54
#endif
 
55
 
 
56
#ifdef truncate
 
57
#undef truncate
 
58
#endif
 
59
 
 
60
#ifndef LLONG_MAX
 
61
#define LLONG_MAX qint64_C(9223372036854775807)
 
62
#endif
 
63
#ifndef LLONG_MIN
 
64
#define LLONG_MIN (-LLONG_MAX - qint64_C(1))
 
65
#endif
 
66
#ifndef ULLONG_MAX
 
67
#define ULLONG_MAX quint64_C(18446744073709551615)
 
68
#endif
 
69
 
 
70
 
 
71
#ifndef QT_NO_TEXTCODEC
 
72
QTextCodec *QString::codecForCStrings;
 
73
#endif
 
74
 
 
75
#ifdef QT3_SUPPORT
 
76
static QHash<void *, QByteArray> *asciiCache = 0;
 
77
#endif
 
78
 
 
79
static int ucstrcmp(const QString &as, const QString &bs)
 
80
{
 
81
    const QChar *a = as.unicode();
 
82
    const QChar *b = bs.unicode();
 
83
    if (a == b)
 
84
        return 0;
 
85
    if (a == 0)
 
86
        return 1;
 
87
    if (b == 0)
 
88
        return -1;
 
89
    int l=qMin(as.length(),bs.length());
 
90
    while (l-- && *a == *b)
 
91
        a++,b++;
 
92
    if (l==-1)
 
93
        return (as.length()-bs.length());
 
94
    return a->unicode() - b->unicode();
 
95
}
 
96
 
 
97
static int ucstrncmp(const QChar *a, const QChar *b, int l)
 
98
{
 
99
    while (l-- && *a == *b)
 
100
        a++,b++;
 
101
    if (l==-1)
 
102
        return 0;
 
103
    return a->unicode() - b->unicode();
 
104
}
 
105
 
 
106
static int ucstrnicmp(const QChar *a, const QChar *b, int l)
 
107
{
 
108
    while (l-- && ::lower(*a) == ::lower(*b))
 
109
        a++,b++;
 
110
    if (l==-1)
 
111
        return 0;
 
112
    return ::lower(*a).unicode() - ::lower(*b).unicode();
 
113
}
 
114
 
 
115
 
 
116
inline bool qIsUpper(char ch)
 
117
{
 
118
    return ch >= 'A' && ch <= 'Z';
 
119
}
 
120
 
 
121
inline bool qIsDigit(char ch)
 
122
{
 
123
    return ch >= '0' && ch <= '9';
 
124
}
 
125
 
 
126
inline char qToLower(char ch)
 
127
{
 
128
    if (ch >= 'A' && ch <= 'Z')
 
129
        return ch - 'A' + 'a';
 
130
    else
 
131
        return ch;
 
132
}
 
133
 
 
134
const QString::Null QString::null = QString::Null();
 
135
 
 
136
/*!
 
137
    \class QCharRef
 
138
    \reentrant
 
139
    \brief The QCharRef class is a helper class for QString.
 
140
 
 
141
    \internal
 
142
 
 
143
    \ingroup text
 
144
 
 
145
    When you get an object of type QCharRef, if you can assign to it,
 
146
    the assignment will apply to the character in the string from
 
147
    which you got the reference. That is its whole purpose in life.
 
148
    The QCharRef becomes invalid once modifications are made to the
 
149
    string: if you want to keep the character, copy it into a QChar.
 
150
 
 
151
    Most of the QChar member functions also exist in QCharRef.
 
152
    However, they are not explicitly documented here.
 
153
 
 
154
    \sa QString::operator[]() QString::at() QChar
 
155
*/
 
156
 
 
157
/*!
 
158
    \class QString
 
159
    \reentrant
 
160
 
 
161
    \brief The QString class provides a Unicode character string.
 
162
 
 
163
    \ingroup tools
 
164
    \ingroup shared
 
165
    \ingroup text
 
166
    \mainclass
 
167
    \reentrant
 
168
 
 
169
    QString stores a string of 16-bit \l{QChar}s, where each QChar
 
170
    stores one Unicode 4.0 character. \l{Unicode} is an international
 
171
    standard that supports most of the writing systems in use today.
 
172
    It is a superset of ASCII and Latin-1 (ISO 8859-1), and all the
 
173
    ASCII/Latin-1 characters are available at the same code
 
174
    positions.
 
175
 
 
176
    Behind the scenes, QString uses \l{implicit sharing}
 
177
    (copy-on-write) to reduce memory usage and to avoid the needless
 
178
    copying of data. This also helps reduce the inherent overhead of
 
179
    storing 16-bit characters instead of 8-bit characters.
 
180
 
 
181
    In addition to QString, Qt also provides the QByteArray class to
 
182
    store raw bytes and traditional 8-bit '\\0'-terminated strings.
 
183
    For most purposes, QString is the class you want to use. It is
 
184
    used throughout the Qt API, and the Unicode support ensures that
 
185
    your applications will be easy to translate if you want to expand
 
186
    your application's market at some point. The two main cases where
 
187
    QByteArray is appropriate are when you need to store raw binary
 
188
    data, and when memory conservation is critical (e.g. with
 
189
    Qt/Embedded).
 
190
 
 
191
    One way to initialize a QString is simply to pass a \c{const char
 
192
    *} to its constructor. For example, the following code creates a
 
193
    QString of size 5 containing the data "Hello":
 
194
 
 
195
    \code
 
196
        QString str = "Hello";
 
197
    \endcode
 
198
 
 
199
    QString converts the \c{const char *} data into Unicode using
 
200
    fromAscii(). By default, fromAscii() treats character above 128
 
201
    as Latin-1 characters, but this can be changed by calling
 
202
    QTextCodec::setCodecForCStrings().
 
203
 
 
204
    In all of the QString methods that take \c{const char *}
 
205
    parameters, the \c{const char *} is interpreted as a classic
 
206
    C-style '\\0'-terminated string. It is legal for the \c{const
 
207
    char *} parameter to be 0.
 
208
 
 
209
    You can also provide string data as an array of \l{QChar}s:
 
210
 
 
211
    \code
 
212
        static const QChar data[4] = { 0x0055, 0x006e, 0x10e3, 0x03a3 };
 
213
        QString str(data, 4);
 
214
    \endcode
 
215
 
 
216
    QString makes a deep copy of the QChar data, so you can modify it
 
217
    later without experiencing side effects. (If for performance
 
218
    reasons you don't want to take a deep copy of the character data,
 
219
    use QString::fromRawData() instead.)
 
220
 
 
221
    Another approach is to set the size of the string using resize()
 
222
    and to initialize the data character per character. QString uses
 
223
    0-based indexes, just like C++ arrays. To access the character at
 
224
    a particular index position, you can use operator[](). On
 
225
    non-const strings, operator[]() returns a reference to a
 
226
    character that can be used on the left side of an assignment. For
 
227
    example:
 
228
 
 
229
    \code
 
230
        QString str;
 
231
        str.resize(4);
 
232
        str[0] = QChar('U');
 
233
        str[1] = QChar('n');
 
234
        str[2] = QChar(0x10e3);
 
235
        str[3] = QChar(0x03a3);
 
236
    \endcode
 
237
 
 
238
    For read-only access, an alternative syntax is to use at():
 
239
 
 
240
    \code
 
241
        for (int i = 0; i < str.size(); ++i) {
 
242
            if (str.at(i) >= QChar('a') && str.at(i) <= QChar('f'))
 
243
                cout << "Found character in range [a-f]" << endl;
 
244
        }
 
245
    \endcode
 
246
 
 
247
    at() can be faster than operator[](), because it never causes a
 
248
    \l{deep copy} to occur.
 
249
 
 
250
    To extract several characters at a time, use left(), right(), or
 
251
    mid().
 
252
 
 
253
    A QString can embed '\\0' characters (QChar::null). The size()
 
254
    function always returns the size of the whole string, including
 
255
    embedded '\\0' characters.
 
256
 
 
257
    After a call to resize(), newly allocated characters have
 
258
    undefined values. To set all the characters in the string to a
 
259
    particular value, call fill().
 
260
 
 
261
    QString provides dozens of overloads designed to simplify string
 
262
    usage. For example, if you want to compare a QString with a string
 
263
    literal, you can write code like this and it will work as expected:
 
264
 
 
265
    \code
 
266
        if (str == "auto" || str == "extern"
 
267
                || str == "static" || str == "register") {
 
268
            ...
 
269
        }
 
270
    \endcode
 
271
 
 
272
    You can also pass string literals to functions that take QStrings
 
273
    and the QString(const char *) constructor will be invoked.
 
274
    Similarly, you can pass a QString to a function that takes a
 
275
    \c{const char *} and \l{operator const char *()} will be invoked.
 
276
 
 
277
    QString provides the following basic functions for modifying the
 
278
    character data: append(), prepend(), insert(), replace(), and
 
279
    remove(). For example:
 
280
 
 
281
    \code
 
282
        QString str = "and";
 
283
        str.prepend("rock ");           // str == "rock and"
 
284
        str.append(" roll");            // str == "rock and roll"
 
285
        str.replace(5, 3, "&");         // str == "rock & roll"
 
286
    \endcode
 
287
 
 
288
    The replace() and remove() functions' first two arguments are the
 
289
    position from which to start erasing and the number of characters
 
290
    that should be erased.
 
291
 
 
292
    A frequent requirement is to remove whitespace characters from a
 
293
    string ('\\n', '\\t', ' ', etc.). If you want to remove whitespace
 
294
    from both ends of a QString, use trimmed(). If you want to remove
 
295
    whitespace from both ends and replace multiple consecutive
 
296
    whitespaces with a single space character within the string, use
 
297
    simplified().
 
298
 
 
299
    If you want to find all occurrences of a particular character or
 
300
    substring in a QString, use indexOf() or lastIndexOf(). The
 
301
    former searches forward starting from a given index position, the
 
302
    latter searches backward. Both return the index position of the
 
303
    character or substring if they find it; otherwise, they return -1.
 
304
    For example, here's a typical loop that finds all occurrences of a
 
305
    particular substring:
 
306
 
 
307
    \code
 
308
        QString str = "We must be <b>bold</b>, very <b>bold</b>";
 
309
        int j = 0;
 
310
        while ((j = str.indexOf("<b>", j)) != -1) {
 
311
            cout << "Found <b> tag at index position " << j << endl;
 
312
            ++j;
 
313
        }
 
314
    \endcode
 
315
 
 
316
    If you want to see if a QString starts or ends with a particular
 
317
    substring use startsWith() or endsWith(). If you simply want to
 
318
    check whether a QString contains a particular character or
 
319
    substring, use contains(). If you want to find out how many times
 
320
    a particular character or substring occurs in the string, use
 
321
    count().
 
322
 
 
323
    QString provides many functions for converting numbers into
 
324
    strings and strings into numbers. See the arg() functions, the
 
325
    setNum() functions, the number() static functions, and the
 
326
    toInt(), toDouble(), and similar functions.
 
327
 
 
328
    To get an upper or lower case version of a string use toUpper() or
 
329
    toLower().
 
330
 
 
331
    If you want to replace all occurrences of a particular substring
 
332
    with another, use one of the two-parameter replace() overloads.
 
333
 
 
334
    QStrings can be compared using overloaded operators such as
 
335
    operator<(), operator<=(), operator==(), operator>=(), and so on.
 
336
    The comparison is based exclusively on the numeric Unicode values
 
337
    of the characters and is very fast, but is not what a human would
 
338
    expect. QString::localeAwareCompare() is a better choice for
 
339
    sorting user-interface strings.
 
340
 
 
341
    Lists of strings are handled by the QStringList class. You can
 
342
    split a string into a list of strings using split(), and join a
 
343
    list of strings into a single string with an optional separator
 
344
    using QStringList::join(). You can obtain a list of strings from
 
345
    a string list that contain a particular substring or that match a
 
346
    particular QRegExp using QStringList::find().
 
347
 
 
348
    If you are building a QString gradually and know in advance
 
349
    approximately how many characters the QString will contain, you
 
350
    can call reserve(), asking QString to preallocate a certain amount
 
351
    of memory. You can also call capacity() to find out how much
 
352
    memory QString actually allocated.
 
353
 
 
354
    To obtain a pointer to the actual character data, call data() or
 
355
    constData(). These functions return a pointer to the beginning of
 
356
    the QChar data. The pointer is guaranteed to remain valid until a
 
357
    non-const function is called on the QString.
 
358
 
 
359
    \section1 Conversions between 8-bit strings and Unicode strings
 
360
 
 
361
    QString provides the following four functions that return a
 
362
    \c{const char *} version of the string as QByteArray: toAscii(),
 
363
    toLatin1(), toUtf8(), and toLocal8Bit().
 
364
 
 
365
    \list
 
366
    \i toAscii() returns an ASCII encoded 8-bit string.
 
367
    \i toLatin1() returns a Latin-1 (ISO 8859-1) encoded 8-bit string.
 
368
    \i toUtf8() returns a UTF-8 encoded 8-bit string. UTF-8 is a
 
369
       superset of ASCII that supports the entire Unicode character
 
370
       set through multibyte sequences.
 
371
    \i toLocal8Bit() returns an 8-bit string using the system's local
 
372
       encoding.
 
373
    \endlist
 
374
 
 
375
    To convert from one of these encodings, QString provides
 
376
    fromAscii(), fromLatin1(), fromUtf8(), and fromLocal8Bit(). Other
 
377
    encodings are supported through QTextCodec.
 
378
 
 
379
    As mentioned above, QString provides a lot of functions and
 
380
    operators that make it easy to interoperate with \c{const char *}
 
381
    strings. This functionaly is a two-edged sword: It makes QString
 
382
    more convenient to use if all strings are ASCII or Latin-1, but
 
383
    there is always the risk that an implicit conversion from or to
 
384
    \c{const char *} is done using the wrong 8-bit encoding. To
 
385
    minimize these risks, you can turn off these implicit conversions
 
386
    by defining these two preprocessor symbols:
 
387
 
 
388
    \list
 
389
    \i \c QT_NO_CAST_FROM_ASCII disables automatic conversions from
 
390
       ASCII to Unicode.
 
391
    \i \c QT_NO_CAST_TO_ASCII disables automatic conversion from QString
 
392
       to ASCII.
 
393
    \endlist
 
394
 
 
395
    One way to define these prepocessor symbols globally for your
 
396
    application is to add the following entry to your
 
397
    \l{qmake Project Files}{qmake project file}:
 
398
 
 
399
    \code
 
400
        DEFINES += QT_NO_CAST_FROM_ASCII \
 
401
                   QT_NO_CAST_TO_ASCII
 
402
    \endcode
 
403
 
 
404
    You then need to explicitly call fromAscii(), fromLatin1(),
 
405
    fromUtf8(), or fromLocal8Bit() to construct a QString from an
 
406
    8-bit string, or use the lightweight QLatin1String class, for
 
407
    example:
 
408
 
 
409
    \code
 
410
        QString url = QLatin1String("http://www.unicode.org/");
 
411
    \endcode
 
412
 
 
413
    Similarly, you must call toAscii(), toLatin1(), toUtf8(), or
 
414
    toLocal8Bit() explicitly to convert the QString to an 8-bit string.
 
415
    (Other encodings are supported through QTextCodec.)
 
416
 
 
417
    \section1 Note for C programmers
 
418
 
 
419
    Due to C++'s type system and the fact that QString is
 
420
    \l{implicitly shared}, QStrings may be treated like \c{int}s or
 
421
    other basic types. For example:
 
422
 
 
423
    \code
 
424
    QString boolToString(bool b)
 
425
    {
 
426
        QString result;
 
427
        if (b)
 
428
            result = "True";
 
429
        else
 
430
            result = "False";
 
431
        return result;
 
432
    }
 
433
    \endcode
 
434
 
 
435
    The variable, result, is a normal variable allocated on the
 
436
    stack. When return is called, because we're returning by value,
 
437
    The copy constructor is called and a copy of the string is
 
438
    returned. (No actual copying takes place thanks to the implicit
 
439
    sharing.)
 
440
 
 
441
    \section1 Distinction between null and empty strings
 
442
 
 
443
    For historical reasons, QString distinguishes between a null
 
444
    string and an empty string. A \e null string is a string that is
 
445
    initialized using QString's default constructor or by passing
 
446
    (const char *)0 to the constructor. An \e empty string is any
 
447
    string with size 0. A null string is always empty, but an empty
 
448
    string isn't necessarily null:
 
449
 
 
450
    \code
 
451
        QString().isNull();             // returns true
 
452
        QString().isEmpty();            // returns true
 
453
 
 
454
        QString("").isNull();           // returns false
 
455
        QString("").isEmpty();          // returns true
 
456
 
 
457
        QString("abc").isNull();        // returns false
 
458
        QString("abc").isEmpty();       // returns false
 
459
    \endcode
 
460
 
 
461
    All functions except isNull() treat null strings the same as
 
462
    empty strings. For example, toAscii().constData() returns a
 
463
    pointer to a '\\0' character for a null string (\e not a null pointer),
 
464
    and QString() compares equal to QString(""). We recommend that you
 
465
    always use isEmpty() and avoid isNull().
 
466
 
 
467
    \sa fromRawData(), QChar, QLatin1String, QByteArray
 
468
*/
 
469
 
 
470
/*!
 
471
    \enum QString::SplitBehavior
 
472
 
 
473
    \value KeepEmptyParts
 
474
    \value SkipEmptyParts
 
475
*/
 
476
 
 
477
QString::Data QString::shared_null = { Q_ATOMIC_INIT(1), 0, 0, shared_null.array, 0, 0, 0, 0, 0, {0} };
 
478
QString::Data QString::shared_empty = { Q_ATOMIC_INIT(1), 0, 0, shared_empty.array, 0, 0, 0, 0, 0, {0} };
 
479
 
 
480
inline int QString::grow(int size)
 
481
{
 
482
    return qAllocMore(size * sizeof(QChar), sizeof(Data)) / sizeof(QChar);
 
483
}
 
484
 
 
485
/*! \typedef QString::ConstIterator
 
486
 
 
487
    \internal
 
488
 
 
489
    Qt-style synonym for QString::const_iterator.
 
490
*/
 
491
 
 
492
/*! \typedef QString::Iterator
 
493
 
 
494
    \internal
 
495
 
 
496
    Qt-style synonym for QString::iterator.
 
497
*/
 
498
 
 
499
/*! \typedef QString::const_iterator
 
500
 
 
501
    \internal
 
502
 
 
503
    The QString::const_iterator typedef provides an STL-style const
 
504
    iterator for QString.
 
505
 
 
506
    \sa QString::iterator
 
507
*/
 
508
 
 
509
/*! \typedef QString::iterator
 
510
 
 
511
    \internal
 
512
 
 
513
    The QString::iterator typedef provides an STL-style non-const
 
514
    iterator for QString.
 
515
 
 
516
    \sa QString::const_iterator
 
517
*/
 
518
 
 
519
/*! \fn QString::iterator QString::begin()
 
520
 
 
521
    \internal
 
522
*/
 
523
 
 
524
/*! \fn QString::const_iterator QString::begin() const
 
525
 
 
526
    \internal
 
527
*/
 
528
 
 
529
/*! \fn QString::const_iterator QString::constBegin() const
 
530
 
 
531
    \internal
 
532
*/
 
533
 
 
534
/*! \fn QString::iterator QString::end()
 
535
 
 
536
    \internal
 
537
*/
 
538
 
 
539
/*! \fn QString::const_iterator QString::end() const
 
540
 
 
541
    \internal
 
542
*/
 
543
 
 
544
/*! \fn QString::const_iterator QString::constEnd() const
 
545
 
 
546
    \internal
 
547
*/
 
548
 
 
549
/*!
 
550
    \fn QString::QString()
 
551
 
 
552
    Constructs a null string. Null strings are also empty.
 
553
 
 
554
    \sa isEmpty()
 
555
*/
 
556
 
 
557
/*! \fn QString::QString(const char *str)
 
558
 
 
559
    Constructs a string initialized with the ASCII string \a str. \a
 
560
    str is converted to Unicode using fromAscii().
 
561
 
 
562
    You can disable this constructor by defining \c
 
563
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
564
    can be useful if you want to ensure that all user-visible strings
 
565
    go through QObject::tr(), for example.
 
566
 
 
567
    \sa fromAscii(), fromLatin1(), fromLocal8Bit(), fromUtf8()
 
568
*/
 
569
 
 
570
/*! \fn QString QString::fromStdString(const std::string &str)
 
571
 
 
572
    Returns a copy of \a str. \a str is converted to Unicode using
 
573
    fromAscii().
 
574
 
 
575
    This constructor is only available if Qt is configured with STL
 
576
    compabitility enabled.
 
577
 
 
578
    \sa  fromAscii(), fromLatin1(), fromLocal8Bit(), fromUtf8()
 
579
*/
 
580
 
 
581
#ifndef QT_NO_STL
 
582
/*! \fn QString QString::fromStdWString(const std::wstring &str)
 
583
 
 
584
    Returns a copy of \a str. \a str is assumed to be encoded in
 
585
    utf16 if the size of wchar_t is 2 bytes (e.g. on windows) and ucs4
 
586
    if the size of wchar_t is 4 bytes (most Unix systems).
 
587
 
 
588
    This constructor is only available if Qt is configured with STL
 
589
    compabitility enabled.
 
590
 
 
591
    \sa fromUtf16(), fromLatin1(), fromLocal8Bit(), fromUtf8()
 
592
*/
 
593
 
 
594
/*! \internal
 
595
*/
 
596
QString QString::fromWCharArray(const wchar_t *a, int l)
 
597
{
 
598
    QString s;
 
599
    if (sizeof(wchar_t) == sizeof(QChar)) {
 
600
        s = fromUtf16((ushort *)a);
 
601
    } else {
 
602
        s.resize(l*2); // worst case
 
603
        QChar *uc = s.data();
 
604
        for (int i = 0; i < l; ++i) {
 
605
            uint u = a[i];
 
606
            if (u > 0xffff) {
 
607
                // decompose into a surrogate pair
 
608
                u -= 0x10000;
 
609
                *uc = QChar(u/0x400 + 0xd800);
 
610
                ++uc;
 
611
                u = u%0x400 + 0xdc00;
 
612
            }
 
613
            *uc = QChar(u);
 
614
            ++uc;
 
615
        }
 
616
        s.resize(uc - s.data());
 
617
    }
 
618
    return s;
 
619
}
 
620
 
 
621
/*! \fn std::wstring QString::toStdWString() const
 
622
 
 
623
    Returns a std::wstring object with the data contained in this
 
624
    QString. The std::wstring is encoded in utf16 on platforms where
 
625
    wchar_t is 2 bytes wide (e.g. windows) and in ucs4 on platforms
 
626
    where wchar_t is 4 bytes wide (most Unix systems).
 
627
 
 
628
    This operator is mostly useful to pass a QString to a function
 
629
    that accepts a std::wstring object.
 
630
 
 
631
    This operator is only available if Qt is configured with STL
 
632
    compabitility enabled.
 
633
 
 
634
    \sa utf16(), toAscii(), toLatin1(), toUtf8(), toLocal8Bit()
 
635
*/
 
636
 
 
637
/*! \internal
 
638
*/
 
639
int QString::toWCharArray(wchar_t *array) const
 
640
{
 
641
    if (sizeof(wchar_t) == sizeof(QChar)) {
 
642
        memcpy(array, utf16(), sizeof(wchar_t)*length());
 
643
        return length();
 
644
    } else {
 
645
        wchar_t *a = array;
 
646
        const unsigned short *uc = utf16();
 
647
        for (int i = 0; i < length(); ++i) {
 
648
            uint u = uc[i];
 
649
            if (u >= 0xd800 && u < 0xdc00 && i < length()-1) {
 
650
                ushort low = uc[i+1];
 
651
                if (low >= 0xdc00 && low < 0xe000) {
 
652
                    ++i;
 
653
                    u = (u - 0xd800)*0x400 + (low - 0xdc00) + 0x10000;
 
654
                }
 
655
            }
 
656
            *a = wchar_t(u);
 
657
            ++a;
 
658
        }
 
659
        return a - array;
 
660
    }
 
661
}
 
662
#endif
 
663
 
 
664
/*! \fn QString::QString(const QString &other)
 
665
 
 
666
    Constructs a copy of \a other.
 
667
 
 
668
    This operation takes \l{constant time}, because QString is
 
669
    \l{implicitly shared}. This makes returning a QString from a
 
670
    function very fast. If a shared instance is modified, it will be
 
671
    copied (copy-on-write), and that takes \l{linear time}.
 
672
 
 
673
    \sa operator=()
 
674
*/
 
675
 
 
676
/*!
 
677
    Constructs a string initialized with the first \a size characters
 
678
    of the QChar array \a unicode.
 
679
 
 
680
    QString makes a deep copy of the string data.
 
681
*/
 
682
QString::QString(const QChar *unicode, int size)
 
683
{
 
684
   if (!unicode) {
 
685
        d = &shared_null;
 
686
        d->ref.ref();
 
687
    } else if (size <= 0) {
 
688
        d = &shared_empty;
 
689
        d->ref.ref();
 
690
    } else {
 
691
        d = (Data*) qMalloc(sizeof(Data)+size*sizeof(QChar));
 
692
        d->ref.init(1);
 
693
        d->alloc = d->size = size;
 
694
        d->clean = d->asciiCache = d->simpletext = d->righttoleft = 0;
 
695
        d->data = d->array;
 
696
        memcpy(d->array, unicode, size * sizeof(QChar));
 
697
        d->array[size] = '\0';
 
698
    }
 
699
}
 
700
 
 
701
 
 
702
/*!
 
703
    Constructs a string of size \a size with every character set to
 
704
    \a ch.
 
705
 
 
706
    \sa fill()
 
707
*/
 
708
QString::QString(int size, QChar ch)
 
709
{
 
710
   if (size <= 0) {
 
711
        d = &shared_empty;
 
712
        d->ref.ref();
 
713
    } else {
 
714
        d = (Data*) qMalloc(sizeof(Data)+size*sizeof(QChar));
 
715
        d->ref.init(1);
 
716
        d->alloc = d->size = size;
 
717
        d->clean = d->asciiCache = d->simpletext = d->righttoleft = 0;
 
718
        d->data = d->array;
 
719
        d->array[size] = '\0';
 
720
        QChar *i = (QChar*)d->array + size;
 
721
        QChar *b = (QChar*)d->array;
 
722
        while (i != b)
 
723
           *--i = ch;
 
724
    }
 
725
}
 
726
 
 
727
/*! \fn QString::QString(const QLatin1String &str)
 
728
 
 
729
    Constructs a copy of the Latin-1 string \a str.
 
730
 
 
731
    \sa fromLatin1()
 
732
*/
 
733
 
 
734
/*!
 
735
    Constructs a string of size 1 containing the character \a ch.
 
736
*/
 
737
QString::QString(QChar ch)
 
738
{
 
739
    d = (Data *)qMalloc(sizeof(Data) + sizeof(QChar));
 
740
    d->ref.init(1);
 
741
    d->alloc = d->size = 1;
 
742
    d->clean = d->asciiCache = d->simpletext = d->righttoleft = 0;
 
743
    d->data = d->array;
 
744
    d->array[0] = ch.unicode();
 
745
    d->array[1] = '\0';
 
746
}
 
747
 
 
748
/*! \fn QString::QString(const QByteArray &ba)
 
749
 
 
750
    Constructs a string initialized with the byte array \a ba. \a ba
 
751
    is converted to Unicode using fromAscii().
 
752
 
 
753
    You can disable this constructor by defining \c
 
754
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
755
    can be useful if you want to ensure that all user-visible strings
 
756
    go through QObject::tr(), for example.
 
757
*/
 
758
 
 
759
/*! \fn QString::QString(const Null &)
 
760
    \internal
 
761
*/
 
762
 
 
763
/*! \fn QString &QString::operator=(const Null &)
 
764
    \internal
 
765
*/
 
766
 
 
767
/*!
 
768
  \fn QString::~QString()
 
769
 
 
770
    Destroys the string.
 
771
*/
 
772
 
 
773
 
 
774
/*! \fn void QString::detach()
 
775
 
 
776
    \internal
 
777
*/
 
778
 
 
779
/*! \fn void QString::isDetached() const
 
780
 
 
781
    \internal
 
782
*/
 
783
 
 
784
void QString::free(Data *d)
 
785
{
 
786
#ifdef QT3_SUPPORT
 
787
    if (d->asciiCache) {
 
788
        Q_ASSERT(asciiCache);
 
789
        asciiCache->remove(d);
 
790
    }
 
791
#endif
 
792
    qFree(d);
 
793
}
 
794
 
 
795
/*!
 
796
    Sets the size of the string to \a size characters.
 
797
 
 
798
    If \a size is greater than the current size, the string is
 
799
    extended to make it \a size characters long with the extra
 
800
    characters added to the end. The new characters are uninitialized.
 
801
 
 
802
    If \a size is less than the current size, characters are removed
 
803
    from the end.
 
804
 
 
805
    Example:
 
806
    \code
 
807
        QString str = "Hello world";
 
808
        str.resize(5);
 
809
        // str == "Hello"
 
810
 
 
811
        str.resize(8);
 
812
        // str == "Hello???" (where ? stands for any character)
 
813
    \endcode
 
814
 
 
815
    If you want to append a certain number of identical characters to
 
816
    the string, use operator+=() as follows rather than resize():
 
817
 
 
818
    \code
 
819
        QString str = "Hello";
 
820
        str += QString(10, 'X');
 
821
        // str == "HelloXXXXXXXXXX"
 
822
    \endcode
 
823
 
 
824
    If you want to expand the string so that it reaches a certain
 
825
    width and fill the new positions with a particular character, use
 
826
    leftJustified():
 
827
 
 
828
    \code
 
829
        QString str = "Hello";
 
830
        str = str.leftJustified(10, ' ');
 
831
        // str == "Hello     "
 
832
    \endcode
 
833
 
 
834
    \sa truncate(), reserve()
 
835
*/
 
836
 
 
837
void QString::resize(int size)
 
838
{
 
839
    if (size <= 0) {
 
840
        Data *x = &shared_empty;
 
841
        x->ref.ref();
 
842
        x = qAtomicSetPtr(&d, x);
 
843
        if (!x->ref.deref())
 
844
            free(x);
 
845
    } else {
 
846
        if (d->ref != 1 || size > d->alloc || (size < d->size && size < d->alloc >> 1))
 
847
            realloc(grow(size));
 
848
        if (d->alloc >= size) {
 
849
            d->size = size;
 
850
            d->data = d->array;
 
851
            d->array[size] = '\0';
 
852
        }
 
853
    }
 
854
}
 
855
 
 
856
/*! \fn int QString::capacity() const
 
857
 
 
858
    Returns the maximum number of characters that can be stored in
 
859
    the string without forcing a reallocation.
 
860
 
 
861
    The sole purpose of this function is to provide a means of fine
 
862
    tuning QString's memory usage. In general, you will rarely ever
 
863
    need to call this function. If you want to know how many
 
864
    characters are in the string, call size().
 
865
 
 
866
    \sa reserve(), squeeze()
 
867
*/
 
868
 
 
869
/*!
 
870
    \fn void QString::reserve(int size)
 
871
 
 
872
    Attempts to allocate memory for at least \a size characters. If
 
873
    you know in advance how large the string will be, you can call
 
874
    this function, and if you resize the string often you are likely
 
875
    to get better performance. If \a size is an underestimate, the
 
876
    worst that will happen is that the QString will be a bit slower.
 
877
 
 
878
    The sole purpose of this function is to provide a means of fine
 
879
    tuning QString's memory usage. In general, you will rarely ever
 
880
    need to call this function. If you want to change the size of the
 
881
    string, call resize().
 
882
 
 
883
    This function is useful for code that needs to build up a long
 
884
    string and wants to avoid repeated reallocation. In this example,
 
885
    we want to add to the string until some condition is true, and
 
886
    we're fairly sure that size is large enough to make a call to
 
887
    reserve() worthwhile:
 
888
 
 
889
    \code
 
890
        QString result;
 
891
        int len = 0;
 
892
        result.reserve(maxSize);
 
893
        while (...) {
 
894
            result[len++] = getNextChar(); // fill part of the space
 
895
        }
 
896
        result.squeeze();
 
897
    \endcode
 
898
 
 
899
    \sa squeeze(), capacity()
 
900
*/
 
901
 
 
902
/*!
 
903
    \fn void QString::squeeze()
 
904
 
 
905
    Releases any memory not required to store the character data.
 
906
 
 
907
    The sole purpose of this function is to provide a means of fine
 
908
    tuning QString's memory usage. In general, you will rarely ever
 
909
    need to call this function.
 
910
 
 
911
    \sa reserve(), capacity()
 
912
*/
 
913
 
 
914
void QString::realloc(int alloc)
 
915
{
 
916
    if (d->ref != 1 || d->data != d->array) {
 
917
        Data *x = static_cast<Data *>(qMalloc(sizeof(Data) + alloc * sizeof(QChar)));
 
918
        if (!x)
 
919
            return;
 
920
        x->size = qMin(alloc, d->size);
 
921
        ::memcpy(x->array, d->data, x->size * sizeof(QChar));
 
922
        x->array[x->size] = 0;
 
923
        x->asciiCache = 0;
 
924
        x->ref.init(1);
 
925
        x->alloc = alloc;
 
926
        x->clean = d->clean;
 
927
        x->simpletext = d->simpletext;
 
928
        x->righttoleft = d->righttoleft;
 
929
        x->data = x->array;
 
930
        x = qAtomicSetPtr(&d, x);
 
931
        if (!x->ref.deref())
 
932
            free(x);
 
933
    } else {
 
934
#ifdef QT3_SUPPORT
 
935
        if (d->asciiCache) {
 
936
            Q_ASSERT(asciiCache);
 
937
            asciiCache->remove(d);
 
938
        }
 
939
#endif
 
940
        Data *x = static_cast<Data *>(qRealloc(d, sizeof(Data) + alloc * sizeof(QChar)));
 
941
        if (!x)
 
942
            return;
 
943
        x->alloc = alloc;
 
944
        x->data = x->array;
 
945
        d = x;
 
946
    }
 
947
}
 
948
 
 
949
void QString::realloc()
 
950
{
 
951
    realloc(d->size);
 
952
}
 
953
 
 
954
void QString::expand(int i)
 
955
{
 
956
    int sz = d->size;
 
957
    resize(qMax(i + 1, sz));
 
958
    if (d->size - 1 > sz) {
 
959
        ushort *n = d->data + d->size - 1;
 
960
        ushort *e = d->data + sz;
 
961
        while (n != e)
 
962
           * --n = ' ';
 
963
    }
 
964
}
 
965
 
 
966
/*! \fn void QString::clear()
 
967
 
 
968
    Clears the contents of the string and makes it empty.
 
969
 
 
970
    \sa resize(), isEmpty()
 
971
*/
 
972
 
 
973
/*! \fn QString &QString::operator=(const QString &other)
 
974
 
 
975
    Assigns \a other to this string and returns a reference to this
 
976
    string.
 
977
*/
 
978
 
 
979
QString &QString::operator=(const QString &other)
 
980
{
 
981
    Data *x = other.d;
 
982
    x->ref.ref();
 
983
    x = qAtomicSetPtr(&d, x);
 
984
    if (!x->ref.deref())
 
985
        free(x);
 
986
    return *this;
 
987
}
 
988
 
 
989
 
 
990
/*! \fn QString &QString::operator=(const QLatin1String &str)
 
991
 
 
992
    \overload
 
993
 
 
994
    Assigns the Latin-1 string \a str to this string.
 
995
*/
 
996
 
 
997
/*! \fn QString &QString::operator=(const QByteArray &ba)
 
998
 
 
999
    \overload
 
1000
 
 
1001
    Assigns \a ba to this string. \a ba is converted to Unicode using
 
1002
    fromAscii().
 
1003
 
 
1004
    You can disable this operator by defining \c
 
1005
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
1006
    can be useful if you want to ensure that all user-visible strings
 
1007
    go through QObject::tr(), for exaple.
 
1008
*/
 
1009
 
 
1010
/*! \fn QString &QString::operator=(const char *str)
 
1011
 
 
1012
    \overload
 
1013
 
 
1014
    Assigns \a str to this string. \a str is converted to Unicode
 
1015
    using fromAscii().
 
1016
 
 
1017
    You can disable this operator by defining \c
 
1018
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
1019
    can be useful if you want to ensure that all user-visible strings
 
1020
    go through QObject::tr(), for example.
 
1021
*/
 
1022
 
 
1023
/*! \fn QString &QString::operator=(char ch)
 
1024
 
 
1025
    \overload
 
1026
 
 
1027
    Assigns character \a ch to this string. The character is converted
 
1028
    to Unicode using fromAscii().
 
1029
 
 
1030
    You can disable this operator by defining \c
 
1031
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
1032
    can be useful if you want to ensure that all user-visible strings
 
1033
    go through QObject::tr(), for example.
 
1034
*/
 
1035
 
 
1036
/*!
 
1037
    \overload
 
1038
 
 
1039
    Sets the string to contain the single character \a ch.
 
1040
*/
 
1041
QString &QString::operator=(QChar ch)
 
1042
{
 
1043
    return operator=(QString(ch));
 
1044
}
 
1045
 
 
1046
/*! \fn QString& QString::insert(int i, const QString &str)
 
1047
 
 
1048
    Inserts the string \a str at index position \a i and returns a
 
1049
    reference to this string.
 
1050
 
 
1051
    Example:
 
1052
    \code
 
1053
        QString str = "Meal";
 
1054
        str.insert(1, QString("ontr"));
 
1055
        // str == "Montreal"
 
1056
    \endcode
 
1057
 
 
1058
    If \a i is greater than size(), the array is first extended using
 
1059
    resize().
 
1060
 
 
1061
    \sa append(), prepend(), replace(), remove()
 
1062
*/
 
1063
 
 
1064
 
 
1065
/*! \overload
 
1066
 
 
1067
    Inserts the Latin-1 string \a str at index position \a i.
 
1068
*/
 
1069
QString &QString::insert(int i, const QLatin1String &str)
 
1070
{
 
1071
    const uchar *s = (const uchar *)str.latin1();
 
1072
    if (i < 0 || !s || !(*s))
 
1073
        return *this;
 
1074
 
 
1075
    int len = qstrlen(str.latin1());
 
1076
    expand(qMax(d->size, i) + len - 1);
 
1077
 
 
1078
    ::memmove(d->data + i + len, d->data + i, (d->size - i - len) * sizeof(QChar));
 
1079
    for (int j = 0; j < len; ++j)
 
1080
        d->data[i + j] = s[j];
 
1081
    return *this;
 
1082
}
 
1083
 
 
1084
/*! \overload
 
1085
 
 
1086
    Inserts the first \a size characters of the QChar array \a
 
1087
    unicode at index position \a i in the string.
 
1088
*/
 
1089
QString& QString::insert(int i, const QChar *unicode, int size)
 
1090
{
 
1091
    if (i < 0 || size <= 0)
 
1092
        return *this;
 
1093
 
 
1094
    const ushort *s = (const ushort *)unicode;
 
1095
    if (s >= d->data && s < d->data + d->alloc) {
 
1096
        // Part of me - take a copy
 
1097
        ushort *tmp = static_cast<ushort *>(malloc(size * sizeof(QChar)));
 
1098
        memcpy(tmp, s, size * sizeof(QChar));
 
1099
        insert(i, reinterpret_cast<const QChar *>(tmp), size);
 
1100
        ::free(tmp);
 
1101
        return *this;
 
1102
    }
 
1103
 
 
1104
    expand(qMax(d->size, i) + size - 1);
 
1105
 
 
1106
    ::memmove(d->data + i + size, d->data + i, (d->size - i - size) * sizeof(QChar));
 
1107
    memcpy(d->data + i, s, size * sizeof(QChar));
 
1108
    return *this;
 
1109
}
 
1110
 
 
1111
/*! \overload
 
1112
 
 
1113
    Inserts \a ch at index position \a i in the string.
 
1114
*/
 
1115
 
 
1116
QString& QString::insert(int i, QChar ch)
 
1117
{
 
1118
    if (i < 0)
 
1119
        i += d->size;
 
1120
    if (i < 0)
 
1121
        return *this;
 
1122
    expand(qMax(i, d->size));
 
1123
    ::memmove(d->data + i + 1, d->data + i, (d->size - i) * sizeof(QChar));
 
1124
    d->data[i] = ch.unicode();
 
1125
    return *this;
 
1126
}
 
1127
 
 
1128
/*!
 
1129
    Appends the string \a str onto the end of this string.
 
1130
 
 
1131
    Example:
 
1132
    \code
 
1133
        QString x = "free";
 
1134
        QString y = "dom";
 
1135
        x.append(y);
 
1136
        // x == "freedom"
 
1137
    \endcode
 
1138
 
 
1139
    This is the same as insert(size(), \a str).
 
1140
 
 
1141
    This operation is typically very fast (\l{constant time}),
 
1142
    because QString preallocates extra space at the end of the string
 
1143
    data so it can grow without reallocating the entire string each
 
1144
    time.
 
1145
 
 
1146
    \sa operator+=(), prepend(), insert()
 
1147
*/
 
1148
QString &QString::append(const QString &str)
 
1149
{
 
1150
    if (str.d != &shared_null) {
 
1151
        if (d == &shared_null) {
 
1152
            operator=(str);
 
1153
        } else {
 
1154
            if (d->ref != 1 || d->size + str.d->size > d->alloc)
 
1155
                realloc(grow(d->size + str.d->size));
 
1156
            memcpy(d->data + d->size, str.d->data, str.d->size * sizeof(QChar));
 
1157
            d->size += str.d->size;
 
1158
            d->data[d->size] = '\0';
 
1159
        }
 
1160
    }
 
1161
    return *this;
 
1162
}
 
1163
 
 
1164
/*! \overload
 
1165
 
 
1166
    Appends the Latin-1 string \a str to this string.
 
1167
*/
 
1168
QString &QString::append(const QLatin1String &str)
 
1169
{
 
1170
    const uchar *s = (const uchar *)str.latin1();
 
1171
    if (s) {
 
1172
        int len = qstrlen((char *)s);
 
1173
        if (d->ref != 1 || d->size + len > d->alloc)
 
1174
            realloc(grow(d->size + len));
 
1175
        ushort *i = d->data + d->size;
 
1176
        while ((*i++ = *s++))
 
1177
            ;
 
1178
        d->size += len;
 
1179
    }
 
1180
    return *this;
 
1181
}
 
1182
 
 
1183
/*! \fn QString &QString::append(const QByteArray &ba)
 
1184
 
 
1185
    \overload
 
1186
 
 
1187
    Appends the byte array \a ba to this string. \a ba is converted
 
1188
    to Unicode using fromAscii().
 
1189
 
 
1190
    You can disable this function by defining \c
 
1191
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
1192
    can be useful if you want to ensure that all user-visible strings
 
1193
    go through QObject::tr(), for example.
 
1194
*/
 
1195
 
 
1196
/*! \fn QString &QString::append(const char *str)
 
1197
 
 
1198
    \overload
 
1199
 
 
1200
    Appends the string \a str to this string. \a str is converted to
 
1201
    Unicode using fromAscii().
 
1202
 
 
1203
    You can disable this function by defining \c
 
1204
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
1205
    can be useful if you want to ensure that all user-visible strings
 
1206
    go through QObject::tr(), for example.
 
1207
*/
 
1208
 
 
1209
/*!
 
1210
    \overload
 
1211
 
 
1212
    Appends the character \a ch to this string.
 
1213
*/
 
1214
QString &QString::append(QChar ch)
 
1215
{
 
1216
    if (d->ref != 1 || d->size + 1 > d->alloc)
 
1217
        realloc(grow(d->size + 1));
 
1218
    d->data[d->size++] = ch.unicode();
 
1219
    d->data[d->size] = '\0';
 
1220
    return *this;
 
1221
}
 
1222
 
 
1223
/*! \fn QString &QString::prepend(const QString &str)
 
1224
 
 
1225
    Prepends the string \a str to the beginning of this string and
 
1226
    returns a reference to this string.
 
1227
 
 
1228
    Example:
 
1229
    \code
 
1230
        QString x = "ship";
 
1231
        QString y = "air";
 
1232
        x.prepend(y);
 
1233
        // x == "airship"
 
1234
    \endcode
 
1235
 
 
1236
    \sa append(), insert()
 
1237
*/
 
1238
 
 
1239
/*! \fn QString &QString::prepend(const QLatin1String &str)
 
1240
 
 
1241
    \overload
 
1242
 
 
1243
    Prepends the Latin-1 string \a str to this string.
 
1244
*/
 
1245
 
 
1246
/*! \fn QString &QString::prepend(const QByteArray &ba)
 
1247
 
 
1248
    \overload
 
1249
 
 
1250
    Prepends the byte array \a ba to this string. \a ba is converted
 
1251
    to Unicode using fromAscii().
 
1252
 
 
1253
    You can disable this function by defining \c
 
1254
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
1255
    can be useful if you want to ensure that all user-visible strings
 
1256
    go through QObject::tr(), for example.
 
1257
*/
 
1258
 
 
1259
/*! \fn QString &QString::prepend(const char *str)
 
1260
 
 
1261
    \overload
 
1262
 
 
1263
    Prepends the string \a str to this string. \a str is converted to
 
1264
    Unicode using fromAscii().
 
1265
 
 
1266
    You can disable this function by defining \c
 
1267
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
1268
    can be useful if you want to ensure that all user-visible strings
 
1269
    go through QObject::tr(), for example.
 
1270
*/
 
1271
 
 
1272
/*! \fn QString &QString::prepend(QChar ch)
 
1273
 
 
1274
    \overload
 
1275
 
 
1276
    Prepends the character \a ch to this string.
 
1277
*/
 
1278
 
 
1279
/*!
 
1280
    Removes \a len characters from the string, starting at index
 
1281
    position \a pos, and returns a reference to the string.
 
1282
 
 
1283
    If \a pos is within the string, but \a pos + \a len is beyond the
 
1284
    end of the string, the string is truncated at position \a pos.
 
1285
 
 
1286
    \code
 
1287
        QString str = "Montreal";
 
1288
        str.remove(1, 4);
 
1289
        // str == "Meal"
 
1290
    \endcode
 
1291
 
 
1292
    \sa insert(), replace()
 
1293
*/
 
1294
 
 
1295
QString &QString::remove(int pos, int len)
 
1296
{
 
1297
    if (pos < 0)
 
1298
        pos += d->size;
 
1299
    if (pos < 0 || pos >= d->size) {
 
1300
        // range problems
 
1301
    } else if (pos + len >= d->size) {  // pos ok
 
1302
        resize(pos);
 
1303
    } else if (len > 0) {
 
1304
        detach();
 
1305
        memmove(d->data + pos, d->data + pos + len,
 
1306
                (d->size - pos - len + 1) * sizeof(ushort));
 
1307
        d->size -= len;
 
1308
    }
 
1309
    return *this;
 
1310
}
 
1311
 
 
1312
/*! \overload
 
1313
 
 
1314
    Removes every occurrence of \a str in this string. Returns a
 
1315
    reference to this string.
 
1316
 
 
1317
    If \a cs is Qt::CaseSensitive (the default), the search is
 
1318
    case sensitive; otherwise the search is case insensitive.
 
1319
 
 
1320
    This is the same as replace(\a str, "", \a cs).
 
1321
*/
 
1322
QString &QString::remove(const QString &str, Qt::CaseSensitivity cs)
 
1323
{
 
1324
    if (str.d->size) {
 
1325
        int i = 0;
 
1326
        while ((i = indexOf(str, i, cs)) != -1)
 
1327
            remove(i, str.d->size);
 
1328
    }
 
1329
    return *this;
 
1330
}
 
1331
 
 
1332
/*! \overload
 
1333
 
 
1334
    Removes every occurrence of the character \a ch in this string,
 
1335
    and returns a reference to this string.
 
1336
 
 
1337
    If \a cs is Qt::CaseSensitive (the default), the search is
 
1338
    case sensitive; otherwise the search is case insensitive.
 
1339
 
 
1340
    Example:
 
1341
    \code
 
1342
        QString str = "Ali Baba";
 
1343
        str.remove(QChar('a'), Qt::CaseInsensitive);
 
1344
        // str == "li Bb"
 
1345
    \endcode
 
1346
 
 
1347
    This is the same as replace(\a ch, "", \a cs).
 
1348
*/
 
1349
QString &QString::remove(QChar ch, Qt::CaseSensitivity cs)
 
1350
{
 
1351
    int i = 0;
 
1352
    if (cs == Qt::CaseSensitive) {
 
1353
        while (i < d->size)
 
1354
            if (*((const QChar*)d->data + i) == ch)
 
1355
                remove(i, 1);
 
1356
            else
 
1357
                i++;
 
1358
    } else {
 
1359
        ch = ::lower(ch);
 
1360
        while (i < d->size)
 
1361
            if (::lower(*((const QChar*)d->data + i)) == ch)
 
1362
                remove(i, 1);
 
1363
            else
 
1364
                i++;
 
1365
    }
 
1366
    return *this;
 
1367
}
 
1368
 
 
1369
/*! \fn QString &QString::remove(const QRegExp &rx)
 
1370
 
 
1371
    \overload
 
1372
 
 
1373
    Removes every occurrence of the regular expression \a rx in the
 
1374
    string, and returns a reference to the string. For example:
 
1375
 
 
1376
    \code
 
1377
        QString str = "Telephone";
 
1378
        str.remove(QRegExp("[aeiou]."));
 
1379
        // str == "The"
 
1380
    \endcode
 
1381
 
 
1382
    \sa indexOf(), lastIndexOf(), replace()
 
1383
*/
 
1384
 
 
1385
/*!
 
1386
    Replaces \a len characters from index position \a pos with the
 
1387
    string \a after, and returns a reference to this string.
 
1388
 
 
1389
    Example:
 
1390
    \code
 
1391
        QString x = "Say yes!";
 
1392
        QString y = "no";
 
1393
        x.replace(4, 3, y);
 
1394
        // x == "Say no!"
 
1395
    \endcode
 
1396
 
 
1397
    \sa insert(), remove()
 
1398
*/
 
1399
 
 
1400
QString &QString::replace(int pos, int len, const QString &after)
 
1401
{
 
1402
    QString copy = after;
 
1403
    remove(pos, len);
 
1404
    return insert(pos, copy.constData(), copy.d->size);
 
1405
}
 
1406
 
 
1407
/*! \overload
 
1408
 
 
1409
    Replaces \a len characters from index position \a pos with the
 
1410
    first \a size characters of the QChar array \a unicode.
 
1411
*/
 
1412
QString &QString::replace(int pos, int len, const QChar *unicode, int size)
 
1413
{
 
1414
    remove(pos, len);
 
1415
    return insert(pos, unicode, size);
 
1416
}
 
1417
 
 
1418
/*!
 
1419
    \overload
 
1420
 
 
1421
    Replaces \a len characters from index position \a pos with the
 
1422
    character \a after.
 
1423
*/
 
1424
QString &QString::replace(int pos, int len, QChar after)
 
1425
{
 
1426
    remove(pos, len);
 
1427
    return insert(pos, after);
 
1428
}
 
1429
 
 
1430
/*! \overload
 
1431
 
 
1432
    Replaces every occurrence of the string \a before with the string
 
1433
    \a after.
 
1434
 
 
1435
    If \a cs is Qt::CaseSensitive (the default), the search is
 
1436
    case sensitive; otherwise the search is case insensitive.
 
1437
 
 
1438
    Example:
 
1439
    \code
 
1440
        QString str = "colour behaviour flavour neighbour";
 
1441
        str.replace(QString("ou"), QString("o"));
 
1442
        // str == "color behavior flavor neighbor"
 
1443
    \endcode
 
1444
*/
 
1445
QString &QString::replace(const QString &before, const QString &after, Qt::CaseSensitivity cs)
 
1446
{
 
1447
    if (d->size == 0) {
 
1448
        if (before.d->size)
 
1449
            return *this;
 
1450
    } else {
 
1451
        if (cs == Qt::CaseSensitive && before == after)
 
1452
            return *this;
 
1453
    }
 
1454
    if (d->ref != 1)
 
1455
        realloc(d->size);
 
1456
 
 
1457
    QStringMatcher matcher(before, cs);
 
1458
    int index = 0;
 
1459
    const int bl = before.d->size;
 
1460
    const int al = after.d->size;
 
1461
 
 
1462
    if (bl == al) {
 
1463
        if (bl) {
 
1464
            const QChar *auc = (const QChar*) after.d->data;
 
1465
            while ((index = matcher.indexIn(*this, index)) != -1) {
 
1466
                // we need memmove(), not memcpy(), in the rare case where
 
1467
                // this == &after, before == after, and cs is Qt::CaseInsensitive
 
1468
                memmove(d->data + index, auc, al * sizeof(QChar));
 
1469
                index += bl;
 
1470
            }
 
1471
        }
 
1472
    } else if (al < bl) {
 
1473
        const QChar *auc = after.unicode();
 
1474
        uint to = 0;
 
1475
        uint movestart = 0;
 
1476
        uint num = 0;
 
1477
        while ((index = matcher.indexIn(*this, index)) != -1) {
 
1478
            if (num) {
 
1479
                int msize = index - movestart;
 
1480
                if (msize > 0) {
 
1481
                    memmove(d->data + to, d->data + movestart, msize * sizeof(QChar));
 
1482
                    to += msize;
 
1483
                }
 
1484
            } else {
 
1485
                to = index;
 
1486
            }
 
1487
            if (al) {
 
1488
                memcpy(d->data+to, auc, al*sizeof(QChar));
 
1489
                to += al;
 
1490
            }
 
1491
            index += bl;
 
1492
            movestart = index;
 
1493
            num++;
 
1494
        }
 
1495
        if (num) {
 
1496
            int msize = d->size - movestart;
 
1497
            if (msize > 0)
 
1498
                memmove(d->data + to, d->data + movestart, msize * sizeof(QChar));
 
1499
            resize(d->size - num*(bl-al));
 
1500
        }
 
1501
    } else {
 
1502
        const QString copy = after;
 
1503
 
 
1504
        // the most complex case. We don't want to lose performance by doing repeated
 
1505
        // copies and reallocs of the string.
 
1506
        while (index != -1) {
 
1507
            uint indices[4096];
 
1508
            uint pos = 0;
 
1509
            while (pos < 4095) {
 
1510
                index = matcher.indexIn(*this, index);
 
1511
                if (index == -1)
 
1512
                    break;
 
1513
                indices[pos++] = index;
 
1514
                index += bl;
 
1515
                // avoid infinite loop
 
1516
                if (!bl)
 
1517
                    index++;
 
1518
            }
 
1519
            if (!pos)
 
1520
                break;
 
1521
 
 
1522
            // we have a table of replacement positions, use them for fast replacing
 
1523
            int adjust = pos*(al-bl);
 
1524
            // index has to be adjusted in case we get back into the loop above.
 
1525
            if (index != -1)
 
1526
                index += adjust;
 
1527
            int newLen = d->size + adjust;
 
1528
            int moveend = d->size;
 
1529
            if (newLen > d->size)
 
1530
                resize(newLen);
 
1531
 
 
1532
            while (pos) {
 
1533
                pos--;
 
1534
                int movestart = indices[pos] + bl;
 
1535
                int insertstart = indices[pos] + pos*(al-bl);
 
1536
                int moveto = insertstart + al;
 
1537
                memmove(d->data + moveto, d->data + movestart, (moveend - movestart)*sizeof(QChar));
 
1538
                memcpy(d->data + insertstart, copy.unicode(), al*sizeof(QChar));
 
1539
                moveend = movestart-bl;
 
1540
            }
 
1541
        }
 
1542
    }
 
1543
    return *this;
 
1544
}
 
1545
 
 
1546
/*! \overload
 
1547
 
 
1548
    Replaces every occurrence of the character \a ch in the string
 
1549
    with \a after. Returns a reference to the string.
 
1550
 
 
1551
    If \a cs is Qt::CaseSensitive (the default), the search is
 
1552
    case sensitive; otherwise the search is case insensitive.
 
1553
*/
 
1554
 
 
1555
QString& QString::replace(QChar ch, const QString &after, Qt::CaseSensitivity cs)
 
1556
{
 
1557
    return replace(QString(ch), after, cs);
 
1558
}
 
1559
 
 
1560
/*! \overload
 
1561
 
 
1562
    Replaces every occurrence of the character \a before with the
 
1563
    character \a after. Returns a reference to the string.
 
1564
 
 
1565
    If \a cs is Qt::CaseSensitive (the default), the search is
 
1566
    case sensitive; otherwise the search is case insensitive.
 
1567
*/
 
1568
QString& QString::replace(QChar before, QChar after, Qt::CaseSensitivity cs)
 
1569
{
 
1570
    if (d->size) {
 
1571
        QChar *i = data();
 
1572
        QChar *e = i + d->size;
 
1573
        if (cs == Qt::CaseSensitive) {
 
1574
            for (; i != e; ++i)
 
1575
                if (*i == before)
 
1576
                   * i = after;
 
1577
        } else {
 
1578
            before = ::lower(before);
 
1579
            for (; i != e; ++i)
 
1580
                if (::lower(*i) == before)
 
1581
                   * i = after;
 
1582
        }
 
1583
    }
 
1584
    return *this;
 
1585
}
 
1586
 
 
1587
/*!
 
1588
    Returns true if string \a other is equal to this string;
 
1589
    otherwise returns false.
 
1590
 
 
1591
    The comparison is based exclusively on the numeric Unicode values
 
1592
    of the characters and is very fast, but is not what a human would
 
1593
    expect. Consider sorting user-interface strings with
 
1594
    localeAwareCompare().
 
1595
*/
 
1596
bool QString::operator==(const QString &other) const
 
1597
{
 
1598
    return (size() == other.size()) &&
 
1599
        (memcmp((char*)unicode(),(char*)other.unicode(), size()*sizeof(QChar))==0);
 
1600
}
 
1601
 
 
1602
/*!
 
1603
    \overload
 
1604
*/
 
1605
bool QString::operator==(const QLatin1String &other) const
 
1606
{
 
1607
    const ushort *uc = d->data;
 
1608
    const ushort *e = uc + d->size;
 
1609
    const uchar *c = (uchar *)other.latin1();
 
1610
 
 
1611
    if (!c)
 
1612
        return isEmpty();
 
1613
 
 
1614
    while (*c) {
 
1615
        if (uc == e || *uc != *c)
 
1616
            return false;
 
1617
        ++uc;
 
1618
        ++c;
 
1619
    }
 
1620
    return (uc == e);
 
1621
}
 
1622
 
 
1623
/*! \fn bool QString::operator==(const QByteArray &other) const
 
1624
 
 
1625
    \overload
 
1626
 
 
1627
    \a other is converted to a QString using fromAscii().
 
1628
 
 
1629
    You can disable this operator by defining \c
 
1630
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
1631
    can be useful if you want to ensure that all user-visible strings
 
1632
    go through QObject::tr(), for example.
 
1633
*/
 
1634
 
 
1635
/*! \fn bool QString::operator==(const char *other) const
 
1636
 
 
1637
    \overload
 
1638
 
 
1639
    \a other is converted to a QString using fromAscii().
 
1640
 
 
1641
    You can disable this operator by defining \c
 
1642
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
1643
    can be useful if you want to ensure that all user-visible strings
 
1644
    go through QObject::tr(), for example.
 
1645
*/
 
1646
 
 
1647
/*!
 
1648
    Returns true if this string is lexically less than string \a
 
1649
    other; otherwise returns false.
 
1650
 
 
1651
    The comparison is based exclusively on the numeric Unicode values
 
1652
    of the characters and is very fast, but is not what a human would
 
1653
    expect. Consider sorting user-interface strings with
 
1654
    localeAwareCompare().
 
1655
*/
 
1656
bool QString::operator<(const QString &other) const
 
1657
{
 
1658
    return ucstrcmp(*this, other) < 0;
 
1659
}
 
1660
 
 
1661
/*!
 
1662
    \overload
 
1663
*/
 
1664
bool QString::operator<(const QLatin1String &other) const
 
1665
{
 
1666
    const ushort *uc = d->data;
 
1667
    const ushort *e = uc + d->size;
 
1668
    const uchar *c = (uchar *) other.latin1();
 
1669
 
 
1670
    if (!c || *c == 0)
 
1671
        return false;
 
1672
 
 
1673
    while (*c) {
 
1674
        if (uc == e || *uc != *c)
 
1675
            break;
 
1676
        ++uc;
 
1677
        ++c;
 
1678
    }
 
1679
    return (uc == e ? *c : *uc < *c);
 
1680
}
 
1681
 
 
1682
/*! \fn bool QString::operator<(const QByteArray &other) const
 
1683
 
 
1684
    \overload
 
1685
 
 
1686
    \a other is converted to a QString using fromAscii().
 
1687
 
 
1688
    You can disable this operator by defining \c
 
1689
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
1690
    can be useful if you want to ensure that all user-visible strings
 
1691
    go through QObject::tr(), for example.
 
1692
*/
 
1693
 
 
1694
/*! \fn bool QString::operator<(const char *other) const
 
1695
 
 
1696
    \overload
 
1697
 
 
1698
    \a other is converted to a QString using fromAscii().
 
1699
 
 
1700
    You can disable this operator by defining \c
 
1701
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
1702
    can be useful if you want to ensure that all user-visible strings
 
1703
    go through QObject::tr(), for example.
 
1704
*/
 
1705
 
 
1706
/*! \fn bool QString::operator<=(const QString &other) const
 
1707
 
 
1708
    Returns true if this string is lexically less than or equal to
 
1709
    string \a other; otherwise returns false.
 
1710
 
 
1711
    The comparison is based exclusively on the numeric Unicode values
 
1712
    of the characters and is very fast, but is not what a human would
 
1713
    expect. Consider sorting user-interface strings with
 
1714
    localeAwareCompare().
 
1715
*/
 
1716
 
 
1717
/*! \fn bool QString::operator<=(const QLatin1String &other) const
 
1718
 
 
1719
    \overload
 
1720
*/
 
1721
 
 
1722
/*! \fn bool QString::operator<=(const QByteArray &other) const
 
1723
 
 
1724
    \overload
 
1725
 
 
1726
    \a other is converted to a QString using fromAscii().
 
1727
 
 
1728
    You can disable this operator by defining \c
 
1729
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
1730
    can be useful if you want to ensure that all user-visible strings
 
1731
    go through QObject::tr(), for example.
 
1732
*/
 
1733
 
 
1734
/*! \fn bool QString::operator<=(const char *other) const
 
1735
 
 
1736
    \overload
 
1737
 
 
1738
    \a other is converted to a QString using fromAscii().
 
1739
 
 
1740
    You can disable this operator by defining \c
 
1741
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
1742
    can be useful if you want to ensure that all user-visible strings
 
1743
    go through QObject::tr(), for example.
 
1744
*/
 
1745
 
 
1746
/*! \fn bool QString::operator>(const QString &other) const
 
1747
 
 
1748
    Returns true if this string is lexically greater than string \a
 
1749
    other; otherwise returns false.
 
1750
 
 
1751
    The comparison is based exclusively on the numeric Unicode values
 
1752
    of the characters and is very fast, but is not what a human would
 
1753
    expect. Consider sorting user-interface strings with
 
1754
    localeAwareCompare().
 
1755
*/
 
1756
 
 
1757
/*!
 
1758
    \overload
 
1759
*/
 
1760
bool QString::operator>(const QLatin1String &other) const
 
1761
{
 
1762
    const ushort *uc = d->data;;
 
1763
    const ushort *e = uc + d->size;
 
1764
    const uchar *c = (uchar *) other.latin1();
 
1765
 
 
1766
    if (!c || *c == '\0')
 
1767
        return !isEmpty();
 
1768
 
 
1769
    while (*c) {
 
1770
        if (uc == e || *uc != *c)
 
1771
            break;
 
1772
        ++uc;
 
1773
        ++c;
 
1774
    }
 
1775
    return (uc == e ? false : *uc > *c);
 
1776
}
 
1777
 
 
1778
/*! \fn bool QString::operator>(const QByteArray &other) const
 
1779
 
 
1780
    \overload
 
1781
 
 
1782
    \a other is converted to a QString using fromAscii().
 
1783
 
 
1784
    You can disable this operator by defining \c
 
1785
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
1786
    can be useful if you want to ensure that all user-visible strings
 
1787
    go through QObject::tr(), for example.
 
1788
*/
 
1789
 
 
1790
/*! \fn bool QString::operator>(const char *other) const
 
1791
 
 
1792
    \overload
 
1793
 
 
1794
    \a other is converted to a QString using fromAscii().
 
1795
 
 
1796
    You can disable this operator by defining \c
 
1797
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
1798
    can be useful if you want to ensure that all user-visible strings
 
1799
    go through QObject::tr(), for example.
 
1800
*/
 
1801
 
 
1802
/*! \fn bool QString::operator>=(const QString &other) const
 
1803
 
 
1804
    Returns true if this string is lexically greater than or equal to
 
1805
    string \a other; otherwise returns false.
 
1806
 
 
1807
    The comparison is based exclusively on the numeric Unicode values
 
1808
    of the characters and is very fast, but is not what a human would
 
1809
    expect. Consider sorting user-interface strings with
 
1810
    localeAwareCompare().
 
1811
*/
 
1812
 
 
1813
/*! \fn bool QString::operator>=(const QLatin1String &other) const
 
1814
 
 
1815
    \overload
 
1816
*/
 
1817
 
 
1818
/*! \fn bool QString::operator>=(const QByteArray &other) const
 
1819
 
 
1820
    \overload
 
1821
 
 
1822
    \a other is converted to a QString using fromAscii().
 
1823
 
 
1824
    You can disable this operator by defining \c
 
1825
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
1826
    can be useful if you want to ensure that all user-visible strings
 
1827
    go through QObject::tr(), for example.
 
1828
*/
 
1829
 
 
1830
/*! \fn bool QString::operator>=(const char *other) const
 
1831
 
 
1832
    \overload
 
1833
 
 
1834
    \a other is converted to a QString using fromAscii().
 
1835
 
 
1836
    You can disable this operator by defining \c
 
1837
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
1838
    can be useful if you want to ensure that all user-visible strings
 
1839
    go through QObject::tr(), for example.
 
1840
*/
 
1841
 
 
1842
/*! \fn bool QString::operator!=(const QString &other) const
 
1843
 
 
1844
    Returns true if this string is not equal to string \a other;
 
1845
    otherwise returns false.
 
1846
 
 
1847
    The comparison is based exclusively on the numeric Unicode values
 
1848
    of the characters and is very fast, but is not what a human would
 
1849
    expect. Consider sorting user-interface strings with
 
1850
    localeAwareCompare().
 
1851
*/
 
1852
 
 
1853
/*! \fn bool QString::operator!=(const QLatin1String &other) const
 
1854
 
 
1855
    \overload
 
1856
*/
 
1857
 
 
1858
/*! \fn bool QString::operator!=(const QByteArray &other) const
 
1859
 
 
1860
    \overload
 
1861
 
 
1862
    \a other is converted to a QString using fromAscii().
 
1863
 
 
1864
    You can disable this operator by defining \c
 
1865
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
1866
    can be useful if you want to ensure that all user-visible strings
 
1867
    go through QObject::tr(), for example.
 
1868
*/
 
1869
 
 
1870
/*! \fn bool QString::operator!=(const char *other) const
 
1871
 
 
1872
    \overload
 
1873
 
 
1874
    \a other is converted to a QString using fromAscii().
 
1875
 
 
1876
    You can disable this operator by defining \c
 
1877
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
1878
    can be useful if you want to ensure that all user-visible strings
 
1879
    go through QObject::tr(), for example.
 
1880
*/
 
1881
 
 
1882
#define REHASH(a) \
 
1883
    if (sl_minus_1 < (int)sizeof(int) * CHAR_BIT)       \
 
1884
        hashHaystack -= (a) << sl_minus_1; \
 
1885
    hashHaystack <<= 1
 
1886
 
 
1887
/*!
 
1888
    Returns the index position of the first occurrence of the string
 
1889
    \a str in this string, searching forward from index position \a
 
1890
    from. Returns -1 if \a str is not found.
 
1891
 
 
1892
    If \a cs is Qt::CaseSensitive (the default), the search is
 
1893
    case sensitive; otherwise the search is case insensitive.
 
1894
 
 
1895
    Example:
 
1896
    \code
 
1897
        QString x = "sticky question";
 
1898
        QString y = "sti";
 
1899
        x.indexOf(y);               // returns 0
 
1900
        x.indexOf(y, 1);            // returns 10
 
1901
        x.indexOf(y, 10);           // returns 10
 
1902
        x.indexOf(y, 11);           // returns -1
 
1903
    \endcode
 
1904
 
 
1905
    If \a from is -1, the search starts at the last character; if it
 
1906
    is -2, at the next to last character and so on.
 
1907
 
 
1908
    \sa lastIndexOf(), contains(), count()
 
1909
*/
 
1910
int QString::indexOf(const QString &str, int from, Qt::CaseSensitivity cs) const
 
1911
{
 
1912
    const int l = d->size;
 
1913
    const int sl = str.d->size;
 
1914
    if (from < 0)
 
1915
        from += l;
 
1916
    if (sl + from > l)
 
1917
        return -1;
 
1918
    if (!sl)
 
1919
        return from;
 
1920
    if (!l)
 
1921
        return -1;
 
1922
 
 
1923
    if (sl == 1)
 
1924
        return indexOf(*(const QChar *)str.d->data, from, cs);
 
1925
 
 
1926
    /*
 
1927
        We use the Boyer-Moore algorithm in cases where the overhead
 
1928
        for the skip table should pay off, otherwise we use a simple
 
1929
        hash function.
 
1930
    */
 
1931
    if (l > 500 && sl > 5)
 
1932
        return QStringMatcher(str, cs).indexIn(*this, from);
 
1933
 
 
1934
    /*
 
1935
        We use some hashing for efficiency's sake. Instead of
 
1936
        comparing strings, we compare the hash value of str with that
 
1937
        of a part of this QString. Only if that matches, we call
 
1938
        ucstrncmp() or ucstrnicmp().
 
1939
    */
 
1940
    const QChar *needle = (const QChar*) str.d->data;
 
1941
    const QChar *haystack = (const QChar*) d->data + from;
 
1942
    const QChar *end = (const QChar*) d->data + (l-sl);
 
1943
    const int sl_minus_1 = sl-1;
 
1944
    int hashNeedle = 0, hashHaystack = 0, idx;
 
1945
 
 
1946
    if (cs == Qt::CaseSensitive) {
 
1947
        for (idx = 0; idx < sl; ++idx) {
 
1948
            hashNeedle = ((hashNeedle<<1) + needle[idx].unicode());
 
1949
            hashHaystack = ((hashHaystack<<1) + haystack[idx].unicode());
 
1950
        }
 
1951
        hashHaystack -= (haystack+sl_minus_1)->unicode();
 
1952
 
 
1953
        while (haystack <= end) {
 
1954
            hashHaystack += (haystack+sl_minus_1)->unicode();
 
1955
            if (hashHaystack == hashNeedle
 
1956
                 && ucstrncmp(needle, haystack, sl) == 0)
 
1957
                return haystack-unicode();
 
1958
 
 
1959
            REHASH(haystack->unicode());
 
1960
            ++haystack;
 
1961
        }
 
1962
    } else {
 
1963
        for (idx = 0; idx < sl; ++idx) {
 
1964
            hashNeedle = ((hashNeedle<<1) +
 
1965
                          ::lower(needle[idx].unicode()).unicode());
 
1966
            hashHaystack = ((hashHaystack<<1) +
 
1967
                            ::lower(haystack[idx].unicode()).unicode());
 
1968
        }
 
1969
 
 
1970
        hashHaystack -= ::lower(*(haystack+sl_minus_1)).unicode();
 
1971
        while (haystack <= end) {
 
1972
            hashHaystack += ::lower(*(haystack+sl_minus_1)).unicode();
 
1973
            if (hashHaystack == hashNeedle
 
1974
                 && ucstrnicmp(needle, haystack, sl) == 0)
 
1975
                return haystack-unicode();
 
1976
 
 
1977
            REHASH(::lower(*haystack).unicode());
 
1978
            ++haystack;
 
1979
        }
 
1980
    }
 
1981
    return -1;
 
1982
}
 
1983
 
 
1984
/*!
 
1985
    \overload
 
1986
 
 
1987
    Returns the index position of the first occurrence of the
 
1988
    character \a ch in the string, searching forward from index
 
1989
    position \a from. Returns -1 if \a ch could not be found.
 
1990
*/
 
1991
int QString::indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
 
1992
{
 
1993
    if (from < 0)
 
1994
        from = qMax(from + d->size, 0);
 
1995
    if (from  < d->size) {
 
1996
        const QChar *n = (const QChar*)d->data + from - 1;
 
1997
        const QChar *e = (const QChar*)d->data + d->size;
 
1998
        if (cs == Qt::CaseSensitive) {
 
1999
            while (++n != e)
 
2000
                if (*n == ch)
 
2001
                    return  n - (const QChar*)d->data;
 
2002
        } else {
 
2003
            ch = ::lower(ch);
 
2004
            while (++n != e)
 
2005
                if (::lower(*n) == ch)
 
2006
                    return  n - (const QChar*)d->data;
 
2007
        }
 
2008
    }
 
2009
    return -1;
 
2010
}
 
2011
 
 
2012
/*!
 
2013
    Returns the index position of the last occurrence of the string
 
2014
    \a str in this string, searching backward from index position \a
 
2015
    from. If \a from is -1 (the default), the search starts at the
 
2016
    last character; if \a from is -2, at the next to last character
 
2017
    and so on. Returns -1 if \a str is not found.
 
2018
 
 
2019
    If \a cs is Qt::CaseSensitive (the default), the search is
 
2020
    case sensitive; otherwise the search is case insensitive.
 
2021
 
 
2022
    Example:
 
2023
    \code
 
2024
        QString x = "crazy azimuths";
 
2025
        QString y = "az";
 
2026
        x.lastIndexOf(y);           // returns 6
 
2027
        x.lastIndexOf(y, 6);        // returns 6
 
2028
        x.lastIndexOf(y, 5);        // returns 2
 
2029
        x.lastIndexOf(y, 1);        // returns -1
 
2030
    \endcode
 
2031
 
 
2032
    \sa indexOf(), contains(), count()
 
2033
*/
 
2034
int QString::lastIndexOf(const QString &str, int from, Qt::CaseSensitivity cs) const
 
2035
{
 
2036
    /*
 
2037
        See indexOf() for explanations.
 
2038
    */
 
2039
    const int l = d->size;
 
2040
    if (from < 0)
 
2041
        from += l;
 
2042
    const int sl = str.d->size;
 
2043
    int delta = l-sl;
 
2044
    if (from < 0 || from >= l || delta < 0)
 
2045
        return -1;
 
2046
    if (from > delta)
 
2047
        from = delta;
 
2048
 
 
2049
    if (sl == 1)
 
2050
        return lastIndexOf(*(const QChar*) str.d->data, from, cs);
 
2051
 
 
2052
    const QChar *needle = (const QChar*) str.d->data;
 
2053
    const QChar *haystack = (const QChar*) d->data + from;
 
2054
    const QChar *end = (const QChar*) d->data;
 
2055
    const int sl_minus_1 = sl-1;
 
2056
    const QChar *n = needle+sl_minus_1;
 
2057
    const QChar *h = haystack+sl_minus_1;
 
2058
    int hashNeedle = 0, hashHaystack = 0, idx;
 
2059
 
 
2060
    if (cs == Qt::CaseSensitive) {
 
2061
        for (idx = 0; idx < sl; ++idx) {
 
2062
            hashNeedle = ((hashNeedle<<1) + (n-idx)->unicode());
 
2063
            hashHaystack = ((hashHaystack<<1) + (h-idx)->unicode());
 
2064
        }
 
2065
        hashHaystack -= haystack->unicode();
 
2066
 
 
2067
        while (haystack >= end) {
 
2068
            hashHaystack += haystack->unicode();
 
2069
            if (hashHaystack == hashNeedle
 
2070
                 && ucstrncmp(needle, haystack, sl) == 0)
 
2071
                return haystack-unicode();
 
2072
            --haystack;
 
2073
            REHASH((haystack+sl)->unicode());
 
2074
        }
 
2075
    } else {
 
2076
        for (idx = 0; idx < sl; ++idx) {
 
2077
            hashNeedle = ((hashNeedle<<1)
 
2078
                          + ::lower((n-idx)->unicode()).unicode());
 
2079
            hashHaystack = ((hashHaystack<<1)
 
2080
                            + ::lower((h-idx)->unicode()).unicode());
 
2081
        }
 
2082
        hashHaystack -= ::lower(*haystack).unicode();
 
2083
 
 
2084
        while (haystack >= end) {
 
2085
            hashHaystack += ::lower(*haystack).unicode();
 
2086
            if (hashHaystack == hashNeedle
 
2087
                 && ucstrnicmp(needle, haystack, sl) == 0)
 
2088
                return haystack-unicode();
 
2089
            --haystack;
 
2090
            REHASH(::lower(*(haystack+sl)).unicode());
 
2091
        }
 
2092
    }
 
2093
    return -1;
 
2094
}
 
2095
 
 
2096
/*! \overload
 
2097
 
 
2098
    Returns the index position of the last occurrence of the
 
2099
    character \a ch, searching backward from position \a from.
 
2100
*/
 
2101
int QString::lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
 
2102
{
 
2103
    if (from < 0)
 
2104
        from += d->size;
 
2105
    if (from < 0 || from >= d->size)
 
2106
        return -1;
 
2107
    if (from >= 0) {
 
2108
        const QChar *n =  (const QChar*)d->data + from;
 
2109
        const QChar *b = (const QChar*)d->data;
 
2110
        if (cs == Qt::CaseSensitive) {
 
2111
            for (; n >= b; --n)
 
2112
                if (*n == ch)
 
2113
                    return  n - b;
 
2114
        } else {
 
2115
            ch = ::lower(ch);
 
2116
            for (; n >= b; --n)
 
2117
                if (::lower(*n) == ch)
 
2118
                    return  n - b;
 
2119
        }
 
2120
    }
 
2121
    return -1;
 
2122
}
 
2123
 
 
2124
#ifndef QT_NO_REGEXP_CAPTURE
 
2125
/*! \overload
 
2126
 
 
2127
    Replaces every occurrence of the regular expression \a rx in the
 
2128
    string with \a after. Returns a reference to the string. For
 
2129
    example:
 
2130
    \code
 
2131
        QString str = "Banana";
 
2132
        str.replace(QRegExp("a[mn]"), "ox");
 
2133
        // str == "Boxoxa"
 
2134
    \endcode
 
2135
 
 
2136
    For regular expressions containing \l{capturing parentheses},
 
2137
    occurrences of \bold{\\1}, \bold{\\2}, ..., in \a after are
 
2138
    replaced with \a{rx}.cap(1), cap(2), ...
 
2139
 
 
2140
    \code
 
2141
        QString str = "A <i>bon mot</i>.";
 
2142
        str.replace(QRegExp("<i>([^<]*)</i>"), "\\emph{\\1}");
 
2143
        // str == "A \\emph{bon mot}."
 
2144
    \endcode
 
2145
 
 
2146
    \sa indexOf(), lastIndexOf(), remove(), QRegExp::cap()
 
2147
*/
 
2148
QString& QString::replace(const QRegExp &rx, const QString &after)
 
2149
{
 
2150
    QRegExp rx2(rx);
 
2151
 
 
2152
    if (isEmpty() && rx2.indexIn(*this) == -1)
 
2153
        return *this;
 
2154
 
 
2155
    realloc();
 
2156
 
 
2157
    int index = 0;
 
2158
    int numCaptures = rx2.numCaptures();
 
2159
    int al = after.length();
 
2160
    QRegExp::CaretMode caretMode = QRegExp::CaretAtZero;
 
2161
 
 
2162
    if (numCaptures > 0) {
 
2163
        if (numCaptures > 9)
 
2164
            numCaptures = 9;
 
2165
 
 
2166
        const QChar *uc = after.unicode();
 
2167
        int numBackRefs = 0;
 
2168
 
 
2169
        for (int i = 0; i < al - 1; i++) {
 
2170
            if (uc[i] == QLatin1Char('\\')) {
 
2171
                int no = uc[i + 1].digitValue();
 
2172
                if (no > 0 && no <= numCaptures)
 
2173
                    numBackRefs++;
 
2174
            }
 
2175
        }
 
2176
 
 
2177
        /*
 
2178
          This is the harder case where we have back-references.
 
2179
          We don't try to optimize it.
 
2180
        */
 
2181
        if (numBackRefs > 0) {
 
2182
            int *capturePositions = new int[numBackRefs];
 
2183
            int *captureNumbers = new int[numBackRefs];
 
2184
            int j = 0;
 
2185
 
 
2186
            for (int i = 0; i < al - 1; i++) {
 
2187
                if (uc[i] == QLatin1Char('\\')) {
 
2188
                    int no = uc[i + 1].digitValue();
 
2189
                    if (no > 0 && no <= numCaptures) {
 
2190
                        capturePositions[j] = i;
 
2191
                        captureNumbers[j] = no;
 
2192
                        j++;
 
2193
                    }
 
2194
                }
 
2195
            }
 
2196
 
 
2197
            while (index <= length()) {
 
2198
                index = rx2.indexIn(*this, index, caretMode);
 
2199
                if (index == -1)
 
2200
                    break;
 
2201
 
 
2202
                QString after2 = after;
 
2203
                for (j = numBackRefs - 1; j >= 0; j--)
 
2204
                    after2.replace(capturePositions[j], 2,
 
2205
                                    rx2.cap(captureNumbers[j]));
 
2206
 
 
2207
                replace(index, rx2.matchedLength(), after2);
 
2208
                index += after2.length();
 
2209
 
 
2210
                if (rx2.matchedLength() == 0) {
 
2211
                    // avoid infinite loop on 0-length matches (e.g., [a-z]*)
 
2212
                    index++;
 
2213
                }
 
2214
                caretMode = QRegExp::CaretWontMatch;
 
2215
            }
 
2216
            delete[] capturePositions;
 
2217
            delete[] captureNumbers;
 
2218
            return *this;
 
2219
        }
 
2220
    }
 
2221
 
 
2222
    /*
 
2223
      This is the simple and optimized case where we don't have
 
2224
      back-references.
 
2225
   */
 
2226
    while (index != -1) {
 
2227
        struct {
 
2228
            int pos;
 
2229
            int length;
 
2230
        } replacements[2048];
 
2231
 
 
2232
        int pos = 0;
 
2233
        int adjust = 0;
 
2234
        while (pos < 2047) {
 
2235
            index = rx2.indexIn(*this, index, caretMode);
 
2236
            if (index == -1)
 
2237
                break;
 
2238
            int ml = rx2.matchedLength();
 
2239
            replacements[pos].pos = index;
 
2240
            replacements[pos++].length = ml;
 
2241
            index += ml;
 
2242
            adjust += al - ml;
 
2243
            // avoid infinite loop
 
2244
            if (!ml)
 
2245
                index++;
 
2246
        }
 
2247
        if (!pos)
 
2248
            break;
 
2249
        replacements[pos].pos = d->size;
 
2250
        int newlen = d->size + adjust;
 
2251
 
 
2252
        // to continue searching at the right position after we did
 
2253
        // the first round of replacements
 
2254
        if (index != -1)
 
2255
            index += adjust;
 
2256
        QString newstring;
 
2257
        newstring.reserve(newlen + 1);
 
2258
        QChar *newuc = newstring.data();
 
2259
        QChar *uc = newuc;
 
2260
        int copystart = 0;
 
2261
        int i = 0;
 
2262
        while (i < pos) {
 
2263
            int copyend = replacements[i].pos;
 
2264
            int size = copyend - copystart;
 
2265
            memcpy(uc, d->data + copystart, size * sizeof(QChar));
 
2266
            uc += size;
 
2267
            memcpy(uc, after.d->data, al * sizeof(QChar));
 
2268
            uc += al;
 
2269
            copystart = copyend + replacements[i].length;
 
2270
            i++;
 
2271
        }
 
2272
        memcpy(uc, d->data + copystart, (d->size - copystart) * sizeof(QChar));
 
2273
        newstring.resize(newlen);
 
2274
        *this = newstring;
 
2275
        caretMode = QRegExp::CaretWontMatch;
 
2276
    }
 
2277
    return *this;
 
2278
}
 
2279
#endif
 
2280
 
 
2281
/*!
 
2282
    Returns the number of (potentially overlapping) occurrences of
 
2283
    the string \a str in this string.
 
2284
 
 
2285
    If \a cs is Qt::CaseSensitive (the default), the search is
 
2286
    case sensitive; otherwise the search is case insensitive.
 
2287
 
 
2288
    \sa contains(), indexOf()
 
2289
*/
 
2290
int QString::count(const QString &str, Qt::CaseSensitivity cs) const
 
2291
{
 
2292
    int num = 0;
 
2293
    int i = -1;
 
2294
    if (d->size > 500 && str.d->size > 5) {
 
2295
        QStringMatcher matcher(str, cs);
 
2296
        while ((i = matcher.indexIn(*this, i + 1)) != -1)
 
2297
            ++num;
 
2298
    } else {
 
2299
        while ((i = indexOf(str, i + 1, cs)) != -1)
 
2300
            ++num;
 
2301
    }
 
2302
    return num;
 
2303
}
 
2304
 
 
2305
/*! \overload
 
2306
 
 
2307
    Returns the number of occurrences of character \a ch in the
 
2308
    string.
 
2309
*/
 
2310
int QString::count(QChar ch, Qt::CaseSensitivity cs) const
 
2311
{
 
2312
    int num = 0;
 
2313
    const QChar *i = (const QChar*) d->data + d->size;
 
2314
    const QChar *b = (const QChar*) d->data;
 
2315
    if (cs == Qt::CaseSensitive) {
 
2316
        while (i != b)
 
2317
            if (*--i == ch)
 
2318
                ++num;
 
2319
    } else {
 
2320
        ch = ::lower(ch);
 
2321
        while (i != b)
 
2322
            if (::lower(*--i) == ch)
 
2323
                ++num;
 
2324
    }
 
2325
    return num;
 
2326
}
 
2327
 
 
2328
/*! \fn bool QString::contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
 
2329
 
 
2330
    Returns true if this string contains an occurrence of the string
 
2331
    \a str; otherwise returns false.
 
2332
 
 
2333
    If \a cs is Qt::CaseSensitive (the default), the search is
 
2334
    case sensitive; otherwise the search is case insensitive.
 
2335
 
 
2336
    Example:
 
2337
    \code
 
2338
        QString str = "Peter Pan";
 
2339
        str.contains("peter", Qt::CaseInsensitive);    // returns true
 
2340
    \endcode
 
2341
 
 
2342
    \sa indexOf(), count()
 
2343
*/
 
2344
 
 
2345
/*! \fn bool QString::contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
 
2346
 
 
2347
    \overload
 
2348
 
 
2349
    Returns true if this string contains an occurrence of the
 
2350
    character \a ch; otherwise returns false.
 
2351
*/
 
2352
 
 
2353
/*! \fn bool QString::contains(const QRegExp &rx) const
 
2354
 
 
2355
    \overload
 
2356
 
 
2357
    Returns true if the regular expression \a rx matches somewhere in
 
2358
    this string; otherwise returns false.
 
2359
*/
 
2360
 
 
2361
#ifndef QT_NO_REGEXP
 
2362
/*!
 
2363
    \overload
 
2364
 
 
2365
    Returns the index position of the first match of the regular
 
2366
    expression \a rx in the string, searching forward from index
 
2367
    position \a from. Returns -1 if \a rx didn't match anywhere.
 
2368
 
 
2369
    Example:
 
2370
    \code
 
2371
        QString str = "the minimum";
 
2372
        str.indexOf(QRegExp("m[aeiou]"), 0);       // returns 4
 
2373
    \endcode
 
2374
*/
 
2375
int QString::indexOf(const QRegExp& rx, int from) const
 
2376
{
 
2377
    return rx.indexIn(*this, from);
 
2378
}
 
2379
 
 
2380
/*!
 
2381
    \overload
 
2382
 
 
2383
    Returns the index position of the last match of the regular
 
2384
    expression \a rx in the string, searching backward from index
 
2385
    position \a from. Returns -1 if \a rx didn't match anywhere.
 
2386
 
 
2387
    Example:
 
2388
    \code
 
2389
        QString str = "the minimum";
 
2390
        str.lastIndexOf(QRegExp("m[aeiou]"));      // returns 8
 
2391
    \endcode
 
2392
*/
 
2393
int QString::lastIndexOf(const QRegExp& rx, int from) const
 
2394
{
 
2395
    return rx.lastIndexIn(*this, from);
 
2396
}
 
2397
 
 
2398
/*! \overload
 
2399
 
 
2400
    Returns the number of times the regular expression \a rx matches
 
2401
    in the string.
 
2402
 
 
2403
    This function counts overlapping matches, so in the example
 
2404
    below, there are four instances of "ana" or "ama":
 
2405
 
 
2406
    \code
 
2407
        QString str = "banana and panama";
 
2408
        str.contains(QRegExp("a[nm]a"));    // returns 4
 
2409
    \endcode
 
2410
*/
 
2411
int QString::count(const QRegExp& rx) const
 
2412
{
 
2413
    int count = 0;
 
2414
    int index = -1;
 
2415
    int len = length();
 
2416
    while (index < len - 1) {                 // count overlapping matches
 
2417
        index = rx.indexIn(*this, index + 1);
 
2418
        if (index == -1)
 
2419
            break;
 
2420
        count++;
 
2421
    }
 
2422
    return count;
 
2423
}
 
2424
#endif // QT_NO_REGEXP
 
2425
 
 
2426
/*! \fn int QString::count() const
 
2427
 
 
2428
    \overload
 
2429
 
 
2430
    Same as size().
 
2431
*/
 
2432
 
 
2433
 
 
2434
/*!
 
2435
    \enum QString::SectionFlag
 
2436
 
 
2437
    \value SectionDefault Empty fields are counted, leading and
 
2438
    trailing separators are not included, and the separator is
 
2439
    compared case sensitively.
 
2440
 
 
2441
    \value SectionSkipEmpty Treat empty fields as if they don't exist,
 
2442
    i.e. they are not considered as far as \e start and \e end are
 
2443
    concerned.
 
2444
 
 
2445
    \value SectionIncludeLeadingSep Include the leading separator (if
 
2446
    any) in the result string.
 
2447
 
 
2448
    \value SectionIncludeTrailingSep Include the trailing separator
 
2449
    (if any) in the result string.
 
2450
 
 
2451
    \value SectionCaseInsensitiveSeps Compare the separator
 
2452
    case-insensitively.
 
2453
 
 
2454
    \sa section()
 
2455
*/
 
2456
 
 
2457
/*!
 
2458
    \fn QString QString::section(QChar sep, int start, int end = -1, SectionFlags flags) const
 
2459
 
 
2460
    This function returns a section of the string.
 
2461
 
 
2462
    This string is treated as a sequence of fields separated by the
 
2463
    character, \a sep. The returned string consists of the fields from
 
2464
    position \a start to position \a end inclusive. If \a end is not
 
2465
    specified, all fields from position \a start to the end of the
 
2466
    string are included. Fields are numbered 0, 1, 2, etc., counting
 
2467
    from the left, and -1, -2, etc., counting from right to left.
 
2468
 
 
2469
    The \a flags argument can be used to affect some aspects of the
 
2470
    function's behavior, e.g. whether to be case sensitive, whether
 
2471
    to skip empty fields and how to deal with leading and trailing
 
2472
    separators; see \l{SectionFlags}.
 
2473
 
 
2474
    \code
 
2475
        QString csv = "forename,middlename,surname,phone";
 
2476
        QString str = csv.section(',', 2, 2);   // str == "surname"
 
2477
 
 
2478
        QString path = "/usr/local/bin/myapp"; // First field is empty
 
2479
        QString str = path.section('/', 3, 4);  // str == "bin/myapp"
 
2480
        QString str = path.section('/', 3, 3, SectionSkipEmpty); // str == "myapp"
 
2481
    \endcode
 
2482
 
 
2483
    If \a start or \a end is negative, we count fields from the right
 
2484
    of the string, the right-most field being -1, the one from
 
2485
    right-most field being -2, and so on.
 
2486
 
 
2487
    \code
 
2488
        QString csv = "forename,middlename,surname,phone";
 
2489
        QString str = csv.section(',', -3, -2);  // str == "middlename,surname"
 
2490
 
 
2491
        QString path = "/usr/local/bin/myapp"; // First field is empty
 
2492
        QString str = path.section('/', -1); // str == "myapp"
 
2493
    \endcode
 
2494
 
 
2495
    \sa split()
 
2496
*/
 
2497
 
 
2498
/*!
 
2499
    \overload
 
2500
 
 
2501
    \code
 
2502
        QString data = "forename**middlename**surname**phone";
 
2503
        QString str = data.section("**", 2, 2); // str == "surname"
 
2504
    \endcode
 
2505
 
 
2506
    \code
 
2507
        QString data = "forename**middlename**surname**phone";
 
2508
        QString str = data.section("**", -3, -2); // str == "middlename**surname"
 
2509
    \endcode
 
2510
 
 
2511
    \sa split()
 
2512
*/
 
2513
 
 
2514
QString QString::section(const QString &sep, int start, int end, SectionFlags flags) const
 
2515
{
 
2516
    QStringList sections = split(sep, KeepEmptyParts,
 
2517
                                 (flags & SectionCaseInsensitiveSeps) ? Qt::CaseInsensitive : Qt::CaseSensitive);
 
2518
    if (sections.isEmpty())
 
2519
        return QString();
 
2520
    if (!(flags & SectionSkipEmpty)) {
 
2521
        if (start < 0)
 
2522
            start += sections.count();
 
2523
        if (end < 0)
 
2524
            end += sections.count();
 
2525
    } else {
 
2526
        int skip = 0;
 
2527
        for (int k=0; k<sections.size(); ++k) {
 
2528
            if (sections.at(k).isEmpty())
 
2529
                skip++;
 
2530
        }
 
2531
        if (start < 0)
 
2532
            start += sections.count() - skip;
 
2533
        if (end < 0)
 
2534
            end += sections.count() - skip;
 
2535
    }
 
2536
    int x = 0;
 
2537
    QString ret;
 
2538
    int first_i = start, last_i = end;
 
2539
    for (int i = 0; x <= end && i < sections.size(); ++i) {
 
2540
        QString section = sections.at(i);
 
2541
        const bool empty = section.isEmpty();
 
2542
        if (x >= start) {
 
2543
            if(x == start)
 
2544
                first_i = i;
 
2545
            if(x == end)
 
2546
                last_i = i;
 
2547
            if(x > start)
 
2548
                ret += sep;
 
2549
            ret += section;
 
2550
        }
 
2551
        if (!empty || !(flags & SectionSkipEmpty))
 
2552
            x++;
 
2553
    }
 
2554
    if((flags & SectionIncludeLeadingSep) && first_i)
 
2555
        ret.prepend(sep);
 
2556
    if((flags & SectionIncludeTrailingSep) && last_i < sections.size()-1)
 
2557
        ret += sep;
 
2558
    return ret;
 
2559
}
 
2560
 
 
2561
#ifndef QT_NO_REGEXP
 
2562
class section_chunk {
 
2563
public:
 
2564
    section_chunk(int l, QString s) { length = l; string = s; }
 
2565
    int length;
 
2566
    QString string;
 
2567
};
 
2568
 
 
2569
/*!
 
2570
    \overload
 
2571
 
 
2572
    This string is treated as a sequence of fields separated by the
 
2573
    regular expression, \a reg.
 
2574
 
 
2575
    \code
 
2576
        QString line = "forename\tmiddlename  surname \t \t phone";
 
2577
        QRegExp sep("\\s+");
 
2578
        QString s = line.section(sep, 2, 2); // s == "surname"
 
2579
    \endcode
 
2580
 
 
2581
    \code
 
2582
        QString line = "forename\tmiddlename  surname \t \t phone";
 
2583
        QRegExp sep("\\s+");
 
2584
        QString s = line.section(sep, -3, -2); // s == "middlename  surname"
 
2585
    \endcode
 
2586
 
 
2587
    \warning Using this QRegExp version is much more expensive than
 
2588
    the overloaded string and character versions.
 
2589
 
 
2590
    \sa split() simplified()
 
2591
*/
 
2592
QString QString::section(const QRegExp &reg, int start, int end, SectionFlags flags) const
 
2593
{
 
2594
    const QChar *uc = unicode();
 
2595
    if(!uc)
 
2596
        return QString();
 
2597
 
 
2598
    QRegExp sep(reg);
 
2599
    sep.setCaseSensitivity((flags & SectionCaseInsensitiveSeps) ? Qt::CaseInsensitive
 
2600
                                                                : Qt::CaseSensitive);
 
2601
 
 
2602
    QList<section_chunk> sections;
 
2603
    int n = length(), m = 0, last_m = 0, last_len = 0;
 
2604
    while ((m = sep.indexIn(*this, m)) != -1) {
 
2605
        sections.append(section_chunk(last_len, QString(uc + last_m, m - last_m)));
 
2606
        last_m = m;
 
2607
        last_len = sep.matchedLength();
 
2608
        m += sep.matchedLength();
 
2609
    }
 
2610
    sections.append(section_chunk(last_len, QString(uc + last_m, n - last_m)));
 
2611
 
 
2612
    if(start < 0)
 
2613
        start += sections.count();
 
2614
    if(end < 0)
 
2615
        end += sections.count();
 
2616
 
 
2617
    QString ret;
 
2618
    int x = 0;
 
2619
    int first_i = start, last_i = end;
 
2620
    for (int i = 0; x <= end && i < sections.size(); ++i) {
 
2621
        const section_chunk &section = sections.at(i);
 
2622
        const bool empty = (section.length == section.string.length());
 
2623
        if (x >= start) {
 
2624
            if(x == start)
 
2625
                first_i = i;
 
2626
            if(x == end)
 
2627
                last_i = i;
 
2628
            if(x != start)
 
2629
                ret += section.string;
 
2630
            else
 
2631
                ret += section.string.mid(section.length);
 
2632
        }
 
2633
        if (!empty || !(flags & SectionSkipEmpty))
 
2634
            x++;
 
2635
    }
 
2636
    if((flags & SectionIncludeLeadingSep)) {
 
2637
        const section_chunk &section = sections.at(first_i);
 
2638
        ret.prepend(section.string.left(section.length));
 
2639
    }
 
2640
    if((flags & SectionIncludeTrailingSep) && last_i+1 <= sections.size()-1) {
 
2641
        const section_chunk &section = sections.at(last_i+1);
 
2642
        ret += section.string.left(section.length);
 
2643
    }
 
2644
    return ret;
 
2645
}
 
2646
#endif
 
2647
 
 
2648
/*!
 
2649
    Returns a substring that contains the \a len leftmost characters
 
2650
    of the string.
 
2651
 
 
2652
    The entire string is returned if \a len is greater than size() or
 
2653
    less than zero.
 
2654
 
 
2655
    \code
 
2656
        QString x = "Pineapple";
 
2657
        QString y = x.left(4);      // y == "Pine"
 
2658
    \endcode
 
2659
 
 
2660
    \sa right(), mid(), startsWith()
 
2661
*/
 
2662
QString QString::left(int len)  const
 
2663
{
 
2664
    if (d == &shared_null)
 
2665
        return QString();
 
2666
    if (len > d->size || len < 0)
 
2667
        return *this;
 
2668
    return QString((const QChar*) d->data, len);
 
2669
}
 
2670
 
 
2671
/*!
 
2672
    Returns a substring that contains the \a len rightmost characters
 
2673
    of the string.
 
2674
 
 
2675
    The entire string is returned if \a len is greater than size() or
 
2676
    less than zero.
 
2677
 
 
2678
    \code
 
2679
        QString x = "Pineapple";
 
2680
        QString y = x.right(5);     // y == "apple"
 
2681
    \endcode
 
2682
 
 
2683
    \sa left(), mid(), endsWith()
 
2684
*/
 
2685
QString QString::right(int len) const
 
2686
{
 
2687
    if (d == &shared_null)
 
2688
        return QString();
 
2689
    if (len > d->size || len < 0)
 
2690
        return *this;
 
2691
    return QString((const QChar*) d->data + d->size - len, len);
 
2692
}
 
2693
 
 
2694
/*!
 
2695
    Returns a string that contains the \a len characters of this
 
2696
    string, starting at position \a i.
 
2697
 
 
2698
    Returns an empty string if index \a i exceeds the length of the
 
2699
    string. If there are less than \a len characters available in the
 
2700
    string starting at position \a i, or if \a len is -1 (the
 
2701
    default), the function returns all characters that are available
 
2702
    from position \a i.
 
2703
 
 
2704
    Example:
 
2705
    \code
 
2706
        QString x = "Nine pineapples";
 
2707
        QString y = x.mid(5, 4);            // y == "pine"
 
2708
        QString z = x.mid(5);               // z == "pineapples"
 
2709
    \endcode
 
2710
 
 
2711
    \sa left(), right()
 
2712
*/
 
2713
 
 
2714
QString QString::mid(int i, int len) const
 
2715
{
 
2716
    if (d == &shared_null || i >= d->size)
 
2717
        return QString();
 
2718
    if (len < 0)
 
2719
        len = d->size - i;
 
2720
    if (i < 0) {
 
2721
        len += i;
 
2722
        i = 0;
 
2723
    }
 
2724
    if (len + i > d->size)
 
2725
        len = d->size - i;
 
2726
    if (i == 0 && len == d->size)
 
2727
        return *this;
 
2728
    return QString((const QChar*) d->data + i, len);
 
2729
}
 
2730
 
 
2731
/*!
 
2732
    Returns true if the string starts with \a s; otherwise returns
 
2733
    false.
 
2734
 
 
2735
    If \a cs is Qt::CaseSensitive (the default), the search is
 
2736
    case sensitive; otherwise the search is case insensitive.
 
2737
 
 
2738
    \code
 
2739
        QString str = "Bananas";
 
2740
        str.startsWith("Ban");     // returns true
 
2741
        str.startsWith("Car");     // returns false
 
2742
    \endcode
 
2743
 
 
2744
    \sa endsWith()
 
2745
*/
 
2746
bool QString::startsWith(const QString& s, Qt::CaseSensitivity cs) const
 
2747
{
 
2748
    if (d == &shared_null)
 
2749
        return (s.d == &shared_null);
 
2750
    if (d->size == 0)
 
2751
        return s.d->size == 0;
 
2752
    if (s.d->size > d->size)
 
2753
        return false;
 
2754
    if (cs == Qt::CaseSensitive) {
 
2755
        return memcmp((char*)d->data, (char*)s.d->data, s.d->size*sizeof(QChar)) == 0;
 
2756
    } else {
 
2757
        for (int i = 0; i < s.d->size; ++i)
 
2758
            if (::lower(d->data[i]) != ::lower(s.d->data[i]))
 
2759
                return false;
 
2760
    }
 
2761
    return true;
 
2762
}
 
2763
 
 
2764
/*!
 
2765
  \overload
 
2766
 */
 
2767
bool QString::startsWith(const QLatin1String& s, Qt::CaseSensitivity cs) const
 
2768
{
 
2769
    if (d == &shared_null)
 
2770
        return (s.latin1() == 0);
 
2771
    if (d->size == 0)
 
2772
        return !s.latin1() || *s.latin1() == 0;
 
2773
    int slen = qstrlen(s.latin1());
 
2774
    if (slen > d->size)
 
2775
        return false;
 
2776
    const uchar *latin = (const uchar *)s.latin1();
 
2777
    if (cs == Qt::CaseSensitive) {
 
2778
        for (int i = 0; i < slen; ++i)
 
2779
            if (d->data[i] != latin[i])
 
2780
                return false;
 
2781
    } else {
 
2782
        for (int i = 0; i < slen; ++i)
 
2783
            if (QUnicodeTables::lower(d->data[i]) != QUnicodeTables::lower(latin[i]))
 
2784
                return false;
 
2785
    }
 
2786
    return true;
 
2787
}
 
2788
 
 
2789
/*!
 
2790
  \overload
 
2791
 */
 
2792
bool QString::startsWith(const QChar &c, Qt::CaseSensitivity cs) const
 
2793
{
 
2794
    return d->size
 
2795
           && cs == Qt::CaseSensitive
 
2796
              ? d->data[0] == c
 
2797
              : QUnicodeTables::lower(d->data[0]) == QUnicodeTables::lower(c.unicode());
 
2798
}
 
2799
 
 
2800
/*!
 
2801
    Returns true if the string ends with \a s; otherwise returns
 
2802
    false.
 
2803
 
 
2804
    If \a cs is Qt::CaseSensitive (the default), the search is case
 
2805
    sensitive; otherwise the search is case insensitive.
 
2806
 
 
2807
    \code
 
2808
        QString str = "Bananas";
 
2809
        str.endsWith("anas");         // returns true
 
2810
        str.endsWith("pple");         // returns false
 
2811
    \endcode
 
2812
 
 
2813
    \sa startsWith()
 
2814
*/
 
2815
bool QString::endsWith(const QString& s, Qt::CaseSensitivity cs) const
 
2816
{
 
2817
    if (d == &shared_null)
 
2818
        return (s.d == &shared_null);
 
2819
    if (d->size == 0)
 
2820
        return s.d->size == 0;
 
2821
    int pos = d->size - s.d->size;
 
2822
    if (pos < 0)
 
2823
        return false;
 
2824
    if (cs == Qt::CaseSensitive) {
 
2825
        return memcmp((char*)&d->data[pos], (char*)s.d->data, s.d->size*sizeof(QChar)) == 0;
 
2826
    } else {
 
2827
        for (int i = 0; i < s.length(); i++)
 
2828
            if (::lower(d->data[pos+i]) != ::lower(s.d->data[i]))
 
2829
                return false;
 
2830
    }
 
2831
    return true;
 
2832
}
 
2833
 
 
2834
/*!
 
2835
    \overload
 
2836
*/
 
2837
bool QString::endsWith(const QLatin1String& s, Qt::CaseSensitivity cs) const
 
2838
{
 
2839
    if (d == &shared_null)
 
2840
        return (s.latin1() == 0);
 
2841
    if (d->size == 0)
 
2842
        return !s.latin1() || *s.latin1() == 0;
 
2843
    int slen = qstrlen(s.latin1());
 
2844
    int pos = d->size - slen;
 
2845
    const uchar *latin = (const uchar *)s.latin1();
 
2846
    if (pos < 0)
 
2847
        return false;
 
2848
    if (cs == Qt::CaseSensitive) {
 
2849
        for (int i = 0; i < slen; i++)
 
2850
            if (d->data[pos+i] != latin[i])
 
2851
                return false;
 
2852
    } else {
 
2853
        for (int i = 0; i < slen; i++)
 
2854
            if (QUnicodeTables::lower(d->data[pos+i]) != QUnicodeTables::lower(latin[i]))
 
2855
                return false;
 
2856
    }
 
2857
    return true;
 
2858
}
 
2859
 
 
2860
/*!
 
2861
  \overload
 
2862
 */
 
2863
bool QString::endsWith(const QChar &c, Qt::CaseSensitivity cs) const
 
2864
{
 
2865
    return d->size
 
2866
           && cs == Qt::CaseSensitive
 
2867
              ? d->data[d->size - 1] == c
 
2868
              : QUnicodeTables::lower(d->data[d->size - 1]) == QUnicodeTables::lower(c.unicode());
 
2869
}
 
2870
 
 
2871
/*! \fn const char *QString::ascii() const
 
2872
 
 
2873
    Use toAscii() instead.
 
2874
*/
 
2875
 
 
2876
/*! \fn const char *QString::latin1() const
 
2877
 
 
2878
    Use toLatin1() instead.
 
2879
*/
 
2880
 
 
2881
/*! \fn const char *QString::utf8() const
 
2882
 
 
2883
    Use toUtf8() instead.
 
2884
*/
 
2885
 
 
2886
/*! \fn const char *QString::local8Bit() const
 
2887
 
 
2888
    Use toLocal8Bit() instead.
 
2889
*/
 
2890
 
 
2891
/*!
 
2892
    Returns a Latin-1 representation of the string as a QByteArray.
 
2893
    The returned byte array is undefined if the string contains
 
2894
    non-Latin1 characters.
 
2895
 
 
2896
    \sa fromLatin1(), toAscii(), toUtf8(), toLocal8Bit(), QTextCodec
 
2897
*/
 
2898
QByteArray QString::toLatin1() const
 
2899
{
 
2900
    QByteArray ba;
 
2901
    if (d->size) {
 
2902
        ba.resize(d->size);
 
2903
        const ushort *i = d->data;
 
2904
        const ushort *e = d->data + d->size;
 
2905
        uchar *s = (uchar*) ba.data();
 
2906
        while (i != e) {
 
2907
            *s++ = (*i>0xff) ? '?' : (uchar) *i;
 
2908
            ++i;
 
2909
        }
 
2910
    }
 
2911
    return ba;
 
2912
}
 
2913
 
 
2914
/*!
 
2915
    Returns an 8-bit ASCII representation of the string as a QByteArray.
 
2916
 
 
2917
    If a codec has been set using QTextCodec::setCodecForCStrings(),
 
2918
    it is used to convert Unicode to 8-bit char; otherwise this
 
2919
    function does the same as toLatin1().
 
2920
 
 
2921
    \sa fromAscii(), toLatin1(), toUtf8(), toLocal8Bit(), QTextCodec
 
2922
*/
 
2923
QByteArray QString::toAscii() const
 
2924
{
 
2925
#ifndef QT_NO_TEXTCODEC
 
2926
    if (codecForCStrings)
 
2927
        return codecForCStrings->fromUnicode(*this);
 
2928
#endif // QT_NO_TEXTCODEC
 
2929
    return toLatin1();
 
2930
}
 
2931
 
 
2932
/*!
 
2933
    Returns the local 8-bit representation of the string as a
 
2934
    QByteArray. The returned byte array is undefined if the string
 
2935
    contains characters not supported by the local 8-bit encoding.
 
2936
 
 
2937
    QTextCodec::codecForLocale() is used to perform the conversion
 
2938
    from Unicode.
 
2939
 
 
2940
    \sa fromLocal8Bit(), toAscii(), toLatin1(), toUtf8(), QTextCodec
 
2941
*/
 
2942
QByteArray QString::toLocal8Bit() const
 
2943
{
 
2944
#ifndef QT_NO_TEXTCODEC
 
2945
    if (QTextCodec::codecForLocale())
 
2946
        return QTextCodec::codecForLocale()->fromUnicode(*this);
 
2947
#endif // QT_NO_TEXTCODEC
 
2948
    return toLatin1();
 
2949
}
 
2950
 
 
2951
/*!
 
2952
    Returns a UTF-8 representation of the string as a QByteArray.
 
2953
 
 
2954
    \sa fromUtf8(), toAscii(), toLatin1(), toLocal8Bit(), QTextCodec
 
2955
*/
 
2956
QByteArray QString::toUtf8() const
 
2957
{
 
2958
    QByteArray ba;
 
2959
    if (d->size) {
 
2960
        int l = d->size;
 
2961
        int rlen = l*3+1;
 
2962
        ba.resize(rlen);
 
2963
        uchar *cursor = (uchar*)ba.data();
 
2964
        const ushort *ch =d->data;
 
2965
        for (int i=0; i < l; i++) {
 
2966
            uint u = *ch;
 
2967
            if (u < 0x80) {
 
2968
                *cursor++ = (uchar)u;
 
2969
            } else {
 
2970
                if (u < 0x0800) {
 
2971
                    *cursor++ = 0xc0 | ((uchar) (u >> 6));
 
2972
                } else {
 
2973
                    if (u >= 0xd800 && u < 0xdc00 && i < l-1) {
 
2974
                        ushort low = ch[1];
 
2975
                        if (low >= 0xdc00 && low < 0xe000) {
 
2976
                            ++ch;
 
2977
                            ++i;
 
2978
                            u = (u - 0xd800)*0x400 + (low - 0xdc00) + 0x10000;
 
2979
                        }
 
2980
                    }
 
2981
                    if (u > 0xffff) {
 
2982
                        // if people are working in utf8, but strings are encoded in eg. latin1, the resulting
 
2983
                        // name might be invalid utf8. This and the corresponding code in fromUtf8 takes care
 
2984
                        // we can handle this without loosing information. This can happen with latin filenames
 
2985
                        // and a utf8 locale under Unix.
 
2986
                        if (u > 0x10fe00 && u < 0x10ff00) {
 
2987
                            *cursor++ = (u - 0x10fe00);
 
2988
                            ++ch;
 
2989
                            continue;
 
2990
                        } else {
 
2991
                            *cursor++ = 0xf0 | ((uchar) (u >> 18));
 
2992
                            *cursor++ = 0x80 | (((uchar) (u >> 12)) & 0x3f);
 
2993
                        }
 
2994
                    } else {
 
2995
                        *cursor++ = 0xe0 | ((uchar) (u >> 12));
 
2996
                    }
 
2997
                    *cursor++ = 0x80 | (((uchar) (u >> 6)) & 0x3f);
 
2998
                }
 
2999
                *cursor++ = 0x80 | ((uchar) (u&0x3f));
 
3000
            }
 
3001
            ++ch;
 
3002
        }
 
3003
        ba.resize(cursor - (uchar*)ba.constData());
 
3004
    }
 
3005
    return ba;
 
3006
}
 
3007
 
 
3008
/*!
 
3009
    Returns a QString initialized with the first \a size characters
 
3010
    of the Latin-1 string \a str.
 
3011
 
 
3012
    If \a size is -1 (the default), it is taken to be qstrlen(\a
 
3013
    str).
 
3014
 
 
3015
    \sa toLatin1(), fromAscii(), fromUtf8(), fromLocal8Bit()
 
3016
*/
 
3017
QString QString::fromLatin1(const char *str, int size)
 
3018
{
 
3019
    Data *d;
 
3020
    if (!str) {
 
3021
        d = &shared_null;
 
3022
        d->ref.ref();
 
3023
    } else if (size == 0 || (!*str && size < 0)) {
 
3024
        d = &shared_empty;
 
3025
        d->ref.ref();
 
3026
    } else {
 
3027
        if (size < 0)
 
3028
            size = qstrlen(str);
 
3029
        d = static_cast<Data *>(qMalloc(sizeof(Data) + size * sizeof(QChar)));
 
3030
        d->ref.init(1);
 
3031
        d->alloc = d->size = size;
 
3032
        d->clean = d->asciiCache = d->simpletext = d->righttoleft = 0;
 
3033
        d->data = d->array;
 
3034
        ushort *i = d->data;
 
3035
        d->array[size] = '\0';
 
3036
        while (size--)
 
3037
           *i++ = (uchar)*str++;
 
3038
    }
 
3039
    return QString(d, 0);
 
3040
}
 
3041
 
 
3042
 
 
3043
#ifdef QT3_SUPPORT
 
3044
 
 
3045
/*!
 
3046
  \internal
 
3047
*/
 
3048
const char *QString::ascii_helper() const
 
3049
{
 
3050
    if (!asciiCache)
 
3051
        asciiCache = new QHash<void *, QByteArray>();
 
3052
 
 
3053
    d->asciiCache = true;
 
3054
    QByteArray ascii = toAscii();
 
3055
    QByteArray old = asciiCache->value(d);
 
3056
    if (old == ascii)
 
3057
        return old.constData();
 
3058
    asciiCache->insert(d, ascii);
 
3059
    return ascii.constData();
 
3060
}
 
3061
 
 
3062
/*!
 
3063
  \internal
 
3064
*/
 
3065
const char *QString::latin1_helper() const
 
3066
{
 
3067
    if (!asciiCache)
 
3068
        asciiCache = new QHash<void *, QByteArray>();
 
3069
 
 
3070
    d->asciiCache = true;
 
3071
    QByteArray ascii = toLatin1();
 
3072
    QByteArray old = asciiCache->value(d);
 
3073
    if (old == ascii)
 
3074
        return old.constData();
 
3075
    asciiCache->insert(d, ascii);
 
3076
    return ascii.constData();
 
3077
}
 
3078
 
 
3079
#endif
 
3080
 
 
3081
#ifdef Q_OS_WIN32
 
3082
#include "qt_windows.h"
 
3083
 
 
3084
QByteArray qt_winQString2MB(const QString& s, int uclen)
 
3085
{
 
3086
    if (uclen < 0)
 
3087
        uclen = s.length();
 
3088
    if (s.isNull())
 
3089
        return QByteArray();
 
3090
    if (uclen == 0)
 
3091
        return QByteArray("");
 
3092
    return qt_winQString2MB(s.constData(), uclen);
 
3093
}
 
3094
 
 
3095
QByteArray qt_winQString2MB(const QChar *ch, int uclen)
 
3096
{
 
3097
    if (!ch)
 
3098
        return QByteArray();
 
3099
    if (uclen == 0)
 
3100
        return QByteArray("");
 
3101
    BOOL used_def;
 
3102
    QByteArray mb(4096, 0);
 
3103
    int len;
 
3104
    while (!(len=WideCharToMultiByte(CP_ACP, 0, (const WCHAR*)ch, uclen,
 
3105
                mb.data(), mb.size()-1, 0, &used_def)))
 
3106
    {
 
3107
        int r = GetLastError();
 
3108
        if (r == ERROR_INSUFFICIENT_BUFFER) {
 
3109
            mb.resize(1+WideCharToMultiByte(CP_ACP, 0,
 
3110
                                (const WCHAR*)ch, uclen,
 
3111
                                0, 0, 0, &used_def));
 
3112
                // and try again...
 
3113
        } else {
 
3114
#ifndef QT_NO_DEBUG
 
3115
            // Fail.
 
3116
            qWarning("WideCharToMultiByte cannot convert multibyte text (error %d): %s (UTF-8)",
 
3117
                r, QString(ch, uclen).toLocal8Bit().data());
 
3118
#endif
 
3119
            break;
 
3120
        }
 
3121
    }
 
3122
    mb.resize(len);
 
3123
    return mb;
 
3124
}
 
3125
 
 
3126
QString qt_winMB2QString(const char *mb, int mblen)
 
3127
{
 
3128
    if (!mb || !mblen)
 
3129
        return QString();
 
3130
    const int wclen_auto = 4096;
 
3131
    WCHAR wc_auto[wclen_auto];
 
3132
    int wclen = wclen_auto;
 
3133
    WCHAR *wc = wc_auto;
 
3134
    int len;
 
3135
    while (!(len=MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,
 
3136
                mb, mblen, wc, wclen)))
 
3137
    {
 
3138
        int r = GetLastError();
 
3139
        if (r == ERROR_INSUFFICIENT_BUFFER) {
 
3140
            if (wc != wc_auto) {
 
3141
                qWarning("Size changed in MultiByteToWideChar");
 
3142
                break;
 
3143
            } else {
 
3144
                wclen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,
 
3145
                                    mb, mblen, 0, 0);
 
3146
                wc = new WCHAR[wclen];
 
3147
                // and try again...
 
3148
            }
 
3149
        } else {
 
3150
            // Fail.
 
3151
            qWarning("MultiByteToWideChar cannot convert multibyte text");
 
3152
            break;
 
3153
        }
 
3154
    }
 
3155
    if (len <= 0)
 
3156
        return QString();
 
3157
    if (wc[len-1] == 0) // len - 1: we don't want terminator
 
3158
        --len;
 
3159
    QString s((QChar*)wc, len);
 
3160
    if (wc != wc_auto)
 
3161
        delete [] wc;
 
3162
    return s;
 
3163
}
 
3164
#endif // Q_OS_WIN32
 
3165
 
 
3166
/*!
 
3167
    Returns a QString initialized with the first \a size characters
 
3168
    of the 8-bit string \a str.
 
3169
 
 
3170
    If \a size is -1 (the default), it is taken to be qstrlen(\a
 
3171
    str).
 
3172
 
 
3173
    QTextCodec::codecForLocale() is used to perform the conversion
 
3174
    from Unicode.
 
3175
 
 
3176
    \sa toLocal8Bit(), fromAscii(), fromLatin1(), fromUtf8()
 
3177
*/
 
3178
QString QString::fromLocal8Bit(const char *str, int size)
 
3179
{
 
3180
    if (!str)
 
3181
        return QString();
 
3182
#if defined(Q_OS_WIN32)
 
3183
    if (size >= 0) {
 
3184
        QByteArray ba(str, size); // creates a '\0'-terminated deep copy
 
3185
        return qt_winMB2QString(ba, size);
 
3186
    } else {
 
3187
        return qt_winMB2QString(str, size);
 
3188
    }
 
3189
#elif defined(Q_OS_UNIX)
 
3190
#  if !defined(QT_NO_TEXTCODEC)
 
3191
    if (size < 0)
 
3192
        size = qstrlen(str);
 
3193
    QTextCodec *codec = QTextCodec::codecForLocale();
 
3194
    if (codec)
 
3195
        return codec->toUnicode(str, size);
 
3196
#  endif // !QT_NO_TEXTCODEC
 
3197
#endif
 
3198
    return fromLatin1(str, size);
 
3199
}
 
3200
 
 
3201
/*!
 
3202
    Returns a QString initialized with the first \a size characters
 
3203
    of the 8-bit ASCII string \a str.
 
3204
 
 
3205
    If \a size is -1 (the default), it is taken to be qstrlen(\a
 
3206
    str).
 
3207
 
 
3208
    If a codec has been set using QTextCodec::setCodecForCStrings(),
 
3209
    it is used to convert \a str to Unicode; otherwise this function
 
3210
    does the same as fromLatin1().
 
3211
 
 
3212
    \sa toAscii(), fromLatin1(), fromUtf8(), fromLocal8Bit()
 
3213
*/
 
3214
QString QString::fromAscii(const char *str, int size)
 
3215
{
 
3216
#ifndef QT_NO_TEXTCODEC
 
3217
    if (codecForCStrings) {
 
3218
        if (!str)
 
3219
            return QString();
 
3220
        if (size == 0 || (!*str && size < 0))
 
3221
            return QLatin1String("");
 
3222
        if (size < 0)
 
3223
            size = qstrlen(str);
 
3224
        return codecForCStrings->toUnicode(str, size);
 
3225
    }
 
3226
#endif
 
3227
    return fromLatin1(str, size);
 
3228
}
 
3229
 
 
3230
static ushort *addOne(ushort *qch, QString &str)
 
3231
{
 
3232
    long sidx = qch - str.utf16();
 
3233
    str.resize(str.length()+1);
 
3234
    return (ushort *)str.data() + sidx;
 
3235
}
 
3236
 
 
3237
/*!
 
3238
    Returns a QString initialized with the first \a size bytes
 
3239
    of the UTF-8 string \a str.
 
3240
 
 
3241
    If \a size is -1 (the default), it is taken to be qstrlen(\a
 
3242
    str).
 
3243
 
 
3244
    \sa toUtf8(), fromAscii(), fromLatin1(), fromLocal8Bit()
 
3245
*/
 
3246
QString QString::fromUtf8(const char *str, int size)
 
3247
{
 
3248
    if (!str)
 
3249
        return QString();
 
3250
    if (size < 0)
 
3251
        size = qstrlen(str);
 
3252
 
 
3253
    QString result;
 
3254
    result.resize(size); // worst case
 
3255
    ushort *qch = result.d->data;
 
3256
    uint uc = 0;
 
3257
    int need = 0;
 
3258
    int error = -1;
 
3259
    uchar ch;
 
3260
    for (int i = 0; i < size; ++i) {
 
3261
        ch = str[i];
 
3262
        if (need) {
 
3263
            if ((ch&0xc0) == 0x80) {
 
3264
                uc = (uc << 6) | (ch & 0x3f);
 
3265
                need--;
 
3266
                if (!need) {
 
3267
                    if (uc > 0xffff) {
 
3268
                        // surrogate pair
 
3269
                        uc -= 0x10000;
 
3270
                        ushort high = uc/0x400 + 0xd800;
 
3271
                        ushort low = uc%0x400 + 0xdc00;
 
3272
                        *qch++ = high;
 
3273
                        *qch++ = low;
 
3274
                    } else {
 
3275
                        *qch++ = uc;
 
3276
                    }
 
3277
                }
 
3278
            } else {
 
3279
                // See QString::toUtf8() for explanation.
 
3280
                //
 
3281
                // The surrogate below corresponds to a Unicode value of (0x10fe00+ch) which
 
3282
                // is in one of the private use areas of Unicode.
 
3283
                i = error;
 
3284
                qch = addOne(qch, result);
 
3285
                *qch++ = 0xdbff;
 
3286
                *qch++ = 0xde00 + ((uchar)str[i]);
 
3287
                need = 0;
 
3288
            }
 
3289
        } else {
 
3290
            if (ch < 128) {
 
3291
                *qch++ = ch;
 
3292
            } else if ((ch & 0xe0) == 0xc0) {
 
3293
                uc = ch & 0x1f;
 
3294
                need = 1;
 
3295
                error = i;
 
3296
            } else if ((ch & 0xf0) == 0xe0) {
 
3297
                uc = ch & 0x0f;
 
3298
                need = 2;
 
3299
                error = i;
 
3300
            } else if ((ch&0xf8) == 0xf0) {
 
3301
                uc = ch & 0x07;
 
3302
                need = 3;
 
3303
                error = i;
 
3304
            } else {
 
3305
                // Error
 
3306
                qch = addOne(qch, result);
 
3307
                *qch++ = 0xdbff;
 
3308
                *qch++ = 0xde00 + ((uchar)str[i]);
 
3309
            }
 
3310
        }
 
3311
    }
 
3312
    if (need) {
 
3313
        // we have some invalid characters remaining we need to add to the string
 
3314
        for (int i = error; i < size; ++i) {
 
3315
            qch = addOne(qch, result);
 
3316
            *qch++ = 0xdbff;
 
3317
            *qch++ = 0xde00 + (uchar)str[i];
 
3318
        }
 
3319
    }
 
3320
 
 
3321
    result.truncate(qch - result.d->data);
 
3322
    return result;
 
3323
}
 
3324
 
 
3325
/*!
 
3326
    Returns a QString initialized with the first \a size characters
 
3327
    of the Unicode string \a unicode (ISO-10646-UTF-16 encoded).
 
3328
 
 
3329
    If \a size is -1 (the default), \a unicode must be terminated
 
3330
    with a 0.
 
3331
 
 
3332
    QString makes a deep copy of the Unicode data.
 
3333
 
 
3334
    \sa utf16(), setUtf16()
 
3335
*/
 
3336
QString QString::fromUtf16(const ushort *unicode, int size)
 
3337
{
 
3338
    if (!unicode)
 
3339
        return QString();
 
3340
    if (size < 0) {
 
3341
        size = 0;
 
3342
        while (unicode[size] != 0)
 
3343
            ++size;
 
3344
    }
 
3345
    return QString((const QChar *)unicode, size);
 
3346
}
 
3347
 
 
3348
/*!
 
3349
    Resizes the string to \a size characters and copies \a unicode
 
3350
    into the string.
 
3351
 
 
3352
    If \a unicode is 0, nothing is copied, but the string is still
 
3353
    resized to \a size.
 
3354
 
 
3355
    \sa unicode(), setUtf16()
 
3356
*/
 
3357
QString& QString::setUnicode(const QChar *unicode, int size)
 
3358
{
 
3359
     resize(size);
 
3360
     if (unicode && size)
 
3361
         memcpy(d->data, unicode, size * sizeof(QChar));
 
3362
     return *this;
 
3363
}
 
3364
 
 
3365
/*!
 
3366
    \fn QString &QString::setUtf16(const ushort *unicode, int size)
 
3367
 
 
3368
    Resizes the string to \a size characters and copies \a unicode
 
3369
    into the string.
 
3370
 
 
3371
    If \a unicode is 0, nothing is copied, but the string is still
 
3372
    resized to \a size.
 
3373
 
 
3374
    \sa utf16(), setUnicode()
 
3375
*/
 
3376
 
 
3377
/*!
 
3378
    Returns a string that has whitespace removed from the start
 
3379
    and the end, and that has each sequence of internal whitespace
 
3380
    replaced with a single space.
 
3381
 
 
3382
    Whitespace means any character for which QChar::isSpace() returns
 
3383
    true. This includes the ASCII characters '\\t', '\\n', '\\v',
 
3384
    '\\f', '\\r', and ' '.
 
3385
 
 
3386
    Example:
 
3387
    \code
 
3388
        QString str = "  lots\t of\nwhitespace\r\n ";
 
3389
        str = str.trimmed();
 
3390
        // str == "lots of whitespace";
 
3391
    \endcode
 
3392
 
 
3393
    \sa trimmed()
 
3394
*/
 
3395
QString QString::simplified() const
 
3396
{
 
3397
    if (d->size == 0)
 
3398
        return *this;
 
3399
    QString result;
 
3400
    result.resize(d->size);
 
3401
    const QChar *from = (const QChar*) d->data;
 
3402
    const QChar *fromend = (const QChar*) from+d->size;
 
3403
    int outc=0;
 
3404
    QChar *to   = (QChar*) result.d->data;
 
3405
    for (;;) {
 
3406
        while (from!=fromend && from->isSpace())
 
3407
            from++;
 
3408
        while (from!=fromend && !from->isSpace())
 
3409
            to[outc++] = *from++;
 
3410
        if (from!=fromend)
 
3411
            to[outc++] = QLatin1Char(' ');
 
3412
        else
 
3413
            break;
 
3414
    }
 
3415
    if (outc > 0 && to[outc-1] == QLatin1Char(' '))
 
3416
        outc--;
 
3417
    result.truncate(outc);
 
3418
    return result;
 
3419
}
 
3420
 
 
3421
/*!
 
3422
    Returns a string that has whitespace removed from the start and
 
3423
    the end.
 
3424
 
 
3425
    Whitespace means any character for which QChar::isSpace() returns
 
3426
    true. This includes the ASCII characters '\\t', '\\n', '\\v',
 
3427
    '\\f', '\\r', and ' '.
 
3428
 
 
3429
    Example:
 
3430
    \code
 
3431
        QString str = "  lots\t of\nwhitespace\r\n ";
 
3432
        str = str.trimmed();
 
3433
        // str == "lots\t of\nwhitespace";
 
3434
    \endcode
 
3435
 
 
3436
    Unlike simplified(), trimmed() leaves internal whitespace alone.
 
3437
 
 
3438
    \sa simplified()
 
3439
*/
 
3440
QString QString::trimmed() const
 
3441
{
 
3442
    if (d->size == 0)
 
3443
        return *this;
 
3444
    const QChar *s = (const QChar*)d->data;
 
3445
    if (!s->isSpace() && !s[d->size-1].isSpace())
 
3446
        return *this;
 
3447
    int start = 0;
 
3448
    int end = d->size - 1;
 
3449
    while (start<=end && s[start].isSpace())  // skip white space from start
 
3450
        start++;
 
3451
    if (start <= end) {                          // only white space
 
3452
        while (end && s[end].isSpace())           // skip white space from end
 
3453
            end--;
 
3454
    }
 
3455
    int l = end - start + 1;
 
3456
    if (l <= 0) {
 
3457
        shared_empty.ref.ref();
 
3458
        return QString(&shared_empty, 0);
 
3459
    }
 
3460
    return QString(s + start, l);
 
3461
}
 
3462
 
 
3463
/*! \fn const QChar QString::at(int i) const
 
3464
 
 
3465
    Returns the character at index position \a i in the string.
 
3466
 
 
3467
    \a i must be a valid index position in the string (i.e., 0 <= \a
 
3468
    i < size()).
 
3469
 
 
3470
    \sa operator[]()
 
3471
*/
 
3472
 
 
3473
/*! \fn QCharRef QString::operator[](int i)
 
3474
 
 
3475
    Returns the character at index position \a i as a modifiable
 
3476
    reference.
 
3477
 
 
3478
    Example:
 
3479
    \code
 
3480
        if (str[0] == QChar('?'))
 
3481
            str[0] = QChar('_');
 
3482
    \endcode
 
3483
 
 
3484
    The return value is of type QCharRef, a helper class for QString.
 
3485
    When you get an object of type QCharRef, you can use it as if it
 
3486
    were a QChar &. If you assign to it, the assignment will apply to
 
3487
    the character in the QString from which you got the reference.
 
3488
 
 
3489
    \sa at()
 
3490
*/
 
3491
 
 
3492
/*! \fn const QChar QString::operator[](int i) const
 
3493
 
 
3494
    \overload
 
3495
 
 
3496
    Same as at(\a i).
 
3497
*/
 
3498
 
 
3499
/*! \fn QCharRef QString::operator[](uint i)
 
3500
 
 
3501
    \overload
 
3502
*/
 
3503
 
 
3504
/*! \fn const QChar QString::operator[](uint i) const
 
3505
 
 
3506
    \overload
 
3507
*/
 
3508
 
 
3509
/*!
 
3510
 
 
3511
    Truncates the string at index position \a pos.
 
3512
 
 
3513
    If \a pos is beyond the end of the string, nothing happens.
 
3514
 
 
3515
    Example:
 
3516
    \code
 
3517
        QString str = "Vladivostok";
 
3518
        str.truncate(4);
 
3519
        // str == "Vlad"
 
3520
    \endcode
 
3521
 
 
3522
    \sa chop(), resize(), left()
 
3523
*/
 
3524
 
 
3525
void QString::truncate(int pos)
 
3526
{
 
3527
    if (pos < d->size)
 
3528
        resize(pos);
 
3529
}
 
3530
 
 
3531
 
 
3532
/*!
 
3533
 
 
3534
    Removes \a n characters from the end of the string.
 
3535
 
 
3536
    If \a n is greater than size(), the result is an empty string.
 
3537
 
 
3538
    Example:
 
3539
    \code
 
3540
        QString str("LOGOUT\r\n");
 
3541
        str.chop(2);
 
3542
        // str == "LOGOUT"
 
3543
    \endcode
 
3544
 
 
3545
    If you want to remove characters from the \e beginning of the
 
3546
    string, use remove() instead.
 
3547
 
 
3548
    \sa truncate(), resize(), remove()
 
3549
*/
 
3550
void QString::chop(int n)
 
3551
{
 
3552
    if (n > 0)
 
3553
        resize(d->size - n);
 
3554
}
 
3555
 
 
3556
/*!
 
3557
    Sets every character in the string to character \a ch. If \a size
 
3558
    is different from -1 (the default), the string is resized to \a
 
3559
    size beforehand.
 
3560
 
 
3561
    Example:
 
3562
    \code
 
3563
        QString str = "Berlin";
 
3564
        str.fill("z");
 
3565
        // str == "zzzzzz"
 
3566
 
 
3567
        str.fill("A", 2);
 
3568
        // str == "AA"
 
3569
    \endcode
 
3570
 
 
3571
    \sa resize()
 
3572
*/
 
3573
 
 
3574
QString& QString::fill(QChar ch, int size)
 
3575
{
 
3576
    resize(size < 0 ? d->size : size);
 
3577
    if (d->size) {
 
3578
        QChar *i = (QChar*)d->data + d->size;
 
3579
        QChar *b = (QChar*)d->data;
 
3580
        while (i != b)
 
3581
           *--i = ch;
 
3582
    }
 
3583
    return *this;
 
3584
}
 
3585
 
 
3586
/*!
 
3587
    \fn int QString::length() const
 
3588
 
 
3589
    Same as size().
 
3590
*/
 
3591
 
 
3592
/*!
 
3593
    \fn int QString::size() const
 
3594
 
 
3595
    Returns the number of characters in this string.
 
3596
 
 
3597
    The last character in the string is at position size() - 1. In
 
3598
    addition, QString ensures that the character at position size()
 
3599
    is always '\\0', so that you can use the return value of data()
 
3600
    and constData() as arguments to functions that expect
 
3601
    '\\0'-terminated strings.
 
3602
 
 
3603
    Example:
 
3604
    \code
 
3605
        QString str = "World";
 
3606
        int n = str.size();         // n == 5
 
3607
        str.data()[0];              // returns 'W'
 
3608
        str.data()[4];              // returns 'd'
 
3609
        str.data()[5];              // returns '\0'
 
3610
    \endcode
 
3611
 
 
3612
    \sa isEmpty(), resize()
 
3613
*/
 
3614
 
 
3615
/*! \fn bool QString::isNull() const
 
3616
 
 
3617
    Returns true if this string is null; otherwise returns false.
 
3618
 
 
3619
    Example:
 
3620
    \code
 
3621
        QString().isNull();             // returns true
 
3622
        QString("").isNull();           // returns false
 
3623
        QString("abc").isNull();        // returns false
 
3624
    \endcode
 
3625
 
 
3626
    Qt makes a distinction between null strings and empty strings for
 
3627
    historical reasons. For most applications, what matters is
 
3628
    whether or not a string contains any data, and this can be
 
3629
    determined using isEmpty().
 
3630
 
 
3631
    \sa isEmpty()
 
3632
*/
 
3633
 
 
3634
/*! \fn bool QString::isEmpty() const
 
3635
 
 
3636
    Returns true if the string has no characters; otherwise returns
 
3637
    false.
 
3638
 
 
3639
    Example:
 
3640
    \code
 
3641
        QString().isEmpty();            // returns true
 
3642
        QString("").isEmpty();          // returns true
 
3643
        QString("x").isEmpty();         // returns false
 
3644
        QString("abc").isEmpty();       // returns false
 
3645
    \endcode
 
3646
 
 
3647
    \sa size()
 
3648
*/
 
3649
 
 
3650
/*! \fn QString &QString::operator+=(const QString &other)
 
3651
 
 
3652
    Appends the string \a other onto the end of this string and
 
3653
    returns a reference to this string.
 
3654
 
 
3655
    Example:
 
3656
    \code
 
3657
        QString x = "free";
 
3658
        QString y = "dom";
 
3659
        x += y;
 
3660
        // x == "freedom"
 
3661
    \endcode
 
3662
 
 
3663
    This operation is typically very fast (\l{constant time}),
 
3664
    because QString preallocates extra space at the end of the string
 
3665
    data so it can grow without reallocating the entire string each
 
3666
    time.
 
3667
 
 
3668
    \sa append(), prepend()
 
3669
*/
 
3670
 
 
3671
/*! \fn QString &QString::operator+=(const QLatin1String &str)
 
3672
 
 
3673
    \overload
 
3674
 
 
3675
    Appends the Latin-1 string \a str to this string.
 
3676
*/
 
3677
 
 
3678
/*! \fn QString &QString::operator+=(const QByteArray &ba)
 
3679
 
 
3680
    \overload
 
3681
 
 
3682
    Appends the byte array \a ba to this string. \a ba is converted
 
3683
    to Unicode using fromAscii().
 
3684
 
 
3685
    You can disable this function by defining \c
 
3686
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
3687
    can be useful if you want to ensure that all user-visible strings
 
3688
    go through QObject::tr(), for example.
 
3689
*/
 
3690
 
 
3691
/*! \fn QString &QString::operator+=(const char *str)
 
3692
 
 
3693
    \overload
 
3694
 
 
3695
    Appends the string \a str to this string. \a str is converted to
 
3696
    Unicode using fromAscii().
 
3697
 
 
3698
    You can disable this function by defining \c
 
3699
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
3700
    can be useful if you want to ensure that all user-visible strings
 
3701
    go through QObject::tr(), for example.
 
3702
*/
 
3703
 
 
3704
/*! \fn QString &QString::operator+=(char ch)
 
3705
 
 
3706
    \overload
 
3707
 
 
3708
    Appends the character \a ch to this string. The character is
 
3709
    converted to Unicode using fromAscii().
 
3710
 
 
3711
    You can disable this function by defining \c
 
3712
    QT_NO_CAST_FROM_ASCII when you compile your applications. This
 
3713
    can be useful if you want to ensure that all user-visible strings
 
3714
    go through QObject::tr(), for example.
 
3715
*/
 
3716
 
 
3717
/*! \fn QString &QString::operator+=(QChar ch)
 
3718
 
 
3719
    \overload
 
3720
 
 
3721
    Appends the character \a ch to the string.
 
3722
*/
 
3723
 
 
3724
/*! \fn QString &QString::operator+=(QChar::SpecialCharacter c)
 
3725
 
 
3726
    \overload
 
3727
 
 
3728
    \internal
 
3729
*/
 
3730
 
 
3731
/*!
 
3732
    \fn bool operator==(const char *s1, const QString &s2)
 
3733
 
 
3734
    \overload
 
3735
    \relates QString
 
3736
 
 
3737
    Returns true if \a s1 is equal to \a s2; otherwise returns false.
 
3738
    Note that no string is equal to \a s1 being 0.
 
3739
 
 
3740
    Equivalent to \a s1 != 0 && compare(\a s1, \a s2) == 0.
 
3741
*/
 
3742
 
 
3743
/*!
 
3744
    \fn bool operator!=(const char *s1, const QString &s2)
 
3745
 
 
3746
    \overload
 
3747
    \relates QString
 
3748
 
 
3749
    Returns true if \a s1 is not equal to \a s2; otherwise returns false.
 
3750
    Note that no string is equal to \a s1 being 0.
 
3751
 
 
3752
    For \a s1 != 0, this is equivalent to compare(\a s1, \a s2) != 0.
 
3753
*/
 
3754
 
 
3755
/*!
 
3756
    \fn bool operator<(const char *s1, const QString &s2)
 
3757
 
 
3758
    \overload
 
3759
    \relates QString
 
3760
 
 
3761
    Returns true if \a s1 is lexically less than \a s2; otherwise returns false.
 
3762
 
 
3763
    The comparison is based exclusively on the numeric Unicode values
 
3764
    of the characters and is very fast, but is not what a human would
 
3765
    expect. Consider sorting user-interface strings with
 
3766
    QString::localeAwareCompare().
 
3767
 
 
3768
    For \a s1 != 0, equivalent to compare(\a s1, \a s2) \< 0.
 
3769
*/
 
3770
 
 
3771
/*!
 
3772
    \fn bool operator<=(const char *s1, const QString &s2)
 
3773
 
 
3774
    \overload
 
3775
    \relates QString
 
3776
 
 
3777
    Returns true if \a s1 is lexically less than or equal to \a s2;
 
3778
    otherwise returns false.
 
3779
 
 
3780
    The comparison is based exclusively on the numeric Unicode values
 
3781
    of the characters and is very fast, but is not what a human would
 
3782
    expect. Consider sorting user-interface strings with
 
3783
    QString::localeAwareCompare().
 
3784
 
 
3785
    For \a s1 != 0, this is equivalent to compare(\a s1, \a s2) \<= 0.
 
3786
*/
 
3787
 
 
3788
/*!
 
3789
    \fn bool operator>(const char *s1, const QString &s2)
 
3790
 
 
3791
    \overload
 
3792
    \relates QString
 
3793
 
 
3794
    Returns true if \a s1 is lexically greater than \a s2; otherwise
 
3795
    returns false.
 
3796
 
 
3797
    The comparison is based exclusively on the numeric Unicode values
 
3798
    of the characters and is very fast, but is not what a human would
 
3799
    expect. Consider sorting user-interface strings with
 
3800
    QString::localeAwareCompare().
 
3801
 
 
3802
    Equivalent to compare(\a s1, \a s2) \> 0.
 
3803
*/
 
3804
 
 
3805
/*!
 
3806
    \fn bool operator>=(const char *s1, const QString &s2)
 
3807
 
 
3808
    \overload
 
3809
    \relates QString
 
3810
 
 
3811
    Returns true if \a s1 is lexically greater than or equal to \a s2;
 
3812
    otherwise returns false.
 
3813
 
 
3814
    The comparison is based exclusively on the numeric Unicode values
 
3815
    of the characters and is very fast, but is not what a human would
 
3816
    expect. Consider sorting user-interface strings with
 
3817
    QString::localeAwareCompare().
 
3818
 
 
3819
    For \a s1 != 0, this is equivalent to compare(\a s1, \a s2) \>= 0.
 
3820
*/
 
3821
 
 
3822
/*!
 
3823
    \fn const QString operator+(const QString &s1, const QString &s2)
 
3824
 
 
3825
    \relates QString
 
3826
 
 
3827
    Returns a string which is the result of concatenating the string
 
3828
    \a s1 and the string \a s2.
 
3829
*/
 
3830
 
 
3831
/*!
 
3832
    \fn const QString operator+(const QString &s1, const char *s2)
 
3833
 
 
3834
    \overload
 
3835
    \relates QString
 
3836
 
 
3837
    Returns a string which is the result of concatenating the string
 
3838
    \a s1 and the string \a s2. \a s2 is converted to
 
3839
    Unicode using fromAscii().
 
3840
*/
 
3841
 
 
3842
/*!
 
3843
    \fn const QString operator+(const char *s1, const QString &s2)
 
3844
 
 
3845
    \overload
 
3846
    \relates QString
 
3847
 
 
3848
    Returns a string which is the result of concatenating the
 
3849
    string \a s1 and string \a s2. \a s1 is converted to
 
3850
    Unicode using fromAscii().
 
3851
*/
 
3852
 
 
3853
/*!
 
3854
    \fn const QString operator+(const QString &s, char ch)
 
3855
 
 
3856
    \overload
 
3857
    \relates QString
 
3858
 
 
3859
    Returns a string which is the result of concatenating the string
 
3860
    \a s and character \a ch.
 
3861
*/
 
3862
 
 
3863
/*!
 
3864
    \fn const QString operator+(char ch, const QString &s)
 
3865
 
 
3866
    \overload
 
3867
    \relates QString
 
3868
 
 
3869
    Returns a string which is the result of concatenating the
 
3870
    character \a ch and string \a s.
 
3871
*/
 
3872
 
 
3873
/*!
 
3874
    \fn int QString::compare(const QString & s1, const QString & s2)
 
3875
 
 
3876
    Lexically compares \a s1 with \a s2 and returns an integer less
 
3877
    than, equal to, or greater than zero if \a s1 is less than, equal
 
3878
    to, or greater than \a s2.
 
3879
 
 
3880
    The comparison is based exclusively on the numeric Unicode values
 
3881
    of the characters and is very fast, but is not what a human would
 
3882
    expect. Consider sorting user-visible strings with
 
3883
    localeAwareCompare().
 
3884
 
 
3885
    \code
 
3886
        int x = QString::compare("auto", "auto");   // x == 0
 
3887
        int y = QString::compare("auto", "car");    // y < 0
 
3888
        int z = QString::compare("car", "auto");    // z > 0
 
3889
    \endcode
 
3890
 
 
3891
    \sa localeAwareCompare(), operator==(), operator<(), operator>()
 
3892
*/
 
3893
 
 
3894
/*!
 
3895
    \overload
 
3896
 
 
3897
    Same as compare(*this, \a other).
 
3898
*/
 
3899
int QString::compare(const QString &other) const
 
3900
{
 
3901
    return ucstrcmp(*this, other);
 
3902
}
 
3903
 
 
3904
 
 
3905
/*!
 
3906
    \fn int QString::localeAwareCompare(const QString & s1, const QString & s2)
 
3907
 
 
3908
    Compares \a s1 with \a s2 and returns an integer less than, equal
 
3909
    to, or greater than zero if \a s1 is less than, equal to, or
 
3910
    greater than \a s2.
 
3911
 
 
3912
    The comparison is performed in a locale- and also
 
3913
    platform-dependent manner. Use this function to present sorted
 
3914
    lists of strings to the user.
 
3915
 
 
3916
    \sa compare(), QTextCodec::locale()
 
3917
*/
 
3918
 
 
3919
/*!
 
3920
    \overload
 
3921
 
 
3922
    Same as localeAwareCompare(*this, \a other).
 
3923
*/
 
3924
 
 
3925
#if !defined(CSTR_LESS_THAN)
 
3926
#define CSTR_LESS_THAN    1
 
3927
#define CSTR_EQUAL        2
 
3928
#define CSTR_GREATER_THAN 3
 
3929
#endif
 
3930
 
 
3931
int QString::localeAwareCompare(const QString &other) const
 
3932
{
 
3933
    // do the right thing for null and empty
 
3934
    if (isEmpty() || other.isEmpty())
 
3935
        return compare(other);
 
3936
 
 
3937
#if defined(Q_OS_WIN32)
 
3938
    int res;
 
3939
    QT_WA({
 
3940
        const TCHAR* s1 = (TCHAR*)utf16();
 
3941
        const TCHAR* s2 = (TCHAR*)other.utf16();
 
3942
        res = CompareStringW(GetThreadLocale(), 0, s1, length(), s2, other.length());
 
3943
    } , {
 
3944
        QByteArray s1 = toLocal8Bit();
 
3945
        QByteArray s2 = other.toLocal8Bit();
 
3946
        res = CompareStringA(GetThreadLocale(), 0, s1.data(), s1.length(), s2.data(), s2.length());
 
3947
    });
 
3948
 
 
3949
    switch (res) {
 
3950
    case CSTR_LESS_THAN:
 
3951
        return -1;
 
3952
    case CSTR_GREATER_THAN:
 
3953
        return 1;
 
3954
    default:
 
3955
        return 0;
 
3956
    }
 
3957
#elif defined(Q_OS_UNIX)
 
3958
    // declared in <string.h>
 
3959
    int delta = strcoll(toLocal8Bit(), other.toLocal8Bit());
 
3960
    if (delta == 0)
 
3961
        delta = ucstrcmp(*this, other);
 
3962
    return delta;
 
3963
#else
 
3964
    return ucstrcmp(*this, other);
 
3965
#endif
 
3966
}
 
3967
 
 
3968
 
 
3969
/*!
 
3970
    \fn const QChar *QString::unicode() const
 
3971
 
 
3972
    Returns a '\\0'-terminated Unicode representation of the string.
 
3973
    The result remains valid until the string is modified.
 
3974
 
 
3975
    \sa utf16()
 
3976
*/
 
3977
 
 
3978
/*!
 
3979
    \fn const ushort *QString::utf16() const
 
3980
 
 
3981
    Returns the QString as a '\\0\'-terminated array of unsigned
 
3982
    shorts. The result remains valid until the string is modified.
 
3983
 
 
3984
    \sa unicode()
 
3985
*/
 
3986
 
 
3987
const ushort *QString::utf16() const
 
3988
{
 
3989
    if (d->data != d->array) {
 
3990
        QString *that = const_cast<QString*>(this);
 
3991
        that->realloc();   // ensure '\\0'-termination for ::fromRawData strings
 
3992
        return that->d->data;
 
3993
    }
 
3994
    return d->array;
 
3995
}
 
3996
 
 
3997
/*!
 
3998
    Returns a string of size() \a width that contains this string
 
3999
    padded by the \a fill character.
 
4000
 
 
4001
    If \a truncate is false and the size() of the string is more than
 
4002
    \a width, then the returned string is a copy of the string.
 
4003
 
 
4004
    If \a truncate is true and the size() of the string is more than
 
4005
    \a width, then any characters in a copy of the string after
 
4006
    position \a width are removed, and the copy is returned.
 
4007
 
 
4008
    \code
 
4009
        QString s = "apple";
 
4010
        QString t = s.leftJustified(8, '.');        // t == "apple..."
 
4011
    \endcode
 
4012
 
 
4013
    \sa rightJustified()
 
4014
*/
 
4015
 
 
4016
QString QString::leftJustified(int width, QChar fill, bool truncate) const
 
4017
{
 
4018
    QString result;
 
4019
    int len = length();
 
4020
    int padlen = width - len;
 
4021
    if (padlen > 0) {
 
4022
        result.resize(len+padlen);
 
4023
        if (len)
 
4024
            memcpy(result.d->data, d->data, sizeof(QChar)*len);
 
4025
        QChar *uc = (QChar*)result.d->data + len;
 
4026
        while (padlen--)
 
4027
           * uc++ = fill;
 
4028
    } else {
 
4029
        if (truncate)
 
4030
            result = left(width);
 
4031
        else
 
4032
            result = *this;
 
4033
    }
 
4034
    return result;
 
4035
}
 
4036
 
 
4037
/*!
 
4038
    Returns a string of size() \a width that contains the \a fill
 
4039
    character followed by the string.
 
4040
 
 
4041
    If \a truncate is false and the size() of the string is more than
 
4042
    \a width, then the returned string is a copy of the string.
 
4043
 
 
4044
    If \a truncate is true and the size() of the string is more than
 
4045
    \a width, then the resulting string is truncated at position \a
 
4046
    width.
 
4047
 
 
4048
    \code
 
4049
        QString str = "apple";
 
4050
        str = str.rightJustified(8, '.');
 
4051
        // str == "...apple"
 
4052
    \endcode
 
4053
 
 
4054
    \sa leftJustified()
 
4055
*/
 
4056
 
 
4057
QString QString::rightJustified(int width, QChar fill, bool truncate) const
 
4058
{
 
4059
    QString result;
 
4060
    int len = length();
 
4061
    int padlen = width - len;
 
4062
    if (padlen > 0) {
 
4063
        result.resize(len+padlen);
 
4064
        QChar *uc = (QChar*)result.d->data;
 
4065
        while (padlen--)
 
4066
           * uc++ = fill;
 
4067
        if (len)
 
4068
            memcpy(uc, d->data, sizeof(QChar)*len);
 
4069
    } else {
 
4070
        if (truncate)
 
4071
            result = left(width);
 
4072
        else
 
4073
            result = *this;
 
4074
    }
 
4075
    return result;
 
4076
}
 
4077
 
 
4078
/*!
 
4079
    Returns a lowercase copy of the string.
 
4080
 
 
4081
    \code
 
4082
        QString str = "TROlltECH";
 
4083
        str = str.toLower();        // str == "trolltech"
 
4084
    \endcode
 
4085
 
 
4086
    \sa toUpper()
 
4087
*/
 
4088
 
 
4089
QString QString::toLower() const
 
4090
{
 
4091
    int l = d->size;
 
4092
    if (l) {
 
4093
        QChar *p = (QChar*)d->data;
 
4094
        if (p) {
 
4095
            while (l) {
 
4096
                bool different;
 
4097
                if (p->unicode() & 0xFF80)
 
4098
                    different = (*p != ::lower(*p));
 
4099
                else
 
4100
                    different = ((uint)p->cell() - 'A' < 26);
 
4101
 
 
4102
                if (different) {
 
4103
                    QString s(*this);
 
4104
                    p = (QChar*)s.data() + (p - (QChar*)d->data);
 
4105
                    while (l) {
 
4106
                        *p = ::lower(*p);
 
4107
                        l--;
 
4108
                        p++;
 
4109
                    }
 
4110
                    return s;
 
4111
                }
 
4112
                l--;
 
4113
                p++;
 
4114
            }
 
4115
        }
 
4116
    }
 
4117
    return *this;
 
4118
}
 
4119
 
 
4120
/*!
 
4121
    Returns an uppercase copy of the string.
 
4122
 
 
4123
    \code
 
4124
        QString str = "TeXt";
 
4125
        str = str.toUpper();        // str == "TEXT"
 
4126
    \endcode
 
4127
 
 
4128
    \sa toLower()
 
4129
*/
 
4130
 
 
4131
QString QString::toUpper() const
 
4132
{
 
4133
    int l = d->size;
 
4134
    if (l) {
 
4135
        QChar *p = (QChar*)d->data;
 
4136
        if (p) {
 
4137
            while (l) {
 
4138
                bool different;
 
4139
                if (p->unicode() & 0xFF80)
 
4140
                    different = (*p != ::upper(*p));
 
4141
                else
 
4142
                    different = ((uint)p->cell() - 'a' < 26);
 
4143
 
 
4144
                if (different) {
 
4145
                    QString s(*this);
 
4146
                    p = s.data() + (p - (QChar*)d->data);
 
4147
                    while (l) {
 
4148
                        *p = ::upper(*p);
 
4149
                        l--;
 
4150
                        p++;
 
4151
                    }
 
4152
                    return s;
 
4153
                }
 
4154
                l--;
 
4155
                p++;
 
4156
            }
 
4157
        }
 
4158
    }
 
4159
    return *this;
 
4160
}
 
4161
 
 
4162
 
 
4163
#ifndef QT_NO_SPRINTF
 
4164
/*!
 
4165
    Safely builds a formatted string from the format string \a cformat
 
4166
    and an arbitrary list of arguments.
 
4167
 
 
4168
    The %lc escape sequence expects a unicode character of type ushort
 
4169
    (as returned by QChar::unicode()). The %ls escape sequence expects
 
4170
    a pointer to a zero-terminated array of unicode characters of type
 
4171
    ushort (as returned by QString::utf16()).
 
4172
 
 
4173
    The format string supports most of the conversion specifiers
 
4174
    provided by printf() in the standard C++ library. It doesn't
 
4175
    honor the length modifiers (e.g. \c h for \c short, \c ll for
 
4176
    \c{long long}). If you need those, use the standard sprintf()
 
4177
    function instead:
 
4178
 
 
4179
    \code
 
4180
        char buf[BufSize];
 
4181
        ::sprintf(buf, "%lld", 123456789LL);
 
4182
        QString str = QString::fromAscii(buf);
 
4183
    \endcode
 
4184
 
 
4185
    \warning We do not recommend using QString::sprintf() in new Qt
 
4186
    code. Instead, consider using QTextOStream or arg(), both of
 
4187
    which support Unicode strings seamlessly and are type-safe.
 
4188
    Here's an example that uses QTextOStream:
 
4189
 
 
4190
    \code
 
4191
        QString result;
 
4192
        QTextOStream(&result) << "pi = " << 3.14;
 
4193
        // result == "pi = 3.14"
 
4194
    \endcode
 
4195
 
 
4196
    For \link QObject::tr() translations,\endlink especially if the
 
4197
    strings contains more than one escape sequence, you should
 
4198
    consider using the arg() function instead. This allows the order
 
4199
    of the replacements to be controlled by the translator.
 
4200
 
 
4201
    \sa arg()
 
4202
*/
 
4203
 
 
4204
QString &QString::sprintf(const char *cformat, ...)
 
4205
{
 
4206
    va_list ap;
 
4207
    va_start(ap, cformat);
 
4208
    QString &s = vsprintf(cformat, ap);
 
4209
    va_end(ap);
 
4210
    return s;
 
4211
}
 
4212
 
 
4213
/*!
 
4214
    Equivalent method to sprintf(), but takes a va_list \a ap
 
4215
    instead a list of variable arguments. See the sprintf()
 
4216
    documentation for an explanation of \a cformat.
 
4217
 
 
4218
    This method does not call the va_end macro, the caller
 
4219
    is responsible to call va_end on \a ap.
 
4220
 
 
4221
    \sa sprintf()
 
4222
*/
 
4223
 
 
4224
QString &QString::vsprintf(const char* cformat, va_list ap)
 
4225
{
 
4226
    QLocale locale(QLocale::C);
 
4227
 
 
4228
    if (!cformat || !*cformat) {
 
4229
        // Qt 1.x compat
 
4230
        *this = fromLatin1("");
 
4231
        return *this;
 
4232
    }
 
4233
 
 
4234
    // Parse cformat
 
4235
 
 
4236
    QString result;
 
4237
    const char *c = cformat;
 
4238
    for (;;) {
 
4239
        // Copy non-escape chars to result
 
4240
        while (*c != '\0' && *c != '%')
 
4241
            result.append(QLatin1Char(*c++));
 
4242
 
 
4243
        if (*c == '\0')
 
4244
            break;
 
4245
 
 
4246
        // Found '%'
 
4247
        const char *escape_start = c;
 
4248
        ++c;
 
4249
 
 
4250
        if (*c == '\0') {
 
4251
            result.append(QLatin1Char('%')); // a % at the end of the string - treat as non-escape text
 
4252
            break;
 
4253
        }
 
4254
        if (*c == '%') {
 
4255
            result.append(QLatin1Char('%')); // %%
 
4256
            ++c;
 
4257
            continue;
 
4258
        }
 
4259
 
 
4260
        // Parse flag characters
 
4261
        uint flags = 0;
 
4262
        bool no_more_flags = false;
 
4263
        do {
 
4264
            switch (*c) {
 
4265
                case '#': flags |= QLocalePrivate::Alternate; break;
 
4266
                case '0': flags |= QLocalePrivate::ZeroPadded; break;
 
4267
                case '-': flags |= QLocalePrivate::LeftAdjusted; break;
 
4268
                case ' ': flags |= QLocalePrivate::BlankBeforePositive; break;
 
4269
                case '+': flags |= QLocalePrivate::AlwaysShowSign; break;
 
4270
                case '\'': flags |= QLocalePrivate::ThousandsGroup; break;
 
4271
                default: no_more_flags = true; break;
 
4272
            }
 
4273
 
 
4274
            if (!no_more_flags)
 
4275
                ++c;
 
4276
        } while (!no_more_flags);
 
4277
 
 
4278
        if (*c == '\0') {
 
4279
            result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text
 
4280
            break;
 
4281
        }
 
4282
 
 
4283
        // Parse field width
 
4284
        int width = -1; // -1 means unspecified
 
4285
        if (qIsDigit(*c)) {
 
4286
            QString width_str;
 
4287
            while (*c != '\0' && qIsDigit(*c))
 
4288
                width_str.append(QLatin1Char(*c++));
 
4289
 
 
4290
            // can't be negative - started with a digit
 
4291
            // contains at least one digit
 
4292
            width = width_str.toInt();
 
4293
        }
 
4294
        else if (*c == '*') {
 
4295
            width = va_arg(ap, int);
 
4296
            if (width < 0)
 
4297
                width = -1; // treat all negative numbers as unspecified
 
4298
            ++c;
 
4299
        }
 
4300
 
 
4301
        if (*c == '\0') {
 
4302
            result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text
 
4303
            break;
 
4304
        }
 
4305
 
 
4306
        // Parse precision
 
4307
        int precision = -1; // -1 means unspecified
 
4308
        if (*c == '.') {
 
4309
            ++c;
 
4310
            if (qIsDigit(*c)) {
 
4311
                QString precision_str;
 
4312
                while (*c != '\0' && qIsDigit(*c))
 
4313
                    precision_str.append(QLatin1Char(*c++));
 
4314
 
 
4315
                // can't be negative - started with a digit
 
4316
                // contains at least one digit
 
4317
                precision = precision_str.toInt();
 
4318
            }
 
4319
            else if (*c == '*') {
 
4320
                precision = va_arg(ap, int);
 
4321
                if (precision < 0)
 
4322
                    precision = -1; // treat all negative numbers as unspecified
 
4323
                ++c;
 
4324
            }
 
4325
        }
 
4326
 
 
4327
        if (*c == '\0') {
 
4328
            result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text
 
4329
            break;
 
4330
        }
 
4331
 
 
4332
        // Parse the length modifier
 
4333
        enum LengthMod { lm_none, lm_hh, lm_h, lm_l, lm_ll, lm_L, lm_j, lm_z, lm_t };
 
4334
        LengthMod length_mod = lm_none;
 
4335
        switch (*c) {
 
4336
            case 'h':
 
4337
                ++c;
 
4338
                if (*c == 'h') {
 
4339
                    length_mod = lm_hh;
 
4340
                    ++c;
 
4341
                }
 
4342
                else
 
4343
                    length_mod = lm_h;
 
4344
                break;
 
4345
 
 
4346
            case 'l':
 
4347
                ++c;
 
4348
                if (*c == 'l') {
 
4349
                    length_mod = lm_ll;
 
4350
                    ++c;
 
4351
                }
 
4352
                else
 
4353
                    length_mod = lm_l;
 
4354
                break;
 
4355
 
 
4356
            case 'L':
 
4357
                ++c;
 
4358
                length_mod = lm_L;
 
4359
                break;
 
4360
 
 
4361
            case 'j':
 
4362
                ++c;
 
4363
                length_mod = lm_j;
 
4364
                break;
 
4365
 
 
4366
            case 'z':
 
4367
            case 'Z':
 
4368
                ++c;
 
4369
                length_mod = lm_z;
 
4370
                break;
 
4371
 
 
4372
            case 't':
 
4373
                ++c;
 
4374
                length_mod = lm_t;
 
4375
                break;
 
4376
 
 
4377
            default: break;
 
4378
        }
 
4379
 
 
4380
        if (*c == '\0') {
 
4381
            result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text
 
4382
            break;
 
4383
        }
 
4384
 
 
4385
        // Parse the conversion specifier and do the conversion
 
4386
        QString subst;
 
4387
        switch (*c) {
 
4388
            case 'd':
 
4389
            case 'i': {
 
4390
                qint64 i;
 
4391
                switch (length_mod) {
 
4392
                    case lm_none: i = va_arg(ap, int); break;
 
4393
                    case lm_hh: i = va_arg(ap, int); break;
 
4394
                    case lm_h: i = va_arg(ap, int); break;
 
4395
                    case lm_l: i = va_arg(ap, long int); break;
 
4396
                    case lm_ll: i = va_arg(ap, qint64); break;
 
4397
                    case lm_j: i = va_arg(ap, long int); break;
 
4398
                    case lm_z: i = va_arg(ap, size_t); break;
 
4399
                    case lm_t: i = va_arg(ap, int); break;
 
4400
                    default: i = 0; break;
 
4401
                }
 
4402
                subst = locale.d->longLongToString(i, precision, 10, width, flags);
 
4403
                ++c;
 
4404
                break;
 
4405
            }
 
4406
            case 'o':
 
4407
            case 'u':
 
4408
            case 'x':
 
4409
            case 'X': {
 
4410
                quint64 u;
 
4411
                switch (length_mod) {
 
4412
                    case lm_none: u = va_arg(ap, uint); break;
 
4413
                    case lm_hh: u = va_arg(ap, uint); break;
 
4414
                    case lm_h: u = va_arg(ap, uint); break;
 
4415
                    case lm_l: u = va_arg(ap, ulong); break;
 
4416
                    case lm_ll: u = va_arg(ap, quint64); break;
 
4417
                    default: u = 0; break;
 
4418
                }
 
4419
 
 
4420
                if (qIsUpper(*c))
 
4421
                    flags |= QLocalePrivate::CapitalEorX;
 
4422
 
 
4423
                int base = 10;
 
4424
                switch (qToLower(*c)) {
 
4425
                    case 'o':
 
4426
                        base = 8; break;
 
4427
                    case 'u':
 
4428
                        base = 10; break;
 
4429
                    case 'x':
 
4430
                        base = 16; break;
 
4431
                    default: break;
 
4432
                }
 
4433
                subst = locale.d->unsLongLongToString(u, precision, base, width, flags);
 
4434
                ++c;
 
4435
                break;
 
4436
            }
 
4437
            case 'E':
 
4438
            case 'e':
 
4439
            case 'F':
 
4440
            case 'f':
 
4441
            case 'G':
 
4442
            case 'g':
 
4443
            case 'A':
 
4444
            case 'a': {
 
4445
                double d;
 
4446
                if (length_mod == lm_L)
 
4447
                    d = va_arg(ap, long double); // not supported - converted to a double
 
4448
                else
 
4449
                    d = va_arg(ap, double);
 
4450
 
 
4451
                if (qIsUpper(*c))
 
4452
                    flags |= QLocalePrivate::CapitalEorX;
 
4453
 
 
4454
                QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
 
4455
                switch (qToLower(*c)) {
 
4456
                    case 'e': form = QLocalePrivate::DFExponent; break;
 
4457
                    case 'a':                             // not supported - decimal form used instead
 
4458
                    case 'f': form = QLocalePrivate::DFDecimal; break;
 
4459
                    case 'g': form = QLocalePrivate::DFSignificantDigits; break;
 
4460
                    default: break;
 
4461
                }
 
4462
                subst = locale.d->doubleToString(d, precision, form, width, flags);
 
4463
                ++c;
 
4464
                break;
 
4465
            }
 
4466
            case 'c': {
 
4467
                if (length_mod == lm_l)
 
4468
                    subst = QChar((ushort) va_arg(ap, int));
 
4469
                else
 
4470
                    subst = QLatin1Char((uchar) va_arg(ap, int));
 
4471
                ++c;
 
4472
                break;
 
4473
            }
 
4474
            case 's': {
 
4475
                if (length_mod == lm_l) {
 
4476
                    const ushort *buff = va_arg(ap, const ushort*);
 
4477
                    const ushort *ch = buff;
 
4478
                    while (*ch != 0)
 
4479
                        ++ch;
 
4480
                    subst.setUtf16(buff, ch - buff);
 
4481
                } else
 
4482
                    subst = QString::fromUtf8(va_arg(ap, const char*));
 
4483
                if (precision != -1)
 
4484
                    subst.truncate(precision);
 
4485
                ++c;
 
4486
                break;
 
4487
            }
 
4488
            case 'p': {
 
4489
                void *arg = va_arg(ap, void*);
 
4490
#ifdef Q_OS_WIN64
 
4491
                quint64 i = reinterpret_cast<quint64>(arg);
 
4492
#else
 
4493
                quint64 i = reinterpret_cast<unsigned long>(arg);
 
4494
#endif
 
4495
                flags |= QLocalePrivate::Alternate;
 
4496
                subst = locale.d->unsLongLongToString(i, precision, 16, width, flags);
 
4497
                ++c;
 
4498
                break;
 
4499
            }
 
4500
            case 'n':
 
4501
                switch (length_mod) {
 
4502
                    case lm_hh: {
 
4503
                        signed char *n = va_arg(ap, signed char*);
 
4504
                        *n = result.length();
 
4505
                        break;
 
4506
                    }
 
4507
                    case lm_h: {
 
4508
                        short int *n = va_arg(ap, short int*);
 
4509
                        *n = result.length();
 
4510
                            break;
 
4511
                    }
 
4512
                    case lm_l: {
 
4513
                        long int *n = va_arg(ap, long int*);
 
4514
                        *n = result.length();
 
4515
                        break;
 
4516
                    }
 
4517
                    case lm_ll: {
 
4518
                        qint64 *n = va_arg(ap, qint64*);
 
4519
                        volatile uint tmp = result.length(); // egcs-2.91.66 gets internal
 
4520
                        *n = tmp;                             // compiler error without volatile
 
4521
                        break;
 
4522
                    }
 
4523
                    default: {
 
4524
                        int *n = va_arg(ap, int*);
 
4525
                        *n = result.length();
 
4526
                        break;
 
4527
                    }
 
4528
                }
 
4529
                ++c;
 
4530
                break;
 
4531
 
 
4532
            default: // bad escape, treat as non-escape text
 
4533
                for (const char *cc = escape_start; cc != c; ++cc)
 
4534
                    result.append(QLatin1Char(*cc));
 
4535
                continue;
 
4536
        }
 
4537
 
 
4538
        if (flags & QLocalePrivate::LeftAdjusted)
 
4539
            result.append(subst.leftJustified(width));
 
4540
        else
 
4541
            result.append(subst.rightJustified(width));
 
4542
    }
 
4543
 
 
4544
    *this = result;
 
4545
 
 
4546
    return *this;
 
4547
}
 
4548
#endif
 
4549
 
 
4550
/*!
 
4551
    Returns the string converted to a \c{long long} using base \a
 
4552
    base, which is 10 by default and must be between 2 and 36, or 0.
 
4553
    Returns 0 if the conversion fails.
 
4554
 
 
4555
    If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
 
4556
    false; otherwise *\a{ok} is set to true.
 
4557
 
 
4558
    If \a base is 0, the C language convention is used: If the string
 
4559
    begins with "0x", base 16 is used; if the string begins with "0",
 
4560
    base 8 is used; otherwise, base 10 is used.
 
4561
 
 
4562
    Example:
 
4563
 
 
4564
    \code
 
4565
        QString str = "FF";
 
4566
        bool ok;
 
4567
        qint64 hex = str.toLongLong(&ok, 16);      // hex == 255, ok == true
 
4568
        qint64 dec = str.toLongLong(&ok, 10);      // dec == 0, ok == false
 
4569
    \endcode
 
4570
 
 
4571
    \sa number(), toULongLong(), toInt()
 
4572
*/
 
4573
 
 
4574
qint64 QString::toLongLong(bool *ok, int base) const
 
4575
{
 
4576
#if defined(QT_CHECK_RANGE)
 
4577
    if (base != 0 && (base < 2 || base > 36)) {
 
4578
        qWarning("QString::toLongLong: Invalid base (%d)", base);
 
4579
        base = 10;
 
4580
    }
 
4581
#endif
 
4582
 
 
4583
    bool my_ok;
 
4584
    QLocale def_locale;
 
4585
    qint64 result = def_locale.d->stringToLongLong(*this, base, &my_ok, QLocalePrivate::FailOnGroupSeparators);
 
4586
    if (my_ok) {
 
4587
        if (ok != 0)
 
4588
            *ok = true;
 
4589
        return result;
 
4590
    }
 
4591
 
 
4592
    // If the default was not "C", try the "C" locale
 
4593
    if (def_locale.language() == QLocale::C) {
 
4594
        if (ok != 0)
 
4595
            *ok = false;
 
4596
        return 0;
 
4597
    }
 
4598
 
 
4599
    QLocale c_locale(QLocale::C);
 
4600
    return c_locale.d->stringToLongLong(*this, base, ok, QLocalePrivate::FailOnGroupSeparators);
 
4601
}
 
4602
 
 
4603
/*!
 
4604
    Returns the string converted to an \c{unsigned long long} using base \a
 
4605
    base, which is 10 by default and must be between 2 and 36, or 0.
 
4606
    Returns 0 if the conversion fails.
 
4607
 
 
4608
    If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
 
4609
    false; otherwise *\a{ok} is set to true.
 
4610
 
 
4611
    If \a base is 0, the C language convention is used: If the string
 
4612
    begins with "0x", base 16 is used; if the string begins with "0",
 
4613
    base 8 is used; otherwise, base 10 is used.
 
4614
 
 
4615
    Example:
 
4616
 
 
4617
    \code
 
4618
        QString str = "FF";
 
4619
        bool ok;
 
4620
        quint64 hex = str.toULongLong(&ok, 16);    // hex == 255, ok == true
 
4621
        quint64 dec = str.toULongLong(&ok, 10);    // dec == 0, ok == false
 
4622
    \endcode
 
4623
 
 
4624
    \sa number(), toLongLong()
 
4625
*/
 
4626
 
 
4627
quint64 QString::toULongLong(bool *ok, int base) const
 
4628
{
 
4629
#if defined(QT_CHECK_RANGE)
 
4630
    if (base != 0 && (base < 2 || base > 36)) {
 
4631
        qWarning("QString::toULongLong: Invalid base %d", base);
 
4632
        base = 10;
 
4633
    }
 
4634
#endif
 
4635
 
 
4636
    bool my_ok;
 
4637
    QLocale def_locale;
 
4638
    quint64 result = def_locale.d->stringToUnsLongLong(*this, base, &my_ok, QLocalePrivate::FailOnGroupSeparators);
 
4639
    if (my_ok) {
 
4640
        if (ok != 0)
 
4641
            *ok = true;
 
4642
        return result;
 
4643
    }
 
4644
 
 
4645
    // If the default was not "C", try the "C" locale
 
4646
    if (def_locale.language() == QLocale::C) {
 
4647
        if (ok != 0)
 
4648
            *ok = false;
 
4649
        return 0;
 
4650
    }
 
4651
 
 
4652
    QLocale c_locale(QLocale::C);
 
4653
    return c_locale.d->stringToUnsLongLong(*this, base, ok, QLocalePrivate::FailOnGroupSeparators);
 
4654
}
 
4655
 
 
4656
/*!
 
4657
  \fn long QString::toLong(bool *ok, int base) const
 
4658
 
 
4659
    Returns the string converted to a \c long using base \a
 
4660
    base, which is 10 by default and must be between 2 and 36, or 0.
 
4661
    Returns 0 if the conversion fails.
 
4662
 
 
4663
    If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
 
4664
    false; otherwise *\a{ok} is set to true.
 
4665
 
 
4666
    If \a base is 0, the C language convention is used: If the string
 
4667
    begins with "0x", base 16 is used; if the string begins with "0",
 
4668
    base 8 is used; otherwise, base 10 is used.
 
4669
 
 
4670
    Example:
 
4671
 
 
4672
    \code
 
4673
        QString str = "FF";
 
4674
        bool ok;
 
4675
        long hex = str.toLong(&ok, 16);     // hex == 255, ok == true
 
4676
        long dec = str.toLong(&ok, 10);     // dec == 0, ok == false
 
4677
    \endcode
 
4678
 
 
4679
    \sa number(), toULong(), toInt()
 
4680
*/
 
4681
 
 
4682
long QString::toLong(bool *ok, int base) const
 
4683
{
 
4684
    qint64 v = toLongLong(ok, base);
 
4685
    if (v < LONG_MIN || v > LONG_MAX) {
 
4686
        if (ok)
 
4687
            *ok = false;
 
4688
        v = 0;
 
4689
    }
 
4690
    return (long)v;
 
4691
}
 
4692
 
 
4693
/*!
 
4694
    \fn ulong QString::toULong(bool *ok, int base) const
 
4695
 
 
4696
    Returns the string converted to an \c{unsigned long} using base \a
 
4697
    base, which is 10 by default and must be between 2 and 36, or 0.
 
4698
    Returns 0 if the conversion fails.
 
4699
 
 
4700
    If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
 
4701
    false; otherwise *\a{ok} is set to true.
 
4702
 
 
4703
    If \a base is 0, the C language convention is used: If the string
 
4704
    begins with "0x", base 16 is used; if the string begins with "0",
 
4705
    base 8 is used; otherwise, base 10 is used.
 
4706
 
 
4707
    Example:
 
4708
 
 
4709
    \code
 
4710
        QString str = "FF";
 
4711
        bool ok;
 
4712
        ulong hex = str.toULong(&ok, 16);   // hex == 255, ok == true
 
4713
        ulong dec = str.toULong(&ok, 10);   // dec == 0, ok == false
 
4714
    \endcode
 
4715
 
 
4716
    \sa number()
 
4717
*/
 
4718
 
 
4719
ulong QString::toULong(bool *ok, int base) const
 
4720
{
 
4721
    quint64 v = toULongLong(ok, base);
 
4722
    if (v > ULONG_MAX) {
 
4723
        if (ok)
 
4724
            *ok = false;
 
4725
        v = 0;
 
4726
    }
 
4727
    return (ulong)v;
 
4728
}
 
4729
 
 
4730
 
 
4731
/*!
 
4732
    Returns the string converted to an \c int using base \a
 
4733
    base, which is 10 by default and must be between 2 and 36, or 0.
 
4734
    Returns 0 if the conversion fails.
 
4735
 
 
4736
    If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
 
4737
    false; otherwise *\a{ok} is set to true.
 
4738
 
 
4739
    If \a base is 0, the C language convention is used: If the string
 
4740
    begins with "0x", base 16 is used; if the string begins with "0",
 
4741
    base 8 is used; otherwise, base 10 is used.
 
4742
 
 
4743
    Example:
 
4744
 
 
4745
    \code
 
4746
        QString str = "FF";
 
4747
        bool ok;
 
4748
        int hex = str.toInt(&ok, 16);       // hex == 255, ok == true
 
4749
        int dec = str.toInt(&ok, 10);       // dec == 0, ok == false
 
4750
    \endcode
 
4751
 
 
4752
    \sa number(), toUInt(), toDouble()
 
4753
*/
 
4754
 
 
4755
int QString::toInt(bool *ok, int base) const
 
4756
{
 
4757
    qint64 v = toLongLong(ok, base);
 
4758
    if (v < INT_MIN || v > INT_MAX) {
 
4759
        if (ok)
 
4760
            *ok = false;
 
4761
        v = 0;
 
4762
    }
 
4763
    return v;
 
4764
}
 
4765
 
 
4766
/*!
 
4767
    Returns the string converted to an \c{unsigned int} using base \a
 
4768
    base, which is 10 by default and must be between 2 and 36, or 0.
 
4769
    Returns 0 if the conversion fails.
 
4770
 
 
4771
    If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
 
4772
    false; otherwise *\a{ok} is set to true.
 
4773
 
 
4774
    If \a base is 0, the C language convention is used: If the string
 
4775
    begins with "0x", base 16 is used; if the string begins with "0",
 
4776
    base 8 is used; otherwise, base 10 is used.
 
4777
 
 
4778
    Example:
 
4779
 
 
4780
    \code
 
4781
        QString str = "FF";
 
4782
        bool ok;
 
4783
        uint hex = str.toUInt(&ok, 16);     // hex == 255, ok == true
 
4784
        uint dec = str.toUInt(&ok, 10);     // dec == 0, ok == false
 
4785
    \endcode
 
4786
 
 
4787
    \sa number(), toInt()
 
4788
*/
 
4789
 
 
4790
uint QString::toUInt(bool *ok, int base) const
 
4791
{
 
4792
    quint64 v = toULongLong(ok, base);
 
4793
    if (v > UINT_MAX) {
 
4794
        if (ok)
 
4795
            *ok = false;
 
4796
        v = 0;
 
4797
    }
 
4798
    return (uint)v;
 
4799
}
 
4800
 
 
4801
/*!
 
4802
    Returns the string converted to a \c short using base \a
 
4803
    base, which is 10 by default and must be between 2 and 36, or 0.
 
4804
    Returns 0 if the conversion fails.
 
4805
 
 
4806
    If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
 
4807
    false; otherwise *\a{ok} is set to true.
 
4808
 
 
4809
    If \a base is 0, the C language convention is used: If the string
 
4810
    begins with "0x", base 16 is used; if the string begins with "0",
 
4811
    base 8 is used; otherwise, base 10 is used.
 
4812
 
 
4813
    Example:
 
4814
 
 
4815
    \code
 
4816
        QString str = "FF";
 
4817
        bool ok;
 
4818
        short hex = str.toShort(&ok, 16);   // hex == 255, ok == true
 
4819
        short dec = str.toShort(&ok, 10);   // dec == 0, ok == false
 
4820
    \endcode
 
4821
 
 
4822
    \sa number(), toUShort(), toInt()
 
4823
*/
 
4824
 
 
4825
short QString::toShort(bool *ok, int base) const
 
4826
{
 
4827
    long v = toLongLong(ok, base);
 
4828
    if (v < SHRT_MIN || v > SHRT_MAX) {
 
4829
        if (ok)
 
4830
            *ok = false;
 
4831
        v = 0;
 
4832
    }
 
4833
    return (short)v;
 
4834
}
 
4835
 
 
4836
/*!
 
4837
    Returns the string converted to an \c{unsigned short} using base \a
 
4838
    base, which is 10 by default and must be between 2 and 36, or 0.
 
4839
    Returns 0 if the conversion fails.
 
4840
 
 
4841
    If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
 
4842
    false; otherwise *\a{ok} is set to true.
 
4843
 
 
4844
    If \a base is 0, the C language convention is used: If the string
 
4845
    begins with "0x", base 16 is used; if the string begins with "0",
 
4846
    base 8 is used; otherwise, base 10 is used.
 
4847
 
 
4848
    Example:
 
4849
 
 
4850
    \code
 
4851
        QString str = "FF";
 
4852
        bool ok;
 
4853
        ushort hex = str.toUShort(&ok, 16);     // hex == 255, ok == true
 
4854
        ushort dec = str.toUShort(&ok, 10);     // dec == 0, ok == false
 
4855
    \endcode
 
4856
 
 
4857
    \sa number(), toShort()
 
4858
*/
 
4859
 
 
4860
ushort QString::toUShort(bool *ok, int base) const
 
4861
{
 
4862
    ulong v = toULongLong(ok, base);
 
4863
    if (v > USHRT_MAX) {
 
4864
        if (ok)
 
4865
            *ok = false;
 
4866
        v = 0;
 
4867
    }
 
4868
    return (ushort)v;
 
4869
}
 
4870
 
 
4871
 
 
4872
/*!
 
4873
    Returns the string converted to a \c double value.
 
4874
 
 
4875
    Returns 0.0 if the conversion fails.
 
4876
 
 
4877
    If \a ok is not 0: if a conversion error occurs, \c{*}\a{ok} is set to
 
4878
    false; otherwise \c{*}\a{ok} is set to true.
 
4879
 
 
4880
    \code
 
4881
        QString str = "1234.56";
 
4882
        double val = str.toDouble();   // val == 1234.56
 
4883
    \endcode
 
4884
 
 
4885
    This function tries to interpret the string according to the
 
4886
    current locale. The current locale is determined from the
 
4887
    system at application startup and can be changed by calling
 
4888
    QLocale::setDefault(). If the string cannot be interpreted
 
4889
    according to the current locale, this function falls back
 
4890
    on the "C" locale.
 
4891
 
 
4892
    \code
 
4893
        bool ok;
 
4894
        double d;
 
4895
 
 
4896
        QLocale::setDefault(QLocale::C);
 
4897
        d = QString( "1234,56" ).toDouble(&ok); // ok == false
 
4898
        d = QString( "1234.56" ).toDouble(&ok); // ok == true, d == 1234.56
 
4899
 
 
4900
        QLocale::setDefault(QLocale::German);
 
4901
        d = QString( "1234,56" ).toDouble(&ok); // ok == true, d == 1234.56
 
4902
        d = QString( "1234.56" ).toDouble(&ok); // ok == true, d == 1234.56
 
4903
    \endcode
 
4904
 
 
4905
    Due to the ambiguity between the decimal point and thousands group
 
4906
    separator in various locales, this function does not handle
 
4907
    thousands group separators. If you need to convert such numbers,
 
4908
    see QLocale::toDouble().
 
4909
 
 
4910
    \code
 
4911
        bool ok;
 
4912
        QLocale::setDefault(QLocale::C);
 
4913
        double d = QString( "1,234,567.89" ).toDouble(&ok); // ok == false
 
4914
    \endcode
 
4915
 
 
4916
    \warning If the string contains trailing whitespace this function
 
4917
    will fail, and set \c{*}\a{ok} to false if \a ok is not 0. Leading
 
4918
    whitespace is ignored.
 
4919
 
 
4920
    \sa number() QLocale::setDefault() QLocale::toDouble() trimmed()
 
4921
*/
 
4922
 
 
4923
double QString::toDouble(bool *ok) const
 
4924
{
 
4925
    bool my_ok;
 
4926
    QLocale def_locale;
 
4927
    double result = def_locale.d->stringToDouble(*this, &my_ok, QLocalePrivate::FailOnGroupSeparators);
 
4928
    if (my_ok) {
 
4929
        if (ok != 0)
 
4930
            *ok = true;
 
4931
        return result;
 
4932
    }
 
4933
 
 
4934
    // If the default was not "C", try the "C" locale
 
4935
    if (def_locale.language() == QLocale::C) {
 
4936
        if (ok != 0)
 
4937
            *ok = false;
 
4938
        return 0.0;
 
4939
    }
 
4940
 
 
4941
    QLocale c_locale(QLocale::C);
 
4942
    return c_locale.d->stringToDouble(*this, ok, QLocalePrivate::FailOnGroupSeparators);
 
4943
}
 
4944
 
 
4945
/*!
 
4946
    Returns the string converted to a \c float value.
 
4947
 
 
4948
    Returns 0.0 if the conversion fails.
 
4949
 
 
4950
    If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
 
4951
    false; otherwise *\a{ok} is set to true.
 
4952
 
 
4953
    Example:
 
4954
    \code
 
4955
        QString str1 = "1234.56";
 
4956
        str1.toFloat();             // returns 1234.56
 
4957
 
 
4958
        bool ok;
 
4959
        QString str2 = "R2D2";
 
4960
        str2.toFloat(&ok);          // returns 0.0, sets ok to false
 
4961
    \endcode
 
4962
 
 
4963
    \sa number(), toDouble(), toInt()
 
4964
*/
 
4965
 
 
4966
#define QT_MAX_FLOAT 3.4028234663852886e+38
 
4967
 
 
4968
float QString::toFloat(bool *ok) const
 
4969
{
 
4970
    bool myOk;
 
4971
    double d = toDouble(&myOk);
 
4972
    if (!myOk || d > QT_MAX_FLOAT || d < -QT_MAX_FLOAT) {
 
4973
        if (ok != 0)
 
4974
            *ok = false;
 
4975
        return 0.0;
 
4976
    }
 
4977
    if (ok != 0)
 
4978
        *ok = true;
 
4979
    return (float) d;
 
4980
}
 
4981
 
 
4982
/*! \fn QString &QString::setNum(int n, int base)
 
4983
 
 
4984
    Sets the string to the printed value of \a n in base \a base and
 
4985
    returns a reference to the string.
 
4986
 
 
4987
    The base is 10 by default and must be between 2 and 36.
 
4988
 
 
4989
    \code
 
4990
        QString str;
 
4991
        str.setNum(1234);       // str == "1234"
 
4992
    \endcode
 
4993
*/
 
4994
 
 
4995
/*! \fn QString &QString::setNum(uint n, int base)
 
4996
 
 
4997
    \overload
 
4998
*/
 
4999
 
 
5000
/*! \fn QString &QString::setNum(long n, int base)
 
5001
 
 
5002
    \overload
 
5003
*/
 
5004
 
 
5005
/*! \fn QString &QString::setNum(ulong n, int base)
 
5006
 
 
5007
    \overload
 
5008
*/
 
5009
 
 
5010
/*!
 
5011
    \overload
 
5012
*/
 
5013
QString &QString::setNum(qlonglong n, int base)
 
5014
{
 
5015
#if defined(QT_CHECK_RANGE)
 
5016
    if (base < 2 || base > 36) {
 
5017
        qWarning("QString::setNum: Invalid base %d", base);
 
5018
        base = 10;
 
5019
    }
 
5020
#endif
 
5021
    QLocale locale(QLocale::C);
 
5022
    *this = locale.d->longLongToString(n, -1, base);
 
5023
    return *this;
 
5024
}
 
5025
 
 
5026
/*!
 
5027
    \overload
 
5028
*/
 
5029
QString &QString::setNum(qulonglong n, int base)
 
5030
{
 
5031
#if defined(QT_CHECK_RANGE)
 
5032
    if (base < 2 || base > 36) {
 
5033
        qWarning("QString::setNum: Invalid base %d", base);
 
5034
        base = 10;
 
5035
    }
 
5036
#endif
 
5037
    QLocale locale(QLocale::C);
 
5038
    *this = locale.d->unsLongLongToString(n, -1, base);
 
5039
    return *this;
 
5040
}
 
5041
 
 
5042
/*! \fn QString &QString::setNum(short n, int base)
 
5043
 
 
5044
    \overload
 
5045
*/
 
5046
 
 
5047
/*! \fn QString &QString::setNum(ushort n, int base)
 
5048
 
 
5049
    \overload
 
5050
*/
 
5051
 
 
5052
/*!
 
5053
    \overload
 
5054
 
 
5055
    Sets the string to the printed value of \a n, formatted using
 
5056
    format \a f with precision \a prec, and returns a reference to the
 
5057
    string.
 
5058
 
 
5059
    The format \a f can be 'f', 'F', 'e', 'E', 'g' or 'G'.
 
5060
    See \l{#arg-formats}{arg()} for an explanation of the formats.
 
5061
*/
 
5062
 
 
5063
QString &QString::setNum(double n, char f, int prec)
 
5064
{
 
5065
    QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
 
5066
    uint flags = 0;
 
5067
 
 
5068
    if (qIsUpper(f))
 
5069
        flags = QLocalePrivate::CapitalEorX;
 
5070
    f = qToLower(f);
 
5071
 
 
5072
    switch (f) {
 
5073
        case 'f':
 
5074
            form = QLocalePrivate::DFDecimal;
 
5075
            break;
 
5076
        case 'e':
 
5077
            form = QLocalePrivate::DFExponent;
 
5078
            break;
 
5079
        case 'g':
 
5080
            form = QLocalePrivate::DFSignificantDigits;
 
5081
            break;
 
5082
        default:
 
5083
#if defined(QT_CHECK_RANGE)
 
5084
            qWarning("QString::setNum: Invalid format char '%c'", f);
 
5085
#endif
 
5086
            break;
 
5087
    }
 
5088
 
 
5089
    QLocale locale(QLocale::C);
 
5090
    *this = locale.d->doubleToString(n, prec, form, -1, flags);
 
5091
    return *this;
 
5092
}
 
5093
 
 
5094
/*!
 
5095
    \fn QString &QString::setNum(float n, char f, int prec)
 
5096
 
 
5097
    \overload
 
5098
 
 
5099
    Sets the string to the printed value of \a n, formatted in format
 
5100
    \a f with precision \a prec, and returns a reference to the
 
5101
    string.
 
5102
 
 
5103
    The format \a f can be 'f', 'F', 'e', 'E', 'g' or 'G'.
 
5104
    See \l{#arg-formats}{arg()} for an explanation of the formats.
 
5105
*/
 
5106
 
 
5107
 
 
5108
/*!
 
5109
  \fn QString QString::number(long n, int base)
 
5110
 
 
5111
    Returns a string equivalent of the number \a n to base \a base,
 
5112
    which is 10 by default and must be between 2 and 36.
 
5113
 
 
5114
    \code
 
5115
        long a = 63;
 
5116
        QString str = QString::number(a, 16);             // str == "3f"
 
5117
        QString str = QString::number(a, 16).upper();     // str == "3F"
 
5118
    \endcode
 
5119
 
 
5120
    \sa setNum()
 
5121
*/
 
5122
 
 
5123
QString QString::number(long n, int base)
 
5124
{
 
5125
    QString s;
 
5126
    s.setNum(n, base);
 
5127
    return s;
 
5128
}
 
5129
 
 
5130
/*!
 
5131
  \fn QString QString::number(ulong n, int base)
 
5132
 
 
5133
    \overload
 
5134
*/
 
5135
QString QString::number(ulong n, int base)
 
5136
{
 
5137
    QString s;
 
5138
    s.setNum(n, base);
 
5139
    return s;
 
5140
}
 
5141
 
 
5142
/*!
 
5143
    \overload
 
5144
*/
 
5145
QString QString::number(int n, int base)
 
5146
{
 
5147
    QString s;
 
5148
    s.setNum(n, base);
 
5149
    return s;
 
5150
}
 
5151
 
 
5152
/*!
 
5153
    \overload
 
5154
*/
 
5155
QString QString::number(uint n, int base)
 
5156
{
 
5157
    QString s;
 
5158
    s.setNum(n, base);
 
5159
    return s;
 
5160
}
 
5161
 
 
5162
/*!
 
5163
    \overload
 
5164
*/
 
5165
QString QString::number(qlonglong n, int base)
 
5166
{
 
5167
    QString s;
 
5168
    s.setNum(n, base);
 
5169
    return s;
 
5170
}
 
5171
 
 
5172
/*!
 
5173
    \overload
 
5174
*/
 
5175
QString QString::number(qulonglong n, int base)
 
5176
{
 
5177
    QString s;
 
5178
    s.setNum(n, base);
 
5179
    return s;
 
5180
}
 
5181
 
 
5182
 
 
5183
/*!
 
5184
    \overload
 
5185
 
 
5186
    Argument \a n is formatted according to the format \a f, and the
 
5187
    precision \a prec. The format \a f can be 'f', 'F', 'e', 'E', 'g'
 
5188
    or 'G'. See \l{#arg-formats}{arg()} for an explanation of the
 
5189
    formats.
 
5190
 
 
5191
    \sa setNum()
 
5192
*/
 
5193
QString QString::number(double n, char f, int prec)
 
5194
{
 
5195
    QString s;
 
5196
    s.setNum(n, f, prec);
 
5197
    return s;
 
5198
}
 
5199
 
 
5200
/*!
 
5201
    Splits the string into substrings wherever \a sep occurs, and
 
5202
    returns the list of those strings. If \a sep does not match
 
5203
    anywhere in the string, split() returns a single-element list
 
5204
    containing this string.
 
5205
 
 
5206
    If \a cs is true, the string is only split only where characters
 
5207
    are found that match \a sep exactly. If \a cs is false, the
 
5208
    string is split, the string is split where characters are found
 
5209
    that match \a sep case insensitively (e.g. "and" matches "AND").
 
5210
 
 
5211
    If \a behavior is QString::SkipEmptyParts, empty entries don't
 
5212
    appear in the result. By default, empty entries are kept.
 
5213
 
 
5214
    Example:
 
5215
    \code
 
5216
        QString str = "a,,b,c";
 
5217
        QStringList list1 = str.split(",");
 
5218
        // list1: [ "a", "", "b", "c" ]
 
5219
 
 
5220
        QStringList list2 = str.split(",", QString::SkipEmptyParts);
 
5221
        // list2: [ "a", "b", "c" ]
 
5222
    \endcode
 
5223
 
 
5224
    \sa QStringList::join(), section()
 
5225
*/
 
5226
QStringList QString::split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
 
5227
{
 
5228
    QStringList list;
 
5229
    int start = 0;
 
5230
    int extra = 0;
 
5231
    int end;
 
5232
    while ((end = indexOf(sep, start + extra, cs)) != -1) {
 
5233
        if (start != end || behavior == KeepEmptyParts)
 
5234
            list.append(mid(start, end - start));
 
5235
        start = end + sep.size();
 
5236
        extra = (sep.size() == 0 ? 1 : 0);
 
5237
    }
 
5238
    if (start != size() || behavior == KeepEmptyParts)
 
5239
        list.append(mid(start));
 
5240
    return list;
 
5241
}
 
5242
 
 
5243
/*!
 
5244
    \overload
 
5245
*/
 
5246
QStringList QString::split(const QChar &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
 
5247
{
 
5248
    QStringList list;
 
5249
    int start = 0;
 
5250
    int end;
 
5251
    while ((end = indexOf(sep, start, cs)) != -1) {
 
5252
        if (start != end || behavior == KeepEmptyParts)
 
5253
            list.append(mid(start, end - start));
 
5254
        start = end + 1;
 
5255
    }
 
5256
    if (start != size() || behavior == KeepEmptyParts)
 
5257
        list.append(mid(start));
 
5258
    return list;
 
5259
}
 
5260
 
 
5261
#ifndef QT_NO_REGEXP
 
5262
/*!
 
5263
    \overload
 
5264
 
 
5265
    Splits the string into substrings wherever the regular expression
 
5266
    \a rx matches, and returns the list of those strings. If \a rx
 
5267
    does not match anywhere in the string, split() returns a
 
5268
    single-element list containing this string.
 
5269
 
 
5270
    Here's an example where we extract the words in a sentence
 
5271
    using one or more whitespace characters as the separator:
 
5272
 
 
5273
    \code
 
5274
        QString str = "Some  text\n\twith  strange whitespace.";
 
5275
        QStringList list = str.split(QRegExp("\\s+"));
 
5276
        // list: [ "Some", "text", "with", "strange", "whitespace." ]
 
5277
    \endcode
 
5278
 
 
5279
    Here's a similar example, but this time we use any sequence of
 
5280
    non-word characters as the separator:
 
5281
 
 
5282
    \code
 
5283
        QString str = "This time, a normal English sentence.";
 
5284
        QStringList list = str.split(QRegExp("\\W+"),
 
5285
                                     QString::SkipEmptyParts);
 
5286
        // list: [ "This", "time", "a", "normal", "English", "sentence" ]
 
5287
    \endcode
 
5288
 
 
5289
    Here's a third example where we use a zero-length assertion,
 
5290
    \bold{\\b} (word boundary), to split the string into an
 
5291
    alternating sequence of non-word and word tokens:
 
5292
 
 
5293
    \code
 
5294
        QString str = "Now: this sentence fragment.";
 
5295
        QStringList list = str.split(QRegExp("\\b"));
 
5296
        // list: [ "", "Now", ": ", "this", " ", "sentence", " ", "fragment", "." ]
 
5297
    \endcode
 
5298
 
 
5299
    \sa QStringList::join(), section()
 
5300
*/
 
5301
QStringList QString::split(const QRegExp &rx, SplitBehavior behavior) const
 
5302
{
 
5303
    QStringList list;
 
5304
    int start = 0;
 
5305
    int extra = 0;
 
5306
    int end;
 
5307
    while ((end = indexOf(rx, start + extra)) != -1) {
 
5308
        int matchedLen = rx.matchedLength();
 
5309
        if (start != end || behavior == KeepEmptyParts)
 
5310
            list.append(mid(start, end - start));
 
5311
        start = end + matchedLen;
 
5312
        extra = (matchedLen == 0) ? 1 : 0;
 
5313
    }
 
5314
    if (start != size() || behavior == KeepEmptyParts)
 
5315
        list.append(mid(start));
 
5316
    return list;
 
5317
}
 
5318
#endif
 
5319
 
 
5320
/*!
 
5321
    \enum QString::NormalizationForm
 
5322
 
 
5323
    \value NormalizationForm_D  Canonical Decomposition
 
5324
    \value NormalizationForm_C  Canonical Decomposition followed by Canonical Composition
 
5325
    \value NormalizationForm_KD  Compatibility Decomposition
 
5326
    \value NormalizationForm_KC  Compatibility Decomposition followed by Canonical Composition
 
5327
 
 
5328
    \sa normalized(),
 
5329
        {http://www.unicode.org/reports/tr15/}{Unicode Standard Annex #15}
 
5330
*/
 
5331
 
 
5332
/*!
 
5333
    Returns the string in the given Unicode normalization \a form.
 
5334
*/
 
5335
QString QString::normalized(NormalizationForm form) const
 
5336
{
 
5337
    return QUnicodeTables::normalize(*this, form);
 
5338
}
 
5339
 
 
5340
/*!
 
5341
    \overload
 
5342
 
 
5343
    Returns the string in the given Unicode normalization \a form,
 
5344
    according to the given \a version of the Unicode standard.
 
5345
*/
 
5346
QString QString::normalized(NormalizationForm form, QChar::UnicodeVersion version) const
 
5347
{
 
5348
    return QUnicodeTables::normalize(*this, form, version);
 
5349
}
 
5350
 
 
5351
struct ArgEscapeData
 
5352
{
 
5353
    int min_escape;            // lowest escape sequence number
 
5354
    int occurrences;           // number of occurrences of the lowest escape sequence number
 
5355
    int locale_occurrences;    // number of occurrences of the lowest escape sequence number that
 
5356
                               // contain 'L'
 
5357
    int escape_len;            // total length of escape sequences which will be replaced
 
5358
};
 
5359
 
 
5360
static ArgEscapeData findArgEscapes(const QString &s)
 
5361
{
 
5362
    const QChar *uc_begin = s.unicode();
 
5363
    const QChar *uc_end = uc_begin + s.length();
 
5364
 
 
5365
    ArgEscapeData d;
 
5366
 
 
5367
    d.min_escape = INT_MAX;
 
5368
    d.occurrences = 0;
 
5369
    d.escape_len = 0;
 
5370
    d.locale_occurrences = 0;
 
5371
 
 
5372
    const QChar *c = uc_begin;
 
5373
    while (c != uc_end) {
 
5374
        while (c != uc_end && c->unicode() != '%')
 
5375
            ++c;
 
5376
 
 
5377
        if (c == uc_end)
 
5378
            break;
 
5379
        const QChar *escape_start = c;
 
5380
        if (++c == uc_end)
 
5381
            break;
 
5382
 
 
5383
        bool locale_arg = false;
 
5384
        if (c->unicode() == 'L') {
 
5385
            locale_arg = true;
 
5386
            if (++c == uc_end)
 
5387
                break;
 
5388
        }
 
5389
 
 
5390
        if (c->digitValue() == -1)
 
5391
            continue;
 
5392
 
 
5393
        int escape = c->digitValue();
 
5394
        ++c;
 
5395
 
 
5396
        if (c != uc_end && c->digitValue() != -1) {
 
5397
            escape = (10 * escape) + c->digitValue();
 
5398
            ++c;
 
5399
        }
 
5400
 
 
5401
        if (escape > d.min_escape)
 
5402
            continue;
 
5403
 
 
5404
        if (escape < d.min_escape) {
 
5405
            d.min_escape = escape;
 
5406
            d.occurrences = 0;
 
5407
            d.escape_len = 0;
 
5408
            d.locale_occurrences = 0;
 
5409
        }
 
5410
 
 
5411
        ++d.occurrences;
 
5412
        if (locale_arg)
 
5413
            ++d.locale_occurrences;
 
5414
        d.escape_len += c - escape_start;
 
5415
    }
 
5416
    return d;
 
5417
}
 
5418
 
 
5419
static QString replaceArgEscapes(const QString &s, const ArgEscapeData &d, int field_width,
 
5420
                                 const QString &arg, const QString &larg, const QChar &fillChar = QLatin1Char(' '))
 
5421
{
 
5422
    const QChar *uc_begin = s.unicode();
 
5423
    const QChar *uc_end = uc_begin + s.length();
 
5424
 
 
5425
    int abs_field_width = qAbs(field_width);
 
5426
    int result_len = s.length()
 
5427
                     - d.escape_len
 
5428
                     + (d.occurrences - d.locale_occurrences)
 
5429
                     *qMax(abs_field_width, arg.length())
 
5430
                     + d.locale_occurrences
 
5431
                     *qMax(abs_field_width, larg.length());
 
5432
 
 
5433
    QString result;
 
5434
    result.resize(result_len);
 
5435
    QChar *result_buff = (QChar*) result.unicode();
 
5436
 
 
5437
    QChar *rc = result_buff;
 
5438
    const QChar *c = uc_begin;
 
5439
    int repl_cnt = 0;
 
5440
    while (c != uc_end) {
 
5441
        /* We don't have to check if we run off the end of the string with c,
 
5442
           because as long as d.occurrences > 0 we KNOW there are valid escape
 
5443
           sequences. */
 
5444
 
 
5445
        const QChar *text_start = c;
 
5446
 
 
5447
        while (c->unicode() != '%')
 
5448
            ++c;
 
5449
 
 
5450
        const QChar *escape_start = c++;
 
5451
 
 
5452
        bool locale_arg = false;
 
5453
        if (c->unicode() == 'L') {
 
5454
            locale_arg = true;
 
5455
            ++c;
 
5456
        }
 
5457
 
 
5458
        int escape = c->digitValue();
 
5459
        if (escape != -1) {
 
5460
            if (c + 1 != uc_end && (c + 1)->digitValue() != -1) {
 
5461
                escape = (10 * escape) + (c + 1)->digitValue();
 
5462
                ++c;
 
5463
            }
 
5464
        }
 
5465
 
 
5466
        if (escape != d.min_escape) {
 
5467
            memcpy(rc, text_start, (c - text_start)*sizeof(QChar));
 
5468
            rc += c - text_start;
 
5469
        }
 
5470
        else {
 
5471
            ++c;
 
5472
 
 
5473
            memcpy(rc, text_start, (escape_start - text_start)*sizeof(QChar));
 
5474
            rc += escape_start - text_start;
 
5475
 
 
5476
            uint pad_chars;
 
5477
            if (locale_arg)
 
5478
                pad_chars = qMax(abs_field_width, larg.length()) - larg.length();
 
5479
            else
 
5480
                pad_chars = qMax(abs_field_width, arg.length()) - arg.length();
 
5481
 
 
5482
            if (field_width > 0) { // left padded
 
5483
                for (uint i = 0; i < pad_chars; ++i)
 
5484
                    (rc++)->unicode() = fillChar.unicode();
 
5485
            }
 
5486
 
 
5487
            if (locale_arg) {
 
5488
                memcpy(rc, larg.unicode(), larg.length()*sizeof(QChar));
 
5489
                rc += larg.length();
 
5490
            }
 
5491
            else {
 
5492
                memcpy(rc, arg.unicode(), arg.length()*sizeof(QChar));
 
5493
                rc += arg.length();
 
5494
            }
 
5495
 
 
5496
            if (field_width < 0) { // right padded
 
5497
                for (uint i = 0; i < pad_chars; ++i)
 
5498
                    (rc++)->unicode() = fillChar.unicode();
 
5499
            }
 
5500
 
 
5501
            if (++repl_cnt == d.occurrences) {
 
5502
                memcpy(rc, c, (uc_end - c)*sizeof(QChar));
 
5503
                rc += uc_end - c;
 
5504
                Q_ASSERT(rc - result_buff == result_len);
 
5505
                c = uc_end;
 
5506
            }
 
5507
        }
 
5508
    }
 
5509
    Q_ASSERT(rc == result_buff + result_len);
 
5510
 
 
5511
    return result;
 
5512
}
 
5513
 
 
5514
/*!
 
5515
    This function returns a copy of this string where \a a replaces
 
5516
    the lowest numbered occurrence of \c %1, \c %2, ..., \c %9.
 
5517
 
 
5518
    The \a fieldWidth value specifies the minimum amount of space that
 
5519
    \a a is padded to and filled with the character \a fillChar. A
 
5520
    positive value will produce right-aligned text, whereas a negative
 
5521
    value will produce left-aligned text.
 
5522
 
 
5523
    The following example shows how we could create a 'status' string
 
5524
    when processing a list of files:
 
5525
 
 
5526
    \code
 
5527
        QString status = QString("Processing file %1 of %2: %3")
 
5528
                            .arg(i)         // current file's number
 
5529
                            .arg(total)     // number of files to process
 
5530
                            .arg(fileName); // current file's name
 
5531
    \endcode
 
5532
 
 
5533
    It is generally fine to use file names and numbers as we have
 
5534
    done in the example above. But note that using arg() to construct
 
5535
    natural language sentences does not usually translate well into
 
5536
    other languages because sentence structure and word order often
 
5537
    differ between languages.
 
5538
 
 
5539
    If there is no place marker (\c %1, \c %2, etc.), a warning
 
5540
    message is output and the result is undefined.
 
5541
*/
 
5542
QString QString::arg(const QString &a, int fieldWidth, const QChar &fillChar) const
 
5543
{
 
5544
    ArgEscapeData d = findArgEscapes(*this);
 
5545
 
 
5546
    if (d.occurrences == 0) {
 
5547
        qWarning("QString::arg(): Argument missing: %s, %s", toLocal8Bit().data(),
 
5548
                  a.toLocal8Bit().data());
 
5549
        return *this;
 
5550
    }
 
5551
    return replaceArgEscapes(*this, d, fieldWidth, a, a, fillChar);
 
5552
}
 
5553
 
 
5554
/*!
 
5555
    \fn QString QString::arg(const QString& a1, const QString& a2) const
 
5556
    \overload
 
5557
 
 
5558
    This is the same as str.arg(\a a1).arg(\a a2), except that
 
5559
    the strings are replaced in one pass. This can make a difference
 
5560
    if \a a1 contains e.g. \c{%1}:
 
5561
 
 
5562
    \code
 
5563
        QString str = "%1 %2";
 
5564
        str.arg("%1f", "Hello");        // returns "%1f Hello"
 
5565
        str.arg("%1f").arg("Hello");    // returns "Hellof"
 
5566
    \endcode
 
5567
*/
 
5568
 
 
5569
/*!
 
5570
    \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3) const
 
5571
    \overload
 
5572
 
 
5573
    This is the same as calling str.arg(\a a1).arg(\a a2).arg(\a a3),
 
5574
    except that the strings are replaced in one pass.
 
5575
*/
 
5576
 
 
5577
/*!
 
5578
    \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4) const
 
5579
    \overload
 
5580
 
 
5581
    This is the same as calling str.arg(\a a1).arg(\a a2).arg(\a
 
5582
    a3).arg(\a a4), except that the strings are replaced in one pass.
 
5583
*/
 
5584
 
 
5585
/*! \fn QString QString::arg(int a, int fieldWidth, int base, const QChar &fillChar) const
 
5586
 
 
5587
    \overload
 
5588
 
 
5589
    \a a is expressed in base \a base, which is 10 by default and must
 
5590
    be between 2 and 36.
 
5591
 
 
5592
    The \a fieldWidth value specifies the minimum amount of space that
 
5593
    \a a is padded to and filled with the character \a fillChar. A
 
5594
    positive value will produce a right-aligned number, whereas a
 
5595
    negative value will produce a left-aligned number.
 
5596
 
 
5597
    The '%' can be followed by an 'L', in which case the sequence is
 
5598
    replaced with a localized representation of \a a. The conversion uses
 
5599
    the default locale, set by QLocale::setDefaultLocale(). If no default
 
5600
    locale was specified, the "C" locale is used. The 'L' flag is ignored
 
5601
    if \a base is not 10.
 
5602
 
 
5603
    \code
 
5604
        QString str;
 
5605
        str = QString("Decimal 63 is %1 in hexadecimal")
 
5606
                .arg(63, 0, 16);
 
5607
        // str == "Decimal 63 is 3f in hexadecimal"
 
5608
 
 
5609
        QLocale::setDefaultLocale(QLocale::English, QLocale::UnitedStates);
 
5610
        str = QString("%1 %L2 %L3")
 
5611
                .arg(12345)
 
5612
                .arg(12345)
 
5613
                .arg(12345, 0, 16);
 
5614
        // str == "12345 12,345 3039"
 
5615
    \endcode
 
5616
*/
 
5617
 
 
5618
/*! \fn QString QString::arg(uint a, int fieldWidth, int base, const QChar &fillChar) const
 
5619
 
 
5620
    \overload
 
5621
 
 
5622
    \a base is the base to use when converting the integer \a a into a
 
5623
    string. \a base must be between 2 and 36, with 8 giving octal, 10
 
5624
    decimal, and 16 hexadecimal numbers.
 
5625
*/
 
5626
 
 
5627
/*! \fn QString QString::arg(long a, int fieldWidth, int base, const QChar &fillChar) const
 
5628
 
 
5629
    \overload
 
5630
 
 
5631
    The \a fieldWidth value specifies the minimum amount of space that
 
5632
    \a a is padded to and filled with the character \a fillChar. A
 
5633
    positive value will produce a right-aligned number, whereas a
 
5634
    negative value will produce a left-aligned number.
 
5635
 
 
5636
    \a a is expressed in base \a base, which is 10 by default and must
 
5637
    be between 2 and 36.
 
5638
 
 
5639
    The '%' can be followed by an 'L', in which case the sequence is
 
5640
    replaced with a localized representation of \a a. The conversion
 
5641
    uses the default locale. The default locale is determined from the
 
5642
    system's locale settings at application startup. It can be changed
 
5643
    using QLocale::setDefault(). The 'L' flag is ignored if \a base is
 
5644
    not 10.
 
5645
 
 
5646
    \code
 
5647
        QString str;
 
5648
        str = QString( "Decimal 63 is %1 in hexadecimal" )
 
5649
                .arg( 63, 0, 16 );
 
5650
        // str == "Decimal 63 is 3f in hexadecimal"
 
5651
 
 
5652
        QLocale::setDefault(QLocale::English, QLocale::UnitedStates);
 
5653
        str = QString( "%1 %L2 %L3" )
 
5654
                .arg( 12345 )
 
5655
                .arg( 12345 )
 
5656
                .arg( 12345, 0, 16 );
 
5657
        // str == "12345 12,345 3039"
 
5658
    \endcode
 
5659
*/
 
5660
 
 
5661
/*! \fn QString QString::arg(ulong a, int fieldWidth, int base, const QChar &fillChar) const
 
5662
 
 
5663
    \overload
 
5664
 
 
5665
    \a base is the base to use when converting the integer \a a into a
 
5666
    string. \a base must be between 2 and 36, with 8 giving octal, 10
 
5667
    decimal, and 16 hexadecimal numbers.
 
5668
*/
 
5669
 
 
5670
/*!
 
5671
    \overload
 
5672
 
 
5673
    \a base is the base to use when converting the integer \a a into a
 
5674
    string. \a base must be between 2 and 36, with 8 giving octal, 10
 
5675
    decimal, and 16 hexadecimal numbers.
 
5676
*/
 
5677
QString QString::arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
 
5678
{
 
5679
    ArgEscapeData d = findArgEscapes(*this);
 
5680
 
 
5681
    if (d.occurrences == 0) {
 
5682
        qWarning("QString::arg(): Argument missing: %s, %lld", toLocal8Bit().data(), a);
 
5683
        return *this;
 
5684
    }
 
5685
 
 
5686
    QString arg;
 
5687
    if (d.occurrences > d.locale_occurrences)
 
5688
        arg = number(a, base);
 
5689
 
 
5690
    QString locale_arg;
 
5691
    if (d.locale_occurrences > 0) {
 
5692
        QLocale locale;
 
5693
        locale_arg = locale.d->longLongToString(a, -1, base, -1, QLocalePrivate::ThousandsGroup);
 
5694
    }
 
5695
 
 
5696
    return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg, fillChar);
 
5697
}
 
5698
 
 
5699
/*!
 
5700
    \overload
 
5701
 
 
5702
    \a base is the base to use when converting the integer \a a into a
 
5703
    string. \a base must be between 2 and 36, with 8 giving octal, 10
 
5704
    decimal, and 16 hexadecimal numbers.
 
5705
*/
 
5706
QString QString::arg(qulonglong a, int fieldWidth, int base, const QChar &fillChar) const
 
5707
{
 
5708
    ArgEscapeData d = findArgEscapes(*this);
 
5709
 
 
5710
    if (d.occurrences == 0) {
 
5711
        qWarning("QString::arg(): Argument missing: %s, %llu", toLocal8Bit().data(), a);
 
5712
        return *this;
 
5713
    }
 
5714
 
 
5715
    QString arg;
 
5716
    if (d.occurrences > d.locale_occurrences)
 
5717
        arg = number(a, base);
 
5718
 
 
5719
    QString locale_arg;
 
5720
    if (d.locale_occurrences > 0) {
 
5721
        QLocale locale;
 
5722
        locale_arg = locale.d->unsLongLongToString(a, -1, base, -1, QLocalePrivate::ThousandsGroup);
 
5723
    }
 
5724
 
 
5725
    return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg, fillChar);
 
5726
}
 
5727
 
 
5728
/*!
 
5729
    \fn QString QString::arg(short a, int fieldWidth, int base, const QChar &fillChar) const
 
5730
 
 
5731
    \overload
 
5732
 
 
5733
    \a base is the base to use when converting the integer \a a into a
 
5734
    string. \a base must be between 2 and 36, with 8 giving octal, 10
 
5735
    decimal, and 16 hexadecimal numbers.
 
5736
*/
 
5737
 
 
5738
/*!
 
5739
    \fn QString QString::arg(ushort a, int fieldWidth, int base, const QChar &fillChar) const
 
5740
 
 
5741
    \overload
 
5742
 
 
5743
    \a base is the base to use when converting the integer \a a into a
 
5744
    string. \a base must be between 2 and 36, with 8 giving octal, 10
 
5745
    decimal, and 16 hexadecimal numbers.
 
5746
*/
 
5747
 
 
5748
/*!
 
5749
    \overload
 
5750
*/
 
5751
QString QString::arg(QChar a, int fieldWidth, const QChar &fillChar) const
 
5752
{
 
5753
    QString c;
 
5754
    c += a;
 
5755
    return arg(c, fieldWidth, fillChar);
 
5756
}
 
5757
 
 
5758
/*!
 
5759
    \overload
 
5760
 
 
5761
    \a a is interpreded as a Latin-1 character.
 
5762
*/
 
5763
QString QString::arg(char a, int fieldWidth, const QChar &fillChar) const
 
5764
{
 
5765
    QString c;
 
5766
    c += QLatin1Char(a);
 
5767
    return arg(c, fieldWidth, fillChar);
 
5768
}
 
5769
 
 
5770
/*!
 
5771
    \overload
 
5772
 
 
5773
    \target arg-formats
 
5774
 
 
5775
    Argument \a a is formatted according to the \a fmt format specified,
 
5776
    which is 'g' by default and can be any of the following:
 
5777
 
 
5778
    \table
 
5779
    \header \i Format \i Meaning
 
5780
    \row \i \c e \i format as [-]9.9e[+|-]999
 
5781
    \row \i \c E \i format as [-]9.9E[+|-]999
 
5782
    \row \i \c f \i format as [-]9.9
 
5783
    \row \i \c g \i use \c e or \c f format, whichever is the most concise
 
5784
    \row \i \c G \i use \c E or \c f format, whichever is the most concise
 
5785
    \endtable
 
5786
 
 
5787
    With 'e', 'E', and 'f', \a prec is the number of digits after the
 
5788
    decimal point. With 'g' and 'G', \a prec is the maximum number of
 
5789
    significant digits (trailing zeroes are omitted).
 
5790
 
 
5791
    \code
 
5792
        double d = 12.34;
 
5793
        QString str = QString("delta: %1").arg(d, 0, 'E', 3);
 
5794
        // str == "delta: 1.234E+01"
 
5795
    \endcode
 
5796
 
 
5797
    The '%' can be followed by an 'L', in which case the sequence is
 
5798
    replaced with a localized representation of \a a. The conversion uses
 
5799
    the default locale, set by QLocale::setDefaultLocale(). If no default
 
5800
    locale was specified, the "C" locale is used.
 
5801
*/
 
5802
QString QString::arg(double a, int fieldWidth, char fmt, int prec, const QChar &fillChar) const
 
5803
{
 
5804
    ArgEscapeData d = findArgEscapes(*this);
 
5805
 
 
5806
    if (d.occurrences == 0) {
 
5807
        qWarning("QString::arg(): Argument missing: %s, %g", toLocal8Bit().data(), a);
 
5808
        return *this;
 
5809
    }
 
5810
 
 
5811
    QString arg;
 
5812
    if (d.occurrences > d.locale_occurrences)
 
5813
        arg = number(a, fmt, prec);
 
5814
 
 
5815
    QString locale_arg;
 
5816
    if (d.locale_occurrences > 0) {
 
5817
        QLocale locale;
 
5818
 
 
5819
        QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
 
5820
        uint flags = 0;
 
5821
 
 
5822
        if (qIsUpper(fmt))
 
5823
            flags = QLocalePrivate::CapitalEorX;
 
5824
        fmt = qToLower(fmt);
 
5825
 
 
5826
        switch (fmt) {
 
5827
            case 'f':
 
5828
                form = QLocalePrivate::DFDecimal;
 
5829
                break;
 
5830
            case 'e':
 
5831
                form = QLocalePrivate::DFExponent;
 
5832
                break;
 
5833
            case 'g':
 
5834
                form = QLocalePrivate::DFSignificantDigits;
 
5835
                break;
 
5836
            default:
 
5837
#if defined(QT_CHECK_RANGE)
 
5838
                qWarning("QString::arg: Invalid format char '%c'", fmt);
 
5839
#endif
 
5840
                break;
 
5841
        }
 
5842
 
 
5843
        flags |= QLocalePrivate::ThousandsGroup;
 
5844
 
 
5845
        locale_arg = locale.d->doubleToString(a, prec, form, -1, flags);
 
5846
    }
 
5847
 
 
5848
    return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg, fillChar);
 
5849
}
 
5850
 
 
5851
 
 
5852
QString QString::multiArg(int numArgs, const QString **args) const
 
5853
{
 
5854
    QString result;
 
5855
    union {
 
5856
        int digitUsed[10];
 
5857
        int argForDigit[10];
 
5858
    };
 
5859
    const QChar *uc = (const QChar*) d->data;
 
5860
    const int len = d->size;
 
5861
    const int end = len - 1;
 
5862
    int lastDigit = -1;
 
5863
    int i;
 
5864
 
 
5865
    memset(digitUsed, 0, sizeof(digitUsed));
 
5866
 
 
5867
    for (i = 0; i < end; i++) {
 
5868
        if (uc[i] == QLatin1Char('%')) {
 
5869
            int digit = uc[i + 1].unicode() - '0';
 
5870
            if (digit >= 0 && digit <= 9)
 
5871
                digitUsed[digit]++;
 
5872
        }
 
5873
    }
 
5874
 
 
5875
    for (i = 0; i < numArgs; i++) {
 
5876
        do {
 
5877
            ++lastDigit;
 
5878
        } while (lastDigit < 10 && digitUsed[lastDigit] == 0);
 
5879
 
 
5880
        if (lastDigit == 10) {
 
5881
            qWarning("QString::arg(): Argument missing: %s, %s", toLocal8Bit().data(), args[i]->toLocal8Bit().data());
 
5882
            numArgs = i;
 
5883
            lastDigit = 9;
 
5884
            break;
 
5885
        }
 
5886
        argForDigit[lastDigit] = i;
 
5887
    }
 
5888
 
 
5889
    i = 0;
 
5890
    while (i < len) {
 
5891
        if (uc[i] == QLatin1Char('%') && i != end) {
 
5892
            int digit = uc[i + 1].unicode() - '0';
 
5893
            if (digit >= 0 && digit <= lastDigit) {
 
5894
                result += *args[argForDigit[digit]];
 
5895
                i += 2;
 
5896
                continue;
 
5897
            }
 
5898
        }
 
5899
        result += uc[i++];
 
5900
    }
 
5901
    return result;
 
5902
}
 
5903
 
 
5904
 
 
5905
 
 
5906
/*! \internal
 
5907
 */
 
5908
void QString::updateProperties() const
 
5909
{
 
5910
    ushort *p = d->data;
 
5911
    ushort *end = p + d->size;
 
5912
    d->simpletext = true;
 
5913
    while (p < end) {
 
5914
        ushort uc = *p;
 
5915
        // sort out regions of complex text formatting
 
5916
        if (uc > 0x058f && (uc < 0x1100 || uc > 0xfb0f)) {
 
5917
            d->simpletext = false;
 
5918
        }
 
5919
        p++;
 
5920
    }
 
5921
 
 
5922
    p = d->data;
 
5923
    d->righttoleft = false;
 
5924
    while (p < end) {
 
5925
        switch(::direction(*p))
 
5926
        {
 
5927
        case QChar::DirL:
 
5928
        case QChar::DirLRO:
 
5929
        case QChar::DirLRE:
 
5930
            goto end;
 
5931
        case QChar::DirR:
 
5932
        case QChar::DirAL:
 
5933
        case QChar::DirRLO:
 
5934
        case QChar::DirRLE:
 
5935
            d->righttoleft = true;
 
5936
            goto end;
 
5937
        default:
 
5938
            break;
 
5939
        }
 
5940
        ++p;
 
5941
    }
 
5942
 end:
 
5943
    d->clean = true;
 
5944
    return;
 
5945
}
 
5946
 
 
5947
/*! \fn bool QString::isSimpleText() const
 
5948
 
 
5949
    \internal
 
5950
*/
 
5951
 
 
5952
/*! \fn bool QString::isRightToLeft() const
 
5953
 
 
5954
    \internal
 
5955
*/
 
5956
 
 
5957
/*! \fn QChar *QString::data()
 
5958
 
 
5959
    Returns a pointer to the data stored in the QString. The pointer
 
5960
    can be used to access and modify the characters that compose the
 
5961
    string. For convenience, the data is '\\0'-terminated.
 
5962
 
 
5963
    The pointer remains valid as long as the string isn't modified
 
5964
    by other means.
 
5965
 
 
5966
    Example:
 
5967
    \code
 
5968
        QString str = "Hello world";
 
5969
        QChar *data = ba.data();
 
5970
        while (*data) {
 
5971
            cout << "[" + data->unicode() + "]" << endl;
 
5972
            ++data;
 
5973
        }
 
5974
    \endcode
 
5975
 
 
5976
    \sa constData(), operator[]()
 
5977
*/
 
5978
 
 
5979
/*! \fn const QChar *QString::data() const
 
5980
 
 
5981
    \overload
 
5982
*/
 
5983
 
 
5984
/*! \fn const QChar *QString::constData() const
 
5985
 
 
5986
    Returns a pointer to the data stored in the QString. The pointer
 
5987
    can be used to access the characters that compose the string. For
 
5988
    convenience, the data is '\\0'-terminated.
 
5989
 
 
5990
    The pointer remains valid as long as the string isn't modified.
 
5991
 
 
5992
    \sa data(), operator[]()
 
5993
*/
 
5994
 
 
5995
/*! \fn void QString::push_front(const QString &other)
 
5996
 
 
5997
    This function is provided for STL compatibility. It is equivalent
 
5998
    to prepend(\a other).
 
5999
*/
 
6000
 
 
6001
/*! \fn void QString::push_front(QChar ch)
 
6002
 
 
6003
    \overload
 
6004
 
 
6005
    Same as prepend(\a ch).
 
6006
*/
 
6007
 
 
6008
/*! \fn void QString::push_back(const QString &other)
 
6009
 
 
6010
    This function is provided for STL compatibility. It is equivalent
 
6011
    to append(\a other).
 
6012
*/
 
6013
 
 
6014
/*! \fn void QString::push_back(QChar ch)
 
6015
 
 
6016
    \overload
 
6017
 
 
6018
    Same as append(\a ch).
 
6019
*/
 
6020
 
 
6021
/*! \fn std::string QString::toStdString() const
 
6022
 
 
6023
    Returns a std::string object with the data contained in this
 
6024
    QString. The Unicode data is converted into 8-bit characters
 
6025
    using toAscii().
 
6026
 
 
6027
    This operator is mostly useful to pass a QString to a function
 
6028
    that accepts a std::string object.
 
6029
 
 
6030
    If the QString contains non-ASCII Unicode characters, using this
 
6031
    operator can lead to loss of information. You can disable this
 
6032
    operator by defining \c QT_NO_CAST_TO_ASCII when you compile your
 
6033
    applications. You then need to call toAscii() (or toLatin1() or
 
6034
    toUtf8() or toLocal8Bit()) explicitly if you want to convert the data
 
6035
    to \c{const char *} and pass the return value on to the
 
6036
    std::string constructor.
 
6037
 
 
6038
    This operator is only available if Qt is configured with STL
 
6039
    compabitility enabled.
 
6040
 
 
6041
    \sa toAscii(), toLatin1(), toUtf8(), toLocal8Bit()
 
6042
*/
 
6043
 
 
6044
/*!
 
6045
    Constructs a QString that uses the first \a size Unicode
 
6046
    characters in the array \a unicode. The data in \a unicode is \e
 
6047
    not copied. The caller must be able to guarantee that \a unicode
 
6048
    will not be deleted or modified as long as the QString (or an
 
6049
    unmodified copy of it) exists.
 
6050
 
 
6051
    Any attempts to modify the QString or copies of it will cause it
 
6052
    to create a deep copy of the data, ensuring that the raw data
 
6053
    isn't modified.
 
6054
 
 
6055
    Here's an example of how we can use a QRegExp on raw data in
 
6056
    memory without requiring to copy the data into a QString:
 
6057
 
 
6058
    \code
 
6059
        static const QChar unicode[] = {
 
6060
            0x005A, 0x007F, 0x00A4, 0x0060, 0x1009, 0x0020,
 
6061
            ...
 
6062
            0x0020
 
6063
        };
 
6064
        int size = sizeof(unicode) / sizeof(QChar);
 
6065
 
 
6066
        QString str = QString::fromRawData(unicode, size);
 
6067
        if (str.contains(QRegExp(pattern)))
 
6068
        ...
 
6069
    \endcode
 
6070
 
 
6071
    \warning A string created with fromRawData() is \e not
 
6072
    '\\0'-terminated, unless the raw data contains a '\\0' character
 
6073
    at position \a size. This means unicode() will \e not return a
 
6074
    '\\0'-terminated string (although utf16() does, at the cost of
 
6075
    copying the raw data).
 
6076
 
 
6077
    \sa fromUtf16()
 
6078
*/
 
6079
QString QString::fromRawData(const QChar *unicode, int size)
 
6080
{
 
6081
    Data *x = static_cast<Data *>(qMalloc(sizeof(Data)));
 
6082
    if (unicode) {
 
6083
        x->data = (ushort *)unicode;
 
6084
    } else {
 
6085
        x->data = x->array;
 
6086
        size = 0;
 
6087
    }
 
6088
    x->ref.init(1);
 
6089
    x->alloc = x->size = size;
 
6090
    *x->array = '\0';
 
6091
    x->clean = x->asciiCache = x->simpletext = x->righttoleft = 0;
 
6092
    return QString(x, 0);
 
6093
}
 
6094
 
 
6095
/*! \class QLatin1String
 
6096
    \brief The QLatin1String class provides a thin wrapper around an ASCII/Latin-1 encoded string literal.
 
6097
 
 
6098
    \ingroup text
 
6099
    \reentrant
 
6100
 
 
6101
    Many of QString's member functions are overloaded to accept
 
6102
    \c{const char *} instead of QString. This includes the copy
 
6103
    constructor, the assignment operator, the comparison operators,
 
6104
    and various other functions such as \link QString::insert()
 
6105
    insert() \endlink, \link QString::replace() replace()\endlink,
 
6106
    and \link QString::indexOf() indexOf()\endlink. These functions
 
6107
    are usually optimized to avoid constructing a QString object for
 
6108
    the \c{const char *} data. For example, assuming \c str is a
 
6109
    QString,
 
6110
 
 
6111
    \code
 
6112
        if (str == "auto" || str == "extern"
 
6113
                || str == "static" || str == "register") {
 
6114
            ...
 
6115
        }
 
6116
    \endcode
 
6117
 
 
6118
    is much faster than
 
6119
 
 
6120
    \code
 
6121
        if (str == QString("auto") || str == QString("extern")
 
6122
                || str == QString("static") || str == QString("register")) {
 
6123
            ...
 
6124
        }
 
6125
    \endcode
 
6126
 
 
6127
    because it doesn't construct four temporary QString objects and
 
6128
    make a deep copy of the character data.
 
6129
 
 
6130
    Applications that define \c QT_NO_CAST_FROM_ASCII (as explained
 
6131
    in the QString documentation) don't have access to QString's
 
6132
    \c{const char *} API. To provide an efficient way of specifying
 
6133
    constant Latin-1 strings, Qt provides the QLatin1String, which is
 
6134
    just a very thin wrapper around a \c{const char *}. Using
 
6135
    QLatin1String, the example code above becomes
 
6136
 
 
6137
    \code
 
6138
        if (str == QLatin1String("auto")
 
6139
                || str == QLatin1String("extern")
 
6140
                || str == QLatin1String("static")
 
6141
                || str == QLatin1String("register") {
 
6142
            ...
 
6143
        }
 
6144
    \endcode
 
6145
 
 
6146
    This is a bit longer to type, but it provides exactly the same
 
6147
    benefits as the first version of the code, and is faster than
 
6148
    converting the Latin-1 strings using QString::fromLatin1().
 
6149
 
 
6150
    Thanks to the QString(const QLatin1String &) constructor,
 
6151
    QLatin1String can be used everywhere a QString is expected. For example:
 
6152
 
 
6153
    \code
 
6154
        QLabel *label = new QLabel(QLatin1String("MOD"), this);
 
6155
    \endcode
 
6156
 
 
6157
    \sa QString, QLatin1Char
 
6158
*/
 
6159
 
 
6160
/*! \fn QLatin1String::QLatin1String(const char *str)
 
6161
 
 
6162
    Constructs a QLatin1String object that stores \a str.
 
6163
 
 
6164
    The string data is \e not copied. The caller must be able to
 
6165
    guarantee that \a str will not be deleted or modified as long as
 
6166
    the QLatin1String object exists.
 
6167
 
 
6168
    \sa latin1()
 
6169
*/
 
6170
 
 
6171
/*! \fn const char *QLatin1String::latin1() const
 
6172
 
 
6173
    Returns the Latin-1 string stored in this object.
 
6174
*/
 
6175
 
 
6176
/*! \fn bool QLatin1String::operator==(const QString &other) const
 
6177
 
 
6178
    Returns true if this string is equal to string \a other;
 
6179
    otherwise returns false.
 
6180
 
 
6181
    The comparison is based exclusively on the numeric Unicode values
 
6182
    of the characters and is very fast, but is not what a human would
 
6183
    expect. Consider sorting user-interface strings with
 
6184
    QString::localeAwareCompare().
 
6185
*/
 
6186
 
 
6187
/*! \fn bool QLatin1String::operator!=(const QString &other) const
 
6188
 
 
6189
    Returns true if this string is not equal to string \a other;
 
6190
    otherwise returns false.
 
6191
 
 
6192
    The comparison is based exclusively on the numeric Unicode values
 
6193
    of the characters and is very fast, but is not what a human would
 
6194
    expect. Consider sorting user-interface strings with
 
6195
    QString::localeAwareCompare().
 
6196
*/
 
6197
 
 
6198
/*! \fn bool QLatin1String::operator>(const QString &other) const
 
6199
 
 
6200
    Returns true if this string is lexically greater than string \a
 
6201
    other; otherwise returns false.
 
6202
 
 
6203
    The comparison is based exclusively on the numeric Unicode values
 
6204
    of the characters and is very fast, but is not what a human would
 
6205
    expect. Consider sorting user-interface strings with
 
6206
    QString::localeAwareCompare().
 
6207
*/
 
6208
 
 
6209
/*! \fn bool QLatin1String::operator<(const QString &other) const
 
6210
 
 
6211
    Returns true if this string is lexically less than string \a
 
6212
    other; otherwise returns false.
 
6213
 
 
6214
    The comparison is based exclusively on the numeric Unicode values
 
6215
    of the characters and is very fast, but is not what a human would
 
6216
    expect. Consider sorting user-interface strings with
 
6217
    QString::localeAwareCompare().
 
6218
*/
 
6219
 
 
6220
/*! \fn bool QLatin1String::operator>=(const QString &other) const
 
6221
 
 
6222
    Returns true if this string is lexically greater than or equal
 
6223
    to string \a other; otherwise returns false.
 
6224
 
 
6225
    The comparison is based exclusively on the numeric Unicode values
 
6226
    of the characters and is very fast, but is not what a human would
 
6227
    expect. Consider sorting user-interface strings with
 
6228
    QString::localeAwareCompare().
 
6229
*/
 
6230
 
 
6231
/*! \fn bool QLatin1String::operator<=(const QString &other) const
 
6232
 
 
6233
    Returns true if this string is lexically less than or equal
 
6234
    to string \a other; otherwise returns false.
 
6235
 
 
6236
    The comparison is based exclusively on the numeric Unicode values
 
6237
    of the characters and is very fast, but is not what a human would
 
6238
    expect. Consider sorting user-interface strings with
 
6239
    QString::localeAwareCompare().
 
6240
*/
 
6241
 
 
6242
#ifndef QT_NO_DATASTREAM
 
6243
/*!
 
6244
    \relates QString
 
6245
 
 
6246
    Writes the string \a str to the stream \a out.
 
6247
 
 
6248
    \sa \link datastreamformat.html Format of the QDataStream operators \endlink
 
6249
*/
 
6250
 
 
6251
QDataStream &operator<<(QDataStream &out, const QString &str)
 
6252
{
 
6253
    if (out.version() == 1) {
 
6254
        out << str.toLatin1();
 
6255
    } else {
 
6256
        if (!str.isNull() || out.version() < 3) {
 
6257
            int byteOrder = out.byteOrder();
 
6258
            const QChar* ub = str.unicode();
 
6259
            static const uint auto_size = 1024;
 
6260
            char t[auto_size];
 
6261
            char *b;
 
6262
            if (str.length()*sizeof(QChar) > auto_size) {
 
6263
                b = new char[str.length()*sizeof(QChar)];
 
6264
            } else {
 
6265
                b = t;
 
6266
            }
 
6267
            int l = str.length();
 
6268
            char *c=b;
 
6269
            while (l--) {
 
6270
                if (byteOrder == QDataStream::BigEndian) {
 
6271
                    *c++ = (char)ub->row();
 
6272
                    *c++ = (char)ub->cell();
 
6273
                } else {
 
6274
                    *c++ = (char)ub->cell();
 
6275
                    *c++ = (char)ub->row();
 
6276
                }
 
6277
                ub++;
 
6278
            }
 
6279
            out.writeBytes(b, sizeof(QChar)*str.length());
 
6280
            if (str.length()*sizeof(QChar) > auto_size)
 
6281
                delete [] b;
 
6282
        } else {
 
6283
            // write null marker
 
6284
            out << (quint32)0xffffffff;
 
6285
        }
 
6286
    }
 
6287
    return out;
 
6288
}
 
6289
 
 
6290
/*!
 
6291
    \relates QString
 
6292
 
 
6293
    Reads a string from the stream \a in into string \a str.
 
6294
 
 
6295
    \sa \link datastreamformat.html Format of the QDataStream operators \endlink
 
6296
*/
 
6297
 
 
6298
QDataStream &operator>>(QDataStream &in, QString &str)
 
6299
{
 
6300
#ifdef QT_QSTRING_UCS_4
 
6301
#if defined(Q_CC_GNU)
 
6302
#warning "operator>> not working properly"
 
6303
#endif
 
6304
#endif
 
6305
 
 
6306
    if (in.version() == 1) {
 
6307
        QByteArray l;
 
6308
        in >> l;
 
6309
        str = QString::fromLatin1(l);
 
6310
    } else {
 
6311
        quint32 bytes = 0;
 
6312
        in >> bytes;                                  // read size of string
 
6313
        if (bytes == 0xffffffff) {                    // null string
 
6314
            str.clear();
 
6315
        } else if (bytes > 0) {                       // not empty
 
6316
            if (bytes & 0x1) {
 
6317
                str.clear();
 
6318
                in.setStatus(QDataStream::ReadCorruptData);
 
6319
                return in;
 
6320
            }
 
6321
 
 
6322
            const quint32 Step = 1024 * 1024;
 
6323
            quint32 len = bytes / 2;
 
6324
            quint32 allocated = 0;
 
6325
 
 
6326
            while (allocated < len) {
 
6327
                int blockSize = qMin(Step, len - allocated);
 
6328
                str.resize(allocated + blockSize);
 
6329
                if (in.readRawData(reinterpret_cast<char *>(str.data()) + allocated * 2,
 
6330
                                   blockSize * 2) != blockSize * 2) {
 
6331
                    str.clear();
 
6332
                    in.setStatus(QDataStream::ReadPastEnd);
 
6333
                    return in;
 
6334
                }
 
6335
                allocated += blockSize;
 
6336
            }
 
6337
 
 
6338
            if ((in.byteOrder() == QDataStream::BigEndian)
 
6339
                    != (QSysInfo::ByteOrder == QSysInfo::BigEndian)) {
 
6340
                ushort *data = reinterpret_cast<ushort *>(str.data());
 
6341
                while (len--) {
 
6342
                    *data = (*data >> 8) | (*data << 8);
 
6343
                    ++data;
 
6344
                }
 
6345
            }
 
6346
        } else {
 
6347
            str = QLatin1String("");
 
6348
        }
 
6349
    }
 
6350
    return in;
 
6351
}
 
6352
#endif // QT_NO_DATASTREAM
 
6353
 
 
6354
/*!
 
6355
    \fn void QString::setLength(int nl)
 
6356
 
 
6357
    Use resize() instead.
 
6358
*/
 
6359
 
 
6360
/*!
 
6361
    \fn QString QString::copy() const
 
6362
 
 
6363
    Use simple assignment instead. QString is implicitly shared so if
 
6364
    a copy is modified only the copy is changed.
 
6365
*/
 
6366
 
 
6367
/*!
 
6368
    \fn QString &QString::remove(QChar c, bool cs)
 
6369
 
 
6370
    Use the remove(QChar, Qt::CaseSensitive) overload instead.
 
6371
*/
 
6372
 
 
6373
/*!
 
6374
    \fn QString &QString::remove(const QString  &s, bool cs)
 
6375
 
 
6376
    Use the remove(QString, Qt::CaseSensitive) overload instead.
 
6377
*/
 
6378
 
 
6379
/*!
 
6380
    \fn QString &QString::replace(QChar c, const QString  &after, bool cs)
 
6381
 
 
6382
    Use the replace(QChar, QString, Qt::CaseSensitive) overload instead.
 
6383
*/
 
6384
 
 
6385
/*!
 
6386
    \fn QString &QString::replace(const QString &before, const QString &after, bool cs)
 
6387
 
 
6388
    Use the replace(QString, QString, Qt::CaseSensitive) overload instead.
 
6389
*/
 
6390
 
 
6391
/*!
 
6392
    \fn QString &QString::replace(char c, const QString &after, bool cs)
 
6393
 
 
6394
    Use the replace(QChar, QString, Qt::CaseSensitive) overload instead.
 
6395
*/
 
6396
 
 
6397
/*!
 
6398
    \fn QString &QString::replace(char c, const QString &after, Qt::CaseSensitivity cs)
 
6399
 
 
6400
    Use the replace(QChar, QString, Qt::CaseSensitive) overload instead.
 
6401
*/
 
6402
 
 
6403
/*!
 
6404
    \fn int QString::find(QChar c, int i = 0, bool cs = true) const
 
6405
 
 
6406
    Use indexOf() instead.
 
6407
*/
 
6408
 
 
6409
/*!
 
6410
    \fn int QString::find(const QString &s, int i = 0, bool cs = true) const
 
6411
 
 
6412
    Use indexOf() instead.
 
6413
*/
 
6414
 
 
6415
/*!
 
6416
    \fn int QString::findRev(QChar c, int i = -1, bool cs = true) const
 
6417
 
 
6418
    Use lastIndexOf() instead.
 
6419
*/
 
6420
 
 
6421
/*!
 
6422
    \fn int QString::findRev(const QString &s, int i = -1, bool cs = true) const
 
6423
 
 
6424
    Use lastIndexOf() instead.
 
6425
*/
 
6426
 
 
6427
/*!
 
6428
    \fn int QString::find(const QRegExp &rx, int i=0) const
 
6429
 
 
6430
    Use indexOf() instead.
 
6431
*/
 
6432
 
 
6433
/*!
 
6434
    \fn int QString::findRev(const QRegExp &rx, int i=-1) const
 
6435
 
 
6436
    Use lastIndexOf() instead.
 
6437
*/
 
6438
 
 
6439
/*!
 
6440
    \fn QBool QString::contains(QChar c, bool cs) const
 
6441
 
 
6442
    Use the contains(QChar, Qt::CaseSensitive) overload instead.
 
6443
*/
 
6444
 
 
6445
/*!
 
6446
    \fn QBool QString::contains(const QString &s, bool cs) const
 
6447
 
 
6448
    Use the contains(QString, Qt::CaseSensitive) overload instead.
 
6449
*/
 
6450
 
 
6451
/*!
 
6452
    \fn bool QString::startsWith(const QString &s, bool cs) const
 
6453
 
 
6454
    Use the startsWith(QString, Qt::CaseSensitive) overload instead.
 
6455
*/
 
6456
 
 
6457
/*!
 
6458
    \fn bool QString::endsWith(const QString &s, bool cs) const
 
6459
 
 
6460
    Use the endsWith(QString, Qt::CaseSensitive) overload instead.
 
6461
*/
 
6462
 
 
6463
/*!
 
6464
    \fn QString QString::leftJustify(int width, QChar fill = QLatin1Char(' '), bool trunc=false) const
 
6465
 
 
6466
    Use leftJustified() instead.
 
6467
*/
 
6468
 
 
6469
/*!
 
6470
    \fn QString QString::rightJustify(int width, QChar fill = QLatin1Char(' '), bool trunc=false) const
 
6471
 
 
6472
    Use rightJustified() instead.
 
6473
*/
 
6474
 
 
6475
/*!
 
6476
    \fn QString QString::lower() const
 
6477
 
 
6478
    Use toLower() instead.
 
6479
*/
 
6480
 
 
6481
/*!
 
6482
    \fn QString QString::upper() const
 
6483
 
 
6484
    Use toUpper() instead.
 
6485
*/
 
6486
 
 
6487
/*!
 
6488
    \fn QString QString::stripWhiteSpace() const
 
6489
 
 
6490
    Use trimmed() instead.
 
6491
*/
 
6492
 
 
6493
/*!
 
6494
    \fn QString QString::simplifyWhiteSpace() const
 
6495
 
 
6496
    Use simplified() instead.
 
6497
*/
 
6498
 
 
6499
/*!
 
6500
    \fn QString &QString::setUnicodeCodes(const ushort *unicode_as_ushorts, int size)
 
6501
 
 
6502
    Use setUtf16() instead.
 
6503
*/
 
6504
 
 
6505
/*!
 
6506
    \fn ushort *QString::ucs2() const
 
6507
 
 
6508
    Use utf16() instead.
 
6509
*/
 
6510
 
 
6511
/*!
 
6512
    \fn QString QString::fromUcs2(const ushort *unicode, int size = -1)
 
6513
 
 
6514
    Use fromUtf16() instead.
 
6515
*/
 
6516
 
 
6517
/*!
 
6518
    \fn QString &QString::setAscii(const char *str, int len = -1)
 
6519
 
 
6520
    Use fromAscii() instead.
 
6521
*/
 
6522
 
 
6523
/*!
 
6524
    \fn QString &QString::setLatin1(const char *str, int len = -1)
 
6525
 
 
6526
    Use fromLatin1() instead.
 
6527
*/
 
6528
 
 
6529
/*!
 
6530
    \fn QChar QString::constref(uint i) const
 
6531
 
 
6532
    Use at() instead.
 
6533
*/
 
6534
 
 
6535
/*!
 
6536
    \fn QChar &QString::ref(uint i);
 
6537
 
 
6538
    Use operator[]() instead.
 
6539
*/
 
6540
 
 
6541
/*!
 
6542
    \fn QString::operator const char *() const
 
6543
 
 
6544
    Use toAscii().constData() instead.
 
6545
*/