1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtCore module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qstringlist.h"
|
---|
43 | #include "qregexp.h"
|
---|
44 | #include "qunicodetables_p.h"
|
---|
45 | #ifndef QT_NO_TEXTCODEC
|
---|
46 | #include <qtextcodec.h>
|
---|
47 | #endif
|
---|
48 | #include <private/qutfcodec_p.h>
|
---|
49 | #include <qdatastream.h>
|
---|
50 | #include <qlist.h>
|
---|
51 | #include "qlocale.h"
|
---|
52 | #include "qlocale_p.h"
|
---|
53 | #include "qstringmatcher.h"
|
---|
54 | #include "qvarlengtharray.h"
|
---|
55 | #include "qtools_p.h"
|
---|
56 | #include "qhash.h"
|
---|
57 | #include "qdebug.h"
|
---|
58 |
|
---|
59 | #ifdef Q_OS_MAC
|
---|
60 | #include <private/qcore_mac_p.h>
|
---|
61 | #endif
|
---|
62 |
|
---|
63 | #ifdef Q_OS_OS2
|
---|
64 | #include "qatomic.h"
|
---|
65 | #include <unidef.h>
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | #include <private/qfunctions_p.h>
|
---|
69 |
|
---|
70 | #if defined(Q_OS_WINCE)
|
---|
71 | #include <windows.h>
|
---|
72 | #include <winnls.h>
|
---|
73 | #endif
|
---|
74 |
|
---|
75 | #include <limits.h>
|
---|
76 | #include <string.h>
|
---|
77 | #include <stdlib.h>
|
---|
78 | #include <stdio.h>
|
---|
79 | #include <stdarg.h>
|
---|
80 |
|
---|
81 | #ifdef truncate
|
---|
82 | #undef truncate
|
---|
83 | #endif
|
---|
84 |
|
---|
85 | #include "qchar.cpp"
|
---|
86 | #include "qstringmatcher.cpp"
|
---|
87 |
|
---|
88 | #ifndef LLONG_MAX
|
---|
89 | #define LLONG_MAX qint64_C(9223372036854775807)
|
---|
90 | #endif
|
---|
91 | #ifndef LLONG_MIN
|
---|
92 | #define LLONG_MIN (-LLONG_MAX - qint64_C(1))
|
---|
93 | #endif
|
---|
94 | #ifndef ULLONG_MAX
|
---|
95 | #define ULLONG_MAX quint64_C(18446744073709551615)
|
---|
96 | #endif
|
---|
97 |
|
---|
98 | QT_BEGIN_NAMESPACE
|
---|
99 |
|
---|
100 | #ifndef QT_NO_TEXTCODEC
|
---|
101 | QTextCodec *QString::codecForCStrings;
|
---|
102 | #endif
|
---|
103 |
|
---|
104 | #ifdef QT3_SUPPORT
|
---|
105 | static QHash<void *, QByteArray> *asciiCache = 0;
|
---|
106 | #endif
|
---|
107 |
|
---|
108 | // internal
|
---|
109 | int qFindString(const QChar *haystack, int haystackLen, int from,
|
---|
110 | const QChar *needle, int needleLen, Qt::CaseSensitivity cs);
|
---|
111 | int qFindStringBoyerMoore(const QChar *haystack, int haystackLen, int from,
|
---|
112 | const QChar *needle, int needleLen, Qt::CaseSensitivity cs);
|
---|
113 |
|
---|
114 |
|
---|
115 | // Unicode case-insensitive comparison
|
---|
116 | static int ucstricmp(const ushort *a, const ushort *ae, const ushort *b, const ushort *be)
|
---|
117 | {
|
---|
118 | if (a == b)
|
---|
119 | return 0;
|
---|
120 | if (a == 0)
|
---|
121 | return 1;
|
---|
122 | if (b == 0)
|
---|
123 | return -1;
|
---|
124 |
|
---|
125 | const ushort *e = ae;
|
---|
126 | if (be - b < ae - a)
|
---|
127 | e = a + (be - b);
|
---|
128 |
|
---|
129 | uint alast = 0;
|
---|
130 | uint blast = 0;
|
---|
131 | while (a != e) {
|
---|
132 | // qDebug() << hex << alast << blast;
|
---|
133 | // qDebug() << hex << "*a=" << *a << "alast=" << alast << "folded=" << foldCase (*a, alast);
|
---|
134 | // qDebug() << hex << "*b=" << *b << "blast=" << blast << "folded=" << foldCase (*b, blast);
|
---|
135 | int diff = foldCase(*a, alast) - foldCase(*b, blast);
|
---|
136 | if ((diff))
|
---|
137 | return diff;
|
---|
138 | ++a;
|
---|
139 | ++b;
|
---|
140 | }
|
---|
141 | if (a == ae) {
|
---|
142 | if (b == be)
|
---|
143 | return 0;
|
---|
144 | return -1;
|
---|
145 | }
|
---|
146 | return 1;
|
---|
147 | }
|
---|
148 |
|
---|
149 | // Case-insensitive comparison between a Unicode string and a QLatin1String
|
---|
150 | static int ucstricmp(const ushort *a, const ushort *ae, const uchar *b)
|
---|
151 | {
|
---|
152 | if (a == 0) {
|
---|
153 | if (b == 0)
|
---|
154 | return 0;
|
---|
155 | return 1;
|
---|
156 | }
|
---|
157 | if (b == 0)
|
---|
158 | return -1;
|
---|
159 |
|
---|
160 | while (a != ae && *b) {
|
---|
161 | int diff = foldCase(*a) - foldCase(*b);
|
---|
162 | if ((diff))
|
---|
163 | return diff;
|
---|
164 | ++a;
|
---|
165 | ++b;
|
---|
166 | }
|
---|
167 | if (a == ae) {
|
---|
168 | if (!*b)
|
---|
169 | return 0;
|
---|
170 | return -1;
|
---|
171 | }
|
---|
172 | return 1;
|
---|
173 | }
|
---|
174 |
|
---|
175 | // Unicode case-insensitive comparison
|
---|
176 | static int ucstrcmp(const QChar *a, int alen, const QChar *b, int blen)
|
---|
177 | {
|
---|
178 | if (a == b && alen == blen)
|
---|
179 | return 0;
|
---|
180 | int l = qMin(alen, blen);
|
---|
181 | while (l-- && *a == *b)
|
---|
182 | a++,b++;
|
---|
183 | if (l == -1)
|
---|
184 | return (alen-blen);
|
---|
185 | return a->unicode() - b->unicode();
|
---|
186 | }
|
---|
187 |
|
---|
188 | // Unicode case-sensitive compare two same-sized strings
|
---|
189 | static int ucstrncmp(const QChar *a, const QChar *b, int l)
|
---|
190 | {
|
---|
191 | while (l-- && *a == *b)
|
---|
192 | a++,b++;
|
---|
193 | if (l==-1)
|
---|
194 | return 0;
|
---|
195 | return a->unicode() - b->unicode();
|
---|
196 | }
|
---|
197 |
|
---|
198 | // Unicode case-insensitive compare two same-sized strings
|
---|
199 | static int ucstrnicmp(const ushort *a, const ushort *b, int l)
|
---|
200 | {
|
---|
201 | return ucstricmp(a, a + l, b, b + l);
|
---|
202 | }
|
---|
203 |
|
---|
204 | static bool qMemEquals(const quint16 *a, const quint16 *b, int length)
|
---|
205 | {
|
---|
206 | // Benchmarking indicates that doing memcmp is much slower than
|
---|
207 | // executing the comparison ourselves.
|
---|
208 | // To make it even faster, we do a 32-bit comparison, comparing
|
---|
209 | // twice the amount of data as a normal word-by-word comparison.
|
---|
210 | //
|
---|
211 | // Benchmarking results on a 2.33 GHz Core2 Duo, with a 64-QChar
|
---|
212 | // block of data, with 4194304 iterations (per iteration):
|
---|
213 | // operation usec cpu ticks
|
---|
214 | // memcmp 330 710
|
---|
215 | // 16-bit 79 167-171
|
---|
216 | // 32-bit aligned 49 105-109
|
---|
217 | //
|
---|
218 | // Testing also indicates that unaligned 32-bit loads are as
|
---|
219 | // performant as 32-bit aligned.
|
---|
220 | if (a == b || !length)
|
---|
221 | return true;
|
---|
222 |
|
---|
223 | register union {
|
---|
224 | const quint16 *w;
|
---|
225 | const quint32 *d;
|
---|
226 | quintptr value;
|
---|
227 | } sa, sb;
|
---|
228 | sa.w = a;
|
---|
229 | sb.w = b;
|
---|
230 |
|
---|
231 | // check alignment
|
---|
232 | if ((sa.value & 2) == (sb.value & 2)) {
|
---|
233 | // both addresses have the same alignment
|
---|
234 | if (sa.value & 2) {
|
---|
235 | // both addresses are not aligned to 4-bytes boundaries
|
---|
236 | // compare the first character
|
---|
237 | if (*sa.w != *sb.w)
|
---|
238 | return false;
|
---|
239 | --length;
|
---|
240 | ++sa.w;
|
---|
241 | ++sb.w;
|
---|
242 |
|
---|
243 | // now both addresses are 4-bytes aligned
|
---|
244 | }
|
---|
245 |
|
---|
246 | // both addresses are 4-bytes aligned
|
---|
247 | // do a fast 32-bit comparison
|
---|
248 | register const quint32 *e = sa.d + (length >> 1);
|
---|
249 | for ( ; sa.d != e; ++sa.d, ++sb.d) {
|
---|
250 | if (*sa.d != *sb.d)
|
---|
251 | return false;
|
---|
252 | }
|
---|
253 |
|
---|
254 | // do we have a tail?
|
---|
255 | return (length & 1) ? *sa.w == *sb.w : true;
|
---|
256 | } else {
|
---|
257 | // one of the addresses isn't 4-byte aligned but the other is
|
---|
258 | register const quint16 *e = sa.w + length;
|
---|
259 | for ( ; sa.w != e; ++sa.w, ++sb.w) {
|
---|
260 | if (*sa.w != *sb.w)
|
---|
261 | return false;
|
---|
262 | }
|
---|
263 | }
|
---|
264 | return true;
|
---|
265 | }
|
---|
266 |
|
---|
267 | /*!
|
---|
268 | \internal
|
---|
269 |
|
---|
270 | Returns the index position of the first occurrence of the
|
---|
271 | character \a ch in the string given by \a str and \a len,
|
---|
272 | searching forward from index
|
---|
273 | position \a from. Returns -1 if \a ch could not be found.
|
---|
274 | */
|
---|
275 | static int findChar(const QChar *str, int len, QChar ch, int from,
|
---|
276 | Qt::CaseSensitivity cs)
|
---|
277 | {
|
---|
278 | const ushort *s = (const ushort *)str;
|
---|
279 | ushort c = ch.unicode();
|
---|
280 | if (from < 0)
|
---|
281 | from = qMax(from + len, 0);
|
---|
282 | if (from < len) {
|
---|
283 | const ushort *n = s + from - 1;
|
---|
284 | const ushort *e = s + len;
|
---|
285 | if (cs == Qt::CaseSensitive) {
|
---|
286 | while (++n != e)
|
---|
287 | if (*n == c)
|
---|
288 | return n - s;
|
---|
289 | } else {
|
---|
290 | c = foldCase(c);
|
---|
291 | while (++n != e)
|
---|
292 | if (foldCase(*n) == c)
|
---|
293 | return n - s;
|
---|
294 | }
|
---|
295 | }
|
---|
296 | return -1;
|
---|
297 | }
|
---|
298 |
|
---|
299 | #define REHASH(a) \
|
---|
300 | if (sl_minus_1 < (int)sizeof(int) * CHAR_BIT) \
|
---|
301 | hashHaystack -= (a) << sl_minus_1; \
|
---|
302 | hashHaystack <<= 1
|
---|
303 |
|
---|
304 | inline bool qIsUpper(char ch)
|
---|
305 | {
|
---|
306 | return ch >= 'A' && ch <= 'Z';
|
---|
307 | }
|
---|
308 |
|
---|
309 | inline bool qIsDigit(char ch)
|
---|
310 | {
|
---|
311 | return ch >= '0' && ch <= '9';
|
---|
312 | }
|
---|
313 |
|
---|
314 | inline char qToLower(char ch)
|
---|
315 | {
|
---|
316 | if (ch >= 'A' && ch <= 'Z')
|
---|
317 | return ch - 'A' + 'a';
|
---|
318 | else
|
---|
319 | return ch;
|
---|
320 | }
|
---|
321 |
|
---|
322 | #if defined(Q_CC_MSVC) && _MSC_VER <= 1300
|
---|
323 | const QString::Null QString::null;
|
---|
324 | #else
|
---|
325 | const QString::Null QString::null = { };
|
---|
326 | #endif
|
---|
327 |
|
---|
328 | /*!
|
---|
329 | \macro QT_NO_CAST_FROM_ASCII
|
---|
330 | \relates QString
|
---|
331 |
|
---|
332 | Disables automatic conversions from 8-bit strings (char *) to unicode QStrings
|
---|
333 |
|
---|
334 | \sa QT_NO_CAST_TO_ASCII, QT_NO_CAST_FROM_BYTEARRAY
|
---|
335 | */
|
---|
336 |
|
---|
337 | /*!
|
---|
338 | \macro QT_NO_CAST_TO_ASCII
|
---|
339 | \relates QString
|
---|
340 |
|
---|
341 | disables automatic conversion from QString to ASCII 8-bit strings (char *)
|
---|
342 |
|
---|
343 | \sa QT_NO_CAST_FROM_ASCII, QT_NO_CAST_FROM_BYTEARRAY
|
---|
344 | */
|
---|
345 |
|
---|
346 | /*!
|
---|
347 | \macro QT_ASCII_CAST_WARNINGS
|
---|
348 | \internal
|
---|
349 | \relates QString
|
---|
350 |
|
---|
351 | This macro can be defined to force a warning whenever a function is
|
---|
352 | called that automatically converts between unicode and 8-bit encodings.
|
---|
353 |
|
---|
354 | Note: This only works for compilers that support warnings for
|
---|
355 | deprecated API.
|
---|
356 |
|
---|
357 | \sa QT_NO_CAST_TO_ASCII, QT_NO_CAST_FROM_ASCII
|
---|
358 | */
|
---|
359 |
|
---|
360 | /*!
|
---|
361 | \class QCharRef
|
---|
362 | \reentrant
|
---|
363 | \brief The QCharRef class is a helper class for QString.
|
---|
364 |
|
---|
365 | \internal
|
---|
366 |
|
---|
367 | \ingroup string-processing
|
---|
368 |
|
---|
369 | When you get an object of type QCharRef, if you can assign to it,
|
---|
370 | the assignment will apply to the character in the string from
|
---|
371 | which you got the reference. That is its whole purpose in life.
|
---|
372 | The QCharRef becomes invalid once modifications are made to the
|
---|
373 | string: if you want to keep the character, copy it into a QChar.
|
---|
374 |
|
---|
375 | Most of the QChar member functions also exist in QCharRef.
|
---|
376 | However, they are not explicitly documented here.
|
---|
377 |
|
---|
378 | \sa QString::operator[]() QString::at() QChar
|
---|
379 | */
|
---|
380 |
|
---|
381 | /*!
|
---|
382 | \class QString
|
---|
383 | \reentrant
|
---|
384 |
|
---|
385 | \brief The QString class provides a Unicode character string.
|
---|
386 |
|
---|
387 | \ingroup tools
|
---|
388 | \ingroup shared
|
---|
389 | \ingroup string-processing
|
---|
390 |
|
---|
391 |
|
---|
392 | QString stores a string of 16-bit \l{QChar}s, where each QChar
|
---|
393 | corresponds one Unicode 4.0 character. (Unicode characters
|
---|
394 | with code values above 65535 are stored using surrogate pairs,
|
---|
395 | i.e., two consecutive \l{QChar}s.)
|
---|
396 |
|
---|
397 | \l{Unicode} is an international standard that supports most of
|
---|
398 | the writing systems in use today. It is a superset of ASCII and
|
---|
399 | Latin-1 (ISO 8859-1), and all the ASCII/Latin-1 characters are
|
---|
400 | available at the same code positions.
|
---|
401 |
|
---|
402 | Behind the scenes, QString uses \l{implicit sharing}
|
---|
403 | (copy-on-write) to reduce memory usage and to avoid the needless
|
---|
404 | copying of data. This also helps reduce the inherent overhead of
|
---|
405 | storing 16-bit characters instead of 8-bit characters.
|
---|
406 |
|
---|
407 | In addition to QString, Qt also provides the QByteArray class to
|
---|
408 | store raw bytes and traditional 8-bit '\\0'-terminated strings.
|
---|
409 | For most purposes, QString is the class you want to use. It is
|
---|
410 | used throughout the Qt API, and the Unicode support ensures that
|
---|
411 | your applications will be easy to translate if you want to expand
|
---|
412 | your application's market at some point. The two main cases where
|
---|
413 | QByteArray is appropriate are when you need to store raw binary
|
---|
414 | data, and when memory conservation is critical (e.g., with
|
---|
415 | \l{Qt for Embedded Linux}).
|
---|
416 |
|
---|
417 | \tableofcontents
|
---|
418 |
|
---|
419 | \section1 Initializing a String
|
---|
420 |
|
---|
421 | One way to initialize a QString is simply to pass a \c{const char
|
---|
422 | *} to its constructor. For example, the following code creates a
|
---|
423 | QString of size 5 containing the data "Hello":
|
---|
424 |
|
---|
425 | \snippet doc/src/snippets/qstring/main.cpp 0
|
---|
426 |
|
---|
427 | QString converts the \c{const char *} data into Unicode using the
|
---|
428 | fromAscii() function. By default, fromAscii() treats character
|
---|
429 | above 128 as Latin-1 characters, but this can be changed by
|
---|
430 | calling QTextCodec::setCodecForCStrings().
|
---|
431 |
|
---|
432 | In all of the QString functions that take \c{const char *}
|
---|
433 | parameters, the \c{const char *} is interpreted as a classic
|
---|
434 | C-style '\\0'-terminated string. It is legal for the \c{const char
|
---|
435 | *} parameter to be 0.
|
---|
436 |
|
---|
437 | You can also provide string data as an array of \l{QChar}s:
|
---|
438 |
|
---|
439 | \snippet doc/src/snippets/qstring/main.cpp 1
|
---|
440 |
|
---|
441 | QString makes a deep copy of the QChar data, so you can modify it
|
---|
442 | later without experiencing side effects. (If for performance
|
---|
443 | reasons you don't want to take a deep copy of the character data,
|
---|
444 | use QString::fromRawData() instead.)
|
---|
445 |
|
---|
446 | Another approach is to set the size of the string using resize()
|
---|
447 | and to initialize the data character per character. QString uses
|
---|
448 | 0-based indexes, just like C++ arrays. To access the character at
|
---|
449 | a particular index position, you can use \l operator[](). On
|
---|
450 | non-const strings, \l operator[]() returns a reference to a
|
---|
451 | character that can be used on the left side of an assignment. For
|
---|
452 | example:
|
---|
453 |
|
---|
454 | \snippet doc/src/snippets/qstring/main.cpp 2
|
---|
455 |
|
---|
456 | For read-only access, an alternative syntax is to use the at()
|
---|
457 | function:
|
---|
458 |
|
---|
459 | \snippet doc/src/snippets/qstring/main.cpp 3
|
---|
460 |
|
---|
461 | The at() function can be faster than \l operator[](), because it
|
---|
462 | never causes a \l{deep copy} to occur. Alternatively, use the
|
---|
463 | left(), right(), or mid() functions to extract several characters
|
---|
464 | at a time.
|
---|
465 |
|
---|
466 | A QString can embed '\\0' characters (QChar::Null). The size()
|
---|
467 | function always returns the size of the whole string, including
|
---|
468 | embedded '\\0' characters.
|
---|
469 |
|
---|
470 | After a call to the resize() function, newly allocated characters
|
---|
471 | have undefined values. To set all the characters in the string to
|
---|
472 | a particular value, use the fill() function.
|
---|
473 |
|
---|
474 | QString provides dozens of overloads designed to simplify string
|
---|
475 | usage. For example, if you want to compare a QString with a string
|
---|
476 | literal, you can write code like this and it will work as expected:
|
---|
477 |
|
---|
478 | \snippet doc/src/snippets/qstring/main.cpp 4
|
---|
479 |
|
---|
480 | You can also pass string literals to functions that take QStrings
|
---|
481 | as arguments, invoking the QString(const char *)
|
---|
482 | constructor. Similarly, you can pass a QString to a function that
|
---|
483 | takes a \c{const char *} argument using the \l qPrintable() macro
|
---|
484 | which returns the given QString as a \c{const char *}. This is
|
---|
485 | equivalent to calling <QString>.toLocal8Bit().constData().
|
---|
486 |
|
---|
487 | \section1 Manipulating String Data
|
---|
488 |
|
---|
489 | QString provides the following basic functions for modifying the
|
---|
490 | character data: append(), prepend(), insert(), replace(), and
|
---|
491 | remove(). For example:
|
---|
492 |
|
---|
493 | \snippet doc/src/snippets/qstring/main.cpp 5
|
---|
494 |
|
---|
495 | If you are building a QString gradually and know in advance
|
---|
496 | approximately how many characters the QString will contain, you
|
---|
497 | can call reserve(), asking QString to preallocate a certain amount
|
---|
498 | of memory. You can also call capacity() to find out how much
|
---|
499 | memory QString actually allocated.
|
---|
500 |
|
---|
501 | The replace() and remove() functions' first two arguments are the
|
---|
502 | position from which to start erasing and the number of characters
|
---|
503 | that should be erased. If you want to replace all occurrences of
|
---|
504 | a particular substring with another, use one of the two-parameter
|
---|
505 | replace() overloads.
|
---|
506 |
|
---|
507 | A frequent requirement is to remove whitespace characters from a
|
---|
508 | string ('\\n', '\\t', ' ', etc.). If you want to remove whitespace
|
---|
509 | from both ends of a QString, use the trimmed() function. If you
|
---|
510 | want to remove whitespace from both ends and replace multiple
|
---|
511 | consecutive whitespaces with a single space character within the
|
---|
512 | string, use simplified().
|
---|
513 |
|
---|
514 | If you want to find all occurrences of a particular character or
|
---|
515 | substring in a QString, use the indexOf() or lastIndexOf()
|
---|
516 | functions. The former searches forward starting from a given index
|
---|
517 | position, the latter searches backward. Both return the index
|
---|
518 | position of the character or substring if they find it; otherwise,
|
---|
519 | they return -1. For example, here's a typical loop that finds all
|
---|
520 | occurrences of a particular substring:
|
---|
521 |
|
---|
522 | \snippet doc/src/snippets/qstring/main.cpp 6
|
---|
523 |
|
---|
524 | QString provides many functions for converting numbers into
|
---|
525 | strings and strings into numbers. See the arg() functions, the
|
---|
526 | setNum() functions, the number() static functions, and the
|
---|
527 | toInt(), toDouble(), and similar functions.
|
---|
528 |
|
---|
529 | To get an upper- or lowercase version of a string use toUpper() or
|
---|
530 | toLower().
|
---|
531 |
|
---|
532 | Lists of strings are handled by the QStringList class. You can
|
---|
533 | split a string into a list of strings using the split() function,
|
---|
534 | and join a list of strings into a single string with an optional
|
---|
535 | separator using QStringList::join(). You can obtain a list of
|
---|
536 | strings from a string list that contain a particular substring or
|
---|
537 | that match a particular QRegExp using the QStringList::find()
|
---|
538 | function.
|
---|
539 | :
|
---|
540 | \section1 Querying String Data
|
---|
541 |
|
---|
542 | If you want to see if a QString starts or ends with a particular
|
---|
543 | substring use startsWith() or endsWith(). If you simply want to
|
---|
544 | check whether a QString contains a particular character or
|
---|
545 | substring, use the contains() function. If you want to find out
|
---|
546 | how many times a particular character or substring occurs in the
|
---|
547 | string, use count().
|
---|
548 |
|
---|
549 | QStrings can be compared using overloaded operators such as \l
|
---|
550 | operator<(), \l operator<=(), \l operator==(), \l operator>=(),
|
---|
551 | and so on. Note that the comparison is based exclusively on the
|
---|
552 | numeric Unicode values of the characters. It is very fast, but is
|
---|
553 | not what a human would expect; the QString::localeAwareCompare()
|
---|
554 | function is a better choice for sorting user-interface strings.
|
---|
555 |
|
---|
556 | To obtain a pointer to the actual character data, call data() or
|
---|
557 | constData(). These functions return a pointer to the beginning of
|
---|
558 | the QChar data. The pointer is guaranteed to remain valid until a
|
---|
559 | non-const function is called on the QString.
|
---|
560 |
|
---|
561 | \section1 Converting Between 8-Bit Strings and Unicode Strings
|
---|
562 |
|
---|
563 | QString provides the following four functions that return a
|
---|
564 | \c{const char *} version of the string as QByteArray: toAscii(),
|
---|
565 | toLatin1(), toUtf8(), and toLocal8Bit().
|
---|
566 |
|
---|
567 | \list
|
---|
568 | \o toAscii() returns an ASCII encoded 8-bit string.
|
---|
569 | \o toLatin1() returns a Latin-1 (ISO 8859-1) encoded 8-bit string.
|
---|
570 | \o toUtf8() returns a UTF-8 encoded 8-bit string. UTF-8 is a
|
---|
571 | superset of ASCII that supports the entire Unicode character
|
---|
572 | set through multibyte sequences.
|
---|
573 | \o toLocal8Bit() returns an 8-bit string using the system's local
|
---|
574 | encoding.
|
---|
575 | \endlist
|
---|
576 |
|
---|
577 | To convert from one of these encodings, QString provides
|
---|
578 | fromAscii(), fromLatin1(), fromUtf8(), and fromLocal8Bit(). Other
|
---|
579 | encodings are supported through the QTextCodec class.
|
---|
580 |
|
---|
581 | As mentioned above, QString provides a lot of functions and
|
---|
582 | operators that make it easy to interoperate with \c{const char *}
|
---|
583 | strings. But this functionality is a double-edged sword: It makes
|
---|
584 | QString more convenient to use if all strings are ASCII or
|
---|
585 | Latin-1, but there is always the risk that an implicit conversion
|
---|
586 | from or to \c{const char *} is done using the wrong 8-bit
|
---|
587 | encoding. To minimize these risks, you can turn off these implicit
|
---|
588 | conversions by defining the following two preprocessor symbols:
|
---|
589 |
|
---|
590 | \list
|
---|
591 | \o \c QT_NO_CAST_FROM_ASCII disables automatic conversions from
|
---|
592 | ASCII to Unicode.
|
---|
593 | \o \c QT_NO_CAST_TO_ASCII disables automatic conversion from QString
|
---|
594 | to ASCII.
|
---|
595 | \endlist
|
---|
596 |
|
---|
597 | One way to define these preprocessor symbols globally for your
|
---|
598 | application is to add the following entry to your
|
---|
599 | \l{qmake Project Files}{qmake project file}:
|
---|
600 |
|
---|
601 | \snippet doc/src/snippets/code/src_corelib_tools_qstring.cpp 0
|
---|
602 |
|
---|
603 | You then need to explicitly call fromAscii(), fromLatin1(),
|
---|
604 | fromUtf8(), or fromLocal8Bit() to construct a QString from an
|
---|
605 | 8-bit string, or use the lightweight QLatin1String class, for
|
---|
606 | example:
|
---|
607 |
|
---|
608 | \snippet doc/src/snippets/code/src_corelib_tools_qstring.cpp 1
|
---|
609 |
|
---|
610 | Similarly, you must call toAscii(), toLatin1(), toUtf8(), or
|
---|
611 | toLocal8Bit() explicitly to convert the QString to an 8-bit
|
---|
612 | string. (Other encodings are supported through the QTextCodec
|
---|
613 | class.)
|
---|
614 |
|
---|
615 | \table 100 %
|
---|
616 | \row
|
---|
617 | \o
|
---|
618 | \section1 Note for C Programmers
|
---|
619 |
|
---|
620 | Due to C++'s type system and the fact that QString is
|
---|
621 | \l{implicitly shared}, QStrings may be treated like \c{int}s or
|
---|
622 | other basic types. For example:
|
---|
623 |
|
---|
624 | \snippet doc/src/snippets/qstring/main.cpp 7
|
---|
625 |
|
---|
626 | The \c result variable, is a normal variable allocated on the
|
---|
627 | stack. When \c return is called, and because we're returning by
|
---|
628 | value, the copy constructor is called and a copy of the string is
|
---|
629 | returned. No actual copying takes place thanks to the implicit
|
---|
630 | sharing.
|
---|
631 |
|
---|
632 | \endtable
|
---|
633 |
|
---|
634 | \section1 Distinction Between Null and Empty Strings
|
---|
635 |
|
---|
636 | For historical reasons, QString distinguishes between a null
|
---|
637 | string and an empty string. A \e null string is a string that is
|
---|
638 | initialized using QString's default constructor or by passing
|
---|
639 | (const char *)0 to the constructor. An \e empty string is any
|
---|
640 | string with size 0. A null string is always empty, but an empty
|
---|
641 | string isn't necessarily null:
|
---|
642 |
|
---|
643 | \snippet doc/src/snippets/qstring/main.cpp 8
|
---|
644 |
|
---|
645 | All functions except isNull() treat null strings the same as empty
|
---|
646 | strings. For example, toAscii().constData() returns a pointer to a
|
---|
647 | '\\0' character for a null string (\e not a null pointer), and
|
---|
648 | QString() compares equal to QString(""). We recommend that you
|
---|
649 | always use the isEmpty() function and avoid isNull().
|
---|
650 |
|
---|
651 | \section1 Argument Formats
|
---|
652 |
|
---|
653 | In member functions where an argument \e format can be specified
|
---|
654 | (e.g., arg(), number()), the argument \e format can be one of the
|
---|
655 | following:
|
---|
656 |
|
---|
657 | \table
|
---|
658 | \header \o Format \o Meaning
|
---|
659 | \row \o \c e \o format as [-]9.9e[+|-]999
|
---|
660 | \row \o \c E \o format as [-]9.9E[+|-]999
|
---|
661 | \row \o \c f \o format as [-]9.9
|
---|
662 | \row \o \c g \o use \c e or \c f format, whichever is the most concise
|
---|
663 | \row \o \c G \o use \c E or \c f format, whichever is the most concise
|
---|
664 | \endtable
|
---|
665 |
|
---|
666 | A \e precision is also specified with the argument \e format. For
|
---|
667 | the 'e', 'E', and 'f' formats, the \e precision represents the
|
---|
668 | number of digits \e after the decimal point. For the 'g' and 'G'
|
---|
669 | formats, the \e precision represents the maximum number of
|
---|
670 | significant digits (trailing zeroes are omitted).
|
---|
671 |
|
---|
672 | \section1 More Efficient String Construction
|
---|
673 |
|
---|
674 | Using the QString \c{'+'} operator, it is easy to construct a
|
---|
675 | complex string from multiple substrings. You will often write code
|
---|
676 | like this:
|
---|
677 |
|
---|
678 | \snippet doc/src/snippets/qstring/stringbuilder.cpp 0
|
---|
679 |
|
---|
680 | There is nothing wrong with either of these string constructions,
|
---|
681 | but there are a few hidden inefficiencies. Beginning with Qt 4.6,
|
---|
682 | you can eliminate them.
|
---|
683 |
|
---|
684 | First, multiple uses of the \c{'+'} operator usually means
|
---|
685 | multiple memory allocations. When concatenating \e{n} substrings,
|
---|
686 | where \e{n > 2}, there can be as many as \e{n - 1} calls to the
|
---|
687 | memory allocator.
|
---|
688 |
|
---|
689 | Second, QLatin1String does not store its length internally but
|
---|
690 | calls qstrlen() when it needs to know its length.
|
---|
691 |
|
---|
692 | In 4.6, an internal template class \c{QStringBuilder} has been
|
---|
693 | added along with a few helper functions. This class is marked
|
---|
694 | internal and does not appear in the documentation, because you
|
---|
695 | aren't meant to instantiate it in your code. Its use will be
|
---|
696 | automatic, as described below. The class is found in
|
---|
697 | \c {src/corelib/tools/qstringbuilder.cpp} if you want to have a
|
---|
698 | look at it.
|
---|
699 |
|
---|
700 | \c{QStringBuilder} uses expression templates and reimplements the
|
---|
701 | \c{'%'} operator so that when you use \c{'%'} for string
|
---|
702 | concatenation instead of \c{'+'}, multiple substring
|
---|
703 | concatenations will be postponed until the final result is about
|
---|
704 | to be assigned to a QString. At this point, the amount of memory
|
---|
705 | required for the final result is known. The memory allocator is
|
---|
706 | then called \e{once} to get the required space, and the substrings
|
---|
707 | are copied into it one by one.
|
---|
708 |
|
---|
709 | \c{QLatin1Literal} is a second internal class that can replace
|
---|
710 | QLatin1String, which can't be changed for compatibility reasons.
|
---|
711 | \c{QLatin1Literal} stores its length, thereby saving time when
|
---|
712 | \c{QStringBuilder} computes the amount of memory required for the
|
---|
713 | final string.
|
---|
714 |
|
---|
715 | Additional efficiency is gained by inlining and reduced reference
|
---|
716 | counting (the QString created from a \c{QStringBuilder} typically
|
---|
717 | has a ref count of 1, whereas QString::append() needs an extra
|
---|
718 | test).
|
---|
719 |
|
---|
720 | There are three ways you can access this improved method of string
|
---|
721 | construction. The straightforward way is to include
|
---|
722 | \c{QStringBuilder} wherever you want to use it, and use the
|
---|
723 | \c{'%'} operator instead of \c{'+'} when concatenating strings:
|
---|
724 |
|
---|
725 | \snippet doc/src/snippets/qstring/stringbuilder.cpp 5
|
---|
726 |
|
---|
727 | A more global approach is to include this define:
|
---|
728 |
|
---|
729 | \snippet doc/src/snippets/qstring/stringbuilder.cpp 3
|
---|
730 |
|
---|
731 | and use \c{'%'} instead of \c{'+'} for string concatenation
|
---|
732 | everywhere. The third approach, which is the most convenient but
|
---|
733 | not entirely source compatible, is to include two defines:
|
---|
734 |
|
---|
735 | \snippet doc/src/snippets/qstring/stringbuilder.cpp 4
|
---|
736 |
|
---|
737 | and the \c{'+'} will automatically be performed as the
|
---|
738 | \c{QStringBuilder} \c{'%'} everywhere.
|
---|
739 |
|
---|
740 | \sa fromRawData(), QChar, QLatin1String, QByteArray, QStringRef
|
---|
741 | */
|
---|
742 |
|
---|
743 | /*!
|
---|
744 | \enum QString::SplitBehavior
|
---|
745 |
|
---|
746 | This enum specifies how the split() function should behave with
|
---|
747 | respect to empty strings.
|
---|
748 |
|
---|
749 | \value KeepEmptyParts If a field is empty, keep it in the result.
|
---|
750 | \value SkipEmptyParts If a field is empty, don't include it in the result.
|
---|
751 |
|
---|
752 | \sa split()
|
---|
753 | */
|
---|
754 |
|
---|
755 | QString::Data QString::shared_null = { Q_BASIC_ATOMIC_INITIALIZER(1),
|
---|
756 | 0, 0, shared_null.array, 0, 0, 0, 0, 0, 0, {0} };
|
---|
757 | QString::Data QString::shared_empty = { Q_BASIC_ATOMIC_INITIALIZER(1),
|
---|
758 | 0, 0, shared_empty.array, 0, 0, 0, 0, 0, 0, {0} };
|
---|
759 |
|
---|
760 | int QString::grow(int size)
|
---|
761 | {
|
---|
762 | return qAllocMore(size * sizeof(QChar), sizeof(Data)) / sizeof(QChar);
|
---|
763 | }
|
---|
764 |
|
---|
765 | /*! \typedef QString::ConstIterator
|
---|
766 |
|
---|
767 | Qt-style synonym for QString::const_iterator.
|
---|
768 | */
|
---|
769 |
|
---|
770 | /*! \typedef QString::Iterator
|
---|
771 |
|
---|
772 | Qt-style synonym for QString::iterator.
|
---|
773 | */
|
---|
774 |
|
---|
775 | /*! \typedef QString::const_iterator
|
---|
776 |
|
---|
777 | The QString::const_iterator typedef provides an STL-style const
|
---|
778 | iterator for QString.
|
---|
779 |
|
---|
780 | \sa QString::iterator
|
---|
781 | */
|
---|
782 |
|
---|
783 | /*! \typedef QString::iterator
|
---|
784 |
|
---|
785 | The QString::iterator typedef provides an STL-style non-const
|
---|
786 | iterator for QString.
|
---|
787 |
|
---|
788 | \sa QString::const_iterator
|
---|
789 | */
|
---|
790 |
|
---|
791 | /*! \fn QString::iterator QString::begin()
|
---|
792 |
|
---|
793 | Returns an \l{STL-style iterator} pointing to the first character in
|
---|
794 | the string.
|
---|
795 |
|
---|
796 | \sa constBegin(), end()
|
---|
797 | */
|
---|
798 |
|
---|
799 | /*! \fn QString::const_iterator QString::begin() const
|
---|
800 |
|
---|
801 | \overload begin()
|
---|
802 | */
|
---|
803 |
|
---|
804 | /*! \fn QString::const_iterator QString::constBegin() const
|
---|
805 |
|
---|
806 | Returns a const \l{STL-style iterator} pointing to the first character
|
---|
807 | in the string.
|
---|
808 |
|
---|
809 | \sa begin(), constEnd()
|
---|
810 | */
|
---|
811 |
|
---|
812 | /*! \fn QString::iterator QString::end()
|
---|
813 |
|
---|
814 | Returns an \l{STL-style iterator} pointing to the imaginary character
|
---|
815 | after the last character in the string.
|
---|
816 |
|
---|
817 | \sa begin(), constEnd()
|
---|
818 | */
|
---|
819 |
|
---|
820 | /*! \fn QString::const_iterator QString::end() const
|
---|
821 |
|
---|
822 | \overload end()
|
---|
823 | */
|
---|
824 |
|
---|
825 | /*! \fn QString::const_iterator QString::constEnd() const
|
---|
826 |
|
---|
827 | Returns a const \l{STL-style iterator} pointing to the imaginary
|
---|
828 | item after the last item in the list.
|
---|
829 |
|
---|
830 | \sa constBegin(), end()
|
---|
831 | */
|
---|
832 |
|
---|
833 | /*!
|
---|
834 | \fn QString::QString()
|
---|
835 |
|
---|
836 | Constructs a null string. Null strings are also empty.
|
---|
837 |
|
---|
838 | \sa isEmpty()
|
---|
839 | */
|
---|
840 |
|
---|
841 | /*! \fn QString::QString(const char *str)
|
---|
842 |
|
---|
843 | Constructs a string initialized with the ASCII string \a str. The
|
---|
844 | given const char pointer is converted to Unicode using the
|
---|
845 | fromAscii() function.
|
---|
846 |
|
---|
847 | You can disable this constructor by defining \c
|
---|
848 | QT_NO_CAST_FROM_ASCII when you compile your applications. This
|
---|
849 | can be useful if you want to ensure that all user-visible strings
|
---|
850 | go through QObject::tr(), for example.
|
---|
851 |
|
---|
852 | \sa fromAscii(), fromLatin1(), fromLocal8Bit(), fromUtf8()
|
---|
853 | */
|
---|
854 |
|
---|
855 | /*! \fn QString QString::fromStdString(const std::string &str)
|
---|
856 |
|
---|
857 | Returns a copy of the \a str string. The given string is converted
|
---|
858 | to Unicode using the fromAscii() function.
|
---|
859 |
|
---|
860 | This constructor is only available if Qt is configured with STL
|
---|
861 | compatibility enabled.
|
---|
862 |
|
---|
863 | \sa fromAscii(), fromLatin1(), fromLocal8Bit(), fromUtf8()
|
---|
864 | */
|
---|
865 |
|
---|
866 | /*! \fn QString QString::fromStdWString(const std::wstring &str)
|
---|
867 |
|
---|
868 | Returns a copy of the \a str string. The given string is assumed
|
---|
869 | to be encoded in utf16 if the size of wchar_t is 2 bytes (e.g. on
|
---|
870 | windows) and ucs4 if the size of wchar_t is 4 bytes (most Unix
|
---|
871 | systems).
|
---|
872 |
|
---|
873 | This method is only available if Qt is configured with STL
|
---|
874 | compatibility enabled.
|
---|
875 |
|
---|
876 | \sa fromUtf16(), fromLatin1(), fromLocal8Bit(), fromUtf8(), fromUcs4()
|
---|
877 | */
|
---|
878 |
|
---|
879 | /*!
|
---|
880 | \since 4.2
|
---|
881 |
|
---|
882 | Returns a copy of the \a string, where the encoding of \a string depends on
|
---|
883 | the size of wchar. If wchar is 4 bytes, the \a string is interpreted as ucs-4,
|
---|
884 | if wchar is 2 bytes it is interpreted as ucs-2.
|
---|
885 |
|
---|
886 | If \a size is -1 (default), the \a string has to be 0 terminated.
|
---|
887 |
|
---|
888 | \sa fromUtf16(), fromLatin1(), fromLocal8Bit(), fromUtf8(), fromUcs4(), fromStdWString()
|
---|
889 | */
|
---|
890 | QString QString::fromWCharArray(const wchar_t *string, int size)
|
---|
891 | {
|
---|
892 | if (sizeof(wchar_t) == sizeof(QChar)) {
|
---|
893 | return fromUtf16((ushort *)string, size);
|
---|
894 | } else {
|
---|
895 | return fromUcs4((uint *)string, size);
|
---|
896 | }
|
---|
897 | }
|
---|
898 |
|
---|
899 | /*! \fn std::wstring QString::toStdWString() const
|
---|
900 |
|
---|
901 | Returns a std::wstring object with the data contained in this
|
---|
902 | QString. The std::wstring is encoded in utf16 on platforms where
|
---|
903 | wchar_t is 2 bytes wide (e.g. windows) and in ucs4 on platforms
|
---|
904 | where wchar_t is 4 bytes wide (most Unix systems).
|
---|
905 |
|
---|
906 | This operator is mostly useful to pass a QString to a function
|
---|
907 | that accepts a std::wstring object.
|
---|
908 |
|
---|
909 | This operator is only available if Qt is configured with STL
|
---|
910 | compatibility enabled.
|
---|
911 |
|
---|
912 | \sa utf16(), toAscii(), toLatin1(), toUtf8(), toLocal8Bit()
|
---|
913 | */
|
---|
914 |
|
---|
915 | /*!
|
---|
916 | \since 4.2
|
---|
917 |
|
---|
918 | Fills the \a array with the data contained in this QString object.
|
---|
919 | The array is encoded in utf16 on platforms where
|
---|
920 | wchar_t is 2 bytes wide (e.g. windows) and in ucs4 on platforms
|
---|
921 | where wchar_t is 4 bytes wide (most Unix systems).
|
---|
922 |
|
---|
923 | \a array has to be allocated by the caller and contain enough space to
|
---|
924 | hold the complete string (allocating the array with the same length as the
|
---|
925 | string is always sufficient).
|
---|
926 |
|
---|
927 | returns the actual length of the string in \a array.
|
---|
928 |
|
---|
929 | \note This function does not append a null character to the array.
|
---|
930 |
|
---|
931 | \sa utf16(), toUcs4(), toAscii(), toLatin1(), toUtf8(), toLocal8Bit(), toStdWString()
|
---|
932 | */
|
---|
933 | int QString::toWCharArray(wchar_t *array) const
|
---|
934 | {
|
---|
935 | if (sizeof(wchar_t) == sizeof(QChar)) {
|
---|
936 | memcpy(array, utf16(), sizeof(wchar_t)*length());
|
---|
937 | return length();
|
---|
938 | } else {
|
---|
939 | wchar_t *a = array;
|
---|
940 | const unsigned short *uc = utf16();
|
---|
941 | for (int i = 0; i < length(); ++i) {
|
---|
942 | uint u = uc[i];
|
---|
943 | if (u >= 0xd800 && u < 0xdc00 && i < length()-1) {
|
---|
944 | ushort low = uc[i+1];
|
---|
945 | if (low >= 0xdc00 && low < 0xe000) {
|
---|
946 | ++i;
|
---|
947 | u = (u - 0xd800)*0x400 + (low - 0xdc00) + 0x10000;
|
---|
948 | }
|
---|
949 | }
|
---|
950 | *a = wchar_t(u);
|
---|
951 | ++a;
|
---|
952 | }
|
---|
953 | return a - array;
|
---|
954 | }
|
---|
955 | }
|
---|
956 |
|
---|
957 | /*! \fn QString::QString(const QString &other)
|
---|
958 |
|
---|
959 | Constructs a copy of \a other.
|
---|
960 |
|
---|
961 | This operation takes \l{constant time}, because QString is
|
---|
962 | \l{implicitly shared}. This makes returning a QString from a
|
---|
963 | function very fast. If a shared instance is modified, it will be
|
---|
964 | copied (copy-on-write), and that takes \l{linear time}.
|
---|
965 |
|
---|
966 | \sa operator=()
|
---|
967 | */
|
---|
968 |
|
---|
969 | /*!
|
---|
970 | Constructs a string initialized with the first \a size characters
|
---|
971 | of the QChar array \a unicode.
|
---|
972 |
|
---|
973 | QString makes a deep copy of the string data. The unicode data is copied as
|
---|
974 | is and the Byte Order Mark is preserved if present.
|
---|
975 | */
|
---|
976 | QString::QString(const QChar *unicode, int size)
|
---|
977 | {
|
---|
978 | if (!unicode) {
|
---|
979 | d = &shared_null;
|
---|
980 | d->ref.ref();
|
---|
981 | } else if (size <= 0) {
|
---|
982 | d = &shared_empty;
|
---|
983 | d->ref.ref();
|
---|
984 | } else {
|
---|
985 | d = (Data*) qMalloc(sizeof(Data)+size*sizeof(QChar));
|
---|
986 | Q_CHECK_PTR(d);
|
---|
987 | d->ref = 1;
|
---|
988 | d->alloc = d->size = size;
|
---|
989 | d->clean = d->asciiCache = d->simpletext = d->righttoleft = d->capacity = 0;
|
---|
990 | d->data = d->array;
|
---|
991 | memcpy(d->array, unicode, size * sizeof(QChar));
|
---|
992 | d->array[size] = '\0';
|
---|
993 | }
|
---|
994 | }
|
---|
995 |
|
---|
996 |
|
---|
997 | /*!
|
---|
998 | Constructs a string of the given \a size with every character set
|
---|
999 | to \a ch.
|
---|
1000 |
|
---|
1001 | \sa fill()
|
---|
1002 | */
|
---|
1003 | QString::QString(int size, QChar ch)
|
---|
1004 | {
|
---|
1005 | if (size <= 0) {
|
---|
1006 | d = &shared_empty;
|
---|
1007 | d->ref.ref();
|
---|
1008 | } else {
|
---|
1009 | d = (Data*) qMalloc(sizeof(Data)+size*sizeof(QChar));
|
---|
1010 | Q_CHECK_PTR(d);
|
---|
1011 | d->ref = 1;
|
---|
1012 | d->alloc = d->size = size;
|
---|
1013 | d->clean = d->asciiCache = d->simpletext = d->righttoleft = d->capacity = 0;
|
---|
1014 | d->data = d->array;
|
---|
1015 | d->array[size] = '\0';
|
---|
1016 | ushort *i = d->array + size;
|
---|
1017 | ushort *b = d->array;
|
---|
1018 | const ushort value = ch.unicode();
|
---|
1019 | while (i != b)
|
---|
1020 | *--i = value;
|
---|
1021 | }
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 | /*! \fn QString::QString(int size, Qt::Initialization)
|
---|
1025 | \internal
|
---|
1026 |
|
---|
1027 | Constructs a string of the given \a size without initializing the
|
---|
1028 | characters. This is only used in \c QStringBuilder::toString().
|
---|
1029 | */
|
---|
1030 | QString::QString(int size, Qt::Initialization)
|
---|
1031 | {
|
---|
1032 | d = (Data*) qMalloc(sizeof(Data)+size*sizeof(QChar));
|
---|
1033 | Q_CHECK_PTR(d);
|
---|
1034 | d->ref = 1;
|
---|
1035 | d->alloc = d->size = size;
|
---|
1036 | d->clean = d->asciiCache = d->simpletext = d->righttoleft = d->capacity = 0;
|
---|
1037 | d->data = d->array;
|
---|
1038 | d->array[size] = '\0';
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | /*! \fn QString::QString(const QLatin1String &str)
|
---|
1042 |
|
---|
1043 | Constructs a copy of the Latin-1 string \a str.
|
---|
1044 |
|
---|
1045 | \sa fromLatin1()
|
---|
1046 | */
|
---|
1047 |
|
---|
1048 | /*!
|
---|
1049 | Constructs a string of size 1 containing the character \a ch.
|
---|
1050 | */
|
---|
1051 | QString::QString(QChar ch)
|
---|
1052 | {
|
---|
1053 | void *buf = qMalloc(sizeof(Data) + sizeof(QChar));
|
---|
1054 | Q_CHECK_PTR(buf);
|
---|
1055 | d = reinterpret_cast<Data *>(buf);
|
---|
1056 | d->ref = 1;
|
---|
1057 | d->alloc = d->size = 1;
|
---|
1058 | d->clean = d->asciiCache = d->simpletext = d->righttoleft = d->capacity = 0;
|
---|
1059 | d->data = d->array;
|
---|
1060 | d->array[0] = ch.unicode();
|
---|
1061 | d->array[1] = '\0';
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | /*! \fn QString::QString(const QByteArray &ba)
|
---|
1065 |
|
---|
1066 | Constructs a string initialized with the byte array \a ba. The
|
---|
1067 | given byte array is converted to Unicode using fromAscii(). Stops
|
---|
1068 | copying at the first 0 character, otherwise copies the entire byte
|
---|
1069 | array.
|
---|
1070 |
|
---|
1071 | You can disable this constructor by defining \c
|
---|
1072 | QT_NO_CAST_FROM_ASCII when you compile your applications. This
|
---|
1073 | can be useful if you want to ensure that all user-visible strings
|
---|
1074 | go through QObject::tr(), for example.
|
---|
1075 |
|
---|
1076 | \sa fromAscii(), fromLatin1(), fromLocal8Bit(), fromUtf8()
|
---|
1077 | */
|
---|
1078 |
|
---|
1079 | /*! \fn QString::QString(const Null &)
|
---|
1080 | \internal
|
---|
1081 | */
|
---|
1082 |
|
---|
1083 | /*! \fn QString &QString::operator=(const Null &)
|
---|
1084 | \internal
|
---|
1085 | */
|
---|
1086 |
|
---|
1087 | /*!
|
---|
1088 | \fn QString::~QString()
|
---|
1089 |
|
---|
1090 | Destroys the string.
|
---|
1091 | */
|
---|
1092 |
|
---|
1093 |
|
---|
1094 | /*! \fn void QString::detach()
|
---|
1095 |
|
---|
1096 | \internal
|
---|
1097 | */
|
---|
1098 |
|
---|
1099 | /*! \fn void QString::isDetached() const
|
---|
1100 |
|
---|
1101 | \internal
|
---|
1102 | */
|
---|
1103 |
|
---|
1104 | // ### Qt 5: rename freeData() to avoid confusion. See task 197625.
|
---|
1105 | void QString::free(Data *d)
|
---|
1106 | {
|
---|
1107 | #ifdef QT3_SUPPORT
|
---|
1108 | if (d->asciiCache) {
|
---|
1109 | Q_ASSERT(asciiCache);
|
---|
1110 | asciiCache->remove(d);
|
---|
1111 | }
|
---|
1112 | #endif
|
---|
1113 | qFree(d);
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | /*!
|
---|
1117 | Sets the size of the string to \a size characters.
|
---|
1118 |
|
---|
1119 | If \a size is greater than the current size, the string is
|
---|
1120 | extended to make it \a size characters long with the extra
|
---|
1121 | characters added to the end. The new characters are uninitialized.
|
---|
1122 |
|
---|
1123 | If \a size is less than the current size, characters are removed
|
---|
1124 | from the end.
|
---|
1125 |
|
---|
1126 | Example:
|
---|
1127 |
|
---|
1128 | \snippet doc/src/snippets/qstring/main.cpp 45
|
---|
1129 |
|
---|
1130 | If you want to append a certain number of identical characters to
|
---|
1131 | the string, use \l operator+=() as follows rather than resize():
|
---|
1132 |
|
---|
1133 | \snippet doc/src/snippets/qstring/main.cpp 46
|
---|
1134 |
|
---|
1135 | If you want to expand the string so that it reaches a certain
|
---|
1136 | width and fill the new positions with a particular character, use
|
---|
1137 | the leftJustified() function:
|
---|
1138 |
|
---|
1139 | If \a size is negative, it is equivalent to passing zero.
|
---|
1140 |
|
---|
1141 | \snippet doc/src/snippets/qstring/main.cpp 47
|
---|
1142 |
|
---|
1143 | \sa truncate(), reserve()
|
---|
1144 | */
|
---|
1145 |
|
---|
1146 | void QString::resize(int size)
|
---|
1147 | {
|
---|
1148 | if (size < 0)
|
---|
1149 | size = 0;
|
---|
1150 |
|
---|
1151 | if (size == 0 && !d->capacity) {
|
---|
1152 | Data *x = &shared_empty;
|
---|
1153 | x->ref.ref();
|
---|
1154 | if (!d->ref.deref())
|
---|
1155 | QString::free(d);
|
---|
1156 | d = x;
|
---|
1157 | } else {
|
---|
1158 | if (d->ref != 1 || size > d->alloc ||
|
---|
1159 | (!d->capacity && size < d->size && size < d->alloc >> 1))
|
---|
1160 | realloc(grow(size));
|
---|
1161 | if (d->alloc >= size) {
|
---|
1162 | d->size = size;
|
---|
1163 | if (d->data == d->array) {
|
---|
1164 | d->array[size] = '\0';
|
---|
1165 | }
|
---|
1166 | }
|
---|
1167 | }
|
---|
1168 | }
|
---|
1169 |
|
---|
1170 | /*! \fn int QString::capacity() const
|
---|
1171 |
|
---|
1172 | Returns the maximum number of characters that can be stored in
|
---|
1173 | the string without forcing a reallocation.
|
---|
1174 |
|
---|
1175 | The sole purpose of this function is to provide a means of fine
|
---|
1176 | tuning QString's memory usage. In general, you will rarely ever
|
---|
1177 | need to call this function. If you want to know how many
|
---|
1178 | characters are in the string, call size().
|
---|
1179 |
|
---|
1180 | \sa reserve(), squeeze()
|
---|
1181 | */
|
---|
1182 |
|
---|
1183 | /*!
|
---|
1184 | \fn void QString::reserve(int size)
|
---|
1185 |
|
---|
1186 | Attempts to allocate memory for at least \a size characters. If
|
---|
1187 | you know in advance how large the string will be, you can call
|
---|
1188 | this function, and if you resize the string often you are likely
|
---|
1189 | to get better performance. If \a size is an underestimate, the
|
---|
1190 | worst that will happen is that the QString will be a bit slower.
|
---|
1191 |
|
---|
1192 | The sole purpose of this function is to provide a means of fine
|
---|
1193 | tuning QString's memory usage. In general, you will rarely ever
|
---|
1194 | need to call this function. If you want to change the size of the
|
---|
1195 | string, call resize().
|
---|
1196 |
|
---|
1197 | This function is useful for code that needs to build up a long
|
---|
1198 | string and wants to avoid repeated reallocation. In this example,
|
---|
1199 | we want to add to the string until some condition is true, and
|
---|
1200 | we're fairly sure that size is large enough to make a call to
|
---|
1201 | reserve() worthwhile:
|
---|
1202 |
|
---|
1203 | \snippet doc/src/snippets/qstring/main.cpp 44
|
---|
1204 |
|
---|
1205 | \sa squeeze(), capacity()
|
---|
1206 | */
|
---|
1207 |
|
---|
1208 | /*!
|
---|
1209 | \fn void QString::squeeze()
|
---|
1210 |
|
---|
1211 | Releases any memory not required to store the character data.
|
---|
1212 |
|
---|
1213 | The sole purpose of this function is to provide a means of fine
|
---|
1214 | tuning QString's memory usage. In general, you will rarely ever
|
---|
1215 | need to call this function.
|
---|
1216 |
|
---|
1217 | \sa reserve(), capacity()
|
---|
1218 | */
|
---|
1219 |
|
---|
1220 | // ### Qt 5: rename reallocData() to avoid confusion. 197625
|
---|
1221 | void QString::realloc(int alloc)
|
---|
1222 | {
|
---|
1223 | if (d->ref != 1 || d->data != d->array) {
|
---|
1224 | Data *x = static_cast<Data *>(qMalloc(sizeof(Data) + alloc * sizeof(QChar)));
|
---|
1225 | Q_CHECK_PTR(x);
|
---|
1226 | x->size = qMin(alloc, d->size);
|
---|
1227 | ::memcpy(x->array, d->data, x->size * sizeof(QChar));
|
---|
1228 | x->array[x->size] = 0;
|
---|
1229 | x->asciiCache = 0;
|
---|
1230 | x->ref = 1;
|
---|
1231 | x->alloc = alloc;
|
---|
1232 | x->clean = d->clean;
|
---|
1233 | x->simpletext = d->simpletext;
|
---|
1234 | x->righttoleft = d->righttoleft;
|
---|
1235 | x->capacity = d->capacity;
|
---|
1236 | x->data = x->array;
|
---|
1237 | if (!d->ref.deref())
|
---|
1238 | QString::free(d);
|
---|
1239 | d = x;
|
---|
1240 | } else {
|
---|
1241 | #ifdef QT3_SUPPORT
|
---|
1242 | if (d->asciiCache) {
|
---|
1243 | Q_ASSERT(asciiCache);
|
---|
1244 | asciiCache->remove(d);
|
---|
1245 | }
|
---|
1246 | #endif
|
---|
1247 | d = static_cast<Data *>(q_check_ptr(qRealloc(d, sizeof(Data) + alloc * sizeof(QChar))));
|
---|
1248 | d->alloc = alloc;
|
---|
1249 | d->data = d->array;
|
---|
1250 | }
|
---|
1251 | }
|
---|
1252 |
|
---|
1253 | void QString::realloc()
|
---|
1254 | {
|
---|
1255 | realloc(d->size);
|
---|
1256 | }
|
---|
1257 |
|
---|
1258 | void QString::expand(int i)
|
---|
1259 | {
|
---|
1260 | int sz = d->size;
|
---|
1261 | resize(qMax(i + 1, sz));
|
---|
1262 | if (d->size - 1 > sz) {
|
---|
1263 | ushort *n = d->data + d->size - 1;
|
---|
1264 | ushort *e = d->data + sz;
|
---|
1265 | while (n != e)
|
---|
1266 | * --n = ' ';
|
---|
1267 | }
|
---|
1268 | }
|
---|
1269 |
|
---|
1270 | /*! \fn void QString::clear()
|
---|
1271 |
|
---|
1272 | Clears the contents of the string and makes it empty.
|
---|
1273 |
|
---|
1274 | \sa resize(), isEmpty()
|
---|
1275 | */
|
---|
1276 |
|
---|
1277 | /*! \fn QString &QString::operator=(const QString &other)
|
---|
1278 |
|
---|
1279 | Assigns \a other to this string and returns a reference to this
|
---|
1280 | string.
|
---|
1281 | */
|
---|
1282 |
|
---|
1283 | QString &QString::operator=(const QString &other)
|
---|
1284 | {
|
---|
1285 | other.d->ref.ref();
|
---|
1286 | if (!d->ref.deref())
|
---|
1287 | QString::free(d);
|
---|
1288 | d = other.d;
|
---|
1289 | return *this;
|
---|
1290 | }
|
---|
1291 |
|
---|
1292 |
|
---|
1293 | /*! \fn QString &QString::operator=(const QLatin1String &str)
|
---|
1294 |
|
---|
1295 | \overload operator=()
|
---|
1296 |
|
---|
1297 | Assigns the Latin-1 string \a str to this string.
|
---|
1298 | */
|
---|
1299 |
|
---|
1300 | /*! \fn QString &QString::operator=(const QByteArray &ba)
|
---|
1301 |
|
---|
1302 | \overload operator=()
|
---|
1303 |
|
---|
1304 | Assigns \a ba to this string. The byte array is converted to
|
---|
1305 | Unicode using the fromAscii() function.
|
---|
1306 |
|
---|
1307 | You can disable this operator by defining \c
|
---|
1308 | QT_NO_CAST_FROM_ASCII when you compile your applications. This
|
---|
1309 | can be useful if you want to ensure that all user-visible strings
|
---|
1310 | go through QObject::tr(), for example.
|
---|
1311 | */
|
---|
1312 |
|
---|
1313 | /*! \fn QString &QString::operator=(const char *str)
|
---|
1314 |
|
---|
1315 | \overload operator=()
|
---|
1316 |
|
---|
1317 | Assigns \a str to this string. The const char pointer is converted
|
---|
1318 | to Unicode using the fromAscii() function.
|
---|
1319 |
|
---|
1320 | You can disable this operator by defining \c
|
---|
1321 | QT_NO_CAST_FROM_ASCII when you compile your applications. This
|
---|
1322 | can be useful if you want to ensure that all user-visible strings
|
---|
1323 | go through QObject::tr(), for example.
|
---|
1324 | */
|
---|
1325 |
|
---|
1326 | /*! \fn QString &QString::operator=(char ch)
|
---|
1327 |
|
---|
1328 | \overload operator=()
|
---|
1329 |
|
---|
1330 | Assigns character \a ch to this string. The character is converted
|
---|
1331 | to Unicode using the fromAscii() function.
|
---|
1332 |
|
---|
1333 | You can disable this operator by defining \c
|
---|
1334 | QT_NO_CAST_FROM_ASCII when you compile your applications. This
|
---|
1335 | can be useful if you want to ensure that all user-visible strings
|
---|
1336 | go through QObject::tr(), for example.
|
---|
1337 | */
|
---|
1338 |
|
---|
1339 | /*!
|
---|
1340 | \overload operator=()
|
---|
1341 |
|
---|
1342 | Sets the string to contain the single character \a ch.
|
---|
1343 | */
|
---|
1344 | QString &QString::operator=(QChar ch)
|
---|
1345 | {
|
---|
1346 | return operator=(QString(ch));
|
---|
1347 | }
|
---|
1348 |
|
---|
1349 | /*!
|
---|
1350 | \fn QString& QString::insert(int position, const QString &str)
|
---|
1351 |
|
---|
1352 | Inserts the string \a str at the given index \a position and
|
---|
1353 | returns a reference to this string.
|
---|
1354 |
|
---|
1355 | Example:
|
---|
1356 |
|
---|
1357 | \snippet doc/src/snippets/qstring/main.cpp 26
|
---|
1358 |
|
---|
1359 | If the given \a position is greater than size(), the array is
|
---|
1360 | first extended using resize().
|
---|
1361 |
|
---|
1362 | \sa append(), prepend(), replace(), remove()
|
---|
1363 | */
|
---|
1364 |
|
---|
1365 |
|
---|
1366 | /*!
|
---|
1367 | \fn QString &QString::insert(int position, const QLatin1String &str)
|
---|
1368 | \overload insert()
|
---|
1369 |
|
---|
1370 | Inserts the Latin-1 string \a str at the given index \a position.
|
---|
1371 | */
|
---|
1372 | QString &QString::insert(int i, const QLatin1String &str)
|
---|
1373 | {
|
---|
1374 | const uchar *s = (const uchar *)str.latin1();
|
---|
1375 | if (i < 0 || !s || !(*s))
|
---|
1376 | return *this;
|
---|
1377 |
|
---|
1378 | int len = qstrlen(str.latin1());
|
---|
1379 | expand(qMax(d->size, i) + len - 1);
|
---|
1380 |
|
---|
1381 | ::memmove(d->data + i + len, d->data + i, (d->size - i - len) * sizeof(QChar));
|
---|
1382 | for (int j = 0; j < len; ++j)
|
---|
1383 | d->data[i + j] = s[j];
|
---|
1384 | return *this;
|
---|
1385 | }
|
---|
1386 |
|
---|
1387 | /*!
|
---|
1388 | \fn QString& QString::insert(int position, const QChar *unicode, int size)
|
---|
1389 | \overload insert()
|
---|
1390 |
|
---|
1391 | Inserts the first \a size characters of the QChar array \a unicode
|
---|
1392 | at the given index \a position in the string.
|
---|
1393 | */
|
---|
1394 | QString& QString::insert(int i, const QChar *unicode, int size)
|
---|
1395 | {
|
---|
1396 | if (i < 0 || size <= 0)
|
---|
1397 | return *this;
|
---|
1398 |
|
---|
1399 | const ushort *s = (const ushort *)unicode;
|
---|
1400 | if (s >= d->data && s < d->data + d->alloc) {
|
---|
1401 | // Part of me - take a copy
|
---|
1402 | ushort *tmp = static_cast<ushort *>(qMalloc(size * sizeof(QChar)));
|
---|
1403 | Q_CHECK_PTR(tmp);
|
---|
1404 | memcpy(tmp, s, size * sizeof(QChar));
|
---|
1405 | insert(i, reinterpret_cast<const QChar *>(tmp), size);
|
---|
1406 | qFree(tmp);
|
---|
1407 | return *this;
|
---|
1408 | }
|
---|
1409 |
|
---|
1410 | expand(qMax(d->size, i) + size - 1);
|
---|
1411 |
|
---|
1412 | ::memmove(d->data + i + size, d->data + i, (d->size - i - size) * sizeof(QChar));
|
---|
1413 | memcpy(d->data + i, s, size * sizeof(QChar));
|
---|
1414 | return *this;
|
---|
1415 | }
|
---|
1416 |
|
---|
1417 | /*!
|
---|
1418 | \fn QString& QString::insert(int position, QChar ch)
|
---|
1419 | \overload insert()
|
---|
1420 |
|
---|
1421 | Inserts \a ch at the given index \a position in the string.
|
---|
1422 | */
|
---|
1423 |
|
---|
1424 | QString& QString::insert(int i, QChar ch)
|
---|
1425 | {
|
---|
1426 | if (i < 0)
|
---|
1427 | i += d->size;
|
---|
1428 | if (i < 0)
|
---|
1429 | return *this;
|
---|
1430 | expand(qMax(i, d->size));
|
---|
1431 | ::memmove(d->data + i + 1, d->data + i, (d->size - i) * sizeof(QChar));
|
---|
1432 | d->data[i] = ch.unicode();
|
---|
1433 | return *this;
|
---|
1434 | }
|
---|
1435 |
|
---|
1436 | /*!
|
---|
1437 | Appends the string \a str onto the end of this string.
|
---|
1438 |
|
---|
1439 | Example:
|
---|
1440 |
|
---|
1441 | \snippet doc/src/snippets/qstring/main.cpp 9
|
---|
1442 |
|
---|
1443 | This is the same as using the insert() function:
|
---|
1444 |
|
---|
1445 | \snippet doc/src/snippets/qstring/main.cpp 10
|
---|
1446 |
|
---|
1447 | The append() function is typically very fast (\l{constant time}),
|
---|
1448 | because QString preallocates extra space at the end of the string
|
---|
1449 | data so it can grow without reallocating the entire string each
|
---|
1450 | time.
|
---|
1451 |
|
---|
1452 | \sa operator+=(), prepend(), insert()
|
---|
1453 | */
|
---|
1454 | QString &QString::append(const QString &str)
|
---|
1455 | {
|
---|
1456 | if (str.d != &shared_null) {
|
---|
1457 | if (d == &shared_null) {
|
---|
1458 | operator=(str);
|
---|
1459 | } else {
|
---|
1460 | if (d->ref != 1 || d->size + str.d->size > d->alloc)
|
---|
1461 | realloc(grow(d->size + str.d->size));
|
---|
1462 | memcpy(d->data + d->size, str.d->data, str.d->size * sizeof(QChar));
|
---|
1463 | d->size += str.d->size;
|
---|
1464 | d->data[d->size] = '\0';
|
---|
1465 | }
|
---|
1466 | }
|
---|
1467 | return *this;
|
---|
1468 | }
|
---|
1469 |
|
---|
1470 | /*!
|
---|
1471 | \overload append()
|
---|
1472 |
|
---|
1473 | Appends the Latin-1 string \a str to this string.
|
---|
1474 | */
|
---|
1475 | QString &QString::append(const QLatin1String &str)
|
---|
1476 | {
|
---|
1477 | const uchar *s = (const uchar *)str.latin1();
|
---|
1478 | if (s) {
|
---|
1479 | int len = qstrlen((char *)s);
|
---|
1480 | if (d->ref != 1 || d->size + len > d->alloc)
|
---|
1481 | realloc(grow(d->size + len));
|
---|
1482 | ushort *i = d->data + d->size;
|
---|
1483 | while ((*i++ = *s++))
|
---|
1484 | ;
|
---|
1485 | d->size += len;
|
---|
1486 | }
|
---|
1487 | return *this;
|
---|
1488 | }
|
---|
1489 |
|
---|
1490 | /*! \fn QString &QString::append(const QByteArray &ba)
|
---|
1491 |
|
---|
1492 | \overload append()
|
---|
1493 |
|
---|
1494 | Appends the byte array \a ba to this string. The given byte array
|
---|
1495 | is converted to Unicode using the fromAscii() function.
|
---|
1496 |
|
---|
1497 | You can disable this function by defining \c QT_NO_CAST_FROM_ASCII
|
---|
1498 | when you compile your applications. This can be useful if you want
|
---|
1499 | to ensure that all user-visible strings go through QObject::tr(),
|
---|
1500 | for example.
|
---|
1501 | */
|
---|
1502 |
|
---|
1503 | /*! \fn QString &QString::append(const char *str)
|
---|
1504 |
|
---|
1505 | \overload append()
|
---|
1506 |
|
---|
1507 | Appends the string \a str to this string. The given const char
|
---|
1508 | pointer is converted to Unicode using the fromAscii() function.
|
---|
1509 |
|
---|
1510 | You can disable this function by defining \c QT_NO_CAST_FROM_ASCII
|
---|
1511 | when you compile your applications. This can be useful if you want
|
---|
1512 | to ensure that all user-visible strings go through QObject::tr(),
|
---|
1513 | for example.
|
---|
1514 | */
|
---|
1515 |
|
---|
1516 | /*!
|
---|
1517 | \overload append()
|
---|
1518 |
|
---|
1519 | Appends the character \a ch to this string.
|
---|
1520 | */
|
---|
1521 | QString &QString::append(QChar ch)
|
---|
1522 | {
|
---|
1523 | if (d->ref != 1 || d->size + 1 > d->alloc)
|
---|
1524 | realloc(grow(d->size + 1));
|
---|
1525 | d->data[d->size++] = ch.unicode();
|
---|
1526 | d->data[d->size] = '\0';
|
---|
1527 | return *this;
|
---|
1528 | }
|
---|
1529 |
|
---|
1530 | /*! \fn QString &QString::prepend(const QString &str)
|
---|
1531 |
|
---|
1532 | Prepends the string \a str to the beginning of this string and
|
---|
1533 | returns a reference to this string.
|
---|
1534 |
|
---|
1535 | Example:
|
---|
1536 |
|
---|
1537 | \snippet doc/src/snippets/qstring/main.cpp 36
|
---|
1538 |
|
---|
1539 | \sa append(), insert()
|
---|
1540 | */
|
---|
1541 |
|
---|
1542 | /*! \fn QString &QString::prepend(const QLatin1String &str)
|
---|
1543 |
|
---|
1544 | \overload prepend()
|
---|
1545 |
|
---|
1546 | Prepends the Latin-1 string \a str to this string.
|
---|
1547 | */
|
---|
1548 |
|
---|
1549 | /*! \fn QString &QString::prepend(const QByteArray &ba)
|
---|
1550 |
|
---|
1551 | \overload prepend()
|
---|
1552 |
|
---|
1553 | Prepends the byte array \a ba to this string. The byte array is
|
---|
1554 | converted to Unicode using the fromAscii() function.
|
---|
1555 |
|
---|
1556 | You can disable this function by defining \c
|
---|
1557 | QT_NO_CAST_FROM_ASCII when you compile your applications. This
|
---|
1558 | can be useful if you want to ensure that all user-visible strings
|
---|
1559 | go through QObject::tr(), for example.
|
---|
1560 | */
|
---|
1561 |
|
---|
1562 | /*! \fn QString &QString::prepend(const char *str)
|
---|
1563 |
|
---|
1564 | \overload prepend()
|
---|
1565 |
|
---|
1566 | Prepends the string \a str to this string. The const char pointer
|
---|
1567 | is converted to Unicode using the fromAscii() function.
|
---|
1568 |
|
---|
1569 | You can disable this function by defining \c
|
---|
1570 | QT_NO_CAST_FROM_ASCII when you compile your applications. This
|
---|
1571 | can be useful if you want to ensure that all user-visible strings
|
---|
1572 | go through QObject::tr(), for example.
|
---|
1573 | */
|
---|
1574 |
|
---|
1575 | /*! \fn QString &QString::prepend(QChar ch)
|
---|
1576 |
|
---|
1577 | \overload prepend()
|
---|
1578 |
|
---|
1579 | Prepends the character \a ch to this string.
|
---|
1580 | */
|
---|
1581 |
|
---|
1582 | /*!
|
---|
1583 | \fn QString &QString::remove(int position, int n)
|
---|
1584 |
|
---|
1585 | Removes \a n characters from the string, starting at the given \a
|
---|
1586 | position index, and returns a reference to the string.
|
---|
1587 |
|
---|
1588 | If the specified \a position index is within the string, but \a
|
---|
1589 | position + \a n is beyond the end of the string, the string is
|
---|
1590 | truncated at the specified \a position.
|
---|
1591 |
|
---|
1592 | \snippet doc/src/snippets/qstring/main.cpp 37
|
---|
1593 |
|
---|
1594 | \sa insert(), replace()
|
---|
1595 | */
|
---|
1596 | QString &QString::remove(int pos, int len)
|
---|
1597 | {
|
---|
1598 | if (pos < 0) // count from end of string
|
---|
1599 | pos += d->size;
|
---|
1600 | if (pos < 0 || pos >= d->size) {
|
---|
1601 | // range problems
|
---|
1602 | } else if (len >= d->size - pos) {
|
---|
1603 | resize(pos); // truncate
|
---|
1604 | } else if (len > 0) {
|
---|
1605 | detach();
|
---|
1606 | memmove(d->data + pos, d->data + pos + len,
|
---|
1607 | (d->size - pos - len + 1) * sizeof(ushort));
|
---|
1608 | d->size -= len;
|
---|
1609 | }
|
---|
1610 | return *this;
|
---|
1611 | }
|
---|
1612 |
|
---|
1613 | /*!
|
---|
1614 | Removes every occurrence of the given \a str string in this
|
---|
1615 | string, and returns a reference to this string.
|
---|
1616 |
|
---|
1617 | If \a cs is Qt::CaseSensitive (default), the search is
|
---|
1618 | case sensitive; otherwise the search is case insensitive.
|
---|
1619 |
|
---|
1620 | This is the same as \c replace(str, "", cs).
|
---|
1621 |
|
---|
1622 | \sa replace()
|
---|
1623 | */
|
---|
1624 | QString &QString::remove(const QString &str, Qt::CaseSensitivity cs)
|
---|
1625 | {
|
---|
1626 | if (str.d->size) {
|
---|
1627 | int i = 0;
|
---|
1628 | while ((i = indexOf(str, i, cs)) != -1)
|
---|
1629 | remove(i, str.d->size);
|
---|
1630 | }
|
---|
1631 | return *this;
|
---|
1632 | }
|
---|
1633 |
|
---|
1634 | /*!
|
---|
1635 | Removes every occurrence of the character \a ch in this string, and
|
---|
1636 | returns a reference to this string.
|
---|
1637 |
|
---|
1638 | If \a cs is Qt::CaseSensitive (default), the search is case
|
---|
1639 | sensitive; otherwise the search is case insensitive.
|
---|
1640 |
|
---|
1641 | Example:
|
---|
1642 |
|
---|
1643 | \snippet doc/src/snippets/qstring/main.cpp 38
|
---|
1644 |
|
---|
1645 | This is the same as \c replace(ch, "", cs).
|
---|
1646 |
|
---|
1647 | \sa replace()
|
---|
1648 | */
|
---|
1649 | QString &QString::remove(QChar ch, Qt::CaseSensitivity cs)
|
---|
1650 | {
|
---|
1651 | int i = 0;
|
---|
1652 | ushort c = ch.unicode();
|
---|
1653 | if (cs == Qt::CaseSensitive) {
|
---|
1654 | while (i < d->size)
|
---|
1655 | if (d->data[i] == ch)
|
---|
1656 | remove(i, 1);
|
---|
1657 | else
|
---|
1658 | i++;
|
---|
1659 | } else {
|
---|
1660 | c = foldCase(c);
|
---|
1661 | while (i < d->size)
|
---|
1662 | if (foldCase(d->data[i]) == c)
|
---|
1663 | remove(i, 1);
|
---|
1664 | else
|
---|
1665 | i++;
|
---|
1666 | }
|
---|
1667 | return *this;
|
---|
1668 | }
|
---|
1669 |
|
---|
1670 | /*!
|
---|
1671 | \fn QString &QString::remove(const QRegExp &rx)
|
---|
1672 |
|
---|
1673 | Removes every occurrence of the regular expression \a rx in the
|
---|
1674 | string, and returns a reference to the string. For example:
|
---|
1675 |
|
---|
1676 | \snippet doc/src/snippets/qstring/main.cpp 39
|
---|
1677 |
|
---|
1678 | \sa indexOf(), lastIndexOf(), replace()
|
---|
1679 | */
|
---|
1680 |
|
---|
1681 | /*!
|
---|
1682 | \fn QString &QString::replace(int position, int n, const QString &after)
|
---|
1683 |
|
---|
1684 | Replaces \a n characters beginning at index \a position with
|
---|
1685 | the string \a after and returns a reference to this string.
|
---|
1686 |
|
---|
1687 | Example:
|
---|
1688 |
|
---|
1689 | \snippet doc/src/snippets/qstring/main.cpp 40
|
---|
1690 |
|
---|
1691 | \sa insert(), remove()
|
---|
1692 | */
|
---|
1693 | QString &QString::replace(int pos, int len, const QString &after)
|
---|
1694 | {
|
---|
1695 | QString copy = after;
|
---|
1696 | return replace(pos, len, copy.constData(), copy.length());
|
---|
1697 | }
|
---|
1698 |
|
---|
1699 | /*!
|
---|
1700 | \fn QString &QString::replace(int position, int n, const QChar *unicode, int size)
|
---|
1701 | \overload replace()
|
---|
1702 | Replaces \a n characters beginning at index \a position with the
|
---|
1703 | first \a size characters of the QChar array \a unicode and returns a
|
---|
1704 | reference to this string.
|
---|
1705 | */
|
---|
1706 | QString &QString::replace(int pos, int len, const QChar *unicode, int size)
|
---|
1707 | {
|
---|
1708 | if (pos < 0 || pos > d->size)
|
---|
1709 | return *this;
|
---|
1710 | if (pos + len > d->size)
|
---|
1711 | len = d->size - pos;
|
---|
1712 |
|
---|
1713 | uint index = pos;
|
---|
1714 | replace_helper(&index, 1, len, unicode, size);
|
---|
1715 | return *this;
|
---|
1716 | }
|
---|
1717 |
|
---|
1718 | /*!
|
---|
1719 | \fn QString &QString::replace(int position, int n, QChar after)
|
---|
1720 | \overload replace()
|
---|
1721 |
|
---|
1722 | Replaces \a n characters beginning at index \a position with the
|
---|
1723 | character \a after and returns a reference to this string.
|
---|
1724 | */
|
---|
1725 | QString &QString::replace(int pos, int len, QChar after)
|
---|
1726 | {
|
---|
1727 | return replace(pos, len, &after, 1);
|
---|
1728 | }
|
---|
1729 |
|
---|
1730 | /*!
|
---|
1731 | \overload replace()
|
---|
1732 | Replaces every occurrence of the string \a before with the string \a
|
---|
1733 | after and returns a reference to this string.
|
---|
1734 |
|
---|
1735 | If \a cs is Qt::CaseSensitive (default), the search is case
|
---|
1736 | sensitive; otherwise the search is case insensitive.
|
---|
1737 |
|
---|
1738 | Example:
|
---|
1739 |
|
---|
1740 | \snippet doc/src/snippets/qstring/main.cpp 41
|
---|
1741 |
|
---|
1742 | \note The replacement text is not rescanned after it is inserted.
|
---|
1743 |
|
---|
1744 | Example:
|
---|
1745 |
|
---|
1746 | \snippet doc/src/snippets/qstring/main.cpp 86
|
---|
1747 | */
|
---|
1748 | QString &QString::replace(const QString &before, const QString &after, Qt::CaseSensitivity cs)
|
---|
1749 | {
|
---|
1750 | return replace(before.constData(), before.size(), after.constData(), after.size(), cs);
|
---|
1751 | }
|
---|
1752 |
|
---|
1753 | /*!
|
---|
1754 | \internal
|
---|
1755 | */
|
---|
1756 | void QString::replace_helper(uint *indices, int nIndices, int blen, const QChar *after, int alen)
|
---|
1757 | {
|
---|
1758 | // copy *after in case it lies inside our own d->data area
|
---|
1759 | // (which we could possibly invalidate via a realloc or corrupt via memcpy operations.)
|
---|
1760 | QChar *afterBuffer = const_cast<QChar *>(after);
|
---|
1761 | if (after >= reinterpret_cast<QChar *>(d->data) && after < reinterpret_cast<QChar *>(d->data) + d->size) {
|
---|
1762 | afterBuffer = static_cast<QChar *>(qMalloc(alen*sizeof(QChar)));
|
---|
1763 | Q_CHECK_PTR(afterBuffer);
|
---|
1764 | ::memcpy(afterBuffer, after, alen*sizeof(QChar));
|
---|
1765 | }
|
---|
1766 |
|
---|
1767 | QT_TRY {
|
---|
1768 | detach();
|
---|
1769 | if (blen == alen) {
|
---|
1770 | // replace in place
|
---|
1771 | for (int i = 0; i < nIndices; ++i)
|
---|
1772 | memcpy(d->data + indices[i], afterBuffer, alen * sizeof(QChar));
|
---|
1773 | } else if (alen < blen) {
|
---|
1774 | // replace from front
|
---|
1775 | uint to = indices[0];
|
---|
1776 | if (alen)
|
---|
1777 | memcpy(d->data+to, after, alen*sizeof(QChar));
|
---|
1778 | to += alen;
|
---|
1779 | uint movestart = indices[0] + blen;
|
---|
1780 | for (int i = 1; i < nIndices; ++i) {
|
---|
1781 | int msize = indices[i] - movestart;
|
---|
1782 | if (msize > 0) {
|
---|
1783 | memmove(d->data + to, d->data + movestart, msize * sizeof(QChar));
|
---|
1784 | to += msize;
|
---|
1785 | }
|
---|
1786 | if (alen) {
|
---|
1787 | memcpy(d->data + to, afterBuffer, alen*sizeof(QChar));
|
---|
1788 | to += alen;
|
---|
1789 | }
|
---|
1790 | movestart = indices[i] + blen;
|
---|
1791 | }
|
---|
1792 | int msize = d->size - movestart;
|
---|
1793 | if (msize > 0)
|
---|
1794 | memmove(d->data + to, d->data + movestart, msize * sizeof(QChar));
|
---|
1795 | resize(d->size - nIndices*(blen-alen));
|
---|
1796 | } else {
|
---|
1797 | // replace from back
|
---|
1798 | int adjust = nIndices*(alen-blen);
|
---|
1799 | int newLen = d->size + adjust;
|
---|
1800 | int moveend = d->size;
|
---|
1801 | resize(newLen);
|
---|
1802 |
|
---|
1803 | while (nIndices) {
|
---|
1804 | --nIndices;
|
---|
1805 | int movestart = indices[nIndices] + blen;
|
---|
1806 | int insertstart = indices[nIndices] + nIndices*(alen-blen);
|
---|
1807 | int moveto = insertstart + alen;
|
---|
1808 | memmove(d->data + moveto, d->data + movestart,
|
---|
1809 | (moveend - movestart)*sizeof(QChar));
|
---|
1810 | memcpy(d->data + insertstart, afterBuffer, alen*sizeof(QChar));
|
---|
1811 | moveend = movestart-blen;
|
---|
1812 | }
|
---|
1813 | }
|
---|
1814 | } QT_CATCH(const std::bad_alloc &) {
|
---|
1815 | if (afterBuffer != after)
|
---|
1816 | qFree(afterBuffer);
|
---|
1817 | QT_RETHROW;
|
---|
1818 | }
|
---|
1819 | if (afterBuffer != after)
|
---|
1820 | qFree(afterBuffer);
|
---|
1821 | }
|
---|
1822 |
|
---|
1823 | /*!
|
---|
1824 | \since 4.5
|
---|
1825 | \overload replace()
|
---|
1826 |
|
---|
1827 | Replaces each occurrence in this string of the first \a blen
|
---|
1828 | characters of \a before with the first \a alen characters of \a
|
---|
1829 | after and returns a reference to this string.
|
---|
1830 |
|
---|
1831 | If \a cs is Qt::CaseSensitive (default), the search is case
|
---|
1832 | sensitive; otherwise the search is case insensitive.
|
---|
1833 | */
|
---|
1834 | QString &QString::replace(const QChar *before, int blen,
|
---|
1835 | const QChar *after, int alen,
|
---|
1836 | Qt::CaseSensitivity cs)
|
---|
1837 | {
|
---|
1838 | if (d->size == 0) {
|
---|
1839 | if (blen)
|
---|
1840 | return *this;
|
---|
1841 | } else {
|
---|
1842 | if (cs == Qt::CaseSensitive && before == after && blen == alen)
|
---|
1843 | return *this;
|
---|
1844 | }
|
---|
1845 | if (alen == 0 && blen == 0)
|
---|
1846 | return *this;
|
---|
1847 |
|
---|
1848 | QStringMatcher matcher(before, blen, cs);
|
---|
1849 |
|
---|
1850 | int index = 0;
|
---|
1851 | while (1) {
|
---|
1852 | uint indices[1024];
|
---|
1853 | uint pos = 0;
|
---|
1854 | while (pos < 1023) {
|
---|
1855 | index = matcher.indexIn(*this, index);
|
---|
1856 | if (index == -1)
|
---|
1857 | break;
|
---|
1858 | indices[pos++] = index;
|
---|
1859 | index += blen;
|
---|
1860 | // avoid infinite loop
|
---|
1861 | if (!blen)
|
---|
1862 | index++;
|
---|
1863 | }
|
---|
1864 | if (!pos)
|
---|
1865 | break;
|
---|
1866 |
|
---|
1867 | replace_helper(indices, pos, blen, after, alen);
|
---|
1868 |
|
---|
1869 | if (index == -1)
|
---|
1870 | break;
|
---|
1871 | // index has to be adjusted in case we get back into the loop above.
|
---|
1872 | index += pos*(alen-blen);
|
---|
1873 | }
|
---|
1874 |
|
---|
1875 | return *this;
|
---|
1876 | }
|
---|
1877 |
|
---|
1878 | /*!
|
---|
1879 | \overload replace()
|
---|
1880 | Replaces every occurrence of the character \a ch in the string with
|
---|
1881 | \a after and returns a reference to this string.
|
---|
1882 |
|
---|
1883 | If \a cs is Qt::CaseSensitive (default), the search is case
|
---|
1884 | sensitive; otherwise the search is case insensitive.
|
---|
1885 | */
|
---|
1886 | QString& QString::replace(QChar ch, const QString &after, Qt::CaseSensitivity cs)
|
---|
1887 | {
|
---|
1888 | if (after.d->size == 0)
|
---|
1889 | return remove(ch, cs);
|
---|
1890 |
|
---|
1891 | if (after.d->size == 1)
|
---|
1892 | return replace(ch, after.d->data[0], cs);
|
---|
1893 |
|
---|
1894 | if (d->size == 0)
|
---|
1895 | return *this;
|
---|
1896 |
|
---|
1897 | ushort cc = (cs == Qt::CaseSensitive ? ch.unicode() : ch.toCaseFolded().unicode());
|
---|
1898 |
|
---|
1899 | int index = 0;
|
---|
1900 | while (1) {
|
---|
1901 | uint indices[1024];
|
---|
1902 | uint pos = 0;
|
---|
1903 | if (cs == Qt::CaseSensitive) {
|
---|
1904 | while (pos < 1023 && index < d->size) {
|
---|
1905 | if (d->data[index] == cc)
|
---|
1906 | indices[pos++] = index;
|
---|
1907 | index++;
|
---|
1908 | }
|
---|
1909 | } else {
|
---|
1910 | while (pos < 1023 && index < d->size) {
|
---|
1911 | if (QChar::toCaseFolded(d->data[index]) == cc)
|
---|
1912 | indices[pos++] = index;
|
---|
1913 | index++;
|
---|
1914 | }
|
---|
1915 | }
|
---|
1916 | if (!pos)
|
---|
1917 | break;
|
---|
1918 |
|
---|
1919 | replace_helper(indices, pos, 1, after.constData(), after.d->size);
|
---|
1920 |
|
---|
1921 | if (index == -1)
|
---|
1922 | break;
|
---|
1923 | // index has to be adjusted in case we get back into the loop above.
|
---|
1924 | index += pos*(after.d->size - 1);
|
---|
1925 | }
|
---|
1926 | return *this;
|
---|
1927 | }
|
---|
1928 |
|
---|
1929 | /*!
|
---|
1930 | \overload replace()
|
---|
1931 | Replaces every occurrence of the character \a before with the
|
---|
1932 | character \a after and returns a reference to this string.
|
---|
1933 |
|
---|
1934 | If \a cs is Qt::CaseSensitive (default), the search is case
|
---|
1935 | sensitive; otherwise the search is case insensitive.
|
---|
1936 | */
|
---|
1937 | QString& QString::replace(QChar before, QChar after, Qt::CaseSensitivity cs)
|
---|
1938 | {
|
---|
1939 | ushort a = after.unicode();
|
---|
1940 | ushort b = before.unicode();
|
---|
1941 | if (d->size) {
|
---|
1942 | detach();
|
---|
1943 | ushort *i = d->data;
|
---|
1944 | const ushort *e = i + d->size;
|
---|
1945 | if (cs == Qt::CaseSensitive) {
|
---|
1946 | for (; i != e; ++i)
|
---|
1947 | if (*i == b)
|
---|
1948 | *i = a;
|
---|
1949 | } else {
|
---|
1950 | b = foldCase(b);
|
---|
1951 | for (; i != e; ++i)
|
---|
1952 | if (foldCase(*i) == b)
|
---|
1953 | *i = a;
|
---|
1954 | }
|
---|
1955 | }
|
---|
1956 | return *this;
|
---|
1957 | }
|
---|
1958 |
|
---|
1959 | /*!
|
---|
1960 | \since 4.5
|
---|
1961 | \overload replace()
|
---|
1962 |
|
---|
1963 | Replaces every occurrence of the string \a before with the string \a
|
---|
1964 | after and returns a reference to this string.
|
---|
1965 |
|
---|
1966 | If \a cs is Qt::CaseSensitive (default), the search is case
|
---|
1967 | sensitive; otherwise the search is case insensitive.
|
---|
1968 |
|
---|
1969 | \note The text is not rescanned after a replacement.
|
---|
1970 | */
|
---|
1971 | QString &QString::replace(const QLatin1String &before,
|
---|
1972 | const QLatin1String &after,
|
---|
1973 | Qt::CaseSensitivity cs)
|
---|
1974 | {
|
---|
1975 | int alen = qstrlen(after.latin1());
|
---|
1976 | QVarLengthArray<ushort> a(alen);
|
---|
1977 | for (int i = 0; i < alen; ++i)
|
---|
1978 | a[i] = (uchar)after.latin1()[i];
|
---|
1979 | int blen = qstrlen(before.latin1());
|
---|
1980 | QVarLengthArray<ushort> b(blen);
|
---|
1981 | for (int i = 0; i < blen; ++i)
|
---|
1982 | b[i] = (uchar)before.latin1()[i];
|
---|
1983 | return replace((const QChar *)b.data(), blen, (const QChar *)a.data(), alen, cs);
|
---|
1984 | }
|
---|
1985 |
|
---|
1986 | /*!
|
---|
1987 | \since 4.5
|
---|
1988 | \overload replace()
|
---|
1989 |
|
---|
1990 | Replaces every occurrence of the string \a before with the string \a
|
---|
1991 | after and returns a reference to this string.
|
---|
1992 |
|
---|
1993 | If \a cs is Qt::CaseSensitive (default), the search is case
|
---|
1994 | sensitive; otherwise the search is case insensitive.
|
---|
1995 |
|
---|
1996 | \note The text is not rescanned after a replacement.
|
---|
1997 | */
|
---|
1998 | QString &QString::replace(const QLatin1String &before,
|
---|
1999 | const QString &after,
|
---|
2000 | Qt::CaseSensitivity cs)
|
---|
2001 | {
|
---|
2002 | int blen = qstrlen(before.latin1());
|
---|
2003 | QVarLengthArray<ushort> b(blen);
|
---|
2004 | for (int i = 0; i < blen; ++i)
|
---|
2005 | b[i] = (uchar)before.latin1()[i];
|
---|
2006 | return replace((const QChar *)b.data(), blen, after.constData(), after.d->size, cs);
|
---|
2007 | }
|
---|
2008 |
|
---|
2009 | /*!
|
---|
2010 | \since 4.5
|
---|
2011 | \overload replace()
|
---|
2012 |
|
---|
2013 | Replaces every occurrence of the string \a before with the string \a
|
---|
2014 | after and returns a reference to this string.
|
---|
2015 |
|
---|
2016 | If \a cs is Qt::CaseSensitive (default), the search is case
|
---|
2017 | sensitive; otherwise the search is case insensitive.
|
---|
2018 |
|
---|
2019 | \note The text is not rescanned after a replacement.
|
---|
2020 | */
|
---|
2021 | QString &QString::replace(const QString &before,
|
---|
2022 | const QLatin1String &after,
|
---|
2023 | Qt::CaseSensitivity cs)
|
---|
2024 | {
|
---|
2025 | int alen = qstrlen(after.latin1());
|
---|
2026 | QVarLengthArray<ushort> a(alen);
|
---|
2027 | for (int i = 0; i < alen; ++i)
|
---|
2028 | a[i] = (uchar)after.latin1()[i];
|
---|
2029 | return replace(before.constData(), before.d->size, (const QChar *)a.data(), alen, cs);
|
---|
2030 | }
|
---|
2031 |
|
---|
2032 | /*!
|
---|
2033 | \since 4.5
|
---|
2034 | \overload replace()
|
---|
2035 |
|
---|
2036 | Replaces every occurrence of the character \a c with the string \a
|
---|
2037 | after and returns a reference to this string.
|
---|
2038 |
|
---|
2039 | If \a cs is Qt::CaseSensitive (default), the search is case
|
---|
2040 | sensitive; otherwise the search is case insensitive.
|
---|
2041 |
|
---|
2042 | \note The text is not rescanned after a replacement.
|
---|
2043 | */
|
---|
2044 | QString &QString::replace(QChar c, const QLatin1String &after, Qt::CaseSensitivity cs)
|
---|
2045 | {
|
---|
2046 | int alen = qstrlen(after.latin1());
|
---|
2047 | QVarLengthArray<ushort> a(alen);
|
---|
2048 | for (int i = 0; i < alen; ++i)
|
---|
2049 | a[i] = (uchar)after.latin1()[i];
|
---|
2050 | return replace(&c, 1, (const QChar *)a.data(), alen, cs);
|
---|
2051 | }
|
---|
2052 |
|
---|
2053 |
|
---|
2054 | /*!
|
---|
2055 | Returns true if string \a other is equal to this string; otherwise
|
---|
2056 | returns false.
|
---|
2057 |
|
---|
2058 | The comparison is based exclusively on the numeric Unicode values of
|
---|
2059 | the characters and is very fast, but is not what a human would
|
---|
2060 | expect. Consider sorting user-interface strings with
|
---|
2061 | localeAwareCompare().
|
---|
2062 | */
|
---|
2063 | bool QString::operator==(const QString &other) const
|
---|
2064 | {
|
---|
2065 | if (d->size != other.d->size)
|
---|
2066 | return false;
|
---|
2067 |
|
---|
2068 | return qMemEquals(d->data, other.d->data, d->size);
|
---|
2069 | }
|
---|
2070 |
|
---|
2071 | /*!
|
---|
2072 | \overload operator==()
|
---|
2073 | */
|
---|
2074 | bool QString::operator==(const QLatin1String &other) const
|
---|
2075 | {
|
---|
2076 | const ushort *uc = d->data;
|
---|
2077 | const ushort *e = uc + d->size;
|
---|
2078 | const uchar *c = (uchar *)other.latin1();
|
---|
2079 |
|
---|
2080 | if (!c)
|
---|
2081 | return isEmpty();
|
---|
2082 |
|
---|
2083 | while (*c) {
|
---|
2084 | if (uc == e || *uc != *c)
|
---|
2085 | return false;
|
---|
2086 | ++uc;
|
---|
2087 | ++c;
|
---|
2088 | }
|
---|
2089 | return (uc == e);
|
---|
2090 | }
|
---|
2091 |
|
---|
2092 | /*! \fn bool QString::operator==(const QByteArray &other) const
|
---|
2093 |
|
---|
2094 | \overload operator==()
|
---|
2095 |
|
---|
2096 | The \a other byte array is converted to a QString using the
|
---|
2097 | fromAscii() function.
|
---|
2098 |
|
---|
2099 | You can disable this operator by defining \c
|
---|
2100 | QT_NO_CAST_FROM_ASCII when you compile your applications. This
|
---|
2101 | can be useful if you want to ensure that all user-visible strings
|
---|
2102 | go through QObject::tr(), for example.
|
---|
2103 | */
|
---|
2104 |
|
---|
2105 | /*! \fn bool QString::operator==(const char *other) const
|
---|
2106 |
|
---|
2107 | \overload operator==()
|
---|
2108 |
|
---|
2109 | The \a other const char pointer is converted to a QString using
|
---|
2110 | the fromAscii() function.
|
---|
2111 |
|
---|
2112 | You can disable this operator by defining \c
|
---|
2113 | QT_NO_CAST_FROM_ASCII when you compile your applications. This
|
---|
2114 | can be useful if you want to ensure that all user-visible strings
|
---|
2115 | go through QObject::tr(), for example.
|
---|
2116 | */
|
---|
2117 |
|
---|
2118 | /*!
|
---|
2119 | Returns true if this string is lexically less than string \a
|
---|
2120 | other; otherwise returns false.
|
---|
2121 |
|
---|
2122 | The comparison is based exclusively on the numeric Unicode values
|
---|
2123 | of the characters and is very fast, but is not what a human would
|
---|
2124 | expect. Consider sorting user-interface strings using the
|
---|
2125 | QString::localeAwareCompare() function.
|
---|
2126 | */
|
---|
2127 | bool QString::operator<(const QString &other) const
|
---|
2128 | {
|
---|
2129 | return ucstrcmp(constData(), length(), other.constData(), other.length()) < 0;
|
---|
2130 | }
|
---|
2131 |
|
---|
2132 | /*!
|
---|
2133 | \overload operator<()
|
---|
2134 | */
|
---|
2135 | bool QString::operator<(const QLatin1String &other) const
|
---|
2136 | {
|
---|
2137 | const ushort *uc = d->data;
|
---|
2138 | const ushort *e = uc + d->size;
|
---|
2139 | const uchar *c = (uchar *) other.latin1();
|
---|
2140 |
|
---|
2141 | if (!c || *c == 0)
|
---|
2142 | return false;
|
---|
2143 |
|
---|
2144 | while (*c) {
|
---|
2145 | if (uc == e || *uc != *c)
|
---|
2146 | break;
|
---|
2147 | ++uc;
|
---|
2148 | ++c;
|
---|
2149 | }
|
---|
2150 | return (uc == e ? *c : *uc < *c);
|
---|
2151 | }
|
---|
2152 |
|
---|
2153 | /*! \fn bool QString::operator<(const QByteArray &other) const
|
---|
2154 |
|
---|
2155 | \overload operator<()
|
---|
2156 |
|
---|
2157 | The \a other byte array is converted to a QString using the
|
---|
2158 | fromAscii() function.
|
---|
2159 |
|
---|
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.
|
---|
2164 | */
|
---|
2165 |
|
---|
2166 | /*! \fn bool QString::operator<(const char *other) const
|
---|
2167 |
|
---|
2168 | \overload operator<()
|
---|
2169 |
|
---|
2170 | The \a other const char pointer is converted to a QString using
|
---|
2171 | the fromAscii() function.
|
---|
2172 |
|
---|
2173 | You can disable this operator by defining \c
|
---|
2174 | QT_NO_CAST_FROM_ASCII when you compile your applications. This
|
---|
2175 | can be useful if you want to ensure that all user-visible strings
|
---|
2176 | go through QObject::tr(), for example.
|
---|
2177 | */
|
---|
2178 |
|
---|
2179 | /*! \fn bool QString::operator<=(const QString &other) const
|
---|
2180 |
|
---|
2181 | Returns true if this string is lexically less than or equal to
|
---|
2182 | string \a other; otherwise returns false.
|
---|
2183 |
|
---|
2184 | The comparison is based exclusively on the numeric Unicode values
|
---|
2185 | of the characters and is very fast, but is not what a human would
|
---|
2186 | expect. Consider sorting user-interface strings with
|
---|
2187 | localeAwareCompare().
|
---|
2188 | */
|
---|
2189 |
|
---|
2190 | /*! \fn bool QString::operator<=(const QLatin1String &other) const
|
---|
2191 |
|
---|
2192 | \overload operator<=()
|
---|
2193 | */
|
---|
2194 |
|
---|
2195 | /*! \fn bool QString::operator<=(const QByteArray &other) const
|
---|
2196 |
|
---|
2197 | \overload operator<=()
|
---|
2198 |
|
---|
2199 | The \a other byte array is converted to a QString using the
|
---|
2200 | fromAscii() function.
|
---|
2201 |
|
---|
2202 | You can disable this operator by defining \c
|
---|
2203 | QT_NO_CAST_FROM_ASCII when you compile your applications. This
|
---|
2204 | can be useful if you want to ensure that all user-visible strings
|
---|
2205 | go through QObject::tr(), for example.
|
---|
2206 | */
|
---|
2207 |
|
---|
2208 | /*! \fn bool QString::operator<=(const char *other) const
|
---|
2209 |
|
---|
2210 | \overload operator<=()
|
---|
2211 |
|
---|
2212 | The \a other const char pointer is converted to a QString using
|
---|
2213 | the fromAscii() function.
|
---|
2214 |
|
---|
2215 | You can disable this operator by defining \c
|
---|
2216 | QT_NO_CAST_FROM_ASCII when you compile your applications. This
|
---|
2217 | can be useful if you want to ensure that all user-visible strings
|
---|
2218 | go through QObject::tr(), for example.
|
---|
2219 | */
|
---|
2220 |
|
---|
2221 | /*! \fn bool QString::operator>(const QString &other) const
|
---|
2222 |
|
---|
2223 | Returns true if this string is lexically greater than string \a
|
---|
2224 | other; otherwise returns false.
|
---|
2225 |
|
---|
2226 | The comparison is based exclusively on the numeric Unicode values
|
---|
2227 | of the characters and is very fast, but is not what a human would
|
---|
2228 | expect. Consider sorting user-interface strings with
|
---|
2229 | localeAwareCompare().
|
---|
2230 | */
|
---|
2231 |
|
---|
2232 | /*!
|
---|
2233 | \overload operator>()
|
---|
2234 | */
|
---|
2235 | bool QString::operator>(const QLatin1String &other) const
|
---|
2236 | {
|
---|
2237 | const ushort *uc = d->data;;
|
---|
2238 | const ushort *e = uc + d->size;
|
---|
2239 | const uchar *c = (uchar *) other.latin1();
|
---|
2240 |
|
---|
2241 | if (!c || *c == '\0')
|
---|
2242 | return !isEmpty();
|
---|
2243 |
|
---|
2244 | while (*c) {
|
---|
2245 | if (uc == e || *uc != *c)
|
---|
2246 | break;
|
---|
2247 | ++uc;
|
---|
2248 | ++c;
|
---|
2249 | }
|
---|
2250 | return (uc == e ? false : *uc > *c);
|
---|
2251 | }
|
---|
2252 |
|
---|
2253 | /*! \fn bool QString::operator>(const QByteArray &other) const
|
---|
2254 |
|
---|
2255 | \overload operator>()
|
---|
2256 |
|
---|
2257 | The \a other byte array is converted to a QString using the
|
---|
2258 | fromAscii() function.
|
---|
2259 |
|
---|
2260 | You can disable this operator by defining \c
|
---|
2261 | QT_NO_CAST_FROM_ASCII when you compile your applications. This
|
---|
2262 | can be useful if you want to ensure that all user-visible strings
|
---|
2263 | go through QObject::tr(), for example.
|
---|
2264 | */
|
---|
2265 |
|
---|
2266 | /*! \fn bool QString::operator>(const char *other) const
|
---|
2267 |
|
---|
2268 | \overload operator>()
|
---|
2269 |
|
---|
2270 | The \a other const char pointer is converted to a QString using
|
---|
2271 | the fromAscii() function.
|
---|
2272 |
|
---|
2273 | You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII
|
---|
2274 | when you compile your applications. This can be useful if you want
|
---|
2275 | to ensure that all user-visible strings go through QObject::tr(),
|
---|
2276 | for example.
|
---|
2277 | */
|
---|
2278 |
|
---|
2279 | /*! \fn bool QString::operator>=(const QString &other) const
|
---|
2280 |
|
---|
2281 | Returns true if this string is lexically greater than or equal to
|
---|
2282 | string \a other; otherwise returns false.
|
---|
2283 |
|
---|
2284 | The comparison is based exclusively on the numeric Unicode values
|
---|
2285 | of the characters and is very fast, but is not what a human would
|
---|
2286 | expect. Consider sorting user-interface strings with
|
---|
2287 | localeAwareCompare().
|
---|
2288 | */
|
---|
2289 |
|
---|
2290 | /*! \fn bool QString::operator>=(const QLatin1String &other) const
|
---|
2291 |
|
---|
2292 | \overload operator>=()
|
---|
2293 | */
|
---|
2294 |
|
---|
2295 | /*! \fn bool QString::operator>=(const QByteArray &other) const
|
---|
2296 |
|
---|
2297 | \overload operator>=()
|
---|
2298 |
|
---|
2299 | The \a other byte array is converted to a QString using the
|
---|
2300 | fromAscii() function.
|
---|
2301 |
|
---|
2302 | You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII
|
---|
2303 | when you compile your applications. This can be useful if you want
|
---|
2304 | to ensure that all user-visible strings go through QObject::tr(),
|
---|
2305 | for example.
|
---|
2306 | */
|
---|
2307 |
|
---|
2308 | /*! \fn bool QString::operator>=(const char *other) const
|
---|
2309 |
|
---|
2310 | \overload operator>=()
|
---|
2311 |
|
---|
2312 | The \a other const char pointer is converted to a QString using
|
---|
2313 | the fromAscii() function.
|
---|
2314 |
|
---|
2315 | You can disable this operator by defining \c
|
---|
2316 | QT_NO_CAST_FROM_ASCII when you compile your applications. This
|
---|
2317 | can be useful if you want to ensure that all user-visible strings
|
---|
2318 | go through QObject::tr(), for example.
|
---|
2319 | */
|
---|
2320 |
|
---|
2321 | /*! \fn bool QString::operator!=(const QString &other) const
|
---|
2322 |
|
---|
2323 | Returns true if this string is not equal to string \a other;
|
---|
2324 | otherwise returns false.
|
---|
2325 |
|
---|
2326 | The comparison is based exclusively on the numeric Unicode values
|
---|
2327 | of the characters and is very fast, but is not what a human would
|
---|
2328 | expect. Consider sorting user-interface strings with
|
---|
2329 | localeAwareCompare().
|
---|
2330 | */
|
---|
2331 |
|
---|
2332 | /*! \fn bool QString::operator!=(const QLatin1String &other) const
|
---|
2333 |
|
---|
2334 | \overload operator!=()
|
---|
2335 | */
|
---|
2336 |
|
---|
2337 | /*! \fn bool QString::operator!=(const QByteArray &other) const
|
---|
2338 |
|
---|
2339 | \overload operator!=()
|
---|
2340 |
|
---|
2341 | The \a other byte array is converted to a QString using the
|
---|
2342 | fromAscii() function.
|
---|
2343 |
|
---|
2344 | You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII
|
---|
2345 | when you compile your applications. This can be useful if you want
|
---|
2346 | to ensure that all user-visible strings go through QObject::tr(),
|
---|
2347 | for example.
|
---|
2348 | */
|
---|
2349 |
|
---|
2350 | /*! \fn bool QString::operator!=(const char *other) const
|
---|
2351 |
|
---|
2352 | \overload operator!=()
|
---|
2353 |
|
---|
2354 | The \a other const char pointer is converted to a QString using
|
---|
2355 | the fromAscii() function.
|
---|
2356 |
|
---|
2357 | You can disable this operator by defining \c
|
---|
2358 | QT_NO_CAST_FROM_ASCII when you compile your applications. This
|
---|
2359 | can be useful if you want to ensure that all user-visible strings
|
---|
2360 | go through QObject::tr(), for example.
|
---|
2361 | */
|
---|
2362 |
|
---|
2363 | /*!
|
---|
2364 | Returns the index position of the first occurrence of the string \a
|
---|
2365 | str in this string, searching forward from index position \a
|
---|
2366 | from. Returns -1 if \a str is not found.
|
---|
2367 |
|
---|
2368 | If \a cs is Qt::CaseSensitive (default), the search is case
|
---|
2369 | sensitive; otherwise the search is case insensitive.
|
---|
2370 |
|
---|
2371 | Example:
|
---|
2372 |
|
---|
2373 | \snippet doc/src/snippets/qstring/main.cpp 24
|
---|
2374 |
|
---|
2375 | If \a from is -1, the search starts at the last character; if it is
|
---|
2376 | -2, at the next to last character and so on.
|
---|
2377 |
|
---|
2378 | \sa lastIndexOf(), contains(), count()
|
---|
2379 | */
|
---|
2380 | int QString::indexOf(const QString &str, int from, Qt::CaseSensitivity cs) const
|
---|
2381 | {
|
---|
2382 | return qFindString(unicode(), length(), from, str.unicode(), str.length(), cs);
|
---|
2383 | }
|
---|
2384 |
|
---|
2385 | /*!
|
---|
2386 | \since 4.5
|
---|
2387 | Returns the index position of the first occurrence of the string \a
|
---|
2388 | str in this string, searching forward from index position \a
|
---|
2389 | from. Returns -1 if \a str is not found.
|
---|
2390 |
|
---|
2391 | If \a cs is Qt::CaseSensitive (default), the search is case
|
---|
2392 | sensitive; otherwise the search is case insensitive.
|
---|
2393 |
|
---|
2394 | Example:
|
---|
2395 |
|
---|
2396 | \snippet doc/src/snippets/qstring/main.cpp 24
|
---|
2397 |
|
---|
2398 | If \a from is -1, the search starts at the last character; if it is
|
---|
2399 | -2, at the next to last character and so on.
|
---|
2400 |
|
---|
2401 | \sa lastIndexOf(), contains(), count()
|
---|
2402 | */
|
---|
2403 | int QString::indexOf(const QLatin1String &str, int from, Qt::CaseSensitivity cs) const
|
---|
2404 | {
|
---|
2405 | int len = qstrlen(str.latin1());
|
---|
2406 | QVarLengthArray<ushort> s(len);
|
---|
2407 | for (int i = 0; i < len; ++i)
|
---|
2408 | s[i] = str.latin1()[i];
|
---|
2409 |
|
---|
2410 | return qFindString(unicode(), length(), from, (const QChar *)s.data(), len, cs);
|
---|
2411 | }
|
---|
2412 |
|
---|
2413 | int qFindString(
|
---|
2414 | const QChar *haystack0, int haystackLen, int from,
|
---|
2415 | const QChar *needle0, int needleLen, Qt::CaseSensitivity cs)
|
---|
2416 | {
|
---|
2417 | const int l = haystackLen;
|
---|
2418 | const int sl = needleLen;
|
---|
2419 | if (from < 0)
|
---|
2420 | from += l;
|
---|
2421 | if (uint(sl + from) > (uint)l)
|
---|
2422 | return -1;
|
---|
2423 | if (!sl)
|
---|
2424 | return from;
|
---|
2425 | if (!l)
|
---|
2426 | return -1;
|
---|
2427 |
|
---|
2428 | if (sl == 1)
|
---|
2429 | return findChar(haystack0, haystackLen, needle0[0], from, cs);
|
---|
2430 |
|
---|
2431 | /*
|
---|
2432 | We use the Boyer-Moore algorithm in cases where the overhead
|
---|
2433 | for the skip table should pay off, otherwise we use a simple
|
---|
2434 | hash function.
|
---|
2435 | */
|
---|
2436 | if (l > 500 && sl > 5)
|
---|
2437 | return qFindStringBoyerMoore(haystack0, haystackLen, from,
|
---|
2438 | needle0, needleLen, cs);
|
---|
2439 |
|
---|
2440 | /*
|
---|
2441 | We use some hashing for efficiency's sake. Instead of
|
---|
2442 | comparing strings, we compare the hash value of str with that
|
---|
2443 | of a part of this QString. Only if that matches, we call
|
---|
2444 | ucstrncmp() or ucstrnicmp().
|
---|
2445 | */
|
---|
2446 | const ushort *needle = (const ushort *)needle0;
|
---|
2447 | const ushort *haystack = (const ushort *)haystack0 + from;
|
---|
2448 | const ushort *end = (const ushort *)haystack0 + (l-sl);
|
---|
2449 | const int sl_minus_1 = sl-1;
|
---|
2450 | int hashNeedle = 0, hashHaystack = 0, idx;
|
---|
2451 |
|
---|
2452 | if (cs == Qt::CaseSensitive) {
|
---|
2453 | for (idx = 0; idx < sl; ++idx) {
|
---|
2454 | hashNeedle = ((hashNeedle<<1) + needle[idx]);
|
---|
2455 | hashHaystack = ((hashHaystack<<1) + haystack[idx]);
|
---|
2456 | }
|
---|
2457 | hashHaystack -= haystack[sl_minus_1];
|
---|
2458 |
|
---|
2459 | while (haystack <= end) {
|
---|
2460 | hashHaystack += haystack[sl_minus_1];
|
---|
2461 | if (hashHaystack == hashNeedle
|
---|
2462 | && ucstrncmp((const QChar *)needle, (const QChar *)haystack, sl) == 0)
|
---|
2463 | return haystack - (const ushort *)haystack0;
|
---|
2464 |
|
---|
2465 | REHASH(*haystack);
|
---|
2466 | ++haystack;
|
---|
2467 | }
|
---|
2468 | } else {
|
---|
2469 | const ushort *haystack_start = (const ushort *)haystack0;
|
---|
2470 | for (idx = 0; idx < sl; ++idx) {
|
---|
2471 | hashNeedle = (hashNeedle<<1) + foldCase(needle + idx, needle);
|
---|
2472 | hashHaystack = (hashHaystack<<1) + foldCase(haystack + idx, haystack_start);
|
---|
2473 | }
|
---|
2474 | hashHaystack -= foldCase(haystack + sl_minus_1, haystack_start);
|
---|
2475 |
|
---|
2476 | while (haystack <= end) {
|
---|
2477 | hashHaystack += foldCase(haystack + sl_minus_1, haystack_start);
|
---|
2478 | if (hashHaystack == hashNeedle && ucstrnicmp(needle, haystack, sl) == 0)
|
---|
2479 | return haystack - (const ushort *)haystack0;
|
---|
2480 |
|
---|
2481 | REHASH(foldCase(haystack, haystack_start));
|
---|
2482 | ++haystack;
|
---|
2483 | }
|
---|
2484 | }
|
---|
2485 | return -1;
|
---|
2486 | }
|
---|
2487 |
|
---|
2488 | /*!
|
---|
2489 | \overload indexOf()
|
---|
2490 |
|
---|
2491 | Returns the index position of the first occurrence of the
|
---|
2492 | character \a ch in the string, searching forward from index
|
---|
2493 | position \a from. Returns -1 if \a ch could not be found.
|
---|
2494 | */
|
---|
2495 | int QString::indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
|
---|
2496 | {
|
---|
2497 | return findChar(unicode(), length(), ch, from, cs);
|
---|
2498 | }
|
---|
2499 |
|
---|
2500 | static int lastIndexOfHelper(const ushort *haystack, int from, const ushort *needle, int sl, Qt::CaseSensitivity cs)
|
---|
2501 | {
|
---|
2502 | /*
|
---|
2503 | See indexOf() for explanations.
|
---|
2504 | */
|
---|
2505 |
|
---|
2506 | const ushort *end = haystack;
|
---|
2507 | haystack += from;
|
---|
2508 | const int sl_minus_1 = sl-1;
|
---|
2509 | const ushort *n = needle+sl_minus_1;
|
---|
2510 | const ushort *h = haystack+sl_minus_1;
|
---|
2511 | int hashNeedle = 0, hashHaystack = 0, idx;
|
---|
2512 |
|
---|
2513 | if (cs == Qt::CaseSensitive) {
|
---|
2514 | for (idx = 0; idx < sl; ++idx) {
|
---|
2515 | hashNeedle = ((hashNeedle<<1) + *(n-idx));
|
---|
2516 | hashHaystack = ((hashHaystack<<1) + *(h-idx));
|
---|
2517 | }
|
---|
2518 | hashHaystack -= *haystack;
|
---|
2519 |
|
---|
2520 | while (haystack >= end) {
|
---|
2521 | hashHaystack += *haystack;
|
---|
2522 | if (hashHaystack == hashNeedle
|
---|
2523 | && ucstrncmp((const QChar *)needle, (const QChar *)haystack, sl) == 0)
|
---|
2524 | return haystack - end;
|
---|
2525 | --haystack;
|
---|
2526 | REHASH(haystack[sl]);
|
---|
2527 | }
|
---|
2528 | } else {
|
---|
2529 | for (idx = 0; idx < sl; ++idx) {
|
---|
2530 | hashNeedle = ((hashNeedle<<1) + foldCase(n-idx, needle));
|
---|
2531 | hashHaystack = ((hashHaystack<<1) + foldCase(h-idx, end));
|
---|
2532 | }
|
---|
2533 | hashHaystack -= foldCase(haystack, end);
|
---|
2534 |
|
---|
2535 | while (haystack >= end) {
|
---|
2536 | hashHaystack += foldCase(haystack, end);
|
---|
2537 | if (hashHaystack == hashNeedle && ucstrnicmp(needle, haystack, sl) == 0)
|
---|
2538 | return haystack - end;
|
---|
2539 | --haystack;
|
---|
2540 | REHASH(foldCase(haystack + sl, end));
|
---|
2541 | }
|
---|
2542 | }
|
---|
2543 | return -1;
|
---|
2544 | }
|
---|
2545 |
|
---|
2546 | /*!
|
---|
2547 | Returns the index position of the last occurrence of the string \a
|
---|
2548 | str in this string, searching backward from index position \a
|
---|
2549 | from. If \a from is -1 (default), the search starts at the last
|
---|
2550 | character; if \a from is -2, at the next to last character and so
|
---|
2551 | on. Returns -1 if \a str is not found.
|
---|
2552 |
|
---|
2553 | If \a cs is Qt::CaseSensitive (default), the search is case
|
---|
2554 | sensitive; otherwise the search is case insensitive.
|
---|
2555 |
|
---|
2556 | Example:
|
---|
2557 |
|
---|
2558 | \snippet doc/src/snippets/qstring/main.cpp 29
|
---|
2559 |
|
---|
2560 | \sa indexOf(), contains(), count()
|
---|
2561 | */
|
---|
2562 | int QString::lastIndexOf(const QString &str, int from, Qt::CaseSensitivity cs) const
|
---|
2563 | {
|
---|
2564 | const int sl = str.d->size;
|
---|
2565 | if (sl == 1)
|
---|
2566 | return lastIndexOf(QChar(str.d->data[0]), from, cs);
|
---|
2567 |
|
---|
2568 | const int l = d->size;
|
---|
2569 | if (from < 0)
|
---|
2570 | from += l;
|
---|
2571 | int delta = l-sl;
|
---|
2572 | if (from == l && sl == 0)
|
---|
2573 | return from;
|
---|
2574 | if (from < 0 || from >= l || delta < 0)
|
---|
2575 | return -1;
|
---|
2576 | if (from > delta)
|
---|
2577 | from = delta;
|
---|
2578 |
|
---|
2579 |
|
---|
2580 | return lastIndexOfHelper(d->data, from, str.d->data, str.d->size, cs);
|
---|
2581 | }
|
---|
2582 |
|
---|
2583 | /*!
|
---|
2584 | \since 4.5
|
---|
2585 | Returns the index position of the last occurrence of the string \a
|
---|
2586 | str in this string, searching backward from index position \a
|
---|
2587 | from. If \a from is -1 (default), the search starts at the last
|
---|
2588 | character; if \a from is -2, at the next to last character and so
|
---|
2589 | on. Returns -1 if \a str is not found.
|
---|
2590 |
|
---|
2591 | If \a cs is Qt::CaseSensitive (default), the search is case
|
---|
2592 | sensitive; otherwise the search is case insensitive.
|
---|
2593 |
|
---|
2594 | Example:
|
---|
2595 |
|
---|
2596 | \snippet doc/src/snippets/qstring/main.cpp 29
|
---|
2597 |
|
---|
2598 | \sa indexOf(), contains(), count()
|
---|
2599 | */
|
---|
2600 | int QString::lastIndexOf(const QLatin1String &str, int from, Qt::CaseSensitivity cs) const
|
---|
2601 | {
|
---|
2602 | const int sl = qstrlen(str.latin1());
|
---|
2603 | if (sl == 1)
|
---|
2604 | return lastIndexOf(QLatin1Char(str.latin1()[0]), from, cs);
|
---|
2605 |
|
---|
2606 | const int l = d->size;
|
---|
2607 | if (from < 0)
|
---|
2608 | from += l;
|
---|
2609 | int delta = l-sl;
|
---|
2610 | if (from == l && sl == 0)
|
---|
2611 | return from;
|
---|
2612 | if (from < 0 || from >= l || delta < 0)
|
---|
2613 | return -1;
|
---|
2614 | if (from > delta)
|
---|
2615 | from = delta;
|
---|
2616 |
|
---|
2617 | QVarLengthArray<ushort> s(sl);
|
---|
2618 | for (int i = 0; i < sl; ++i)
|
---|
2619 | s[i] = str.latin1()[i];
|
---|
2620 |
|
---|
2621 | return lastIndexOfHelper(d->data, from, s.data(), sl, cs);
|
---|
2622 | }
|
---|
2623 |
|
---|
2624 | /*!
|
---|
2625 | \overload lastIndexOf()
|
---|
2626 |
|
---|
2627 | Returns the index position of the last occurrence of the character
|
---|
2628 | \a ch, searching backward from position \a from.
|
---|
2629 | */
|
---|
2630 | int QString::lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
|
---|
2631 | {
|
---|
2632 | ushort c = ch.unicode();
|
---|
2633 | if (from < 0)
|
---|
2634 | from += d->size;
|
---|
2635 | if (from < 0 || from >= d->size)
|
---|
2636 | return -1;
|
---|
2637 | if (from >= 0) {
|
---|
2638 | const ushort *n = d->data + from;
|
---|
2639 | const ushort *b = d->data;
|
---|
2640 | if (cs == Qt::CaseSensitive) {
|
---|
2641 | for (; n >= b; --n)
|
---|
2642 | if (*n == c)
|
---|
2643 | return n - b;
|
---|
2644 | } else {
|
---|
2645 | c = foldCase(c);
|
---|
2646 | for (; n >= b; --n)
|
---|
2647 | if (foldCase(*n) == c)
|
---|
2648 | return n - b;
|
---|
2649 | }
|
---|
2650 | }
|
---|
2651 | return -1;
|
---|
2652 | }
|
---|
2653 |
|
---|
2654 | #ifndef QT_NO_REGEXP
|
---|
2655 | struct QStringCapture
|
---|
2656 | {
|
---|
2657 | int pos;
|
---|
2658 | int len;
|
---|
2659 | int no;
|
---|
2660 | };
|
---|
2661 |
|
---|
2662 | /*!
|
---|
2663 | \overload replace()
|
---|
2664 |
|
---|
2665 | Replaces every occurrence of the regular expression \a rx in the
|
---|
2666 | string with \a after. Returns a reference to the string. For
|
---|
2667 | example:
|
---|
2668 |
|
---|
2669 | \snippet doc/src/snippets/qstring/main.cpp 42
|
---|
2670 |
|
---|
2671 | For regular expressions containing \l{capturing parentheses},
|
---|
2672 | occurrences of \bold{\\1}, \bold{\\2}, ..., in \a after are replaced
|
---|
2673 | with \a{rx}.cap(1), cap(2), ...
|
---|
2674 |
|
---|
2675 | \snippet doc/src/snippets/qstring/main.cpp 43
|
---|
2676 |
|
---|
2677 | \sa indexOf(), lastIndexOf(), remove(), QRegExp::cap()
|
---|
2678 | */
|
---|
2679 | QString& QString::replace(const QRegExp &rx, const QString &after)
|
---|
2680 | {
|
---|
2681 | QRegExp rx2(rx);
|
---|
2682 |
|
---|
2683 | if (isEmpty() && rx2.indexIn(*this) == -1)
|
---|
2684 | return *this;
|
---|
2685 |
|
---|
2686 | realloc();
|
---|
2687 |
|
---|
2688 | int index = 0;
|
---|
2689 | int numCaptures = rx2.captureCount();
|
---|
2690 | int al = after.length();
|
---|
2691 | QRegExp::CaretMode caretMode = QRegExp::CaretAtZero;
|
---|
2692 |
|
---|
2693 | if (numCaptures > 0) {
|
---|
2694 | const QChar *uc = after.unicode();
|
---|
2695 | int numBackRefs = 0;
|
---|
2696 |
|
---|
2697 | for (int i = 0; i < al - 1; i++) {
|
---|
2698 | if (uc[i] == QLatin1Char('\\')) {
|
---|
2699 | int no = uc[i + 1].digitValue();
|
---|
2700 | if (no > 0 && no <= numCaptures)
|
---|
2701 | numBackRefs++;
|
---|
2702 | }
|
---|
2703 | }
|
---|
2704 |
|
---|
2705 | /*
|
---|
2706 | This is the harder case where we have back-references.
|
---|
2707 | */
|
---|
2708 | if (numBackRefs > 0) {
|
---|
2709 | QVarLengthArray<QStringCapture, 16> captures(numBackRefs);
|
---|
2710 | int j = 0;
|
---|
2711 |
|
---|
2712 | for (int i = 0; i < al - 1; i++) {
|
---|
2713 | if (uc[i] == QLatin1Char('\\')) {
|
---|
2714 | int no = uc[i + 1].digitValue();
|
---|
2715 | if (no > 0 && no <= numCaptures) {
|
---|
2716 | QStringCapture capture;
|
---|
2717 | capture.pos = i;
|
---|
2718 | capture.len = 2;
|
---|
2719 |
|
---|
2720 | if (i < al - 2) {
|
---|
2721 | int secondDigit = uc[i + 2].digitValue();
|
---|
2722 | if (secondDigit != -1 && ((no * 10) + secondDigit) <= numCaptures) {
|
---|
2723 | no = (no * 10) + secondDigit;
|
---|
2724 | ++capture.len;
|
---|
2725 | }
|
---|
2726 | }
|
---|
2727 |
|
---|
2728 | capture.no = no;
|
---|
2729 | captures[j++] = capture;
|
---|
2730 | }
|
---|
2731 | }
|
---|
2732 | }
|
---|
2733 |
|
---|
2734 | while (index <= length()) {
|
---|
2735 | index = rx2.indexIn(*this, index, caretMode);
|
---|
2736 | if (index == -1)
|
---|
2737 | break;
|
---|
2738 |
|
---|
2739 | QString after2(after);
|
---|
2740 | for (j = numBackRefs - 1; j >= 0; j--) {
|
---|
2741 | const QStringCapture &capture = captures[j];
|
---|
2742 | after2.replace(capture.pos, capture.len, rx2.cap(capture.no));
|
---|
2743 | }
|
---|
2744 |
|
---|
2745 | replace(index, rx2.matchedLength(), after2);
|
---|
2746 | index += after2.length();
|
---|
2747 |
|
---|
2748 | // avoid infinite loop on 0-length matches (e.g., QRegExp("[a-z]*"))
|
---|
2749 | if (rx2.matchedLength() == 0)
|
---|
2750 | ++index;
|
---|
2751 |
|
---|
2752 | caretMode = QRegExp::CaretWontMatch;
|
---|
2753 | }
|
---|
2754 | return *this;
|
---|
2755 | }
|
---|
2756 | }
|
---|
2757 |
|
---|
2758 | /*
|
---|
2759 | This is the simple and optimized case where we don't have
|
---|
2760 | back-references.
|
---|
2761 | */
|
---|
2762 | while (index != -1) {
|
---|
2763 | struct {
|
---|
2764 | int pos;
|
---|
2765 | int length;
|
---|
2766 | } replacements[2048];
|
---|
2767 |
|
---|
2768 | int pos = 0;
|
---|
2769 | int adjust = 0;
|
---|
2770 | while (pos < 2047) {
|
---|
2771 | index = rx2.indexIn(*this, index, caretMode);
|
---|
2772 | if (index == -1)
|
---|
2773 | break;
|
---|
2774 | int ml = rx2.matchedLength();
|
---|
2775 | replacements[pos].pos = index;
|
---|
2776 | replacements[pos++].length = ml;
|
---|
2777 | index += ml;
|
---|
2778 | adjust += al - ml;
|
---|
2779 | // avoid infinite loop
|
---|
2780 | if (!ml)
|
---|
2781 | index++;
|
---|
2782 | }
|
---|
2783 | if (!pos)
|
---|
2784 | break;
|
---|
2785 | replacements[pos].pos = d->size;
|
---|
2786 | int newlen = d->size + adjust;
|
---|
2787 |
|
---|
2788 | // to continue searching at the right position after we did
|
---|
2789 | // the first round of replacements
|
---|
2790 | if (index != -1)
|
---|
2791 | index += adjust;
|
---|
2792 | QString newstring;
|
---|
2793 | newstring.reserve(newlen + 1);
|
---|
2794 | QChar *newuc = newstring.data();
|
---|
2795 | QChar *uc = newuc;
|
---|
2796 | int copystart = 0;
|
---|
2797 | int i = 0;
|
---|
2798 | while (i < pos) {
|
---|
2799 | int copyend = replacements[i].pos;
|
---|
2800 | int size = copyend - copystart;
|
---|
2801 | memcpy(uc, d->data + copystart, size * sizeof(QChar));
|
---|
2802 | uc += size;
|
---|
2803 | memcpy(uc, after.d->data, al * sizeof(QChar));
|
---|
2804 | uc += al;
|
---|
2805 | copystart = copyend + replacements[i].length;
|
---|
2806 | i++;
|
---|
2807 | }
|
---|
2808 | memcpy(uc, d->data + copystart, (d->size - copystart) * sizeof(QChar));
|
---|
2809 | newstring.resize(newlen);
|
---|
2810 | *this = newstring;
|
---|
2811 | caretMode = QRegExp::CaretWontMatch;
|
---|
2812 | }
|
---|
2813 | return *this;
|
---|
2814 | }
|
---|
2815 | #endif
|
---|
2816 |
|
---|
2817 | /*!
|
---|
2818 | Returns the number of (potentially overlapping) occurrences of
|
---|
2819 | the string \a str in this string.
|
---|
2820 |
|
---|
2821 | If \a cs is Qt::CaseSensitive (default), the search is
|
---|
2822 | case sensitive; otherwise the search is case insensitive.
|
---|
2823 |
|
---|
2824 | \sa contains(), indexOf()
|
---|
2825 | */
|
---|
2826 | int QString::count(const QString &str, Qt::CaseSensitivity cs) const
|
---|
2827 | {
|
---|
2828 | int num = 0;
|
---|
2829 | int i = -1;
|
---|
2830 | if (d->size > 500 && str.d->size > 5) {
|
---|
2831 | QStringMatcher matcher(str, cs);
|
---|
2832 | while ((i = matcher.indexIn(*this, i + 1)) != -1)
|
---|
2833 | ++num;
|
---|
2834 | } else {
|
---|
2835 | while ((i = indexOf(str, i + 1, cs)) != -1)
|
---|
2836 | ++num;
|
---|
2837 | }
|
---|
2838 | return num;
|
---|
2839 | }
|
---|
2840 |
|
---|
2841 | /*!
|
---|
2842 | \overload count()
|
---|
2843 |
|
---|
2844 | Returns the number of occurrences of character \a ch in the string.
|
---|
2845 | */
|
---|
2846 | int QString::count(QChar ch, Qt::CaseSensitivity cs) const
|
---|
2847 | {
|
---|
2848 | ushort c = ch.unicode();
|
---|
2849 | int num = 0;
|
---|
2850 | const ushort *i = d->data + d->size;
|
---|
2851 | const ushort *b = d->data;
|
---|
2852 | if (cs == Qt::CaseSensitive) {
|
---|
2853 | while (i != b)
|
---|
2854 | if (*--i == c)
|
---|
2855 | ++num;
|
---|
2856 | } else {
|
---|
2857 | c = foldCase(c);
|
---|
2858 | while (i != b)
|
---|
2859 | if (foldCase(*(--i)) == c)
|
---|
2860 | ++num;
|
---|
2861 | }
|
---|
2862 | return num;
|
---|
2863 | }
|
---|
2864 |
|
---|
2865 | /*! \fn bool QString::contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
|
---|
2866 |
|
---|
2867 | Returns true if this string contains an occurrence of the string
|
---|
2868 | \a str; otherwise returns false.
|
---|
2869 |
|
---|
2870 | If \a cs is Qt::CaseSensitive (default), the search is
|
---|
2871 | case sensitive; otherwise the search is case insensitive.
|
---|
2872 |
|
---|
2873 | Example:
|
---|
2874 | \snippet doc/src/snippets/qstring/main.cpp 17
|
---|
2875 |
|
---|
2876 | \sa indexOf(), count()
|
---|
2877 | */
|
---|
2878 |
|
---|
2879 | /*! \fn bool QString::contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
|
---|
2880 |
|
---|
2881 | \overload contains()
|
---|
2882 |
|
---|
2883 | Returns true if this string contains an occurrence of the
|
---|
2884 | character \a ch; otherwise returns false.
|
---|
2885 | */
|
---|
2886 |
|
---|
2887 | /*! \fn bool QString::contains(const QRegExp &rx) const
|
---|
2888 |
|
---|
2889 | \overload contains()
|
---|
2890 |
|
---|
2891 | Returns true if the regular expression \a rx matches somewhere in
|
---|
2892 | this string; otherwise returns false.
|
---|
2893 | */
|
---|
2894 |
|
---|
2895 | /*! \fn bool QString::contains(QRegExp &rx) const
|
---|
2896 | \overload contains()
|
---|
2897 | \since 4.5
|
---|
2898 |
|
---|
2899 | Returns true if the regular expression \a rx matches somewhere in
|
---|
2900 | this string; otherwise returns false.
|
---|
2901 |
|
---|
2902 | If there is a match, the \a rx regular expression will contain the
|
---|
2903 | matched captures (see QRegExp::matchedLength, QRegExp::cap).
|
---|
2904 | */
|
---|
2905 |
|
---|
2906 | #ifndef QT_NO_REGEXP
|
---|
2907 | /*!
|
---|
2908 | \overload indexOf()
|
---|
2909 |
|
---|
2910 | Returns the index position of the first match of the regular
|
---|
2911 | expression \a rx in the string, searching forward from index
|
---|
2912 | position \a from. Returns -1 if \a rx didn't match anywhere.
|
---|
2913 |
|
---|
2914 | Example:
|
---|
2915 |
|
---|
2916 | \snippet doc/src/snippets/qstring/main.cpp 25
|
---|
2917 | */
|
---|
2918 | int QString::indexOf(const QRegExp& rx, int from) const
|
---|
2919 | {
|
---|
2920 | QRegExp rx2(rx);
|
---|
2921 | return rx2.indexIn(*this, from);
|
---|
2922 | }
|
---|
2923 |
|
---|
2924 | /*!
|
---|
2925 | \overload indexOf()
|
---|
2926 | \since 4.5
|
---|
2927 |
|
---|
2928 | Returns the index position of the first match of the regular
|
---|
2929 | expression \a rx in the string, searching forward from index
|
---|
2930 | position \a from. Returns -1 if \a rx didn't match anywhere.
|
---|
2931 |
|
---|
2932 | If there is a match, the \a rx regular expression will contain the
|
---|
2933 | matched captures (see QRegExp::matchedLength, QRegExp::cap).
|
---|
2934 |
|
---|
2935 | Example:
|
---|
2936 |
|
---|
2937 | \snippet doc/src/snippets/qstring/main.cpp 25
|
---|
2938 | */
|
---|
2939 | int QString::indexOf(QRegExp& rx, int from) const
|
---|
2940 | {
|
---|
2941 | return rx.indexIn(*this, from);
|
---|
2942 | }
|
---|
2943 |
|
---|
2944 | /*!
|
---|
2945 | \overload lastIndexOf()
|
---|
2946 |
|
---|
2947 | Returns the index position of the last match of the regular
|
---|
2948 | expression \a rx in the string, searching backward from index
|
---|
2949 | position \a from. Returns -1 if \a rx didn't match anywhere.
|
---|
2950 |
|
---|
2951 | Example:
|
---|
2952 |
|
---|
2953 | \snippet doc/src/snippets/qstring/main.cpp 30
|
---|
2954 | */
|
---|
2955 | int QString::lastIndexOf(const QRegExp& rx, int from) const
|
---|
2956 | {
|
---|
2957 | QRegExp rx2(rx);
|
---|
2958 | return rx2.lastIndexIn(*this, from);
|
---|
2959 | }
|
---|
2960 |
|
---|
2961 | /*!
|
---|
2962 | \overload lastIndexOf()
|
---|
2963 | \since 4.5
|
---|
2964 |
|
---|
2965 | Returns the index position of the last match of the regular
|
---|
2966 | expression \a rx in the string, searching backward from index
|
---|
2967 | position \a from. Returns -1 if \a rx didn't match anywhere.
|
---|
2968 |
|
---|
2969 | If there is a match, the \a rx regular expression will contain the
|
---|
2970 | matched captures (see QRegExp::matchedLength, QRegExp::cap).
|
---|
2971 |
|
---|
2972 | Example:
|
---|
2973 |
|
---|
2974 | \snippet doc/src/snippets/qstring/main.cpp 30
|
---|
2975 | */
|
---|
2976 | int QString::lastIndexOf(QRegExp& rx, int from) const
|
---|
2977 | {
|
---|
2978 | return rx.lastIndexIn(*this, from);
|
---|
2979 | }
|
---|
2980 |
|
---|
2981 | /*!
|
---|
2982 | \overload count()
|
---|
2983 |
|
---|
2984 | Returns the number of times the regular expression \a rx matches
|
---|
2985 | in the string.
|
---|
2986 |
|
---|
2987 | This function counts overlapping matches, so in the example
|
---|
2988 | below, there are four instances of "ana" or "ama":
|
---|
2989 |
|
---|
2990 | \snippet doc/src/snippets/qstring/main.cpp 18
|
---|
2991 |
|
---|
2992 | */
|
---|
2993 | int QString::count(const QRegExp& rx) const
|
---|
2994 | {
|
---|
2995 | QRegExp rx2(rx);
|
---|
2996 | int count = 0;
|
---|
2997 | int index = -1;
|
---|
2998 | int len = length();
|
---|
2999 | while (index < len - 1) { // count overlapping matches
|
---|
3000 | index = rx2.indexIn(*this, index + 1);
|
---|
3001 | if (index == -1)
|
---|
3002 | break;
|
---|
3003 | count++;
|
---|
3004 | }
|
---|
3005 | return count;
|
---|
3006 | }
|
---|
3007 | #endif // QT_NO_REGEXP
|
---|
3008 |
|
---|
3009 | /*! \fn int QString::count() const
|
---|
3010 |
|
---|
3011 | \overload count()
|
---|
3012 |
|
---|
3013 | Same as size().
|
---|
3014 | */
|
---|
3015 |
|
---|
3016 |
|
---|
3017 | /*!
|
---|
3018 | \enum QString::SectionFlag
|
---|
3019 |
|
---|
3020 | This enum specifies flags that can be used to affect various
|
---|
3021 | aspects of the section() function's behavior with respect to
|
---|
3022 | separators and empty fields.
|
---|
3023 |
|
---|
3024 | \value SectionDefault Empty fields are counted, leading and
|
---|
3025 | trailing separators are not included, and the separator is
|
---|
3026 | compared case sensitively.
|
---|
3027 |
|
---|
3028 | \value SectionSkipEmpty Treat empty fields as if they don't exist,
|
---|
3029 | i.e. they are not considered as far as \e start and \e end are
|
---|
3030 | concerned.
|
---|
3031 |
|
---|
3032 | \value SectionIncludeLeadingSep Include the leading separator (if
|
---|
3033 | any) in the result string.
|
---|
3034 |
|
---|
3035 | \value SectionIncludeTrailingSep Include the trailing separator
|
---|
3036 | (if any) in the result string.
|
---|
3037 |
|
---|
3038 | \value SectionCaseInsensitiveSeps Compare the separator
|
---|
3039 | case-insensitively.
|
---|
3040 |
|
---|
3041 | \sa section()
|
---|
3042 | */
|
---|
3043 |
|
---|
3044 | /*!
|
---|
3045 | \fn QString QString::section(QChar sep, int start, int end = -1, SectionFlags flags) const
|
---|
3046 |
|
---|
3047 | This function returns a section of the string.
|
---|
3048 |
|
---|
3049 | This string is treated as a sequence of fields separated by the
|
---|
3050 | character, \a sep. The returned string consists of the fields from
|
---|
3051 | position \a start to position \a end inclusive. If \a end is not
|
---|
3052 | specified, all fields from position \a start to the end of the
|
---|
3053 | string are included. Fields are numbered 0, 1, 2, etc., counting
|
---|
3054 | from the left, and -1, -2, etc., counting from right to left.
|
---|
3055 |
|
---|
3056 | The \a flags argument can be used to affect some aspects of the
|
---|
3057 | function's behavior, e.g. whether to be case sensitive, whether
|
---|
3058 | to skip empty fields and how to deal with leading and trailing
|
---|
3059 | separators; see \l{SectionFlags}.
|
---|
3060 |
|
---|
3061 | \snippet doc/src/snippets/qstring/main.cpp 52
|
---|
3062 |
|
---|
3063 | If \a start or \a end is negative, we count fields from the right
|
---|
3064 | of the string, the right-most field being -1, the one from
|
---|
3065 | right-most field being -2, and so on.
|
---|
3066 |
|
---|
3067 | \snippet doc/src/snippets/qstring/main.cpp 53
|
---|
3068 |
|
---|
3069 | \sa split()
|
---|
3070 | */
|
---|
3071 |
|
---|
3072 | /*!
|
---|
3073 | \overload section()
|
---|
3074 |
|
---|
3075 | \snippet doc/src/snippets/qstring/main.cpp 51
|
---|
3076 | \snippet doc/src/snippets/qstring/main.cpp 54
|
---|
3077 |
|
---|
3078 | \sa split()
|
---|
3079 | */
|
---|
3080 |
|
---|
3081 | QString QString::section(const QString &sep, int start, int end, SectionFlags flags) const
|
---|
3082 | {
|
---|
3083 | QStringList sections = split(sep, KeepEmptyParts,
|
---|
3084 | (flags & SectionCaseInsensitiveSeps) ? Qt::CaseInsensitive : Qt::CaseSensitive);
|
---|
3085 | if (sections.isEmpty())
|
---|
3086 | return QString();
|
---|
3087 | if (!(flags & SectionSkipEmpty)) {
|
---|
3088 | if (start < 0)
|
---|
3089 | start += sections.count();
|
---|
3090 | if (end < 0)
|
---|
3091 | end += sections.count();
|
---|
3092 | } else {
|
---|
3093 | int skip = 0;
|
---|
3094 | for (int k=0; k<sections.size(); ++k) {
|
---|
3095 | if (sections.at(k).isEmpty())
|
---|
3096 | skip++;
|
---|
3097 | }
|
---|
3098 | if (start < 0)
|
---|
3099 | start += sections.count() - skip;
|
---|
3100 | if (end < 0)
|
---|
3101 | end += sections.count() - skip;
|
---|
3102 | }
|
---|
3103 | int x = 0;
|
---|
3104 | QString ret;
|
---|
3105 | int first_i = start, last_i = end;
|
---|
3106 | for (int i = 0; x <= end && i < sections.size(); ++i) {
|
---|
3107 | QString section = sections.at(i);
|
---|
3108 | const bool empty = section.isEmpty();
|
---|
3109 | if (x >= start) {
|
---|
3110 | if(x == start)
|
---|
3111 | first_i = i;
|
---|
3112 | if(x == end)
|
---|
3113 | last_i = i;
|
---|
3114 | if(x > start)
|
---|
3115 | ret += sep;
|
---|
3116 | ret += section;
|
---|
3117 | }
|
---|
3118 | if (!empty || !(flags & SectionSkipEmpty))
|
---|
3119 | x++;
|
---|
3120 | }
|
---|
3121 | if((flags & SectionIncludeLeadingSep) && first_i)
|
---|
3122 | ret.prepend(sep);
|
---|
3123 | if((flags & SectionIncludeTrailingSep) && last_i < sections.size()-1)
|
---|
3124 | ret += sep;
|
---|
3125 | return ret;
|
---|
3126 | }
|
---|
3127 |
|
---|
3128 | #ifndef QT_NO_REGEXP
|
---|
3129 | class qt_section_chunk {
|
---|
3130 | public:
|
---|
3131 | qt_section_chunk(int l, QString s) { length = l; string = s; }
|
---|
3132 | int length;
|
---|
3133 | QString string;
|
---|
3134 | };
|
---|
3135 |
|
---|
3136 | /*!
|
---|
3137 | \overload section()
|
---|
3138 |
|
---|
3139 | This string is treated as a sequence of fields separated by the
|
---|
3140 | regular expression, \a reg.
|
---|
3141 |
|
---|
3142 | \snippet doc/src/snippets/qstring/main.cpp 55
|
---|
3143 |
|
---|
3144 | \warning Using this QRegExp version is much more expensive than
|
---|
3145 | the overloaded string and character versions.
|
---|
3146 |
|
---|
3147 | \sa split() simplified()
|
---|
3148 | */
|
---|
3149 | QString QString::section(const QRegExp ®, int start, int end, SectionFlags flags) const
|
---|
3150 | {
|
---|
3151 | const QChar *uc = unicode();
|
---|
3152 | if(!uc)
|
---|
3153 | return QString();
|
---|
3154 |
|
---|
3155 | QRegExp sep(reg);
|
---|
3156 | sep.setCaseSensitivity((flags & SectionCaseInsensitiveSeps) ? Qt::CaseInsensitive
|
---|
3157 | : Qt::CaseSensitive);
|
---|
3158 |
|
---|
3159 | QList<qt_section_chunk> sections;
|
---|
3160 | int n = length(), m = 0, last_m = 0, last_len = 0;
|
---|
3161 | while ((m = sep.indexIn(*this, m)) != -1) {
|
---|
3162 | sections.append(qt_section_chunk(last_len, QString(uc + last_m, m - last_m)));
|
---|
3163 | last_m = m;
|
---|
3164 | last_len = sep.matchedLength();
|
---|
3165 | m += qMax(sep.matchedLength(), 1);
|
---|
3166 | }
|
---|
3167 | sections.append(qt_section_chunk(last_len, QString(uc + last_m, n - last_m)));
|
---|
3168 |
|
---|
3169 | if(start < 0)
|
---|
3170 | start += sections.count();
|
---|
3171 | if(end < 0)
|
---|
3172 | end += sections.count();
|
---|
3173 |
|
---|
3174 | QString ret;
|
---|
3175 | int x = 0;
|
---|
3176 | int first_i = start, last_i = end;
|
---|
3177 | for (int i = 0; x <= end && i < sections.size(); ++i) {
|
---|
3178 | const qt_section_chunk §ion = sections.at(i);
|
---|
3179 | const bool empty = (section.length == section.string.length());
|
---|
3180 | if (x >= start) {
|
---|
3181 | if(x == start)
|
---|
3182 | first_i = i;
|
---|
3183 | if(x == end)
|
---|
3184 | last_i = i;
|
---|
3185 | if(x != start)
|
---|
3186 | ret += section.string;
|
---|
3187 | else
|
---|
3188 | ret += section.string.mid(section.length);
|
---|
3189 | }
|
---|
3190 | if (!empty || !(flags & SectionSkipEmpty))
|
---|
3191 | x++;
|
---|
3192 | }
|
---|
3193 | if((flags & SectionIncludeLeadingSep)) {
|
---|
3194 | const qt_section_chunk §ion = sections.at(first_i);
|
---|
3195 | ret.prepend(section.string.left(section.length));
|
---|
3196 | }
|
---|
3197 | if((flags & SectionIncludeTrailingSep) && last_i+1 <= sections.size()-1) {
|
---|
3198 | const qt_section_chunk §ion = sections.at(last_i+1);
|
---|
3199 | ret += section.string.left(section.length);
|
---|
3200 | }
|
---|
3201 | return ret;
|
---|
3202 | }
|
---|
3203 | #endif
|
---|
3204 |
|
---|
3205 | /*!
|
---|
3206 | Returns a substring that contains the \a n leftmost characters
|
---|
3207 | of the string.
|
---|
3208 |
|
---|
3209 | The entire string is returned if \a n is greater than size() or
|
---|
3210 | less than zero.
|
---|
3211 |
|
---|
3212 | \snippet doc/src/snippets/qstring/main.cpp 31
|
---|
3213 |
|
---|
3214 | \sa right(), mid(), startsWith()
|
---|
3215 | */
|
---|
3216 | QString QString::left(int n) const
|
---|
3217 | {
|
---|
3218 | if (n >= d->size || n < 0)
|
---|
3219 | return *this;
|
---|
3220 | return QString((const QChar*) d->data, n);
|
---|
3221 | }
|
---|
3222 |
|
---|
3223 | /*!
|
---|
3224 | Returns a substring that contains the \a n rightmost characters
|
---|
3225 | of the string.
|
---|
3226 |
|
---|
3227 | The entire string is returned if \a n is greater than size() or
|
---|
3228 | less than zero.
|
---|
3229 |
|
---|
3230 | \snippet doc/src/snippets/qstring/main.cpp 48
|
---|
3231 |
|
---|
3232 | \sa left(), mid(), endsWith()
|
---|
3233 | */
|
---|
3234 | QString QString::right(int n) const
|
---|
3235 | {
|
---|
3236 | if (n >= d->size || n < 0)
|
---|
3237 | return *this;
|
---|
3238 | return QString((const QChar*) d->data + d->size - n, n);
|
---|
3239 | }
|
---|
3240 |
|
---|
3241 | /*!
|
---|
3242 | Returns a string that contains \a n characters of this string,
|
---|
3243 | starting at the specified \a position index.
|
---|
3244 |
|
---|
3245 | Returns a null string if the \a position index exceeds the
|
---|
3246 | length of the string. If there are less than \a n characters
|
---|
3247 | available in the string starting at the given \a position, or if
|
---|
3248 | \a n is -1 (default), the function returns all characters that
|
---|
3249 | are available from the specified \a position.
|
---|
3250 |
|
---|
3251 | Example:
|
---|
3252 |
|
---|
3253 | \snippet doc/src/snippets/qstring/main.cpp 34
|
---|
3254 |
|
---|
3255 | \sa left(), right()
|
---|
3256 | */
|
---|
3257 |
|
---|
3258 | QString QString::mid(int position, int n) const
|
---|
3259 | {
|
---|
3260 | if (d == &shared_null || position >= d->size)
|
---|
3261 | return QString();
|
---|
3262 | if (n < 0)
|
---|
3263 | n = d->size - position;
|
---|
3264 | if (position < 0) {
|
---|
3265 | n += position;
|
---|
3266 | position = 0;
|
---|
3267 | }
|
---|
3268 | if (n + position > d->size)
|
---|
3269 | n = d->size - position;
|
---|
3270 | if (position == 0 && n == d->size)
|
---|
3271 | return *this;
|
---|
3272 | return QString((const QChar*) d->data + position, n);
|
---|
3273 | }
|
---|
3274 |
|
---|
3275 | /*!
|
---|
3276 | Returns true if the string starts with \a s; otherwise returns
|
---|
3277 | false.
|
---|
3278 |
|
---|
3279 | If \a cs is Qt::CaseSensitive (default), the search is
|
---|
3280 | case sensitive; otherwise the search is case insensitive.
|
---|
3281 |
|
---|
3282 | \snippet doc/src/snippets/qstring/main.cpp 65
|
---|
3283 |
|
---|
3284 | \sa endsWith()
|
---|
3285 | */
|
---|
3286 | bool QString::startsWith(const QString& s, Qt::CaseSensitivity cs) const
|
---|
3287 | {
|
---|
3288 | if (d == &shared_null)
|
---|
3289 | return (s.d == &shared_null);
|
---|
3290 | if (d->size == 0)
|
---|
3291 | return s.d->size == 0;
|
---|
3292 | if (s.d->size > d->size)
|
---|
3293 | return false;
|
---|
3294 | if (cs == Qt::CaseSensitive) {
|
---|
3295 | return qMemEquals(d->data, s.d->data, s.d->size);
|
---|
3296 | } else {
|
---|
3297 | uint last = 0;
|
---|
3298 | uint olast = 0;
|
---|
3299 | for (int i = 0; i < s.d->size; ++i)
|
---|
3300 | if (foldCase(d->data[i], last) != foldCase(s.d->data[i], olast))
|
---|
3301 | return false;
|
---|
3302 | }
|
---|
3303 | return true;
|
---|
3304 | }
|
---|
3305 |
|
---|
3306 | /*!
|
---|
3307 | \overload startsWith()
|
---|
3308 | */
|
---|
3309 | bool QString::startsWith(const QLatin1String& s, Qt::CaseSensitivity cs) const
|
---|
3310 | {
|
---|
3311 | if (d == &shared_null)
|
---|
3312 | return (s.latin1() == 0);
|
---|
3313 | if (d->size == 0)
|
---|
3314 | return !s.latin1() || *s.latin1() == 0;
|
---|
3315 | int slen = qstrlen(s.latin1());
|
---|
3316 | if (slen > d->size)
|
---|
3317 | return false;
|
---|
3318 | const uchar *latin = (const uchar *)s.latin1();
|
---|
3319 | if (cs == Qt::CaseSensitive) {
|
---|
3320 | for (int i = 0; i < slen; ++i)
|
---|
3321 | if (d->data[i] != latin[i])
|
---|
3322 | return false;
|
---|
3323 | } else {
|
---|
3324 | for (int i = 0; i < slen; ++i)
|
---|
3325 | if (foldCase(d->data[i]) != foldCase((ushort)latin[i]))
|
---|
3326 | return false;
|
---|
3327 | }
|
---|
3328 | return true;
|
---|
3329 | }
|
---|
3330 |
|
---|
3331 | /*!
|
---|
3332 | \overload startsWith()
|
---|
3333 |
|
---|
3334 | Returns true if the string starts with \a c; otherwise returns
|
---|
3335 | false.
|
---|
3336 | */
|
---|
3337 | bool QString::startsWith(const QChar &c, Qt::CaseSensitivity cs) const
|
---|
3338 | {
|
---|
3339 | return d->size
|
---|
3340 | && (cs == Qt::CaseSensitive
|
---|
3341 | ? d->data[0] == c
|
---|
3342 | : foldCase(d->data[0]) == foldCase(c.unicode()));
|
---|
3343 | }
|
---|
3344 |
|
---|
3345 | /*!
|
---|
3346 | Returns true if the string ends with \a s; otherwise returns
|
---|
3347 | false.
|
---|
3348 |
|
---|
3349 | If \a cs is Qt::CaseSensitive (default), the search is case
|
---|
3350 | sensitive; otherwise the search is case insensitive.
|
---|
3351 |
|
---|
3352 | \snippet doc/src/snippets/qstring/main.cpp 20
|
---|
3353 |
|
---|
3354 | \sa startsWith()
|
---|
3355 | */
|
---|
3356 | bool QString::endsWith(const QString& s, Qt::CaseSensitivity cs) const
|
---|
3357 | {
|
---|
3358 | if (d == &shared_null)
|
---|
3359 | return (s.d == &shared_null);
|
---|
3360 | if (d->size == 0)
|
---|
3361 | return s.d->size == 0;
|
---|
3362 | int pos = d->size - s.d->size;
|
---|
3363 | if (pos < 0)
|
---|
3364 | return false;
|
---|
3365 | if (cs == Qt::CaseSensitive) {
|
---|
3366 | return qMemEquals(d->data + pos, s.d->data, s.d->size);
|
---|
3367 | } else {
|
---|
3368 | uint last = 0;
|
---|
3369 | uint olast = 0;
|
---|
3370 | for (int i = 0; i < s.length(); i++)
|
---|
3371 | if (foldCase(d->data[pos+i], last) != foldCase(s.d->data[i], olast))
|
---|
3372 | return false;
|
---|
3373 | }
|
---|
3374 | return true;
|
---|
3375 | }
|
---|
3376 |
|
---|
3377 | /*!
|
---|
3378 | \overload endsWith()
|
---|
3379 | */
|
---|
3380 | bool QString::endsWith(const QLatin1String& s, Qt::CaseSensitivity cs) const
|
---|
3381 | {
|
---|
3382 | if (d == &shared_null)
|
---|
3383 | return (s.latin1() == 0);
|
---|
3384 | if (d->size == 0)
|
---|
3385 | return !s.latin1() || *s.latin1() == 0;
|
---|
3386 | int slen = qstrlen(s.latin1());
|
---|
3387 | int pos = d->size - slen;
|
---|
3388 | const uchar *latin = (const uchar *)s.latin1();
|
---|
3389 | if (pos < 0)
|
---|
3390 | return false;
|
---|
3391 | if (cs == Qt::CaseSensitive) {
|
---|
3392 | for (int i = 0; i < slen; i++)
|
---|
3393 | if (d->data[pos+i] != latin[i])
|
---|
3394 | return false;
|
---|
3395 | } else {
|
---|
3396 | for (int i = 0; i < slen; i++)
|
---|
3397 | if (foldCase(d->data[pos+i]) != foldCase((ushort)latin[i]))
|
---|
3398 | return false;
|
---|
3399 | }
|
---|
3400 | return true;
|
---|
3401 | }
|
---|
3402 |
|
---|
3403 | /*!
|
---|
3404 | Returns true if the string ends with \a c; otherwise returns
|
---|
3405 | false.
|
---|
3406 |
|
---|
3407 | \overload endsWith()
|
---|
3408 | */
|
---|
3409 | bool QString::endsWith(const QChar &c, Qt::CaseSensitivity cs) const
|
---|
3410 | {
|
---|
3411 | return d->size
|
---|
3412 | && (cs == Qt::CaseSensitive
|
---|
3413 | ? d->data[d->size - 1] == c
|
---|
3414 | : foldCase(d->data[d->size - 1]) == foldCase(c.unicode()));
|
---|
3415 | }
|
---|
3416 |
|
---|
3417 | /*! \fn const char *QString::ascii() const
|
---|
3418 | \nonreentrant
|
---|
3419 |
|
---|
3420 | Use toAscii() instead.
|
---|
3421 | */
|
---|
3422 |
|
---|
3423 | /*! \fn const char *QString::latin1() const
|
---|
3424 | \nonreentrant
|
---|
3425 |
|
---|
3426 | Use toLatin1() instead.
|
---|
3427 | */
|
---|
3428 |
|
---|
3429 | /*! \fn const char *QString::utf8() const
|
---|
3430 | \nonreentrant
|
---|
3431 |
|
---|
3432 | Use toUtf8() instead.
|
---|
3433 | */
|
---|
3434 |
|
---|
3435 | /*! \fn const char *QString::local8Bit() const
|
---|
3436 | \nonreentrant
|
---|
3437 |
|
---|
3438 | Use toLocal8Bit() instead.
|
---|
3439 | */
|
---|
3440 |
|
---|
3441 | static QByteArray toLatin1_helper(const QChar *data, int length)
|
---|
3442 | {
|
---|
3443 | QByteArray ba;
|
---|
3444 | if (length) {
|
---|
3445 | ba.resize(length);
|
---|
3446 | const ushort *i = reinterpret_cast<const ushort *>(data);
|
---|
3447 | const ushort *e = i + length;
|
---|
3448 | uchar *s = (uchar*) ba.data();
|
---|
3449 | while (i != e) {
|
---|
3450 | *s++ = (*i>0xff) ? '?' : (uchar) *i;
|
---|
3451 | ++i;
|
---|
3452 | }
|
---|
3453 | }
|
---|
3454 | return ba;
|
---|
3455 | }
|
---|
3456 |
|
---|
3457 | /*!
|
---|
3458 | Returns a Latin-1 representation of the string as a QByteArray.
|
---|
3459 | The returned byte array is undefined if the string contains
|
---|
3460 | non-Latin1 characters.
|
---|
3461 |
|
---|
3462 | \sa fromLatin1(), toAscii(), toUtf8(), toLocal8Bit(), QTextCodec
|
---|
3463 | */
|
---|
3464 | QByteArray QString::toLatin1() const
|
---|
3465 | {
|
---|
3466 | return toLatin1_helper(unicode(), length());
|
---|
3467 | }
|
---|
3468 |
|
---|
3469 | // ### Qt 5: Change the return type of at least toAscii(),
|
---|
3470 | // toLatin1() and unicode() such that the use of Q_COMPILER_MANGLES_RETURN_TYPE
|
---|
3471 | // isn't necessary in the header. See task 177402.
|
---|
3472 |
|
---|
3473 | /*!
|
---|
3474 | Returns an 8-bit ASCII representation of the string as a QByteArray.
|
---|
3475 |
|
---|
3476 | If a codec has been set using QTextCodec::setCodecForCStrings(),
|
---|
3477 | it is used to convert Unicode to 8-bit char; otherwise this
|
---|
3478 | function does the same as toLatin1().
|
---|
3479 |
|
---|
3480 | \sa fromAscii(), toLatin1(), toUtf8(), toLocal8Bit(), QTextCodec
|
---|
3481 | */
|
---|
3482 | QByteArray QString::toAscii() const
|
---|
3483 | {
|
---|
3484 | #ifndef QT_NO_TEXTCODEC
|
---|
3485 | if (codecForCStrings)
|
---|
3486 | return codecForCStrings->fromUnicode(*this);
|
---|
3487 | #endif // QT_NO_TEXTCODEC
|
---|
3488 | return toLatin1();
|
---|
3489 | }
|
---|
3490 |
|
---|
3491 | #if !defined(Q_WS_MAC) && defined(Q_OS_UNIX) && !defined(Q_OS_OS2)
|
---|
3492 | static QByteArray toLocal8Bit_helper(const QChar *data, int length)
|
---|
3493 | {
|
---|
3494 | #ifndef QT_NO_TEXTCODEC
|
---|
3495 | if (QTextCodec::codecForLocale())
|
---|
3496 | return QTextCodec::codecForLocale()->fromUnicode(data, length);
|
---|
3497 | #endif // QT_NO_TEXTCODEC
|
---|
3498 | return toLatin1_helper(data, length);
|
---|
3499 | }
|
---|
3500 | #endif
|
---|
3501 |
|
---|
3502 | /*!
|
---|
3503 | Returns the local 8-bit representation of the string as a
|
---|
3504 | QByteArray. The returned byte array is undefined if the string
|
---|
3505 | contains characters not supported by the local 8-bit encoding.
|
---|
3506 |
|
---|
3507 | QTextCodec::codecForLocale() is used to perform the conversion
|
---|
3508 | from Unicode.
|
---|
3509 |
|
---|
3510 | \sa fromLocal8Bit(), toAscii(), toLatin1(), toUtf8(), QTextCodec
|
---|
3511 | */
|
---|
3512 | QByteArray QString::toLocal8Bit() const
|
---|
3513 | {
|
---|
3514 | #ifndef QT_NO_TEXTCODEC
|
---|
3515 | if (QTextCodec::codecForLocale())
|
---|
3516 | return QTextCodec::codecForLocale()->fromUnicode(*this);
|
---|
3517 | #endif // QT_NO_TEXTCODEC
|
---|
3518 | return toLatin1();
|
---|
3519 | }
|
---|
3520 |
|
---|
3521 | /*!
|
---|
3522 | Returns a UTF-8 representation of the string as a QByteArray.
|
---|
3523 |
|
---|
3524 | \sa fromUtf8(), toAscii(), toLatin1(), toLocal8Bit(), QTextCodec
|
---|
3525 | */
|
---|
3526 | QByteArray QString::toUtf8() const
|
---|
3527 | {
|
---|
3528 | QByteArray ba;
|
---|
3529 | if (d->size) {
|
---|
3530 | int l = d->size;
|
---|
3531 | int rlen = l*3+1;
|
---|
3532 | ba.resize(rlen);
|
---|
3533 | uchar *cursor = (uchar*)ba.data();
|
---|
3534 | const ushort *ch =d->data;
|
---|
3535 | for (int i=0; i < l; i++) {
|
---|
3536 | uint u = *ch;
|
---|
3537 | if (u < 0x80) {
|
---|
3538 | *cursor++ = (uchar)u;
|
---|
3539 | } else {
|
---|
3540 | if (u < 0x0800) {
|
---|
3541 | *cursor++ = 0xc0 | ((uchar) (u >> 6));
|
---|
3542 | } else {
|
---|
3543 | if (QChar(u).isHighSurrogate() && i < l-1) {
|
---|
3544 | ushort low = ch[1];
|
---|
3545 | if (QChar(low).isLowSurrogate()) {
|
---|
3546 | ++ch;
|
---|
3547 | ++i;
|
---|
3548 | u = QChar::surrogateToUcs4(u,low);
|
---|
3549 | }
|
---|
3550 | }
|
---|
3551 | if (u > 0xffff) {
|
---|
3552 | *cursor++ = 0xf0 | ((uchar) (u >> 18));
|
---|
3553 | *cursor++ = 0x80 | (((uchar) (u >> 12)) & 0x3f);
|
---|
3554 | } else {
|
---|
3555 | *cursor++ = 0xe0 | ((uchar) (u >> 12));
|
---|
3556 | }
|
---|
3557 | *cursor++ = 0x80 | (((uchar) (u >> 6)) & 0x3f);
|
---|
3558 | }
|
---|
3559 | *cursor++ = 0x80 | ((uchar) (u&0x3f));
|
---|
3560 | }
|
---|
3561 | ++ch;
|
---|
3562 | }
|
---|
3563 | ba.resize(cursor - (uchar*)ba.constData());
|
---|
3564 | }
|
---|
3565 | return ba;
|
---|
3566 | }
|
---|
3567 |
|
---|
3568 | /*!
|
---|
3569 | \since 4.2
|
---|
3570 |
|
---|
3571 | Returns a UCS-4 representation of the string as a QVector<uint>.
|
---|
3572 |
|
---|
3573 | \sa fromUtf8(), toAscii(), toLatin1(), toLocal8Bit(), QTextCodec, fromUcs4(), toWCharArray()
|
---|
3574 | */
|
---|
3575 | QVector<uint> QString::toUcs4() const
|
---|
3576 | {
|
---|
3577 | QVector<uint> v(length());
|
---|
3578 | uint *a = v.data();
|
---|
3579 | const unsigned short *uc = utf16();
|
---|
3580 | for (int i = 0; i < length(); ++i) {
|
---|
3581 | uint u = uc[i];
|
---|
3582 | if (QChar(u).isHighSurrogate() && i < length()-1) {
|
---|
3583 | ushort low = uc[i+1];
|
---|
3584 | if (QChar(low).isLowSurrogate()) {
|
---|
3585 | ++i;
|
---|
3586 | u = QChar::surrogateToUcs4(u, low);
|
---|
3587 | }
|
---|
3588 | }
|
---|
3589 | *a = u;
|
---|
3590 | ++a;
|
---|
3591 | }
|
---|
3592 | v.resize(a - v.data());
|
---|
3593 | return v;
|
---|
3594 | }
|
---|
3595 |
|
---|
3596 | QString::Data *QString::fromLatin1_helper(const char *str, int size)
|
---|
3597 | {
|
---|
3598 | Data *d;
|
---|
3599 | if (!str) {
|
---|
3600 | d = &shared_null;
|
---|
3601 | d->ref.ref();
|
---|
3602 | } else if (size == 0 || (!*str && size < 0)) {
|
---|
3603 | d = &shared_empty;
|
---|
3604 | d->ref.ref();
|
---|
3605 | } else {
|
---|
3606 | if (size < 0)
|
---|
3607 | size = qstrlen(str);
|
---|
3608 | d = static_cast<Data *>(qMalloc(sizeof(Data) + size * sizeof(QChar)));
|
---|
3609 | Q_CHECK_PTR(d);
|
---|
3610 | d->ref = 1;
|
---|
3611 | d->alloc = d->size = size;
|
---|
3612 | d->clean = d->asciiCache = d->simpletext = d->righttoleft = d->capacity = 0;
|
---|
3613 | d->data = d->array;
|
---|
3614 | ushort *i = d->data;
|
---|
3615 | d->array[size] = '\0';
|
---|
3616 | while (size--)
|
---|
3617 | *i++ = (uchar)*str++;
|
---|
3618 | }
|
---|
3619 | return d;
|
---|
3620 | }
|
---|
3621 |
|
---|
3622 | QString::Data *QString::fromAscii_helper(const char *str, int size)
|
---|
3623 | {
|
---|
3624 | #ifndef QT_NO_TEXTCODEC
|
---|
3625 | if (codecForCStrings) {
|
---|
3626 | Data *d;
|
---|
3627 | if (!str) {
|
---|
3628 | d = &shared_null;
|
---|
3629 | d->ref.ref();
|
---|
3630 | } else if (size == 0 || (!*str && size < 0)) {
|
---|
3631 | d = &shared_empty;
|
---|
3632 | d->ref.ref();
|
---|
3633 | } else {
|
---|
3634 | if (size < 0)
|
---|
3635 | size = qstrlen(str);
|
---|
3636 | QString s = codecForCStrings->toUnicode(str, size);
|
---|
3637 | d = s.d;
|
---|
3638 | d->ref.ref();
|
---|
3639 | }
|
---|
3640 | return d;
|
---|
3641 | }
|
---|
3642 | #endif
|
---|
3643 | return fromLatin1_helper(str, size);
|
---|
3644 | }
|
---|
3645 |
|
---|
3646 | /*!
|
---|
3647 | Returns a QString initialized with the first \a size characters
|
---|
3648 | of the Latin-1 string \a str.
|
---|
3649 |
|
---|
3650 | If \a size is -1 (default), it is taken to be qstrlen(\a
|
---|
3651 | str).
|
---|
3652 |
|
---|
3653 | \sa toLatin1(), fromAscii(), fromUtf8(), fromLocal8Bit()
|
---|
3654 | */
|
---|
3655 | QString QString::fromLatin1(const char *str, int size)
|
---|
3656 | {
|
---|
3657 | return QString(fromLatin1_helper(str, size), 0);
|
---|
3658 | }
|
---|
3659 |
|
---|
3660 |
|
---|
3661 | #ifdef QT3_SUPPORT
|
---|
3662 |
|
---|
3663 | /*!
|
---|
3664 | \internal
|
---|
3665 | */
|
---|
3666 | const char *QString::ascii_helper() const
|
---|
3667 | {
|
---|
3668 | if (!asciiCache)
|
---|
3669 | asciiCache = new QHash<void *, QByteArray>();
|
---|
3670 |
|
---|
3671 | d->asciiCache = true;
|
---|
3672 | QByteArray ascii = toAscii();
|
---|
3673 | QByteArray old = asciiCache->value(d);
|
---|
3674 | if (old == ascii)
|
---|
3675 | return old.constData();
|
---|
3676 | asciiCache->insert(d, ascii);
|
---|
3677 | return ascii.constData();
|
---|
3678 | }
|
---|
3679 |
|
---|
3680 | /*!
|
---|
3681 | \internal
|
---|
3682 | */
|
---|
3683 | const char *QString::latin1_helper() const
|
---|
3684 | {
|
---|
3685 | if (!asciiCache)
|
---|
3686 | asciiCache = new QHash<void *, QByteArray>();
|
---|
3687 |
|
---|
3688 | d->asciiCache = true;
|
---|
3689 | QByteArray ascii = toLatin1();
|
---|
3690 | QByteArray old = asciiCache->value(d);
|
---|
3691 | if (old == ascii)
|
---|
3692 | return old.constData();
|
---|
3693 | asciiCache->insert(d, ascii);
|
---|
3694 | return ascii.constData();
|
---|
3695 | }
|
---|
3696 |
|
---|
3697 | #endif
|
---|
3698 |
|
---|
3699 | QT_END_NAMESPACE
|
---|
3700 |
|
---|
3701 | #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
|
---|
3702 | #include "qt_windows.h"
|
---|
3703 |
|
---|
3704 | QT_BEGIN_NAMESPACE
|
---|
3705 |
|
---|
3706 | QByteArray qt_winQString2MB(const QString& s, int uclen)
|
---|
3707 | {
|
---|
3708 | if (uclen < 0)
|
---|
3709 | uclen = s.length();
|
---|
3710 | if (s.isNull())
|
---|
3711 | return QByteArray();
|
---|
3712 | if (uclen == 0)
|
---|
3713 | return QByteArray("");
|
---|
3714 | return qt_winQString2MB(s.constData(), uclen);
|
---|
3715 | }
|
---|
3716 |
|
---|
3717 | QByteArray qt_winQString2MB(const QChar *ch, int uclen)
|
---|
3718 | {
|
---|
3719 | if (!ch)
|
---|
3720 | return QByteArray();
|
---|
3721 | if (uclen == 0)
|
---|
3722 | return QByteArray("");
|
---|
3723 | BOOL used_def;
|
---|
3724 | QByteArray mb(4096, 0);
|
---|
3725 | int len;
|
---|
3726 | while (!(len=WideCharToMultiByte(CP_ACP, 0, (const wchar_t*)ch, uclen,
|
---|
3727 | mb.data(), mb.size()-1, 0, &used_def)))
|
---|
3728 | {
|
---|
3729 | int r = GetLastError();
|
---|
3730 | if (r == ERROR_INSUFFICIENT_BUFFER) {
|
---|
3731 | mb.resize(1+WideCharToMultiByte(CP_ACP, 0,
|
---|
3732 | (const wchar_t*)ch, uclen,
|
---|
3733 | 0, 0, 0, &used_def));
|
---|
3734 | // and try again...
|
---|
3735 | } else {
|
---|
3736 | #ifndef QT_NO_DEBUG
|
---|
3737 | // Fail.
|
---|
3738 | qWarning("WideCharToMultiByte: Cannot convert multibyte text (error %d): %s (UTF-8)",
|
---|
3739 | r, QString(ch, uclen).toLocal8Bit().data());
|
---|
3740 | #endif
|
---|
3741 | break;
|
---|
3742 | }
|
---|
3743 | }
|
---|
3744 | mb.resize(len);
|
---|
3745 | return mb;
|
---|
3746 | }
|
---|
3747 |
|
---|
3748 | QString qt_winMB2QString(const char *mb, int mblen)
|
---|
3749 | {
|
---|
3750 | if (!mb || !mblen)
|
---|
3751 | return QString();
|
---|
3752 | const int wclen_auto = 4096;
|
---|
3753 | wchar_t wc_auto[wclen_auto];
|
---|
3754 | int wclen = wclen_auto;
|
---|
3755 | wchar_t *wc = wc_auto;
|
---|
3756 | int len;
|
---|
3757 | while (!(len=MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,
|
---|
3758 | mb, mblen, wc, wclen)))
|
---|
3759 | {
|
---|
3760 | int r = GetLastError();
|
---|
3761 | if (r == ERROR_INSUFFICIENT_BUFFER) {
|
---|
3762 | if (wc != wc_auto) {
|
---|
3763 | qWarning("MultiByteToWideChar: Size changed");
|
---|
3764 | break;
|
---|
3765 | } else {
|
---|
3766 | wclen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,
|
---|
3767 | mb, mblen, 0, 0);
|
---|
3768 | wc = new wchar_t[wclen];
|
---|
3769 | // and try again...
|
---|
3770 | }
|
---|
3771 | } else {
|
---|
3772 | // Fail.
|
---|
3773 | qWarning("MultiByteToWideChar: Cannot convert multibyte text");
|
---|
3774 | break;
|
---|
3775 | }
|
---|
3776 | }
|
---|
3777 | if (len <= 0)
|
---|
3778 | return QString();
|
---|
3779 | if (wc[len-1] == 0) // len - 1: we don't want terminator
|
---|
3780 | --len;
|
---|
3781 | QString s((QChar*)wc, len);
|
---|
3782 | if (wc != wc_auto)
|
---|
3783 | delete [] wc;
|
---|
3784 | return s;
|
---|
3785 | }
|
---|
3786 |
|
---|
3787 | QT_END_NAMESPACE
|
---|
3788 |
|
---|
3789 | #endif // Q_OS_WIN32
|
---|
3790 |
|
---|
3791 | QT_BEGIN_NAMESPACE
|
---|
3792 |
|
---|
3793 | /*!
|
---|
3794 | Returns a QString initialized with the first \a size characters
|
---|
3795 | of the 8-bit string \a str.
|
---|
3796 |
|
---|
3797 | If \a size is -1 (default), it is taken to be qstrlen(\a
|
---|
3798 | str).
|
---|
3799 |
|
---|
3800 | QTextCodec::codecForLocale() is used to perform the conversion
|
---|
3801 | from Unicode.
|
---|
3802 |
|
---|
3803 | \sa toLocal8Bit(), fromAscii(), fromLatin1(), fromUtf8()
|
---|
3804 | */
|
---|
3805 | QString QString::fromLocal8Bit(const char *str, int size)
|
---|
3806 | {
|
---|
3807 | if (!str)
|
---|
3808 | return QString();
|
---|
3809 | if (size == 0 || (!*str && size < 0))
|
---|
3810 | return QLatin1String("");
|
---|
3811 | #if !defined(QT_NO_TEXTCODEC)
|
---|
3812 | if (size < 0)
|
---|
3813 | size = qstrlen(str);
|
---|
3814 | QTextCodec *codec = QTextCodec::codecForLocale();
|
---|
3815 | if (codec)
|
---|
3816 | return codec->toUnicode(str, size);
|
---|
3817 | #endif // !QT_NO_TEXTCODEC
|
---|
3818 | return fromLatin1(str, size);
|
---|
3819 | }
|
---|
3820 |
|
---|
3821 | /*!
|
---|
3822 | Returns a QString initialized with the first \a size characters
|
---|
3823 | of the 8-bit ASCII string \a str.
|
---|
3824 |
|
---|
3825 | If \a size is -1 (default), it is taken to be qstrlen(\a
|
---|
3826 | str).
|
---|
3827 |
|
---|
3828 | If a codec has been set using QTextCodec::setCodecForCStrings(),
|
---|
3829 | it is used to convert \a str to Unicode; otherwise this function
|
---|
3830 | does the same as fromLatin1().
|
---|
3831 |
|
---|
3832 | \sa toAscii(), fromLatin1(), fromUtf8(), fromLocal8Bit()
|
---|
3833 | */
|
---|
3834 | QString QString::fromAscii(const char *str, int size)
|
---|
3835 | {
|
---|
3836 | return QString(fromAscii_helper(str, size), 0);
|
---|
3837 | }
|
---|
3838 |
|
---|
3839 | /*!
|
---|
3840 | Returns a QString initialized with the first \a size bytes
|
---|
3841 | of the UTF-8 string \a str.
|
---|
3842 |
|
---|
3843 | If \a size is -1 (default), it is taken to be qstrlen(\a
|
---|
3844 | str).
|
---|
3845 |
|
---|
3846 | \sa toUtf8(), fromAscii(), fromLatin1(), fromLocal8Bit()
|
---|
3847 | */
|
---|
3848 | QString QString::fromUtf8(const char *str, int size)
|
---|
3849 | {
|
---|
3850 | if (!str)
|
---|
3851 | return QString();
|
---|
3852 | if (size < 0)
|
---|
3853 | size = qstrlen(str);
|
---|
3854 |
|
---|
3855 | return QUtf8::convertToUnicode(str, size, 0);
|
---|
3856 | }
|
---|
3857 |
|
---|
3858 | /*!
|
---|
3859 | Returns a QString initialized with the first \a size characters
|
---|
3860 | of the Unicode string \a unicode (ISO-10646-UTF-16 encoded).
|
---|
3861 |
|
---|
3862 | If \a size is -1 (default), \a unicode must be terminated
|
---|
3863 | with a 0.
|
---|
3864 |
|
---|
3865 | QString makes a deep copy of the Unicode data.
|
---|
3866 |
|
---|
3867 | \sa utf16(), setUtf16()
|
---|
3868 | */
|
---|
3869 | QString QString::fromUtf16(const ushort *unicode, int size)
|
---|
3870 | {
|
---|
3871 | if (!unicode)
|
---|
3872 | return QString();
|
---|
3873 | if (size < 0) {
|
---|
3874 | size = 0;
|
---|
3875 | while (unicode[size] != 0)
|
---|
3876 | ++size;
|
---|
3877 | }
|
---|
3878 | return QUtf16::convertToUnicode((const char *)unicode, size*2, 0);
|
---|
3879 | }
|
---|
3880 |
|
---|
3881 |
|
---|
3882 | /*!
|
---|
3883 | \since 4.2
|
---|
3884 |
|
---|
3885 | Returns a QString initialized with the first \a size characters
|
---|
3886 | of the Unicode string \a unicode (ISO-10646-UCS-4 encoded).
|
---|
3887 |
|
---|
3888 | If \a size is -1 (default), \a unicode must be terminated
|
---|
3889 | with a 0.
|
---|
3890 |
|
---|
3891 | \sa toUcs4(), fromUtf16(), utf16(), setUtf16(), fromWCharArray()
|
---|
3892 | */
|
---|
3893 | QString QString::fromUcs4(const uint *unicode, int size)
|
---|
3894 | {
|
---|
3895 | if (!unicode)
|
---|
3896 | return QString();
|
---|
3897 | if (size < 0) {
|
---|
3898 | size = 0;
|
---|
3899 | while (unicode[size] != 0)
|
---|
3900 | ++size;
|
---|
3901 | }
|
---|
3902 | return QUtf32::convertToUnicode((const char *)unicode, size*4, 0);
|
---|
3903 | }
|
---|
3904 |
|
---|
3905 | /*!
|
---|
3906 | Resizes the string to \a size characters and copies \a unicode
|
---|
3907 | into the string.
|
---|
3908 |
|
---|
3909 | If \a unicode is 0, nothing is copied, but the string is still
|
---|
3910 | resized to \a size.
|
---|
3911 |
|
---|
3912 | \sa unicode(), setUtf16()
|
---|
3913 | */
|
---|
3914 | QString& QString::setUnicode(const QChar *unicode, int size)
|
---|
3915 | {
|
---|
3916 | resize(size);
|
---|
3917 | if (unicode && size)
|
---|
3918 | memcpy(d->data, unicode, size * sizeof(QChar));
|
---|
3919 | return *this;
|
---|
3920 | }
|
---|
3921 |
|
---|
3922 | /*!
|
---|
3923 | \fn QString &QString::setUtf16(const ushort *unicode, int size)
|
---|
3924 |
|
---|
3925 | Resizes the string to \a size characters and copies \a unicode
|
---|
3926 | into the string.
|
---|
3927 |
|
---|
3928 | If \a unicode is 0, nothing is copied, but the string is still
|
---|
3929 | resized to \a size.
|
---|
3930 |
|
---|
3931 | \sa utf16(), setUnicode()
|
---|
3932 | */
|
---|
3933 |
|
---|
3934 | /*!
|
---|
3935 | Returns a string that has whitespace removed from the start
|
---|
3936 | and the end, and that has each sequence of internal whitespace
|
---|
3937 | replaced with a single space.
|
---|
3938 |
|
---|
3939 | Whitespace means any character for which QChar::isSpace() returns
|
---|
3940 | true. This includes the ASCII characters '\\t', '\\n', '\\v',
|
---|
3941 | '\\f', '\\r', and ' '.
|
---|
3942 |
|
---|
3943 | Example:
|
---|
3944 |
|
---|
3945 | \snippet doc/src/snippets/qstring/main.cpp 57
|
---|
3946 |
|
---|
3947 | \sa trimmed()
|
---|
3948 | */
|
---|
3949 | QString QString::simplified() const
|
---|
3950 | {
|
---|
3951 | if (d->size == 0)
|
---|
3952 | return *this;
|
---|
3953 | QString result(d->size, Qt::Uninitialized);
|
---|
3954 | const QChar *from = (const QChar*) d->data;
|
---|
3955 | const QChar *fromend = (const QChar*) from+d->size;
|
---|
3956 | int outc=0;
|
---|
3957 | QChar *to = (QChar*) result.d->data;
|
---|
3958 | for (;;) {
|
---|
3959 | while (from!=fromend && from->isSpace())
|
---|
3960 | from++;
|
---|
3961 | while (from!=fromend && !from->isSpace())
|
---|
3962 | to[outc++] = *from++;
|
---|
3963 | if (from!=fromend)
|
---|
3964 | to[outc++] = QLatin1Char(' ');
|
---|
3965 | else
|
---|
3966 | break;
|
---|
3967 | }
|
---|
3968 | if (outc > 0 && to[outc-1] == QLatin1Char(' '))
|
---|
3969 | outc--;
|
---|
3970 | result.truncate(outc);
|
---|
3971 | return result;
|
---|
3972 | }
|
---|
3973 |
|
---|
3974 | /*!
|
---|
3975 | Returns a string that has whitespace removed from the start and
|
---|
3976 | the end.
|
---|
3977 |
|
---|
3978 | Whitespace means any character for which QChar::isSpace() returns
|
---|
3979 | true. This includes the ASCII characters '\\t', '\\n', '\\v',
|
---|
3980 | '\\f', '\\r', and ' '.
|
---|
3981 |
|
---|
3982 | Example:
|
---|
3983 |
|
---|
3984 | \snippet doc/src/snippets/qstring/main.cpp 82
|
---|
3985 |
|
---|
3986 | Unlike simplified(), trimmed() leaves internal whitespace alone.
|
---|
3987 |
|
---|
3988 | \sa simplified()
|
---|
3989 | */
|
---|
3990 | QString QString::trimmed() const
|
---|
3991 | {
|
---|
3992 | if (d->size == 0)
|
---|
3993 | return *this;
|
---|
3994 | const QChar *s = (const QChar*)d->data;
|
---|
3995 | if (!s->isSpace() && !s[d->size-1].isSpace())
|
---|
3996 | return *this;
|
---|
3997 | int start = 0;
|
---|
3998 | int end = d->size - 1;
|
---|
3999 | while (start<=end && s[start].isSpace()) // skip white space from start
|
---|
4000 | start++;
|
---|
4001 | if (start <= end) { // only white space
|
---|
4002 | while (end && s[end].isSpace()) // skip white space from end
|
---|
4003 | end--;
|
---|
4004 | }
|
---|
4005 | int l = end - start + 1;
|
---|
4006 | if (l <= 0) {
|
---|
4007 | shared_empty.ref.ref();
|
---|
4008 | return QString(&shared_empty, 0);
|
---|
4009 | }
|
---|
4010 | return QString(s + start, l);
|
---|
4011 | }
|
---|
4012 |
|
---|
4013 | /*! \fn const QChar QString::at(int position) const
|
---|
4014 |
|
---|
4015 | Returns the character at the given index \a position in the
|
---|
4016 | string.
|
---|
4017 |
|
---|
4018 | The \a position must be a valid index position in the string
|
---|
4019 | (i.e., 0 <= \a position < size()).
|
---|
4020 |
|
---|
4021 | \sa operator[]()
|
---|
4022 | */
|
---|
4023 |
|
---|
4024 | /*!
|
---|
4025 | \fn QCharRef QString::operator[](int position)
|
---|
4026 |
|
---|
4027 | Returns the character at the specified \a position in the string as a
|
---|
4028 | modifiable reference.
|
---|
4029 |
|
---|
4030 | Example:
|
---|
4031 |
|
---|
4032 | \snippet doc/src/snippets/qstring/main.cpp 85
|
---|
4033 |
|
---|
4034 | The return value is of type QCharRef, a helper class for QString.
|
---|
4035 | When you get an object of type QCharRef, you can use it as if it
|
---|
4036 | were a QChar &. If you assign to it, the assignment will apply to
|
---|
4037 | the character in the QString from which you got the reference.
|
---|
4038 |
|
---|
4039 | \sa at()
|
---|
4040 | */
|
---|
4041 |
|
---|
4042 | /*!
|
---|
4043 | \fn const QChar QString::operator[](int position) const
|
---|
4044 |
|
---|
4045 | \overload operator[]()
|
---|
4046 | */
|
---|
4047 |
|
---|
4048 | /*! \fn QCharRef QString::operator[](uint position)
|
---|
4049 |
|
---|
4050 | \overload operator[]()
|
---|
4051 |
|
---|
4052 | Returns the character at the specified \a position in the string as a
|
---|
4053 | modifiable reference. Equivalent to \c at(position).
|
---|
4054 | */
|
---|
4055 |
|
---|
4056 | /*! \fn const QChar QString::operator[](uint position) const
|
---|
4057 |
|
---|
4058 | \overload operator[]()
|
---|
4059 | */
|
---|
4060 |
|
---|
4061 | /*!
|
---|
4062 | \fn void QString::truncate(int position)
|
---|
4063 |
|
---|
4064 | Truncates the string at the given \a position index.
|
---|
4065 |
|
---|
4066 | If the specified \a position index is beyond the end of the
|
---|
4067 | string, nothing happens.
|
---|
4068 |
|
---|
4069 | Example:
|
---|
4070 |
|
---|
4071 | \snippet doc/src/snippets/qstring/main.cpp 83
|
---|
4072 |
|
---|
4073 | If \a position is negative, it is equivalent to passing zero.
|
---|
4074 |
|
---|
4075 | \sa chop(), resize(), left()
|
---|
4076 | */
|
---|
4077 |
|
---|
4078 | void QString::truncate(int pos)
|
---|
4079 | {
|
---|
4080 | if (pos < d->size)
|
---|
4081 | resize(pos);
|
---|
4082 | }
|
---|
4083 |
|
---|
4084 |
|
---|
4085 | /*!
|
---|
4086 | Removes \a n characters from the end of the string.
|
---|
4087 |
|
---|
4088 | If \a n is greater than size(), the result is an empty string.
|
---|
4089 |
|
---|
4090 | Example:
|
---|
4091 | \snippet doc/src/snippets/qstring/main.cpp 15
|
---|
4092 |
|
---|
4093 | If you want to remove characters from the \e beginning of the
|
---|
4094 | string, use remove() instead.
|
---|
4095 |
|
---|
4096 | \sa truncate(), resize(), remove()
|
---|
4097 | */
|
---|
4098 | void QString::chop(int n)
|
---|
4099 | {
|
---|
4100 | if (n > 0)
|
---|
4101 | resize(d->size - n);
|
---|
4102 | }
|
---|
4103 |
|
---|
4104 | /*!
|
---|
4105 | Sets every character in the string to character \a ch. If \a size
|
---|
4106 | is different from -1 (default), the string is resized to \a
|
---|
4107 | size beforehand.
|
---|
4108 |
|
---|
4109 | Example:
|
---|
4110 |
|
---|
4111 | \snippet doc/src/snippets/qstring/main.cpp 21
|
---|
4112 |
|
---|
4113 | \sa resize()
|
---|
4114 | */
|
---|
4115 |
|
---|
4116 | QString& QString::fill(QChar ch, int size)
|
---|
4117 | {
|
---|
4118 | resize(size < 0 ? d->size : size);
|
---|
4119 | if (d->size) {
|
---|
4120 | QChar *i = (QChar*)d->data + d->size;
|
---|
4121 | QChar *b = (QChar*)d->data;
|
---|
4122 | while (i != b)
|
---|
4123 | *--i = ch;
|
---|
4124 | }
|
---|
4125 | return *this;
|
---|
4126 | }
|
---|
4127 |
|
---|
4128 | /*!
|
---|
4129 | \fn int QString::length() const
|
---|
4130 |
|
---|
4131 | Returns the number of characters in this string. Equivalent to
|
---|
4132 | size().
|
---|
4133 |
|
---|
4134 | \sa setLength()
|
---|
4135 | */
|
---|
4136 |
|
---|
4137 | /*!
|
---|
4138 | \fn int QString::size() const
|
---|
4139 |
|
---|
4140 | Returns the number of characters in this string.
|
---|
4141 |
|
---|
4142 | The last character in the string is at position size() - 1. In
|
---|
4143 | addition, QString ensures that the character at position size()
|
---|
4144 | is always '\\0', so that you can use the return value of data()
|
---|
4145 | and constData() as arguments to functions that expect
|
---|
4146 | '\\0'-terminated strings.
|
---|
4147 |
|
---|
4148 | Example:
|
---|
4149 |
|
---|
4150 | \snippet doc/src/snippets/qstring/main.cpp 58
|
---|
4151 |
|
---|
4152 | \sa isEmpty(), resize()
|
---|
4153 | */
|
---|
4154 |
|
---|
4155 | /*! \fn bool QString::isNull() const
|
---|
4156 |
|
---|
4157 | Returns true if this string is null; otherwise returns false.
|
---|
4158 |
|
---|
4159 | Example:
|
---|
4160 |
|
---|
4161 | \snippet doc/src/snippets/qstring/main.cpp 28
|
---|
4162 |
|
---|
4163 | Qt makes a distinction between null strings and empty strings for
|
---|
4164 | historical reasons. For most applications, what matters is
|
---|
4165 | whether or not a string contains any data, and this can be
|
---|
4166 | determined using the isEmpty() function.
|
---|
4167 |
|
---|
4168 | \sa isEmpty()
|
---|
4169 | */
|
---|
4170 |
|
---|
4171 | /*! \fn bool QString::isEmpty() const
|
---|
4172 |
|
---|
4173 | Returns true if the string has no characters; otherwise returns
|
---|
4174 | false.
|
---|
4175 |
|
---|
4176 | Example:
|
---|
4177 |
|
---|
4178 | \snippet doc/src/snippets/qstring/main.cpp 27
|
---|
4179 |
|
---|
4180 | \sa size()
|
---|
4181 | */
|
---|
4182 |
|
---|
4183 | /*! \fn QString &QString::operator+=(const QString &other)
|
---|
4184 |
|
---|
4185 | Appends the string \a other onto the end of this string and
|
---|
4186 | returns a reference to this string.
|
---|
4187 |
|
---|
4188 | Example:
|
---|
4189 |
|
---|
4190 | \snippet doc/src/snippets/qstring/main.cpp 84
|
---|
4191 |
|
---|
4192 | This operation is typically very fast (\l{constant time}),
|
---|
4193 | because QString preallocates extra space at the end of the string
|
---|
4194 | data so it can grow without reallocating the entire string each
|
---|
4195 | time.
|
---|
4196 |
|
---|
4197 | \sa append(), prepend()
|
---|
4198 | */
|
---|
4199 |
|
---|
4200 | /*! \fn QString &QString::operator+=(const QLatin1String &str)
|
---|
4201 |
|
---|
4202 | \overload operator+=()
|
---|
4203 |
|
---|
4204 | Appends the Latin-1 string \a str to this string.
|
---|
4205 | */
|
---|
4206 |
|
---|
4207 | /*! \fn QString &QString::operator+=(const QByteArray &ba)
|
---|
4208 |
|
---|
4209 | \overload operator+=()
|
---|
4210 |
|
---|
4211 | Appends the byte array \a ba to this string. The byte array is
|
---|
4212 | converted to Unicode using the fromAscii() function.
|
---|
4213 |
|
---|
4214 | You can disable this function by defining \c
|
---|
4215 | QT_NO_CAST_FROM_ASCII when you compile your applications. This
|
---|
4216 | can be useful if you want to ensure that all user-visible strings
|
---|
4217 | go through QObject::tr(), for example.
|
---|
4218 | */
|
---|
4219 |
|
---|
4220 | /*! \fn QString &QString::operator+=(const char *str)
|
---|
4221 |
|
---|
4222 | \overload operator+=()
|
---|
4223 |
|
---|
4224 | Appends the string \a str to this string. The const char pointer
|
---|
4225 | is converted to Unicode using the fromAscii() function.
|
---|
4226 |
|
---|
4227 | You can disable this function by defining \c QT_NO_CAST_FROM_ASCII
|
---|
4228 | when you compile your applications. This can be useful if you want
|
---|
4229 | to ensure that all user-visible strings go through QObject::tr(),
|
---|
4230 | for example.
|
---|
4231 | */
|
---|
4232 |
|
---|
4233 | /*! \fn QString &QString::operator+=(const QStringRef &str)
|
---|
4234 |
|
---|
4235 | \overload operator+=()
|
---|
4236 |
|
---|
4237 | Appends the string section referenced by \a str to this string.
|
---|
4238 | */
|
---|
4239 |
|
---|
4240 | /*! \fn QString &QString::operator+=(char ch)
|
---|
4241 |
|
---|
4242 | \overload operator+=()
|
---|
4243 |
|
---|
4244 | Appends the character \a ch to this string. The character is
|
---|
4245 | converted to Unicode using the fromAscii() function.
|
---|
4246 |
|
---|
4247 | You can disable this function by defining \c QT_NO_CAST_FROM_ASCII
|
---|
4248 | when you compile your applications. This can be useful if you want
|
---|
4249 | to ensure that all user-visible strings go through QObject::tr(),
|
---|
4250 | for example.
|
---|
4251 | */
|
---|
4252 |
|
---|
4253 | /*! \fn QString &QString::operator+=(QChar ch)
|
---|
4254 |
|
---|
4255 | \overload operator+=()
|
---|
4256 |
|
---|
4257 | Appends the character \a ch to the string.
|
---|
4258 | */
|
---|
4259 |
|
---|
4260 | /*! \fn QString &QString::operator+=(QChar::SpecialCharacter c)
|
---|
4261 |
|
---|
4262 | \overload operator+=()
|
---|
4263 |
|
---|
4264 | \internal
|
---|
4265 | */
|
---|
4266 |
|
---|
4267 | /*!
|
---|
4268 | \fn bool operator==(const char *s1, const QString &s2)
|
---|
4269 |
|
---|
4270 | \overload operator==()
|
---|
4271 | \relates QString
|
---|
4272 |
|
---|
4273 | Returns true if \a s1 is equal to \a s2; otherwise returns false.
|
---|
4274 | Note that no string is equal to \a s1 being 0.
|
---|
4275 |
|
---|
4276 | Equivalent to \c {s1 != 0 && compare(s1, s2) == 0}.
|
---|
4277 |
|
---|
4278 | \sa QString::compare()
|
---|
4279 | */
|
---|
4280 |
|
---|
4281 | /*!
|
---|
4282 | \fn bool operator!=(const char *s1, const QString &s2)
|
---|
4283 | \relates QString
|
---|
4284 |
|
---|
4285 | Returns true if \a s1 is not equal to \a s2; otherwise returns
|
---|
4286 | false.
|
---|
4287 |
|
---|
4288 | For \a s1 != 0, this is equivalent to \c {compare(} \a s1, \a s2
|
---|
4289 | \c {) != 0}. Note that no string is equal to \a s1 being 0.
|
---|
4290 |
|
---|
4291 | \sa QString::compare()
|
---|
4292 | */
|
---|
4293 |
|
---|
4294 | /*!
|
---|
4295 | \fn bool operator<(const char *s1, const QString &s2)
|
---|
4296 | \relates QString
|
---|
4297 |
|
---|
4298 | Returns true if \a s1 is lexically less than \a s2; otherwise
|
---|
4299 | returns false. For \a s1 != 0, this is equivalent to \c
|
---|
4300 | {compare(s1, s2) < 0}.
|
---|
4301 |
|
---|
4302 | The comparison is based exclusively on the numeric Unicode values
|
---|
4303 | of the characters and is very fast, but is not what a human would
|
---|
4304 | expect. Consider sorting user-interface strings using the
|
---|
4305 | QString::localeAwareCompare() function.
|
---|
4306 |
|
---|
4307 | \sa QString::compare()
|
---|
4308 | */
|
---|
4309 |
|
---|
4310 | /*!
|
---|
4311 | \fn bool operator<=(const char *s1, const QString &s2)
|
---|
4312 | \relates QString
|
---|
4313 |
|
---|
4314 | Returns true if \a s1 is lexically less than or equal to \a s2;
|
---|
4315 | otherwise returns false. For \a s1 != 0, this is equivalent to \c
|
---|
4316 | {compare(s1, s2) <= 0}.
|
---|
4317 |
|
---|
4318 | The comparison is based exclusively on the numeric Unicode values
|
---|
4319 | of the characters and is very fast, but is not what a human would
|
---|
4320 | expect. Consider sorting user-interface strings with
|
---|
4321 | QString::localeAwareCompare().
|
---|
4322 |
|
---|
4323 | \sa QString::compare()
|
---|
4324 | */
|
---|
4325 |
|
---|
4326 | /*!
|
---|
4327 | \fn bool operator>(const char *s1, const QString &s2)
|
---|
4328 | \relates QString
|
---|
4329 |
|
---|
4330 | Returns true if \a s1 is lexically greater than \a s2; otherwise
|
---|
4331 | returns false. Equivalent to \c {compare(s1, s2) > 0}.
|
---|
4332 |
|
---|
4333 | The comparison is based exclusively on the numeric Unicode values
|
---|
4334 | of the characters and is very fast, but is not what a human would
|
---|
4335 | expect. Consider sorting user-interface strings using the
|
---|
4336 | QString::localeAwareCompare() function.
|
---|
4337 |
|
---|
4338 | \sa QString::compare()
|
---|
4339 | */
|
---|
4340 |
|
---|
4341 | /*!
|
---|
4342 | \fn bool operator>=(const char *s1, const QString &s2)
|
---|
4343 | \relates QString
|
---|
4344 |
|
---|
4345 | Returns true if \a s1 is lexically greater than or equal to \a s2;
|
---|
4346 | otherwise returns false. For \a s1 != 0, this is equivalent to \c
|
---|
4347 | {compare(s1, s2) >= 0}.
|
---|
4348 |
|
---|
4349 | The comparison is based exclusively on the numeric Unicode values
|
---|
4350 | of the characters and is very fast, but is not what a human would
|
---|
4351 | expect. Consider sorting user-interface strings using the
|
---|
4352 | QString::localeAwareCompare() function.
|
---|
4353 | */
|
---|
4354 |
|
---|
4355 | /*!
|
---|
4356 | \fn const QString operator+(const QString &s1, const QString &s2)
|
---|
4357 | \relates QString
|
---|
4358 |
|
---|
4359 | Returns a string which is the result of concatenating \a s1 and \a
|
---|
4360 | s2.
|
---|
4361 | */
|
---|
4362 |
|
---|
4363 | /*!
|
---|
4364 | \fn const QString operator+(const QString &s1, const char *s2)
|
---|
4365 | \relates QString
|
---|
4366 |
|
---|
4367 | Returns a string which is the result of concatenating \a s1 and \a
|
---|
4368 | s2 (\a s2 is converted to Unicode using the QString::fromAscii()
|
---|
4369 | function).
|
---|
4370 |
|
---|
4371 | \sa QString::fromAscii()
|
---|
4372 | */
|
---|
4373 |
|
---|
4374 | /*!
|
---|
4375 | \fn const QString operator+(const char *s1, const QString &s2)
|
---|
4376 | \relates QString
|
---|
4377 |
|
---|
4378 | Returns a string which is the result of concatenating \a s1 and \a
|
---|
4379 | s2 (\a s1 is converted to Unicode using the QString::fromAscii()
|
---|
4380 | function).
|
---|
4381 |
|
---|
4382 | \sa QString::fromAscii()
|
---|
4383 | */
|
---|
4384 |
|
---|
4385 | /*!
|
---|
4386 | \fn const QString operator+(const QString &s, char ch)
|
---|
4387 | \relates QString
|
---|
4388 |
|
---|
4389 | Returns a string which is the result of concatenating the string
|
---|
4390 | \a s and the character \a ch.
|
---|
4391 | */
|
---|
4392 |
|
---|
4393 | /*!
|
---|
4394 | \fn const QString operator+(char ch, const QString &s)
|
---|
4395 | \relates QString
|
---|
4396 |
|
---|
4397 | Returns a string which is the result of concatenating the
|
---|
4398 | character \a ch and the string \a s.
|
---|
4399 | */
|
---|
4400 |
|
---|
4401 | /*!
|
---|
4402 | \fn int QString::compare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs)
|
---|
4403 | \since 4.2
|
---|
4404 |
|
---|
4405 | Compares \a s1 with \a s2 and returns an integer less than, equal
|
---|
4406 | to, or greater than zero if \a s1 is less than, equal to, or
|
---|
4407 | greater than \a s2.
|
---|
4408 |
|
---|
4409 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
|
---|
4410 | otherwise the comparison is case insensitive.
|
---|
4411 |
|
---|
4412 | Case sensitive comparison is based exclusively on the numeric
|
---|
4413 | Unicode values of the characters and is very fast, but is not what
|
---|
4414 | a human would expect. Consider sorting user-visible strings with
|
---|
4415 | localeAwareCompare().
|
---|
4416 |
|
---|
4417 | \snippet doc/src/snippets/qstring/main.cpp 16
|
---|
4418 |
|
---|
4419 | \sa operator==(), operator<(), operator>()
|
---|
4420 | */
|
---|
4421 |
|
---|
4422 | /*!
|
---|
4423 | \fn int QString::compare(const QString & s1, const QString & s2)
|
---|
4424 |
|
---|
4425 | \overload compare()
|
---|
4426 |
|
---|
4427 | Performs a case sensitive compare of \a s1 and \a s2.
|
---|
4428 | */
|
---|
4429 |
|
---|
4430 | /*!
|
---|
4431 | \fn int QString::compare(const QString &s1, const QLatin1String &s2, Qt::CaseSensitivity cs)
|
---|
4432 | \since 4.2
|
---|
4433 | \overload compare()
|
---|
4434 |
|
---|
4435 | Performs a comparison of \a s1 and \a s2, using the case
|
---|
4436 | sensitivity setting \a cs.
|
---|
4437 | */
|
---|
4438 |
|
---|
4439 | /*!
|
---|
4440 | \fn int QString::compare(const QLatin1String &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
|
---|
4441 |
|
---|
4442 | \since 4.2
|
---|
4443 | \overload compare()
|
---|
4444 |
|
---|
4445 | Performs a comparison of \a s1 and \a s2, using the case
|
---|
4446 | sensitivity setting \a cs.
|
---|
4447 | */
|
---|
4448 |
|
---|
4449 | /*!
|
---|
4450 | \overload compare()
|
---|
4451 |
|
---|
4452 | Lexically compares this string with the \a other string and
|
---|
4453 | returns an integer less than, equal to, or greater than zero if
|
---|
4454 | this string is less than, equal to, or greater than the other
|
---|
4455 | string.
|
---|
4456 |
|
---|
4457 | Equivalent to \c {compare(*this, other)}.
|
---|
4458 | */
|
---|
4459 | int QString::compare(const QString &other) const
|
---|
4460 | {
|
---|
4461 | return ucstrcmp(constData(), length(), other.constData(), other.length());
|
---|
4462 | }
|
---|
4463 |
|
---|
4464 | /*!
|
---|
4465 | \overload compare()
|
---|
4466 | \since 4.2
|
---|
4467 |
|
---|
4468 | Same as compare(*this, \a other, \a cs).
|
---|
4469 | */
|
---|
4470 | int QString::compare(const QString &other, Qt::CaseSensitivity cs) const
|
---|
4471 | {
|
---|
4472 | if (cs == Qt::CaseSensitive)
|
---|
4473 | return ucstrcmp(constData(), length(), other.constData(), other.length());
|
---|
4474 | return ucstricmp(d->data, d->data + d->size, other.d->data, other.d->data + other.d->size);
|
---|
4475 | }
|
---|
4476 |
|
---|
4477 | /*!
|
---|
4478 | \internal
|
---|
4479 | \since 4.5
|
---|
4480 | */
|
---|
4481 | int QString::compare_helper(const QChar *data1, int length1, const QChar *data2, int length2,
|
---|
4482 | Qt::CaseSensitivity cs)
|
---|
4483 | {
|
---|
4484 | if (cs == Qt::CaseSensitive)
|
---|
4485 | return ucstrcmp(data1, length1, data2, length2);
|
---|
4486 | register const ushort *s1 = reinterpret_cast<const ushort *>(data1);
|
---|
4487 | register const ushort *s2 = reinterpret_cast<const ushort *>(data2);
|
---|
4488 | return ucstricmp(s1, s1 + length1, s2, s2 + length2);
|
---|
4489 | }
|
---|
4490 |
|
---|
4491 | /*!
|
---|
4492 | \overload compare()
|
---|
4493 | \since 4.2
|
---|
4494 |
|
---|
4495 | Same as compare(*this, \a other, \a cs).
|
---|
4496 | */
|
---|
4497 | int QString::compare(const QLatin1String &other, Qt::CaseSensitivity cs) const
|
---|
4498 | {
|
---|
4499 | return compare_helper(unicode(), length(), other, cs);
|
---|
4500 | }
|
---|
4501 |
|
---|
4502 | /*!
|
---|
4503 | \fn int QString::compare(const QStringRef &ref, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
|
---|
4504 | \overload compare()
|
---|
4505 |
|
---|
4506 | Compares the string reference, \a ref, with the string and returns
|
---|
4507 | an integer less than, equal to, or greater than zero if the string
|
---|
4508 | is less than, equal to, or greater than \a ref.
|
---|
4509 | */
|
---|
4510 |
|
---|
4511 | /*!
|
---|
4512 | \fn int QString::compare(const QString &s1, const QStringRef &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
|
---|
4513 | \overload compare()
|
---|
4514 | */
|
---|
4515 |
|
---|
4516 | /*!
|
---|
4517 | \internal
|
---|
4518 | \since 4.5
|
---|
4519 | */
|
---|
4520 | int QString::compare_helper(const QChar *data1, int length1, QLatin1String s2,
|
---|
4521 | Qt::CaseSensitivity cs)
|
---|
4522 | {
|
---|
4523 | const ushort *uc = reinterpret_cast<const ushort *>(data1);
|
---|
4524 | const ushort *e = uc + length1;
|
---|
4525 | const uchar *c = (uchar *)s2.latin1();
|
---|
4526 |
|
---|
4527 | if (!c)
|
---|
4528 | return length1;
|
---|
4529 |
|
---|
4530 | if (cs == Qt::CaseSensitive) {
|
---|
4531 | while (uc != e && *c && *uc == *c)
|
---|
4532 | uc++, c++;
|
---|
4533 |
|
---|
4534 | return *uc - *c;
|
---|
4535 | } else {
|
---|
4536 | return ucstricmp(uc, e, c);
|
---|
4537 | }
|
---|
4538 | }
|
---|
4539 |
|
---|
4540 | /*!
|
---|
4541 | \fn int QString::localeAwareCompare(const QString & s1, const QString & s2)
|
---|
4542 |
|
---|
4543 | Compares \a s1 with \a s2 and returns an integer less than, equal
|
---|
4544 | to, or greater than zero if \a s1 is less than, equal to, or
|
---|
4545 | greater than \a s2.
|
---|
4546 |
|
---|
4547 | The comparison is performed in a locale- and also
|
---|
4548 | platform-dependent manner. Use this function to present sorted
|
---|
4549 | lists of strings to the user.
|
---|
4550 |
|
---|
4551 | On Mac OS X since Qt 4.3, this function compares according the
|
---|
4552 | "Order for sorted lists" setting in the International prefereces panel.
|
---|
4553 |
|
---|
4554 | \sa compare(), QTextCodec::locale()
|
---|
4555 | */
|
---|
4556 |
|
---|
4557 | /*!
|
---|
4558 | \fn int QString::localeAwareCompare(const QStringRef &other) const
|
---|
4559 | \since 4.5
|
---|
4560 | \overload localeAwareCompare()
|
---|
4561 |
|
---|
4562 | Compares this string with the \a other string and returns an
|
---|
4563 | integer less than, equal to, or greater than zero if this string
|
---|
4564 | is less than, equal to, or greater than the \a other string.
|
---|
4565 |
|
---|
4566 | The comparison is performed in a locale- and also
|
---|
4567 | platform-dependent manner. Use this function to present sorted
|
---|
4568 | lists of strings to the user.
|
---|
4569 |
|
---|
4570 | Same as \c {localeAwareCompare(*this, other)}.
|
---|
4571 | */
|
---|
4572 |
|
---|
4573 | /*!
|
---|
4574 | \fn int QString::localeAwareCompare(const QString &s1, const QStringRef &s2)
|
---|
4575 | \since 4.5
|
---|
4576 | \overload localeAwareCompare()
|
---|
4577 |
|
---|
4578 | Compares \a s1 with \a s2 and returns an integer less than, equal
|
---|
4579 | to, or greater than zero if \a s1 is less than, equal to, or
|
---|
4580 | greater than \a s2.
|
---|
4581 |
|
---|
4582 | The comparison is performed in a locale- and also
|
---|
4583 | platform-dependent manner. Use this function to present sorted
|
---|
4584 | lists of strings to the user.
|
---|
4585 | */
|
---|
4586 |
|
---|
4587 |
|
---|
4588 | #if !defined(CSTR_LESS_THAN)
|
---|
4589 | #define CSTR_LESS_THAN 1
|
---|
4590 | #define CSTR_EQUAL 2
|
---|
4591 | #define CSTR_GREATER_THAN 3
|
---|
4592 | #endif
|
---|
4593 |
|
---|
4594 | /*!
|
---|
4595 | \overload localeAwareCompare()
|
---|
4596 |
|
---|
4597 | Compares this string with the \a other string and returns an
|
---|
4598 | integer less than, equal to, or greater than zero if this string
|
---|
4599 | is less than, equal to, or greater than the \a other string.
|
---|
4600 |
|
---|
4601 | The comparison is performed in a locale- and also
|
---|
4602 | platform-dependent manner. Use this function to present sorted
|
---|
4603 | lists of strings to the user.
|
---|
4604 |
|
---|
4605 | Same as \c {localeAwareCompare(*this, other)}.
|
---|
4606 | */
|
---|
4607 | int QString::localeAwareCompare(const QString &other) const
|
---|
4608 | {
|
---|
4609 | return localeAwareCompare_helper(constData(), length(), other.constData(), other.length());
|
---|
4610 | }
|
---|
4611 |
|
---|
4612 | /*!
|
---|
4613 | \internal
|
---|
4614 | \since 4.5
|
---|
4615 | */
|
---|
4616 | int QString::localeAwareCompare_helper(const QChar *data1, int length1,
|
---|
4617 | const QChar *data2, int length2)
|
---|
4618 | {
|
---|
4619 | // do the right thing for null and empty
|
---|
4620 | if (length1 == 0 || length2 == 0)
|
---|
4621 | return ucstrcmp(data1, length1, data2, length2);
|
---|
4622 |
|
---|
4623 | #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
|
---|
4624 | int res = CompareString(GetUserDefaultLCID(), 0, (wchar_t*)data1, length1, (wchar_t*)data2, length2);
|
---|
4625 |
|
---|
4626 | switch (res) {
|
---|
4627 | case CSTR_LESS_THAN:
|
---|
4628 | return -1;
|
---|
4629 | case CSTR_GREATER_THAN:
|
---|
4630 | return 1;
|
---|
4631 | default:
|
---|
4632 | return 0;
|
---|
4633 | }
|
---|
4634 | #elif defined(Q_OS_OS2) && !defined(QT_NO_TEXTCODEC)
|
---|
4635 | #if !defined(QT_NO_THREAD)
|
---|
4636 | static QAtomicPointer<void> localeObj;
|
---|
4637 | if (!localeObj) {
|
---|
4638 | LocaleObject lo = 0;
|
---|
4639 | UniCreateLocaleObject(UNI_UCS_STRING_POINTER, (UniChar *)L"", &lo);
|
---|
4640 | if (!localeObj.testAndSetRelaxed(0, lo))
|
---|
4641 | UniFreeLocaleObject(lo); // we are too late
|
---|
4642 | }
|
---|
4643 | #else
|
---|
4644 | static LocaleObject localeObj = 0;
|
---|
4645 | if (!localeObj)
|
---|
4646 | UniCreateLocaleObject(UNI_UCS_STRING_POINTER, (UniChar *)L"", &localeObj);
|
---|
4647 | #endif
|
---|
4648 | if (localeObj) {
|
---|
4649 | // note: we assume that data1 and data2 are zero-terminated (to avoid
|
---|
4650 | // creation of dumb deep copies) which is at least true for 4.5.1.
|
---|
4651 | #if QT_VERSION == 0x040501
|
---|
4652 | return UniStrcoll(localeObj, (UniChar*)data1, (UniChar *)data2);
|
---|
4653 | #else
|
---|
4654 | # error "Check me!"
|
---|
4655 | #endif
|
---|
4656 | } else {
|
---|
4657 | return ucstrcmp(data1, length1, data2, length2);
|
---|
4658 | }
|
---|
4659 | #elif defined (Q_OS_MAC)
|
---|
4660 | // Use CFStringCompare for comparing strings on Mac. This makes Qt order
|
---|
4661 | // strings the same way as native applications do, and also respects
|
---|
4662 | // the "Order for sorted lists" setting in the International preferences
|
---|
4663 | // panel.
|
---|
4664 | const CFStringRef thisString =
|
---|
4665 | CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault,
|
---|
4666 | reinterpret_cast<const UniChar *>(data1), length1, kCFAllocatorNull);
|
---|
4667 | const CFStringRef otherString =
|
---|
4668 | CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault,
|
---|
4669 | reinterpret_cast<const UniChar *>(data2), length2, kCFAllocatorNull);
|
---|
4670 |
|
---|
4671 | const int result = CFStringCompare(thisString, otherString, kCFCompareLocalized);
|
---|
4672 | CFRelease(thisString);
|
---|
4673 | CFRelease(otherString);
|
---|
4674 | return result;
|
---|
4675 | #elif defined(Q_OS_UNIX)
|
---|
4676 | // declared in <string.h>
|
---|
4677 | int delta = strcoll(toLocal8Bit_helper(data1, length1), toLocal8Bit_helper(data2, length2));
|
---|
4678 | if (delta == 0)
|
---|
4679 | delta = ucstrcmp(data1, length1, data2, length2);
|
---|
4680 | return delta;
|
---|
4681 | #else
|
---|
4682 | return ucstrcmp(data1, length1, data2, length2);
|
---|
4683 | #endif
|
---|
4684 | }
|
---|
4685 |
|
---|
4686 |
|
---|
4687 | /*!
|
---|
4688 | \fn const QChar *QString::unicode() const
|
---|
4689 |
|
---|
4690 | Returns a '\\0'-terminated Unicode representation of the string.
|
---|
4691 | The result remains valid until the string is modified.
|
---|
4692 |
|
---|
4693 | \sa utf16()
|
---|
4694 | */
|
---|
4695 |
|
---|
4696 | /*!
|
---|
4697 | \fn const ushort *QString::utf16() const
|
---|
4698 |
|
---|
4699 | Returns the QString as a '\\0\'-terminated array of unsigned
|
---|
4700 | shorts. The result remains valid until the string is modified.
|
---|
4701 |
|
---|
4702 | \sa unicode()
|
---|
4703 | */
|
---|
4704 |
|
---|
4705 | const ushort *QString::utf16() const
|
---|
4706 | {
|
---|
4707 | if (d->data != d->array) {
|
---|
4708 | QString *that = const_cast<QString*>(this);
|
---|
4709 | that->realloc(); // ensure '\\0'-termination for ::fromRawData strings
|
---|
4710 | return that->d->data;
|
---|
4711 | }
|
---|
4712 | return d->array;
|
---|
4713 | }
|
---|
4714 |
|
---|
4715 | /*!
|
---|
4716 | Returns a string of size \a width that contains this string
|
---|
4717 | padded by the \a fill character.
|
---|
4718 |
|
---|
4719 | If \a truncate is false and the size() of the string is more than
|
---|
4720 | \a width, then the returned string is a copy of the string.
|
---|
4721 |
|
---|
4722 | \snippet doc/src/snippets/qstring/main.cpp 32
|
---|
4723 |
|
---|
4724 | If \a truncate is true and the size() of the string is more than
|
---|
4725 | \a width, then any characters in a copy of the string after
|
---|
4726 | position \a width are removed, and the copy is returned.
|
---|
4727 |
|
---|
4728 | \snippet doc/src/snippets/qstring/main.cpp 33
|
---|
4729 |
|
---|
4730 | \sa rightJustified()
|
---|
4731 | */
|
---|
4732 |
|
---|
4733 | QString QString::leftJustified(int width, QChar fill, bool truncate) const
|
---|
4734 | {
|
---|
4735 | QString result;
|
---|
4736 | int len = length();
|
---|
4737 | int padlen = width - len;
|
---|
4738 | if (padlen > 0) {
|
---|
4739 | result.resize(len+padlen);
|
---|
4740 | if (len)
|
---|
4741 | memcpy(result.d->data, d->data, sizeof(QChar)*len);
|
---|
4742 | QChar *uc = (QChar*)result.d->data + len;
|
---|
4743 | while (padlen--)
|
---|
4744 | * uc++ = fill;
|
---|
4745 | } else {
|
---|
4746 | if (truncate)
|
---|
4747 | result = left(width);
|
---|
4748 | else
|
---|
4749 | result = *this;
|
---|
4750 | }
|
---|
4751 | return result;
|
---|
4752 | }
|
---|
4753 |
|
---|
4754 | /*!
|
---|
4755 | Returns a string of size() \a width that contains the \a fill
|
---|
4756 | character followed by the string. For example:
|
---|
4757 |
|
---|
4758 | \snippet doc/src/snippets/qstring/main.cpp 49
|
---|
4759 |
|
---|
4760 | If \a truncate is false and the size() of the string is more than
|
---|
4761 | \a width, then the returned string is a copy of the string.
|
---|
4762 |
|
---|
4763 | If \a truncate is true and the size() of the string is more than
|
---|
4764 | \a width, then the resulting string is truncated at position \a
|
---|
4765 | width.
|
---|
4766 |
|
---|
4767 | \snippet doc/src/snippets/qstring/main.cpp 50
|
---|
4768 |
|
---|
4769 | \sa leftJustified()
|
---|
4770 | */
|
---|
4771 |
|
---|
4772 | QString QString::rightJustified(int width, QChar fill, bool truncate) const
|
---|
4773 | {
|
---|
4774 | QString result;
|
---|
4775 | int len = length();
|
---|
4776 | int padlen = width - len;
|
---|
4777 | if (padlen > 0) {
|
---|
4778 | result.resize(len+padlen);
|
---|
4779 | QChar *uc = (QChar*)result.d->data;
|
---|
4780 | while (padlen--)
|
---|
4781 | * uc++ = fill;
|
---|
4782 | if (len)
|
---|
4783 | memcpy(uc, d->data, sizeof(QChar)*len);
|
---|
4784 | } else {
|
---|
4785 | if (truncate)
|
---|
4786 | result = left(width);
|
---|
4787 | else
|
---|
4788 | result = *this;
|
---|
4789 | }
|
---|
4790 | return result;
|
---|
4791 | }
|
---|
4792 |
|
---|
4793 | /*!
|
---|
4794 | Returns a lowercase copy of the string.
|
---|
4795 |
|
---|
4796 | \snippet doc/src/snippets/qstring/main.cpp 75
|
---|
4797 |
|
---|
4798 | \sa toUpper()
|
---|
4799 | */
|
---|
4800 |
|
---|
4801 | QString QString::toLower() const
|
---|
4802 | {
|
---|
4803 | const ushort *p = d->data;
|
---|
4804 | if (!p)
|
---|
4805 | return *this;
|
---|
4806 | if (!d->size)
|
---|
4807 | return *this;
|
---|
4808 |
|
---|
4809 | const ushort *e = d->data + d->size;
|
---|
4810 |
|
---|
4811 | // this avoids one out of bounds check in the loop
|
---|
4812 | if (QChar(*p).isLowSurrogate())
|
---|
4813 | ++p;
|
---|
4814 |
|
---|
4815 | while (p != e) {
|
---|
4816 | uint c = *p;
|
---|
4817 | if (QChar(c).isLowSurrogate() && QChar(*(p - 1)).isHighSurrogate())
|
---|
4818 | c = QChar::surrogateToUcs4(*(p - 1), c);
|
---|
4819 | const QUnicodeTables::Properties *prop = qGetProp(c);
|
---|
4820 | if (prop->lowerCaseDiff || prop->lowerCaseSpecial) {
|
---|
4821 | QString s(d->size, Qt::Uninitialized);
|
---|
4822 | memcpy(s.d->data, d->data, (p - d->data)*sizeof(ushort));
|
---|
4823 | ushort *pp = s.d->data + (p - d->data);
|
---|
4824 | while (p < e) {
|
---|
4825 | uint c = *p;
|
---|
4826 | if (QChar(c).isLowSurrogate() && QChar(*(p - 1)).isHighSurrogate())
|
---|
4827 | c = QChar::surrogateToUcs4(*(p - 1), c);
|
---|
4828 | prop = qGetProp(c);
|
---|
4829 | if (prop->lowerCaseSpecial) {
|
---|
4830 | int pos = pp - s.d->data;
|
---|
4831 | s.resize(s.d->size + SPECIAL_CASE_MAX_LEN);
|
---|
4832 | pp = s.d->data + pos;
|
---|
4833 | const ushort *specialCase = specialCaseMap + prop->lowerCaseDiff;
|
---|
4834 | while (*specialCase)
|
---|
4835 | *pp++ = *specialCase++;
|
---|
4836 | } else {
|
---|
4837 | *pp++ = *p + prop->lowerCaseDiff;
|
---|
4838 | }
|
---|
4839 | ++p;
|
---|
4840 | }
|
---|
4841 | s.truncate(pp - s.d->data);
|
---|
4842 | return s;
|
---|
4843 | }
|
---|
4844 | ++p;
|
---|
4845 | }
|
---|
4846 | return *this;
|
---|
4847 | }
|
---|
4848 |
|
---|
4849 | /*!
|
---|
4850 | Returns the case folded equivalent of the string. For most Unicode
|
---|
4851 | characters this is the same as toLower().
|
---|
4852 | */
|
---|
4853 | QString QString::toCaseFolded() const
|
---|
4854 | {
|
---|
4855 | if (!d->size)
|
---|
4856 | return *this;
|
---|
4857 |
|
---|
4858 | const ushort *p = d->data;
|
---|
4859 | if (!p)
|
---|
4860 | return *this;
|
---|
4861 |
|
---|
4862 | const ushort *e = d->data + d->size;
|
---|
4863 |
|
---|
4864 | uint last = 0;
|
---|
4865 | while (p < e) {
|
---|
4866 | ushort folded = foldCase(*p, last);
|
---|
4867 | if (folded != *p) {
|
---|
4868 | QString s(*this);
|
---|
4869 | s.detach();
|
---|
4870 | ushort *pp = s.d->data + (p - d->data);
|
---|
4871 | const ushort *ppe = s.d->data + s.d->size;
|
---|
4872 | last = pp > s.d->data ? *(pp - 1) : 0;
|
---|
4873 | while (pp < ppe) {
|
---|
4874 | *pp = foldCase(*pp, last);
|
---|
4875 | ++pp;
|
---|
4876 | }
|
---|
4877 | return s;
|
---|
4878 | }
|
---|
4879 | p++;
|
---|
4880 | }
|
---|
4881 | return *this;
|
---|
4882 | }
|
---|
4883 |
|
---|
4884 | /*!
|
---|
4885 | Returns an uppercase copy of the string.
|
---|
4886 |
|
---|
4887 | \snippet doc/src/snippets/qstring/main.cpp 81
|
---|
4888 |
|
---|
4889 | \sa toLower()
|
---|
4890 | */
|
---|
4891 |
|
---|
4892 | QString QString::toUpper() const
|
---|
4893 | {
|
---|
4894 | const ushort *p = d->data;
|
---|
4895 | if (!p)
|
---|
4896 | return *this;
|
---|
4897 | if (!d->size)
|
---|
4898 | return *this;
|
---|
4899 |
|
---|
4900 | const ushort *e = d->data + d->size;
|
---|
4901 |
|
---|
4902 | // this avoids one out of bounds check in the loop
|
---|
4903 | if (QChar(*p).isLowSurrogate())
|
---|
4904 | ++p;
|
---|
4905 |
|
---|
4906 | while (p != e) {
|
---|
4907 | uint c = *p;
|
---|
4908 | if (QChar(c).isLowSurrogate() && QChar(*(p - 1)).isHighSurrogate())
|
---|
4909 | c = QChar::surrogateToUcs4(*(p - 1), c);
|
---|
4910 | const QUnicodeTables::Properties *prop = qGetProp(c);
|
---|
4911 | if (prop->upperCaseDiff || prop->upperCaseSpecial) {
|
---|
4912 | QString s(d->size, Qt::Uninitialized);
|
---|
4913 | memcpy(s.d->data, d->data, (p - d->data)*sizeof(ushort));
|
---|
4914 | ushort *pp = s.d->data + (p - d->data);
|
---|
4915 | while (p < e) {
|
---|
4916 | uint c = *p;
|
---|
4917 | if (QChar(c).isLowSurrogate() && QChar(*(p - 1)).isHighSurrogate())
|
---|
4918 | c = QChar::surrogateToUcs4(*(p - 1), c);
|
---|
4919 | prop = qGetProp(c);
|
---|
4920 | if (prop->upperCaseSpecial) {
|
---|
4921 | int pos = pp - s.d->data;
|
---|
4922 | s.resize(s.d->size + SPECIAL_CASE_MAX_LEN);
|
---|
4923 | pp = s.d->data + pos;
|
---|
4924 | const ushort *specialCase = specialCaseMap + prop->upperCaseDiff;
|
---|
4925 | while (*specialCase)
|
---|
4926 | *pp++ = *specialCase++;
|
---|
4927 | } else {
|
---|
4928 | *pp++ = *p + prop->upperCaseDiff;
|
---|
4929 | }
|
---|
4930 | ++p;
|
---|
4931 | }
|
---|
4932 | s.truncate(pp - s.d->data);
|
---|
4933 | return s;
|
---|
4934 | }
|
---|
4935 | ++p;
|
---|
4936 | }
|
---|
4937 | return *this;
|
---|
4938 | }
|
---|
4939 |
|
---|
4940 | // ### Qt 5: Consider whether this function shouldn't be removed See task 202871.
|
---|
4941 | /*!
|
---|
4942 | Safely builds a formatted string from the format string \a cformat
|
---|
4943 | and an arbitrary list of arguments.
|
---|
4944 |
|
---|
4945 | The %lc escape sequence expects a unicode character of type ushort
|
---|
4946 | (as returned by QChar::unicode()). The %ls escape sequence expects
|
---|
4947 | a pointer to a zero-terminated array of unicode characters of type
|
---|
4948 | ushort (as returned by QString::utf16()).
|
---|
4949 |
|
---|
4950 | \note This function expects a UTF-8 string for %s and Latin-1 for
|
---|
4951 | the format string.
|
---|
4952 |
|
---|
4953 | The format string supports most of the conversion specifiers
|
---|
4954 | provided by printf() in the standard C++ library. It doesn't
|
---|
4955 | honor the length modifiers (e.g. \c h for \c short, \c ll for
|
---|
4956 | \c{long long}). If you need those, use the standard snprintf()
|
---|
4957 | function instead:
|
---|
4958 |
|
---|
4959 | \snippet doc/src/snippets/qstring/main.cpp 63
|
---|
4960 |
|
---|
4961 | \warning We do not recommend using QString::sprintf() in new Qt
|
---|
4962 | code. Instead, consider using QTextStream or arg(), both of
|
---|
4963 | which support Unicode strings seamlessly and are type-safe.
|
---|
4964 | Here's an example that uses QTextStream:
|
---|
4965 |
|
---|
4966 | \snippet doc/src/snippets/qstring/main.cpp 64
|
---|
4967 |
|
---|
4968 | For \l {QObject::tr()}{translations}, especially if the strings
|
---|
4969 | contains more than one escape sequence, you should consider using
|
---|
4970 | the arg() function instead. This allows the order of the
|
---|
4971 | replacements to be controlled by the translator.
|
---|
4972 |
|
---|
4973 | \sa arg()
|
---|
4974 | */
|
---|
4975 |
|
---|
4976 | QString &QString::sprintf(const char *cformat, ...)
|
---|
4977 | {
|
---|
4978 | va_list ap;
|
---|
4979 | va_start(ap, cformat);
|
---|
4980 | QString &s = vsprintf(cformat, ap);
|
---|
4981 | va_end(ap);
|
---|
4982 | return s;
|
---|
4983 | }
|
---|
4984 |
|
---|
4985 | /*!
|
---|
4986 | Equivalent method to sprintf(), but takes a va_list \a ap
|
---|
4987 | instead a list of variable arguments. See the sprintf()
|
---|
4988 | documentation for an explanation of \a cformat.
|
---|
4989 |
|
---|
4990 | This method does not call the va_end macro, the caller
|
---|
4991 | is responsible to call va_end on \a ap.
|
---|
4992 |
|
---|
4993 | \sa sprintf()
|
---|
4994 | */
|
---|
4995 |
|
---|
4996 | QString &QString::vsprintf(const char* cformat, va_list ap)
|
---|
4997 | {
|
---|
4998 | QLocale locale(QLocale::C);
|
---|
4999 |
|
---|
5000 | if (!cformat || !*cformat) {
|
---|
5001 | // Qt 1.x compat
|
---|
5002 | *this = fromLatin1("");
|
---|
5003 | return *this;
|
---|
5004 | }
|
---|
5005 |
|
---|
5006 | // Parse cformat
|
---|
5007 |
|
---|
5008 | QString result;
|
---|
5009 | const char *c = cformat;
|
---|
5010 | for (;;) {
|
---|
5011 | // Copy non-escape chars to result
|
---|
5012 | while (*c != '\0' && *c != '%')
|
---|
5013 | result.append(QLatin1Char(*c++));
|
---|
5014 |
|
---|
5015 | if (*c == '\0')
|
---|
5016 | break;
|
---|
5017 |
|
---|
5018 | // Found '%'
|
---|
5019 | const char *escape_start = c;
|
---|
5020 | ++c;
|
---|
5021 |
|
---|
5022 | if (*c == '\0') {
|
---|
5023 | result.append(QLatin1Char('%')); // a % at the end of the string - treat as non-escape text
|
---|
5024 | break;
|
---|
5025 | }
|
---|
5026 | if (*c == '%') {
|
---|
5027 | result.append(QLatin1Char('%')); // %%
|
---|
5028 | ++c;
|
---|
5029 | continue;
|
---|
5030 | }
|
---|
5031 |
|
---|
5032 | // Parse flag characters
|
---|
5033 | uint flags = 0;
|
---|
5034 | bool no_more_flags = false;
|
---|
5035 | do {
|
---|
5036 | switch (*c) {
|
---|
5037 | case '#': flags |= QLocalePrivate::Alternate; break;
|
---|
5038 | case '0': flags |= QLocalePrivate::ZeroPadded; break;
|
---|
5039 | case '-': flags |= QLocalePrivate::LeftAdjusted; break;
|
---|
5040 | case ' ': flags |= QLocalePrivate::BlankBeforePositive; break;
|
---|
5041 | case '+': flags |= QLocalePrivate::AlwaysShowSign; break;
|
---|
5042 | case '\'': flags |= QLocalePrivate::ThousandsGroup; break;
|
---|
5043 | default: no_more_flags = true; break;
|
---|
5044 | }
|
---|
5045 |
|
---|
5046 | if (!no_more_flags)
|
---|
5047 | ++c;
|
---|
5048 | } while (!no_more_flags);
|
---|
5049 |
|
---|
5050 | if (*c == '\0') {
|
---|
5051 | result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text
|
---|
5052 | break;
|
---|
5053 | }
|
---|
5054 |
|
---|
5055 | // Parse field width
|
---|
5056 | int width = -1; // -1 means unspecified
|
---|
5057 | if (qIsDigit(*c)) {
|
---|
5058 | QString width_str;
|
---|
5059 | while (*c != '\0' && qIsDigit(*c))
|
---|
5060 | width_str.append(QLatin1Char(*c++));
|
---|
5061 |
|
---|
5062 | // can't be negative - started with a digit
|
---|
5063 | // contains at least one digit
|
---|
5064 | width = width_str.toInt();
|
---|
5065 | }
|
---|
5066 | else if (*c == '*') {
|
---|
5067 | width = va_arg(ap, int);
|
---|
5068 | if (width < 0)
|
---|
5069 | width = -1; // treat all negative numbers as unspecified
|
---|
5070 | ++c;
|
---|
5071 | }
|
---|
5072 |
|
---|
5073 | if (*c == '\0') {
|
---|
5074 | result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text
|
---|
5075 | break;
|
---|
5076 | }
|
---|
5077 |
|
---|
5078 | // Parse precision
|
---|
5079 | int precision = -1; // -1 means unspecified
|
---|
5080 | if (*c == '.') {
|
---|
5081 | ++c;
|
---|
5082 | if (qIsDigit(*c)) {
|
---|
5083 | QString precision_str;
|
---|
5084 | while (*c != '\0' && qIsDigit(*c))
|
---|
5085 | precision_str.append(QLatin1Char(*c++));
|
---|
5086 |
|
---|
5087 | // can't be negative - started with a digit
|
---|
5088 | // contains at least one digit
|
---|
5089 | precision = precision_str.toInt();
|
---|
5090 | }
|
---|
5091 | else if (*c == '*') {
|
---|
5092 | precision = va_arg(ap, int);
|
---|
5093 | if (precision < 0)
|
---|
5094 | precision = -1; // treat all negative numbers as unspecified
|
---|
5095 | ++c;
|
---|
5096 | }
|
---|
5097 | }
|
---|
5098 |
|
---|
5099 | if (*c == '\0') {
|
---|
5100 | result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text
|
---|
5101 | break;
|
---|
5102 | }
|
---|
5103 |
|
---|
5104 | // Parse the length modifier
|
---|
5105 | enum LengthMod { lm_none, lm_hh, lm_h, lm_l, lm_ll, lm_L, lm_j, lm_z, lm_t };
|
---|
5106 | LengthMod length_mod = lm_none;
|
---|
5107 | switch (*c) {
|
---|
5108 | case 'h':
|
---|
5109 | ++c;
|
---|
5110 | if (*c == 'h') {
|
---|
5111 | length_mod = lm_hh;
|
---|
5112 | ++c;
|
---|
5113 | }
|
---|
5114 | else
|
---|
5115 | length_mod = lm_h;
|
---|
5116 | break;
|
---|
5117 |
|
---|
5118 | case 'l':
|
---|
5119 | ++c;
|
---|
5120 | if (*c == 'l') {
|
---|
5121 | length_mod = lm_ll;
|
---|
5122 | ++c;
|
---|
5123 | }
|
---|
5124 | else
|
---|
5125 | length_mod = lm_l;
|
---|
5126 | break;
|
---|
5127 |
|
---|
5128 | case 'L':
|
---|
5129 | ++c;
|
---|
5130 | length_mod = lm_L;
|
---|
5131 | break;
|
---|
5132 |
|
---|
5133 | case 'j':
|
---|
5134 | ++c;
|
---|
5135 | length_mod = lm_j;
|
---|
5136 | break;
|
---|
5137 |
|
---|
5138 | case 'z':
|
---|
5139 | case 'Z':
|
---|
5140 | ++c;
|
---|
5141 | length_mod = lm_z;
|
---|
5142 | break;
|
---|
5143 |
|
---|
5144 | case 't':
|
---|
5145 | ++c;
|
---|
5146 | length_mod = lm_t;
|
---|
5147 | break;
|
---|
5148 |
|
---|
5149 | default: break;
|
---|
5150 | }
|
---|
5151 |
|
---|
5152 | if (*c == '\0') {
|
---|
5153 | result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text
|
---|
5154 | break;
|
---|
5155 | }
|
---|
5156 |
|
---|
5157 | // Parse the conversion specifier and do the conversion
|
---|
5158 | QString subst;
|
---|
5159 | switch (*c) {
|
---|
5160 | case 'd':
|
---|
5161 | case 'i': {
|
---|
5162 | qint64 i;
|
---|
5163 | switch (length_mod) {
|
---|
5164 | case lm_none: i = va_arg(ap, int); break;
|
---|
5165 | case lm_hh: i = va_arg(ap, int); break;
|
---|
5166 | case lm_h: i = va_arg(ap, int); break;
|
---|
5167 | case lm_l: i = va_arg(ap, long int); break;
|
---|
5168 | case lm_ll: i = va_arg(ap, qint64); break;
|
---|
5169 | case lm_j: i = va_arg(ap, long int); break;
|
---|
5170 | case lm_z: i = va_arg(ap, size_t); break;
|
---|
5171 | case lm_t: i = va_arg(ap, int); break;
|
---|
5172 | default: i = 0; break;
|
---|
5173 | }
|
---|
5174 | subst = locale.d()->longLongToString(i, precision, 10, width, flags);
|
---|
5175 | ++c;
|
---|
5176 | break;
|
---|
5177 | }
|
---|
5178 | case 'o':
|
---|
5179 | case 'u':
|
---|
5180 | case 'x':
|
---|
5181 | case 'X': {
|
---|
5182 | quint64 u;
|
---|
5183 | switch (length_mod) {
|
---|
5184 | case lm_none: u = va_arg(ap, uint); break;
|
---|
5185 | case lm_hh: u = va_arg(ap, uint); break;
|
---|
5186 | case lm_h: u = va_arg(ap, uint); break;
|
---|
5187 | case lm_l: u = va_arg(ap, ulong); break;
|
---|
5188 | case lm_ll: u = va_arg(ap, quint64); break;
|
---|
5189 | case lm_z: u = va_arg(ap, size_t); break;
|
---|
5190 | default: u = 0; break;
|
---|
5191 | }
|
---|
5192 |
|
---|
5193 | if (qIsUpper(*c))
|
---|
5194 | flags |= QLocalePrivate::CapitalEorX;
|
---|
5195 |
|
---|
5196 | int base = 10;
|
---|
5197 | switch (qToLower(*c)) {
|
---|
5198 | case 'o':
|
---|
5199 | base = 8; break;
|
---|
5200 | case 'u':
|
---|
5201 | base = 10; break;
|
---|
5202 | case 'x':
|
---|
5203 | base = 16; break;
|
---|
5204 | default: break;
|
---|
5205 | }
|
---|
5206 | subst = locale.d()->unsLongLongToString(u, precision, base, width, flags);
|
---|
5207 | ++c;
|
---|
5208 | break;
|
---|
5209 | }
|
---|
5210 | case 'E':
|
---|
5211 | case 'e':
|
---|
5212 | case 'F':
|
---|
5213 | case 'f':
|
---|
5214 | case 'G':
|
---|
5215 | case 'g':
|
---|
5216 | case 'A':
|
---|
5217 | case 'a': {
|
---|
5218 | double d;
|
---|
5219 | if (length_mod == lm_L)
|
---|
5220 | d = va_arg(ap, long double); // not supported - converted to a double
|
---|
5221 | else
|
---|
5222 | d = va_arg(ap, double);
|
---|
5223 |
|
---|
5224 | if (qIsUpper(*c))
|
---|
5225 | flags |= QLocalePrivate::CapitalEorX;
|
---|
5226 |
|
---|
5227 | QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
|
---|
5228 | switch (qToLower(*c)) {
|
---|
5229 | case 'e': form = QLocalePrivate::DFExponent; break;
|
---|
5230 | case 'a': // not supported - decimal form used instead
|
---|
5231 | case 'f': form = QLocalePrivate::DFDecimal; break;
|
---|
5232 | case 'g': form = QLocalePrivate::DFSignificantDigits; break;
|
---|
5233 | default: break;
|
---|
5234 | }
|
---|
5235 | subst = locale.d()->doubleToString(d, precision, form, width, flags);
|
---|
5236 | ++c;
|
---|
5237 | break;
|
---|
5238 | }
|
---|
5239 | case 'c': {
|
---|
5240 | if (length_mod == lm_l)
|
---|
5241 | subst = QChar((ushort) va_arg(ap, int));
|
---|
5242 | else
|
---|
5243 | subst = QLatin1Char((uchar) va_arg(ap, int));
|
---|
5244 | ++c;
|
---|
5245 | break;
|
---|
5246 | }
|
---|
5247 | case 's': {
|
---|
5248 | if (length_mod == lm_l) {
|
---|
5249 | const ushort *buff = va_arg(ap, const ushort*);
|
---|
5250 | const ushort *ch = buff;
|
---|
5251 | while (*ch != 0)
|
---|
5252 | ++ch;
|
---|
5253 | subst.setUtf16(buff, ch - buff);
|
---|
5254 | } else
|
---|
5255 | subst = QString::fromUtf8(va_arg(ap, const char*));
|
---|
5256 | if (precision != -1)
|
---|
5257 | subst.truncate(precision);
|
---|
5258 | ++c;
|
---|
5259 | break;
|
---|
5260 | }
|
---|
5261 | case 'p': {
|
---|
5262 | void *arg = va_arg(ap, void*);
|
---|
5263 | #ifdef Q_OS_WIN64
|
---|
5264 | quint64 i = reinterpret_cast<quint64>(arg);
|
---|
5265 | #else
|
---|
5266 | quint64 i = reinterpret_cast<unsigned long>(arg);
|
---|
5267 | #endif
|
---|
5268 | flags |= QLocalePrivate::Alternate;
|
---|
5269 | subst = locale.d()->unsLongLongToString(i, precision, 16, width, flags);
|
---|
5270 | ++c;
|
---|
5271 | break;
|
---|
5272 | }
|
---|
5273 | case 'n':
|
---|
5274 | switch (length_mod) {
|
---|
5275 | case lm_hh: {
|
---|
5276 | signed char *n = va_arg(ap, signed char*);
|
---|
5277 | *n = result.length();
|
---|
5278 | break;
|
---|
5279 | }
|
---|
5280 | case lm_h: {
|
---|
5281 | short int *n = va_arg(ap, short int*);
|
---|
5282 | *n = result.length();
|
---|
5283 | break;
|
---|
5284 | }
|
---|
5285 | case lm_l: {
|
---|
5286 | long int *n = va_arg(ap, long int*);
|
---|
5287 | *n = result.length();
|
---|
5288 | break;
|
---|
5289 | }
|
---|
5290 | case lm_ll: {
|
---|
5291 | qint64 *n = va_arg(ap, qint64*);
|
---|
5292 | volatile uint tmp = result.length(); // egcs-2.91.66 gets internal
|
---|
5293 | *n = tmp; // compiler error without volatile
|
---|
5294 | break;
|
---|
5295 | }
|
---|
5296 | default: {
|
---|
5297 | int *n = va_arg(ap, int*);
|
---|
5298 | *n = result.length();
|
---|
5299 | break;
|
---|
5300 | }
|
---|
5301 | }
|
---|
5302 | ++c;
|
---|
5303 | break;
|
---|
5304 |
|
---|
5305 | default: // bad escape, treat as non-escape text
|
---|
5306 | for (const char *cc = escape_start; cc != c; ++cc)
|
---|
5307 | result.append(QLatin1Char(*cc));
|
---|
5308 | continue;
|
---|
5309 | }
|
---|
5310 |
|
---|
5311 | if (flags & QLocalePrivate::LeftAdjusted)
|
---|
5312 | result.append(subst.leftJustified(width));
|
---|
5313 | else
|
---|
5314 | result.append(subst.rightJustified(width));
|
---|
5315 | }
|
---|
5316 |
|
---|
5317 | *this = result;
|
---|
5318 |
|
---|
5319 | return *this;
|
---|
5320 | }
|
---|
5321 |
|
---|
5322 | /*!
|
---|
5323 | Returns the string converted to a \c{long long} using base \a
|
---|
5324 | base, which is 10 by default and must be between 2 and 36, or 0.
|
---|
5325 | Returns 0 if the conversion fails.
|
---|
5326 |
|
---|
5327 | If a conversion error occurs, *\a{ok} is set to false; otherwise
|
---|
5328 | *\a{ok} is set to true.
|
---|
5329 |
|
---|
5330 | If \a base is 0, the C language convention is used: If the string
|
---|
5331 | begins with "0x", base 16 is used; if the string begins with "0",
|
---|
5332 | base 8 is used; otherwise, base 10 is used.
|
---|
5333 |
|
---|
5334 | Example:
|
---|
5335 |
|
---|
5336 | \snippet doc/src/snippets/qstring/main.cpp 74
|
---|
5337 |
|
---|
5338 | \sa number(), toULongLong(), toInt()
|
---|
5339 | */
|
---|
5340 |
|
---|
5341 | qint64 QString::toLongLong(bool *ok, int base) const
|
---|
5342 | {
|
---|
5343 | #if defined(QT_CHECK_RANGE)
|
---|
5344 | if (base != 0 && (base < 2 || base > 36)) {
|
---|
5345 | qWarning("QString::toLongLong: Invalid base (%d)", base);
|
---|
5346 | base = 10;
|
---|
5347 | }
|
---|
5348 | #endif
|
---|
5349 |
|
---|
5350 | bool my_ok;
|
---|
5351 | QLocale def_locale;
|
---|
5352 | qint64 result = def_locale.d()->stringToLongLong(*this, base, &my_ok, QLocalePrivate::FailOnGroupSeparators);
|
---|
5353 | if (my_ok) {
|
---|
5354 | if (ok != 0)
|
---|
5355 | *ok = true;
|
---|
5356 | return result;
|
---|
5357 | }
|
---|
5358 |
|
---|
5359 | QLocale c_locale(QLocale::C);
|
---|
5360 | return c_locale.d()->stringToLongLong(*this, base, ok, QLocalePrivate::FailOnGroupSeparators);
|
---|
5361 | }
|
---|
5362 |
|
---|
5363 | /*!
|
---|
5364 | Returns the string converted to an \c{unsigned long long} using base \a
|
---|
5365 | base, which is 10 by default and must be between 2 and 36, or 0.
|
---|
5366 | Returns 0 if the conversion fails.
|
---|
5367 |
|
---|
5368 | If a conversion error occurs, *\a{ok} is set to false; otherwise
|
---|
5369 | *\a{ok} is set to true.
|
---|
5370 |
|
---|
5371 | If \a base is 0, the C language convention is used: If the string
|
---|
5372 | begins with "0x", base 16 is used; if the string begins with "0",
|
---|
5373 | base 8 is used; otherwise, base 10 is used.
|
---|
5374 |
|
---|
5375 | Example:
|
---|
5376 |
|
---|
5377 | \snippet doc/src/snippets/qstring/main.cpp 79
|
---|
5378 |
|
---|
5379 | \sa number(), toLongLong()
|
---|
5380 | */
|
---|
5381 |
|
---|
5382 | quint64 QString::toULongLong(bool *ok, int base) const
|
---|
5383 | {
|
---|
5384 | #if defined(QT_CHECK_RANGE)
|
---|
5385 | if (base != 0 && (base < 2 || base > 36)) {
|
---|
5386 | qWarning("QString::toULongLong: Invalid base (%d)", base);
|
---|
5387 | base = 10;
|
---|
5388 | }
|
---|
5389 | #endif
|
---|
5390 |
|
---|
5391 | bool my_ok;
|
---|
5392 | QLocale def_locale;
|
---|
5393 | quint64 result = def_locale.d()->stringToUnsLongLong(*this, base, &my_ok, QLocalePrivate::FailOnGroupSeparators);
|
---|
5394 | if (my_ok) {
|
---|
5395 | if (ok != 0)
|
---|
5396 | *ok = true;
|
---|
5397 | return result;
|
---|
5398 | }
|
---|
5399 |
|
---|
5400 | QLocale c_locale(QLocale::C);
|
---|
5401 | return c_locale.d()->stringToUnsLongLong(*this, base, ok, QLocalePrivate::FailOnGroupSeparators);
|
---|
5402 | }
|
---|
5403 |
|
---|
5404 | /*!
|
---|
5405 | \fn long QString::toLong(bool *ok, int base) const
|
---|
5406 |
|
---|
5407 | Returns the string converted to a \c long using base \a
|
---|
5408 | base, which is 10 by default and must be between 2 and 36, or 0.
|
---|
5409 | Returns 0 if the conversion fails.
|
---|
5410 |
|
---|
5411 | If a conversion error occurs, *\a{ok} is set to false; otherwise
|
---|
5412 | *\a{ok} is set to true.
|
---|
5413 |
|
---|
5414 | If \a base is 0, the C language convention is used: If the string
|
---|
5415 | begins with "0x", base 16 is used; if the string begins with "0",
|
---|
5416 | base 8 is used; otherwise, base 10 is used.
|
---|
5417 |
|
---|
5418 | Example:
|
---|
5419 |
|
---|
5420 | \snippet doc/src/snippets/qstring/main.cpp 73
|
---|
5421 |
|
---|
5422 | \sa number(), toULong(), toInt()
|
---|
5423 | */
|
---|
5424 |
|
---|
5425 | long QString::toLong(bool *ok, int base) const
|
---|
5426 | {
|
---|
5427 | qint64 v = toLongLong(ok, base);
|
---|
5428 | if (v < LONG_MIN || v > LONG_MAX) {
|
---|
5429 | if (ok)
|
---|
5430 | *ok = false;
|
---|
5431 | v = 0;
|
---|
5432 | }
|
---|
5433 | return (long)v;
|
---|
5434 | }
|
---|
5435 |
|
---|
5436 | /*!
|
---|
5437 | \fn ulong QString::toULong(bool *ok, int base) const
|
---|
5438 |
|
---|
5439 | Returns the string converted to an \c{unsigned long} using base \a
|
---|
5440 | base, which is 10 by default and must be between 2 and 36, or 0.
|
---|
5441 | Returns 0 if the conversion fails.
|
---|
5442 |
|
---|
5443 | If a conversion error occurs, *\a{ok} is set to false; otherwise
|
---|
5444 | *\a{ok} is set to true.
|
---|
5445 |
|
---|
5446 | If \a base is 0, the C language convention is used: If the string
|
---|
5447 | begins with "0x", base 16 is used; if the string begins with "0",
|
---|
5448 | base 8 is used; otherwise, base 10 is used.
|
---|
5449 |
|
---|
5450 | Example:
|
---|
5451 |
|
---|
5452 | \snippet doc/src/snippets/qstring/main.cpp 78
|
---|
5453 |
|
---|
5454 | \sa number()
|
---|
5455 | */
|
---|
5456 |
|
---|
5457 | ulong QString::toULong(bool *ok, int base) const
|
---|
5458 | {
|
---|
5459 | quint64 v = toULongLong(ok, base);
|
---|
5460 | if (v > ULONG_MAX) {
|
---|
5461 | if (ok)
|
---|
5462 | *ok = false;
|
---|
5463 | v = 0;
|
---|
5464 | }
|
---|
5465 | return (ulong)v;
|
---|
5466 | }
|
---|
5467 |
|
---|
5468 |
|
---|
5469 | /*!
|
---|
5470 | Returns the string converted to an \c int using base \a
|
---|
5471 | base, which is 10 by default and must be between 2 and 36, or 0.
|
---|
5472 | Returns 0 if the conversion fails.
|
---|
5473 |
|
---|
5474 | If a conversion error occurs, *\a{ok} is set to false; otherwise
|
---|
5475 | *\a{ok} is set to true.
|
---|
5476 |
|
---|
5477 | If \a base is 0, the C language convention is used: If the string
|
---|
5478 | begins with "0x", base 16 is used; if the string begins with "0",
|
---|
5479 | base 8 is used; otherwise, base 10 is used.
|
---|
5480 |
|
---|
5481 | Example:
|
---|
5482 |
|
---|
5483 | \snippet doc/src/snippets/qstring/main.cpp 72
|
---|
5484 |
|
---|
5485 | \sa number(), toUInt(), toDouble()
|
---|
5486 | */
|
---|
5487 |
|
---|
5488 | int QString::toInt(bool *ok, int base) const
|
---|
5489 | {
|
---|
5490 | qint64 v = toLongLong(ok, base);
|
---|
5491 | if (v < INT_MIN || v > INT_MAX) {
|
---|
5492 | if (ok)
|
---|
5493 | *ok = false;
|
---|
5494 | v = 0;
|
---|
5495 | }
|
---|
5496 | return v;
|
---|
5497 | }
|
---|
5498 |
|
---|
5499 | /*!
|
---|
5500 | Returns the string converted to an \c{unsigned int} using base \a
|
---|
5501 | base, which is 10 by default and must be between 2 and 36, or 0.
|
---|
5502 | Returns 0 if the conversion fails.
|
---|
5503 |
|
---|
5504 | If a conversion error occurs, *\a{ok} is set to false; otherwise
|
---|
5505 | *\a{ok} is set to true.
|
---|
5506 |
|
---|
5507 | If \a base is 0, the C language convention is used: If the string
|
---|
5508 | begins with "0x", base 16 is used; if the string begins with "0",
|
---|
5509 | base 8 is used; otherwise, base 10 is used.
|
---|
5510 |
|
---|
5511 | Example:
|
---|
5512 |
|
---|
5513 | \snippet doc/src/snippets/qstring/main.cpp 77
|
---|
5514 |
|
---|
5515 | \sa number(), toInt()
|
---|
5516 | */
|
---|
5517 |
|
---|
5518 | uint QString::toUInt(bool *ok, int base) const
|
---|
5519 | {
|
---|
5520 | quint64 v = toULongLong(ok, base);
|
---|
5521 | if (v > UINT_MAX) {
|
---|
5522 | if (ok)
|
---|
5523 | *ok = false;
|
---|
5524 | v = 0;
|
---|
5525 | }
|
---|
5526 | return (uint)v;
|
---|
5527 | }
|
---|
5528 |
|
---|
5529 | /*!
|
---|
5530 | Returns the string converted to a \c short using base \a
|
---|
5531 | base, which is 10 by default and must be between 2 and 36, or 0.
|
---|
5532 | Returns 0 if the conversion fails.
|
---|
5533 |
|
---|
5534 | If a conversion error occurs, *\a{ok} is set to false; otherwise
|
---|
5535 | *\a{ok} is set to true.
|
---|
5536 |
|
---|
5537 | If \a base is 0, the C language convention is used: If the string
|
---|
5538 | begins with "0x", base 16 is used; if the string begins with "0",
|
---|
5539 | base 8 is used; otherwise, base 10 is used.
|
---|
5540 |
|
---|
5541 | Example:
|
---|
5542 |
|
---|
5543 | \snippet doc/src/snippets/qstring/main.cpp 76
|
---|
5544 |
|
---|
5545 | \sa number(), toUShort(), toInt()
|
---|
5546 | */
|
---|
5547 |
|
---|
5548 | short QString::toShort(bool *ok, int base) const
|
---|
5549 | {
|
---|
5550 | long v = toLongLong(ok, base);
|
---|
5551 | if (v < SHRT_MIN || v > SHRT_MAX) {
|
---|
5552 | if (ok)
|
---|
5553 | *ok = false;
|
---|
5554 | v = 0;
|
---|
5555 | }
|
---|
5556 | return (short)v;
|
---|
5557 | }
|
---|
5558 |
|
---|
5559 | /*!
|
---|
5560 | Returns the string converted to an \c{unsigned short} using base \a
|
---|
5561 | base, which is 10 by default and must be between 2 and 36, or 0.
|
---|
5562 | Returns 0 if the conversion fails.
|
---|
5563 |
|
---|
5564 | If a conversion error occurs, *\a{ok} is set to false; otherwise
|
---|
5565 | *\a{ok} is set to true.
|
---|
5566 |
|
---|
5567 | If \a base is 0, the C language convention is used: If the string
|
---|
5568 | begins with "0x", base 16 is used; if the string begins with "0",
|
---|
5569 | base 8 is used; otherwise, base 10 is used.
|
---|
5570 |
|
---|
5571 | Example:
|
---|
5572 |
|
---|
5573 | \snippet doc/src/snippets/qstring/main.cpp 80
|
---|
5574 |
|
---|
5575 | \sa number(), toShort()
|
---|
5576 | */
|
---|
5577 |
|
---|
5578 | ushort QString::toUShort(bool *ok, int base) const
|
---|
5579 | {
|
---|
5580 | ulong v = toULongLong(ok, base);
|
---|
5581 | if (v > USHRT_MAX) {
|
---|
5582 | if (ok)
|
---|
5583 | *ok = false;
|
---|
5584 | v = 0;
|
---|
5585 | }
|
---|
5586 | return (ushort)v;
|
---|
5587 | }
|
---|
5588 |
|
---|
5589 |
|
---|
5590 | /*!
|
---|
5591 | Returns the string converted to a \c double value.
|
---|
5592 |
|
---|
5593 | Returns 0.0 if the conversion fails.
|
---|
5594 |
|
---|
5595 | If a conversion error occurs, \c{*}\a{ok} is set to false;
|
---|
5596 | otherwise \c{*}\a{ok} is set to true.
|
---|
5597 |
|
---|
5598 | \snippet doc/src/snippets/qstring/main.cpp 66
|
---|
5599 |
|
---|
5600 | Various string formats for floating point numbers can be converted
|
---|
5601 | to double values:
|
---|
5602 |
|
---|
5603 | \snippet doc/src/snippets/qstring/main.cpp 67
|
---|
5604 |
|
---|
5605 | This function tries to interpret the string according to the
|
---|
5606 | current locale. The current locale is determined from the
|
---|
5607 | system at application startup and can be changed by calling
|
---|
5608 | QLocale::setDefault(). If the string cannot be interpreted
|
---|
5609 | according to the current locale, this function falls back
|
---|
5610 | on the "C" locale.
|
---|
5611 |
|
---|
5612 | \snippet doc/src/snippets/qstring/main.cpp 69
|
---|
5613 | \snippet doc/src/snippets/qstring/main.cpp 70
|
---|
5614 |
|
---|
5615 | Due to the ambiguity between the decimal point and thousands group
|
---|
5616 | separator in various locales, this function does not handle
|
---|
5617 | thousands group separators. If you need to convert such numbers,
|
---|
5618 | see QLocale::toDouble().
|
---|
5619 |
|
---|
5620 | \snippet doc/src/snippets/qstring/main.cpp 68
|
---|
5621 |
|
---|
5622 | \sa number() QLocale::setDefault() QLocale::toDouble() trimmed()
|
---|
5623 | */
|
---|
5624 |
|
---|
5625 | double QString::toDouble(bool *ok) const
|
---|
5626 | {
|
---|
5627 | bool my_ok;
|
---|
5628 | QLocale def_locale;
|
---|
5629 | double result = def_locale.d()->stringToDouble(*this, &my_ok, QLocalePrivate::FailOnGroupSeparators);
|
---|
5630 | if (my_ok) {
|
---|
5631 | if (ok != 0)
|
---|
5632 | *ok = true;
|
---|
5633 | return result;
|
---|
5634 | }
|
---|
5635 |
|
---|
5636 | QLocale c_locale(QLocale::C);
|
---|
5637 | return c_locale.d()->stringToDouble(*this, ok, QLocalePrivate::FailOnGroupSeparators);
|
---|
5638 | }
|
---|
5639 |
|
---|
5640 | /*!
|
---|
5641 | Returns the string converted to a \c float value.
|
---|
5642 |
|
---|
5643 | If a conversion error occurs, *\a{ok} is set to false; otherwise
|
---|
5644 | *\a{ok} is set to true. Returns 0.0 if the conversion fails.
|
---|
5645 |
|
---|
5646 | Example:
|
---|
5647 |
|
---|
5648 | \snippet doc/src/snippets/qstring/main.cpp 71
|
---|
5649 |
|
---|
5650 | \sa number(), toDouble(), toInt()
|
---|
5651 | */
|
---|
5652 |
|
---|
5653 | #define QT_MAX_FLOAT 3.4028234663852886e+38
|
---|
5654 |
|
---|
5655 | float QString::toFloat(bool *ok) const
|
---|
5656 | {
|
---|
5657 | bool myOk;
|
---|
5658 | double d = toDouble(&myOk);
|
---|
5659 | if (!myOk || d > QT_MAX_FLOAT || d < -QT_MAX_FLOAT) {
|
---|
5660 | if (ok != 0)
|
---|
5661 | *ok = false;
|
---|
5662 | return 0.0;
|
---|
5663 | }
|
---|
5664 | if (ok != 0)
|
---|
5665 | *ok = true;
|
---|
5666 | return (float) d;
|
---|
5667 | }
|
---|
5668 |
|
---|
5669 | /*! \fn QString &QString::setNum(int n, int base)
|
---|
5670 |
|
---|
5671 | Sets the string to the printed value of \a n in the specified \a
|
---|
5672 | base, and returns a reference to the string.
|
---|
5673 |
|
---|
5674 | The base is 10 by default and must be between 2 and 36. For bases
|
---|
5675 | other than 10, \a n is treated as an unsigned integer.
|
---|
5676 |
|
---|
5677 | \snippet doc/src/snippets/qstring/main.cpp 56
|
---|
5678 |
|
---|
5679 | The formatting always uses QLocale::C, i.e., English/UnitedStates.
|
---|
5680 | To get a localized string representation of a number, use
|
---|
5681 | QLocale::toString() with the appropriate locale.
|
---|
5682 | */
|
---|
5683 |
|
---|
5684 | /*! \fn QString &QString::setNum(uint n, int base)
|
---|
5685 |
|
---|
5686 | \overload
|
---|
5687 | */
|
---|
5688 |
|
---|
5689 | /*! \fn QString &QString::setNum(long n, int base)
|
---|
5690 |
|
---|
5691 | \overload
|
---|
5692 | */
|
---|
5693 |
|
---|
5694 | /*! \fn QString &QString::setNum(ulong n, int base)
|
---|
5695 |
|
---|
5696 | \overload
|
---|
5697 | */
|
---|
5698 |
|
---|
5699 | /*!
|
---|
5700 | \overload
|
---|
5701 | */
|
---|
5702 | QString &QString::setNum(qlonglong n, int base)
|
---|
5703 | {
|
---|
5704 | #if defined(QT_CHECK_RANGE)
|
---|
5705 | if (base < 2 || base > 36) {
|
---|
5706 | qWarning("QString::setNum: Invalid base (%d)", base);
|
---|
5707 | base = 10;
|
---|
5708 | }
|
---|
5709 | #endif
|
---|
5710 | QLocale locale(QLocale::C);
|
---|
5711 | *this = locale.d()->longLongToString(n, -1, base);
|
---|
5712 | return *this;
|
---|
5713 | }
|
---|
5714 |
|
---|
5715 | /*!
|
---|
5716 | \overload
|
---|
5717 | */
|
---|
5718 | QString &QString::setNum(qulonglong n, int base)
|
---|
5719 | {
|
---|
5720 | #if defined(QT_CHECK_RANGE)
|
---|
5721 | if (base < 2 || base > 36) {
|
---|
5722 | qWarning("QString::setNum: Invalid base (%d)", base);
|
---|
5723 | base = 10;
|
---|
5724 | }
|
---|
5725 | #endif
|
---|
5726 | QLocale locale(QLocale::C);
|
---|
5727 | *this = locale.d()->unsLongLongToString(n, -1, base);
|
---|
5728 | return *this;
|
---|
5729 | }
|
---|
5730 |
|
---|
5731 | /*! \fn QString &QString::setNum(short n, int base)
|
---|
5732 |
|
---|
5733 | \overload
|
---|
5734 | */
|
---|
5735 |
|
---|
5736 | /*! \fn QString &QString::setNum(ushort n, int base)
|
---|
5737 |
|
---|
5738 | \overload
|
---|
5739 | */
|
---|
5740 |
|
---|
5741 | /*!
|
---|
5742 | \fn QString &QString::setNum(double n, char format, int precision)
|
---|
5743 | \overload
|
---|
5744 |
|
---|
5745 | Sets the string to the printed value of \a n, formatted according
|
---|
5746 | to the given \a format and \a precision, and returns a reference
|
---|
5747 | to the string.
|
---|
5748 |
|
---|
5749 | The \a format can be 'f', 'F', 'e', 'E', 'g' or 'G' (see the
|
---|
5750 | arg() function documentation for an explanation of the formats).
|
---|
5751 |
|
---|
5752 | Unlike QLocale::toString(), this function doesn't honor the
|
---|
5753 | user's locale settings.
|
---|
5754 | */
|
---|
5755 |
|
---|
5756 | QString &QString::setNum(double n, char f, int prec)
|
---|
5757 | {
|
---|
5758 | QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
|
---|
5759 | uint flags = 0;
|
---|
5760 |
|
---|
5761 | if (qIsUpper(f))
|
---|
5762 | flags = QLocalePrivate::CapitalEorX;
|
---|
5763 | f = qToLower(f);
|
---|
5764 |
|
---|
5765 | switch (f) {
|
---|
5766 | case 'f':
|
---|
5767 | form = QLocalePrivate::DFDecimal;
|
---|
5768 | break;
|
---|
5769 | case 'e':
|
---|
5770 | form = QLocalePrivate::DFExponent;
|
---|
5771 | break;
|
---|
5772 | case 'g':
|
---|
5773 | form = QLocalePrivate::DFSignificantDigits;
|
---|
5774 | break;
|
---|
5775 | default:
|
---|
5776 | #if defined(QT_CHECK_RANGE)
|
---|
5777 | qWarning("QString::setNum: Invalid format char '%c'", f);
|
---|
5778 | #endif
|
---|
5779 | break;
|
---|
5780 | }
|
---|
5781 |
|
---|
5782 | QLocale locale(QLocale::C);
|
---|
5783 | *this = locale.d()->doubleToString(n, prec, form, -1, flags);
|
---|
5784 | return *this;
|
---|
5785 | }
|
---|
5786 |
|
---|
5787 | /*!
|
---|
5788 | \fn QString &QString::setNum(float n, char format, int precision)
|
---|
5789 | \overload
|
---|
5790 |
|
---|
5791 | Sets the string to the printed value of \a n, formatted according
|
---|
5792 | to the given \a format and \a precision, and returns a reference
|
---|
5793 | to the string.
|
---|
5794 | */
|
---|
5795 |
|
---|
5796 |
|
---|
5797 | /*!
|
---|
5798 | \fn QString QString::number(long n, int base)
|
---|
5799 |
|
---|
5800 | Returns a string equivalent of the number \a n according to the
|
---|
5801 | specified \a base.
|
---|
5802 |
|
---|
5803 | The base is 10 by default and must be between 2
|
---|
5804 | and 36. For bases other than 10, \a n is treated as an
|
---|
5805 | unsigned integer.
|
---|
5806 |
|
---|
5807 | \snippet doc/src/snippets/qstring/main.cpp 35
|
---|
5808 |
|
---|
5809 | \sa setNum()
|
---|
5810 | */
|
---|
5811 |
|
---|
5812 | QString QString::number(long n, int base)
|
---|
5813 | {
|
---|
5814 | QString s;
|
---|
5815 | s.setNum(n, base);
|
---|
5816 | return s;
|
---|
5817 | }
|
---|
5818 |
|
---|
5819 | /*!
|
---|
5820 | \fn QString QString::number(ulong n, int base)
|
---|
5821 |
|
---|
5822 | \overload
|
---|
5823 | */
|
---|
5824 | QString QString::number(ulong n, int base)
|
---|
5825 | {
|
---|
5826 | QString s;
|
---|
5827 | s.setNum(n, base);
|
---|
5828 | return s;
|
---|
5829 | }
|
---|
5830 |
|
---|
5831 | /*!
|
---|
5832 | \overload
|
---|
5833 | */
|
---|
5834 | QString QString::number(int n, int base)
|
---|
5835 | {
|
---|
5836 | QString s;
|
---|
5837 | s.setNum(n, base);
|
---|
5838 | return s;
|
---|
5839 | }
|
---|
5840 |
|
---|
5841 | /*!
|
---|
5842 | \overload
|
---|
5843 | */
|
---|
5844 | QString QString::number(uint n, int base)
|
---|
5845 | {
|
---|
5846 | QString s;
|
---|
5847 | s.setNum(n, base);
|
---|
5848 | return s;
|
---|
5849 | }
|
---|
5850 |
|
---|
5851 | /*!
|
---|
5852 | \overload
|
---|
5853 | */
|
---|
5854 | QString QString::number(qlonglong n, int base)
|
---|
5855 | {
|
---|
5856 | QString s;
|
---|
5857 | s.setNum(n, base);
|
---|
5858 | return s;
|
---|
5859 | }
|
---|
5860 |
|
---|
5861 | /*!
|
---|
5862 | \overload
|
---|
5863 | */
|
---|
5864 | QString QString::number(qulonglong n, int base)
|
---|
5865 | {
|
---|
5866 | QString s;
|
---|
5867 | s.setNum(n, base);
|
---|
5868 | return s;
|
---|
5869 | }
|
---|
5870 |
|
---|
5871 |
|
---|
5872 | /*!
|
---|
5873 | \fn QString QString::number(double n, char format, int precision)
|
---|
5874 |
|
---|
5875 | Returns a string equivalent of the number \a n, formatted
|
---|
5876 | according to the specified \a format and \a precision. See
|
---|
5877 | \l{Argument Formats} for details.
|
---|
5878 |
|
---|
5879 | Unlike QLocale::toString(), this function does not honor the
|
---|
5880 | user's locale settings.
|
---|
5881 |
|
---|
5882 | \sa setNum(), QLocale::toString()
|
---|
5883 | */
|
---|
5884 | QString QString::number(double n, char f, int prec)
|
---|
5885 | {
|
---|
5886 | QString s;
|
---|
5887 | s.setNum(n, f, prec);
|
---|
5888 | return s;
|
---|
5889 | }
|
---|
5890 |
|
---|
5891 | /*!
|
---|
5892 | Splits the string into substrings wherever \a sep occurs, and
|
---|
5893 | returns the list of those strings. If \a sep does not match
|
---|
5894 | anywhere in the string, split() returns a single-element list
|
---|
5895 | containing this string.
|
---|
5896 |
|
---|
5897 | \a cs specifies whether \a sep should be matched case
|
---|
5898 | sensitively or case insensitively.
|
---|
5899 |
|
---|
5900 | If \a behavior is QString::SkipEmptyParts, empty entries don't
|
---|
5901 | appear in the result. By default, empty entries are kept.
|
---|
5902 |
|
---|
5903 | Example:
|
---|
5904 |
|
---|
5905 | \snippet doc/src/snippets/qstring/main.cpp 62
|
---|
5906 |
|
---|
5907 | \sa QStringList::join(), section()
|
---|
5908 | */
|
---|
5909 | QStringList QString::split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
|
---|
5910 | {
|
---|
5911 | QStringList list;
|
---|
5912 | int start = 0;
|
---|
5913 | int extra = 0;
|
---|
5914 | int end;
|
---|
5915 | while ((end = indexOf(sep, start + extra, cs)) != -1) {
|
---|
5916 | if (start != end || behavior == KeepEmptyParts)
|
---|
5917 | list.append(mid(start, end - start));
|
---|
5918 | start = end + sep.size();
|
---|
5919 | extra = (sep.size() == 0 ? 1 : 0);
|
---|
5920 | }
|
---|
5921 | if (start != size() || behavior == KeepEmptyParts)
|
---|
5922 | list.append(mid(start));
|
---|
5923 | return list;
|
---|
5924 | }
|
---|
5925 |
|
---|
5926 | /*!
|
---|
5927 | \overload
|
---|
5928 | */
|
---|
5929 | QStringList QString::split(const QChar &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
|
---|
5930 | {
|
---|
5931 | QStringList list;
|
---|
5932 | int start = 0;
|
---|
5933 | int end;
|
---|
5934 | while ((end = indexOf(sep, start, cs)) != -1) {
|
---|
5935 | if (start != end || behavior == KeepEmptyParts)
|
---|
5936 | list.append(mid(start, end - start));
|
---|
5937 | start = end + 1;
|
---|
5938 | }
|
---|
5939 | if (start != size() || behavior == KeepEmptyParts)
|
---|
5940 | list.append(mid(start));
|
---|
5941 | return list;
|
---|
5942 | }
|
---|
5943 |
|
---|
5944 | #ifndef QT_NO_REGEXP
|
---|
5945 | /*!
|
---|
5946 | \overload
|
---|
5947 |
|
---|
5948 | Splits the string into substrings wherever the regular expression
|
---|
5949 | \a rx matches, and returns the list of those strings. If \a rx
|
---|
5950 | does not match anywhere in the string, split() returns a
|
---|
5951 | single-element list containing this string.
|
---|
5952 |
|
---|
5953 | Here's an example where we extract the words in a sentence
|
---|
5954 | using one or more whitespace characters as the separator:
|
---|
5955 |
|
---|
5956 | \snippet doc/src/snippets/qstring/main.cpp 59
|
---|
5957 |
|
---|
5958 | Here's a similar example, but this time we use any sequence of
|
---|
5959 | non-word characters as the separator:
|
---|
5960 |
|
---|
5961 | \snippet doc/src/snippets/qstring/main.cpp 60
|
---|
5962 |
|
---|
5963 | Here's a third example where we use a zero-length assertion,
|
---|
5964 | \bold{\\b} (word boundary), to split the string into an
|
---|
5965 | alternating sequence of non-word and word tokens:
|
---|
5966 |
|
---|
5967 | \snippet doc/src/snippets/qstring/main.cpp 61
|
---|
5968 |
|
---|
5969 | \sa QStringList::join(), section()
|
---|
5970 | */
|
---|
5971 | QStringList QString::split(const QRegExp &rx, SplitBehavior behavior) const
|
---|
5972 | {
|
---|
5973 | QRegExp rx2(rx);
|
---|
5974 | QStringList list;
|
---|
5975 | int start = 0;
|
---|
5976 | int extra = 0;
|
---|
5977 | int end;
|
---|
5978 | while ((end = rx2.indexIn(*this, start + extra)) != -1) {
|
---|
5979 | int matchedLen = rx2.matchedLength();
|
---|
5980 | if (start != end || behavior == KeepEmptyParts)
|
---|
5981 | list.append(mid(start, end - start));
|
---|
5982 | start = end + matchedLen;
|
---|
5983 | extra = (matchedLen == 0) ? 1 : 0;
|
---|
5984 | }
|
---|
5985 | if (start != size() || behavior == KeepEmptyParts)
|
---|
5986 | list.append(mid(start));
|
---|
5987 | return list;
|
---|
5988 | }
|
---|
5989 | #endif
|
---|
5990 |
|
---|
5991 | /*!
|
---|
5992 | \enum QString::NormalizationForm
|
---|
5993 |
|
---|
5994 | This enum describes the various normalized forms of Unicode text.
|
---|
5995 |
|
---|
5996 | \value NormalizationForm_D Canonical Decomposition
|
---|
5997 | \value NormalizationForm_C Canonical Decomposition followed by Canonical Composition
|
---|
5998 | \value NormalizationForm_KD Compatibility Decomposition
|
---|
5999 | \value NormalizationForm_KC Compatibility Decomposition followed by Canonical Composition
|
---|
6000 |
|
---|
6001 | \sa normalized(),
|
---|
6002 | {http://www.unicode.org/reports/tr15/}{Unicode Standard Annex #15}
|
---|
6003 | */
|
---|
6004 |
|
---|
6005 | /*!
|
---|
6006 | \fn QString QString::normalized(NormalizationForm mode) const
|
---|
6007 | Returns the string in the given Unicode normalization \a mode.
|
---|
6008 | */
|
---|
6009 | QString QString::normalized(QString::NormalizationForm mode) const
|
---|
6010 | {
|
---|
6011 | return normalized(mode, CURRENT_VERSION);
|
---|
6012 | }
|
---|
6013 |
|
---|
6014 | /*!
|
---|
6015 | \since 4.5
|
---|
6016 |
|
---|
6017 | Returns a copy of this string repeated the specified number of \a times.
|
---|
6018 |
|
---|
6019 | If \a times is less than 1, an empty string is returned.
|
---|
6020 |
|
---|
6021 | Example:
|
---|
6022 |
|
---|
6023 | \code
|
---|
6024 | QString str("ab");
|
---|
6025 | str.repeated(4); // returns "abababab"
|
---|
6026 | \endcode
|
---|
6027 | */
|
---|
6028 | QString QString::repeated(int times) const
|
---|
6029 | {
|
---|
6030 | if (d->size == 0)
|
---|
6031 | return *this;
|
---|
6032 |
|
---|
6033 | if (times <= 1) {
|
---|
6034 | if (times == 1)
|
---|
6035 | return *this;
|
---|
6036 | return QString();
|
---|
6037 | }
|
---|
6038 |
|
---|
6039 | const int resultSize = times * d->size;
|
---|
6040 |
|
---|
6041 | QString result;
|
---|
6042 | result.reserve(resultSize);
|
---|
6043 | if (result.d->alloc != resultSize)
|
---|
6044 | return QString(); // not enough memory
|
---|
6045 |
|
---|
6046 | qMemCopy(result.d->data, d->data, d->size * sizeof(ushort));
|
---|
6047 |
|
---|
6048 | int sizeSoFar = d->size;
|
---|
6049 | ushort *end = result.d->data + sizeSoFar;
|
---|
6050 |
|
---|
6051 | const int halfResultSize = resultSize >> 1;
|
---|
6052 | while (sizeSoFar <= halfResultSize) {
|
---|
6053 | qMemCopy(end, result.d->data, sizeSoFar * sizeof(ushort));
|
---|
6054 | end += sizeSoFar;
|
---|
6055 | sizeSoFar <<= 1;
|
---|
6056 | }
|
---|
6057 | qMemCopy(end, result.d->data, (resultSize - sizeSoFar) * sizeof(ushort));
|
---|
6058 | result.d->data[resultSize] = '\0';
|
---|
6059 | result.d->size = resultSize;
|
---|
6060 | return result;
|
---|
6061 | }
|
---|
6062 |
|
---|
6063 | void qt_string_normalize(QString *data, QString::NormalizationForm mode, QChar::UnicodeVersion version, int from);
|
---|
6064 | /*!
|
---|
6065 | \overload
|
---|
6066 | \fn QString QString::normalized(NormalizationForm mode, QChar::UnicodeVersion version) const
|
---|
6067 |
|
---|
6068 | Returns the string in the given Unicode normalization \a mode,
|
---|
6069 | according to the given \a version of the Unicode standard.
|
---|
6070 | */
|
---|
6071 | QString QString::normalized(QString::NormalizationForm mode, QChar::UnicodeVersion version) const
|
---|
6072 | {
|
---|
6073 | QString copy = *this;
|
---|
6074 | qt_string_normalize(©, mode, version, 0);
|
---|
6075 | return copy;
|
---|
6076 | }
|
---|
6077 |
|
---|
6078 | void qt_string_normalize(QString *data, QString::NormalizationForm mode, QChar::UnicodeVersion version, int from)
|
---|
6079 | {
|
---|
6080 | bool simple = true;
|
---|
6081 | const QChar *p = data->constData();
|
---|
6082 | int len = data->length();
|
---|
6083 | for (int i = from; i < len; ++i) {
|
---|
6084 | if (p[i].unicode() >= 0x80) {
|
---|
6085 | simple = false;
|
---|
6086 | break;
|
---|
6087 | }
|
---|
6088 | }
|
---|
6089 | if (simple)
|
---|
6090 | return;
|
---|
6091 |
|
---|
6092 | QString &s = *data;
|
---|
6093 | if (version != CURRENT_VERSION) {
|
---|
6094 | for (int i = 0; i < NumNormalizationCorrections; ++i) {
|
---|
6095 | const NormalizationCorrection &n = uc_normalization_corrections[i];
|
---|
6096 | if (n.version > version) {
|
---|
6097 | int pos = from;
|
---|
6098 | if (n.ucs4 > 0xffff) {
|
---|
6099 | ushort ucs4High = QChar::highSurrogate(n.ucs4);
|
---|
6100 | ushort ucs4Low = QChar::lowSurrogate(n.ucs4);
|
---|
6101 | ushort oldHigh = QChar::highSurrogate(n.old_mapping);
|
---|
6102 | ushort oldLow = QChar::lowSurrogate(n.old_mapping);
|
---|
6103 | while (pos < s.length() - 1) {
|
---|
6104 | if (s.at(pos).unicode() == ucs4High && s.at(pos + 1).unicode() == ucs4Low) {
|
---|
6105 | s[pos] = oldHigh;
|
---|
6106 | s[pos + 1] = oldLow;
|
---|
6107 | ++pos;
|
---|
6108 | }
|
---|
6109 | ++pos;
|
---|
6110 | }
|
---|
6111 | } else {
|
---|
6112 | while (pos < s.length()) {
|
---|
6113 | if (s.at(pos).unicode() == n.ucs4) {
|
---|
6114 | s[pos] = n.old_mapping;
|
---|
6115 | }
|
---|
6116 | ++pos;
|
---|
6117 | }
|
---|
6118 | }
|
---|
6119 | }
|
---|
6120 | }
|
---|
6121 | }
|
---|
6122 | decomposeHelper(data, mode < QString::NormalizationForm_KD, version, from);
|
---|
6123 |
|
---|
6124 | canonicalOrderHelper(data, version, from);
|
---|
6125 |
|
---|
6126 | if (mode == QString::NormalizationForm_D || mode == QString::NormalizationForm_KD)
|
---|
6127 | return;
|
---|
6128 |
|
---|
6129 | composeHelper(data, from);
|
---|
6130 | }
|
---|
6131 |
|
---|
6132 |
|
---|
6133 | struct ArgEscapeData
|
---|
6134 | {
|
---|
6135 | int min_escape; // lowest escape sequence number
|
---|
6136 | int occurrences; // number of occurrences of the lowest escape sequence number
|
---|
6137 | int locale_occurrences; // number of occurrences of the lowest escape sequence number that
|
---|
6138 | // contain 'L'
|
---|
6139 | int escape_len; // total length of escape sequences which will be replaced
|
---|
6140 | };
|
---|
6141 |
|
---|
6142 | static ArgEscapeData findArgEscapes(const QString &s)
|
---|
6143 | {
|
---|
6144 | const QChar *uc_begin = s.unicode();
|
---|
6145 | const QChar *uc_end = uc_begin + s.length();
|
---|
6146 |
|
---|
6147 | ArgEscapeData d;
|
---|
6148 |
|
---|
6149 | d.min_escape = INT_MAX;
|
---|
6150 | d.occurrences = 0;
|
---|
6151 | d.escape_len = 0;
|
---|
6152 | d.locale_occurrences = 0;
|
---|
6153 |
|
---|
6154 | const QChar *c = uc_begin;
|
---|
6155 | while (c != uc_end) {
|
---|
6156 | while (c != uc_end && c->unicode() != '%')
|
---|
6157 | ++c;
|
---|
6158 |
|
---|
6159 | if (c == uc_end)
|
---|
6160 | break;
|
---|
6161 | const QChar *escape_start = c;
|
---|
6162 | if (++c == uc_end)
|
---|
6163 | break;
|
---|
6164 |
|
---|
6165 | bool locale_arg = false;
|
---|
6166 | if (c->unicode() == 'L') {
|
---|
6167 | locale_arg = true;
|
---|
6168 | if (++c == uc_end)
|
---|
6169 | break;
|
---|
6170 | }
|
---|
6171 |
|
---|
6172 | if (c->digitValue() == -1)
|
---|
6173 | continue;
|
---|
6174 |
|
---|
6175 | int escape = c->digitValue();
|
---|
6176 | ++c;
|
---|
6177 |
|
---|
6178 | if (c != uc_end && c->digitValue() != -1) {
|
---|
6179 | escape = (10 * escape) + c->digitValue();
|
---|
6180 | ++c;
|
---|
6181 | }
|
---|
6182 |
|
---|
6183 | if (escape > d.min_escape)
|
---|
6184 | continue;
|
---|
6185 |
|
---|
6186 | if (escape < d.min_escape) {
|
---|
6187 | d.min_escape = escape;
|
---|
6188 | d.occurrences = 0;
|
---|
6189 | d.escape_len = 0;
|
---|
6190 | d.locale_occurrences = 0;
|
---|
6191 | }
|
---|
6192 |
|
---|
6193 | ++d.occurrences;
|
---|
6194 | if (locale_arg)
|
---|
6195 | ++d.locale_occurrences;
|
---|
6196 | d.escape_len += c - escape_start;
|
---|
6197 | }
|
---|
6198 | return d;
|
---|
6199 | }
|
---|
6200 |
|
---|
6201 | static QString replaceArgEscapes(const QString &s, const ArgEscapeData &d, int field_width,
|
---|
6202 | const QString &arg, const QString &larg, const QChar &fillChar = QLatin1Char(' '))
|
---|
6203 | {
|
---|
6204 | const QChar *uc_begin = s.unicode();
|
---|
6205 | const QChar *uc_end = uc_begin + s.length();
|
---|
6206 |
|
---|
6207 | int abs_field_width = qAbs(field_width);
|
---|
6208 | int result_len = s.length()
|
---|
6209 | - d.escape_len
|
---|
6210 | + (d.occurrences - d.locale_occurrences)
|
---|
6211 | *qMax(abs_field_width, arg.length())
|
---|
6212 | + d.locale_occurrences
|
---|
6213 | *qMax(abs_field_width, larg.length());
|
---|
6214 |
|
---|
6215 | QString result(result_len, Qt::Uninitialized);
|
---|
6216 | QChar *result_buff = (QChar*) result.unicode();
|
---|
6217 |
|
---|
6218 | QChar *rc = result_buff;
|
---|
6219 | const QChar *c = uc_begin;
|
---|
6220 | int repl_cnt = 0;
|
---|
6221 | while (c != uc_end) {
|
---|
6222 | /* We don't have to check if we run off the end of the string with c,
|
---|
6223 | because as long as d.occurrences > 0 we KNOW there are valid escape
|
---|
6224 | sequences. */
|
---|
6225 |
|
---|
6226 | const QChar *text_start = c;
|
---|
6227 |
|
---|
6228 | while (c->unicode() != '%')
|
---|
6229 | ++c;
|
---|
6230 |
|
---|
6231 | const QChar *escape_start = c++;
|
---|
6232 |
|
---|
6233 | bool locale_arg = false;
|
---|
6234 | if (c->unicode() == 'L') {
|
---|
6235 | locale_arg = true;
|
---|
6236 | ++c;
|
---|
6237 | }
|
---|
6238 |
|
---|
6239 | int escape = c->digitValue();
|
---|
6240 | if (escape != -1) {
|
---|
6241 | if (c + 1 != uc_end && (c + 1)->digitValue() != -1) {
|
---|
6242 | escape = (10 * escape) + (c + 1)->digitValue();
|
---|
6243 | ++c;
|
---|
6244 | }
|
---|
6245 | }
|
---|
6246 |
|
---|
6247 | if (escape != d.min_escape) {
|
---|
6248 | memcpy(rc, text_start, (c - text_start)*sizeof(QChar));
|
---|
6249 | rc += c - text_start;
|
---|
6250 | }
|
---|
6251 | else {
|
---|
6252 | ++c;
|
---|
6253 |
|
---|
6254 | memcpy(rc, text_start, (escape_start - text_start)*sizeof(QChar));
|
---|
6255 | rc += escape_start - text_start;
|
---|
6256 |
|
---|
6257 | uint pad_chars;
|
---|
6258 | if (locale_arg)
|
---|
6259 | pad_chars = qMax(abs_field_width, larg.length()) - larg.length();
|
---|
6260 | else
|
---|
6261 | pad_chars = qMax(abs_field_width, arg.length()) - arg.length();
|
---|
6262 |
|
---|
6263 | if (field_width > 0) { // left padded
|
---|
6264 | for (uint i = 0; i < pad_chars; ++i)
|
---|
6265 | (rc++)->unicode() = fillChar.unicode();
|
---|
6266 | }
|
---|
6267 |
|
---|
6268 | if (locale_arg) {
|
---|
6269 | memcpy(rc, larg.unicode(), larg.length()*sizeof(QChar));
|
---|
6270 | rc += larg.length();
|
---|
6271 | }
|
---|
6272 | else {
|
---|
6273 | memcpy(rc, arg.unicode(), arg.length()*sizeof(QChar));
|
---|
6274 | rc += arg.length();
|
---|
6275 | }
|
---|
6276 |
|
---|
6277 | if (field_width < 0) { // right padded
|
---|
6278 | for (uint i = 0; i < pad_chars; ++i)
|
---|
6279 | (rc++)->unicode() = fillChar.unicode();
|
---|
6280 | }
|
---|
6281 |
|
---|
6282 | if (++repl_cnt == d.occurrences) {
|
---|
6283 | memcpy(rc, c, (uc_end - c)*sizeof(QChar));
|
---|
6284 | rc += uc_end - c;
|
---|
6285 | Q_ASSERT(rc - result_buff == result_len);
|
---|
6286 | c = uc_end;
|
---|
6287 | }
|
---|
6288 | }
|
---|
6289 | }
|
---|
6290 | Q_ASSERT(rc == result_buff + result_len);
|
---|
6291 |
|
---|
6292 | return result;
|
---|
6293 | }
|
---|
6294 |
|
---|
6295 | /*!
|
---|
6296 | Returns a copy of this string with the lowest numbered place marker
|
---|
6297 | replaced by string \a a, i.e., \c %1, \c %2, ..., \c %99.
|
---|
6298 |
|
---|
6299 | \a fieldWidth specifies the minimum amount of space that argument \a
|
---|
6300 | a shall occupy. If \a a requires less space than \a fieldWidth, it
|
---|
6301 | is padded to \a fieldWidth with character \a fillChar. A positive
|
---|
6302 | \a fieldWidth produces right-aligned text. A negative \a fieldWidth
|
---|
6303 | produces left-aligned text.
|
---|
6304 |
|
---|
6305 | This example shows how we might create a \c status string for
|
---|
6306 | reporting progress while processing a list of files:
|
---|
6307 |
|
---|
6308 | \snippet doc/src/snippets/qstring/main.cpp 11
|
---|
6309 |
|
---|
6310 | First, \c arg(i) replaces \c %1. Then \c arg(total) replaces \c
|
---|
6311 | %2. Finally, \c arg(fileName) replaces \c %3.
|
---|
6312 |
|
---|
6313 | One advantage of using arg() over sprintf() is that the order of the
|
---|
6314 | numbered place markers can change, if the application's strings are
|
---|
6315 | translated into other languages, but each arg() will still replace
|
---|
6316 | the lowest numbered unreplaced place marker, no matter where it
|
---|
6317 | appears. Also, if place marker \c %i appears more than once in the
|
---|
6318 | string, the arg() replaces all of them.
|
---|
6319 |
|
---|
6320 | If there is no unreplaced place marker remaining, a warning message
|
---|
6321 | is output and the result is undefined. Place marker numbers must be
|
---|
6322 | in the range 1 to 99.
|
---|
6323 | */
|
---|
6324 | QString QString::arg(const QString &a, int fieldWidth, const QChar &fillChar) const
|
---|
6325 | {
|
---|
6326 | ArgEscapeData d = findArgEscapes(*this);
|
---|
6327 |
|
---|
6328 | if (d.occurrences == 0) {
|
---|
6329 | qWarning("QString::arg: Argument missing: %s, %s", toLocal8Bit().data(),
|
---|
6330 | a.toLocal8Bit().data());
|
---|
6331 | return *this;
|
---|
6332 | }
|
---|
6333 | return replaceArgEscapes(*this, d, fieldWidth, a, a, fillChar);
|
---|
6334 | }
|
---|
6335 |
|
---|
6336 | /*!
|
---|
6337 | \fn QString QString::arg(const QString& a1, const QString& a2) const
|
---|
6338 | \overload arg()
|
---|
6339 |
|
---|
6340 | This is the same as \c {str.arg(a1).arg(a2)}, except that the
|
---|
6341 | strings \a a1 and \a a2 are replaced in one pass. This can make a
|
---|
6342 | difference if \a a1 contains e.g. \c{%1}:
|
---|
6343 |
|
---|
6344 | \snippet doc/src/snippets/qstring/main.cpp 13
|
---|
6345 | */
|
---|
6346 |
|
---|
6347 | /*!
|
---|
6348 | \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3) const
|
---|
6349 | \overload arg()
|
---|
6350 |
|
---|
6351 | This is the same as calling \c str.arg(a1).arg(a2).arg(a3), except
|
---|
6352 | that the strings \a a1, \a a2 and \a a3 are replaced in one pass.
|
---|
6353 | */
|
---|
6354 |
|
---|
6355 | /*!
|
---|
6356 | \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4) const
|
---|
6357 | \overload arg()
|
---|
6358 |
|
---|
6359 | This is the same as calling \c
|
---|
6360 | {str.arg(a1).arg(a2).arg(a3).arg(a4)}, except that the strings \a
|
---|
6361 | a1, \a a2, \a a3 and \a a4 are replaced in one pass.
|
---|
6362 | */
|
---|
6363 |
|
---|
6364 | /*!
|
---|
6365 | \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4, const QString& a5) const
|
---|
6366 | \overload arg()
|
---|
6367 |
|
---|
6368 | This is the same as calling \c
|
---|
6369 | {str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5)}, except that the strings
|
---|
6370 | \a a1, \a a2, \a a3, \a a4, and \a a5 are replaced in one pass.
|
---|
6371 | */
|
---|
6372 |
|
---|
6373 | /*!
|
---|
6374 | \fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4, const QString& a5, const QString& a6) const
|
---|
6375 | \overload arg()
|
---|
6376 |
|
---|
6377 | This is the same as calling \c
|
---|
6378 | {str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5).arg(a6))}, except that
|
---|
6379 | the strings \a a1, \a a2, \a a3, \a a4, \a a5, and \a a6 are
|
---|
6380 | replaced in one pass.
|
---|
6381 | */
|
---|
6382 |
|
---|
6383 | /*!
|
---|
6384 | \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
|
---|
6385 | \overload arg()
|
---|
6386 |
|
---|
6387 | This is the same as calling \c
|
---|
6388 | {str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5).arg(a6).arg(a7)},
|
---|
6389 | except that the strings \a a1, \a a2, \a a3, \a a4, \a a5, \a a6,
|
---|
6390 | and \a a7 are replaced in one pass.
|
---|
6391 | */
|
---|
6392 |
|
---|
6393 | /*!
|
---|
6394 | \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
|
---|
6395 | \overload arg()
|
---|
6396 |
|
---|
6397 | This is the same as calling \c
|
---|
6398 | {str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5).arg(a6).arg(a7).arg(a8)},
|
---|
6399 | except that the strings \a a1, \a a2, \a a3, \a a4, \a a5, \a a6, \a
|
---|
6400 | a7, and \a a8 are replaced in one pass.
|
---|
6401 | */
|
---|
6402 |
|
---|
6403 | /*!
|
---|
6404 | \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
|
---|
6405 | \overload arg()
|
---|
6406 |
|
---|
6407 | This is the same as calling \c
|
---|
6408 | {str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5).arg(a6).arg(a7).arg(a8).arg(a9)},
|
---|
6409 | except that the strings \a a1, \a a2, \a a3, \a a4, \a a5, \a a6, \a
|
---|
6410 | a7, \a a8, and \a a9 are replaced in one pass.
|
---|
6411 | */
|
---|
6412 |
|
---|
6413 | /*! \fn QString QString::arg(int a, int fieldWidth, int base, const QChar &fillChar) const
|
---|
6414 | \overload arg()
|
---|
6415 |
|
---|
6416 | The \a a argument is expressed in base \a base, which is 10 by
|
---|
6417 | default and must be between 2 and 36. For bases other than 10, \a a
|
---|
6418 | is treated as an unsigned integer.
|
---|
6419 |
|
---|
6420 | \a fieldWidth specifies the minimum amount of space that \a a is
|
---|
6421 | padded to and filled with the character \a fillChar. A positive
|
---|
6422 | value produces right-aligned text; a negative value produces
|
---|
6423 | left-aligned text.
|
---|
6424 |
|
---|
6425 | The '%' can be followed by an 'L', in which case the sequence is
|
---|
6426 | replaced with a localized representation of \a a. The conversion
|
---|
6427 | uses the default locale, set by QLocale::setDefault(). If no default
|
---|
6428 | locale was specified, the "C" locale is used. The 'L' flag is
|
---|
6429 | ignored if \a base is not 10.
|
---|
6430 |
|
---|
6431 | \snippet doc/src/snippets/qstring/main.cpp 12
|
---|
6432 | \snippet doc/src/snippets/qstring/main.cpp 14
|
---|
6433 |
|
---|
6434 | If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
|
---|
6435 | used. For negative numbers, zero padding might appear before the
|
---|
6436 | minus sign.
|
---|
6437 | */
|
---|
6438 |
|
---|
6439 | /*! \fn QString QString::arg(uint a, int fieldWidth, int base, const QChar &fillChar) const
|
---|
6440 | \overload arg()
|
---|
6441 |
|
---|
6442 | The \a base argument specifies the base to use when converting the
|
---|
6443 | integer \a a into a string. The base must be between 2 and 36.
|
---|
6444 |
|
---|
6445 | If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
|
---|
6446 | used. For negative numbers, zero padding might appear before the
|
---|
6447 | minus sign.
|
---|
6448 | */
|
---|
6449 |
|
---|
6450 | /*! \fn QString QString::arg(long a, int fieldWidth, int base, const QChar &fillChar) const
|
---|
6451 | \overload arg()
|
---|
6452 |
|
---|
6453 | \a fieldWidth specifies the minimum amount of space that \a a is
|
---|
6454 | padded to and filled with the character \a fillChar. A positive
|
---|
6455 | value produces right-aligned text; a negative value produces
|
---|
6456 | left-aligned text.
|
---|
6457 |
|
---|
6458 | The \a a argument is expressed in the given \a base, which is 10 by
|
---|
6459 | default and must be between 2 and 36.
|
---|
6460 |
|
---|
6461 | The '%' can be followed by an 'L', in which case the sequence is
|
---|
6462 | replaced with a localized representation of \a a. The conversion
|
---|
6463 | uses the default locale. The default locale is determined from the
|
---|
6464 | system's locale settings at application startup. It can be changed
|
---|
6465 | using QLocale::setDefault(). The 'L' flag is ignored if \a base is
|
---|
6466 | not 10.
|
---|
6467 |
|
---|
6468 | \snippet doc/src/snippets/qstring/main.cpp 12
|
---|
6469 | \snippet doc/src/snippets/qstring/main.cpp 14
|
---|
6470 |
|
---|
6471 | If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
|
---|
6472 | used. For negative numbers, zero padding might appear before the
|
---|
6473 | minus sign.
|
---|
6474 | */
|
---|
6475 |
|
---|
6476 | /*! \fn QString QString::arg(ulong a, int fieldWidth, int base, const QChar &fillChar) const
|
---|
6477 | \overload arg()
|
---|
6478 |
|
---|
6479 | \a fieldWidth specifies the minimum amount of space that \a a is
|
---|
6480 | padded to and filled with the character \a fillChar. A positive
|
---|
6481 | value produces right-aligned text; a negative value produces
|
---|
6482 | left-aligned text.
|
---|
6483 |
|
---|
6484 | The \a base argument specifies the base to use when converting the
|
---|
6485 | integer \a a to a string. The base must be between 2 and 36, with 8
|
---|
6486 | giving octal, 10 decimal, and 16 hexadecimal numbers.
|
---|
6487 |
|
---|
6488 | If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
|
---|
6489 | used. For negative numbers, zero padding might appear before the
|
---|
6490 | minus sign.
|
---|
6491 | */
|
---|
6492 |
|
---|
6493 | /*!
|
---|
6494 | \overload arg()
|
---|
6495 |
|
---|
6496 | \a fieldWidth specifies the minimum amount of space that \a a is
|
---|
6497 | padded to and filled with the character \a fillChar. A positive
|
---|
6498 | value produces right-aligned text; a negative value produces
|
---|
6499 | left-aligned text.
|
---|
6500 |
|
---|
6501 | The \a base argument specifies the base to use when converting the
|
---|
6502 | integer \a a into a string. The base must be between 2 and 36, with
|
---|
6503 | 8 giving octal, 10 decimal, and 16 hexadecimal numbers.
|
---|
6504 |
|
---|
6505 | If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
|
---|
6506 | used. For negative numbers, zero padding might appear before the
|
---|
6507 | minus sign.
|
---|
6508 | */
|
---|
6509 | QString QString::arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
|
---|
6510 | {
|
---|
6511 | ArgEscapeData d = findArgEscapes(*this);
|
---|
6512 |
|
---|
6513 | if (d.occurrences == 0) {
|
---|
6514 | qWarning() << "QString::arg: Argument missing:" << *this << ',' << a;
|
---|
6515 | return *this;
|
---|
6516 | }
|
---|
6517 |
|
---|
6518 | unsigned flags = QLocalePrivate::NoFlags;
|
---|
6519 | if (fillChar == QLatin1Char('0'))
|
---|
6520 | flags = QLocalePrivate::ZeroPadded;
|
---|
6521 |
|
---|
6522 | QString arg;
|
---|
6523 | if (d.occurrences > d.locale_occurrences)
|
---|
6524 | arg = QLocale::c().d()->longLongToString(a, -1, base, fieldWidth, flags);
|
---|
6525 |
|
---|
6526 | QString locale_arg;
|
---|
6527 | if (d.locale_occurrences > 0) {
|
---|
6528 | QLocale locale;
|
---|
6529 | locale_arg = locale.d()->longLongToString(a, -1, base, fieldWidth,
|
---|
6530 | flags | QLocalePrivate::ThousandsGroup);
|
---|
6531 | }
|
---|
6532 |
|
---|
6533 | return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg, fillChar);
|
---|
6534 | }
|
---|
6535 |
|
---|
6536 | /*!
|
---|
6537 | \overload arg()
|
---|
6538 |
|
---|
6539 | \a fieldWidth specifies the minimum amount of space that \a a is
|
---|
6540 | padded to and filled with the character \a fillChar. A positive
|
---|
6541 | value produces right-aligned text; a negative value produces
|
---|
6542 | left-aligned text.
|
---|
6543 |
|
---|
6544 | The \a base argument specifies the base to use when converting the
|
---|
6545 | integer \a a into a string. \a base must be between 2 and 36, with 8
|
---|
6546 | giving octal, 10 decimal, and 16 hexadecimal numbers.
|
---|
6547 |
|
---|
6548 | If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
|
---|
6549 | used. For negative numbers, zero padding might appear before the
|
---|
6550 | minus sign.
|
---|
6551 | */
|
---|
6552 | QString QString::arg(qulonglong a, int fieldWidth, int base, const QChar &fillChar) const
|
---|
6553 | {
|
---|
6554 | ArgEscapeData d = findArgEscapes(*this);
|
---|
6555 |
|
---|
6556 | if (d.occurrences == 0) {
|
---|
6557 | qWarning() << "QString::arg: Argument missing:" << *this << ',' << a;
|
---|
6558 | return *this;
|
---|
6559 | }
|
---|
6560 |
|
---|
6561 | unsigned flags = QLocalePrivate::NoFlags;
|
---|
6562 | if (fillChar == QLatin1Char('0'))
|
---|
6563 | flags = QLocalePrivate::ZeroPadded;
|
---|
6564 |
|
---|
6565 | QString arg;
|
---|
6566 | if (d.occurrences > d.locale_occurrences)
|
---|
6567 | arg = QLocale::c().d()->unsLongLongToString(a, -1, base, fieldWidth, flags);
|
---|
6568 |
|
---|
6569 | QString locale_arg;
|
---|
6570 | if (d.locale_occurrences > 0) {
|
---|
6571 | QLocale locale;
|
---|
6572 | locale_arg = locale.d()->unsLongLongToString(a, -1, base, fieldWidth,
|
---|
6573 | flags | QLocalePrivate::ThousandsGroup);
|
---|
6574 | }
|
---|
6575 |
|
---|
6576 | return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg, fillChar);
|
---|
6577 | }
|
---|
6578 |
|
---|
6579 | /*!
|
---|
6580 | \overload arg()
|
---|
6581 |
|
---|
6582 | \fn QString QString::arg(short a, int fieldWidth, int base, const QChar &fillChar) const
|
---|
6583 |
|
---|
6584 | \a fieldWidth specifies the minimum amount of space that \a a is
|
---|
6585 | padded to and filled with the character \a fillChar. A positive
|
---|
6586 | value produces right-aligned text; a negative value produces
|
---|
6587 | left-aligned text.
|
---|
6588 |
|
---|
6589 | The \a base argument specifies the base to use when converting the
|
---|
6590 | integer \a a into a string. The base must be between 2 and 36, with
|
---|
6591 | 8 giving octal, 10 decimal, and 16 hexadecimal numbers.
|
---|
6592 |
|
---|
6593 | If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
|
---|
6594 | used. For negative numbers, zero padding might appear before the
|
---|
6595 | minus sign.
|
---|
6596 | */
|
---|
6597 |
|
---|
6598 | /*!
|
---|
6599 | \fn QString QString::arg(ushort a, int fieldWidth, int base, const QChar &fillChar) const
|
---|
6600 | \overload arg()
|
---|
6601 |
|
---|
6602 | \a fieldWidth specifies the minimum amount of space that \a a is
|
---|
6603 | padded to and filled with the character \a fillChar. A positive
|
---|
6604 | value produces right-aligned text; a negative value produces
|
---|
6605 | left-aligned text.
|
---|
6606 |
|
---|
6607 | The \a base argument specifies the base to use when converting the
|
---|
6608 | integer \a a into a string. The base must be between 2 and 36, with
|
---|
6609 | 8 giving octal, 10 decimal, and 16 hexadecimal numbers.
|
---|
6610 |
|
---|
6611 | If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is
|
---|
6612 | used. For negative numbers, zero padding might appear before the
|
---|
6613 | minus sign.
|
---|
6614 | */
|
---|
6615 |
|
---|
6616 | /*!
|
---|
6617 | \overload arg()
|
---|
6618 | */
|
---|
6619 | QString QString::arg(QChar a, int fieldWidth, const QChar &fillChar) const
|
---|
6620 | {
|
---|
6621 | QString c;
|
---|
6622 | c += a;
|
---|
6623 | return arg(c, fieldWidth, fillChar);
|
---|
6624 | }
|
---|
6625 |
|
---|
6626 | /*!
|
---|
6627 | \overload arg()
|
---|
6628 |
|
---|
6629 | The \a a argument is interpreted as a Latin-1 character.
|
---|
6630 | */
|
---|
6631 | QString QString::arg(char a, int fieldWidth, const QChar &fillChar) const
|
---|
6632 | {
|
---|
6633 | QString c;
|
---|
6634 | c += QLatin1Char(a);
|
---|
6635 | return arg(c, fieldWidth, fillChar);
|
---|
6636 | }
|
---|
6637 |
|
---|
6638 | /*!
|
---|
6639 | \fn QString QString::arg(double a, int fieldWidth, char format, int precision, const QChar &fillChar) const
|
---|
6640 | \overload arg()
|
---|
6641 |
|
---|
6642 | Argument \a a is formatted according to the specified \a format and
|
---|
6643 | \a precision. See \l{Argument Formats} for details.
|
---|
6644 |
|
---|
6645 | \a fieldWidth specifies the minimum amount of space that \a a is
|
---|
6646 | padded to and filled with the character \a fillChar. A positive
|
---|
6647 | value produces right-aligned text; a negative value produces
|
---|
6648 | left-aligned text.
|
---|
6649 |
|
---|
6650 | \snippet doc/src/snippets/code/src_corelib_tools_qstring.cpp 2
|
---|
6651 |
|
---|
6652 | The '%' can be followed by an 'L', in which case the sequence is
|
---|
6653 | replaced with a localized representation of \a a. The conversion
|
---|
6654 | uses the default locale, set by QLocale::setDefaultLocale(). If no
|
---|
6655 | default locale was specified, the "C" locale is used.
|
---|
6656 |
|
---|
6657 | If \a fillChar is '0' (the number 0, ASCII 48), this function will
|
---|
6658 | use the locale's zero to pad. For negative numbers, the zero padding
|
---|
6659 | will probably appear before the minus sign.
|
---|
6660 |
|
---|
6661 | \sa QLocale::toString()
|
---|
6662 | */
|
---|
6663 | QString QString::arg(double a, int fieldWidth, char fmt, int prec, const QChar &fillChar) const
|
---|
6664 | {
|
---|
6665 | ArgEscapeData d = findArgEscapes(*this);
|
---|
6666 |
|
---|
6667 | if (d.occurrences == 0) {
|
---|
6668 | qWarning("QString::arg: Argument missing: %s, %g", toLocal8Bit().data(), a);
|
---|
6669 | return *this;
|
---|
6670 | }
|
---|
6671 |
|
---|
6672 | unsigned flags = QLocalePrivate::NoFlags;
|
---|
6673 | if (fillChar == QLatin1Char('0'))
|
---|
6674 | flags = QLocalePrivate::ZeroPadded;
|
---|
6675 |
|
---|
6676 | if (qIsUpper(fmt))
|
---|
6677 | flags |= QLocalePrivate::CapitalEorX;
|
---|
6678 | fmt = qToLower(fmt);
|
---|
6679 |
|
---|
6680 | QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
|
---|
6681 | switch (fmt) {
|
---|
6682 | case 'f':
|
---|
6683 | form = QLocalePrivate::DFDecimal;
|
---|
6684 | break;
|
---|
6685 | case 'e':
|
---|
6686 | form = QLocalePrivate::DFExponent;
|
---|
6687 | break;
|
---|
6688 | case 'g':
|
---|
6689 | form = QLocalePrivate::DFSignificantDigits;
|
---|
6690 | break;
|
---|
6691 | default:
|
---|
6692 | #if defined(QT_CHECK_RANGE)
|
---|
6693 | qWarning("QString::arg: Invalid format char '%c'", fmt);
|
---|
6694 | #endif
|
---|
6695 | break;
|
---|
6696 | }
|
---|
6697 |
|
---|
6698 | QString arg;
|
---|
6699 | if (d.occurrences > d.locale_occurrences)
|
---|
6700 | arg = QLocale::c().d()->doubleToString(a, prec, form, fieldWidth, flags);
|
---|
6701 |
|
---|
6702 | QString locale_arg;
|
---|
6703 | if (d.locale_occurrences > 0) {
|
---|
6704 | QLocale locale;
|
---|
6705 |
|
---|
6706 | flags |= QLocalePrivate::ThousandsGroup;
|
---|
6707 | locale_arg = locale.d()->doubleToString(a, prec, form, fieldWidth, flags);
|
---|
6708 | }
|
---|
6709 |
|
---|
6710 | return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg, fillChar);
|
---|
6711 | }
|
---|
6712 |
|
---|
6713 | static int getEscape(const QChar *uc, int *pos, int len, int maxNumber = 999)
|
---|
6714 | {
|
---|
6715 | int i = *pos;
|
---|
6716 | ++i;
|
---|
6717 | if (i < len && uc[i] == QLatin1Char('L'))
|
---|
6718 | ++i;
|
---|
6719 | if (i < len) {
|
---|
6720 | int escape = uc[i].unicode() - '0';
|
---|
6721 | if (uint(escape) >= 10U)
|
---|
6722 | return -1;
|
---|
6723 | ++i;
|
---|
6724 | while (i < len) {
|
---|
6725 | int digit = uc[i].unicode() - '0';
|
---|
6726 | if (uint(digit) >= 10U)
|
---|
6727 | break;
|
---|
6728 | escape = (escape * 10) + digit;
|
---|
6729 | ++i;
|
---|
6730 | }
|
---|
6731 | if (escape <= maxNumber) {
|
---|
6732 | *pos = i;
|
---|
6733 | return escape;
|
---|
6734 | }
|
---|
6735 | }
|
---|
6736 | return -1;
|
---|
6737 | }
|
---|
6738 |
|
---|
6739 | QString QString::multiArg(int numArgs, const QString **args) const
|
---|
6740 | {
|
---|
6741 | QString result;
|
---|
6742 | QMap<int, int> numbersUsed;
|
---|
6743 | const QChar *uc = (const QChar *) d->data;
|
---|
6744 | const int len = d->size;
|
---|
6745 | const int end = len - 1;
|
---|
6746 | int lastNumber = -1;
|
---|
6747 | int i = 0;
|
---|
6748 |
|
---|
6749 | // populate the numbersUsed map with the %n's that actually occur in the string
|
---|
6750 | while (i < end) {
|
---|
6751 | if (uc[i] == QLatin1Char('%')) {
|
---|
6752 | int number = getEscape(uc, &i, len);
|
---|
6753 | if (number != -1) {
|
---|
6754 | numbersUsed.insert(number, -1);
|
---|
6755 | continue;
|
---|
6756 | }
|
---|
6757 | }
|
---|
6758 | ++i;
|
---|
6759 | }
|
---|
6760 |
|
---|
6761 | // assign an argument number to each of the %n's
|
---|
6762 | QMap<int, int>::iterator j = numbersUsed.begin();
|
---|
6763 | QMap<int, int>::iterator jend = numbersUsed.end();
|
---|
6764 | int arg = 0;
|
---|
6765 | while (j != jend && arg < numArgs) {
|
---|
6766 | *j = arg++;
|
---|
6767 | lastNumber = j.key();
|
---|
6768 | ++j;
|
---|
6769 | }
|
---|
6770 |
|
---|
6771 | // sanity
|
---|
6772 | if (numArgs > arg) {
|
---|
6773 | qWarning("QString::arg: %d argument(s) missing in %s", numArgs - arg, toLocal8Bit().data());
|
---|
6774 | numArgs = arg;
|
---|
6775 | }
|
---|
6776 |
|
---|
6777 | i = 0;
|
---|
6778 | while (i < len) {
|
---|
6779 | if (uc[i] == QLatin1Char('%') && i != end) {
|
---|
6780 | int number = getEscape(uc, &i, len, lastNumber);
|
---|
6781 | int arg = numbersUsed[number];
|
---|
6782 | if (number != -1 && arg != -1) {
|
---|
6783 | result += *args[arg];
|
---|
6784 | continue;
|
---|
6785 | }
|
---|
6786 | }
|
---|
6787 | result += uc[i++];
|
---|
6788 | }
|
---|
6789 | return result;
|
---|
6790 | }
|
---|
6791 |
|
---|
6792 | /*! \internal
|
---|
6793 | */
|
---|
6794 | void QString::updateProperties() const
|
---|
6795 | {
|
---|
6796 | ushort *p = d->data;
|
---|
6797 | ushort *end = p + d->size;
|
---|
6798 | d->simpletext = true;
|
---|
6799 | while (p < end) {
|
---|
6800 | ushort uc = *p;
|
---|
6801 | // sort out regions of complex text formatting
|
---|
6802 | if (uc > 0x058f && (uc < 0x1100 || uc > 0xfb0f)) {
|
---|
6803 | d->simpletext = false;
|
---|
6804 | }
|
---|
6805 | p++;
|
---|
6806 | }
|
---|
6807 |
|
---|
6808 | p = d->data;
|
---|
6809 | d->righttoleft = false;
|
---|
6810 | while (p < end) {
|
---|
6811 | switch(QChar::direction(*p))
|
---|
6812 | {
|
---|
6813 | case QChar::DirL:
|
---|
6814 | case QChar::DirLRO:
|
---|
6815 | case QChar::DirLRE:
|
---|
6816 | goto end;
|
---|
6817 | case QChar::DirR:
|
---|
6818 | case QChar::DirAL:
|
---|
6819 | case QChar::DirRLO:
|
---|
6820 | case QChar::DirRLE:
|
---|
6821 | d->righttoleft = true;
|
---|
6822 | goto end;
|
---|
6823 | default:
|
---|
6824 | break;
|
---|
6825 | }
|
---|
6826 | ++p;
|
---|
6827 | }
|
---|
6828 | end:
|
---|
6829 | d->clean = true;
|
---|
6830 | return;
|
---|
6831 | }
|
---|
6832 |
|
---|
6833 | /*! \fn bool QString::isSimpleText() const
|
---|
6834 |
|
---|
6835 | \internal
|
---|
6836 | */
|
---|
6837 |
|
---|
6838 | /*! \fn bool QString::isRightToLeft() const
|
---|
6839 |
|
---|
6840 | \internal
|
---|
6841 | */
|
---|
6842 |
|
---|
6843 |
|
---|
6844 | /*! \fn QChar *QString::data()
|
---|
6845 |
|
---|
6846 | Returns a pointer to the data stored in the QString. The pointer
|
---|
6847 | can be used to access and modify the characters that compose the
|
---|
6848 | string. For convenience, the data is '\\0'-terminated.
|
---|
6849 |
|
---|
6850 | Example:
|
---|
6851 |
|
---|
6852 | \snippet doc/src/snippets/qstring/main.cpp 19
|
---|
6853 |
|
---|
6854 | Note that the pointer remains valid only as long as the string is
|
---|
6855 | not modified by other means. For read-only access, constData() is
|
---|
6856 | faster because it never causes a \l{deep copy} to occur.
|
---|
6857 |
|
---|
6858 | \sa constData(), operator[]()
|
---|
6859 | */
|
---|
6860 |
|
---|
6861 | /*! \fn const QChar *QString::data() const
|
---|
6862 |
|
---|
6863 | \overload
|
---|
6864 | */
|
---|
6865 |
|
---|
6866 | /*! \fn const QChar *QString::constData() const
|
---|
6867 |
|
---|
6868 | Returns a pointer to the data stored in the QString. The pointer
|
---|
6869 | can be used to access the characters that compose the string. For
|
---|
6870 | convenience, the data is '\\0'-terminated.
|
---|
6871 |
|
---|
6872 | Note that the pointer remains valid only as long as the string is
|
---|
6873 | not modified.
|
---|
6874 |
|
---|
6875 | \sa data(), operator[]()
|
---|
6876 | */
|
---|
6877 |
|
---|
6878 | /*! \fn void QString::push_front(const QString &other)
|
---|
6879 |
|
---|
6880 | This function is provided for STL compatibility, prepending the
|
---|
6881 | given \a other string to the beginning of this string. It is
|
---|
6882 | equivalent to \c prepend(other).
|
---|
6883 |
|
---|
6884 | \sa prepend()
|
---|
6885 | */
|
---|
6886 |
|
---|
6887 | /*! \fn void QString::push_front(QChar ch)
|
---|
6888 |
|
---|
6889 | \overload
|
---|
6890 |
|
---|
6891 | Prepends the given \a ch character to the beginning of this string.
|
---|
6892 | */
|
---|
6893 |
|
---|
6894 | /*! \fn void QString::push_back(const QString &other)
|
---|
6895 |
|
---|
6896 | This function is provided for STL compatibility, appending the
|
---|
6897 | given \a other string onto the end of this string. It is
|
---|
6898 | equivalent to \c append(other).
|
---|
6899 |
|
---|
6900 | \sa append()
|
---|
6901 | */
|
---|
6902 |
|
---|
6903 | /*! \fn void QString::push_back(QChar ch)
|
---|
6904 |
|
---|
6905 | \overload
|
---|
6906 |
|
---|
6907 | Appends the given \a ch character onto the end of this string.
|
---|
6908 | */
|
---|
6909 |
|
---|
6910 | /*!
|
---|
6911 | \fn std::string QString::toStdString() const
|
---|
6912 |
|
---|
6913 | Returns a std::string object with the data contained in this
|
---|
6914 | QString. The Unicode data is converted into 8-bit characters using
|
---|
6915 | the toAscii() function.
|
---|
6916 |
|
---|
6917 | This operator is mostly useful to pass a QString to a function
|
---|
6918 | that accepts a std::string object.
|
---|
6919 |
|
---|
6920 | If the QString contains non-ASCII Unicode characters, using this
|
---|
6921 | operator can lead to loss of information, since the implementation
|
---|
6922 | calls toAscii().
|
---|
6923 |
|
---|
6924 | This operator is only available if Qt is configured with STL
|
---|
6925 | compatibility enabled.
|
---|
6926 |
|
---|
6927 | \sa toAscii(), toLatin1(), toUtf8(), toLocal8Bit()
|
---|
6928 | */
|
---|
6929 |
|
---|
6930 | /*!
|
---|
6931 | Constructs a QString that uses the first \a size Unicode characters
|
---|
6932 | in the array \a unicode. The data in \a unicode is \e not
|
---|
6933 | copied. The caller must be able to guarantee that \a unicode will
|
---|
6934 | not be deleted or modified as long as the QString (or an
|
---|
6935 | unmodified copy of it) exists.
|
---|
6936 |
|
---|
6937 | Any attempts to modify the QString or copies of it will cause it
|
---|
6938 | to create a deep copy of the data, ensuring that the raw data
|
---|
6939 | isn't modified.
|
---|
6940 |
|
---|
6941 | Here's an example of how we can use a QRegExp on raw data in
|
---|
6942 | memory without requiring to copy the data into a QString:
|
---|
6943 |
|
---|
6944 | \snippet doc/src/snippets/qstring/main.cpp 22
|
---|
6945 | \snippet doc/src/snippets/qstring/main.cpp 23
|
---|
6946 |
|
---|
6947 | \warning A string created with fromRawData() is \e not
|
---|
6948 | '\\0'-terminated, unless the raw data contains a '\\0' character
|
---|
6949 | at position \a size. This means unicode() will \e not return a
|
---|
6950 | '\\0'-terminated string (although utf16() does, at the cost of
|
---|
6951 | copying the raw data).
|
---|
6952 |
|
---|
6953 | \sa fromUtf16()
|
---|
6954 | */
|
---|
6955 | QString QString::fromRawData(const QChar *unicode, int size)
|
---|
6956 | {
|
---|
6957 | Data *x = static_cast<Data *>(qMalloc(sizeof(Data)));
|
---|
6958 | Q_CHECK_PTR(x);
|
---|
6959 | if (unicode) {
|
---|
6960 | x->data = (ushort *)unicode;
|
---|
6961 | } else {
|
---|
6962 | x->data = x->array;
|
---|
6963 | size = 0;
|
---|
6964 | }
|
---|
6965 | x->ref = 1;
|
---|
6966 | x->alloc = x->size = size;
|
---|
6967 | *x->array = '\0';
|
---|
6968 | x->clean = x->asciiCache = x->simpletext = x->righttoleft = x->capacity = 0;
|
---|
6969 | return QString(x, 0);
|
---|
6970 | }
|
---|
6971 |
|
---|
6972 | /*! \class QLatin1String
|
---|
6973 | \brief The QLatin1String class provides a thin wrapper around an ASCII/Latin-1 encoded string literal.
|
---|
6974 |
|
---|
6975 | \ingroup string-processing
|
---|
6976 | \reentrant
|
---|
6977 |
|
---|
6978 | Many of QString's member functions are overloaded to accept
|
---|
6979 | \c{const char *} instead of QString. This includes the copy
|
---|
6980 | constructor, the assignment operator, the comparison operators,
|
---|
6981 | and various other functions such as \link QString::insert()
|
---|
6982 | insert() \endlink, \link QString::replace() replace()\endlink,
|
---|
6983 | and \link QString::indexOf() indexOf()\endlink. These functions
|
---|
6984 | are usually optimized to avoid constructing a QString object for
|
---|
6985 | the \c{const char *} data. For example, assuming \c str is a
|
---|
6986 | QString,
|
---|
6987 |
|
---|
6988 | \snippet doc/src/snippets/code/src_corelib_tools_qstring.cpp 3
|
---|
6989 |
|
---|
6990 | is much faster than
|
---|
6991 |
|
---|
6992 | \snippet doc/src/snippets/code/src_corelib_tools_qstring.cpp 4
|
---|
6993 |
|
---|
6994 | because it doesn't construct four temporary QString objects and
|
---|
6995 | make a deep copy of the character data.
|
---|
6996 |
|
---|
6997 | Applications that define \c QT_NO_CAST_FROM_ASCII (as explained
|
---|
6998 | in the QString documentation) don't have access to QString's
|
---|
6999 | \c{const char *} API. To provide an efficient way of specifying
|
---|
7000 | constant Latin-1 strings, Qt provides the QLatin1String, which is
|
---|
7001 | just a very thin wrapper around a \c{const char *}. Using
|
---|
7002 | QLatin1String, the example code above becomes
|
---|
7003 |
|
---|
7004 | \snippet doc/src/snippets/code/src_corelib_tools_qstring.cpp 5
|
---|
7005 |
|
---|
7006 | This is a bit longer to type, but it provides exactly the same
|
---|
7007 | benefits as the first version of the code, and is faster than
|
---|
7008 | converting the Latin-1 strings using QString::fromLatin1().
|
---|
7009 |
|
---|
7010 | Thanks to the QString(const QLatin1String &) constructor,
|
---|
7011 | QLatin1String can be used everywhere a QString is expected. For
|
---|
7012 | example:
|
---|
7013 |
|
---|
7014 | \snippet doc/src/snippets/code/src_corelib_tools_qstring.cpp 6
|
---|
7015 |
|
---|
7016 | \sa QString, QLatin1Char
|
---|
7017 | */
|
---|
7018 |
|
---|
7019 | /*! \fn QLatin1String::QLatin1String(const char *str)
|
---|
7020 |
|
---|
7021 | Constructs a QLatin1String object that stores \a str. Note that if
|
---|
7022 | \a str is 0, an empty string is created; this case is handled by
|
---|
7023 | QString.
|
---|
7024 |
|
---|
7025 | The string data is \e not copied. The caller must be able to
|
---|
7026 | guarantee that \a str will not be deleted or modified as long as
|
---|
7027 | the QLatin1String object exists.
|
---|
7028 |
|
---|
7029 | \sa latin1()
|
---|
7030 | */
|
---|
7031 |
|
---|
7032 | /*!
|
---|
7033 | \since 4.1
|
---|
7034 | \fn QLatin1String &QLatin1String::operator=(const QLatin1String &other)
|
---|
7035 |
|
---|
7036 | Constructs a copy of \a other.
|
---|
7037 | */
|
---|
7038 |
|
---|
7039 | /*! \fn const char *QLatin1String::latin1() const
|
---|
7040 |
|
---|
7041 | Returns the Latin-1 string stored in this object.
|
---|
7042 | */
|
---|
7043 |
|
---|
7044 | /*! \fn bool QLatin1String::operator==(const QString &other) const
|
---|
7045 |
|
---|
7046 | Returns true if this string is equal to string \a other;
|
---|
7047 | otherwise returns false.
|
---|
7048 |
|
---|
7049 | The comparison is based exclusively on the numeric Unicode values
|
---|
7050 | of the characters and is very fast, but is not what a human would
|
---|
7051 | expect. Consider sorting user-interface strings with
|
---|
7052 | QString::localeAwareCompare().
|
---|
7053 | */
|
---|
7054 |
|
---|
7055 | /*!
|
---|
7056 | \fn bool QLatin1String::operator==(const char *other) const
|
---|
7057 | \since 4.3
|
---|
7058 | \overload
|
---|
7059 |
|
---|
7060 | The \a other const char pointer is converted to a QLatin1String using
|
---|
7061 | the QString::fromAscii() function.
|
---|
7062 |
|
---|
7063 | You can disable this operator by defining \c
|
---|
7064 | QT_NO_CAST_FROM_ASCII when you compile your applications. This
|
---|
7065 | can be useful if you want to ensure that all user-visible strings
|
---|
7066 | go through QObject::tr(), for example.
|
---|
7067 | */
|
---|
7068 |
|
---|
7069 | /*! \fn bool QLatin1String::operator!=(const QString &other) const
|
---|
7070 |
|
---|
7071 | Returns true if this string is not equal to string \a other;
|
---|
7072 | otherwise returns false.
|
---|
7073 |
|
---|
7074 | The comparison is based exclusively on the numeric Unicode values
|
---|
7075 | of the characters and is very fast, but is not what a human would
|
---|
7076 | expect. Consider sorting user-interface strings with
|
---|
7077 | QString::localeAwareCompare().
|
---|
7078 | */
|
---|
7079 |
|
---|
7080 | /*!
|
---|
7081 | \fn bool QLatin1String::operator!=(const char *other) const
|
---|
7082 | \since 4.3
|
---|
7083 | \overload operator!=()
|
---|
7084 |
|
---|
7085 | The \a other const char pointer is converted to a QLatin1String using
|
---|
7086 | the QString::fromAscii() function.
|
---|
7087 |
|
---|
7088 | You can disable this operator by defining \c
|
---|
7089 | QT_NO_CAST_FROM_ASCII when you compile your applications. This
|
---|
7090 | can be useful if you want to ensure that all user-visible strings
|
---|
7091 | go through QObject::tr(), for example.
|
---|
7092 | */
|
---|
7093 |
|
---|
7094 | /*!
|
---|
7095 | \fn bool QLatin1String::operator>(const QString &other) const
|
---|
7096 |
|
---|
7097 | Returns true if this string is lexically greater than string \a
|
---|
7098 | other; otherwise returns false.
|
---|
7099 |
|
---|
7100 | The comparison is based exclusively on the numeric Unicode values
|
---|
7101 | of the characters and is very fast, but is not what a human would
|
---|
7102 | expect. Consider sorting user-interface strings with
|
---|
7103 | QString::localeAwareCompare().
|
---|
7104 | */
|
---|
7105 |
|
---|
7106 | /*!
|
---|
7107 | \fn bool QLatin1String::operator>(const char *other) const
|
---|
7108 | \since 4.3
|
---|
7109 | \overload
|
---|
7110 |
|
---|
7111 | The \a other const char pointer is converted to a QLatin1String using
|
---|
7112 | the QString::fromAscii() function.
|
---|
7113 |
|
---|
7114 | You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII
|
---|
7115 | when you compile your applications. This can be useful if you want
|
---|
7116 | to ensure that all user-visible strings go through QObject::tr(),
|
---|
7117 | for example.
|
---|
7118 | */
|
---|
7119 |
|
---|
7120 | /*!
|
---|
7121 | \fn bool QLatin1String::operator<(const QString &other) const
|
---|
7122 |
|
---|
7123 | Returns true if this string is lexically less than the \a other
|
---|
7124 | string; otherwise returns false.
|
---|
7125 |
|
---|
7126 | The comparison is based exclusively on the numeric Unicode values
|
---|
7127 | of the characters and is very fast, but is not what a human would
|
---|
7128 | expect. Consider sorting user-interface strings using the
|
---|
7129 | QString::localeAwareCompare() function.
|
---|
7130 | */
|
---|
7131 |
|
---|
7132 | /*!
|
---|
7133 | \fn bool QLatin1String::operator<(const char *other) const
|
---|
7134 | \since 4.3
|
---|
7135 | \overload
|
---|
7136 |
|
---|
7137 | The \a other const char pointer is converted to a QLatin1String using
|
---|
7138 | the QString::fromAscii() function.
|
---|
7139 |
|
---|
7140 | You can disable this operator by defining \c
|
---|
7141 | QT_NO_CAST_FROM_ASCII when you compile your applications. This
|
---|
7142 | can be useful if you want to ensure that all user-visible strings
|
---|
7143 | go through QObject::tr(), for example.
|
---|
7144 | */
|
---|
7145 |
|
---|
7146 | /*!
|
---|
7147 | \fn bool QLatin1String::operator>=(const QString &other) const
|
---|
7148 |
|
---|
7149 | Returns true if this string is lexically greater than or equal
|
---|
7150 | to string \a other; otherwise returns false.
|
---|
7151 |
|
---|
7152 | The comparison is based exclusively on the numeric Unicode values
|
---|
7153 | of the characters and is very fast, but is not what a human would
|
---|
7154 | expect. Consider sorting user-interface strings with
|
---|
7155 | QString::localeAwareCompare().
|
---|
7156 | */
|
---|
7157 |
|
---|
7158 | /*!
|
---|
7159 | \fn bool QLatin1String::operator>=(const char *other) const
|
---|
7160 | \since 4.3
|
---|
7161 | \overload
|
---|
7162 |
|
---|
7163 | The \a other const char pointer is converted to a QLatin1String using
|
---|
7164 | the QString::fromAscii() function.
|
---|
7165 |
|
---|
7166 | You can disable this operator by defining \c
|
---|
7167 | QT_NO_CAST_FROM_ASCII when you compile your applications. This
|
---|
7168 | can be useful if you want to ensure that all user-visible strings
|
---|
7169 | go through QObject::tr(), for example.
|
---|
7170 | */
|
---|
7171 |
|
---|
7172 | /*! \fn bool QLatin1String::operator<=(const QString &other) const
|
---|
7173 |
|
---|
7174 | Returns true if this string is lexically less than or equal
|
---|
7175 | to string \a other; otherwise returns false.
|
---|
7176 |
|
---|
7177 | The comparison is based exclusively on the numeric Unicode values
|
---|
7178 | of the characters and is very fast, but is not what a human would
|
---|
7179 | expect. Consider sorting user-interface strings with
|
---|
7180 | QString::localeAwareCompare().
|
---|
7181 | */
|
---|
7182 |
|
---|
7183 | /*!
|
---|
7184 | \fn bool QLatin1String::operator<=(const char *other) const
|
---|
7185 | \since 4.3
|
---|
7186 | \overload
|
---|
7187 |
|
---|
7188 | The \a other const char pointer is converted to a QString using
|
---|
7189 | the QString::fromAscii() function.
|
---|
7190 |
|
---|
7191 | You can disable this operator by defining \c
|
---|
7192 | QT_NO_CAST_FROM_ASCII when you compile your applications. This
|
---|
7193 | can be useful if you want to ensure that all user-visible strings
|
---|
7194 | go through QObject::tr(), for example.
|
---|
7195 | */
|
---|
7196 |
|
---|
7197 |
|
---|
7198 |
|
---|
7199 | /* \fn bool operator==(const QLatin1String &s1, const QLatin1String &s2)
|
---|
7200 | \relates QLatin1String
|
---|
7201 |
|
---|
7202 | Returns true if string \a s1 is lexically equal to string \a s2; otherwise
|
---|
7203 | returns false.
|
---|
7204 | */
|
---|
7205 | /* \fn bool operator!=(const QLatin1String &s1, const QLatin1String &s2)
|
---|
7206 | \relates QLatin1String
|
---|
7207 |
|
---|
7208 | Returns true if string \a s1 is lexically unequal to string \a s2; otherwise
|
---|
7209 | returns false.
|
---|
7210 | */
|
---|
7211 | /* \fn bool operator<(const QLatin1String &s1, const QLatin1String &s2)
|
---|
7212 | \relates QLatin1String
|
---|
7213 |
|
---|
7214 | Returns true if string \a s1 is lexically smaller than string \a s2; otherwise
|
---|
7215 | returns false.
|
---|
7216 | */
|
---|
7217 | /* \fn bool operator<=(const QLatin1String &s1, const QLatin1String &s2)
|
---|
7218 | \relates QLatin1String
|
---|
7219 |
|
---|
7220 | Returns true if string \a s1 is lexically smaller than or equal to string \a s2; otherwise
|
---|
7221 | returns false.
|
---|
7222 | */
|
---|
7223 | /* \fn bool operator>(const QLatin1String &s1, const QLatin1String &s2)
|
---|
7224 | \relates QLatin1String
|
---|
7225 |
|
---|
7226 | Returns true if string \a s1 is lexically greater than string \a s2; otherwise
|
---|
7227 | returns false.
|
---|
7228 | */
|
---|
7229 | /* \fn bool operator>=(const QLatin1String &s1, const QLatin1String &s2)
|
---|
7230 | \relates QLatin1String
|
---|
7231 |
|
---|
7232 | Returns true if string \a s1 is lexically greater than or equal to
|
---|
7233 | string \a s2; otherwise returns false.
|
---|
7234 | */
|
---|
7235 |
|
---|
7236 |
|
---|
7237 | #if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE))
|
---|
7238 | /*!
|
---|
7239 | \fn QDataStream &operator<<(QDataStream &stream, const QString &string)
|
---|
7240 | \relates QString
|
---|
7241 |
|
---|
7242 | Writes the given \a string to the specified \a stream.
|
---|
7243 |
|
---|
7244 | \sa {Format of the QDataStream Operators}
|
---|
7245 | */
|
---|
7246 |
|
---|
7247 | QDataStream &operator<<(QDataStream &out, const QString &str)
|
---|
7248 | {
|
---|
7249 | if (out.version() == 1) {
|
---|
7250 | out << str.toLatin1();
|
---|
7251 | } else {
|
---|
7252 | if (!str.isNull() || out.version() < 3) {
|
---|
7253 | int byteOrder = out.byteOrder();
|
---|
7254 | const QChar* ub = str.unicode();
|
---|
7255 | static const uint auto_size = 1024;
|
---|
7256 | char t[auto_size];
|
---|
7257 | char *b;
|
---|
7258 | if (str.length()*sizeof(QChar) > auto_size) {
|
---|
7259 | b = new char[str.length()*sizeof(QChar)];
|
---|
7260 | } else {
|
---|
7261 | b = t;
|
---|
7262 | }
|
---|
7263 | int l = str.length();
|
---|
7264 | char *c=b;
|
---|
7265 | while (l--) {
|
---|
7266 | if (byteOrder == QDataStream::BigEndian) {
|
---|
7267 | *c++ = (char)ub->row();
|
---|
7268 | *c++ = (char)ub->cell();
|
---|
7269 | } else {
|
---|
7270 | *c++ = (char)ub->cell();
|
---|
7271 | *c++ = (char)ub->row();
|
---|
7272 | }
|
---|
7273 | ub++;
|
---|
7274 | }
|
---|
7275 | out.writeBytes(b, sizeof(QChar)*str.length());
|
---|
7276 | if (str.length()*sizeof(QChar) > auto_size)
|
---|
7277 | delete [] b;
|
---|
7278 | } else {
|
---|
7279 | // write null marker
|
---|
7280 | out << (quint32)0xffffffff;
|
---|
7281 | }
|
---|
7282 | }
|
---|
7283 | return out;
|
---|
7284 | }
|
---|
7285 |
|
---|
7286 | /*!
|
---|
7287 | \fn QDataStream &operator>>(QDataStream &stream, QString &string)
|
---|
7288 | \relates QString
|
---|
7289 |
|
---|
7290 | Reads a string from the specified \a stream into the given \a string.
|
---|
7291 |
|
---|
7292 | \sa {Format of the QDataStream Operators}
|
---|
7293 | */
|
---|
7294 |
|
---|
7295 | QDataStream &operator>>(QDataStream &in, QString &str)
|
---|
7296 | {
|
---|
7297 | #ifdef QT_QSTRING_UCS_4
|
---|
7298 | #if defined(Q_CC_GNU)
|
---|
7299 | #warning "operator>> not working properly"
|
---|
7300 | #endif
|
---|
7301 | #endif
|
---|
7302 |
|
---|
7303 | if (in.version() == 1) {
|
---|
7304 | QByteArray l;
|
---|
7305 | in >> l;
|
---|
7306 | str = QString::fromLatin1(l);
|
---|
7307 | } else {
|
---|
7308 | quint32 bytes = 0;
|
---|
7309 | in >> bytes; // read size of string
|
---|
7310 | if (bytes == 0xffffffff) { // null string
|
---|
7311 | str.clear();
|
---|
7312 | } else if (bytes > 0) { // not empty
|
---|
7313 | if (bytes & 0x1) {
|
---|
7314 | str.clear();
|
---|
7315 | in.setStatus(QDataStream::ReadCorruptData);
|
---|
7316 | return in;
|
---|
7317 | }
|
---|
7318 |
|
---|
7319 | const quint32 Step = 1024 * 1024;
|
---|
7320 | quint32 len = bytes / 2;
|
---|
7321 | quint32 allocated = 0;
|
---|
7322 |
|
---|
7323 | while (allocated < len) {
|
---|
7324 | int blockSize = qMin(Step, len - allocated);
|
---|
7325 | str.resize(allocated + blockSize);
|
---|
7326 | if (in.readRawData(reinterpret_cast<char *>(str.data()) + allocated * 2,
|
---|
7327 | blockSize * 2) != blockSize * 2) {
|
---|
7328 | str.clear();
|
---|
7329 | in.setStatus(QDataStream::ReadPastEnd);
|
---|
7330 | return in;
|
---|
7331 | }
|
---|
7332 | allocated += blockSize;
|
---|
7333 | }
|
---|
7334 |
|
---|
7335 | if ((in.byteOrder() == QDataStream::BigEndian)
|
---|
7336 | != (QSysInfo::ByteOrder == QSysInfo::BigEndian)) {
|
---|
7337 | ushort *data = reinterpret_cast<ushort *>(str.data());
|
---|
7338 | while (len--) {
|
---|
7339 | *data = (*data >> 8) | (*data << 8);
|
---|
7340 | ++data;
|
---|
7341 | }
|
---|
7342 | }
|
---|
7343 | } else {
|
---|
7344 | str = QLatin1String("");
|
---|
7345 | }
|
---|
7346 | }
|
---|
7347 | return in;
|
---|
7348 | }
|
---|
7349 | #endif // QT_NO_DATASTREAM
|
---|
7350 |
|
---|
7351 | /*!
|
---|
7352 | \fn void QString::setLength(int nl)
|
---|
7353 |
|
---|
7354 | Use resize() instead.
|
---|
7355 | */
|
---|
7356 |
|
---|
7357 | /*!
|
---|
7358 | \fn QString QString::copy() const
|
---|
7359 |
|
---|
7360 | Use simple assignment instead. QString is implicitly shared so if
|
---|
7361 | a copy is modified only the copy is changed.
|
---|
7362 | */
|
---|
7363 |
|
---|
7364 | /*!
|
---|
7365 | \fn QString &QString::remove(QChar c, bool cs)
|
---|
7366 |
|
---|
7367 | Use the remove(QChar, Qt::CaseSensitive) overload instead.
|
---|
7368 | */
|
---|
7369 |
|
---|
7370 | /*!
|
---|
7371 | \fn QString &QString::remove(const QString &s, bool cs)
|
---|
7372 |
|
---|
7373 | Use the remove(QString, Qt::CaseSensitive) overload instead.
|
---|
7374 | */
|
---|
7375 |
|
---|
7376 | /*!
|
---|
7377 | \fn QString &QString::replace(QChar c, const QString &after, bool cs)
|
---|
7378 |
|
---|
7379 | Use the replace(QChar, QString, Qt::CaseSensitive) overload instead.
|
---|
7380 | */
|
---|
7381 |
|
---|
7382 | /*!
|
---|
7383 | \fn QString &QString::replace(const QString &before, const QString &after, bool cs)
|
---|
7384 |
|
---|
7385 | Use the replace(QString, QString, Qt::CaseSensitive) overload instead.
|
---|
7386 | */
|
---|
7387 |
|
---|
7388 | /*!
|
---|
7389 | \fn QString &QString::replace(char c, const QString &after, bool cs)
|
---|
7390 |
|
---|
7391 | Use the replace(QChar, QString, Qt::CaseSensitive) overload instead.
|
---|
7392 | */
|
---|
7393 |
|
---|
7394 | /*!
|
---|
7395 | \fn QString &QString::replace(char c, const QString &after, Qt::CaseSensitivity cs)
|
---|
7396 |
|
---|
7397 | Use the replace(QChar, QString, Qt::CaseSensitive) overload instead.
|
---|
7398 | */
|
---|
7399 |
|
---|
7400 | /*!
|
---|
7401 | \fn int QString::find(QChar c, int i = 0, bool cs = true) const
|
---|
7402 |
|
---|
7403 | Use indexOf() instead.
|
---|
7404 | */
|
---|
7405 |
|
---|
7406 | /*!
|
---|
7407 | \fn int QString::find(const QString &s, int i = 0, bool cs = true) const
|
---|
7408 |
|
---|
7409 | Use indexOf() instead.
|
---|
7410 | */
|
---|
7411 |
|
---|
7412 | /*!
|
---|
7413 | \fn int QString::findRev(QChar c, int i = -1, bool cs = true) const
|
---|
7414 |
|
---|
7415 | Use lastIndexOf() instead.
|
---|
7416 | */
|
---|
7417 |
|
---|
7418 | /*!
|
---|
7419 | \fn int QString::findRev(const QString &s, int i = -1, bool cs = true) const
|
---|
7420 |
|
---|
7421 | Use lastIndexOf() instead.
|
---|
7422 | */
|
---|
7423 |
|
---|
7424 | /*!
|
---|
7425 | \fn int QString::find(const QRegExp &rx, int i=0) const
|
---|
7426 |
|
---|
7427 | Use indexOf() instead.
|
---|
7428 | */
|
---|
7429 |
|
---|
7430 | /*!
|
---|
7431 | \fn int QString::find(QRegExp &rx, int i=0) const
|
---|
7432 | \internal
|
---|
7433 | \since 4.5
|
---|
7434 |
|
---|
7435 | Use indexOf() instead.
|
---|
7436 | */
|
---|
7437 |
|
---|
7438 | /*!
|
---|
7439 | \fn int QString::findRev(const QRegExp &rx, int i=-1) const
|
---|
7440 |
|
---|
7441 | Use lastIndexOf() instead.
|
---|
7442 | */
|
---|
7443 |
|
---|
7444 | /*!
|
---|
7445 | \fn int QString::findRev(QRegExp &rx, int i=0) const
|
---|
7446 | \internal
|
---|
7447 | \since 4.5
|
---|
7448 |
|
---|
7449 | Use lastIndexOf() instead.
|
---|
7450 | */
|
---|
7451 |
|
---|
7452 | /*!
|
---|
7453 | \fn QBool QString::contains(QChar c, bool cs) const
|
---|
7454 |
|
---|
7455 | Use the contains(QChar, Qt::CaseSensitive) overload instead.
|
---|
7456 | */
|
---|
7457 |
|
---|
7458 | /*!
|
---|
7459 | \fn QBool QString::contains(const QString &s, bool cs) const
|
---|
7460 |
|
---|
7461 | Use the contains(QString, Qt::CaseSensitive) overload instead.
|
---|
7462 | */
|
---|
7463 |
|
---|
7464 | /*!
|
---|
7465 | \fn bool QString::startsWith(const QString &s, bool cs) const
|
---|
7466 |
|
---|
7467 | Use the startsWith(QString, Qt::CaseSensitive) overload instead.
|
---|
7468 | */
|
---|
7469 |
|
---|
7470 | /*!
|
---|
7471 | \fn bool QString::endsWith(const QString &s, bool cs) const
|
---|
7472 |
|
---|
7473 | Use the endsWith(QString, Qt::CaseSensitive) overload instead.
|
---|
7474 | */
|
---|
7475 |
|
---|
7476 | /*!
|
---|
7477 | \fn QString QString::leftJustify(int width, QChar fill = QLatin1Char(' '), bool trunc=false) const
|
---|
7478 |
|
---|
7479 | Use leftJustified() instead.
|
---|
7480 | */
|
---|
7481 |
|
---|
7482 | /*!
|
---|
7483 | \fn QString QString::rightJustify(int width, QChar fill = QLatin1Char(' '), bool trunc=false) const
|
---|
7484 |
|
---|
7485 | Use rightJustified() instead.
|
---|
7486 | */
|
---|
7487 |
|
---|
7488 | /*!
|
---|
7489 | \fn QString QString::lower() const
|
---|
7490 |
|
---|
7491 | Use toLower() instead.
|
---|
7492 | */
|
---|
7493 |
|
---|
7494 | /*!
|
---|
7495 | \fn QString QString::upper() const
|
---|
7496 |
|
---|
7497 | Use toUpper() instead.
|
---|
7498 | */
|
---|
7499 |
|
---|
7500 | /*!
|
---|
7501 | \fn QString QString::stripWhiteSpace() const
|
---|
7502 |
|
---|
7503 | Use trimmed() instead.
|
---|
7504 | */
|
---|
7505 |
|
---|
7506 | /*!
|
---|
7507 | \fn QString QString::simplifyWhiteSpace() const
|
---|
7508 |
|
---|
7509 | Use simplified() instead.
|
---|
7510 | */
|
---|
7511 |
|
---|
7512 | /*!
|
---|
7513 | \fn QString &QString::setUnicodeCodes(const ushort *unicode_as_ushorts, int size)
|
---|
7514 |
|
---|
7515 | Use setUtf16() instead.
|
---|
7516 | */
|
---|
7517 |
|
---|
7518 | /*!
|
---|
7519 | \fn ushort *QString::ucs2() const
|
---|
7520 |
|
---|
7521 | Use utf16() instead.
|
---|
7522 | */
|
---|
7523 |
|
---|
7524 | /*!
|
---|
7525 | \fn QString QString::fromUcs2(const ushort *unicode, int size = -1)
|
---|
7526 |
|
---|
7527 | Use fromUtf16() instead.
|
---|
7528 | */
|
---|
7529 |
|
---|
7530 | /*!
|
---|
7531 | \fn QString &QString::setAscii(const char *str, int len = -1)
|
---|
7532 |
|
---|
7533 | Use fromAscii() instead.
|
---|
7534 | */
|
---|
7535 |
|
---|
7536 | /*!
|
---|
7537 | \fn QString &QString::setLatin1(const char *str, int len = -1)
|
---|
7538 |
|
---|
7539 | Use fromLatin1() instead.
|
---|
7540 | */
|
---|
7541 |
|
---|
7542 | /*!
|
---|
7543 | \fn QChar QString::constref(uint i) const
|
---|
7544 |
|
---|
7545 | Use at() instead.
|
---|
7546 | */
|
---|
7547 |
|
---|
7548 | /*!
|
---|
7549 | \fn QChar &QString::ref(uint i);
|
---|
7550 |
|
---|
7551 | Use operator[]() instead.
|
---|
7552 | */
|
---|
7553 |
|
---|
7554 | /*!
|
---|
7555 | \fn QString::operator const char *() const
|
---|
7556 |
|
---|
7557 | Use toAscii().constData() instead.
|
---|
7558 | */
|
---|
7559 |
|
---|
7560 | /*!
|
---|
7561 | \class QConstString
|
---|
7562 | \brief The QConstString class is a wrapper for constant Unicode string data.
|
---|
7563 | \compat
|
---|
7564 |
|
---|
7565 | In Qt 4, QConstString is replaced by QString::fromRawData(), a
|
---|
7566 | static function that constructs a QString object based on Unicode
|
---|
7567 | string data.
|
---|
7568 |
|
---|
7569 | Because QString::fromRawData() has slightly more stringent
|
---|
7570 | constraints than QConstString had in Qt 3, the new QConstString
|
---|
7571 | class takes a deep copy of the string data.
|
---|
7572 |
|
---|
7573 | \sa QString::fromRawData()
|
---|
7574 | */
|
---|
7575 |
|
---|
7576 | /*!
|
---|
7577 | \fn QConstString::QConstString(const QChar *unicode, int size)
|
---|
7578 |
|
---|
7579 | Use QString(\a unicode, \a size) or
|
---|
7580 | QString::fromRawData(\a unicode, \a size) instead.
|
---|
7581 | */
|
---|
7582 |
|
---|
7583 | /*!
|
---|
7584 | \fn const QString &QConstString::string() const
|
---|
7585 |
|
---|
7586 | Returns \c *this. Not necessary in Qt 4.
|
---|
7587 | */
|
---|
7588 |
|
---|
7589 |
|
---|
7590 |
|
---|
7591 | /*!
|
---|
7592 | \class QStringRef
|
---|
7593 | \since 4.3
|
---|
7594 | \brief The QStringRef class provides a thin wrapper around QString substrings.
|
---|
7595 | \reentrant
|
---|
7596 | \ingroup tools
|
---|
7597 | \ingroup string-processing
|
---|
7598 |
|
---|
7599 | QStringRef provides a read-only subset of the QString API.
|
---|
7600 |
|
---|
7601 | A string reference explicitly references a portion of a string()
|
---|
7602 | with a given size(), starting at a specific position(). Calling
|
---|
7603 | toString() returns a copy of the data as a real QString instance.
|
---|
7604 |
|
---|
7605 | This class is designed to improve the performance of substring
|
---|
7606 | handling when manipulating substrings obtained from existing QString
|
---|
7607 | instances. QStringRef avoids the memory allocation and reference
|
---|
7608 | counting overhead of a standard QString by simply referencing a
|
---|
7609 | part of the original string. This can prove to be advantageous in
|
---|
7610 | low level code, such as that used in a parser, at the expense of
|
---|
7611 | potentially more complex code.
|
---|
7612 |
|
---|
7613 | For most users, there are no semantic benefits to using QStringRef
|
---|
7614 | instead of QString since QStringRef requires attention to be paid
|
---|
7615 | to memory management issues, potentially making code more complex
|
---|
7616 | to write and maintain.
|
---|
7617 |
|
---|
7618 | \warning A QStringRef is only valid as long as the referenced
|
---|
7619 | string exists. If the original string is deleted, the string
|
---|
7620 | reference points to an invalid memory location.
|
---|
7621 |
|
---|
7622 | We suggest that you only use this class in stable code where profiling
|
---|
7623 | has clearly identified that performance improvements can be made by
|
---|
7624 | replacing standard string operations with the optimized substring
|
---|
7625 | handling provided by this class.
|
---|
7626 |
|
---|
7627 | \sa {Implicitly Shared Classes}
|
---|
7628 | */
|
---|
7629 |
|
---|
7630 |
|
---|
7631 | /*!
|
---|
7632 | \fn QStringRef::QStringRef()
|
---|
7633 |
|
---|
7634 | Constructs an empty string reference.
|
---|
7635 | */
|
---|
7636 |
|
---|
7637 | /*! \fn QStringRef::QStringRef(const QString *string, int position, int length)
|
---|
7638 |
|
---|
7639 | Constructs a string reference to the range of characters in the given
|
---|
7640 | \a string specified by the starting \a position and \a length in characters.
|
---|
7641 |
|
---|
7642 | \warning This function exists to improve performance as much as possible,
|
---|
7643 | and performs no bounds checking. For program correctness, \a position and
|
---|
7644 | \a length must describe a valid substring of \a string.
|
---|
7645 |
|
---|
7646 | This means that the starting \a position must be positive or 0 and smaller
|
---|
7647 | than \a string's length, and \a length must be positive or 0 but smaller than
|
---|
7648 | the string's length minus the starting \a position;
|
---|
7649 | i.e, 0 <= position < string->length() and
|
---|
7650 | 0 <= length <= string->length() - position must both be satisfied.
|
---|
7651 | */
|
---|
7652 |
|
---|
7653 | /*! \fn QStringRef::QStringRef(const QString *string)
|
---|
7654 |
|
---|
7655 | Constructs a string reference to the given \a string.
|
---|
7656 | */
|
---|
7657 |
|
---|
7658 | /*! \fn QStringRef::QStringRef(const QStringRef &other)
|
---|
7659 |
|
---|
7660 | Constructs a copy of the \a other string reference.
|
---|
7661 | */
|
---|
7662 | /*!
|
---|
7663 | \fn QStringRef::~QStringRef()
|
---|
7664 |
|
---|
7665 | Destroys the string reference.
|
---|
7666 |
|
---|
7667 | Since this class is only used to refer to string data, and does not take
|
---|
7668 | ownership of it, no memory is freed when instances are destroyed.
|
---|
7669 | */
|
---|
7670 |
|
---|
7671 |
|
---|
7672 | /*!
|
---|
7673 | \fn int QStringRef::position() const
|
---|
7674 |
|
---|
7675 | Returns the starting position in the referenced string that is referred to
|
---|
7676 | by the string reference.
|
---|
7677 |
|
---|
7678 | \sa size(), string()
|
---|
7679 | */
|
---|
7680 |
|
---|
7681 | /*!
|
---|
7682 | \fn int QStringRef::size() const
|
---|
7683 |
|
---|
7684 | Returns the number of characters referred to by the string reference.
|
---|
7685 | Equivalent to length() and count().
|
---|
7686 |
|
---|
7687 | \sa position(), string()
|
---|
7688 | */
|
---|
7689 | /*!
|
---|
7690 | \fn int QStringRef::count() const
|
---|
7691 | Returns the number of characters referred to by the string reference.
|
---|
7692 | Equivalent to size() and length().
|
---|
7693 |
|
---|
7694 | \sa position(), string()
|
---|
7695 | */
|
---|
7696 | /*!
|
---|
7697 | \fn int QStringRef::length() const
|
---|
7698 | Returns the number of characters referred to by the string reference.
|
---|
7699 | Equivalent to size() and count().
|
---|
7700 |
|
---|
7701 | \sa position(), string()
|
---|
7702 | */
|
---|
7703 |
|
---|
7704 |
|
---|
7705 | /*!
|
---|
7706 | \fn bool QStringRef::isEmpty() const
|
---|
7707 |
|
---|
7708 | Returns true if the string reference has no characters; otherwise returns
|
---|
7709 | false.
|
---|
7710 |
|
---|
7711 | A string reference is empty if its size is zero.
|
---|
7712 |
|
---|
7713 | \sa size()
|
---|
7714 | */
|
---|
7715 |
|
---|
7716 | /*!
|
---|
7717 | \fn bool QStringRef::isNull() const
|
---|
7718 |
|
---|
7719 | Returns true if string() returns a null pointer or a pointer to a
|
---|
7720 | null string; otherwise returns true.
|
---|
7721 |
|
---|
7722 | \sa size()
|
---|
7723 | */
|
---|
7724 |
|
---|
7725 | /*!
|
---|
7726 | \fn const QString *QStringRef::string() const
|
---|
7727 |
|
---|
7728 | Returns a pointer to the string referred to by the string reference, or
|
---|
7729 | 0 if it does not reference a string.
|
---|
7730 |
|
---|
7731 | \sa unicode()
|
---|
7732 | */
|
---|
7733 |
|
---|
7734 |
|
---|
7735 | /*!
|
---|
7736 | \fn const QChar *QStringRef::unicode() const
|
---|
7737 |
|
---|
7738 | Returns a Unicode representation of the string reference. Since
|
---|
7739 | the data stems directly from the referenced string, it is not
|
---|
7740 | null-terminated unless the string reference includes the string's
|
---|
7741 | null terminator.
|
---|
7742 |
|
---|
7743 | \sa string()
|
---|
7744 | */
|
---|
7745 |
|
---|
7746 | /*!
|
---|
7747 | \fn const QChar *QStringRef::data() const
|
---|
7748 |
|
---|
7749 | Same as unicode().
|
---|
7750 | */
|
---|
7751 |
|
---|
7752 | /*!
|
---|
7753 | \fn const QChar *QStringRef::constData() const
|
---|
7754 |
|
---|
7755 | Same as unicode().
|
---|
7756 | */
|
---|
7757 |
|
---|
7758 | /*!
|
---|
7759 | Returns a copy of the string reference as a QString object.
|
---|
7760 |
|
---|
7761 | If the string reference is not a complete reference of the string
|
---|
7762 | (meaning that position() is 0 and size() equals string()->size()),
|
---|
7763 | this function will allocate a new string to return.
|
---|
7764 |
|
---|
7765 | \sa string()
|
---|
7766 | */
|
---|
7767 |
|
---|
7768 | QString QStringRef::toString() const {
|
---|
7769 | if (!m_string)
|
---|
7770 | return QString();
|
---|
7771 | if (m_size && m_position == 0 && m_size == m_string->size())
|
---|
7772 | return *m_string;
|
---|
7773 | return QString::fromUtf16(reinterpret_cast<const ushort*>(m_string->unicode() + m_position), m_size);
|
---|
7774 | }
|
---|
7775 |
|
---|
7776 |
|
---|
7777 | /*! \relates QStringRef
|
---|
7778 |
|
---|
7779 | Returns true if string reference \a s1 is lexically equal to string reference \a s2; otherwise
|
---|
7780 | returns false.
|
---|
7781 | */
|
---|
7782 | bool operator==(const QStringRef &s1,const QStringRef &s2)
|
---|
7783 | { return (s1.size() == s2.size() &&
|
---|
7784 | qMemEquals((const ushort *)s1.unicode(), (const ushort *)s2.unicode(), s1.size()));
|
---|
7785 | }
|
---|
7786 |
|
---|
7787 | /*! \relates QStringRef
|
---|
7788 |
|
---|
7789 | Returns true if string \a s1 is lexically equal to string reference \a s2; otherwise
|
---|
7790 | returns false.
|
---|
7791 | */
|
---|
7792 | bool operator==(const QString &s1,const QStringRef &s2)
|
---|
7793 | { return (s1.size() == s2.size() &&
|
---|
7794 | qMemEquals((const ushort *)s1.unicode(), (const ushort *)s2.unicode(), s1.size()));
|
---|
7795 | }
|
---|
7796 |
|
---|
7797 | /*! \relates QStringRef
|
---|
7798 |
|
---|
7799 | Returns true if string \a s1 is lexically equal to string reference \a s2; otherwise
|
---|
7800 | returns false.
|
---|
7801 | */
|
---|
7802 | bool operator==(const QLatin1String &s1, const QStringRef &s2)
|
---|
7803 | {
|
---|
7804 | const ushort *uc = reinterpret_cast<const ushort *>(s2.unicode());
|
---|
7805 | const ushort *e = uc + s2.size();
|
---|
7806 | const uchar *c = reinterpret_cast<const uchar *>(s1.latin1());
|
---|
7807 | if (!c)
|
---|
7808 | return s2.isEmpty();
|
---|
7809 |
|
---|
7810 | while (*c) {
|
---|
7811 | if (uc == e || *uc != *c)
|
---|
7812 | return false;
|
---|
7813 | ++uc;
|
---|
7814 | ++c;
|
---|
7815 | }
|
---|
7816 | return (uc == e);
|
---|
7817 | }
|
---|
7818 |
|
---|
7819 | /*!
|
---|
7820 | \relates QStringRef
|
---|
7821 |
|
---|
7822 | Returns true if string reference \a s1 is lexically less than
|
---|
7823 | string reference \a s2; otherwise returns false.
|
---|
7824 |
|
---|
7825 | The comparison is based exclusively on the numeric Unicode values
|
---|
7826 | of the characters and is very fast, but is not what a human would
|
---|
7827 | expect. Consider sorting user-interface strings using the
|
---|
7828 | QString::localeAwareCompare() function.
|
---|
7829 | */
|
---|
7830 | bool operator<(const QStringRef &s1,const QStringRef &s2)
|
---|
7831 | {
|
---|
7832 | return ucstrcmp(s1.constData(), s1.length(), s2.constData(), s2.length()) < 0;
|
---|
7833 | }
|
---|
7834 |
|
---|
7835 | /*!\fn bool operator<=(const QStringRef &s1,const QStringRef &s2)
|
---|
7836 |
|
---|
7837 | \relates QStringRef
|
---|
7838 |
|
---|
7839 | Returns true if string reference \a s1 is lexically less than
|
---|
7840 | or equal to string reference \a s2; otherwise returns false.
|
---|
7841 |
|
---|
7842 | The comparison is based exclusively on the numeric Unicode values
|
---|
7843 | of the characters and is very fast, but is not what a human would
|
---|
7844 | expect. Consider sorting user-interface strings using the
|
---|
7845 | QString::localeAwareCompare() function.
|
---|
7846 | */
|
---|
7847 |
|
---|
7848 | /*!\fn bool operator>=(const QStringRef &s1,const QStringRef &s2)
|
---|
7849 |
|
---|
7850 | \relates QStringRef
|
---|
7851 |
|
---|
7852 | Returns true if string reference \a s1 is lexically greater than
|
---|
7853 | or equal to string reference \a s2; otherwise returns false.
|
---|
7854 |
|
---|
7855 | The comparison is based exclusively on the numeric Unicode values
|
---|
7856 | of the characters and is very fast, but is not what a human would
|
---|
7857 | expect. Consider sorting user-interface strings using the
|
---|
7858 | QString::localeAwareCompare() function.
|
---|
7859 | */
|
---|
7860 |
|
---|
7861 | /*!\fn bool operator>(const QStringRef &s1,const QStringRef &s2)
|
---|
7862 |
|
---|
7863 | \relates QStringRef
|
---|
7864 |
|
---|
7865 | Returns true if string reference \a s1 is lexically greater than
|
---|
7866 | string reference \a s2; otherwise returns false.
|
---|
7867 |
|
---|
7868 | The comparison is based exclusively on the numeric Unicode values
|
---|
7869 | of the characters and is very fast, but is not what a human would
|
---|
7870 | expect. Consider sorting user-interface strings using the
|
---|
7871 | QString::localeAwareCompare() function.
|
---|
7872 | */
|
---|
7873 |
|
---|
7874 |
|
---|
7875 | /*!
|
---|
7876 | \fn const QChar QStringRef::at(int position) const
|
---|
7877 |
|
---|
7878 | Returns the character at the given index \a position in the
|
---|
7879 | string reference.
|
---|
7880 |
|
---|
7881 | The \a position must be a valid index position in the string
|
---|
7882 | (i.e., 0 <= \a position < size()).
|
---|
7883 | */
|
---|
7884 |
|
---|
7885 | /*!
|
---|
7886 | \fn void QStringRef::clear()
|
---|
7887 |
|
---|
7888 | Clears the contents of the string reference by making it null and empty.
|
---|
7889 |
|
---|
7890 | \sa isEmpty(), isNull()
|
---|
7891 | */
|
---|
7892 |
|
---|
7893 | /*!
|
---|
7894 | \fn QStringRef &QStringRef::operator=(const QStringRef &other)
|
---|
7895 |
|
---|
7896 | Assigns the \a other string reference to this string reference, and
|
---|
7897 | returns the result.
|
---|
7898 | */
|
---|
7899 |
|
---|
7900 | /*!
|
---|
7901 | \fn QStringRef &QStringRef::operator=(const QString *string)
|
---|
7902 |
|
---|
7903 | Constructs a string reference to the given \a string and assigns it to
|
---|
7904 | this string reference, returning the result.
|
---|
7905 | */
|
---|
7906 |
|
---|
7907 | /*!
|
---|
7908 | \typedef QString::DataPtr
|
---|
7909 | \internal
|
---|
7910 | */
|
---|
7911 |
|
---|
7912 | /*!
|
---|
7913 | \fn DataPtr & QString::data_ptr()
|
---|
7914 | \internal
|
---|
7915 | */
|
---|
7916 |
|
---|
7917 |
|
---|
7918 |
|
---|
7919 | /*! Appends the string reference to \a string, and returns a new
|
---|
7920 | reference to the combined string data.
|
---|
7921 | */
|
---|
7922 | QStringRef QStringRef::appendTo(QString *string) const
|
---|
7923 | {
|
---|
7924 | if (!string)
|
---|
7925 | return QStringRef();
|
---|
7926 | int pos = string->size();
|
---|
7927 | string->insert(pos, unicode(), size());
|
---|
7928 | return QStringRef(string, pos, size());
|
---|
7929 | }
|
---|
7930 |
|
---|
7931 | /*!
|
---|
7932 | \fn int QStringRef::compare(const QStringRef &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
|
---|
7933 | \since 4.5
|
---|
7934 |
|
---|
7935 | Compares the string \a s1 with the string \a s2 and returns an
|
---|
7936 | integer less than, equal to, or greater than zero if \a s1
|
---|
7937 | is less than, equal to, or greater than \a s2.
|
---|
7938 |
|
---|
7939 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
|
---|
7940 | otherwise the comparison is case insensitive.
|
---|
7941 | */
|
---|
7942 |
|
---|
7943 | /*!
|
---|
7944 | \fn int QStringRef::compare(const QStringRef &s1, const QStringRef &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
|
---|
7945 | \since 4.5
|
---|
7946 | \overload
|
---|
7947 |
|
---|
7948 | Compares the string \a s1 with the string \a s2 and returns an
|
---|
7949 | integer less than, equal to, or greater than zero if \a s1
|
---|
7950 | is less than, equal to, or greater than \a s2.
|
---|
7951 |
|
---|
7952 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
|
---|
7953 | otherwise the comparison is case insensitive.
|
---|
7954 | */
|
---|
7955 |
|
---|
7956 | /*!
|
---|
7957 | \fn int QStringRef::compare(const QStringRef &s1, QLatin1String s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
|
---|
7958 | \since 4.5
|
---|
7959 | \overload
|
---|
7960 |
|
---|
7961 | Compares the string \a s1 with the string \a s2 and returns an
|
---|
7962 | integer less than, equal to, or greater than zero if \a s1
|
---|
7963 | is less than, equal to, or greater than \a s2.
|
---|
7964 |
|
---|
7965 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
|
---|
7966 | otherwise the comparison is case insensitive.
|
---|
7967 | */
|
---|
7968 |
|
---|
7969 | /*!
|
---|
7970 | \overload
|
---|
7971 | \fn int QStringRef::compare(const QString &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
|
---|
7972 | \since 4.5
|
---|
7973 |
|
---|
7974 | Compares this string with the \a other string and returns an
|
---|
7975 | integer less than, equal to, or greater than zero if this string
|
---|
7976 | is less than, equal to, or greater than the \a other string.
|
---|
7977 |
|
---|
7978 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
|
---|
7979 | otherwise the comparison is case insensitive.
|
---|
7980 |
|
---|
7981 | Equivalent to \c {compare(*this, other, cs)}.
|
---|
7982 |
|
---|
7983 | \sa QString::compare()
|
---|
7984 | */
|
---|
7985 |
|
---|
7986 | /*!
|
---|
7987 | \overload
|
---|
7988 | \fn int QStringRef::compare(const QStringRef &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
|
---|
7989 | \since 4.5
|
---|
7990 |
|
---|
7991 | Compares this string with the \a other string and returns an
|
---|
7992 | integer less than, equal to, or greater than zero if this string
|
---|
7993 | is less than, equal to, or greater than the \a other string.
|
---|
7994 |
|
---|
7995 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
|
---|
7996 | otherwise the comparison is case insensitive.
|
---|
7997 |
|
---|
7998 | Equivalent to \c {compare(*this, other, cs)}.
|
---|
7999 |
|
---|
8000 | \sa QString::compare()
|
---|
8001 | */
|
---|
8002 |
|
---|
8003 | /*!
|
---|
8004 | \overload
|
---|
8005 | \fn int QStringRef::compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
|
---|
8006 | \since 4.5
|
---|
8007 |
|
---|
8008 | Compares this string with the \a other string and returns an
|
---|
8009 | integer less than, equal to, or greater than zero if this string
|
---|
8010 | is less than, equal to, or greater than the \a other string.
|
---|
8011 |
|
---|
8012 | If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
|
---|
8013 | otherwise the comparison is case insensitive.
|
---|
8014 |
|
---|
8015 | Equivalent to \c {compare(*this, other, cs)}.
|
---|
8016 |
|
---|
8017 | \sa QString::compare()
|
---|
8018 | */
|
---|
8019 |
|
---|
8020 | /*!
|
---|
8021 | \fn int QStringRef::localeAwareCompare(const QStringRef &s1, const QString & s2)
|
---|
8022 | \since 4.5
|
---|
8023 |
|
---|
8024 | Compares \a s1 with \a s2 and returns an integer less than, equal
|
---|
8025 | to, or greater than zero if \a s1 is less than, equal to, or
|
---|
8026 | greater than \a s2.
|
---|
8027 |
|
---|
8028 | The comparison is performed in a locale- and also
|
---|
8029 | platform-dependent manner. Use this function to present sorted
|
---|
8030 | lists of strings to the user.
|
---|
8031 |
|
---|
8032 | On Mac OS X, this function compares according the
|
---|
8033 | "Order for sorted lists" setting in the International prefereces panel.
|
---|
8034 |
|
---|
8035 | \sa compare(), QTextCodec::locale()
|
---|
8036 | */
|
---|
8037 |
|
---|
8038 | /*!
|
---|
8039 | \fn int QStringRef::localeAwareCompare(const QStringRef &s1, const QStringRef & s2)
|
---|
8040 | \since 4.5
|
---|
8041 | \overload
|
---|
8042 |
|
---|
8043 | Compares \a s1 with \a s2 and returns an integer less than, equal
|
---|
8044 | to, or greater than zero if \a s1 is less than, equal to, or
|
---|
8045 | greater than \a s2.
|
---|
8046 |
|
---|
8047 | The comparison is performed in a locale- and also
|
---|
8048 | platform-dependent manner. Use this function to present sorted
|
---|
8049 | lists of strings to the user.
|
---|
8050 |
|
---|
8051 | */
|
---|
8052 |
|
---|
8053 | /*!
|
---|
8054 | \fn int QStringRef::localeAwareCompare(const QString &other) const
|
---|
8055 | \since 4.5
|
---|
8056 | \overload
|
---|
8057 |
|
---|
8058 | Compares this string with the \a other string and returns an
|
---|
8059 | integer less than, equal to, or greater than zero if this string
|
---|
8060 | is less than, equal to, or greater than the \a other string.
|
---|
8061 |
|
---|
8062 | The comparison is performed in a locale- and also
|
---|
8063 | platform-dependent manner. Use this function to present sorted
|
---|
8064 | lists of strings to the user.
|
---|
8065 | */
|
---|
8066 |
|
---|
8067 | /*!
|
---|
8068 | \fn int QStringRef::localeAwareCompare(const QStringRef &other) const
|
---|
8069 | \since 4.5
|
---|
8070 | \overload
|
---|
8071 |
|
---|
8072 | Compares this string with the \a other string and returns an
|
---|
8073 | integer less than, equal to, or greater than zero if this string
|
---|
8074 | is less than, equal to, or greater than the \a other string.
|
---|
8075 |
|
---|
8076 | The comparison is performed in a locale- and also
|
---|
8077 | platform-dependent manner. Use this function to present sorted
|
---|
8078 | lists of strings to the user.
|
---|
8079 | */
|
---|
8080 |
|
---|
8081 | /*!
|
---|
8082 | \fn QString &QString::append(const QStringRef &reference)
|
---|
8083 | \since 4.4
|
---|
8084 |
|
---|
8085 | Appends the given string \a reference to this string and returns the result.
|
---|
8086 | */
|
---|
8087 | QString &QString::append(const QStringRef &str)
|
---|
8088 | {
|
---|
8089 | if (str.string() == this) {
|
---|
8090 | str.appendTo(this);
|
---|
8091 | } else if (str.string()) {
|
---|
8092 | int oldSize = size();
|
---|
8093 | resize(oldSize + str.size());
|
---|
8094 | memcpy(data() + oldSize, str.unicode(), str.size() * sizeof(QChar));
|
---|
8095 | }
|
---|
8096 | return *this;
|
---|
8097 | }
|
---|
8098 |
|
---|
8099 | /*!
|
---|
8100 | \since 4.4
|
---|
8101 |
|
---|
8102 | Returns a substring reference to the \a n leftmost characters
|
---|
8103 | of the string.
|
---|
8104 |
|
---|
8105 | If \a n is greater than size() or less than zero, a reference to the entire
|
---|
8106 | string is returned.
|
---|
8107 |
|
---|
8108 | \snippet doc/src/snippets/qstring/main.cpp leftRef
|
---|
8109 |
|
---|
8110 | \sa left(), rightRef(), midRef(), startsWith()
|
---|
8111 | */
|
---|
8112 | QStringRef QString::leftRef(int n) const
|
---|
8113 | {
|
---|
8114 | if (n >= d->size || n < 0)
|
---|
8115 | n = d->size;
|
---|
8116 | return QStringRef(this, 0, n);
|
---|
8117 | }
|
---|
8118 |
|
---|
8119 | /*!
|
---|
8120 | \since 4.4
|
---|
8121 |
|
---|
8122 | Returns a substring reference to the \a n rightmost characters
|
---|
8123 | of the string.
|
---|
8124 |
|
---|
8125 | If \a n is greater than size() or less than zero, a reference to the entire
|
---|
8126 | string is returned.
|
---|
8127 |
|
---|
8128 | \snippet doc/src/snippets/qstring/main.cpp rightRef
|
---|
8129 |
|
---|
8130 | \sa right(), leftRef(), midRef(), endsWith()
|
---|
8131 | */
|
---|
8132 | QStringRef QString::rightRef(int n) const
|
---|
8133 | {
|
---|
8134 | if (n >= d->size || n < 0)
|
---|
8135 | n = d->size;
|
---|
8136 | return QStringRef(this, d->size - n, n);
|
---|
8137 | }
|
---|
8138 |
|
---|
8139 | /*!
|
---|
8140 | \since 4.4
|
---|
8141 |
|
---|
8142 | Returns a substring reference to \a n characters of this string,
|
---|
8143 | starting at the specified \a position.
|
---|
8144 |
|
---|
8145 | If the \a position exceeds the length of the string, an empty
|
---|
8146 | reference is returned.
|
---|
8147 |
|
---|
8148 | If there are less than \a n characters available in the string,
|
---|
8149 | starting at the given \a position, or if \a n is -1 (default), the
|
---|
8150 | function returns all characters from the specified \a position
|
---|
8151 | onwards.
|
---|
8152 |
|
---|
8153 | Example:
|
---|
8154 |
|
---|
8155 | \snippet doc/src/snippets/qstring/main.cpp midRef
|
---|
8156 |
|
---|
8157 | \sa mid(), leftRef(), rightRef()
|
---|
8158 | */
|
---|
8159 |
|
---|
8160 | QStringRef QString::midRef(int position, int n) const
|
---|
8161 | {
|
---|
8162 | if (d == &shared_null || position >= d->size)
|
---|
8163 | return QStringRef();
|
---|
8164 | if (n < 0)
|
---|
8165 | n = d->size - position;
|
---|
8166 | if (position < 0) {
|
---|
8167 | n += position;
|
---|
8168 | position = 0;
|
---|
8169 | }
|
---|
8170 | if (n + position > d->size)
|
---|
8171 | n = d->size - position;
|
---|
8172 | return QStringRef(this, position, n);
|
---|
8173 | }
|
---|
8174 |
|
---|
8175 | QT_END_NAMESPACE
|
---|