tools/qstring.cpp

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

Generated by Squish Coco Non-Commercial