1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qstringlist.h"
44 #include "qregularexpression.h"
45 #include "qunicodetables_p.h"
46 #ifndef QT_NO_TEXTCODEC
47 #include <qtextcodec.h>
49 #include <private/qutfcodec_p.h>
52 #include <qdatastream.h>
55 #include "qlocale_p.h"
56 #include "qstringmatcher.h"
57 #include "qvarlengtharray.h"
62 #include "qcollator_p.h"
65 #include <private/qcore_mac_p.h>
68 #include <private/qfunctions_p.h>
77 #include "qstringmatcher.cpp"
80 # include <qt_windows.h>
91 #define LLONG_MAX qint64_C(9223372036854775807)
94 #define LLONG_MIN (-LLONG_MAX - qint64_C(1))
97 #define ULLONG_MAX quint64_C(18446744073709551615)
100 #define IS_RAW_DATA(d) ((d)->offset != sizeof(QStringData))
105 int qFindString(const QChar *haystack, int haystackLen, int from,
106 const QChar *needle, int needleLen, Qt::CaseSensitivity cs);
107 int qFindStringBoyerMoore(const QChar *haystack, int haystackLen, int from,
108 const QChar *needle, int needleLen, Qt::CaseSensitivity cs);
109 static inline int qt_last_index_of(const QChar *haystack, int haystackLen, QChar needle,
110 int from, Qt::CaseSensitivity cs);
111 static inline int qt_string_count(const QChar *haystack, int haystackLen,
112 const QChar *needle, int needleLen,
113 Qt::CaseSensitivity cs);
114 static inline int qt_string_count(const QChar *haystack, int haystackLen,
115 QChar needle, Qt::CaseSensitivity cs);
116 static inline int qt_find_latin1_string(const QChar *hay, int size, QLatin1String needle,
117 int from, Qt::CaseSensitivity cs);
118 static inline bool qt_starts_with(const QChar *haystack, int haystackLen,
119 const QChar *needle, int needleLen, Qt::CaseSensitivity cs);
120 static inline bool qt_starts_with(const QChar *haystack, int haystackLen,
121 QLatin1String needle, Qt::CaseSensitivity cs);
122 static inline bool qt_ends_with(const QChar *haystack, int haystackLen,
123 const QChar *needle, int needleLen, Qt::CaseSensitivity cs);
124 static inline bool qt_ends_with(const QChar *haystack, int haystackLen,
125 QLatin1String needle, Qt::CaseSensitivity cs);
127 // Unicode case-insensitive comparison
128 static int ucstricmp(const ushort *a, const ushort *ae, const ushort *b, const ushort *be)
137 const ushort *e = ae;
144 // qDebug() << hex << alast << blast;
145 // qDebug() << hex << "*a=" << *a << "alast=" << alast << "folded=" << foldCase (*a, alast);
146 // qDebug() << hex << "*b=" << *b << "blast=" << blast << "folded=" << foldCase (*b, blast);
147 int diff = foldCase(*a, alast) - foldCase(*b, blast);
161 // Case-insensitive comparison between a Unicode string and a QLatin1String
162 static int ucstricmp(const ushort *a, const ushort *ae, const uchar *b, const uchar *be)
172 const ushort *e = ae;
177 int diff = foldCase(*a) - foldCase(*b);
191 // Unicode case-sensitive compare two same-sized strings
192 static int ucstrncmp(const QChar *a, const QChar *b, int l)
194 while (l-- && *a == *b)
198 return a->unicode() - b->unicode();
201 // Unicode case-sensitive comparison
202 static int ucstrcmp(const QChar *a, int alen, const QChar *b, int blen)
204 if (a == b && alen == blen)
206 int l = qMin(alen, blen);
207 int cmp = ucstrncmp(a, b, l);
208 return cmp ? cmp : (alen-blen);
211 // Unicode case-insensitive compare two same-sized strings
212 static int ucstrnicmp(const ushort *a, const ushort *b, int l)
214 return ucstricmp(a, a + l, b, b + l);
217 // Benchmarking indicates that doing memcmp is much slower than
218 // executing the comparison ourselves.
220 // The profiling was done on a population of calls to qMemEquals, generated
221 // during a run of the demo browser. The profile of the data (32-bit x86
224 // total number of comparisons: 21353
225 // longest string compared: 95
226 // average comparison length: 14.8786
227 // cache-line crosses: 5661 (13.3%)
228 // alignment histogram:
229 // 0xXXX0 = 512 (1.2%) strings, 0 (0.0%) of which same-aligned
230 // 0xXXX2 = 15087 (35.3%) strings, 5145 (34.1%) of which same-aligned
231 // 0xXXX4 = 525 (1.2%) strings, 0 (0.0%) of which same-aligned
232 // 0xXXX6 = 557 (1.3%) strings, 6 (1.1%) of which same-aligned
233 // 0xXXX8 = 509 (1.2%) strings, 0 (0.0%) of which same-aligned
234 // 0xXXXa = 24358 (57.0%) strings, 9901 (40.6%) of which same-aligned
235 // 0xXXXc = 557 (1.3%) strings, 0 (0.0%) of which same-aligned
236 // 0xXXXe = 601 (1.4%) strings, 15 (2.5%) of which same-aligned
237 // total = 42706 (100%) strings, 15067 (35.3%) of which same-aligned
239 // 92% of the strings have alignment of 2 or 10, which is due to malloc on
240 // 32-bit Linux returning values aligned to 8 bytes, and offsetof(array, QString::Data) == 18.
242 // The profile on 64-bit will be different since offsetof(array, QString::Data) == 26.
244 // The benchmark results were, for a Core-i7 @ 2.67 GHz 32-bit, compiled with -O3 -funroll-loops:
245 // 16-bit loads only: 872,301 CPU ticks [Qt 4.5 / memcmp]
246 // 32- and 16-bit loads: 773,362 CPU ticks [Qt 4.6]
247 // SSE2 "movdqu" 128-bit loads: 618,736 CPU ticks
248 // SSE3 "lddqu" 128-bit loads: 619,954 CPU ticks
249 // SSSE3 "palignr" corrections: 852,147 CPU ticks
250 // SSE4.2 "pcmpestrm": 738,702 CPU ticks
252 // The same benchmark on an Atom N450 @ 1.66 GHz, is:
253 // 16-bit loads only: 2,185,882 CPU ticks
254 // 32- and 16-bit loads: 1,805,060 CPU ticks
255 // SSE2 "movdqu" 128-bit loads: 2,529,843 CPU ticks
256 // SSE3 "lddqu" 128-bit loads: 2,514,858 CPU ticks
257 // SSSE3 "palignr" corrections: 2,160,325 CPU ticks
258 // SSE4.2 not available
260 // The conclusion we reach is that alignment the SSE2 unaligned code can gain
261 // 20% improvement in performance in some systems, but suffers a penalty due
262 // to the unaligned loads on others.
264 static bool qMemEquals(const quint16 *a, const quint16 *b, int length)
266 if (a == b || !length)
278 if ((sa.value & 2) == (sb.value & 2)) {
279 // both addresses have the same alignment
281 // both addresses are not aligned to 4-bytes boundaries
282 // compare the first character
289 // now both addresses are 4-bytes aligned
292 // both addresses are 4-bytes aligned
293 // do a fast 32-bit comparison
294 register const quint32 *e = sa.d + (length >> 1);
295 for ( ; sa.d != e; ++sa.d, ++sb.d) {
300 // do we have a tail?
301 return (length & 1) ? *sa.w == *sb.w : true;
303 // one of the addresses isn't 4-byte aligned but the other is
304 register const quint16 *e = sa.w + length;
305 for ( ; sa.w != e; ++sa.w, ++sb.w) {
316 Returns the index position of the first occurrence of the
317 character \a ch in the string given by \a str and \a len,
318 searching forward from index
319 position \a from. Returns -1 if \a ch could not be found.
321 static int findChar(const QChar *str, int len, QChar ch, int from,
322 Qt::CaseSensitivity cs)
324 const ushort *s = (const ushort *)str;
325 ushort c = ch.unicode();
327 from = qMax(from + len, 0);
329 const ushort *n = s + from - 1;
330 const ushort *e = s + len;
331 if (cs == Qt::CaseSensitive) {
338 if (foldCase(*n) == c)
346 if (sl_minus_1 < (int)sizeof(int) * CHAR_BIT) \
347 hashHaystack -= (a) << sl_minus_1; \
350 inline bool qIsUpper(char ch)
352 return ch >= 'A' && ch <= 'Z';
355 inline bool qIsDigit(char ch)
357 return ch >= '0' && ch <= '9';
360 inline char qToLower(char ch)
362 if (ch >= 'A' && ch <= 'Z')
363 return ch - 'A' + 'a';
369 const QString::Null QString::null = { };
372 \macro QT_NO_CAST_FROM_ASCII
375 Disables automatic conversions from 8-bit strings (char *) to unicode QStrings
377 \sa QT_NO_CAST_TO_ASCII, QT_NO_CAST_FROM_BYTEARRAY
381 \macro QT_NO_CAST_TO_ASCII
384 disables automatic conversion from QString to 8-bit strings (char *)
386 \sa QT_NO_CAST_FROM_ASCII, QT_NO_CAST_FROM_BYTEARRAY
390 \macro QT_ASCII_CAST_WARNINGS
394 This macro can be defined to force a warning whenever a function is
395 called that automatically converts between unicode and 8-bit encodings.
397 Note: This only works for compilers that support warnings for
400 \sa QT_NO_CAST_TO_ASCII, QT_NO_CAST_FROM_ASCII
406 \brief The QCharRef class is a helper class for QString.
410 \ingroup string-processing
412 When you get an object of type QCharRef, if you can assign to it,
413 the assignment will apply to the character in the string from
414 which you got the reference. That is its whole purpose in life.
415 The QCharRef becomes invalid once modifications are made to the
416 string: if you want to keep the character, copy it into a QChar.
418 Most of the QChar member functions also exist in QCharRef.
419 However, they are not explicitly documented here.
421 \sa QString::operator[](), QString::at(), QChar
428 \brief The QString class provides a Unicode character string.
432 \ingroup string-processing
434 QString stores a string of 16-bit \l{QChar}s, where each QChar
435 corresponds one Unicode 4.0 character. (Unicode characters
436 with code values above 65535 are stored using surrogate pairs,
437 i.e., two consecutive \l{QChar}s.)
439 \l{Unicode} is an international standard that supports most of the
440 writing systems in use today. It is a superset of US-ASCII (ANSI
441 X3.4-1986) and Latin-1 (ISO 8859-1), and all the US-ASCII/Latin-1
442 characters are available at the same code positions.
444 Behind the scenes, QString uses \l{implicit sharing}
445 (copy-on-write) to reduce memory usage and to avoid the needless
446 copying of data. This also helps reduce the inherent overhead of
447 storing 16-bit characters instead of 8-bit characters.
449 In addition to QString, Qt also provides the QByteArray class to
450 store raw bytes and traditional 8-bit '\\0'-terminated strings.
451 For most purposes, QString is the class you want to use. It is
452 used throughout the Qt API, and the Unicode support ensures that
453 your applications will be easy to translate if you want to expand
454 your application's market at some point. The two main cases where
455 QByteArray is appropriate are when you need to store raw binary
456 data, and when memory conservation is critical (e.g., with
457 \l{Qt for Embedded Linux}).
461 \section1 Initializing a String
463 One way to initialize a QString is simply to pass a \c{const char
464 *} to its constructor. For example, the following code creates a
465 QString of size 5 containing the data "Hello":
467 \snippet qstring/main.cpp 0
469 QString converts the \c{const char *} data into Unicode using the
472 In all of the QString functions that take \c{const char *}
473 parameters, the \c{const char *} is interpreted as a classic
474 C-style '\\0'-terminated string encoded in UTF-8. It is legal for
475 the \c{const char *} parameter to be 0.
477 You can also provide string data as an array of \l{QChar}s:
479 \snippet qstring/main.cpp 1
481 QString makes a deep copy of the QChar data, so you can modify it
482 later without experiencing side effects. (If for performance
483 reasons you don't want to take a deep copy of the character data,
484 use QString::fromRawData() instead.)
486 Another approach is to set the size of the string using resize()
487 and to initialize the data character per character. QString uses
488 0-based indexes, just like C++ arrays. To access the character at
489 a particular index position, you can use \l operator[](). On
490 non-const strings, \l operator[]() returns a reference to a
491 character that can be used on the left side of an assignment. For
494 \snippet qstring/main.cpp 2
496 For read-only access, an alternative syntax is to use the at()
499 \snippet qstring/main.cpp 3
501 The at() function can be faster than \l operator[](), because it
502 never causes a \l{deep copy} to occur. Alternatively, use the
503 left(), right(), or mid() functions to extract several characters
506 A QString can embed '\\0' characters (QChar::Null). The size()
507 function always returns the size of the whole string, including
508 embedded '\\0' characters.
510 After a call to the resize() function, newly allocated characters
511 have undefined values. To set all the characters in the string to
512 a particular value, use the fill() function.
514 QString provides dozens of overloads designed to simplify string
515 usage. For example, if you want to compare a QString with a string
516 literal, you can write code like this and it will work as expected:
518 \snippet qstring/main.cpp 4
520 You can also pass string literals to functions that take QStrings
521 as arguments, invoking the QString(const char *)
522 constructor. Similarly, you can pass a QString to a function that
523 takes a \c{const char *} argument using the \l qPrintable() macro
524 which returns the given QString as a \c{const char *}. This is
525 equivalent to calling <QString>.toLocal8Bit().constData().
527 \section1 Manipulating String Data
529 QString provides the following basic functions for modifying the
530 character data: append(), prepend(), insert(), replace(), and
531 remove(). For example:
533 \snippet qstring/main.cpp 5
535 If you are building a QString gradually and know in advance
536 approximately how many characters the QString will contain, you
537 can call reserve(), asking QString to preallocate a certain amount
538 of memory. You can also call capacity() to find out how much
539 memory QString actually allocated.
541 The replace() and remove() functions' first two arguments are the
542 position from which to start erasing and the number of characters
543 that should be erased. If you want to replace all occurrences of
544 a particular substring with another, use one of the two-parameter
547 A frequent requirement is to remove whitespace characters from a
548 string ('\\n', '\\t', ' ', etc.). If you want to remove whitespace
549 from both ends of a QString, use the trimmed() function. If you
550 want to remove whitespace from both ends and replace multiple
551 consecutive whitespaces with a single space character within the
552 string, use simplified().
554 If you want to find all occurrences of a particular character or
555 substring in a QString, use the indexOf() or lastIndexOf()
556 functions. The former searches forward starting from a given index
557 position, the latter searches backward. Both return the index
558 position of the character or substring if they find it; otherwise,
559 they return -1. For example, here's a typical loop that finds all
560 occurrences of a particular substring:
562 \snippet qstring/main.cpp 6
564 QString provides many functions for converting numbers into
565 strings and strings into numbers. See the arg() functions, the
566 setNum() functions, the number() static functions, and the
567 toInt(), toDouble(), and similar functions.
569 To get an upper- or lowercase version of a string use toUpper() or
572 Lists of strings are handled by the QStringList class. You can
573 split a string into a list of strings using the split() function,
574 and join a list of strings into a single string with an optional
575 separator using QStringList::join(). You can obtain a list of
576 strings from a string list that contain a particular substring or
577 that match a particular QRegExp using the QStringList::filter()
580 \section1 Querying String Data
582 If you want to see if a QString starts or ends with a particular
583 substring use startsWith() or endsWith(). If you simply want to
584 check whether a QString contains a particular character or
585 substring, use the contains() function. If you want to find out
586 how many times a particular character or substring occurs in the
589 QStrings can be compared using overloaded operators such as \l
590 operator<(), \l operator<=(), \l operator==(), \l operator>=(),
591 and so on. Note that the comparison is based exclusively on the
592 numeric Unicode values of the characters. It is very fast, but is
593 not what a human would expect; the QString::localeAwareCompare()
594 function is a better choice for sorting user-interface strings.
596 To obtain a pointer to the actual character data, call data() or
597 constData(). These functions return a pointer to the beginning of
598 the QChar data. The pointer is guaranteed to remain valid until a
599 non-const function is called on the QString.
601 \section1 Converting Between 8-Bit Strings and Unicode Strings
603 QString provides the following three functions that return a
604 \c{const char *} version of the string as QByteArray: toUtf8(),
605 toLatin1(), and toLocal8Bit().
608 \li toLatin1() returns a Latin-1 (ISO 8859-1) encoded 8-bit string.
609 \li toUtf8() returns a UTF-8 encoded 8-bit string. UTF-8 is a
610 superset of US-ASCII (ANSI X3.4-1986) that supports the entire
611 Unicode character set through multibyte sequences.
612 \li toLocal8Bit() returns an 8-bit string using the system's local
616 To convert from one of these encodings, QString provides
617 fromLatin1(), fromUtf8(), and fromLocal8Bit(). Other
618 encodings are supported through the QTextCodec class.
620 As mentioned above, QString provides a lot of functions and
621 operators that make it easy to interoperate with \c{const char *}
622 strings. But this functionality is a double-edged sword: It makes
623 QString more convenient to use if all strings are US-ASCII or
624 Latin-1, but there is always the risk that an implicit conversion
625 from or to \c{const char *} is done using the wrong 8-bit
626 encoding. To minimize these risks, you can turn off these implicit
627 conversions by defining the following two preprocessor symbols:
630 \li \c QT_NO_CAST_FROM_ASCII disables automatic conversions from
631 C string literals and pointers to Unicode.
632 \li \c QT_NO_CAST_TO_ASCII disables automatic conversion from QString
636 One way to define these preprocessor symbols globally for your
637 application is to add the following entry to your
638 \l{qmake Project Files}{qmake project file}:
640 \snippet code/src_corelib_tools_qstring.cpp 0
642 You then need to explicitly call fromUtf8(), fromLatin1(),
643 or fromLocal8Bit() to construct a QString from an
644 8-bit string, or use the lightweight QLatin1String class, for
647 \snippet code/src_corelib_tools_qstring.cpp 1
649 Similarly, you must call toLatin1(), toUtf8(), or
650 toLocal8Bit() explicitly to convert the QString to an 8-bit
651 string. (Other encodings are supported through the QTextCodec
656 \li Note for C Programmers
660 Due to C++'s type system and the fact that QString is
661 \l{implicitly shared}, QStrings may be treated like \c{int}s or
662 other basic types. For example:
664 \snippet qstring/main.cpp 7
666 The \c result variable, is a normal variable allocated on the
667 stack. When \c return is called, and because we're returning by
668 value, the copy constructor is called and a copy of the string is
669 returned. No actual copying takes place thanks to the implicit
674 \section1 Distinction Between Null and Empty Strings
676 For historical reasons, QString distinguishes between a null
677 string and an empty string. A \e null string is a string that is
678 initialized using QString's default constructor or by passing
679 (const char *)0 to the constructor. An \e empty string is any
680 string with size 0. A null string is always empty, but an empty
681 string isn't necessarily null:
683 \snippet qstring/main.cpp 8
685 All functions except isNull() treat null strings the same as empty
686 strings. For example, toUtf8().constData() returns a pointer to a
687 '\\0' character for a null string (\e not a null pointer), and
688 QString() compares equal to QString(""). We recommend that you
689 always use the isEmpty() function and avoid isNull().
691 \section1 Argument Formats
693 In member functions where an argument \e format can be specified
694 (e.g., arg(), number()), the argument \e format can be one of the
698 \header \li Format \li Meaning
699 \row \li \c e \li format as [-]9.9e[+|-]999
700 \row \li \c E \li format as [-]9.9E[+|-]999
701 \row \li \c f \li format as [-]9.9
702 \row \li \c g \li use \c e or \c f format, whichever is the most concise
703 \row \li \c G \li use \c E or \c f format, whichever is the most concise
706 A \e precision is also specified with the argument \e format. For
707 the 'e', 'E', and 'f' formats, the \e precision represents the
708 number of digits \e after the decimal point. For the 'g' and 'G'
709 formats, the \e precision represents the maximum number of
710 significant digits (trailing zeroes are omitted).
712 \section1 More Efficient String Construction
714 Many strings are known at compile time. But the trivial
715 constructor QString("Hello"), will copy the contents of the string,
716 treating the contents as Latin-1. To avoid this one can use the
717 QStringLiteral macro to directly create the required data at compile
718 time. Constructing a QString out of the literal does then not cause
719 any overhead at runtime.
721 A slightly less efficient way is to use QLatin1String. This class wraps
722 a C string literal, precalculates it length at compile time and can
723 then be used for faster comparison with QStrings and conversion to
724 QStrings than a regular C string literal.
726 Using the QString \c{'+'} operator, it is easy to construct a
727 complex string from multiple substrings. You will often write code
730 \snippet qstring/stringbuilder.cpp 0
732 There is nothing wrong with either of these string constructions,
733 but there are a few hidden inefficiencies. Beginning with Qt 4.6,
734 you can eliminate them.
736 First, multiple uses of the \c{'+'} operator usually means
737 multiple memory allocations. When concatenating \e{n} substrings,
738 where \e{n > 2}, there can be as many as \e{n - 1} calls to the
741 In 4.6, an internal template class \c{QStringBuilder} has been
742 added along with a few helper functions. This class is marked
743 internal and does not appear in the documentation, because you
744 aren't meant to instantiate it in your code. Its use will be
745 automatic, as described below. The class is found in
746 \c {src/corelib/tools/qstringbuilder.cpp} if you want to have a
749 \c{QStringBuilder} uses expression templates and reimplements the
750 \c{'%'} operator so that when you use \c{'%'} for string
751 concatenation instead of \c{'+'}, multiple substring
752 concatenations will be postponed until the final result is about
753 to be assigned to a QString. At this point, the amount of memory
754 required for the final result is known. The memory allocator is
755 then called \e{once} to get the required space, and the substrings
756 are copied into it one by one.
758 Additional efficiency is gained by inlining and reduced reference
759 counting (the QString created from a \c{QStringBuilder} typically
760 has a ref count of 1, whereas QString::append() needs an extra
763 There are three ways you can access this improved method of string
764 construction. The straightforward way is to include
765 \c{QStringBuilder} wherever you want to use it, and use the
766 \c{'%'} operator instead of \c{'+'} when concatenating strings:
768 \snippet qstring/stringbuilder.cpp 5
770 A more global approach which is the most convenient but
771 not entirely source compatible, is to this define in your
774 \snippet qstring/stringbuilder.cpp 3
776 and the \c{'+'} will automatically be performed as the
777 \c{QStringBuilder} \c{'%'} everywhere.
779 \sa fromRawData(), QChar, QLatin1String, QByteArray, QStringRef
783 \enum QString::SplitBehavior
785 This enum specifies how the split() function should behave with
786 respect to empty strings.
788 \value KeepEmptyParts If a field is empty, keep it in the result.
789 \value SkipEmptyParts If a field is empty, don't include it in the result.
794 /*! \typedef QString::ConstIterator
796 Qt-style synonym for QString::const_iterator.
799 /*! \typedef QString::Iterator
801 Qt-style synonym for QString::iterator.
804 /*! \typedef QString::const_iterator
806 The QString::const_iterator typedef provides an STL-style const
807 iterator for QString.
809 \sa QString::iterator
812 /*! \typedef QString::iterator
814 The QString::iterator typedef provides an STL-style non-const
815 iterator for QString.
817 \sa QString::const_iterator
821 \typedef QString::const_reference
823 The QString::const_reference typedef provides an STL-style
824 const reference for QString.
827 \typedef QString::reference
829 The QString::const_reference typedef provides an STL-style
830 reference for QString.
833 \typedef QString::value_type
835 The QString::const_reference typedef provides an STL-style
836 value type for QString.
839 /*! \fn QString::iterator QString::begin()
841 Returns an \l{STL-style iterator} pointing to the first character in
844 \sa constBegin(), end()
847 /*! \fn QString::const_iterator QString::begin() const
852 /*! \fn QString::const_iterator QString::cbegin() const
855 Returns a const \l{STL-style iterator} pointing to the first character
861 /*! \fn QString::const_iterator QString::constBegin() const
863 Returns a const \l{STL-style iterator} pointing to the first character
866 \sa begin(), constEnd()
869 /*! \fn QString::iterator QString::end()
871 Returns an \l{STL-style iterator} pointing to the imaginary character
872 after the last character in the string.
874 \sa begin(), constEnd()
877 /*! \fn QString::const_iterator QString::end() const
882 /*! \fn QString::const_iterator QString::cend() const
885 Returns a const \l{STL-style iterator} pointing to the imaginary
886 item after the last item in the list.
891 /*! \fn QString::const_iterator QString::constEnd() const
893 Returns a const \l{STL-style iterator} pointing to the imaginary
894 item after the last item in the list.
896 \sa constBegin(), end()
900 \fn QString::QString()
902 Constructs a null string. Null strings are also empty.
907 /*! \fn QString::QString(const char *str)
909 Constructs a string initialized with the 8-bit string \a str. The
910 given const char pointer is converted to Unicode using the
913 You can disable this constructor by defining \c
914 QT_NO_CAST_FROM_ASCII when you compile your applications. This
915 can be useful if you want to ensure that all user-visible strings
916 go through QObject::tr(), for example.
918 \sa fromLatin1(), fromLocal8Bit(), fromUtf8()
921 /*! \fn QString QString::fromStdString(const std::string &str)
923 Returns a copy of the \a str string. The given string is converted
924 to Unicode using the fromUtf8() function.
926 This constructor is only available if Qt is configured with STL
927 compatibility enabled.
929 \sa fromLatin1(), fromLocal8Bit(), fromUtf8()
932 /*! \fn QString QString::fromStdWString(const std::wstring &str)
934 Returns a copy of the \a str string. The given string is assumed
935 to be encoded in utf16 if the size of wchar_t is 2 bytes (e.g. on
936 windows) and ucs4 if the size of wchar_t is 4 bytes (most Unix
939 \sa fromUtf16(), fromLatin1(), fromLocal8Bit(), fromUtf8(), fromUcs4()
942 /*! \fn QString QString::fromWCharArray(const wchar_t *string, int size)
945 Returns a copy of the \a string, where the encoding of \a string depends on
946 the size of wchar. If wchar is 4 bytes, the \a string is interpreted as ucs-4,
947 if wchar is 2 bytes it is interpreted as ucs-2.
949 If \a size is -1 (default), the \a string has to be 0 terminated.
951 \sa fromUtf16(), fromLatin1(), fromLocal8Bit(), fromUtf8(), fromUcs4(), fromStdWString()
954 /*! \fn std::wstring QString::toStdWString() const
956 Returns a std::wstring object with the data contained in this
957 QString. The std::wstring is encoded in utf16 on platforms where
958 wchar_t is 2 bytes wide (e.g. windows) and in ucs4 on platforms
959 where wchar_t is 4 bytes wide (most Unix systems).
961 This operator is mostly useful to pass a QString to a function
962 that accepts a std::wstring object.
964 \sa utf16(), toLatin1(), toUtf8(), toLocal8Bit()
967 // ### replace with QCharIterator
968 int QString::toUcs4_helper(const ushort *uc, int length, uint *out)
971 for (; i < length; ++i) {
973 if (QChar::isHighSurrogate(u) && i + 1 < length) {
974 ushort low = uc[i+1];
975 if (QChar::isLowSurrogate(low)) {
977 u = QChar::surrogateToUcs4(u, low);
985 /*! \fn int QString::toWCharArray(wchar_t *array) const
988 Fills the \a array with the data contained in this QString object.
989 The array is encoded in utf16 on platforms where
990 wchar_t is 2 bytes wide (e.g. windows) and in ucs4 on platforms
991 where wchar_t is 4 bytes wide (most Unix systems).
993 \a array has to be allocated by the caller and contain enough space to
994 hold the complete string (allocating the array with the same length as the
995 string is always sufficient).
997 This function returns the actual length of the string in \a array.
999 \note This function does not append a null character to the array.
1001 \sa utf16(), toUcs4(), toLatin1(), toUtf8(), toLocal8Bit(), toStdWString()
1004 /*! \fn QString::QString(const QString &other)
1006 Constructs a copy of \a other.
1008 This operation takes \l{constant time}, because QString is
1009 \l{implicitly shared}. This makes returning a QString from a
1010 function very fast. If a shared instance is modified, it will be
1011 copied (copy-on-write), and that takes \l{linear time}.
1017 Constructs a string initialized with the first \a size characters
1018 of the QChar array \a unicode.
1020 If \a unicode is 0, a null string is constructed.
1022 If \a size is negative, \a unicode is assumed to point to a nul-terminated
1023 array and its length is determined dynamically. The terminating
1024 nul-character is not considered part of the string.
1026 QString makes a deep copy of the string data. The unicode data is copied as
1027 is and the Byte Order Mark is preserved if present.
1031 QString::QString(const QChar *unicode, int size)
1034 d = Data::sharedNull();
1038 while (unicode[size] != 0)
1042 d = Data::allocate(0);
1044 d = Data::allocate(size + 1);
1047 memcpy(d->data(), unicode, size * sizeof(QChar));
1048 d->data()[size] = '\0';
1054 Constructs a string of the given \a size with every character set
1059 QString::QString(int size, QChar ch)
1062 d = Data::allocate(0);
1064 d = Data::allocate(size + 1);
1067 d->data()[size] = '\0';
1068 ushort *i = d->data() + size;
1069 ushort *b = d->data();
1070 const ushort value = ch.unicode();
1076 /*! \fn QString::QString(int size, Qt::Initialization)
1079 Constructs a string of the given \a size without initializing the
1080 characters. This is only used in \c QStringBuilder::toString().
1082 QString::QString(int size, Qt::Initialization)
1084 d = Data::allocate(size + 1);
1087 d->data()[size] = '\0';
1090 /*! \fn QString::QString(QLatin1String str)
1092 Constructs a copy of the Latin-1 string \a str.
1098 Constructs a string of size 1 containing the character \a ch.
1100 QString::QString(QChar ch)
1102 d = Data::allocate(2);
1105 d->data()[0] = ch.unicode();
1106 d->data()[1] = '\0';
1109 /*! \fn QString::QString(const QByteArray &ba)
1111 Constructs a string initialized with the byte array \a ba. The
1112 given byte array is converted to Unicode using fromUtf8(). Stops
1113 copying at the first 0 character, otherwise copies the entire byte
1116 You can disable this constructor by defining \c
1117 QT_NO_CAST_FROM_ASCII when you compile your applications. This
1118 can be useful if you want to ensure that all user-visible strings
1119 go through QObject::tr(), for example.
1121 \sa fromLatin1(), fromLocal8Bit(), fromUtf8()
1124 /*! \fn QString::QString(const Null &)
1128 /*! \fn QString &QString::operator=(const Null &)
1133 \fn QString::~QString()
1135 Destroys the string.
1139 /*! \fn void QString::swap(QString &other)
1142 Swaps string \a other with this string. This operation is very fast and
1146 /*! \fn void QString::detach()
1151 /*! \fn bool QString::isDetached() const
1156 /*! \fn bool QString::isSharedWith(const QString &other) const
1162 Sets the size of the string to \a size characters.
1164 If \a size is greater than the current size, the string is
1165 extended to make it \a size characters long with the extra
1166 characters added to the end. The new characters are uninitialized.
1168 If \a size is less than the current size, characters are removed
1173 \snippet qstring/main.cpp 45
1175 If you want to append a certain number of identical characters to
1176 the string, use \l operator+=() as follows rather than resize():
1178 \snippet qstring/main.cpp 46
1180 If you want to expand the string so that it reaches a certain
1181 width and fill the new positions with a particular character, use
1182 the leftJustified() function:
1184 If \a size is negative, it is equivalent to passing zero.
1186 \snippet qstring/main.cpp 47
1188 \sa truncate(), reserve()
1191 void QString::resize(int size)
1196 if (IS_RAW_DATA(d) && !d->ref.isShared() && size < d->size) {
1201 if (size == 0 && !d->capacityReserved) {
1202 Data *x = Data::allocate(0);
1203 if (!d->ref.deref())
1204 Data::deallocate(d);
1207 if (d->ref.isShared() || uint(size) + 1u > d->alloc
1208 || (!d->capacityReserved && size < d->size
1209 && uint(size) + 1u < uint(d->alloc >> 1)))
1210 reallocData(uint(size) + 1u, true);
1213 d->data()[size] = '\0';
1218 /*! \fn int QString::capacity() const
1220 Returns the maximum number of characters that can be stored in
1221 the string without forcing a reallocation.
1223 The sole purpose of this function is to provide a means of fine
1224 tuning QString's memory usage. In general, you will rarely ever
1225 need to call this function. If you want to know how many
1226 characters are in the string, call size().
1228 \sa reserve(), squeeze()
1232 \fn void QString::reserve(int size)
1234 Attempts to allocate memory for at least \a size characters. If
1235 you know in advance how large the string will be, you can call
1236 this function, and if you resize the string often you are likely
1237 to get better performance. If \a size is an underestimate, the
1238 worst that will happen is that the QString will be a bit slower.
1240 The sole purpose of this function is to provide a means of fine
1241 tuning QString's memory usage. In general, you will rarely ever
1242 need to call this function. If you want to change the size of the
1243 string, call resize().
1245 This function is useful for code that needs to build up a long
1246 string and wants to avoid repeated reallocation. In this example,
1247 we want to add to the string until some condition is true, and
1248 we're fairly sure that size is large enough to make a call to
1249 reserve() worthwhile:
1251 \snippet qstring/main.cpp 44
1253 \sa squeeze(), capacity()
1257 \fn void QString::squeeze()
1259 Releases any memory not required to store the character data.
1261 The sole purpose of this function is to provide a means of fine
1262 tuning QString's memory usage. In general, you will rarely ever
1263 need to call this function.
1265 \sa reserve(), capacity()
1268 void QString::reallocData(uint alloc, bool grow)
1271 alloc = qAllocMore(alloc * sizeof(QChar), sizeof(Data)) / sizeof(QChar);
1273 if (d->ref.isShared() || IS_RAW_DATA(d)) {
1274 Data::AllocationOptions allocOptions(d->capacityReserved ? Data::CapacityReserved : 0);
1275 Data *x = Data::allocate(alloc, allocOptions);
1277 x->size = qMin(int(alloc) - 1, d->size);
1278 ::memcpy(x->data(), d->data(), x->size * sizeof(QChar));
1279 x->data()[x->size] = 0;
1280 if (!d->ref.deref())
1281 Data::deallocate(d);
1284 Data *p = static_cast<Data *>(::realloc(d, sizeof(Data) + alloc * sizeof(QChar)));
1288 d->offset = sizeof(QStringData);
1292 void QString::expand(int i)
1295 resize(qMax(i + 1, sz));
1296 if (d->size - 1 > sz) {
1297 ushort *n = d->data() + d->size - 1;
1298 ushort *e = d->data() + sz;
1304 /*! \fn void QString::clear()
1306 Clears the contents of the string and makes it empty.
1308 \sa resize(), isEmpty()
1311 /*! \fn QString &QString::operator=(const QString &other)
1313 Assigns \a other to this string and returns a reference to this
1317 QString &QString::operator=(const QString &other)
1320 if (!d->ref.deref())
1321 Data::deallocate(d);
1327 /*! \fn QString &QString::operator=(QLatin1String str)
1329 \overload operator=()
1331 Assigns the Latin-1 string \a str to this string.
1334 /*! \fn QString &QString::operator=(const QByteArray &ba)
1336 \overload operator=()
1338 Assigns \a ba to this string. The byte array is converted to Unicode
1339 using the fromUtf8() function. This function stops conversion at the
1340 first NUL character found, or the end of the \a ba byte array.
1342 You can disable this operator by defining \c
1343 QT_NO_CAST_FROM_ASCII when you compile your applications. This
1344 can be useful if you want to ensure that all user-visible strings
1345 go through QObject::tr(), for example.
1348 /*! \fn QString &QString::operator=(const char *str)
1350 \overload operator=()
1352 Assigns \a str to this string. The const char pointer is converted
1353 to Unicode using the fromUtf8() function.
1355 You can disable this operator by defining \c
1356 QT_NO_CAST_FROM_ASCII when you compile your applications. This
1357 can be useful if you want to ensure that all user-visible strings
1358 go through QObject::tr(), for example.
1361 /*! \fn QString &QString::operator=(char ch)
1363 \overload operator=()
1365 Assigns character \a ch to this string. Note that the character is
1366 converted to Unicode using the fromLatin1() function, unlike other 8-bit
1367 functions that operate on UTF-8 data.
1369 You can disable this operator by defining \c
1370 QT_NO_CAST_FROM_ASCII when you compile your applications. This
1371 can be useful if you want to ensure that all user-visible strings
1372 go through QObject::tr(), for example.
1376 \overload operator=()
1378 Sets the string to contain the single character \a ch.
1380 QString &QString::operator=(QChar ch)
1382 return operator=(QString(ch));
1386 \fn QString& QString::insert(int position, const QString &str)
1388 Inserts the string \a str at the given index \a position and
1389 returns a reference to this string.
1393 \snippet qstring/main.cpp 26
1395 If the given \a position is greater than size(), the array is
1396 first extended using resize().
1398 \sa append(), prepend(), replace(), remove()
1403 \fn QString &QString::insert(int position, QLatin1String str)
1406 Inserts the Latin-1 string \a str at the given index \a position.
1408 QString &QString::insert(int i, QLatin1String str)
1410 const uchar *s = (const uchar *)str.latin1();
1411 if (i < 0 || !s || !(*s))
1414 int len = str.size();
1415 expand(qMax(d->size, i) + len - 1);
1417 ::memmove(d->data() + i + len, d->data() + i, (d->size - i - len) * sizeof(QChar));
1418 for (int j = 0; j < len; ++j)
1419 d->data()[i + j] = s[j];
1424 \fn QString& QString::insert(int position, const QChar *unicode, int size)
1427 Inserts the first \a size characters of the QChar array \a unicode
1428 at the given index \a position in the string.
1430 QString& QString::insert(int i, const QChar *unicode, int size)
1432 if (i < 0 || size <= 0)
1435 const ushort *s = (const ushort *)unicode;
1436 if (s >= d->data() && s < d->data() + d->alloc) {
1437 // Part of me - take a copy
1438 ushort *tmp = static_cast<ushort *>(::malloc(size * sizeof(QChar)));
1440 memcpy(tmp, s, size * sizeof(QChar));
1441 insert(i, reinterpret_cast<const QChar *>(tmp), size);
1446 expand(qMax(d->size, i) + size - 1);
1448 ::memmove(d->data() + i + size, d->data() + i, (d->size - i - size) * sizeof(QChar));
1449 memcpy(d->data() + i, s, size * sizeof(QChar));
1454 \fn QString& QString::insert(int position, QChar ch)
1457 Inserts \a ch at the given index \a position in the string.
1460 QString& QString::insert(int i, QChar ch)
1466 expand(qMax(i, d->size));
1467 ::memmove(d->data() + i + 1, d->data() + i, (d->size - i - 1) * sizeof(QChar));
1468 d->data()[i] = ch.unicode();
1473 Appends the string \a str onto the end of this string.
1477 \snippet qstring/main.cpp 9
1479 This is the same as using the insert() function:
1481 \snippet qstring/main.cpp 10
1483 The append() function is typically very fast (\l{constant time}),
1484 because QString preallocates extra space at the end of the string
1485 data so it can grow without reallocating the entire string each
1488 \sa operator+=(), prepend(), insert()
1490 QString &QString::append(const QString &str)
1492 if (str.d != Data::sharedNull()) {
1493 if (d == Data::sharedNull()) {
1496 if (d->ref.isShared() || uint(d->size + str.d->size) + 1u > d->alloc)
1497 reallocData(uint(d->size + str.d->size) + 1u, true);
1498 memcpy(d->data() + d->size, str.d->data(), str.d->size * sizeof(QChar));
1499 d->size += str.d->size;
1500 d->data()[d->size] = '\0';
1509 Appends the Latin-1 string \a str to this string.
1511 QString &QString::append(QLatin1String str)
1513 const uchar *s = (const uchar *)str.latin1();
1515 int len = str.size();
1516 if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc)
1517 reallocData(uint(d->size + len) + 1u, true);
1518 ushort *i = d->data() + d->size;
1519 while ((*i++ = *s++))
1526 /*! \fn QString &QString::append(const QByteArray &ba)
1530 Appends the byte array \a ba to this string. The given byte array
1531 is converted to Unicode using the fromUtf8() function.
1533 You can disable this function by defining \c QT_NO_CAST_FROM_ASCII
1534 when you compile your applications. This can be useful if you want
1535 to ensure that all user-visible strings go through QObject::tr(),
1539 /*! \fn QString &QString::append(const char *str)
1543 Appends the string \a str to this string. The given const char
1544 pointer is converted to Unicode using the fromUtf8() function.
1546 You can disable this function by defining \c QT_NO_CAST_FROM_ASCII
1547 when you compile your applications. This can be useful if you want
1548 to ensure that all user-visible strings go through QObject::tr(),
1555 Appends the character \a ch to this string.
1557 QString &QString::append(QChar ch)
1559 if (d->ref.isShared() || uint(d->size) + 2u > d->alloc)
1560 reallocData(uint(d->size) + 2u, true);
1561 d->data()[d->size++] = ch.unicode();
1562 d->data()[d->size] = '\0';
1566 /*! \fn QString &QString::prepend(const QString &str)
1568 Prepends the string \a str to the beginning of this string and
1569 returns a reference to this string.
1573 \snippet qstring/main.cpp 36
1575 \sa append(), insert()
1578 /*! \fn QString &QString::prepend(QLatin1String str)
1582 Prepends the Latin-1 string \a str to this string.
1585 /*! \fn QString &QString::prepend(const QByteArray &ba)
1589 Prepends the byte array \a ba to this string. The byte array is
1590 converted to Unicode using the fromUtf8() function.
1592 You can disable this function by defining \c
1593 QT_NO_CAST_FROM_ASCII when you compile your applications. This
1594 can be useful if you want to ensure that all user-visible strings
1595 go through QObject::tr(), for example.
1598 /*! \fn QString &QString::prepend(const char *str)
1602 Prepends the string \a str to this string. The const char pointer
1603 is converted to Unicode using the fromUtf8() function.
1605 You can disable this function by defining \c
1606 QT_NO_CAST_FROM_ASCII when you compile your applications. This
1607 can be useful if you want to ensure that all user-visible strings
1608 go through QObject::tr(), for example.
1611 /*! \fn QString &QString::prepend(QChar ch)
1615 Prepends the character \a ch to this string.
1619 \fn QString &QString::remove(int position, int n)
1621 Removes \a n characters from the string, starting at the given \a
1622 position index, and returns a reference to the string.
1624 If the specified \a position index is within the string, but \a
1625 position + \a n is beyond the end of the string, the string is
1626 truncated at the specified \a position.
1628 \snippet qstring/main.cpp 37
1630 \sa insert(), replace()
1632 QString &QString::remove(int pos, int len)
1634 if (pos < 0) // count from end of string
1636 if (pos < 0 || pos >= d->size) {
1638 } else if (len >= d->size - pos) {
1639 resize(pos); // truncate
1640 } else if (len > 0) {
1642 memmove(d->data() + pos, d->data() + pos + len,
1643 (d->size - pos - len + 1) * sizeof(ushort));
1650 Removes every occurrence of the given \a str string in this
1651 string, and returns a reference to this string.
1653 If \a cs is Qt::CaseSensitive (default), the search is
1654 case sensitive; otherwise the search is case insensitive.
1656 This is the same as \c replace(str, "", cs).
1660 QString &QString::remove(const QString &str, Qt::CaseSensitivity cs)
1664 while ((i = indexOf(str, i, cs)) != -1)
1665 remove(i, str.d->size);
1671 Removes every occurrence of the character \a ch in this string, and
1672 returns a reference to this string.
1674 If \a cs is Qt::CaseSensitive (default), the search is case
1675 sensitive; otherwise the search is case insensitive.
1679 \snippet qstring/main.cpp 38
1681 This is the same as \c replace(ch, "", cs).
1685 QString &QString::remove(QChar ch, Qt::CaseSensitivity cs)
1688 ushort c = ch.unicode();
1689 if (cs == Qt::CaseSensitive) {
1691 if (d->data()[i] == ch)
1698 if (foldCase(d->data()[i]) == c)
1707 \fn QString &QString::remove(const QRegExp &rx)
1709 Removes every occurrence of the regular expression \a rx in the
1710 string, and returns a reference to the string. For example:
1712 \snippet qstring/main.cpp 39
1714 \sa indexOf(), lastIndexOf(), replace()
1718 \fn QString &QString::remove(const QRegularExpression &re)
1721 Removes every occurrence of the regular expression \a re in the
1722 string, and returns a reference to the string. For example:
1724 \snippet qstring/main.cpp 96
1726 \sa indexOf(), lastIndexOf(), replace()
1730 \fn QString &QString::replace(int position, int n, const QString &after)
1732 Replaces \a n characters beginning at index \a position with
1733 the string \a after and returns a reference to this string.
1737 \snippet qstring/main.cpp 40
1739 \sa insert(), remove()
1741 QString &QString::replace(int pos, int len, const QString &after)
1743 QString copy = after;
1744 return replace(pos, len, copy.constData(), copy.length());
1748 \fn QString &QString::replace(int position, int n, const QChar *unicode, int size)
1750 Replaces \a n characters beginning at index \a position with the
1751 first \a size characters of the QChar array \a unicode and returns a
1752 reference to this string.
1754 QString &QString::replace(int pos, int len, const QChar *unicode, int size)
1756 if (pos < 0 || pos > d->size)
1758 if (pos + len > d->size)
1759 len = d->size - pos;
1762 replace_helper(&index, 1, len, unicode, size);
1767 \fn QString &QString::replace(int position, int n, QChar after)
1770 Replaces \a n characters beginning at index \a position with the
1771 character \a after and returns a reference to this string.
1773 QString &QString::replace(int pos, int len, QChar after)
1775 return replace(pos, len, &after, 1);
1780 Replaces every occurrence of the string \a before with the string \a
1781 after and returns a reference to this string.
1783 If \a cs is Qt::CaseSensitive (default), the search is case
1784 sensitive; otherwise the search is case insensitive.
1788 \snippet qstring/main.cpp 41
1790 \note The replacement text is not rescanned after it is inserted.
1794 \snippet qstring/main.cpp 86
1796 QString &QString::replace(const QString &before, const QString &after, Qt::CaseSensitivity cs)
1798 return replace(before.constData(), before.size(), after.constData(), after.size(), cs);
1804 void QString::replace_helper(uint *indices, int nIndices, int blen, const QChar *after, int alen)
1806 // copy *after in case it lies inside our own d->data() area
1807 // (which we could possibly invalidate via a realloc or corrupt via memcpy operations.)
1808 QChar *afterBuffer = const_cast<QChar *>(after);
1809 if (after >= reinterpret_cast<QChar *>(d->data()) && after < reinterpret_cast<QChar *>(d->data()) + d->size) {
1810 afterBuffer = static_cast<QChar *>(::malloc(alen*sizeof(QChar)));
1811 Q_CHECK_PTR(afterBuffer);
1812 ::memcpy(afterBuffer, after, alen*sizeof(QChar));
1819 for (int i = 0; i < nIndices; ++i)
1820 memcpy(d->data() + indices[i], afterBuffer, alen * sizeof(QChar));
1821 } else if (alen < blen) {
1822 // replace from front
1824 uint to = indices[0];
1826 memcpy(d->data()+to, after, alen*sizeof(QChar));
1828 uint movestart = indices[0] + blen;
1829 for (int i = 1; i < nIndices; ++i) {
1830 int msize = indices[i] - movestart;
1832 memmove(d->data() + to, d->data() + movestart, msize * sizeof(QChar));
1836 memcpy(d->data() + to, afterBuffer, alen*sizeof(QChar));
1839 movestart = indices[i] + blen;
1841 int msize = d->size - movestart;
1843 memmove(d->data() + to, d->data() + movestart, msize * sizeof(QChar));
1844 resize(d->size - nIndices*(blen-alen));
1846 // replace from back
1847 int adjust = nIndices*(alen-blen);
1848 int newLen = d->size + adjust;
1849 int moveend = d->size;
1854 int movestart = indices[nIndices] + blen;
1855 int insertstart = indices[nIndices] + nIndices*(alen-blen);
1856 int moveto = insertstart + alen;
1857 memmove(d->data() + moveto, d->data() + movestart,
1858 (moveend - movestart)*sizeof(QChar));
1859 memcpy(d->data() + insertstart, afterBuffer, alen*sizeof(QChar));
1860 moveend = movestart-blen;
1863 } QT_CATCH(const std::bad_alloc &) {
1864 if (afterBuffer != after)
1865 ::free(afterBuffer);
1868 if (afterBuffer != after)
1869 ::free(afterBuffer);
1876 Replaces each occurrence in this string of the first \a blen
1877 characters of \a before with the first \a alen characters of \a
1878 after and returns a reference to this string.
1880 If \a cs is Qt::CaseSensitive (default), the search is case
1881 sensitive; otherwise the search is case insensitive.
1883 QString &QString::replace(const QChar *before, int blen,
1884 const QChar *after, int alen,
1885 Qt::CaseSensitivity cs)
1891 if (cs == Qt::CaseSensitive && before == after && blen == alen)
1894 if (alen == 0 && blen == 0)
1897 QStringMatcher matcher(before, blen, cs);
1903 while (pos < 1023) {
1904 index = matcher.indexIn(*this, index);
1907 indices[pos++] = index;
1909 // avoid infinite loop
1916 replace_helper(indices, pos, blen, after, alen);
1920 // index has to be adjusted in case we get back into the loop above.
1921 index += pos*(alen-blen);
1929 Replaces every occurrence of the character \a ch in the string with
1930 \a after and returns a reference to this string.
1932 If \a cs is Qt::CaseSensitive (default), the search is case
1933 sensitive; otherwise the search is case insensitive.
1935 QString& QString::replace(QChar ch, const QString &after, Qt::CaseSensitivity cs)
1937 if (after.d->size == 0)
1938 return remove(ch, cs);
1940 if (after.d->size == 1)
1941 return replace(ch, after.d->data()[0], cs);
1946 ushort cc = (cs == Qt::CaseSensitive ? ch.unicode() : ch.toCaseFolded().unicode());
1952 if (cs == Qt::CaseSensitive) {
1953 while (pos < 1023 && index < d->size) {
1954 if (d->data()[index] == cc)
1955 indices[pos++] = index;
1959 while (pos < 1023 && index < d->size) {
1960 if (QChar::toCaseFolded(d->data()[index]) == cc)
1961 indices[pos++] = index;
1968 replace_helper(indices, pos, 1, after.constData(), after.d->size);
1972 // index has to be adjusted in case we get back into the loop above.
1973 index += pos*(after.d->size - 1);
1980 Replaces every occurrence of the character \a before with the
1981 character \a after and returns a reference to this string.
1983 If \a cs is Qt::CaseSensitive (default), the search is case
1984 sensitive; otherwise the search is case insensitive.
1986 QString& QString::replace(QChar before, QChar after, Qt::CaseSensitivity cs)
1988 ushort a = after.unicode();
1989 ushort b = before.unicode();
1992 ushort *i = d->data();
1993 const ushort *e = i + d->size;
1994 if (cs == Qt::CaseSensitive) {
2001 if (foldCase(*i) == b)
2012 Replaces every occurrence of the string \a before with the string \a
2013 after and returns a reference to this string.
2015 If \a cs is Qt::CaseSensitive (default), the search is case
2016 sensitive; otherwise the search is case insensitive.
2018 \note The text is not rescanned after a replacement.
2020 QString &QString::replace(QLatin1String before, QLatin1String after, Qt::CaseSensitivity cs)
2022 int alen = after.size();
2023 QVarLengthArray<ushort> a(alen);
2024 for (int i = 0; i < alen; ++i)
2025 a[i] = (uchar)after.latin1()[i];
2026 int blen = before.size();
2027 QVarLengthArray<ushort> b(blen);
2028 for (int i = 0; i < blen; ++i)
2029 b[i] = (uchar)before.latin1()[i];
2030 return replace((const QChar *)b.data(), blen, (const QChar *)a.data(), alen, cs);
2037 Replaces every occurrence of the string \a before with the string \a
2038 after and returns a reference to this string.
2040 If \a cs is Qt::CaseSensitive (default), the search is case
2041 sensitive; otherwise the search is case insensitive.
2043 \note The text is not rescanned after a replacement.
2045 QString &QString::replace(QLatin1String before, const QString &after, Qt::CaseSensitivity cs)
2047 int blen = before.size();
2048 QVarLengthArray<ushort> b(blen);
2049 for (int i = 0; i < blen; ++i)
2050 b[i] = (uchar)before.latin1()[i];
2051 return replace((const QChar *)b.data(), blen, after.constData(), after.d->size, cs);
2058 Replaces every occurrence of the string \a before with the string \a
2059 after and returns a reference to this string.
2061 If \a cs is Qt::CaseSensitive (default), the search is case
2062 sensitive; otherwise the search is case insensitive.
2064 \note The text is not rescanned after a replacement.
2066 QString &QString::replace(const QString &before, QLatin1String after, Qt::CaseSensitivity cs)
2068 int alen = after.size();
2069 QVarLengthArray<ushort> a(alen);
2070 for (int i = 0; i < alen; ++i)
2071 a[i] = (uchar)after.latin1()[i];
2072 return replace(before.constData(), before.d->size, (const QChar *)a.data(), alen, cs);
2079 Replaces every occurrence of the character \a c with the string \a
2080 after and returns a reference to this string.
2082 If \a cs is Qt::CaseSensitive (default), the search is case
2083 sensitive; otherwise the search is case insensitive.
2085 \note The text is not rescanned after a replacement.
2087 QString &QString::replace(QChar c, QLatin1String after, Qt::CaseSensitivity cs)
2089 int alen = after.size();
2090 QVarLengthArray<ushort> a(alen);
2091 for (int i = 0; i < alen; ++i)
2092 a[i] = (uchar)after.latin1()[i];
2093 return replace(&c, 1, (const QChar *)a.data(), alen, cs);
2099 Returns true if string \a s1 is equal to string \a s2; otherwise
2102 The comparison is based exclusively on the numeric Unicode values of
2103 the characters and is very fast, but is not what a human would
2104 expect. Consider sorting user-interface strings with
2105 localeAwareCompare().
2107 bool operator==(const QString &s1, const QString &s2)
2109 if (s1.d->size != s2.d->size)
2112 return qMemEquals(s1.d->data(), s2.d->data(), s1.d->size);
2116 \overload operator==()
2118 bool QString::operator==(QLatin1String other) const
2120 if (d->size != other.size())
2126 const ushort *uc = d->data();
2127 const ushort *e = uc + d->size;
2128 const uchar *c = (uchar *)other.latin1();
2139 /*! \fn bool QString::operator==(const QByteArray &other) const
2141 \overload operator==()
2143 The \a other byte array is converted to a QString using the
2144 fromUtf8() function. This function stops conversion at the
2145 first NUL character found, or the end of the byte array.
2147 You can disable this operator by defining \c
2148 QT_NO_CAST_FROM_ASCII when you compile your applications. This
2149 can be useful if you want to ensure that all user-visible strings
2150 go through QObject::tr(), for example.
2153 /*! \fn bool QString::operator==(const char *other) const
2155 \overload operator==()
2157 The \a other const char pointer is converted to a QString using
2158 the fromUtf8() function.
2160 You can disable this operator by defining \c
2161 QT_NO_CAST_FROM_ASCII when you compile your applications. This
2162 can be useful if you want to ensure that all user-visible strings
2163 go through QObject::tr(), for example.
2168 Returns true if string \a s1 is lexically less than string
2169 \a s2; otherwise returns false.
2171 The comparison is based exclusively on the numeric Unicode values
2172 of the characters and is very fast, but is not what a human would
2173 expect. Consider sorting user-interface strings using the
2174 QString::localeAwareCompare() function.
2176 bool operator<(const QString &s1, const QString &s2)
2178 return ucstrcmp(s1.constData(), s1.length(), s2.constData(), s2.length()) < 0;
2182 \overload operator<()
2184 bool QString::operator<(QLatin1String other) const
2186 const uchar *c = (uchar *) other.latin1();
2190 const ushort *uc = d->data();
2191 const ushort *e = uc + qMin(d->size, other.size());
2199 return (uc == e ? d->size < other.size() : *uc < *c);
2202 /*! \fn bool QString::operator<(const QByteArray &other) const
2204 \overload operator<()
2206 The \a other byte array is converted to a QString using the
2207 fromUtf8() function. If any NUL characters ('\\0') are embedded
2208 in the byte array, they will be included in the transformation.
2210 You can disable this operator by defining \c
2211 QT_NO_CAST_FROM_ASCII when you compile your applications. This
2212 can be useful if you want to ensure that all user-visible strings
2213 go through QObject::tr(), for example.
2216 /*! \fn bool QString::operator<(const char *other) const
2218 \overload operator<()
2220 The \a other const char pointer is converted to a QString using
2221 the fromUtf8() function.
2223 You can disable this operator by defining \c
2224 QT_NO_CAST_FROM_ASCII when you compile your applications. This
2225 can be useful if you want to ensure that all user-visible strings
2226 go through QObject::tr(), for example.
2229 /*! \fn bool QString::operator<=(const QString &s1, const QString &s2)
2231 Returns true if string \a s1 is lexically less than or equal to
2232 string \a s2; otherwise returns false.
2234 The comparison is based exclusively on the numeric Unicode values
2235 of the characters and is very fast, but is not what a human would
2236 expect. Consider sorting user-interface strings with
2237 localeAwareCompare().
2240 /*! \fn bool QString::operator<=(QLatin1String other) const
2242 \overload operator<=()
2245 /*! \fn bool QString::operator<=(const QByteArray &other) const
2247 \overload operator<=()
2249 The \a other byte array is converted to a QString using the
2250 fromUtf8() function. If any NUL characters ('\\0') are embedded
2251 in the byte array, they will be included in the transformation.
2253 You can disable this operator by defining \c
2254 QT_NO_CAST_FROM_ASCII when you compile your applications. This
2255 can be useful if you want to ensure that all user-visible strings
2256 go through QObject::tr(), for example.
2259 /*! \fn bool QString::operator<=(const char *other) const
2261 \overload operator<=()
2263 The \a other const char pointer is converted to a QString using
2264 the fromUtf8() function.
2266 You can disable this operator by defining \c
2267 QT_NO_CAST_FROM_ASCII when you compile your applications. This
2268 can be useful if you want to ensure that all user-visible strings
2269 go through QObject::tr(), for example.
2272 /*! \fn bool QString::operator>(const QString &s1, const QString &s2)
2274 Returns true if string \a s1 is lexically greater than string \a
2275 s2; otherwise returns false.
2277 The comparison is based exclusively on the numeric Unicode values
2278 of the characters and is very fast, but is not what a human would
2279 expect. Consider sorting user-interface strings with
2280 localeAwareCompare().
2284 \overload operator>()
2286 bool QString::operator>(QLatin1String other) const
2288 const uchar *c = (uchar *) other.latin1();
2289 if (!c || *c == '\0')
2292 const ushort *uc = d->data();
2293 const ushort *e = uc + qMin(d->size, other.size());
2301 return (uc == e) ? d->size > other.size() : *uc > *c;
2304 /*! \fn bool QString::operator>(const QByteArray &other) const
2306 \overload operator>()
2308 The \a other byte array is converted to a QString using the
2309 fromUtf8() function. If any NUL characters ('\\0') are embedded
2310 in the byte array, they will be included in the transformation.
2312 You can disable this operator by defining \c
2313 QT_NO_CAST_FROM_ASCII when you compile your applications. This
2314 can be useful if you want to ensure that all user-visible strings
2315 go through QObject::tr(), for example.
2318 /*! \fn bool QString::operator>(const char *other) const
2320 \overload operator>()
2322 The \a other const char pointer is converted to a QString using
2323 the fromUtf8() function.
2325 You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII
2326 when you compile your applications. This can be useful if you want
2327 to ensure that all user-visible strings go through QObject::tr(),
2331 /*! \fn bool operator>=(const QString &s1, const QString &s2)
2334 Returns true if string \a s1 is lexically greater than or equal to
2335 string \a s2; otherwise returns false.
2337 The comparison is based exclusively on the numeric Unicode values
2338 of the characters and is very fast, but is not what a human would
2339 expect. Consider sorting user-interface strings with
2340 localeAwareCompare().
2343 /*! \fn bool QString::operator>=(QLatin1String other) const
2345 \overload operator>=()
2348 /*! \fn bool QString::operator>=(const QByteArray &other) const
2350 \overload operator>=()
2352 The \a other byte array is converted to a QString using the
2353 fromUtf8() function. If any NUL characters ('\\0') are embedded in
2354 the byte array, they will be included in the transformation.
2356 You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII
2357 when you compile your applications. This can be useful if you want
2358 to ensure that all user-visible strings go through QObject::tr(),
2362 /*! \fn bool QString::operator>=(const char *other) const
2364 \overload operator>=()
2366 The \a other const char pointer is converted to a QString using
2367 the fromUtf8() function.
2369 You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII
2370 when you compile your applications. This can be useful if you want
2371 to ensure that all user-visible strings go through QObject::tr(),
2375 /*! \fn bool operator!=(const QString &s1, const QString &s2)
2378 Returns true if string \a s1 is not equal to string \a s2;
2379 otherwise returns false.
2381 The comparison is based exclusively on the numeric Unicode values
2382 of the characters and is very fast, but is not what a human would
2383 expect. Consider sorting user-interface strings with
2384 localeAwareCompare().
2387 /*! \fn bool QString::operator!=(QLatin1String other) const
2389 \overload operator!=()
2392 /*! \fn bool QString::operator!=(const QByteArray &other) const
2394 \overload operator!=()
2396 The \a other byte array is converted to a QString using the
2397 fromUtf8() function. If any NUL characters ('\\0') are embedded
2398 in the byte array, they will be included in the transformation.
2400 You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII
2401 when you compile your applications. This can be useful if you want
2402 to ensure that all user-visible strings go through QObject::tr(),
2406 /*! \fn bool QString::operator!=(const char *other) const
2408 \overload operator!=()
2410 The \a other const char pointer is converted to a QString using
2411 the fromUtf8() function.
2413 You can disable this operator by defining \c
2414 QT_NO_CAST_FROM_ASCII when you compile your applications. This
2415 can be useful if you want to ensure that all user-visible strings
2416 go through QObject::tr(), for example.
2420 Returns the index position of the first occurrence of the string \a
2421 str in this string, searching forward from index position \a
2422 from. Returns -1 if \a str is not found.
2424 If \a cs is Qt::CaseSensitive (default), the search is case
2425 sensitive; otherwise the search is case insensitive.
2429 \snippet qstring/main.cpp 24
2431 If \a from is -1, the search starts at the last character; if it is
2432 -2, at the next to last character and so on.
2434 \sa lastIndexOf(), contains(), count()
2436 int QString::indexOf(const QString &str, int from, Qt::CaseSensitivity cs) const
2438 return qFindString(unicode(), length(), from, str.unicode(), str.length(), cs);
2443 Returns the index position of the first occurrence of the string \a
2444 str in this string, searching forward from index position \a
2445 from. Returns -1 if \a str is not found.
2447 If \a cs is Qt::CaseSensitive (default), the search is case
2448 sensitive; otherwise the search is case insensitive.
2452 \snippet qstring/main.cpp 24
2454 If \a from is -1, the search starts at the last character; if it is
2455 -2, at the next to last character and so on.
2457 \sa lastIndexOf(), contains(), count()
2460 int QString::indexOf(QLatin1String str, int from, Qt::CaseSensitivity cs) const
2462 return qt_find_latin1_string(unicode(), size(), str, from, cs);
2466 const QChar *haystack0, int haystackLen, int from,
2467 const QChar *needle0, int needleLen, Qt::CaseSensitivity cs)
2469 const int l = haystackLen;
2470 const int sl = needleLen;
2473 if (uint(sl + from) > (uint)l)
2481 return findChar(haystack0, haystackLen, needle0[0], from, cs);
2484 We use the Boyer-Moore algorithm in cases where the overhead
2485 for the skip table should pay off, otherwise we use a simple
2488 if (l > 500 && sl > 5)
2489 return qFindStringBoyerMoore(haystack0, haystackLen, from,
2490 needle0, needleLen, cs);
2493 We use some hashing for efficiency's sake. Instead of
2494 comparing strings, we compare the hash value of str with that
2495 of a part of this QString. Only if that matches, we call
2496 ucstrncmp() or ucstrnicmp().
2498 const ushort *needle = (const ushort *)needle0;
2499 const ushort *haystack = (const ushort *)haystack0 + from;
2500 const ushort *end = (const ushort *)haystack0 + (l-sl);
2501 const int sl_minus_1 = sl-1;
2502 int hashNeedle = 0, hashHaystack = 0, idx;
2504 if (cs == Qt::CaseSensitive) {
2505 for (idx = 0; idx < sl; ++idx) {
2506 hashNeedle = ((hashNeedle<<1) + needle[idx]);
2507 hashHaystack = ((hashHaystack<<1) + haystack[idx]);
2509 hashHaystack -= haystack[sl_minus_1];
2511 while (haystack <= end) {
2512 hashHaystack += haystack[sl_minus_1];
2513 if (hashHaystack == hashNeedle
2514 && ucstrncmp((const QChar *)needle, (const QChar *)haystack, sl) == 0)
2515 return haystack - (const ushort *)haystack0;
2521 const ushort *haystack_start = (const ushort *)haystack0;
2522 for (idx = 0; idx < sl; ++idx) {
2523 hashNeedle = (hashNeedle<<1) + foldCase(needle + idx, needle);
2524 hashHaystack = (hashHaystack<<1) + foldCase(haystack + idx, haystack_start);
2526 hashHaystack -= foldCase(haystack + sl_minus_1, haystack_start);
2528 while (haystack <= end) {
2529 hashHaystack += foldCase(haystack + sl_minus_1, haystack_start);
2530 if (hashHaystack == hashNeedle && ucstrnicmp(needle, haystack, sl) == 0)
2531 return haystack - (const ushort *)haystack0;
2533 REHASH(foldCase(haystack, haystack_start));
2543 Returns the index position of the first occurrence of the
2544 character \a ch in the string, searching forward from index
2545 position \a from. Returns -1 if \a ch could not be found.
2547 int QString::indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
2549 return findChar(unicode(), length(), ch, from, cs);
2557 Returns the index position of the first occurrence of the string
2558 reference \a str in this string, searching forward from index
2559 position \a from. Returns -1 if \a str is not found.
2561 If \a cs is Qt::CaseSensitive (default), the search is case
2562 sensitive; otherwise the search is case insensitive.
2564 int QString::indexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) const
2566 return qFindString(unicode(), length(), from, str.unicode(), str.length(), cs);
2569 static int lastIndexOfHelper(const ushort *haystack, int from, const ushort *needle, int sl, Qt::CaseSensitivity cs)
2572 See indexOf() for explanations.
2575 const ushort *end = haystack;
2577 const int sl_minus_1 = sl-1;
2578 const ushort *n = needle+sl_minus_1;
2579 const ushort *h = haystack+sl_minus_1;
2580 int hashNeedle = 0, hashHaystack = 0, idx;
2582 if (cs == Qt::CaseSensitive) {
2583 for (idx = 0; idx < sl; ++idx) {
2584 hashNeedle = ((hashNeedle<<1) + *(n-idx));
2585 hashHaystack = ((hashHaystack<<1) + *(h-idx));
2587 hashHaystack -= *haystack;
2589 while (haystack >= end) {
2590 hashHaystack += *haystack;
2591 if (hashHaystack == hashNeedle
2592 && ucstrncmp((const QChar *)needle, (const QChar *)haystack, sl) == 0)
2593 return haystack - end;
2595 REHASH(haystack[sl]);
2598 for (idx = 0; idx < sl; ++idx) {
2599 hashNeedle = ((hashNeedle<<1) + foldCase(n-idx, needle));
2600 hashHaystack = ((hashHaystack<<1) + foldCase(h-idx, end));
2602 hashHaystack -= foldCase(haystack, end);
2604 while (haystack >= end) {
2605 hashHaystack += foldCase(haystack, end);
2606 if (hashHaystack == hashNeedle && ucstrnicmp(needle, haystack, sl) == 0)
2607 return haystack - end;
2609 REHASH(foldCase(haystack + sl, end));
2616 Returns the index position of the last occurrence of the string \a
2617 str in this string, searching backward from index position \a
2618 from. If \a from is -1 (default), the search starts at the last
2619 character; if \a from is -2, at the next to last character and so
2620 on. Returns -1 if \a str is not found.
2622 If \a cs is Qt::CaseSensitive (default), the search is case
2623 sensitive; otherwise the search is case insensitive.
2627 \snippet qstring/main.cpp 29
2629 \sa indexOf(), contains(), count()
2631 int QString::lastIndexOf(const QString &str, int from, Qt::CaseSensitivity cs) const
2633 const int sl = str.d->size;
2635 return lastIndexOf(QChar(str.d->data()[0]), from, cs);
2637 const int l = d->size;
2641 if (from == l && sl == 0)
2643 if (from < 0 || from >= l || delta < 0)
2648 return lastIndexOfHelper(d->data(), from, str.d->data(), str.d->size, cs);
2653 \overload lastIndexOf()
2655 Returns the index position of the last occurrence of the string \a
2656 str in this string, searching backward from index position \a
2657 from. If \a from is -1 (default), the search starts at the last
2658 character; if \a from is -2, at the next to last character and so
2659 on. Returns -1 if \a str is not found.
2661 If \a cs is Qt::CaseSensitive (default), the search is case
2662 sensitive; otherwise the search is case insensitive.
2666 \snippet qstring/main.cpp 29
2668 \sa indexOf(), contains(), count()
2670 int QString::lastIndexOf(QLatin1String str, int from, Qt::CaseSensitivity cs) const
2672 const int sl = str.size();
2674 return lastIndexOf(QLatin1Char(str.latin1()[0]), from, cs);
2676 const int l = d->size;
2680 if (from == l && sl == 0)
2682 if (from < 0 || from >= l || delta < 0)
2687 QVarLengthArray<ushort> s(sl);
2688 for (int i = 0; i < sl; ++i)
2689 s[i] = str.latin1()[i];
2691 return lastIndexOfHelper(d->data(), from, s.data(), sl, cs);
2695 \overload lastIndexOf()
2697 Returns the index position of the last occurrence of the character
2698 \a ch, searching backward from position \a from.
2700 int QString::lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
2702 return qt_last_index_of(unicode(), size(), ch, from, cs);
2707 \overload lastIndexOf()
2709 Returns the index position of the last occurrence of the string
2710 reference \a str in this string, searching backward from index
2711 position \a from. If \a from is -1 (default), the search starts at
2712 the last character; if \a from is -2, at the next to last character
2713 and so on. Returns -1 if \a str is not found.
2715 If \a cs is Qt::CaseSensitive (default), the search is case
2716 sensitive; otherwise the search is case insensitive.
2718 \sa indexOf(), contains(), count()
2720 int QString::lastIndexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) const
2722 const int sl = str.size();
2724 return lastIndexOf(str.at(0), from, cs);
2726 const int l = d->size;
2730 if (from == l && sl == 0)
2732 if (from < 0 || from >= l || delta < 0)
2737 return lastIndexOfHelper(d->data(), from, reinterpret_cast<const ushort*>(str.unicode()),
2741 #ifndef QT_NO_REGEXP
2742 struct QStringCapture
2752 Replaces every occurrence of the regular expression \a rx in the
2753 string with \a after. Returns a reference to the string. For
2756 \snippet qstring/main.cpp 42
2758 For regular expressions containing \l{capturing parentheses},
2759 occurrences of \b{\\1}, \b{\\2}, ..., in \a after are replaced
2760 with \a{rx}.cap(1), cap(2), ...
2762 \snippet qstring/main.cpp 43
2764 \sa indexOf(), lastIndexOf(), remove(), QRegExp::cap()
2766 QString& QString::replace(const QRegExp &rx, const QString &after)
2770 if (isEmpty() && rx2.indexIn(*this) == -1)
2773 reallocData(uint(d->size) + 1u);
2776 int numCaptures = rx2.captureCount();
2777 int al = after.length();
2778 QRegExp::CaretMode caretMode = QRegExp::CaretAtZero;
2780 if (numCaptures > 0) {
2781 const QChar *uc = after.unicode();
2782 int numBackRefs = 0;
2784 for (int i = 0; i < al - 1; i++) {
2785 if (uc[i] == QLatin1Char('\\')) {
2786 int no = uc[i + 1].digitValue();
2787 if (no > 0 && no <= numCaptures)
2793 This is the harder case where we have back-references.
2795 if (numBackRefs > 0) {
2796 QVarLengthArray<QStringCapture, 16> captures(numBackRefs);
2799 for (int i = 0; i < al - 1; i++) {
2800 if (uc[i] == QLatin1Char('\\')) {
2801 int no = uc[i + 1].digitValue();
2802 if (no > 0 && no <= numCaptures) {
2803 QStringCapture capture;
2808 int secondDigit = uc[i + 2].digitValue();
2809 if (secondDigit != -1 && ((no * 10) + secondDigit) <= numCaptures) {
2810 no = (no * 10) + secondDigit;
2816 captures[j++] = capture;
2821 while (index <= length()) {
2822 index = rx2.indexIn(*this, index, caretMode);
2826 QString after2(after);
2827 for (j = numBackRefs - 1; j >= 0; j--) {
2828 const QStringCapture &capture = captures[j];
2829 after2.replace(capture.pos, capture.len, rx2.cap(capture.no));
2832 replace(index, rx2.matchedLength(), after2);
2833 index += after2.length();
2835 // avoid infinite loop on 0-length matches (e.g., QRegExp("[a-z]*"))
2836 if (rx2.matchedLength() == 0)
2839 caretMode = QRegExp::CaretWontMatch;
2846 This is the simple and optimized case where we don't have
2849 while (index != -1) {
2853 } replacements[2048];
2857 while (pos < 2047) {
2858 index = rx2.indexIn(*this, index, caretMode);
2861 int ml = rx2.matchedLength();
2862 replacements[pos].pos = index;
2863 replacements[pos++].length = ml;
2866 // avoid infinite loop
2872 replacements[pos].pos = d->size;
2873 int newlen = d->size + adjust;
2875 // to continue searching at the right position after we did
2876 // the first round of replacements
2880 newstring.reserve(newlen + 1);
2881 QChar *newuc = newstring.data();
2886 int copyend = replacements[i].pos;
2887 int size = copyend - copystart;
2888 memcpy(uc, d->data() + copystart, size * sizeof(QChar));
2890 memcpy(uc, after.d->data(), al * sizeof(QChar));
2892 copystart = copyend + replacements[i].length;
2895 memcpy(uc, d->data() + copystart, (d->size - copystart) * sizeof(QChar));
2896 newstring.resize(newlen);
2898 caretMode = QRegExp::CaretWontMatch;
2904 #ifndef QT_NO_REGEXP
2905 #ifndef QT_BOOTSTRAPPED
2910 Replaces every occurrence of the regular expression \a re in the
2911 string with \a after. Returns a reference to the string. For
2914 \snippet qstring/main.cpp 87
2916 For regular expressions containing capturing groups,
2917 occurrences of \b{\\1}, \b{\\2}, ..., in \a after are replaced
2918 with the string captured by the corresponding capturing group.
2920 \snippet qstring/main.cpp 88
2922 \sa indexOf(), lastIndexOf(), remove(), QRegularExpression, QRegularExpressionMatch
2924 QString &QString::replace(const QRegularExpression &re, const QString &after)
2926 if (!re.isValid()) {
2927 qWarning("QString::replace: invalid QRegularExpresssion object");
2931 const QString copy(*this);
2932 QRegularExpressionMatchIterator iterator = re.globalMatch(copy);
2933 if (!iterator.hasNext()) // no matches at all
2936 reallocData(uint(d->size) + 1u);
2938 int numCaptures = re.captureCount();
2940 // 1. build the backreferences vector, holding where the backreferences
2941 // are in the replacement string
2942 QVector<QStringCapture> backReferences;
2943 const int al = after.length();
2944 const QChar *ac = after.unicode();
2946 for (int i = 0; i < al - 1; i++) {
2947 if (ac[i] == QLatin1Char('\\')) {
2948 int no = ac[i + 1].digitValue();
2949 if (no > 0 && no <= numCaptures) {
2950 QStringCapture backReference;
2951 backReference.pos = i;
2952 backReference.len = 2;
2955 int secondDigit = ac[i + 2].digitValue();
2956 if (secondDigit != -1 && ((no * 10) + secondDigit) <= numCaptures) {
2957 no = (no * 10) + secondDigit;
2958 ++backReference.len;
2962 backReference.no = no;
2963 backReferences.append(backReference);
2968 // 2. iterate on the matches. For every match, copy in chunks
2969 // - the part before the match
2970 // - the after string, with the proper replacements for the backreferences
2972 int newLength = 0; // length of the new string, with all the replacements
2974 QVector<QStringRef> chunks;
2975 while (iterator.hasNext()) {
2976 QRegularExpressionMatch match = iterator.next();
2978 // add the part before the match
2979 len = match.capturedStart() - lastEnd;
2981 chunks << copy.midRef(lastEnd, len);
2986 // add the after string, with replacements for the backreferences
2987 foreach (const QStringCapture &backReference, backReferences) {
2988 // part of "after" before the backreference
2989 len = backReference.pos - lastEnd;
2991 chunks << after.midRef(lastEnd, len);
2995 // backreference itself
2996 len = match.capturedLength(backReference.no);
2998 chunks << copy.midRef(match.capturedStart(backReference.no), len);
3002 lastEnd = backReference.pos + backReference.len;
3005 // add the last part of the after string
3006 len = after.length() - lastEnd;
3008 chunks << after.midRef(lastEnd, len);
3012 lastEnd = match.capturedEnd();
3015 // 3. trailing string after the last match
3016 if (copy.length() > lastEnd) {
3017 chunks << copy.midRef(lastEnd);
3018 newLength += copy.length() - lastEnd;
3021 // 4. assemble the chunks together
3025 foreach (const QStringRef &chunk, chunks) {
3026 int len = chunk.length();
3027 memcpy(uc + i, chunk.unicode(), len * sizeof(QChar));
3033 #endif // QT_BOOTSTRAPPED
3034 #endif // QT_NO_REGEXP
3037 Returns the number of (potentially overlapping) occurrences of
3038 the string \a str in this string.
3040 If \a cs is Qt::CaseSensitive (default), the search is
3041 case sensitive; otherwise the search is case insensitive.
3043 \sa contains(), indexOf()
3046 int QString::count(const QString &str, Qt::CaseSensitivity cs) const
3048 return qt_string_count(unicode(), size(), str.unicode(), str.size(), cs);
3054 Returns the number of occurrences of character \a ch in the string.
3057 int QString::count(QChar ch, Qt::CaseSensitivity cs) const
3059 return qt_string_count(unicode(), size(), ch, cs);
3065 Returns the number of (potentially overlapping) occurrences of the
3066 string reference \a str in this string.
3068 If \a cs is Qt::CaseSensitive (default), the search is
3069 case sensitive; otherwise the search is case insensitive.
3071 \sa contains(), indexOf()
3073 int QString::count(const QStringRef &str, Qt::CaseSensitivity cs) const
3075 return qt_string_count(unicode(), size(), str.unicode(), str.size(), cs);
3079 /*! \fn bool QString::contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
3081 Returns true if this string contains an occurrence of the string
3082 \a str; otherwise returns false.
3084 If \a cs is Qt::CaseSensitive (default), the search is
3085 case sensitive; otherwise the search is case insensitive.
3088 \snippet qstring/main.cpp 17
3090 \sa indexOf(), count()
3093 /*! \fn bool QString::contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
3095 \overload contains()
3097 Returns true if this string contains an occurrence of the
3098 character \a ch; otherwise returns false.
3101 /*! \fn bool QString::contains(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
3104 Returns true if this string contains an occurrence of the string
3105 reference \a str; otherwise returns false.
3107 If \a cs is Qt::CaseSensitive (default), the search is
3108 case sensitive; otherwise the search is case insensitive.
3110 \sa indexOf(), count()
3113 /*! \fn bool QString::contains(const QRegExp &rx) const
3115 \overload contains()
3117 Returns true if the regular expression \a rx matches somewhere in
3118 this string; otherwise returns false.
3121 /*! \fn bool QString::contains(QRegExp &rx) const
3122 \overload contains()
3125 Returns true if the regular expression \a rx matches somewhere in
3126 this string; otherwise returns false.
3128 If there is a match, the \a rx regular expression will contain the
3129 matched captures (see QRegExp::matchedLength, QRegExp::cap).
3132 #ifndef QT_NO_REGEXP
3136 Returns the index position of the first match of the regular
3137 expression \a rx in the string, searching forward from index
3138 position \a from. Returns -1 if \a rx didn't match anywhere.
3142 \snippet qstring/main.cpp 25
3144 int QString::indexOf(const QRegExp& rx, int from) const
3147 return rx2.indexIn(*this, from);
3154 Returns the index position of the first match of the regular
3155 expression \a rx in the string, searching forward from index
3156 position \a from. Returns -1 if \a rx didn't match anywhere.
3158 If there is a match, the \a rx regular expression will contain the
3159 matched captures (see QRegExp::matchedLength, QRegExp::cap).
3163 \snippet qstring/main.cpp 25
3165 int QString::indexOf(QRegExp& rx, int from) const
3167 return rx.indexIn(*this, from);
3171 \overload lastIndexOf()
3173 Returns the index position of the last match of the regular
3174 expression \a rx in the string, searching backward from index
3175 position \a from. Returns -1 if \a rx didn't match anywhere.
3179 \snippet qstring/main.cpp 30
3181 int QString::lastIndexOf(const QRegExp& rx, int from) const
3184 return rx2.lastIndexIn(*this, from);
3188 \overload lastIndexOf()
3191 Returns the index position of the last match of the regular
3192 expression \a rx in the string, searching backward from index
3193 position \a from. Returns -1 if \a rx didn't match anywhere.
3195 If there is a match, the \a rx regular expression will contain the
3196 matched captures (see QRegExp::matchedLength, QRegExp::cap).
3200 \snippet qstring/main.cpp 30
3202 int QString::lastIndexOf(QRegExp& rx, int from) const
3204 return rx.lastIndexIn(*this, from);
3210 Returns the number of times the regular expression \a rx matches
3213 This function counts overlapping matches, so in the example
3214 below, there are four instances of "ana" or "ama":
3216 \snippet qstring/main.cpp 18
3219 int QString::count(const QRegExp& rx) const
3225 while (index < len - 1) { // count overlapping matches
3226 index = rx2.indexIn(*this, index + 1);
3233 #endif // QT_NO_REGEXP
3235 #ifndef QT_NO_REGEXP
3236 #ifndef QT_BOOTSTRAPPED
3241 Returns the index position of the first match of the regular
3242 expression \a re in the string, searching forward from index
3243 position \a from. Returns -1 if \a re didn't match anywhere.
3247 \snippet qstring/main.cpp 93
3249 int QString::indexOf(const QRegularExpression& re, int from) const
3251 if (!re.isValid()) {
3252 qWarning("QString::indexOf: invalid QRegularExpresssion object");
3256 QRegularExpressionMatch match = re.match(*this, from);
3257 if (match.hasMatch())
3258 return match.capturedStart();
3264 \overload lastIndexOf()
3267 Returns the index position of the last match of the regular
3268 expression \a re in the string, which starts before the index
3269 position \a from. Returns -1 if \a re didn't match anywhere.
3273 \snippet qstring/main.cpp 94
3275 int QString::lastIndexOf(const QRegularExpression &re, int from) const
3277 if (!re.isValid()) {
3278 qWarning("QString::lastIndexOf: invalid QRegularExpresssion object");
3282 int endpos = (from < 0) ? (size() + from + 1) : (from + 1);
3284 QRegularExpressionMatchIterator iterator = re.globalMatch(*this);
3286 while (iterator.hasNext()) {
3287 QRegularExpressionMatch match = iterator.next();
3288 int start = match.capturedStart();
3298 /*! \overload contains()
3301 Returns true if the regular expression \a re matches somewhere in
3302 this string; otherwise returns false.
3304 bool QString::contains(const QRegularExpression &re) const
3306 if (!re.isValid()) {
3307 qWarning("QString::contains: invalid QRegularExpresssion object");
3310 QRegularExpressionMatch match = re.match(*this);
3311 return match.hasMatch();
3318 Returns the number of times the regular expression \a re matches
3321 This function counts overlapping matches, so in the example
3322 below, there are four instances of "ana" or "ama":
3324 \snippet qstring/main.cpp 95
3326 int QString::count(const QRegularExpression &re) const
3328 if (!re.isValid()) {
3329 qWarning("QString::count: invalid QRegularExpresssion object");
3335 while (index < len - 1) {
3336 QRegularExpressionMatch match = re.match(*this, index + 1);
3337 if (!match.hasMatch())
3339 index = match.capturedStart();
3344 #endif // QT_BOOTSTRAPPED
3345 #endif // QT_NO_REGEXP
3347 /*! \fn int QString::count() const
3356 \enum QString::SectionFlag
3358 This enum specifies flags that can be used to affect various
3359 aspects of the section() function's behavior with respect to
3360 separators and empty fields.
3362 \value SectionDefault Empty fields are counted, leading and
3363 trailing separators are not included, and the separator is
3364 compared case sensitively.
3366 \value SectionSkipEmpty Treat empty fields as if they don't exist,
3367 i.e. they are not considered as far as \e start and \e end are
3370 \value SectionIncludeLeadingSep Include the leading separator (if
3371 any) in the result string.
3373 \value SectionIncludeTrailingSep Include the trailing separator
3374 (if any) in the result string.
3376 \value SectionCaseInsensitiveSeps Compare the separator
3383 \fn QString QString::section(QChar sep, int start, int end = -1, SectionFlags flags) const
3385 This function returns a section of the string.
3387 This string is treated as a sequence of fields separated by the
3388 character, \a sep. The returned string consists of the fields from
3389 position \a start to position \a end inclusive. If \a end is not
3390 specified, all fields from position \a start to the end of the
3391 string are included. Fields are numbered 0, 1, 2, etc., counting
3392 from the left, and -1, -2, etc., counting from right to left.
3394 The \a flags argument can be used to affect some aspects of the
3395 function's behavior, e.g. whether to be case sensitive, whether
3396 to skip empty fields and how to deal with leading and trailing
3397 separators; see \l{SectionFlags}.
3399 \snippet qstring/main.cpp 52
3401 If \a start or \a end is negative, we count fields from the right
3402 of the string, the right-most field being -1, the one from
3403 right-most field being -2, and so on.
3405 \snippet qstring/main.cpp 53
3413 \snippet qstring/main.cpp 51
3414 \snippet qstring/main.cpp 54
3419 QString QString::section(const QString &sep, int start, int end, SectionFlags flags) const
3421 QStringList sections = split(sep, KeepEmptyParts,
3422 (flags & SectionCaseInsensitiveSeps) ? Qt::CaseInsensitive : Qt::CaseSensitive);
3423 if (sections.isEmpty())
3425 if (!(flags & SectionSkipEmpty)) {
3427 start += sections.count();
3429 end += sections.count();
3432 for (int k=0; k<sections.size(); ++k) {
3433 if (sections.at(k).isEmpty())
3437 start += sections.count() - skip;
3439 end += sections.count() - skip;
3443 int first_i = start, last_i = end;
3444 for (int i = 0; x <= end && i < sections.size(); ++i) {
3445 QString section = sections.at(i);
3446 const bool empty = section.isEmpty();
3456 if (!empty || !(flags & SectionSkipEmpty))
3459 if((flags & SectionIncludeLeadingSep) && first_i)
3461 if((flags & SectionIncludeTrailingSep) && last_i < sections.size()-1)
3466 #ifndef QT_NO_REGEXP
3467 class qt_section_chunk {
3469 qt_section_chunk(int l, QString s) { length = l; string = s; }
3474 static QString extractSections(const QList<qt_section_chunk> §ions,
3477 QString::SectionFlags flags)
3480 start += sections.count();
3482 end += sections.count();
3486 int first_i = start, last_i = end;
3487 for (int i = 0; x <= end && i < sections.size(); ++i) {
3488 const qt_section_chunk §ion = sections.at(i);
3489 const bool empty = (section.length == section.string.length());
3496 ret += section.string;
3498 ret += section.string.mid(section.length);
3500 if (!empty || !(flags & QString::SectionSkipEmpty))
3504 if ((flags & QString::SectionIncludeLeadingSep) && first_i < sections.size()) {
3505 const qt_section_chunk §ion = sections.at(first_i);
3506 ret.prepend(section.string.left(section.length));
3509 if ((flags & QString::SectionIncludeTrailingSep) && last_i+1 <= sections.size()-1) {
3510 const qt_section_chunk §ion = sections.at(last_i+1);
3511 ret += section.string.left(section.length);
3520 This string is treated as a sequence of fields separated by the
3521 regular expression, \a reg.
3523 \snippet qstring/main.cpp 55
3525 \warning Using this QRegExp version is much more expensive than
3526 the overloaded string and character versions.
3528 \sa split(), simplified()
3530 QString QString::section(const QRegExp ®, int start, int end, SectionFlags flags) const
3532 const QChar *uc = unicode();
3537 sep.setCaseSensitivity((flags & SectionCaseInsensitiveSeps) ? Qt::CaseInsensitive
3538 : Qt::CaseSensitive);
3540 QList<qt_section_chunk> sections;
3541 int n = length(), m = 0, last_m = 0, last_len = 0;
3542 while ((m = sep.indexIn(*this, m)) != -1) {
3543 sections.append(qt_section_chunk(last_len, QString(uc + last_m, m - last_m)));
3545 last_len = sep.matchedLength();
3546 m += qMax(sep.matchedLength(), 1);
3548 sections.append(qt_section_chunk(last_len, QString(uc + last_m, n - last_m)));
3550 return extractSections(sections, start, end, flags);
3554 #ifndef QT_NO_REGEXP
3555 #ifndef QT_BOOTSTRAPPED
3560 This string is treated as a sequence of fields separated by the
3561 regular expression, \a re.
3563 \snippet qstring/main.cpp 89
3565 \warning Using this QRegularExpression version is much more expensive than
3566 the overloaded string and character versions.
3568 \sa split(), simplified()
3570 QString QString::section(const QRegularExpression &re, int start, int end, SectionFlags flags) const
3572 if (!re.isValid()) {
3573 qWarning("QString::section: invalid QRegularExpression object");
3577 const QChar *uc = unicode();
3581 QRegularExpression sep(re);
3582 if (flags & SectionCaseInsensitiveSeps)
3583 sep.setPatternOptions(sep.patternOptions() | QRegularExpression::CaseInsensitiveOption);
3585 QList<qt_section_chunk> sections;
3586 int n = length(), m = 0, last_m = 0, last_len = 0;
3587 QRegularExpressionMatchIterator iterator = sep.globalMatch(*this);
3588 while (iterator.hasNext()) {
3589 QRegularExpressionMatch match = iterator.next();
3590 m = match.capturedStart();
3591 sections.append(qt_section_chunk(last_len, QString(uc + last_m, m - last_m)));
3593 last_len = match.capturedLength();
3595 sections.append(qt_section_chunk(last_len, QString(uc + last_m, n - last_m)));
3597 return extractSections(sections, start, end, flags);
3599 #endif // QT_BOOTSTRAPPED
3600 #endif // QT_NO_REGEXP
3603 Returns a substring that contains the \a n leftmost characters
3606 The entire string is returned if \a n is greater than size() or
3609 \snippet qstring/main.cpp 31
3611 \sa right(), mid(), startsWith()
3613 QString QString::left(int n) const
3615 if (n >= d->size || n < 0)
3617 return QString((const QChar*) d->data(), n);
3621 Returns a substring that contains the \a n rightmost characters
3624 The entire string is returned if \a n is greater than size() or
3627 \snippet qstring/main.cpp 48
3629 \sa left(), mid(), endsWith()
3631 QString QString::right(int n) const
3633 if (n >= d->size || n < 0)
3635 return QString((const QChar*) d->data() + d->size - n, n);
3639 Returns a string that contains \a n characters of this string,
3640 starting at the specified \a position index.
3642 Returns a null string if the \a position index exceeds the
3643 length of the string. If there are less than \a n characters
3644 available in the string starting at the given \a position, or if
3645 \a n is -1 (default), the function returns all characters that
3646 are available from the specified \a position.
3650 \snippet qstring/main.cpp 34
3655 QString QString::mid(int position, int n) const
3657 if (position > d->size)
3660 if (n < 0 || n + position >= d->size)
3662 if (n + position <= 0)
3667 } else if (n < 0 || n > d->size - position)
3668 n = d->size - position;
3669 if (position == 0 && n == d->size)
3671 return QString((const QChar*) d->data() + position, n);
3675 Returns true if the string starts with \a s; otherwise returns
3678 If \a cs is Qt::CaseSensitive (default), the search is
3679 case sensitive; otherwise the search is case insensitive.
3681 \snippet qstring/main.cpp 65
3685 bool QString::startsWith(const QString& s, Qt::CaseSensitivity cs) const
3687 return qt_starts_with(isNull() ? 0 : unicode(), size(),
3688 s.isNull() ? 0 : s.unicode(), s.size(), cs);
3692 \overload startsWith()
3694 bool QString::startsWith(QLatin1String s, Qt::CaseSensitivity cs) const
3696 return qt_starts_with(isNull() ? 0 : unicode(), size(), s, cs);
3700 \overload startsWith()
3702 Returns true if the string starts with \a c; otherwise returns
3705 bool QString::startsWith(QChar c, Qt::CaseSensitivity cs) const
3708 && (cs == Qt::CaseSensitive
3710 : foldCase(d->data()[0]) == foldCase(c.unicode()));
3716 Returns true if the string starts with the string reference \a s;
3717 otherwise returns false.
3719 If \a cs is Qt::CaseSensitive (default), the search is case
3720 sensitive; otherwise the search is case insensitive.
3724 bool QString::startsWith(const QStringRef &s, Qt::CaseSensitivity cs) const
3726 return qt_starts_with(isNull() ? 0 : unicode(), size(),
3727 s.isNull() ? 0 : s.unicode(), s.size(), cs);
3731 Returns true if the string ends with \a s; otherwise returns
3734 If \a cs is Qt::CaseSensitive (default), the search is case
3735 sensitive; otherwise the search is case insensitive.
3737 \snippet qstring/main.cpp 20
3741 bool QString::endsWith(const QString& s, Qt::CaseSensitivity cs) const
3743 return qt_ends_with(isNull() ? 0 : unicode(), size(),
3744 s.isNull() ? 0 : s.unicode(), s.size(), cs);
3749 \overload endsWith()
3750 Returns true if the string ends with the string reference \a s;
3751 otherwise returns false.
3753 If \a cs is Qt::CaseSensitive (default), the search is case
3754 sensitive; otherwise the search is case insensitive.
3758 bool QString::endsWith(const QStringRef &s, Qt::CaseSensitivity cs) const
3760 return qt_ends_with(isNull() ? 0 : unicode(), size(),
3761 s.isNull() ? 0 : s.unicode(), s.size(), cs);
3766 \overload endsWith()
3768 bool QString::endsWith(QLatin1String s, Qt::CaseSensitivity cs) const
3770 return qt_ends_with(isNull() ? 0 : unicode(), size(), s, cs);
3774 Returns true if the string ends with \a c; otherwise returns
3777 \overload endsWith()
3779 bool QString::endsWith(QChar c, Qt::CaseSensitivity cs) const
3782 && (cs == Qt::CaseSensitive
3783 ? d->data()[d->size - 1] == c
3784 : foldCase(d->data()[d->size - 1]) == foldCase(c.unicode()));
3788 #if defined(__SSE2__)
3789 static inline __m128i mergeQuestionMarks(__m128i chunk)
3791 const __m128i questionMark = _mm_set1_epi16('?');
3794 // compare the unsigned shorts for the range 0x0100-0xFFFF
3795 // note on the use of _mm_cmpestrm:
3796 // The MSDN documentation online (http://technet.microsoft.com/en-us/library/bb514080.aspx)
3797 // says for range search the following:
3798 // For each character c in a, determine whether b0 <= c <= b1 or b2 <= c <= b3
3800 // However, all examples on the Internet, including from Intel
3801 // (see http://software.intel.com/en-us/articles/xml-parsing-accelerator-with-intel-streaming-simd-extensions-4-intel-sse4/)
3802 // put the range to be searched first
3804 // Disassembly and instruction-level debugging with GCC and ICC show
3805 // that they are doing the right thing. Inverting the arguments in the
3806 // instruction does cause a bunch of test failures.
3808 const int mode = _SIDD_UWORD_OPS | _SIDD_CMP_RANGES | _SIDD_UNIT_MASK;
3809 const __m128i rangeMatch = _mm_cvtsi32_si128(0xffff0100);
3810 const __m128i offLimitMask = _mm_cmpestrm(rangeMatch, 2, chunk, 8, mode);
3812 // replace the non-Latin 1 characters in the chunk with question marks
3813 chunk = _mm_blendv_epi8(chunk, questionMark, offLimitMask);
3815 // SSE has no compare instruction for unsigned comparison.
3816 // The variables must be shiffted + 0x8000 to be compared
3817 const __m128i signedBitOffset = _mm_set1_epi16(0x8000);
3818 const __m128i thresholdMask = _mm_set1_epi16(0xff + 0x8000);
3820 const __m128i signedChunk = _mm_add_epi16(chunk, signedBitOffset);
3821 const __m128i offLimitMask = _mm_cmpgt_epi16(signedChunk, thresholdMask);
3824 // replace the non-Latin 1 characters in the chunk with question marks
3825 chunk = _mm_blendv_epi8(chunk, questionMark, offLimitMask);
3827 // offLimitQuestionMark contains '?' for each 16 bits that was off-limit
3828 // the 16 bits that were correct contains zeros
3829 const __m128i offLimitQuestionMark = _mm_and_si128(offLimitMask, questionMark);
3831 // correctBytes contains the bytes that were in limit
3832 // the 16 bits that were off limits contains zeros
3833 const __m128i correctBytes = _mm_andnot_si128(offLimitMask, chunk);
3835 // merge offLimitQuestionMark and correctBytes to have the result
3836 chunk = _mm_or_si128(correctBytes, offLimitQuestionMark);
3843 static QByteArray toLatin1_helper(const QChar *data, int length)
3848 const ushort *src = reinterpret_cast<const ushort *>(data);
3849 uchar *dst = (uchar*) ba.data();
3850 #if defined(__SSE2__)
3852 const int chunkCount = length >> 4; // divided by 16
3854 for (int i = 0; i < chunkCount; ++i) {
3855 __m128i chunk1 = _mm_loadu_si128((__m128i*)src); // load
3856 chunk1 = mergeQuestionMarks(chunk1);
3859 __m128i chunk2 = _mm_loadu_si128((__m128i*)src); // load
3860 chunk2 = mergeQuestionMarks(chunk2);
3863 // pack the two vector to 16 x 8bits elements
3864 const __m128i result = _mm_packus_epi16(chunk1, chunk2);
3866 _mm_storeu_si128((__m128i*)dst, result); // store
3869 length = length % 16;
3871 #elif defined(__ARM_NEON__)
3872 // Refer to the documentation of the SSE2 implementation
3873 // this use eactly the same method as for SSE except:
3874 // 1) neon has unsigned comparison
3875 // 2) packing is done to 64 bits (8 x 8bits component).
3877 const int chunkCount = length >> 3; // divided by 8
3878 const uint16x8_t questionMark = vdupq_n_u16('?'); // set
3879 const uint16x8_t thresholdMask = vdupq_n_u16(0xff); // set
3880 for (int i = 0; i < chunkCount; ++i) {
3881 uint16x8_t chunk = vld1q_u16((uint16_t *)src); // load
3884 const uint16x8_t offLimitMask = vcgtq_u16(chunk, thresholdMask); // chunk > thresholdMask
3885 const uint16x8_t offLimitQuestionMark = vandq_u16(offLimitMask, questionMark); // offLimitMask & questionMark
3886 const uint16x8_t correctBytes = vbicq_u16(chunk, offLimitMask); // !offLimitMask & chunk
3887 chunk = vorrq_u16(correctBytes, offLimitQuestionMark); // correctBytes | offLimitQuestionMark
3888 const uint8x8_t result = vmovn_u16(chunk); // narrowing move->packing
3889 vst1_u8(dst, result); // store
3892 length = length % 8;
3896 *dst++ = (*src>0xff) ? '?' : (uchar) *src;
3904 Returns a Latin-1 representation of the string as a QByteArray.
3906 The returned byte array is undefined if the string contains non-Latin1
3907 characters. Those characters may be suppressed or replaced with a
3910 \sa fromLatin1(), toUtf8(), toLocal8Bit(), QTextCodec
3912 QByteArray QString::toLatin1() const
3914 return toLatin1_helper(unicode(), length());
3918 \fn QByteArray QString::toAscii() const
3920 Returns an 8-bit representation of the string as a QByteArray.
3922 This function does the same as toLatin1().
3924 Note that, despite the name, this function does not necessarily return an US-ASCII
3925 (ANSI X3.4-1986) string and its result may not be US-ASCII compatible.
3927 \sa fromAscii(), toLatin1(), toUtf8(), toLocal8Bit(), QTextCodec
3930 #if !defined(Q_OS_MAC) && defined(Q_OS_UNIX)
3931 static QByteArray toLocal8Bit_helper(const QChar *data, int length)
3933 #ifndef QT_NO_TEXTCODEC
3934 if (QTextCodec::codecForLocale())
3935 return QTextCodec::codecForLocale()->fromUnicode(data, length);
3936 #endif // QT_NO_TEXTCODEC
3937 return toLatin1_helper(data, length);
3942 Returns the local 8-bit representation of the string as a
3943 QByteArray. The returned byte array is undefined if the string
3944 contains characters not supported by the local 8-bit encoding.
3946 QTextCodec::codecForLocale() is used to perform the conversion from
3947 Unicode. If the locale encoding could not be determined, this function
3948 does the same as toLatin1().
3950 If this string contains any characters that cannot be encoded in the
3951 locale, the returned byte array is undefined. Those characters may be
3952 suppressed or replaced by another.
3954 \sa fromLocal8Bit(), toLatin1(), toUtf8(), QTextCodec
3956 QByteArray QString::toLocal8Bit() const
3958 #ifndef QT_NO_TEXTCODEC
3959 if (QTextCodec::codecForLocale())
3960 return QTextCodec::codecForLocale()->fromUnicode(*this);
3961 #endif // QT_NO_TEXTCODEC
3966 Returns a UTF-8 representation of the string as a QByteArray.
3968 UTF-8 is a Unicode codec and can represent all characters in a Unicode
3969 string like QString.
3971 However, in the Unicode range, there are certain codepoints that are not
3972 considered characters. The Unicode standard reserves the last two
3973 codepoints in each Unicode Plane (U+FFFE, U+FFFF, U+1FFFE, U+1FFFF,
3974 U+2FFFE, etc.), as well as 32 codepoints in the range U+FDD0..U+FDEF,
3975 inclusive, as non-characters. If any of those appear in the string, they
3976 may be discarded and will not appear in the UTF-8 representation, or they
3977 may be replaced by one or more replacement characters.
3979 \sa fromUtf8(), toLatin1(), toLocal8Bit(), QTextCodec
3981 QByteArray QString::toUtf8() const
3984 return QByteArray();
3986 return QUtf8::convertFromUnicode(constData(), length(), 0);
3992 Returns a UCS-4/UTF-32 representation of the string as a QVector<uint>.
3994 UCS-4 is a Unicode codec and is lossless. All characters from this string
3995 can be encoded in UCS-4. The vector is not null terminated.
3997 \sa fromUtf8(), toUtf8(), toLatin1(), toLocal8Bit(), QTextCodec, fromUcs4(), toWCharArray()
3999 QVector<uint> QString::toUcs4() const
4001 QVector<uint> v(length());
4003 int len = toUcs4_helper(d->data(), length(), a);
4008 QString::Data *QString::fromLatin1_helper(const char *str, int size)
4012 d = Data::sharedNull();
4013 } else if (size == 0 || (!*str && size < 0)) {
4014 d = Data::allocate(0);
4017 size = qstrlen(str);
4018 d = Data::allocate(size + 1);
4021 d->data()[size] = '\0';
4022 ushort *dst = d->data();
4024 * Unpacking with SSE has been shown to improve performance on recent CPUs
4025 * The same method gives no improvement with NEON.
4027 #if defined(__SSE2__)
4029 int chunkCount = size >> 4; // divided by 16
4030 const __m128i nullMask = _mm_set1_epi32(0);
4031 for (int i = 0; i < chunkCount; ++i) {
4032 const __m128i chunk = _mm_loadu_si128((__m128i*)str); // load
4035 // unpack the first 8 bytes, padding with zeros
4036 const __m128i firstHalf = _mm_unpacklo_epi8(chunk, nullMask);
4037 _mm_storeu_si128((__m128i*)dst, firstHalf); // store
4040 // unpack the last 8 bytes, padding with zeros
4041 const __m128i secondHalf = _mm_unpackhi_epi8 (chunk, nullMask);
4042 _mm_storeu_si128((__m128i*)dst, secondHalf); // store
4049 *dst++ = (uchar)*str++;
4054 QString::Data *QString::fromAscii_helper(const char *str, int size)
4056 QString s = fromUtf8(str, size);
4061 /*! \fn QString QString::fromLatin1(const char *str, int size)
4062 Returns a QString initialized with the first \a size characters
4063 of the Latin-1 string \a str.
4065 If \a size is -1 (default), it is taken to be strlen(\a
4068 \sa toLatin1(), fromUtf8(), fromLocal8Bit()
4072 /*! \fn QString QString::fromLocal8Bit(const char *str, int size)
4073 Returns a QString initialized with the first \a size characters
4074 of the 8-bit string \a str.
4076 If \a size is -1 (default), it is taken to be strlen(\a
4079 QTextCodec::codecForLocale() is used to perform the conversion.
4081 \sa toLocal8Bit(), fromLatin1(), fromUtf8()
4083 QString QString::fromLocal8Bit_helper(const char *str, int size)
4087 if (size == 0 || (!*str && size < 0)) {
4088 QStringDataPtr empty = { Data::allocate(0) };
4089 return QString(empty);
4091 #if !defined(QT_NO_TEXTCODEC)
4093 size = qstrlen(str);
4094 QTextCodec *codec = QTextCodec::codecForLocale();
4096 return codec->toUnicode(str, size);
4097 #endif // !QT_NO_TEXTCODEC
4098 return fromLatin1(str, size);
4101 /*! \fn QString QString::fromAscii(const char *, int size);
4104 Returns a QString initialized with the first \a size characters
4105 from the string \a str.
4107 If \a size is -1 (default), it is taken to be strlen(\a
4110 This function does the same as fromLatin1().
4112 \sa toAscii(), fromLatin1(), fromUtf8(), fromLocal8Bit()
4115 /*! \fn QString QString::fromUtf8(const char *str, int size)
4116 Returns a QString initialized with the first \a size bytes
4117 of the UTF-8 string \a str.
4119 If \a size is -1 (default), it is taken to be strlen(\a
4122 UTF-8 is a Unicode codec and can represent all characters in a Unicode
4123 string like QString. However, invalid sequences are possible with UTF-8
4124 and, if any such are found, they will be replaced with one or more
4125 "replacement characters", or suppressed. These include non-Unicode
4126 sequences, non-characters, overlong sequences or surrogate codepoints
4129 Non-characters are codepoints that the Unicode standard reserves and must
4130 not be used in text interchange. They are the last two codepoints in each
4131 Unicode Plane (U+FFFE, U+FFFF, U+1FFFE, U+1FFFF, U+2FFFE, etc.), as well
4132 as 32 codepoints in the range U+FDD0..U+FDEF, inclusive.
4134 \sa toUtf8(), fromLatin1(), fromLocal8Bit()
4136 QString QString::fromUtf8_helper(const char *str, int size)
4141 Q_ASSERT(size != -1);
4142 return QUtf8::convertToUnicode(str, size, 0);
4146 Returns a QString initialized with the first \a size characters
4147 of the Unicode string \a unicode (ISO-10646-UTF-16 encoded).
4149 If \a size is -1 (default), \a unicode must be terminated
4152 This function checks for a Byte Order Mark (BOM). If it is missing,
4153 host byte order is assumed.
4155 This function is slow compared to the other Unicode conversions.
4156 Use QString(const QChar *, int) or QString(const QChar *) if possible.
4158 QString makes a deep copy of the Unicode data.
4160 \sa utf16(), setUtf16()
4162 QString QString::fromUtf16(const ushort *unicode, int size)
4168 while (unicode[size] != 0)
4171 return QUtf16::convertToUnicode((const char *)unicode, size*2, 0);
4178 Returns a QString initialized with the first \a size characters
4179 of the Unicode string \a unicode (ISO-10646-UCS-4 encoded).
4181 If \a size is -1 (default), \a unicode must be terminated
4184 \sa toUcs4(), fromUtf16(), utf16(), setUtf16(), fromWCharArray()
4186 QString QString::fromUcs4(const uint *unicode, int size)
4192 while (unicode[size] != 0)
4195 return QUtf32::convertToUnicode((const char *)unicode, size*4, 0);
4199 Resizes the string to \a size characters and copies \a unicode
4202 If \a unicode is 0, nothing is copied, but the string is still
4205 \sa unicode(), setUtf16()
4207 QString& QString::setUnicode(const QChar *unicode, int size)
4210 if (unicode && size)
4211 memcpy(d->data(), unicode, size * sizeof(QChar));
4216 \fn QString &QString::setUtf16(const ushort *unicode, int size)
4218 Resizes the string to \a size characters and copies \a unicode
4221 If \a unicode is 0, nothing is copied, but the string is still
4224 Note that unlike fromUtf16(), this function does not consider BOMs and
4225 possibly differing byte ordering.
4227 \sa utf16(), setUnicode()
4231 Returns a string that has whitespace removed from the start
4232 and the end, and that has each sequence of internal whitespace
4233 replaced with a single space.
4235 Whitespace means any character for which QChar::isSpace() returns
4236 true. This includes the ASCII characters '\\t', '\\n', '\\v',
4237 '\\f', '\\r', and ' '.
4241 \snippet qstring/main.cpp 57
4245 QString QString::simplified() const
4250 const QChar * const start = reinterpret_cast<QChar *>(d->data());
4251 const QChar *from = start;
4252 const QChar *fromEnd = start + d->size;
4257 if (++from == fromEnd) {
4258 // All-whitespace string
4259 QStringDataPtr empty = { Data::allocate(0) };
4260 return QString(empty);
4263 // This loop needs no underflow check, as we already determined that
4264 // the string contains non-whitespace. If the string has exactly one
4265 // non-whitespace, it will be checked twice - we can live with that.
4266 while (fromEnd[-1].isSpace())
4268 // The rest of the function depends on the fact that we already know
4269 // that the last character in the source is no whitespace.
4270 const QChar *copyFrom = from;
4273 if (++from == fromEnd) {
4274 // Only leading and/or trailing whitespace, if any at all
4275 return mid(copyFrom - start, from - copyFrom);
4280 if (ch != QLatin1Char(' ')) {
4281 copyCount = from - copyFrom;
4286 copyCount = from - copyFrom - 1;
4290 // 'from' now points at the non-trailing whitespace which made the
4291 // string not simplified in the first place. 'copyCount' is the number
4292 // of already simplified characters - at least one, obviously -
4293 // without a trailing space.
4294 QString result((fromEnd - from) + copyCount, Qt::Uninitialized);
4295 QChar *to = reinterpret_cast<QChar *>(result.d->data());
4296 ::memcpy(to, copyFrom, copyCount * 2);
4301 *to++ = QLatin1Char(' ');
4304 } while (ch.isSpace());
4305 if (from == fromEnd)
4310 if (from == fromEnd)
4312 } while (!ch.isSpace());
4317 result.truncate(to - reinterpret_cast<QChar *>(result.d->data()));
4322 Returns a string that has whitespace removed from the start and
4325 Whitespace means any character for which QChar::isSpace() returns
4326 true. This includes the ASCII characters '\\t', '\\n', '\\v',
4327 '\\f', '\\r', and ' '.
4331 \snippet qstring/main.cpp 82
4333 Unlike simplified(), trimmed() leaves internal whitespace alone.
4337 QString QString::trimmed() const
4341 const QChar *s = (const QChar*)d->data();
4342 if (!s->isSpace() && !s[d->size-1].isSpace())
4345 int end = d->size - 1;
4346 while (start<=end && s[start].isSpace()) // skip white space from start
4348 if (start <= end) { // only white space
4349 while (end && s[end].isSpace()) // skip white space from end
4352 int l = end - start + 1;
4354 QStringDataPtr empty = { Data::allocate(0) };
4355 return QString(empty);
4357 return QString(s + start, l);
4360 /*! \fn const QChar QString::at(int position) const
4362 Returns the character at the given index \a position in the
4365 The \a position must be a valid index position in the string
4366 (i.e., 0 <= \a position < size()).
4372 \fn QCharRef QString::operator[](int position)
4374 Returns the character at the specified \a position in the string as a
4375 modifiable reference.
4379 \snippet qstring/main.cpp 85
4381 The return value is of type QCharRef, a helper class for QString.
4382 When you get an object of type QCharRef, you can use it as if it
4383 were a QChar &. If you assign to it, the assignment will apply to
4384 the character in the QString from which you got the reference.
4390 \fn const QChar QString::operator[](int position) const
4392 \overload operator[]()
4395 /*! \fn QCharRef QString::operator[](uint position)
4397 \overload operator[]()
4399 Returns the character at the specified \a position in the string as a
4400 modifiable reference. Equivalent to \c at(position).
4403 /*! \fn const QChar QString::operator[](uint position) const
4405 \overload operator[]()
4409 \fn void QString::truncate(int position)
4411 Truncates the string at the given \a position index.
4413 If the specified \a position index is beyond the end of the
4414 string, nothing happens.
4418 \snippet qstring/main.cpp 83
4420 If \a position is negative, it is equivalent to passing zero.
4422 \sa chop(), resize(), left()
4425 void QString::truncate(int pos)
4433 Removes \a n characters from the end of the string.
4435 If \a n is greater than size(), the result is an empty string.
4438 \snippet qstring/main.cpp 15
4440 If you want to remove characters from the \e beginning of the
4441 string, use remove() instead.
4443 \sa truncate(), resize(), remove()
4445 void QString::chop(int n)
4448 resize(d->size - n);
4452 Sets every character in the string to character \a ch. If \a size
4453 is different from -1 (default), the string is resized to \a
4458 \snippet qstring/main.cpp 21
4463 QString& QString::fill(QChar ch, int size)
4465 resize(size < 0 ? d->size : size);
4467 QChar *i = (QChar*)d->data() + d->size;
4468 QChar *b = (QChar*)d->data();
4476 \fn int QString::length() const
4478 Returns the number of characters in this string. Equivalent to
4485 \fn int QString::size() const
4487 Returns the number of characters in this string.
4489 The last character in the string is at position size() - 1. In
4490 addition, QString ensures that the character at position size()
4491 is always '\\0', so that you can use the return value of data()
4492 and constData() as arguments to functions that expect
4493 '\\0'-terminated strings.
4497 \snippet qstring/main.cpp 58
4499 \sa isEmpty(), resize()
4502 /*! \fn bool QString::isNull() const
4504 Returns true if this string is null; otherwise returns false.
4508 \snippet qstring/main.cpp 28
4510 Qt makes a distinction between null strings and empty strings for
4511 historical reasons. For most applications, what matters is
4512 whether or not a string contains any data, and this can be
4513 determined using the isEmpty() function.
4518 /*! \fn bool QString::isEmpty() const
4520 Returns true if the string has no characters; otherwise returns
4525 \snippet qstring/main.cpp 27
4530 /*! \fn QString &QString::operator+=(const QString &other)
4532 Appends the string \a other onto the end of this string and
4533 returns a reference to this string.
4537 \snippet qstring/main.cpp 84
4539 This operation is typically very fast (\l{constant time}),
4540 because QString preallocates extra space at the end of the string
4541 data so it can grow without reallocating the entire string each
4544 \sa append(), prepend()
4547 /*! \fn QString &QString::operator+=(QLatin1String str)
4549 \overload operator+=()
4551 Appends the Latin-1 string \a str to this string.
4554 /*! \fn QString &QString::operator+=(const QByteArray &ba)
4556 \overload operator+=()
4558 Appends the byte array \a ba to this string. The byte array is converted
4559 to Unicode using the fromUtf8() function. If any NUL characters ('\\0')
4560 are embedded in the \a ba byte array, they will be included in the
4563 You can disable this function by defining \c
4564 QT_NO_CAST_FROM_ASCII when you compile your applications. This
4565 can be useful if you want to ensure that all user-visible strings
4566 go through QObject::tr(), for example.
4569 /*! \fn QString &QString::operator+=(const char *str)
4571 \overload operator+=()
4573 Appends the string \a str to this string. The const char pointer
4574 is converted to Unicode using the fromUtf8() function.
4576 You can disable this function by defining \c QT_NO_CAST_FROM_ASCII
4577 when you compile your applications. This can be useful if you want
4578 to ensure that all user-visible strings go through QObject::tr(),
4582 /*! \fn QString &QString::operator+=(const QStringRef &str)
4584 \overload operator+=()
4586 Appends the string section referenced by \a str to this string.
4589 /*! \fn QString &QString::operator+=(char ch)
4591 \overload operator+=()
4593 Appends the character \a ch to this string. Note that the character is
4594 converted to Unicode using the fromLatin1() function, unlike other 8-bit
4595 functions that operate on UTF-8 data.
4597 You can disable this function by defining \c QT_NO_CAST_FROM_ASCII
4598 when you compile your applications. This can be useful if you want
4599 to ensure that all user-visible strings go through QObject::tr(),
4603 /*! \fn QString &QString::operator+=(QChar ch)
4605 \overload operator+=()
4607 Appends the character \a ch to the string.
4610 /*! \fn QString &QString::operator+=(QChar::SpecialCharacter c)
4612 \overload operator+=()
4618 \fn bool operator==(const char *s1, const QString &s2)
4620 \overload operator==()
4623 Returns true if \a s1 is equal to \a s2; otherwise returns false.
4624 Note that no string is equal to \a s1 being 0.
4626 Equivalent to \c {s1 != 0 && compare(s1, s2) == 0}.
4628 \sa QString::compare()
4632 \fn bool operator!=(const char *s1, const QString &s2)
4635 Returns true if \a s1 is not equal to \a s2; otherwise returns
4638 For \a s1 != 0, this is equivalent to \c {compare(} \a s1, \a s2
4639 \c {) != 0}. Note that no string is equal to \a s1 being 0.
4641 \sa QString::compare()
4645 \fn bool operator<(const char *s1, const QString &s2)
4648 Returns true if \a s1 is lexically less than \a s2; otherwise
4649 returns false. For \a s1 != 0, this is equivalent to \c
4650 {compare(s1, s2) < 0}.
4652 The comparison is based exclusively on the numeric Unicode values
4653 of the characters and is very fast, but is not what a human would
4654 expect. Consider sorting user-interface strings using the
4655 QString::localeAwareCompare() function.
4657 \sa QString::compare()
4661 \fn bool operator<=(const char *s1, const QString &s2)
4664 Returns true if \a s1 is lexically less than or equal to \a s2;
4665 otherwise returns false. For \a s1 != 0, this is equivalent to \c
4666 {compare(s1, s2) <= 0}.
4668 The comparison is based exclusively on the numeric Unicode values
4669 of the characters and is very fast, but is not what a human would
4670 expect. Consider sorting user-interface strings with
4671 QString::localeAwareCompare().
4673 \sa QString::compare()
4677 \fn bool operator>(const char *s1, const QString &s2)
4680 Returns true if \a s1 is lexically greater than \a s2; otherwise
4681 returns false. Equivalent to \c {compare(s1, s2) > 0}.
4683 The comparison is based exclusively on the numeric Unicode values
4684 of the characters and is very fast, but is not what a human would
4685 expect. Consider sorting user-interface strings using the
4686 QString::localeAwareCompare() function.
4688 \sa QString::compare()
4692 \fn bool operator>=(const char *s1, const QString &s2)
4695 Returns true if \a s1 is lexically greater than or equal to \a s2;
4696 otherwise returns false. For \a s1 != 0, this is equivalent to \c
4697 {compare(s1, s2) >= 0}.
4699 The comparison is based exclusively on the numeric Unicode values
4700 of the characters and is very fast, but is not what a human would
4701 expect. Consider sorting user-interface strings using the
4702 QString::localeAwareCompare() function.
4706 \fn const QString operator+(const QString &s1, const QString &s2)
4709 Returns a string which is the result of concatenating \a s1 and \a
4714 \fn const QString operator+(const QString &s1, const char *s2)
4717 Returns a string which is the result of concatenating \a s1 and \a
4718 s2 (\a s2 is converted to Unicode using the QString::fromUtf8()
4721 \sa QString::fromUtf8()
4725 \fn const QString operator+(const char *s1, const QString &s2)
4728 Returns a string which is the result of concatenating \a s1 and \a
4729 s2 (\a s1 is converted to Unicode using the QString::fromUtf8()
4732 \sa QString::fromUtf8()
4736 \fn const QString operator+(const QString &s, char ch)
4739 Returns a string which is the result of concatenating the string
4740 \a s and the character \a ch.
4744 \fn const QString operator+(char ch, const QString &s)
4747 Returns a string which is the result of concatenating the
4748 character \a ch and the string \a s.
4752 \fn int QString::compare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs)
4755 Compares \a s1 with \a s2 and returns an integer less than, equal
4756 to, or greater than zero if \a s1 is less than, equal to, or
4759 If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
4760 otherwise the comparison is case insensitive.
4762 Case sensitive comparison is based exclusively on the numeric
4763 Unicode values of the characters and is very fast, but is not what
4764 a human would expect. Consider sorting user-visible strings with
4765 localeAwareCompare().
4767 \snippet qstring/main.cpp 16
4769 \sa operator==(), operator<(), operator>()
4773 \fn int QString::compare(const QString &s1, QLatin1String s2, Qt::CaseSensitivity cs)
4777 Performs a comparison of \a s1 and \a s2, using the case
4778 sensitivity setting \a cs.
4782 \fn int QString::compare(QLatin1String s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
4787 Performs a comparison of \a s1 and \a s2, using the case
4788 sensitivity setting \a cs.
4796 Lexically compares this string with the \a other string and
4797 returns an integer less than, equal to, or greater than zero if
4798 this string is less than, equal to, or greater than the other
4801 Same as compare(*this, \a other, \a cs).
4803 int QString::compare(const QString &other, Qt::CaseSensitivity cs) const
4805 if (cs == Qt::CaseSensitive)
4806 return ucstrcmp(constData(), length(), other.constData(), other.length());
4807 return ucstricmp(d->data(), d->data() + d->size, other.d->data(), other.d->data() + other.d->size);
4814 int QString::compare_helper(const QChar *data1, int length1, const QChar *data2, int length2,
4815 Qt::CaseSensitivity cs)
4817 if (cs == Qt::CaseSensitive)
4818 return ucstrcmp(data1, length1, data2, length2);
4819 register const ushort *s1 = reinterpret_cast<const ushort *>(data1);
4820 register const ushort *s2 = reinterpret_cast<const ushort *>(data2);
4821 return ucstricmp(s1, s1 + length1, s2, s2 + length2);
4828 Same as compare(*this, \a other, \a cs).
4830 int QString::compare(QLatin1String other, Qt::CaseSensitivity cs) const
4832 return compare_helper(unicode(), length(), other, cs);
4836 \fn int QString::compare(const QStringRef &ref, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
4839 Compares the string reference, \a ref, with the string and returns
4840 an integer less than, equal to, or greater than zero if the string
4841 is less than, equal to, or greater than \a ref.
4848 int QString::compare_helper(const QChar *data1, int length1, const char *data2, int length2,
4849 Qt::CaseSensitivity cs)
4852 const QString s2 = QString::fromUtf8(data2, length2 == -1 ? (data2 ? int(strlen(data2)) : -1) : length2);
4853 return compare_helper(data1, length1, s2.constData(), s2.size(), cs);
4857 \fn int QString::compare(const QString &s1, const QStringRef &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
4865 int QString::compare_helper(const QChar *data1, int length1, QLatin1String s2,
4866 Qt::CaseSensitivity cs)
4868 const ushort *uc = reinterpret_cast<const ushort *>(data1);
4869 const ushort *uce = uc + length1;
4870 const uchar *c = (uchar *)s2.latin1();
4875 if (cs == Qt::CaseSensitive) {
4876 const ushort *e = uc + length1;
4877 if (s2.size() < length1)
4880 int diff = *uc - *c;
4887 if (c == (const uchar *)s2.latin1() + s2.size())
4893 return ucstricmp(uc, uce, c, c + s2.size());
4898 \fn int QString::localeAwareCompare(const QString & s1, const QString & s2)
4900 Compares \a s1 with \a s2 and returns an integer less than, equal
4901 to, or greater than zero if \a s1 is less than, equal to, or
4904 The comparison is performed in a locale- and also
4905 platform-dependent manner. Use this function to present sorted
4906 lists of strings to the user.
4908 On Mac OS X since Qt 4.3, this function compares according the
4909 "Order for sorted lists" setting in the International prefereces panel.
4911 \sa compare(), QTextCodec::locale()
4915 \fn int QString::localeAwareCompare(const QStringRef &other) const
4917 \overload localeAwareCompare()
4919 Compares this string with the \a other string and returns an
4920 integer less than, equal to, or greater than zero if this string
4921 is less than, equal to, or greater than the \a other string.
4923 The comparison is performed in a locale- and also
4924 platform-dependent manner. Use this function to present sorted
4925 lists of strings to the user.
4927 Same as \c {localeAwareCompare(*this, other)}.
4931 \fn int QString::localeAwareCompare(const QString &s1, const QStringRef &s2)
4933 \overload localeAwareCompare()
4935 Compares \a s1 with \a s2 and returns an integer less than, equal
4936 to, or greater than zero if \a s1 is less than, equal to, or
4939 The comparison is performed in a locale- and also
4940 platform-dependent manner. Use this function to present sorted
4941 lists of strings to the user.
4945 #if !defined(CSTR_LESS_THAN)
4946 #define CSTR_LESS_THAN 1
4947 #define CSTR_EQUAL 2
4948 #define CSTR_GREATER_THAN 3
4952 \overload localeAwareCompare()
4954 Compares this string with the \a other string and returns an
4955 integer less than, equal to, or greater than zero if this string
4956 is less than, equal to, or greater than the \a other string.
4958 The comparison is performed in a locale- and also
4959 platform-dependent manner. Use this function to present sorted
4960 lists of strings to the user.
4962 Same as \c {localeAwareCompare(*this, other)}.
4964 int QString::localeAwareCompare(const QString &other) const
4966 return localeAwareCompare_helper(constData(), length(), other.constData(), other.length());
4973 int QString::localeAwareCompare_helper(const QChar *data1, int length1,
4974 const QChar *data2, int length2)
4976 // do the right thing for null and empty
4977 if (length1 == 0 || length2 == 0)
4978 return ucstrcmp(data1, length1, data2, length2);
4980 #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
4981 int res = CompareString(GetUserDefaultLCID(), 0, (wchar_t*)data1, length1, (wchar_t*)data2, length2);
4984 case CSTR_LESS_THAN:
4986 case CSTR_GREATER_THAN:
4991 #elif defined (Q_OS_MAC)
4992 // Use CFStringCompare for comparing strings on Mac. This makes Qt order
4993 // strings the same way as native applications do, and also respects
4994 // the "Order for sorted lists" setting in the International preferences
4996 const CFStringRef thisString =
4997 CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault,
4998 reinterpret_cast<const UniChar *>(data1), length1, kCFAllocatorNull);
4999 const CFStringRef otherString =
5000 CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault,
5001 reinterpret_cast<const UniChar *>(data2), length2, kCFAllocatorNull);
5003 const int result = CFStringCompare(thisString, otherString, kCFCompareLocalized);
5004 CFRelease(thisString);
5005 CFRelease(otherString);
5007 #elif defined(QT_USE_ICU)
5009 return collator.compare(data1, length1, data2, length2);
5010 #elif defined(Q_OS_UNIX)
5011 // declared in <string.h>
5012 int delta = strcoll(toLocal8Bit_helper(data1, length1).constData(), toLocal8Bit_helper(data2, length2).constData());
5014 delta = ucstrcmp(data1, length1, data2, length2);
5017 return ucstrcmp(data1, length1, data2, length2);
5023 \fn const QChar *QString::unicode() const
5025 Returns a '\\0'-terminated Unicode representation of the string.
5026 The result remains valid until the string is modified.
5032 \fn const ushort *QString::utf16() const
5034 Returns the QString as a '\\0\'-terminated array of unsigned
5035 shorts. The result remains valid until the string is modified.
5037 The returned string is in host byte order.
5042 const ushort *QString::utf16() const
5044 if (IS_RAW_DATA(d)) {
5045 // ensure '\0'-termination for ::fromRawData strings
5046 const_cast<QString*>(this)->reallocData(uint(d->size) + 1u);
5052 Returns a string of size \a width that contains this string
5053 padded by the \a fill character.
5055 If \a truncate is false and the size() of the string is more than
5056 \a width, then the returned string is a copy of the string.
5058 \snippet qstring/main.cpp 32
5060 If \a truncate is true and the size() of the string is more than
5061 \a width, then any characters in a copy of the string after
5062 position \a width are removed, and the copy is returned.
5064 \snippet qstring/main.cpp 33
5066 \sa rightJustified()
5069 QString QString::leftJustified(int width, QChar fill, bool truncate) const
5073 int padlen = width - len;
5075 result.resize(len+padlen);
5077 memcpy(result.d->data(), d->data(), sizeof(QChar)*len);
5078 QChar *uc = (QChar*)result.d->data() + len;
5083 result = left(width);
5091 Returns a string of size() \a width that contains the \a fill
5092 character followed by the string. For example:
5094 \snippet qstring/main.cpp 49
5096 If \a truncate is false and the size() of the string is more than
5097 \a width, then the returned string is a copy of the string.
5099 If \a truncate is true and the size() of the string is more than
5100 \a width, then the resulting string is truncated at position \a
5103 \snippet qstring/main.cpp 50
5108 QString QString::rightJustified(int width, QChar fill, bool truncate) const
5112 int padlen = width - len;
5114 result.resize(len+padlen);
5115 QChar *uc = (QChar*)result.d->data();
5119 memcpy(uc, d->data(), sizeof(QChar)*len);
5122 result = left(width);
5130 Returns a lowercase copy of the string.
5132 \snippet qstring/main.cpp 75
5134 The case conversion will always happen in the 'C' locale. For locale dependent
5135 case folding use QLocale::toLower()
5137 \sa toUpper(), QLocale::toLower()
5140 QString QString::toLower() const
5142 const ushort *p = d->data();
5146 const ushort *e = p + d->size;
5147 // this avoids out of bounds check in the loop
5148 while (e != p && QChar::isHighSurrogate(*(e - 1)))
5151 const QUnicodeTables::Properties *prop;
5153 if (QChar::isHighSurrogate(*p) && QChar::isLowSurrogate(p[1])) {
5155 prop = qGetProp(QChar::surrogateToUcs4(high, *p));
5157 prop = qGetProp(*p);
5159 if (prop->lowerCaseDiff) {
5160 if (QChar::isLowSurrogate(*p))
5161 --p; // safe; diff is 0 for surrogates
5162 QString s(d->size, Qt::Uninitialized);
5163 memcpy(s.d->data(), d->data(), (p - d->data())*sizeof(ushort));
5164 ushort *pp = s.d->data() + (p - d->data());
5166 if (QChar::isHighSurrogate(*p) && QChar::isLowSurrogate(p[1])) {
5168 prop = qGetProp(QChar::surrogateToUcs4(*pp++, *p));
5170 prop = qGetProp(*p);
5172 if (prop->lowerCaseSpecial) {
5173 const ushort *specialCase = specialCaseMap + prop->lowerCaseDiff;
5174 ushort length = *specialCase++;
5175 int pos = pp - s.d->data();
5176 s.resize(s.d->size + length - 1);
5177 pp = s.d->data() + pos;
5179 *pp++ = *specialCase++;
5181 *pp++ = *p + prop->lowerCaseDiff;
5186 // this restores high surrogate parts eaten above, if any
5187 while (e != d->data() + d->size)
5198 Returns the case folded equivalent of the string. For most Unicode
5199 characters this is the same as toLower().
5201 QString QString::toCaseFolded() const
5203 const ushort *p = d->data();
5207 const ushort *e = p + d->size;
5208 // this avoids out of bounds check in the loop
5209 while (e != p && QChar::isHighSurrogate(*(e - 1)))
5212 const QUnicodeTables::Properties *prop;
5214 if (QChar::isHighSurrogate(*p) && QChar::isLowSurrogate(p[1])) {
5216 prop = qGetProp(QChar::surrogateToUcs4(high, *p));
5218 prop = qGetProp(*p);
5220 if (prop->caseFoldDiff) {
5221 if (QChar::isLowSurrogate(*p))
5222 --p; // safe; diff is 0 for surrogates
5223 QString s(d->size, Qt::Uninitialized);
5224 memcpy(s.d->data(), d->data(), (p - d->data())*sizeof(ushort));
5225 ushort *pp = s.d->data() + (p - d->data());
5227 if (QChar::isHighSurrogate(*p) && QChar::isLowSurrogate(p[1])) {
5229 prop = qGetProp(QChar::surrogateToUcs4(*pp++, *p));
5231 prop = qGetProp(*p);
5233 if (prop->caseFoldSpecial) {
5234 const ushort *specialCase = specialCaseMap + prop->caseFoldDiff;
5235 ushort length = *specialCase++;
5237 int pos = pp - s.d->data;
5238 s.resize(s.d->size + length - 1);
5239 pp = s.d->data + pos;
5241 *pp++ = *specialCase++;
5243 //### we currently don't support full case foldings
5244 Q_ASSERT(length == 1);
5246 *pp++ = *specialCase;
5249 *pp++ = *p + prop->caseFoldDiff;
5254 // this restores high surrogate parts eaten above, if any
5255 while (e != d->data() + d->size)
5266 Returns an uppercase copy of the string.
5268 \snippet qstring/main.cpp 81
5270 The case conversion will always happen in the 'C' locale. For locale dependent
5271 case folding use QLocale::toUpper()
5273 \sa toLower(), QLocale::toLower()
5275 QString QString::toUpper() const
5277 const ushort *p = d->data();
5281 const ushort *e = p + d->size;
5282 // this avoids out of bounds check in the loop
5283 while (e != p && QChar::isHighSurrogate(*(e - 1)))
5286 const QUnicodeTables::Properties *prop;
5288 if (QChar::isHighSurrogate(*p) && QChar::isLowSurrogate(p[1])) {
5290 prop = qGetProp(QChar::surrogateToUcs4(high, *p));
5292 prop = qGetProp(*p);
5294 if (prop->upperCaseDiff) {
5295 if (QChar::isLowSurrogate(*p))
5296 --p; // safe; diff is 0 for surrogates
5297 QString s(d->size, Qt::Uninitialized);
5298 memcpy(s.d->data(), d->data(), (p - d->data())*sizeof(ushort));
5299 ushort *pp = s.d->data() + (p - d->data());
5301 if (QChar::isHighSurrogate(*p) && QChar::isLowSurrogate(p[1])) {
5303 prop = qGetProp(QChar::surrogateToUcs4(*pp++, *p));
5305 prop = qGetProp(*p);
5307 if (prop->upperCaseSpecial) {
5308 const ushort *specialCase = specialCaseMap + prop->upperCaseDiff;
5309 ushort length = *specialCase++;
5310 int pos = pp - s.d->data();
5311 s.resize(s.d->size + length - 1);
5312 pp = s.d->data() + pos;
5314 *pp++ = *specialCase++;
5316 *pp++ = *p + prop->upperCaseDiff;
5321 // this restores high surrogate parts eaten above, if any
5322 while (e != d->data() + d->size)
5332 // ### Qt 6: Consider whether this function shouldn't be removed See task 202871.
5334 Safely builds a formatted string from the format string \a cformat
5335 and an arbitrary list of arguments.
5337 The %lc escape sequence expects a unicode character of type ushort
5338 (as returned by QChar::unicode()). The %ls escape sequence expects
5339 a pointer to a zero-terminated array of unicode characters of type
5340 ushort (as returned by QString::utf16()).
5342 \note This function expects a UTF-8 string for %s and Latin-1 for
5345 The format string supports most of the conversion specifiers
5346 provided by printf() in the standard C++ library. It doesn't
5347 honor the length modifiers (e.g. \c h for \c short, \c ll for
5348 \c{long long}). If you need those, use the standard snprintf()
5351 \snippet qstring/main.cpp 63
5353 \warning We do not recommend using QString::sprintf() in new Qt
5354 code. Instead, consider using QTextStream or arg(), both of
5355 which support Unicode strings seamlessly and are type-safe.
5356 Here's an example that uses QTextStream:
5358 \snippet qstring/main.cpp 64
5360 For \l {QObject::tr()}{translations}, especially if the strings
5361 contains more than one escape sequence, you should consider using
5362 the arg() function instead. This allows the order of the
5363 replacements to be controlled by the translator.
5368 QString &QString::sprintf(const char *cformat, ...)
5371 va_start(ap, cformat);
5372 QString &s = vsprintf(cformat, ap);
5378 Equivalent method to sprintf(), but takes a va_list \a ap
5379 instead a list of variable arguments. See the sprintf()
5380 documentation for an explanation of \a cformat.
5382 This method does not call the va_end macro, the caller
5383 is responsible to call va_end on \a ap.
5388 QString &QString::vsprintf(const char* cformat, va_list ap)
5390 QLocale locale(QLocale::C);
5392 if (!cformat || !*cformat) {
5394 *this = fromLatin1("");
5401 const char *c = cformat;
5403 // Copy non-escape chars to result
5404 while (*c != '\0' && *c != '%')
5405 result.append(QLatin1Char(*c++));
5411 const char *escape_start = c;
5415 result.append(QLatin1Char('%')); // a % at the end of the string - treat as non-escape text
5419 result.append(QLatin1Char('%')); // %%
5424 // Parse flag characters
5426 bool no_more_flags = false;
5429 case '#': flags |= QLocalePrivate::Alternate; break;
5430 case '0': flags |= QLocalePrivate::ZeroPadded; break;
5431 case '-': flags |= QLocalePrivate::LeftAdjusted; break;
5432 case ' ': flags |= QLocalePrivate::BlankBeforePositive; break;
5433 case '+': flags |= QLocalePrivate::AlwaysShowSign; break;
5434 case '\'': flags |= QLocalePrivate::ThousandsGroup; break;
5435 default: no_more_flags = true; break;
5440 } while (!no_more_flags);
5443 result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text
5447 // Parse field width
5448 int width = -1; // -1 means unspecified
5451 while (*c != '\0' && qIsDigit(*c))
5452 width_str.append(QLatin1Char(*c++));
5454 // can't be negative - started with a digit
5455 // contains at least one digit
5456 width = width_str.toInt();
5458 else if (*c == '*') {
5459 width = va_arg(ap, int);
5461 width = -1; // treat all negative numbers as unspecified
5466 result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text
5471 int precision = -1; // -1 means unspecified
5475 QString precision_str;
5476 while (*c != '\0' && qIsDigit(*c))
5477 precision_str.append(QLatin1Char(*c++));
5479 // can't be negative - started with a digit
5480 // contains at least one digit
5481 precision = precision_str.toInt();
5483 else if (*c == '*') {
5484 precision = va_arg(ap, int);
5486 precision = -1; // treat all negative numbers as unspecified
5492 result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text
5496 // Parse the length modifier
5497 enum LengthMod { lm_none, lm_hh, lm_h, lm_l, lm_ll, lm_L, lm_j, lm_z, lm_t };
5498 LengthMod length_mod = lm_none;
5545 result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text
5549 // Parse the conversion specifier and do the conversion
5555 switch (length_mod) {
5556 case lm_none: i = va_arg(ap, int); break;
5557 case lm_hh: i = va_arg(ap, int); break;
5558 case lm_h: i = va_arg(ap, int); break;
5559 case lm_l: i = va_arg(ap, long int); break;
5560 case lm_ll: i = va_arg(ap, qint64); break;
5561 case lm_j: i = va_arg(ap, long int); break;
5562 case lm_z: i = va_arg(ap, size_t); break;
5563 case lm_t: i = va_arg(ap, int); break;
5564 default: i = 0; break;
5566 subst = locale.d->longLongToString(i, precision, 10, width, flags);
5575 switch (length_mod) {
5576 case lm_none: u = va_arg(ap, uint); break;
5577 case lm_hh: u = va_arg(ap, uint); break;
5578 case lm_h: u = va_arg(ap, uint); break;
5579 case lm_l: u = va_arg(ap, ulong); break;
5580 case lm_ll: u = va_arg(ap, quint64); break;
5581 case lm_z: u = va_arg(ap, size_t); break;
5582 default: u = 0; break;
5586 flags |= QLocalePrivate::CapitalEorX;
5589 switch (qToLower(*c)) {
5598 subst = locale.d->unsLongLongToString(u, precision, base, width, flags);
5611 if (length_mod == lm_L)
5612 d = va_arg(ap, long double); // not supported - converted to a double
5614 d = va_arg(ap, double);
5617 flags |= QLocalePrivate::CapitalEorX;
5619 QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
5620 switch (qToLower(*c)) {
5621 case 'e': form = QLocalePrivate::DFExponent; break;
5622 case 'a': // not supported - decimal form used instead
5623 case 'f': form = QLocalePrivate::DFDecimal; break;
5624 case 'g': form = QLocalePrivate::DFSignificantDigits; break;
5627 subst = locale.d->doubleToString(d, precision, form, width, flags);
5632 if (length_mod == lm_l)
5633 subst = QChar((ushort) va_arg(ap, int));
5635 subst = QLatin1Char((uchar) va_arg(ap, int));
5640 if (length_mod == lm_l) {
5641 const ushort *buff = va_arg(ap, const ushort*);
5642 const ushort *ch = buff;
5645 subst.setUtf16(buff, ch - buff);
5647 subst = QString::fromUtf8(va_arg(ap, const char*));
5648 if (precision != -1)
5649 subst.truncate(precision);
5654 void *arg = va_arg(ap, void*);
5656 quint64 i = reinterpret_cast<quint64>(arg);
5658 quint64 i = reinterpret_cast<unsigned long>(arg);
5660 flags |= QLocalePrivate::Alternate;
5661 subst = locale.d->unsLongLongToString(i, precision, 16, width, flags);
5666 switch (length_mod) {
5668 signed char *n = va_arg(ap, signed char*);
5669 *n = result.length();
5673 short int *n = va_arg(ap, short int*);
5674 *n = result.length();
5678 long int *n = va_arg(ap, long int*);
5679 *n = result.length();
5683 qint64 *n = va_arg(ap, qint64*);
5684 volatile uint tmp = result.length(); // egcs-2.91.66 gets internal
5685 *n = tmp; // compiler error without volatile
5689 int *n = va_arg(ap, int*);
5690 *n = result.length();
5697 default: // bad escape, treat as non-escape text
5698 for (const char *cc = escape_start; cc != c; ++cc)
5699 result.append(QLatin1Char(*cc));
5703 if (flags & QLocalePrivate::LeftAdjusted)
5704 result.append(subst.leftJustified(width));
5706 result.append(subst.rightJustified(width));
5715 Returns the string converted to a \c{long long} using base \a
5716 base, which is 10 by default and must be between 2 and 36, or 0.
5717 Returns 0 if the conversion fails.
5719 If a conversion error occurs, *\a{ok} is set to false; otherwise
5720 *\a{ok} is set to true.
5722 If \a base is 0, the C language convention is used: If the string
5723 begins with "0x", base 16 is used; if the string begins with "0",
5724 base 8 is used; otherwise, base 10 is used.
5726 The string conversion will always happen in the 'C' locale. For locale
5727 dependent conversion use QLocale::toLongLong()
5731 \snippet qstring/main.cpp 74
5733 \sa number(), toULongLong(), toInt(), QLocale::toLongLong()
5736 qint64 QString::toLongLong(bool *ok, int base) const
5738 #if defined(QT_CHECK_RANGE)
5739 if (base != 0 && (base < 2 || base > 36)) {
5740 qWarning("QString::toLongLong: Invalid base (%d)", base);
5745 QLocale c_locale(QLocale::C);
5746 return c_locale.d->stringToLongLong(*this, base, ok, QLocalePrivate::FailOnGroupSeparators);
5750 Returns the string converted to an \c{unsigned long long} using base \a
5751 base, which is 10 by default and must be between 2 and 36, or 0.
5752 Returns 0 if the conversion fails.
5754 If a conversion error occurs, *\a{ok} is set to false; otherwise
5755 *\a{ok} is set to true.
5757 If \a base is 0, the C language convention is used: If the string
5758 begins with "0x", base 16 is used; if the string begins with "0",
5759 base 8 is used; otherwise, base 10 is used.
5761 The string conversion will always happen in the 'C' locale. For locale
5762 dependent conversion use QLocale::toULongLong()
5766 \snippet qstring/main.cpp 79
5768 \sa number(), toLongLong(), QLocale::toULongLong()
5771 quint64 QString::toULongLong(bool *ok, int base) const
5773 #if defined(QT_CHECK_RANGE)
5774 if (base != 0 && (base < 2 || base > 36)) {
5775 qWarning("QString::toULongLong: Invalid base (%d)", base);
5780 QLocale c_locale(QLocale::C);
5781 return c_locale.d->stringToUnsLongLong(*this, base, ok, QLocalePrivate::FailOnGroupSeparators);
5785 \fn long QString::toLong(bool *ok, int base) const
5787 Returns the string converted to a \c long using base \a
5788 base, which is 10 by default and must be between 2 and 36, or 0.
5789 Returns 0 if the conversion fails.
5791 If a conversion error occurs, *\a{ok} is set to false; otherwise
5792 *\a{ok} is set to true.
5794 If \a base is 0, the C language convention is used: If the string
5795 begins with "0x", base 16 is used; if the string begins with "0",
5796 base 8 is used; otherwise, base 10 is used.
5798 The string conversion will always happen in the 'C' locale. For locale
5799 dependent conversion use QLocale::toLong()
5803 \snippet qstring/main.cpp 73
5805 \sa number(), toULong(), toInt(), QLocale::toLong()
5808 long QString::toLong(bool *ok, int base) const
5810 qint64 v = toLongLong(ok, base);
5811 if (v < LONG_MIN || v > LONG_MAX) {
5820 \fn ulong QString::toULong(bool *ok, int base) const
5822 Returns the string converted to an \c{unsigned long} using base \a
5823 base, which is 10 by default and must be between 2 and 36, or 0.
5824 Returns 0 if the conversion fails.
5826 If a conversion error occurs, *\a{ok} is set to false; otherwise
5827 *\a{ok} is set to true.
5829 If \a base is 0, the C language convention is used: If the string
5830 begins with "0x", base 16 is used; if the string begins with "0",
5831 base 8 is used; otherwise, base 10 is used.
5833 The string conversion will always happen in the 'C' locale. For locale
5834 dependent conversion use QLocale::toULong()
5838 \snippet qstring/main.cpp 78
5840 \sa number(), QLocale::toULong()
5843 ulong QString::toULong(bool *ok, int base) const
5845 quint64 v = toULongLong(ok, base);
5846 if (v > ULONG_MAX) {
5856 Returns the string converted to an \c int using base \a
5857 base, which is 10 by default and must be between 2 and 36, or 0.
5858 Returns 0 if the conversion fails.
5860 If a conversion error occurs, *\a{ok} is set to false; otherwise
5861 *\a{ok} is set to true.
5863 If \a base is 0, the C language convention is used: If the string
5864 begins with "0x", base 16 is used; if the string begins with "0",
5865 base 8 is used; otherwise, base 10 is used.
5867 The string conversion will always happen in the 'C' locale. For locale
5868 dependent conversion use QLocale::toInt()
5872 \snippet qstring/main.cpp 72
5874 \sa number(), toUInt(), toDouble(), QLocale::toInt()
5877 int QString::toInt(bool *ok, int base) const
5879 qint64 v = toLongLong(ok, base);
5880 if (v < INT_MIN || v > INT_MAX) {
5889 Returns the string converted to an \c{unsigned int} using base \a
5890 base, which is 10 by default and must be between 2 and 36, or 0.
5891 Returns 0 if the conversion fails.
5893 If a conversion error occurs, *\a{ok} is set to false; otherwise
5894 *\a{ok} is set to true.
5896 If \a base is 0, the C language convention is used: If the string
5897 begins with "0x", base 16 is used; if the string begins with "0",
5898 base 8 is used; otherwise, base 10 is used.
5900 The string conversion will always happen in the 'C' locale. For locale
5901 dependent conversion use QLocale::toUInt()
5905 \snippet qstring/main.cpp 77
5907 \sa number(), toInt(), QLocale::toUInt()
5910 uint QString::toUInt(bool *ok, int base) const
5912 quint64 v = toULongLong(ok, base);
5922 Returns the string converted to a \c short using base \a
5923 base, which is 10 by default and must be between 2 and 36, or 0.
5924 Returns 0 if the conversion fails.
5926 If a conversion error occurs, *\a{ok} is set to false; otherwise
5927 *\a{ok} is set to true.
5929 If \a base is 0, the C language convention is used: If the string
5930 begins with "0x", base 16 is used; if the string begins with "0",
5931 base 8 is used; otherwise, base 10 is used.
5933 The string conversion will always happen in the 'C' locale. For locale
5934 dependent conversion use QLocale::toShort()
5938 \snippet qstring/main.cpp 76
5940 \sa number(), toUShort(), toInt(), QLocale::toShort()
5943 short QString::toShort(bool *ok, int base) const
5945 long v = toLongLong(ok, base);
5946 if (v < SHRT_MIN || v > SHRT_MAX) {
5955 Returns the string converted to an \c{unsigned short} using base \a
5956 base, which is 10 by default and must be between 2 and 36, or 0.
5957 Returns 0 if the conversion fails.
5959 If a conversion error occurs, *\a{ok} is set to false; otherwise
5960 *\a{ok} is set to true.
5962 If \a base is 0, the C language convention is used: If the string
5963 begins with "0x", base 16 is used; if the string begins with "0",
5964 base 8 is used; otherwise, base 10 is used.
5966 The string conversion will always happen in the 'C' locale. For locale
5967 dependent conversion use QLocale::toUShort()
5971 \snippet qstring/main.cpp 80
5973 \sa number(), toShort(), QLocale::toUShort()
5976 ushort QString::toUShort(bool *ok, int base) const
5978 ulong v = toULongLong(ok, base);
5979 if (v > USHRT_MAX) {
5989 Returns the string converted to a \c double value.
5991 Returns 0.0 if the conversion fails.
5993 If a conversion error occurs, \c{*}\a{ok} is set to false;
5994 otherwise \c{*}\a{ok} is set to true.
5996 \snippet qstring/main.cpp 66
5998 Various string formats for floating point numbers can be converted
6001 \snippet qstring/main.cpp 67
6003 The string conversion will always happen in the 'C' locale. For locale
6004 dependent conversion use QLocale::toDouble()
6006 \snippet qstring/main.cpp 68
6008 For historic reasons, this function does not handle
6009 thousands group separators. If you need to convert such numbers,
6010 use QLocale::toDouble().
6012 \snippet qstring/main.cpp 69
6014 \sa number(), QLocale::setDefault(), QLocale::toDouble(), trimmed()
6017 double QString::toDouble(bool *ok) const
6019 QLocale c_locale(QLocale::C);
6020 return c_locale.d->stringToDouble(*this, ok, QLocalePrivate::FailOnGroupSeparators);
6024 Returns the string converted to a \c float value.
6026 If a conversion error occurs, *\a{ok} is set to false; otherwise
6027 *\a{ok} is set to true. Returns 0.0 if the conversion fails.
6029 The string conversion will always happen in the 'C' locale. For locale
6030 dependent conversion use QLocale::toFloat()
6034 \snippet qstring/main.cpp 71
6036 \sa number(), toDouble(), toInt(), QLocale::toFloat()
6039 #define QT_MAX_FLOAT 3.4028234663852886e+38
6041 float QString::toFloat(bool *ok) const
6044 double d = toDouble(&myOk);
6052 if (d > QT_MAX_FLOAT || d < -QT_MAX_FLOAT) {
6062 /*! \fn QString &QString::setNum(int n, int base)
6064 Sets the string to the printed value of \a n in the specified \a
6065 base, and returns a reference to the string.
6067 The base is 10 by default and must be between 2 and 36. For bases
6068 other than 10, \a n is treated as an unsigned integer.
6070 \snippet qstring/main.cpp 56
6072 The formatting always uses QLocale::C, i.e., English/UnitedStates.
6073 To get a localized string representation of a number, use
6074 QLocale::toString() with the appropriate locale.
6077 /*! \fn QString &QString::setNum(uint n, int base)
6082 /*! \fn QString &QString::setNum(long n, int base)
6087 /*! \fn QString &QString::setNum(ulong n, int base)
6095 QString &QString::setNum(qlonglong n, int base)
6097 #if defined(QT_CHECK_RANGE)
6098 if (base < 2 || base > 36) {
6099 qWarning("QString::setNum: Invalid base (%d)", base);
6103 QLocale locale(QLocale::C);
6104 *this = locale.d->longLongToString(n, -1, base);
6111 QString &QString::setNum(qulonglong n, int base)
6113 #if defined(QT_CHECK_RANGE)
6114 if (base < 2 || base > 36) {
6115 qWarning("QString::setNum: Invalid base (%d)", base);
6119 QLocale locale(QLocale::C);
6120 *this = locale.d->unsLongLongToString(n, -1, base);
6124 /*! \fn QString &QString::setNum(short n, int base)
6129 /*! \fn QString &QString::setNum(ushort n, int base)
6135 \fn QString &QString::setNum(double n, char format, int precision)
6138 Sets the string to the printed value of \a n, formatted according
6139 to the given \a format and \a precision, and returns a reference
6142 The \a format can be 'f', 'F', 'e', 'E', 'g' or 'G' (see the
6143 arg() function documentation for an explanation of the formats).
6145 The formatting always uses QLocale::C, i.e., English/UnitedStates.
6146 To get a localized string representation of a number, use
6147 QLocale::toString() with the appropriate locale.
6150 QString &QString::setNum(double n, char f, int prec)
6152 QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
6156 flags = QLocalePrivate::CapitalEorX;
6161 form = QLocalePrivate::DFDecimal;
6164 form = QLocalePrivate::DFExponent;
6167 form = QLocalePrivate::DFSignificantDigits;
6170 #if defined(QT_CHECK_RANGE)
6171 qWarning("QString::setNum: Invalid format char '%c'", f);
6176 QLocale locale(QLocale::C);
6177 *this = locale.d->doubleToString(n, prec, form, -1, flags);
6182 \fn QString &QString::setNum(float n, char format, int precision)
6185 Sets the string to the printed value of \a n, formatted according
6186 to the given \a format and \a precision, and returns a reference
6189 The formatting always uses QLocale::C, i.e., English/UnitedStates.
6190 To get a localized string representation of a number, use
6191 QLocale::toString() with the appropriate locale.
6196 \fn QString QString::number(long n, int base)
6198 Returns a string equivalent of the number \a n according to the
6201 The base is 10 by default and must be between 2
6202 and 36. For bases other than 10, \a n is treated as an
6205 The formatting always uses QLocale::C, i.e., English/UnitedStates.
6206 To get a localized string representation of a number, use
6207 QLocale::toString() with the appropriate locale.
6209 \snippet qstring/main.cpp 35
6214 QString QString::number(long n, int base)
6222 \fn QString QString::number(ulong n, int base)
6226 QString QString::number(ulong n, int base)
6236 QString QString::number(int n, int base)
6246 QString QString::number(uint n, int base)
6256 QString QString::number(qlonglong n, int base)
6266 QString QString::number(qulonglong n, int base)
6275 \fn QString QString::number(double n, char format, int precision)
6277 Returns a string equivalent of the number \a n, formatted
6278 according to the specified \a format and \a precision. See
6279 \l{Argument Formats} for details.
6281 Unlike QLocale::toString(), this function does not honor the
6282 user's locale settings.
6284 \sa setNum(), QLocale::toString()
6286 QString QString::number(double n, char f, int prec)
6289 s.setNum(n, f, prec);
6294 Splits the string into substrings wherever \a sep occurs, and
6295 returns the list of those strings. If \a sep does not match
6296 anywhere in the string, split() returns a single-element list
6297 containing this string.
6299 \a cs specifies whether \a sep should be matched case
6300 sensitively or case insensitively.
6302 If \a behavior is QString::SkipEmptyParts, empty entries don't
6303 appear in the result. By default, empty entries are kept.
6307 \snippet qstring/main.cpp 62
6309 \sa QStringList::join(), section()
6311 QStringList QString::split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
6317 while ((end = indexOf(sep, start + extra, cs)) != -1) {
6318 if (start != end || behavior == KeepEmptyParts)
6319 list.append(mid(start, end - start));
6320 start = end + sep.size();
6321 extra = (sep.size() == 0 ? 1 : 0);
6323 if (start != size() || behavior == KeepEmptyParts)
6324 list.append(mid(start));
6331 QStringList QString::split(QChar sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
6336 while ((end = indexOf(sep, start, cs)) != -1) {
6337 if (start != end || behavior == KeepEmptyParts)
6338 list.append(mid(start, end - start));
6341 if (start != size() || behavior == KeepEmptyParts)
6342 list.append(mid(start));
6346 #ifndef QT_NO_REGEXP
6350 Splits the string into substrings wherever the regular expression
6351 \a rx matches, and returns the list of those strings. If \a rx
6352 does not match anywhere in the string, split() returns a
6353 single-element list containing this string.
6355 Here's an example where we extract the words in a sentence
6356 using one or more whitespace characters as the separator:
6358 \snippet qstring/main.cpp 59
6360 Here's a similar example, but this time we use any sequence of
6361 non-word characters as the separator:
6363 \snippet qstring/main.cpp 60
6365 Here's a third example where we use a zero-length assertion,
6366 \b{\\b} (word boundary), to split the string into an
6367 alternating sequence of non-word and word tokens:
6369 \snippet qstring/main.cpp 61
6371 \sa QStringList::join(), section()
6373 QStringList QString::split(const QRegExp &rx, SplitBehavior behavior) const
6380 while ((end = rx2.indexIn(*this, start + extra)) != -1) {
6381 int matchedLen = rx2.matchedLength();
6382 if (start != end || behavior == KeepEmptyParts)
6383 list.append(mid(start, end - start));
6384 start = end + matchedLen;
6385 extra = (matchedLen == 0) ? 1 : 0;
6387 if (start != size() || behavior == KeepEmptyParts)
6388 list.append(mid(start));
6393 #ifndef QT_NO_REGEXP
6394 #ifndef QT_BOOTSTRAPPED
6399 Splits the string into substrings wherever the regular expression
6400 \a re matches, and returns the list of those strings. If \a re
6401 does not match anywhere in the string, split() returns a
6402 single-element list containing this string.
6404 Here's an example where we extract the words in a sentence
6405 using one or more whitespace characters as the separator:
6407 \snippet qstring/main.cpp 90
6409 Here's a similar example, but this time we use any sequence of
6410 non-word characters as the separator:
6412 \snippet qstring/main.cpp 91
6414 Here's a third example where we use a zero-length assertion,
6415 \b{\\b} (word boundary), to split the string into an
6416 alternating sequence of non-word and word tokens:
6418 \snippet qstring/main.cpp 92
6420 \sa QStringList::join(), section()
6422 QStringList QString::split(const QRegularExpression &re, SplitBehavior behavior) const
6425 if (!re.isValid()) {
6426 qWarning("QString::split: invalid QRegularExpression object");
6432 QRegularExpressionMatchIterator iterator = re.globalMatch(*this);
6433 while (iterator.hasNext()) {
6434 QRegularExpressionMatch match = iterator.next();
6435 end = match.capturedStart();
6436 if (start != end || behavior == KeepEmptyParts)
6437 list.append(mid(start, end - start));
6438 start = match.capturedEnd();
6441 if (start != size() || behavior == KeepEmptyParts)
6442 list.append(mid(start));
6446 #endif // QT_BOOTSTRAPPED
6447 #endif // QT_NO_REGEXP
6450 \enum QString::NormalizationForm
6452 This enum describes the various normalized forms of Unicode text.
6454 \value NormalizationForm_D Canonical Decomposition
6455 \value NormalizationForm_C Canonical Decomposition followed by Canonical Composition
6456 \value NormalizationForm_KD Compatibility Decomposition
6457 \value NormalizationForm_KC Compatibility Decomposition followed by Canonical Composition
6460 {http://www.unicode.org/reports/tr15/}{Unicode Standard Annex #15}
6466 Returns a copy of this string repeated the specified number of \a times.
6468 If \a times is less than 1, an empty string is returned.
6474 str.repeated(4); // returns "abababab"
6477 QString QString::repeated(int times) const
6488 const int resultSize = times * d->size;
6491 result.reserve(resultSize);
6492 if (result.d->alloc != uint(resultSize) + 1u)
6493 return QString(); // not enough memory
6495 memcpy(result.d->data(), d->data(), d->size * sizeof(ushort));
6497 int sizeSoFar = d->size;
6498 ushort *end = result.d->data() + sizeSoFar;
6500 const int halfResultSize = resultSize >> 1;
6501 while (sizeSoFar <= halfResultSize) {
6502 memcpy(end, result.d->data(), sizeSoFar * sizeof(ushort));
6506 memcpy(end, result.d->data(), (resultSize - sizeSoFar) * sizeof(ushort));
6507 result.d->data()[resultSize] = '\0';
6508 result.d->size = resultSize;
6512 void qt_string_normalize(QString *data, QString::NormalizationForm mode, QChar::UnicodeVersion version, int from)
6515 const QChar *p = data->constData();
6516 int len = data->length();
6517 for (int i = from; i < len; ++i) {
6518 if (p[i].unicode() >= 0x80) {
6526 if (version == QChar::Unicode_Unassigned) {
6527 version = QChar::currentUnicodeVersion();
6528 } else if (int(version) <= NormalizationCorrectionsVersionMax) {
6529 const QString &s = *data;
6531 for (int i = 0; i < NumNormalizationCorrections; ++i) {
6532 const NormalizationCorrection &n = uc_normalization_corrections[i];
6533 if (n.version > version) {
6535 if (QChar::requiresSurrogates(n.ucs4)) {
6536 ushort ucs4High = QChar::highSurrogate(n.ucs4);
6537 ushort ucs4Low = QChar::lowSurrogate(n.ucs4);
6538 ushort oldHigh = QChar::highSurrogate(n.old_mapping);
6539 ushort oldLow = QChar::lowSurrogate(n.old_mapping);
6540 while (pos < s.length() - 1) {
6541 if (s.at(pos).unicode() == ucs4High && s.at(pos + 1).unicode() == ucs4Low) {
6544 d[pos] = QChar(oldHigh);
6545 d[++pos] = QChar(oldLow);
6550 while (pos < s.length()) {
6551 if (s.at(pos).unicode() == n.ucs4) {
6554 d[pos] = QChar(n.old_mapping);
6562 decomposeHelper(data, mode < QString::NormalizationForm_KD, version, from);
6564 canonicalOrderHelper(data, version, from);
6566 if (mode == QString::NormalizationForm_D || mode == QString::NormalizationForm_KD)
6569 composeHelper(data, version, from);
6573 Returns the string in the given Unicode normalization \a mode,
6574 according to the given \a version of the Unicode standard.
6576 QString QString::normalized(QString::NormalizationForm mode, QChar::UnicodeVersion version) const
6578 QString copy = *this;
6579 qt_string_normalize(©, mode, version, 0);
6584 struct ArgEscapeData
6586 int min_escape; // lowest escape sequence number
6587 int occurrences; // number of occurrences of the lowest escape sequence number
6588 int locale_occurrences; // number of occurrences of the lowest escape sequence number that
6590 int escape_len; // total length of escape sequences which will be replaced
6593 static ArgEscapeData findArgEscapes(const QString &s)
6595 const QChar *uc_begin = s.unicode();
6596 const QChar *uc_end = uc_begin + s.length();
6600 d.min_escape = INT_MAX;
6603 d.locale_occurrences = 0;
6605 const QChar *c = uc_begin;
6606 while (c != uc_end) {
6607 while (c != uc_end && c->unicode() != '%')
6612 const QChar *escape_start = c;
6616 bool locale_arg = false;
6617 if (c->unicode() == 'L') {
6623 if (c->digitValue() == -1)
6626 int escape = c->digitValue();
6629 if (c != uc_end && c->digitValue() != -1) {
6630 escape = (10 * escape) + c->digitValue();
6634 if (escape > d.min_escape)
6637 if (escape < d.min_escape) {
6638 d.min_escape = escape;
6641 d.locale_occurrences = 0;
6646 ++d.locale_occurrences;
6647 d.escape_len += c - escape_start;
6652 static QString replaceArgEscapes(const QString &s, const ArgEscapeData &d, int field_width,
6653 const QString &arg, const QString &larg, QChar fillChar = QLatin1Char(' '))
6655 const QChar *uc_begin = s.unicode();
6656 const QChar *uc_end = uc_begin + s.length();
6658 int abs_field_width = qAbs(field_width);
6659 int result_len = s.length()
6661 + (d.occurrences - d.locale_occurrences)
6662 *qMax(abs_field_width, arg.length())
6663 + d.locale_occurrences
6664 *qMax(abs_field_width, larg.length());
6666 QString result(result_len, Qt::Uninitialized);
6667 QChar *result_buff = (QChar*) result.unicode();
6669 QChar *rc = result_buff;
6670 const QChar *c = uc_begin;
6672 while (c != uc_end) {
6673 /* We don't have to check if we run off the end of the string with c,
6674 because as long as d.occurrences > 0 we KNOW there are valid escape
6677 const QChar *text_start = c;
6679 while (c->unicode() != '%')
6682 const QChar *escape_start = c++;
6684 bool locale_arg = false;
6685 if (c->unicode() == 'L') {
6690 int escape = c->digitValue();
6692 if (c + 1 != uc_end && (c + 1)->digitValue() != -1) {
6693 escape = (10 * escape) + (c + 1)->digitValue();
6698 if (escape != d.min_escape) {
6699 memcpy(rc, text_start, (c - text_start)*sizeof(QChar));
6700 rc += c - text_start;
6705 memcpy(rc, text_start, (escape_start - text_start)*sizeof(QChar));
6706 rc += escape_start - text_start;
6710 pad_chars = qMax(abs_field_width, larg.length()) - larg.length();
6712 pad_chars = qMax(abs_field_width, arg.length()) - arg.length();
6714 if (field_width > 0) { // left padded
6715 for (uint i = 0; i < pad_chars; ++i)
6716 (rc++)->unicode() = fillChar.unicode();
6720 memcpy(rc, larg.unicode(), larg.length()*sizeof(QChar));
6721 rc += larg.length();
6724 memcpy(rc, arg.unicode(), arg.length()*sizeof(QChar));
6728 if (field_width < 0) { // right padded
6729 for (uint i = 0; i < pad_chars; ++i)
6730 (rc++)->unicode() = fillChar.unicode();
6733 if (++repl_cnt == d.occurrences) {
6734 memcpy(rc, c, (uc_end - c)*sizeof(QChar));
6736 Q_ASSERT(rc - result_buff == result_len);
6741 Q_ASSERT(rc == result_buff + result_len);
6747 Returns a copy of this string with the lowest numbered place marker
6748 replaced by string \a a, i.e., \c %1, \c %2, ..., \c %99.
6750 \a fieldWidth specifies the minimum amount of space that argument \a
6751 a shall occupy. If \a a requires less space than \a fieldWidth, it
6752 is padded to \a fieldWidth with character \a fillChar. A positive
6753 \a fieldWidth produces right-aligned text. A negative \a fieldWidth
6754 produces left-aligned text.
6756 This example shows how we might create a \c status string for
6757 reporting progress while processing a list of files:
6759 \snippet qstring/main.cpp 11
6761 First, \c arg(i) replaces \c %1. Then \c arg(total) replaces \c
6762 %2. Finally, \c arg(fileName) replaces \c %3.
6764 One advantage of using arg() over sprintf() is that the order of the
6765 numbered place markers can change, if the application's strings are
6766 translated into other languages, but each arg() will still replace
6767 the lowest numbered unreplaced place marker, no matter where it
6768 appears. Also, if place marker \c %i appears more than once in the
6769 string, the arg() replaces all of them.
6771 If there is no unreplaced place marker remaining, a warning message
6772 is output and the result is undefined. Place marker numbers must be
6773 in the range 1 to 99.
6775 QString QString::arg(const QString &a, int fieldWidth, QChar fillChar) const
6777 ArgEscapeData d = findArgEscapes(*this);
6779 if (d.occurrences == 0) {
6780 qWarning("QString::arg: Argument missing: %s, %s", toLocal8Bit().data(),
6781 a.toLocal8Bit().data());
6784 return replaceArgEscapes(*this, d, fieldWidth, a, a, fillChar);
6788 \fn QString QString::arg(const QString& a1, const QString& a2) const
6791 This is the same as \c {str.arg(a1).arg(a2)}, except that the
6792 strings \a a1 and \a a2 are replaced in one pass. This can make a
6793 difference if \a a1 contains e.g. \c{%1}:
6795 \snippet qstring/main.cpp 13
6799 \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3) const
6802 This is the same as calling \c str.arg(a1).arg(a2).arg(a3), except
6803 that the strings \a a1, \a a2 and \a a3 are replaced in one pass.
6807 \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4) const
6810 This is the same as calling \c
6811 {str.arg(a1).arg(a2).arg(a3).arg(a4)}, except that the strings \a
6812 a1, \a a2, \a a3 and \a a4 are replaced in one pass.
6816 \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4, const QString& a5) const
6819 This is the same as calling \c
6820 {str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5)}, except that the strings
6821 \a a1, \a a2, \a a3, \a a4, and \a a5 are replaced in one pass.
6825 \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4, const QString& a5, const QString& a6) const
6828 This is the same as calling \c
6829 {str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5).arg(a6))}, except that
6830 the strings \a a1, \a a2, \a a3, \a a4, \a a5, and \a a6 are
6831 replaced in one pass.
6835 \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4, const QString& a5, const QString& a6, const QString& a7) const
6838 This is the same as calling \c
6839 {str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5).arg(a6).arg(a7)},
6840 except that the strings \a a1, \a a2, \a a3, \a a4, \a a5, \a a6,
6841 and \a a7 are replaced in one pass.
6845 \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4, const QString& a5, const QString& a6, const QString& a7, const QString& a8) const
6848 This is the same as calling \c
6849 {str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5).arg(a6).arg(a7).arg(a8)},
6850 except that the strings \a a1, \a a2, \a a3, \a a4, \a a5, \a a6, \a
6851 a7, and \a a8 are replaced in one pass.
6855 \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4, const QString& a5, const QString& a6, const QString& a7, const QString& a8, const QString& a9) const
6858 This is the same as calling \c
6859 {str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5).arg(a6).arg(a7).arg(a8).arg(a9)},
6860 except that the strings \a a1, \a a2, \a a3, \a a4, \a a5, \a a6, \a
6861 a7, \a a8, and \a a9 are replaced in one pass.
6864 /*! \fn QString QString::arg(int a, int fieldWidth, int base, QChar fillChar) const
6867 The \a a argument is expressed in base \a base, which is 10 by
6868 default and must be between 2 and 36. For bases other than 10, \a a
6869 is treated as an unsigned integer.
6871 \a fieldWidth specifies the minimum amount of space that \a a is
6872 padded to and filled with the character \a fillChar. A positive
6873 value produces right-aligned text; a negative value produces
6876 The '%' can be followed by an 'L', in which case the sequence is
6877 replaced with a localized representation of \a a. The conversion
6878 uses the default locale, set by QLocale::setDefault(). If no default
6879 locale was specified, the "C" locale is used. The 'L' flag is
6880 ignored if \a base is not 10.
6882 \snippet qstring/main.cpp 12
6883 \snippet qstring/main.cpp 14
6885 If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
6886 used. For negative numbers, zero padding might appear before the
6890 /*! \fn QString QString::arg(uint a, int fieldWidth, int base, QChar fillChar) const
6893 The \a base argument specifies the base to use when converting the
6894 integer \a a into a string. The base must be between 2 and 36.
6896 If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
6897 used. For negative numbers, zero padding might appear before the
6901 /*! \fn QString QString::arg(long a, int fieldWidth, int base, QChar fillChar) const
6904 \a fieldWidth specifies the minimum amount of space that \a a is
6905 padded to and filled with the character \a fillChar. A positive
6906 value produces right-aligned text; a negative value produces
6909 The \a a argument is expressed in the given \a base, which is 10 by
6910 default and must be between 2 and 36.
6912 The '%' can be followed by an 'L', in which case the sequence is
6913 replaced with a localized representation of \a a. The conversion
6914 uses the default locale. The default locale is determined from the
6915 system's locale settings at application startup. It can be changed
6916 using QLocale::setDefault(). The 'L' flag is ignored if \a base is
6919 \snippet qstring/main.cpp 12
6920 \snippet qstring/main.cpp 14
6922 If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
6923 used. For negative numbers, zero padding might appear before the
6927 /*! \fn QString QString::arg(ulong a, int fieldWidth, int base, QChar fillChar) const
6930 \a fieldWidth specifies the minimum amount of space that \a a is
6931 padded to and filled with the character \a fillChar. A positive
6932 value produces right-aligned text; a negative value produces
6935 The \a base argument specifies the base to use when converting the
6936 integer \a a to a string. The base must be between 2 and 36, with 8
6937 giving octal, 10 decimal, and 16 hexadecimal numbers.
6939 If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
6940 used. For negative numbers, zero padding might appear before the
6947 \a fieldWidth specifies the minimum amount of space that \a a is
6948 padded to and filled with the character \a fillChar. A positive
6949 value produces right-aligned text; a negative value produces
6952 The \a base argument specifies the base to use when converting the
6953 integer \a a into a string. The base must be between 2 and 36, with
6954 8 giving octal, 10 decimal, and 16 hexadecimal numbers.
6956 If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
6957 used. For negative numbers, zero padding might appear before the
6960 QString QString::arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const
6962 ArgEscapeData d = findArgEscapes(*this);
6964 if (d.occurrences == 0) {
6965 qWarning() << "QString::arg: Argument missing:" << *this << ',' << a;
6969 unsigned flags = QLocalePrivate::NoFlags;
6970 if (fillChar == QLatin1Char('0'))
6971 flags = QLocalePrivate::ZeroPadded;
6974 if (d.occurrences > d.locale_occurrences)
6975 arg = QLocale::c().d->longLongToString(a, -1, base, fieldWidth, flags);
6978 if (d.locale_occurrences > 0) {
6980 if (!locale.numberOptions() & QLocale::OmitGroupSeparator)
6981 flags |= QLocalePrivate::ThousandsGroup;
6982 locale_arg = locale.d->longLongToString(a, -1, base, fieldWidth, flags);
6985 return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg, fillChar);
6991 \a fieldWidth specifies the minimum amount of space that \a a is
6992 padded to and filled with the character \a fillChar. A positive
6993 value produces right-aligned text; a negative value produces
6996 The \a base argument specifies the base to use when converting the
6997 integer \a a into a string. \a base must be between 2 and 36, with 8
6998 giving octal, 10 decimal, and 16 hexadecimal numbers.
7000 If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
7001 used. For negative numbers, zero padding might appear before the
7004 QString QString::arg(qulonglong a, int fieldWidth, int base, QChar fillChar) const
7006 ArgEscapeData d = findArgEscapes(*this);
7008 if (d.occurrences == 0) {
7009 qWarning() << "QString::arg: Argument missing:" << *this << ',' << a;
7013 unsigned flags = QLocalePrivate::NoFlags;
7014 if (fillChar == QLatin1Char('0'))
7015 flags = QLocalePrivate::ZeroPadded;
7018 if (d.occurrences > d.locale_occurrences)
7019 arg = QLocale::c().d->unsLongLongToString(a, -1, base, fieldWidth, flags);
7022 if (d.locale_occurrences > 0) {
7024 if (!locale.numberOptions() & QLocale::OmitGroupSeparator)
7025 flags |= QLocalePrivate::ThousandsGroup;
7026 locale_arg = locale.d->unsLongLongToString(a, -1, base, fieldWidth, flags);
7029 return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg, fillChar);
7035 \fn QString QString::arg(short a, int fieldWidth, int base, QChar fillChar) const
7037 \a fieldWidth specifies the minimum amount of space that \a a is
7038 padded to and filled with the character \a fillChar. A positive
7039 value produces right-aligned text; a negative value produces
7042 The \a base argument specifies the base to use when converting the
7043 integer \a a into a string. The base must be between 2 and 36, with
7044 8 giving octal, 10 decimal, and 16 hexadecimal numbers.
7046 If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
7047 used. For negative numbers, zero padding might appear before the
7052 \fn QString QString::arg(ushort a, int fieldWidth, int base, QChar fillChar) const
7055 \a fieldWidth specifies the minimum amount of space that \a a is
7056 padded to and filled with the character \a fillChar. A positive
7057 value produces right-aligned text; a negative value produces
7060 The \a base argument specifies the base to use when converting the
7061 integer \a a into a string. The base must be between 2 and 36, with
7062 8 giving octal, 10 decimal, and 16 hexadecimal numbers.
7064 If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
7065 used. For negative numbers, zero padding might appear before the
7072 QString QString::arg(QChar a, int fieldWidth, QChar fillChar) const
7076 return arg(c, fieldWidth, fillChar);
7082 The \a a argument is interpreted as a Latin-1 character.
7084 QString QString::arg(char a, int fieldWidth, QChar fillChar) const
7087 c += QLatin1Char(a);
7088 return arg(c, fieldWidth, fillChar);
7092 \fn QString QString::arg(double a, int fieldWidth, char format, int precision, QChar fillChar) const
7095 Argument \a a is formatted according to the specified \a format and
7096 \a precision. See \l{Argument Formats} for details.
7098 \a fieldWidth specifies the minimum amount of space that \a a is
7099 padded to and filled with the character \a fillChar. A positive
7100 value produces right-aligned text; a negative value produces
7103 \snippet code/src_corelib_tools_qstring.cpp 2
7105 The '%' can be followed by an 'L', in which case the sequence is
7106 replaced with a localized representation of \a a. The conversion
7107 uses the default locale, set by QLocale::setDefaultLocale(). If no
7108 default locale was specified, the "C" locale is used.
7110 If \a fillChar is '0' (the number 0, ASCII 48), this function will
7111 use the locale's zero to pad. For negative numbers, the zero padding
7112 will probably appear before the minus sign.
7114 \sa QLocale::toString()
7116 QString QString::arg(double a, int fieldWidth, char fmt, int prec, QChar fillChar) const
7118 ArgEscapeData d = findArgEscapes(*this);
7120 if (d.occurrences == 0) {
7121 qWarning("QString::arg: Argument missing: %s, %g", toLocal8Bit().data(), a);
7125 unsigned flags = QLocalePrivate::NoFlags;
7126 if (fillChar == QLatin1Char('0'))
7127 flags = QLocalePrivate::ZeroPadded;
7130 flags |= QLocalePrivate::CapitalEorX;
7131 fmt = qToLower(fmt);
7133 QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
7136 form = QLocalePrivate::DFDecimal;
7139 form = QLocalePrivate::DFExponent;
7142 form = QLocalePrivate::DFSignificantDigits;
7145 #if defined(QT_CHECK_RANGE)
7146 qWarning("QString::arg: Invalid format char '%c'", fmt);
7152 if (d.occurrences > d.locale_occurrences)
7153 arg = QLocale::c().d->doubleToString(a, prec, form, fieldWidth, flags);
7156 if (d.locale_occurrences > 0) {
7159 if (!locale.numberOptions() & QLocale::OmitGroupSeparator)
7160 flags |= QLocalePrivate::ThousandsGroup;
7161 locale_arg = locale.d->doubleToString(a, prec, form, fieldWidth, flags);
7164 return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg, fillChar);
7167 static int getEscape(const QChar *uc, int *pos, int len, int maxNumber = 999)
7171 if (i < len && uc[i] == QLatin1Char('L'))
7174 int escape = uc[i].unicode() - '0';
7175 if (uint(escape) >= 10U)
7179 int digit = uc[i].unicode() - '0';
7180 if (uint(digit) >= 10U)
7182 escape = (escape * 10) + digit;
7185 if (escape <= maxNumber) {
7193 QString QString::multiArg(int numArgs, const QString **args) const
7196 QMap<int, int> numbersUsed;
7197 const QChar *uc = (const QChar *) d->data();
7198 const int len = d->size;
7199 const int end = len - 1;
7200 int lastNumber = -1;
7203 // populate the numbersUsed map with the %n's that actually occur in the string
7205 if (uc[i] == QLatin1Char('%')) {
7206 int number = getEscape(uc, &i, len);
7208 numbersUsed.insert(number, -1);
7215 // assign an argument number to each of the %n's
7216 QMap<int, int>::iterator j = numbersUsed.begin();
7217 QMap<int, int>::iterator jend = numbersUsed.end();
7219 while (j != jend && arg < numArgs) {
7221 lastNumber = j.key();
7226 if (numArgs > arg) {
7227 qWarning("QString::arg: %d argument(s) missing in %s", numArgs - arg, toLocal8Bit().data());
7233 if (uc[i] == QLatin1Char('%') && i != end) {
7234 int number = getEscape(uc, &i, len, lastNumber);
7235 int arg = numbersUsed[number];
7236 if (number != -1 && arg != -1) {
7237 result += *args[arg];
7247 /*! \fn bool QString::isSimpleText() const
7251 bool QString::isSimpleText() const
7253 const ushort *p = d->data();
7254 const ushort * const end = p + d->size;
7257 // sort out regions of complex text formatting
7258 if (uc > 0x058f && (uc < 0x1100 || uc > 0xfb0f)) {
7267 /*! \fn bool QString::isRightToLeft() const
7269 Returns true if the string is read right to left.
7271 bool QString::isRightToLeft() const
7273 const ushort *p = d->data();
7274 const ushort * const end = p + d->size;
7277 if (QChar::isHighSurrogate(ucs4) && p < end - 1) {
7279 if (QChar::isLowSurrogate(low)) {
7280 ucs4 = QChar::surrogateToUcs4(ucs4, low);
7284 switch (QChar::direction(ucs4))
7299 /*! \fn QChar *QString::data()
7301 Returns a pointer to the data stored in the QString. The pointer
7302 can be used to access and modify the characters that compose the
7303 string. For convenience, the data is '\\0'-terminated.
7307 \snippet qstring/main.cpp 19
7309 Note that the pointer remains valid only as long as the string is
7310 not modified by other means. For read-only access, constData() is
7311 faster because it never causes a \l{deep copy} to occur.
7313 \sa constData(), operator[]()
7316 /*! \fn const QChar *QString::data() const
7321 /*! \fn const QChar *QString::constData() const
7323 Returns a pointer to the data stored in the QString. The pointer
7324 can be used to access the characters that compose the string. For
7325 convenience, the data is '\\0'-terminated.
7327 Note that the pointer remains valid only as long as the string is
7330 \sa data(), operator[]()
7333 /*! \fn void QString::push_front(const QString &other)
7335 This function is provided for STL compatibility, prepending the
7336 given \a other string to the beginning of this string. It is
7337 equivalent to \c prepend(other).
7342 /*! \fn void QString::push_front(QChar ch)
7346 Prepends the given \a ch character to the beginning of this string.
7349 /*! \fn void QString::push_back(const QString &other)
7351 This function is provided for STL compatibility, appending the
7352 given \a other string onto the end of this string. It is
7353 equivalent to \c append(other).
7358 /*! \fn void QString::push_back(QChar ch)
7362 Appends the given \a ch character onto the end of this string.
7366 \fn std::string QString::toStdString() const
7368 Returns a std::string object with the data contained in this
7369 QString. The Unicode data is converted into 8-bit characters using
7370 the toUtf8() function.
7372 This operator is mostly useful to pass a QString to a function
7373 that accepts a std::string object.
7375 If the QString contains non-Latin1 Unicode characters, using this
7376 can lead to loss of information.
7378 \sa toLatin1(), toUtf8(), toLocal8Bit()
7382 Constructs a QString that uses the first \a size Unicode characters
7383 in the array \a unicode. The data in \a unicode is \e not
7384 copied. The caller must be able to guarantee that \a unicode will
7385 not be deleted or modified as long as the QString (or an
7386 unmodified copy of it) exists.
7388 Any attempts to modify the QString or copies of it will cause it
7389 to create a deep copy of the data, ensuring that the raw data
7392 Here's an example of how we can use a QRegExp on raw data in
7393 memory without requiring to copy the data into a QString:
7395 \snippet qstring/main.cpp 22
7396 \snippet qstring/main.cpp 23
7398 \warning A string created with fromRawData() is \e not
7399 '\\0'-terminated, unless the raw data contains a '\\0' character
7400 at position \a size. This means unicode() will \e not return a
7401 '\\0'-terminated string (although utf16() does, at the cost of
7402 copying the raw data).
7404 \sa fromUtf16(), setRawData()
7406 QString QString::fromRawData(const QChar *unicode, int size)
7410 x = Data::sharedNull();
7412 x = Data::allocate(0);
7414 x = Data::fromRawData(reinterpret_cast<const ushort *>(unicode), size);
7417 QStringDataPtr dataPtr = { x };
7418 return QString(dataPtr);
7424 Resets the QString to use the first \a size Unicode characters
7425 in the array \a unicode. The data in \a unicode is \e not
7426 copied. The caller must be able to guarantee that \a unicode will
7427 not be deleted or modified as long as the QString (or an
7428 unmodified copy of it) exists.
7430 This function can be used instead of fromRawData() to re-use
7431 existings QString objects to save memory re-allocations.
7435 QString &QString::setRawData(const QChar *unicode, int size)
7437 if (d->ref.isShared() || d->alloc) {
7438 *this = fromRawData(unicode, size);
7442 d->offset = reinterpret_cast<const char *>(unicode) - reinterpret_cast<char *>(d);
7444 d->offset = sizeof(QStringData);
7451 /*! \class QLatin1String
7452 \brief The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal.
7454 \ingroup string-processing
7457 Many of QString's member functions are overloaded to accept
7458 \c{const char *} instead of QString. This includes the copy
7459 constructor, the assignment operator, the comparison operators,
7460 and various other functions such as \l{QString::insert()}{insert()}, \l{QString::replace()}{replace()},
7461 and \l{QString::indexOf()}{indexOf()}. These functions
7462 are usually optimized to avoid constructing a QString object for
7463 the \c{const char *} data. For example, assuming \c str is a
7466 \snippet code/src_corelib_tools_qstring.cpp 3
7470 \snippet code/src_corelib_tools_qstring.cpp 4
7472 because it doesn't construct four temporary QString objects and
7473 make a deep copy of the character data.
7475 Applications that define \c QT_NO_CAST_FROM_ASCII (as explained
7476 in the QString documentation) don't have access to QString's
7477 \c{const char *} API. To provide an efficient way of specifying
7478 constant Latin-1 strings, Qt provides the QLatin1String, which is
7479 just a very thin wrapper around a \c{const char *}. Using
7480 QLatin1String, the example code above becomes
7482 \snippet code/src_corelib_tools_qstring.cpp 5
7484 This is a bit longer to type, but it provides exactly the same
7485 benefits as the first version of the code, and is faster than
7486 converting the Latin-1 strings using QString::fromLatin1().
7488 Thanks to the QString(QLatin1String) constructor,
7489 QLatin1String can be used everywhere a QString is expected. For
7492 \snippet code/src_corelib_tools_qstring.cpp 6
7494 \sa QString, QLatin1Char, QStringLiteral
7497 /*! \fn QLatin1String::QLatin1String(const char *str)
7499 Constructs a QLatin1String object that stores \a str. Note that if
7500 \a str is 0, an empty string is created; this case is handled by
7503 The string data is \e not copied. The caller must be able to
7504 guarantee that \a str will not be deleted or modified as long as
7505 the QLatin1String object exists.
7510 /*! \fn QLatin1String::QLatin1String(const char *str, int size)
7512 Constructs a QLatin1String object that stores \a str with \a size.
7513 Note that if \a str is 0, an empty string is created; this case
7514 is handled by QString.
7516 The string data is \e not copied. The caller must be able to
7517 guarantee that \a str will not be deleted or modified as long as
7518 the QLatin1String object exists.
7523 /*! \fn QLatin1String::QLatin1String(const QByteArray &str)
7525 Constructs a QLatin1String object that stores \a str.
7527 The string data is \e not copied. The caller must be able to
7528 guarantee that \a str will not be deleted or modified as long as
7529 the QLatin1String object exists.
7534 /*! \fn const char *QLatin1String::latin1() const
7536 Returns the Latin-1 string stored in this object.
7539 /*! \fn int QLatin1String::size() const
7541 Returns the size of the Latin-1 string stored in this object.
7544 /*! \fn bool QLatin1String::operator==(const QString &other) const
7546 Returns true if this string is equal to string \a other;
7547 otherwise returns false.
7549 The comparison is based exclusively on the numeric Unicode values
7550 of the characters and is very fast, but is not what a human would
7551 expect. Consider sorting user-interface strings with
7552 QString::localeAwareCompare().
7556 \fn bool QLatin1String::operator==(const char *other) const
7560 The \a other const char pointer is converted to a QString using
7561 the QString::fromUtf8() function.
7563 You can disable this operator by defining \c
7564 QT_NO_CAST_FROM_ASCII when you compile your applications. This
7565 can be useful if you want to ensure that all user-visible strings
7566 go through QObject::tr(), for example.
7569 /*! \fn bool QLatin1String::operator!=(const QString &other) const
7571 Returns true if this string is not equal to string \a other;
7572 otherwise returns false.
7574 The comparison is based exclusively on the numeric Unicode values
7575 of the characters and is very fast, but is not what a human would
7576 expect. Consider sorting user-interface strings with
7577 QString::localeAwareCompare().
7581 \fn bool QLatin1String::operator!=(const char *other) const
7583 \overload operator!=()
7585 The \a other const char pointer is converted to a QString using
7586 the QString::fromUtf8() function.
7588 You can disable this operator by defining \c
7589 QT_NO_CAST_FROM_ASCII when you compile your applications. This
7590 can be useful if you want to ensure that all user-visible strings
7591 go through QObject::tr(), for example.
7595 \fn bool QLatin1String::operator>(const QString &other) const
7597 Returns true if this string is lexically greater than string \a
7598 other; otherwise returns false.
7600 The comparison is based exclusively on the numeric Unicode values
7601 of the characters and is very fast, but is not what a human would
7602 expect. Consider sorting user-interface strings with
7603 QString::localeAwareCompare().
7607 \fn bool QLatin1String::operator>(const char *other) const
7611 The \a other const char pointer is converted to a QString using
7612 the QString::fromUtf8() function.
7614 You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII
7615 when you compile your applications. This can be useful if you want
7616 to ensure that all user-visible strings go through QObject::tr(),
7621 \fn bool QLatin1String::operator<(const QString &other) const
7623 Returns true if this string is lexically less than the \a other
7624 string; otherwise returns false.
7626 The comparison is based exclusively on the numeric Unicode values
7627 of the characters and is very fast, but is not what a human would
7628 expect. Consider sorting user-interface strings using the
7629 QString::localeAwareCompare() function.
7633 \fn bool QLatin1String::operator<(const char *other) const
7637 The \a other const char pointer is converted to a QString using
7638 the QString::fromUtf8() function.
7640 You can disable this operator by defining \c
7641 QT_NO_CAST_FROM_ASCII when you compile your applications. This
7642 can be useful if you want to ensure that all user-visible strings
7643 go through QObject::tr(), for example.
7647 \fn bool QLatin1String::operator>=(const QString &other) const
7649 Returns true if this string is lexically greater than or equal
7650 to string \a other; otherwise returns false.
7652 The comparison is based exclusively on the numeric Unicode values
7653 of the characters and is very fast, but is not what a human would
7654 expect. Consider sorting user-interface strings with
7655 QString::localeAwareCompare().
7659 \fn bool QLatin1String::operator>=(const char *other) const
7663 The \a other const char pointer is converted to a QString using
7664 the QString::fromUtf8() function.
7666 You can disable this operator by defining \c
7667 QT_NO_CAST_FROM_ASCII when you compile your applications. This
7668 can be useful if you want to ensure that all user-visible strings
7669 go through QObject::tr(), for example.
7672 /*! \fn bool QLatin1String::operator<=(const QString &other) const
7674 Returns true if this string is lexically less than or equal
7675 to string \a other; otherwise returns false.
7677 The comparison is based exclusively on the numeric Unicode values
7678 of the characters and is very fast, but is not what a human would
7679 expect. Consider sorting user-interface strings with
7680 QString::localeAwareCompare().
7684 \fn bool QLatin1String::operator<=(const char *other) const
7688 The \a other const char pointer is converted to a QString using
7689 the QString::fromUtf8() function.
7691 You can disable this operator by defining \c
7692 QT_NO_CAST_FROM_ASCII when you compile your applications. This
7693 can be useful if you want to ensure that all user-visible strings
7694 go through QObject::tr(), for example.
7699 /*! \fn bool operator==(QLatin1String s1, QLatin1String s2)
7700 \relates QLatin1String
7702 Returns true if string \a s1 is lexically equal to string \a s2; otherwise
7705 /*! \fn bool operator!=(QLatin1String s1, QLatin1String s2)
7706 \relates QLatin1String
7708 Returns true if string \a s1 is lexically unequal to string \a s2; otherwise
7711 /*! \fn bool operator<(QLatin1String s1, QLatin1String s2)
7712 \relates QLatin1String
7714 Returns true if string \a s1 is lexically smaller than string \a s2; otherwise
7717 /*! \fn bool operator<=(QLatin1String s1, QLatin1String s2)
7718 \relates QLatin1String
7720 Returns true if string \a s1 is lexically smaller than or equal to string \a s2; otherwise
7723 /*! \fn bool operator>(QLatin1String s1, QLatin1String s2)
7724 \relates QLatin1String
7726 Returns true if string \a s1 is lexically greater than string \a s2; otherwise
7729 /*! \fn bool operator>=(QLatin1String s1, QLatin1String s2)
7730 \relates QLatin1String
7732 Returns true if string \a s1 is lexically greater than or equal to
7733 string \a s2; otherwise returns false.
7737 #if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE))
7739 \fn QDataStream &operator<<(QDataStream &stream, const QString &string)
7742 Writes the given \a string to the specified \a stream.
7744 \sa {Serializing Qt Data Types}
7747 QDataStream &operator<<(QDataStream &out, const QString &str)
7749 if (out.version() == 1) {
7750 out << str.toLatin1();
7752 if (!str.isNull() || out.version() < 3) {
7753 if ((out.byteOrder() == QDataStream::BigEndian) == (QSysInfo::ByteOrder == QSysInfo::BigEndian)) {
7754 out.writeBytes(reinterpret_cast<const char *>(str.unicode()), sizeof(QChar) * str.length());
7756 QVarLengthArray<ushort> buffer(str.length());
7757 const ushort *data = reinterpret_cast<const ushort *>(str.constData());
7758 for (int i = 0; i < str.length(); i++) {
7759 buffer[i] = qbswap(*data);
7762 out.writeBytes(reinterpret_cast<const char *>(buffer.data()), sizeof(ushort) * buffer.size());
7765 // write null marker
7766 out << (quint32)0xffffffff;
7773 \fn QDataStream &operator>>(QDataStream &stream, QString &string)
7776 Reads a string from the specified \a stream into the given \a string.
7778 \sa {Serializing Qt Data Types}
7781 QDataStream &operator>>(QDataStream &in, QString &str)
7783 #ifdef QT_QSTRING_UCS_4
7784 #if defined(Q_CC_GNU)
7785 #warning "operator>> not working properly"
7789 if (in.version() == 1) {
7792 str = QString::fromLatin1(l);
7795 in >> bytes; // read size of string
7796 if (bytes == 0xffffffff) { // null string
7798 } else if (bytes > 0) { // not empty
7801 in.setStatus(QDataStream::ReadCorruptData);
7805 const quint32 Step = 1024 * 1024;
7806 quint32 len = bytes / 2;
7807 quint32 allocated = 0;
7809 while (allocated < len) {
7810 int blockSize = qMin(Step, len - allocated);
7811 str.resize(allocated + blockSize);
7812 if (in.readRawData(reinterpret_cast<char *>(str.data()) + allocated * 2,
7813 blockSize * 2) != blockSize * 2) {
7815 in.setStatus(QDataStream::ReadPastEnd);
7818 allocated += blockSize;
7821 if ((in.byteOrder() == QDataStream::BigEndian)
7822 != (QSysInfo::ByteOrder == QSysInfo::BigEndian)) {
7823 ushort *data = reinterpret_cast<ushort *>(str.data());
7825 *data = qbswap(*data);
7830 str = QString(QLatin1String(""));
7835 #endif // QT_NO_DATASTREAM
7843 \brief The QStringRef class provides a thin wrapper around QString substrings.
7846 \ingroup string-processing
7848 QStringRef provides a read-only subset of the QString API.
7850 A string reference explicitly references a portion of a string()
7851 with a given size(), starting at a specific position(). Calling
7852 toString() returns a copy of the data as a real QString instance.
7854 This class is designed to improve the performance of substring
7855 handling when manipulating substrings obtained from existing QString
7856 instances. QStringRef avoids the memory allocation and reference
7857 counting overhead of a standard QString by simply referencing a
7858 part of the original string. This can prove to be advantageous in
7859 low level code, such as that used in a parser, at the expense of
7860 potentially more complex code.
7862 For most users, there are no semantic benefits to using QStringRef
7863 instead of QString since QStringRef requires attention to be paid
7864 to memory management issues, potentially making code more complex
7865 to write and maintain.
7867 \warning A QStringRef is only valid as long as the referenced
7868 string exists. If the original string is deleted, the string
7869 reference points to an invalid memory location.
7871 We suggest that you only use this class in stable code where profiling
7872 has clearly identified that performance improvements can be made by
7873 replacing standard string operations with the optimized substring
7874 handling provided by this class.
7876 \sa {Implicitly Shared Classes}
7881 \fn QStringRef::QStringRef()
7883 Constructs an empty string reference.
7886 /*! \fn QStringRef::QStringRef(const QString *string, int position, int length)
7888 Constructs a string reference to the range of characters in the given
7889 \a string specified by the starting \a position and \a length in characters.
7891 \warning This function exists to improve performance as much as possible,
7892 and performs no bounds checking. For program correctness, \a position and
7893 \a length must describe a valid substring of \a string.
7895 This means that the starting \a position must be positive or 0 and smaller
7896 than \a string's length, and \a length must be positive or 0 but smaller than
7897 the string's length minus the starting \a position;
7898 i.e, 0 <= position < string->length() and
7899 0 <= length <= string->length() - position must both be satisfied.
7902 /*! \fn QStringRef::QStringRef(const QString *string)
7904 Constructs a string reference to the given \a string.
7907 /*! \fn QStringRef::QStringRef(const QStringRef &other)
7909 Constructs a copy of the \a other string reference.
7912 \fn QStringRef::~QStringRef()
7914 Destroys the string reference.
7916 Since this class is only used to refer to string data, and does not take
7917 ownership of it, no memory is freed when instances are destroyed.
7922 \fn int QStringRef::position() const
7924 Returns the starting position in the referenced string that is referred to
7925 by the string reference.
7927 \sa size(), string()
7931 \fn int QStringRef::size() const
7933 Returns the number of characters referred to by the string reference.
7934 Equivalent to length() and count().
7936 \sa position(), string()
7939 \fn int QStringRef::count() const
7940 Returns the number of characters referred to by the string reference.
7941 Equivalent to size() and length().
7943 \sa position(), string()
7946 \fn int QStringRef::length() const
7947 Returns the number of characters referred to by the string reference.
7948 Equivalent to size() and count().
7950 \sa position(), string()
7955 \fn bool QStringRef::isEmpty() const
7957 Returns true if the string reference has no characters; otherwise returns
7960 A string reference is empty if its size is zero.
7966 \fn bool QStringRef::isNull() const
7968 Returns true if string() returns a null pointer or a pointer to a
7969 null string; otherwise returns true.
7975 \fn const QString *QStringRef::string() const
7977 Returns a pointer to the string referred to by the string reference, or
7978 0 if it does not reference a string.
7985 \fn const QChar *QStringRef::unicode() const
7987 Returns a Unicode representation of the string reference. Since
7988 the data stems directly from the referenced string, it is not
7989 null-terminated unless the string reference includes the string's
7996 \fn const QChar *QStringRef::data() const
8002 \fn const QChar *QStringRef::constData() const
8008 Returns a copy of the string reference as a QString object.
8010 If the string reference is not a complete reference of the string
8011 (meaning that position() is 0 and size() equals string()->size()),
8012 this function will allocate a new string to return.
8017 QString QStringRef::toString() const {
8020 if (m_size && m_position == 0 && m_size == m_string->size())
8022 return QString(m_string->unicode() + m_position, m_size);
8026 /*! \relates QStringRef
8028 Returns true if string reference \a s1 is lexically equal to string reference \a s2; otherwise
8031 bool operator==(const QStringRef &s1,const QStringRef &s2)
8032 { return (s1.size() == s2.size() &&
8033 qMemEquals((const ushort *)s1.unicode(), (const ushort *)s2.unicode(), s1.size()));
8036 /*! \relates QStringRef
8038 Returns true if string \a s1 is lexically equal to string reference \a s2; otherwise
8041 bool operator==(const QString &s1,const QStringRef &s2)
8042 { return (s1.size() == s2.size() &&
8043 qMemEquals((const ushort *)s1.unicode(), (const ushort *)s2.unicode(), s1.size()));
8046 /*! \relates QStringRef
8048 Returns true if string \a s1 is lexically equal to string reference \a s2; otherwise
8051 bool operator==(QLatin1String s1, const QStringRef &s2)
8053 if (s1.size() != s2.size())
8056 const ushort *uc = reinterpret_cast<const ushort *>(s2.unicode());
8057 const ushort *e = uc + s2.size();
8058 const uchar *c = reinterpret_cast<const uchar *>(s1.latin1());
8060 return s2.isEmpty();
8063 if (uc == e || *uc != *c)
8074 Returns true if string reference \a s1 is lexically less than
8075 string reference \a s2; otherwise returns false.
8077 The comparison is based exclusively on the numeric Unicode values
8078 of the characters and is very fast, but is not what a human would
8079 expect. Consider sorting user-interface strings using the
8080 QString::localeAwareCompare() function.
8082 bool operator<(const QStringRef &s1,const QStringRef &s2)
8084 return ucstrcmp(s1.constData(), s1.length(), s2.constData(), s2.length()) < 0;
8087 /*!\fn bool operator<=(const QStringRef &s1,const QStringRef &s2)
8091 Returns true if string reference \a s1 is lexically less than
8092 or equal to string reference \a s2; otherwise returns false.
8094 The comparison is based exclusively on the numeric Unicode values
8095 of the characters and is very fast, but is not what a human would
8096 expect. Consider sorting user-interface strings using the
8097 QString::localeAwareCompare() function.
8100 /*!\fn bool operator>=(const QStringRef &s1,const QStringRef &s2)
8104 Returns true if string reference \a s1 is lexically greater than
8105 or equal to string reference \a s2; otherwise returns false.
8107 The comparison is based exclusively on the numeric Unicode values
8108 of the characters and is very fast, but is not what a human would
8109 expect. Consider sorting user-interface strings using the
8110 QString::localeAwareCompare() function.
8113 /*!\fn bool operator>(const QStringRef &s1,const QStringRef &s2)
8117 Returns true if string reference \a s1 is lexically greater than
8118 string reference \a s2; otherwise returns false.
8120 The comparison is based exclusively on the numeric Unicode values
8121 of the characters and is very fast, but is not what a human would
8122 expect. Consider sorting user-interface strings using the
8123 QString::localeAwareCompare() function.
8128 \fn const QChar QStringRef::at(int position) const
8130 Returns the character at the given index \a position in the
8133 The \a position must be a valid index position in the string
8134 (i.e., 0 <= \a position < size()).
8138 \fn void QStringRef::clear()
8140 Clears the contents of the string reference by making it null and empty.
8142 \sa isEmpty(), isNull()
8146 \fn QStringRef &QStringRef::operator=(const QStringRef &other)
8148 Assigns the \a other string reference to this string reference, and
8153 \fn QStringRef &QStringRef::operator=(const QString *string)
8155 Constructs a string reference to the given \a string and assigns it to
8156 this string reference, returning the result.
8160 \typedef QString::DataPtr
8165 \fn DataPtr & QString::data_ptr()
8171 /*! Appends the string reference to \a string, and returns a new
8172 reference to the combined string data.
8174 QStringRef QStringRef::appendTo(QString *string) const
8177 return QStringRef();
8178 int pos = string->size();
8179 string->insert(pos, unicode(), size());
8180 return QStringRef(string, pos, size());
8184 \fn int QStringRef::compare(const QStringRef &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
8187 Compares the string \a s1 with the string \a s2 and returns an
8188 integer less than, equal to, or greater than zero if \a s1
8189 is less than, equal to, or greater than \a s2.
8191 If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
8192 otherwise the comparison is case insensitive.
8196 \fn int QStringRef::compare(const QStringRef &s1, const QStringRef &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
8200 Compares the string \a s1 with the string \a s2 and returns an
8201 integer less than, equal to, or greater than zero if \a s1
8202 is less than, equal to, or greater than \a s2.
8204 If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
8205 otherwise the comparison is case insensitive.
8209 \fn int QStringRef::compare(const QStringRef &s1, QLatin1String s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
8213 Compares the string \a s1 with the string \a s2 and returns an
8214 integer less than, equal to, or greater than zero if \a s1
8215 is less than, equal to, or greater than \a s2.
8217 If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
8218 otherwise the comparison is case insensitive.
8223 \fn int QStringRef::compare(const QString &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
8226 Compares this string with the \a other string and returns an
8227 integer less than, equal to, or greater than zero if this string
8228 is less than, equal to, or greater than the \a other string.
8230 If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
8231 otherwise the comparison is case insensitive.
8233 Equivalent to \c {compare(*this, other, cs)}.
8235 \sa QString::compare()
8240 \fn int QStringRef::compare(const QStringRef &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
8243 Compares this string with the \a other string and returns an
8244 integer less than, equal to, or greater than zero if this string
8245 is less than, equal to, or greater than the \a other string.
8247 If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
8248 otherwise the comparison is case insensitive.
8250 Equivalent to \c {compare(*this, other, cs)}.
8252 \sa QString::compare()
8257 \fn int QStringRef::compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
8260 Compares this string with the \a other string and returns an
8261 integer less than, equal to, or greater than zero if this string
8262 is less than, equal to, or greater than the \a other string.
8264 If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
8265 otherwise the comparison is case insensitive.
8267 Equivalent to \c {compare(*this, other, cs)}.
8269 \sa QString::compare()
8273 \fn int QStringRef::localeAwareCompare(const QStringRef &s1, const QString & s2)
8276 Compares \a s1 with \a s2 and returns an integer less than, equal
8277 to, or greater than zero if \a s1 is less than, equal to, or
8280 The comparison is performed in a locale- and also
8281 platform-dependent manner. Use this function to present sorted
8282 lists of strings to the user.
8284 On Mac OS X, this function compares according the
8285 "Order for sorted lists" setting in the International prefereces panel.
8287 \sa compare(), QTextCodec::locale()
8291 \fn int QStringRef::localeAwareCompare(const QStringRef &s1, const QStringRef & s2)
8295 Compares \a s1 with \a s2 and returns an integer less than, equal
8296 to, or greater than zero if \a s1 is less than, equal to, or
8299 The comparison is performed in a locale- and also
8300 platform-dependent manner. Use this function to present sorted
8301 lists of strings to the user.
8306 \fn int QStringRef::localeAwareCompare(const QString &other) const
8310 Compares this string with the \a other string and returns an
8311 integer less than, equal to, or greater than zero if this string
8312 is less than, equal to, or greater than the \a other string.
8314 The comparison is performed in a locale- and also
8315 platform-dependent manner. Use this function to present sorted
8316 lists of strings to the user.
8320 \fn int QStringRef::localeAwareCompare(const QStringRef &other) const
8324 Compares this string with the \a other string and returns an
8325 integer less than, equal to, or greater than zero if this string
8326 is less than, equal to, or greater than the \a other string.
8328 The comparison is performed in a locale- and also
8329 platform-dependent manner. Use this function to present sorted
8330 lists of strings to the user.
8334 \fn QString &QString::append(const QStringRef &reference)
8337 Appends the given string \a reference to this string and returns the result.
8339 QString &QString::append(const QStringRef &str)
8341 if (str.string() == this) {
8343 } else if (str.string()) {
8344 int oldSize = size();
8345 resize(oldSize + str.size());
8346 memcpy(data() + oldSize, str.unicode(), str.size() * sizeof(QChar));
8354 Returns a substring reference to the \a n leftmost characters
8357 If \a n is greater than size() or less than zero, a reference to the entire
8360 \snippet qstring/main.cpp leftRef
8362 \sa left(), rightRef(), midRef(), startsWith()
8364 QStringRef QString::leftRef(int n) const
8366 if (n >= d->size || n < 0)
8368 return QStringRef(this, 0, n);
8374 Returns a substring reference to the \a n rightmost characters
8377 If \a n is greater than size() or less than zero, a reference to the entire
8380 \snippet qstring/main.cpp rightRef
8382 \sa right(), leftRef(), midRef(), endsWith()
8384 QStringRef QString::rightRef(int n) const
8386 if (n >= d->size || n < 0)
8388 return QStringRef(this, d->size - n, n);
8394 Returns a substring reference to \a n characters of this string,
8395 starting at the specified \a position.
8397 If the \a position exceeds the length of the string, a null
8398 reference is returned.
8400 If there are less than \a n characters available in the string,
8401 starting at the given \a position, or if \a n is -1 (default), the
8402 function returns all characters from the specified \a position
8407 \snippet qstring/main.cpp midRef
8409 \sa mid(), leftRef(), rightRef()
8412 QStringRef QString::midRef(int position, int n) const
8414 if (position > d->size)
8415 return QStringRef();
8417 if (n < 0 || n + position >= d->size)
8418 return QStringRef(this, 0, d->size);
8419 if (n + position <= 0)
8420 return QStringRef();
8424 } else if (n < 0 || n > d->size - position)
8425 n = d->size - position;
8426 return QStringRef(this, position, n);
8432 Returns the index position of the first occurrence of the string \a
8433 str in this string reference, searching forward from index position
8434 \a from. Returns -1 if \a str is not found.
8436 If \a cs is Qt::CaseSensitive (default), the search is case
8437 sensitive; otherwise the search is case insensitive.
8439 If \a from is -1, the search starts at the last character; if it is
8440 -2, at the next to last character and so on.
8442 \sa QString::indexOf(), lastIndexOf(), contains(), count()
8444 int QStringRef::indexOf(const QString &str, int from, Qt::CaseSensitivity cs) const
8446 return qFindString(unicode(), length(), from, str.unicode(), str.length(), cs);
8453 Returns the index position of the first occurrence of the
8454 character \a ch in the string reference, searching forward from
8455 index position \a from. Returns -1 if \a ch could not be found.
8457 \sa QString::indexOf(), lastIndexOf(), contains(), count()
8459 int QStringRef::indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
8461 return findChar(unicode(), length(), ch, from, cs);
8467 Returns the index position of the first occurrence of the string \a
8468 str in this string reference, searching forward from index position
8469 \a from. Returns -1 if \a str is not found.
8471 If \a cs is Qt::CaseSensitive (default), the search is case
8472 sensitive; otherwise the search is case insensitive.
8474 If \a from is -1, the search starts at the last character; if it is
8475 -2, at the next to last character and so on.
8477 \sa QString::indexOf(), lastIndexOf(), contains(), count()
8479 int QStringRef::indexOf(QLatin1String str, int from, Qt::CaseSensitivity cs) const
8481 return qt_find_latin1_string(unicode(), size(), str, from, cs);
8489 Returns the index position of the first occurrence of the string
8490 reference \a str in this string reference, searching forward from
8491 index position \a from. Returns -1 if \a str is not found.
8493 If \a cs is Qt::CaseSensitive (default), the search is case
8494 sensitive; otherwise the search is case insensitive.
8496 \sa QString::indexOf(), lastIndexOf(), contains(), count()
8498 int QStringRef::indexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) const
8500 return qFindString(unicode(), size(), from, str.unicode(), str.size(), cs);
8506 Returns the index position of the last occurrence of the string \a
8507 str in this string reference, searching backward from index position
8508 \a from. If \a from is -1 (default), the search starts at the last
8509 character; if \a from is -2, at the next to last character and so
8510 on. Returns -1 if \a str is not found.
8512 If \a cs is Qt::CaseSensitive (default), the search is case
8513 sensitive; otherwise the search is case insensitive.
8515 \sa QString::lastIndexOf(), indexOf(), contains(), count()
8517 int QStringRef::lastIndexOf(const QString &str, int from, Qt::CaseSensitivity cs) const
8519 const int sl = str.size();
8521 return lastIndexOf(str.at(0), from, cs);
8523 const int l = size();;
8527 if (from == l && sl == 0)
8529 if (from < 0 || from >= l || delta < 0)
8534 return lastIndexOfHelper(reinterpret_cast<const ushort*>(unicode()), from,
8535 reinterpret_cast<const ushort*>(str.unicode()), str.size(), cs);
8540 \overload lastIndexOf()
8542 Returns the index position of the last occurrence of the character
8543 \a ch, searching backward from position \a from.
8545 \sa QString::lastIndexOf(), indexOf(), contains(), count()
8547 int QStringRef::lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
8549 return qt_last_index_of(unicode(), size(), ch, from, cs);
8554 \overload lastIndexOf()
8556 Returns the index position of the last occurrence of the string \a
8557 str in this string reference, searching backward from index position
8558 \a from. If \a from is -1 (default), the search starts at the last
8559 character; if \a from is -2, at the next to last character and so
8560 on. Returns -1 if \a str is not found.
8562 If \a cs is Qt::CaseSensitive (default), the search is case
8563 sensitive; otherwise the search is case insensitive.
8565 \sa QString::lastIndexOf(), indexOf(), contains(), count()
8567 int QStringRef::lastIndexOf(QLatin1String str, int from, Qt::CaseSensitivity cs) const
8569 const int sl = str.size();
8571 return lastIndexOf(QLatin1Char(str.latin1()[0]), from, cs);
8573 const int l = size();
8577 if (from == l && sl == 0)
8579 if (from < 0 || from >= l || delta < 0)
8584 QVarLengthArray<ushort> s(sl);
8585 for (int i = 0; i < sl; ++i)
8586 s[i] = str.latin1()[i];
8588 return lastIndexOfHelper(reinterpret_cast<const ushort*>(unicode()), from, s.data(), sl, cs);
8593 \overload lastIndexOf()
8595 Returns the index position of the last occurrence of the string
8596 reference \a str in this string reference, searching backward from
8597 index position \a from. If \a from is -1 (default), the search
8598 starts at the last character; if \a from is -2, at the next to last
8599 character and so on. Returns -1 if \a str is not found.
8601 If \a cs is Qt::CaseSensitive (default), the search is case
8602 sensitive; otherwise the search is case insensitive.
8604 \sa QString::lastIndexOf(), indexOf(), contains(), count()
8606 int QStringRef::lastIndexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) const
8608 const int sl = str.size();
8610 return lastIndexOf(str.at(0), from, cs);
8612 const int l = size();
8616 if (from == l && sl == 0)
8618 if (from < 0 || from >= l || delta < 0)
8623 return lastIndexOfHelper(reinterpret_cast<const ushort*>(unicode()), from,
8624 reinterpret_cast<const ushort*>(str.unicode()),
8630 Returns the number of (potentially overlapping) occurrences of
8631 the string \a str in this string reference.
8633 If \a cs is Qt::CaseSensitive (default), the search is
8634 case sensitive; otherwise the search is case insensitive.
8636 \sa QString::count(), contains(), indexOf()
8638 int QStringRef::count(const QString &str, Qt::CaseSensitivity cs) const
8640 return qt_string_count(unicode(), size(), str.unicode(), str.size(), cs);
8647 Returns the number of occurrences of the character \a ch in the
8650 If \a cs is Qt::CaseSensitive (default), the search is
8651 case sensitive; otherwise the search is case insensitive.
8653 \sa QString::count(), contains(), indexOf()
8655 int QStringRef::count(QChar ch, Qt::CaseSensitivity cs) const
8657 return qt_string_count(unicode(), size(), ch, cs);
8664 Returns the number of (potentially overlapping) occurrences of the
8665 string reference \a str in this string reference.
8667 If \a cs is Qt::CaseSensitive (default), the search is
8668 case sensitive; otherwise the search is case insensitive.
8670 \sa QString::count(), contains(), indexOf()
8672 int QStringRef::count(const QStringRef &str, Qt::CaseSensitivity cs) const
8674 return qt_string_count(unicode(), size(), str.unicode(), str.size(), cs);
8680 Returns true if the string reference starts with \a str; otherwise
8683 If \a cs is Qt::CaseSensitive (default), the search is
8684 case sensitive; otherwise the search is case insensitive.
8686 \sa QString::startsWith(), endsWith()
8688 bool QStringRef::startsWith(const QString &str, Qt::CaseSensitivity cs) const
8690 return qt_starts_with(isNull() ? 0 : unicode(), size(),
8691 str.isNull() ? 0 : str.unicode(), str.size(), cs);
8696 \overload startsWith()
8697 \sa QString::startsWith(), endsWith()
8699 bool QStringRef::startsWith(QLatin1String str, Qt::CaseSensitivity cs) const
8701 return qt_starts_with(isNull() ? 0 : unicode(), size(), str, cs);
8706 \overload startsWith()
8707 \sa QString::startsWith(), endsWith()
8709 bool QStringRef::startsWith(const QStringRef &str, Qt::CaseSensitivity cs) const
8711 return qt_starts_with(isNull() ? 0 : unicode(), size(),
8712 str.isNull() ? 0 : str.unicode(), str.size(), cs);
8717 \overload startsWith()
8719 Returns true if the string reference starts with \a ch; otherwise
8722 If \a cs is Qt::CaseSensitive (default), the search is case
8723 sensitive; otherwise the search is case insensitive.
8725 \sa QString::startsWith(), endsWith()
8727 bool QStringRef::startsWith(QChar ch, Qt::CaseSensitivity cs) const
8730 const ushort *data = reinterpret_cast<const ushort*>(unicode());
8731 return (cs == Qt::CaseSensitive
8733 : foldCase(data[0]) == foldCase(ch.unicode()));
8741 Returns true if the string reference ends with \a str; otherwise
8744 If \a cs is Qt::CaseSensitive (default), the search is case
8745 sensitive; otherwise the search is case insensitive.
8747 \sa QString::endsWith(), startsWith()
8749 bool QStringRef::endsWith(const QString &str, Qt::CaseSensitivity cs) const
8751 return qt_ends_with(isNull() ? 0 : unicode(), size(),
8752 str.isNull() ? 0 : str.unicode(), str.size(), cs);
8757 \overload endsWith()
8759 Returns true if the string reference ends with \a ch; otherwise
8762 If \a cs is Qt::CaseSensitive (default), the search is case
8763 sensitive; otherwise the search is case insensitive.
8765 \sa QString::endsWith(), endsWith()
8767 bool QStringRef::endsWith(QChar ch, Qt::CaseSensitivity cs) const
8770 const ushort *data = reinterpret_cast<const ushort*>(unicode());
8771 const int size = length();
8772 return (cs == Qt::CaseSensitive
8773 ? data[size - 1] == ch
8774 : foldCase(data[size - 1]) == foldCase(ch.unicode()));
8782 \overload endsWith()
8783 \sa QString::endsWith(), endsWith()
8785 bool QStringRef::endsWith(QLatin1String str, Qt::CaseSensitivity cs) const
8787 return qt_ends_with(isNull() ? 0 : unicode(), size(), str, cs);
8792 \overload endsWith()
8793 \sa QString::endsWith(), endsWith()
8795 bool QStringRef::endsWith(const QStringRef &str, Qt::CaseSensitivity cs) const
8797 return qt_ends_with(isNull() ? 0 : unicode(), size(),
8798 str.isNull() ? 0 : str.unicode(), str.size(), cs);
8802 /*! \fn bool QStringRef::contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
8805 Returns true if this string reference contains an occurrence of
8806 the string \a str; otherwise returns false.
8808 If \a cs is Qt::CaseSensitive (default), the search is
8809 case sensitive; otherwise the search is case insensitive.
8811 \sa indexOf(), count()
8814 /*! \fn bool QStringRef::contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
8816 \overload contains()
8819 Returns true if this string contains an occurrence of the
8820 character \a ch; otherwise returns false.
8822 If \a cs is Qt::CaseSensitive (default), the search is
8823 case sensitive; otherwise the search is case insensitive.
8827 /*! \fn bool QStringRef::contains(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
8828 \overload contains()
8831 Returns true if this string reference contains an occurrence of
8832 the string reference \a str; otherwise returns false.
8834 If \a cs is Qt::CaseSensitive (default), the search is
8835 case sensitive; otherwise the search is case insensitive.
8837 \sa indexOf(), count()
8840 /*! \fn bool QStringRef::contains(QLatin1String str, Qt::CaseSensitivity cs) const
8842 \overload contains()
8844 Returns true if this string reference contains an occurrence of
8845 the string \a str; otherwise returns false.
8847 If \a cs is Qt::CaseSensitive (default), the search is
8848 case sensitive; otherwise the search is case insensitive.
8850 \sa indexOf(), count()
8853 static inline int qt_last_index_of(const QChar *haystack, int haystackLen, QChar needle,
8854 int from, Qt::CaseSensitivity cs)
8856 ushort c = needle.unicode();
8858 from += haystackLen;
8859 if (from < 0 || from >= haystackLen)
8862 const ushort *b = reinterpret_cast<const ushort*>(haystack);
8863 const ushort *n = b + from;
8864 if (cs == Qt::CaseSensitive) {
8871 if (foldCase(*n) == c)
8880 static inline int qt_string_count(const QChar *haystack, int haystackLen,
8881 const QChar *needle, int needleLen,
8882 Qt::CaseSensitivity cs)
8886 if (haystackLen > 500 && needleLen > 5) {
8887 QStringMatcher matcher(needle, needleLen, cs);
8888 while ((i = matcher.indexIn(haystack, haystackLen, i + 1)) != -1)
8891 while ((i = qFindString(haystack, haystackLen, i + 1, needle, needleLen, cs)) != -1)
8897 static inline int qt_string_count(const QChar *unicode, int size, QChar ch,
8898 Qt::CaseSensitivity cs)
8900 ushort c = ch.unicode();
8902 const ushort *b = reinterpret_cast<const ushort*>(unicode);
8903 const ushort *i = b + size;
8904 if (cs == Qt::CaseSensitive) {
8911 if (foldCase(*(--i)) == c)
8917 static inline int qt_find_latin1_string(const QChar *haystack, int size,
8918 QLatin1String needle,
8919 int from, Qt::CaseSensitivity cs)
8921 const char *latin1 = needle.latin1();
8922 int len = needle.size();
8923 QVarLengthArray<ushort> s(len);
8924 for (int i = 0; i < len; ++i)
8927 return qFindString(haystack, size, from,
8928 reinterpret_cast<const QChar*>(s.constData()), len, cs);
8931 static inline bool qt_starts_with(const QChar *haystack, int haystackLen,
8932 const QChar *needle, int needleLen, Qt::CaseSensitivity cs)
8936 if (haystackLen == 0)
8937 return needleLen == 0;
8938 if (needleLen > haystackLen)
8941 const ushort *h = reinterpret_cast<const ushort*>(haystack);
8942 const ushort *n = reinterpret_cast<const ushort*>(needle);
8944 if (cs == Qt::CaseSensitive) {
8945 return qMemEquals(h, n, needleLen);
8949 for (int i = 0; i < needleLen; ++i)
8950 if (foldCase(h[i], last) != foldCase(n[i], olast))
8956 static inline bool qt_starts_with(const QChar *haystack, int haystackLen,
8957 QLatin1String needle, Qt::CaseSensitivity cs)
8960 return !needle.latin1();
8961 if (haystackLen == 0)
8962 return !needle.latin1() || *needle.latin1() == 0;
8963 const int slen = needle.size();
8964 if (slen > haystackLen)
8966 const ushort *data = reinterpret_cast<const ushort*>(haystack);
8967 const uchar *latin = reinterpret_cast<const uchar*>(needle.latin1());
8968 if (cs == Qt::CaseSensitive) {
8969 for (int i = 0; i < slen; ++i)
8970 if (data[i] != latin[i])
8973 for (int i = 0; i < slen; ++i)
8974 if (foldCase(data[i]) != foldCase((ushort)latin[i]))
8980 static inline bool qt_ends_with(const QChar *haystack, int haystackLen,
8981 const QChar *needle, int needleLen, Qt::CaseSensitivity cs)
8985 if (haystackLen == 0)
8986 return needleLen == 0;
8987 const int pos = haystackLen - needleLen;
8991 const ushort *h = reinterpret_cast<const ushort*>(haystack);
8992 const ushort *n = reinterpret_cast<const ushort*>(needle);
8994 if (cs == Qt::CaseSensitive) {
8995 return qMemEquals(h + pos, n, needleLen);
8999 for (int i = 0; i < needleLen; i++)
9000 if (foldCase(h[pos+i], last) != foldCase(n[i], olast))
9007 static inline bool qt_ends_with(const QChar *haystack, int haystackLen,
9008 QLatin1String needle, Qt::CaseSensitivity cs)
9011 return !needle.latin1();
9012 if (haystackLen == 0)
9013 return !needle.latin1() || *needle.latin1() == 0;
9014 const int slen = needle.size();
9015 int pos = haystackLen - slen;
9018 const uchar *latin = reinterpret_cast<const uchar*>(needle.latin1());
9019 const ushort *data = reinterpret_cast<const ushort*>(haystack);
9020 if (cs == Qt::CaseSensitive) {
9021 for (int i = 0; i < slen; i++)
9022 if (data[pos+i] != latin[i])
9025 for (int i = 0; i < slen; i++)
9026 if (foldCase(data[pos+i]) != foldCase((ushort)latin[i]))
9035 Returns a Latin-1 representation of the string as a QByteArray.
9037 The returned byte array is undefined if the string contains non-Latin1
9038 characters. Those characters may be suppressed or replaced with a
9041 \sa toUtf8(), toLocal8Bit(), QTextCodec
9043 QByteArray QStringRef::toLatin1() const
9045 return toLatin1_helper(unicode(), length());
9049 \fn QByteArray QStringRef::toAscii() const
9053 Returns an 8-bit representation of the string as a QByteArray.
9055 This function does the same as toLatin1().
9057 Note that, despite the name, this function does not necessarily return an US-ASCII
9058 (ANSI X3.4-1986) string and its result may not be US-ASCII compatible.
9060 \sa toLatin1(), toUtf8(), toLocal8Bit(), QTextCodec
9066 Returns the local 8-bit representation of the string as a
9067 QByteArray. The returned byte array is undefined if the string
9068 contains characters not supported by the local 8-bit encoding.
9070 QTextCodec::codecForLocale() is used to perform the conversion from
9071 Unicode. If the locale encoding could not be determined, this function
9072 does the same as toLatin1().
9074 If this string contains any characters that cannot be encoded in the
9075 locale, the returned byte array is undefined. Those characters may be
9076 suppressed or replaced by another.
9078 \sa toLatin1(), toUtf8(), QTextCodec
9080 QByteArray QStringRef::toLocal8Bit() const
9082 #ifndef QT_NO_TEXTCODEC
9083 if (QTextCodec::codecForLocale())
9084 return QTextCodec::codecForLocale()->fromUnicode(unicode(), length());
9085 #endif // QT_NO_TEXTCODEC
9092 Returns a UTF-8 representation of the string as a QByteArray.
9094 UTF-8 is a Unicode codec and can represent all characters in a Unicode
9095 string like QString.
9097 However, in the Unicode range, there are certain codepoints that are not
9098 considered characters. The Unicode standard reserves the last two
9099 codepoints in each Unicode Plane (U+FFFE, U+FFFF, U+1FFFE, U+1FFFF,
9100 U+2FFFE, etc.), as well as 16 codepoints in the range U+FDD0..U+FDDF,
9101 inclusive, as non-characters. If any of those appear in the string, they
9102 may be discarded and will not appear in the UTF-8 representation, or they
9103 may be replaced by one or more replacement characters.
9105 \sa toLatin1(), toLocal8Bit(), QTextCodec
9107 QByteArray QStringRef::toUtf8() const
9110 return QByteArray();
9112 return QUtf8::convertFromUnicode(constData(), length(), 0);
9118 Returns a UCS-4/UTF-32 representation of the string as a QVector<uint>.
9120 UCS-4 is a Unicode codec and is lossless. All characters from this string
9121 can be encoded in UCS-4.
9123 \sa toUtf8(), toLatin1(), toLocal8Bit(), QTextCodec
9125 QVector<uint> QStringRef::toUcs4() const
9127 QVector<uint> v(length());
9129 int len = QString::toUcs4_helper(reinterpret_cast<const ushort *>(unicode()), length(), a);
9137 \fn QString Qt::escape(const QString &plain)
9139 \sa QString::toHtmlEscaped()
9143 Converts the plain text string \a plain to a HTML string with
9144 HTML metacharacters \c{<}, \c{>}, \c{&}, and \c{"} replaced by HTML
9149 \snippet code/src_corelib_tools_qstring.cpp 7
9151 QString QString::toHtmlEscaped() const
9154 const int len = length();
9155 rich.reserve(int(len * 1.1));
9156 for (int i = 0; i < len; ++i) {
9157 if (at(i) == QLatin1Char('<'))
9158 rich += QLatin1String("<");
9159 else if (at(i) == QLatin1Char('>'))
9160 rich += QLatin1String(">");
9161 else if (at(i) == QLatin1Char('&'))
9162 rich += QLatin1String("&");
9163 else if (at(i) == QLatin1Char('"'))
9164 rich += QLatin1String(""");
9173 \macro QStringLiteral(str)
9176 The macro generates the data for a QString out of \a str at compile time if the compiler supports it.
9177 Creating a QString from it is free in this case, and the generated string data is stored in
9178 the read-only segment of the compiled object file.
9180 For compilers not supporting the creation of compile time strings, QStringLiteral will fall back to
9183 The result of the QStringLiteral expression can be cast into a QString.
9185 If you have code looking like:
9187 if (node.hasAttribute("http-contents-length")) //...
9189 One temporary QString will be created to be passed as the hasAttribute function parameter.
9190 This can be quite expensive, as it involves a memory allocation and the copy and the conversion
9191 of the data into QString's internal encoding.
9193 This can be avoided by doing
9195 if (node.hasAttribute(QStringLiteral("http-contents-length"))) //...
9197 Then the QString's internal data will be generated at compile time and no conversion or allocation
9198 will occur at runtime
9200 Using QStringLiteral instead of a double quoted ascii literal can significantly speed up creation
9201 of QString's from data known at compile time.
9203 If the compiler is C++11 enabled the string \a str can actually contain unicode data.
9205 \note There are still a few cases in which QLatin1String is more efficient than QStringLiteral:
9206 If it is passed to a function that has an overload that takes the QLatin1String directly, without
9207 conversion to QString. For instance, this is the case of QString::operator==
9209 if (attribute.name() == QLatin1String("http-contents-length")) //...