author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 33 | 3e2da88830cd |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtCore module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qstringlist.h" |
|
43 |
#include "qregexp.h" |
|
44 |
#include "qunicodetables_p.h" |
|
45 |
#ifndef QT_NO_TEXTCODEC |
|
46 |
#include <qtextcodec.h> |
|
47 |
#endif |
|
48 |
#include <private/qutfcodec_p.h> |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
49 |
#include "qsimd_p.h" |
0 | 50 |
#include <qdatastream.h> |
51 |
#include <qlist.h> |
|
52 |
#include "qlocale.h" |
|
53 |
#include "qlocale_p.h" |
|
54 |
#include "qstringmatcher.h" |
|
55 |
#include "qvarlengtharray.h" |
|
56 |
#include "qtools_p.h" |
|
57 |
#include "qhash.h" |
|
58 |
#include "qdebug.h" |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
59 |
#include "qendian.h" |
0 | 60 |
|
61 |
#ifdef Q_OS_MAC |
|
62 |
#include <private/qcore_mac_p.h> |
|
63 |
#endif |
|
64 |
||
65 |
#include <private/qfunctions_p.h> |
|
66 |
||
67 |
#if defined(Q_OS_WINCE) |
|
68 |
#include <windows.h> |
|
69 |
#include <winnls.h> |
|
70 |
#endif |
|
71 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
72 |
#ifdef Q_OS_SYMBIAN |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
73 |
#include <e32cmn.h> |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
74 |
#endif |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
75 |
|
0 | 76 |
#include <limits.h> |
77 |
#include <string.h> |
|
78 |
#include <stdlib.h> |
|
79 |
#include <stdio.h> |
|
80 |
#include <stdarg.h> |
|
81 |
||
82 |
#ifdef truncate |
|
83 |
#undef truncate |
|
84 |
#endif |
|
85 |
||
86 |
#include "qchar.cpp" |
|
87 |
#include "qstringmatcher.cpp" |
|
88 |
||
89 |
#ifndef LLONG_MAX |
|
90 |
#define LLONG_MAX qint64_C(9223372036854775807) |
|
91 |
#endif |
|
92 |
#ifndef LLONG_MIN |
|
93 |
#define LLONG_MIN (-LLONG_MAX - qint64_C(1)) |
|
94 |
#endif |
|
95 |
#ifndef ULLONG_MAX |
|
96 |
#define ULLONG_MAX quint64_C(18446744073709551615) |
|
97 |
#endif |
|
98 |
||
99 |
QT_BEGIN_NAMESPACE |
|
100 |
||
101 |
#ifndef QT_NO_TEXTCODEC |
|
102 |
QTextCodec *QString::codecForCStrings; |
|
103 |
#endif |
|
104 |
||
105 |
#ifdef QT3_SUPPORT |
|
106 |
static QHash<void *, QByteArray> *asciiCache = 0; |
|
107 |
#endif |
|
108 |
||
109 |
// internal |
|
110 |
int qFindString(const QChar *haystack, int haystackLen, int from, |
|
111 |
const QChar *needle, int needleLen, Qt::CaseSensitivity cs); |
|
112 |
int qFindStringBoyerMoore(const QChar *haystack, int haystackLen, int from, |
|
113 |
const QChar *needle, int needleLen, Qt::CaseSensitivity cs); |
|
114 |
||
115 |
||
116 |
// Unicode case-insensitive comparison |
|
117 |
static int ucstricmp(const ushort *a, const ushort *ae, const ushort *b, const ushort *be) |
|
118 |
{ |
|
119 |
if (a == b) |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
120 |
return (ae - be); |
0 | 121 |
if (a == 0) |
122 |
return 1; |
|
123 |
if (b == 0) |
|
124 |
return -1; |
|
125 |
||
126 |
const ushort *e = ae; |
|
127 |
if (be - b < ae - a) |
|
128 |
e = a + (be - b); |
|
129 |
||
130 |
uint alast = 0; |
|
131 |
uint blast = 0; |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
132 |
while (a < e) { |
0 | 133 |
// qDebug() << hex << alast << blast; |
134 |
// qDebug() << hex << "*a=" << *a << "alast=" << alast << "folded=" << foldCase (*a, alast); |
|
135 |
// qDebug() << hex << "*b=" << *b << "blast=" << blast << "folded=" << foldCase (*b, blast); |
|
136 |
int diff = foldCase(*a, alast) - foldCase(*b, blast); |
|
137 |
if ((diff)) |
|
138 |
return diff; |
|
139 |
++a; |
|
140 |
++b; |
|
141 |
} |
|
142 |
if (a == ae) { |
|
143 |
if (b == be) |
|
144 |
return 0; |
|
145 |
return -1; |
|
146 |
} |
|
147 |
return 1; |
|
148 |
} |
|
149 |
||
150 |
// Case-insensitive comparison between a Unicode string and a QLatin1String |
|
151 |
static int ucstricmp(const ushort *a, const ushort *ae, const uchar *b) |
|
152 |
{ |
|
153 |
if (a == 0) { |
|
154 |
if (b == 0) |
|
155 |
return 0; |
|
156 |
return 1; |
|
157 |
} |
|
158 |
if (b == 0) |
|
159 |
return -1; |
|
160 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
161 |
while (a < ae && *b) { |
0 | 162 |
int diff = foldCase(*a) - foldCase(*b); |
163 |
if ((diff)) |
|
164 |
return diff; |
|
165 |
++a; |
|
166 |
++b; |
|
167 |
} |
|
168 |
if (a == ae) { |
|
169 |
if (!*b) |
|
170 |
return 0; |
|
171 |
return -1; |
|
172 |
} |
|
173 |
return 1; |
|
174 |
} |
|
175 |
||
176 |
// Unicode case-sensitive compare two same-sized strings |
|
177 |
static int ucstrncmp(const QChar *a, const QChar *b, int l) |
|
178 |
{ |
|
179 |
while (l-- && *a == *b) |
|
180 |
a++,b++; |
|
181 |
if (l==-1) |
|
182 |
return 0; |
|
183 |
return a->unicode() - b->unicode(); |
|
184 |
} |
|
185 |
||
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
186 |
// Unicode case-sensitive comparison |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
187 |
static int ucstrcmp(const QChar *a, int alen, const QChar *b, int blen) |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
188 |
{ |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
189 |
if (a == b && alen == blen) |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
190 |
return 0; |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
191 |
int l = qMin(alen, blen); |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
192 |
int cmp = ucstrncmp(a, b, l); |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
193 |
return cmp ? cmp : (alen-blen); |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
194 |
} |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
195 |
|
0 | 196 |
// Unicode case-insensitive compare two same-sized strings |
197 |
static int ucstrnicmp(const ushort *a, const ushort *b, int l) |
|
198 |
{ |
|
199 |
return ucstricmp(a, a + l, b, b + l); |
|
200 |
} |
|
201 |
||
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
202 |
// Benchmarking indicates that doing memcmp is much slower than |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
203 |
// executing the comparison ourselves. |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
204 |
// |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
205 |
// The profiling was done on a population of calls to qMemEquals, generated |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
206 |
// during a run of the demo browser. The profile of the data (32-bit x86 |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
207 |
// Linux) was: |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
208 |
// |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
209 |
// total number of comparisons: 21353 |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
210 |
// longest string compared: 95 |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
211 |
// average comparison length: 14.8786 |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
212 |
// cache-line crosses: 5661 (13.3%) |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
213 |
// alignment histogram: |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
214 |
// 0xXXX0 = 512 (1.2%) strings, 0 (0.0%) of which same-aligned |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
215 |
// 0xXXX2 = 15087 (35.3%) strings, 5145 (34.1%) of which same-aligned |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
216 |
// 0xXXX4 = 525 (1.2%) strings, 0 (0.0%) of which same-aligned |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
217 |
// 0xXXX6 = 557 (1.3%) strings, 6 (1.1%) of which same-aligned |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
218 |
// 0xXXX8 = 509 (1.2%) strings, 0 (0.0%) of which same-aligned |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
219 |
// 0xXXXa = 24358 (57.0%) strings, 9901 (40.6%) of which same-aligned |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
220 |
// 0xXXXc = 557 (1.3%) strings, 0 (0.0%) of which same-aligned |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
221 |
// 0xXXXe = 601 (1.4%) strings, 15 (2.5%) of which same-aligned |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
222 |
// total = 42706 (100%) strings, 15067 (35.3%) of which same-aligned |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
223 |
// |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
224 |
// 92% of the strings have alignment of 2 or 10, which is due to malloc on |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
225 |
// 32-bit Linux returning values aligned to 8 bytes, and offsetof(array, QString::Data) == 18. |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
226 |
// |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
227 |
// The profile on 64-bit will be different since offsetof(array, QString::Data) == 26. |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
228 |
// |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
229 |
// The benchmark results were, for a Core-i7 @ 2.67 GHz 32-bit, compiled with -O3 -funroll-loops: |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
230 |
// 16-bit loads only: 872,301 CPU ticks [Qt 4.5 / memcmp] |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
231 |
// 32- and 16-bit loads: 773,362 CPU ticks [Qt 4.6] |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
232 |
// SSE2 "movdqu" 128-bit loads: 618,736 CPU ticks |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
233 |
// SSE3 "lddqu" 128-bit loads: 619,954 CPU ticks |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
234 |
// SSSE3 "palignr" corrections: 852,147 CPU ticks |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
235 |
// SSE4.2 "pcmpestrm": 738,702 CPU ticks |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
236 |
// |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
237 |
// The same benchmark on an Atom N450 @ 1.66 GHz, is: |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
238 |
// 16-bit loads only: 2,185,882 CPU ticks |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
239 |
// 32- and 16-bit loads: 1,805,060 CPU ticks |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
240 |
// SSE2 "movdqu" 128-bit loads: 2,529,843 CPU ticks |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
241 |
// SSE3 "lddqu" 128-bit loads: 2,514,858 CPU ticks |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
242 |
// SSSE3 "palignr" corrections: 2,160,325 CPU ticks |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
243 |
// SSE4.2 not available |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
244 |
// |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
245 |
// The conclusion we reach is that alignment the SSE2 unaligned code can gain |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
246 |
// 20% improvement in performance in some systems, but suffers a penalty due |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
247 |
// to the unaligned loads on others. |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
248 |
|
0 | 249 |
static bool qMemEquals(const quint16 *a, const quint16 *b, int length) |
250 |
{ |
|
251 |
if (a == b || !length) |
|
252 |
return true; |
|
253 |
||
254 |
register union { |
|
255 |
const quint16 *w; |
|
256 |
const quint32 *d; |
|
257 |
quintptr value; |
|
258 |
} sa, sb; |
|
259 |
sa.w = a; |
|
260 |
sb.w = b; |
|
261 |
||
262 |
// check alignment |
|
263 |
if ((sa.value & 2) == (sb.value & 2)) { |
|
264 |
// both addresses have the same alignment |
|
265 |
if (sa.value & 2) { |
|
266 |
// both addresses are not aligned to 4-bytes boundaries |
|
267 |
// compare the first character |
|
268 |
if (*sa.w != *sb.w) |
|
269 |
return false; |
|
270 |
--length; |
|
271 |
++sa.w; |
|
272 |
++sb.w; |
|
273 |
||
274 |
// now both addresses are 4-bytes aligned |
|
275 |
} |
|
276 |
||
277 |
// both addresses are 4-bytes aligned |
|
278 |
// do a fast 32-bit comparison |
|
279 |
register const quint32 *e = sa.d + (length >> 1); |
|
280 |
for ( ; sa.d != e; ++sa.d, ++sb.d) { |
|
281 |
if (*sa.d != *sb.d) |
|
282 |
return false; |
|
283 |
} |
|
284 |
||
285 |
// do we have a tail? |
|
286 |
return (length & 1) ? *sa.w == *sb.w : true; |
|
287 |
} else { |
|
288 |
// one of the addresses isn't 4-byte aligned but the other is |
|
289 |
register const quint16 *e = sa.w + length; |
|
290 |
for ( ; sa.w != e; ++sa.w, ++sb.w) { |
|
291 |
if (*sa.w != *sb.w) |
|
292 |
return false; |
|
293 |
} |
|
294 |
} |
|
295 |
return true; |
|
296 |
} |
|
297 |
||
298 |
/*! |
|
299 |
\internal |
|
300 |
||
301 |
Returns the index position of the first occurrence of the |
|
302 |
character \a ch in the string given by \a str and \a len, |
|
303 |
searching forward from index |
|
304 |
position \a from. Returns -1 if \a ch could not be found. |
|
305 |
*/ |
|
306 |
static int findChar(const QChar *str, int len, QChar ch, int from, |
|
307 |
Qt::CaseSensitivity cs) |
|
308 |
{ |
|
309 |
const ushort *s = (const ushort *)str; |
|
310 |
ushort c = ch.unicode(); |
|
311 |
if (from < 0) |
|
312 |
from = qMax(from + len, 0); |
|
313 |
if (from < len) { |
|
314 |
const ushort *n = s + from - 1; |
|
315 |
const ushort *e = s + len; |
|
316 |
if (cs == Qt::CaseSensitive) { |
|
317 |
while (++n != e) |
|
318 |
if (*n == c) |
|
319 |
return n - s; |
|
320 |
} else { |
|
321 |
c = foldCase(c); |
|
322 |
while (++n != e) |
|
323 |
if (foldCase(*n) == c) |
|
324 |
return n - s; |
|
325 |
} |
|
326 |
} |
|
327 |
return -1; |
|
328 |
} |
|
329 |
||
330 |
#define REHASH(a) \ |
|
331 |
if (sl_minus_1 < (int)sizeof(int) * CHAR_BIT) \ |
|
332 |
hashHaystack -= (a) << sl_minus_1; \ |
|
333 |
hashHaystack <<= 1 |
|
334 |
||
335 |
inline bool qIsUpper(char ch) |
|
336 |
{ |
|
337 |
return ch >= 'A' && ch <= 'Z'; |
|
338 |
} |
|
339 |
||
340 |
inline bool qIsDigit(char ch) |
|
341 |
{ |
|
342 |
return ch >= '0' && ch <= '9'; |
|
343 |
} |
|
344 |
||
345 |
inline char qToLower(char ch) |
|
346 |
{ |
|
347 |
if (ch >= 'A' && ch <= 'Z') |
|
348 |
return ch - 'A' + 'a'; |
|
349 |
else |
|
350 |
return ch; |
|
351 |
} |
|
352 |
||
353 |
#if defined(Q_CC_MSVC) && _MSC_VER <= 1300 |
|
354 |
const QString::Null QString::null; |
|
355 |
#else |
|
356 |
const QString::Null QString::null = { }; |
|
357 |
#endif |
|
358 |
||
359 |
/*! |
|
360 |
\macro QT_NO_CAST_FROM_ASCII |
|
361 |
\relates QString |
|
362 |
||
363 |
Disables automatic conversions from 8-bit strings (char *) to unicode QStrings |
|
364 |
||
365 |
\sa QT_NO_CAST_TO_ASCII, QT_NO_CAST_FROM_BYTEARRAY |
|
366 |
*/ |
|
367 |
||
368 |
/*! |
|
369 |
\macro QT_NO_CAST_TO_ASCII |
|
370 |
\relates QString |
|
371 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
372 |
disables automatic conversion from QString to 8-bit strings (char *) |
0 | 373 |
|
374 |
\sa QT_NO_CAST_FROM_ASCII, QT_NO_CAST_FROM_BYTEARRAY |
|
375 |
*/ |
|
376 |
||
377 |
/*! |
|
378 |
\macro QT_ASCII_CAST_WARNINGS |
|
379 |
\internal |
|
380 |
\relates QString |
|
381 |
||
382 |
This macro can be defined to force a warning whenever a function is |
|
383 |
called that automatically converts between unicode and 8-bit encodings. |
|
384 |
||
385 |
Note: This only works for compilers that support warnings for |
|
386 |
deprecated API. |
|
387 |
||
388 |
\sa QT_NO_CAST_TO_ASCII, QT_NO_CAST_FROM_ASCII |
|
389 |
*/ |
|
390 |
||
391 |
/*! |
|
392 |
\class QCharRef |
|
393 |
\reentrant |
|
394 |
\brief The QCharRef class is a helper class for QString. |
|
395 |
||
396 |
\internal |
|
397 |
||
398 |
\ingroup string-processing |
|
399 |
||
400 |
When you get an object of type QCharRef, if you can assign to it, |
|
401 |
the assignment will apply to the character in the string from |
|
402 |
which you got the reference. That is its whole purpose in life. |
|
403 |
The QCharRef becomes invalid once modifications are made to the |
|
404 |
string: if you want to keep the character, copy it into a QChar. |
|
405 |
||
406 |
Most of the QChar member functions also exist in QCharRef. |
|
407 |
However, they are not explicitly documented here. |
|
408 |
||
409 |
\sa QString::operator[]() QString::at() QChar |
|
410 |
*/ |
|
411 |
||
412 |
/*! |
|
413 |
\class QString |
|
414 |
\reentrant |
|
415 |
||
416 |
\brief The QString class provides a Unicode character string. |
|
417 |
||
418 |
\ingroup tools |
|
419 |
\ingroup shared |
|
420 |
\ingroup string-processing |
|
421 |
||
422 |
||
423 |
QString stores a string of 16-bit \l{QChar}s, where each QChar |
|
424 |
corresponds one Unicode 4.0 character. (Unicode characters |
|
425 |
with code values above 65535 are stored using surrogate pairs, |
|
426 |
i.e., two consecutive \l{QChar}s.) |
|
427 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
428 |
\l{Unicode} is an international standard that supports most of the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
429 |
writing systems in use today. It is a superset of US-ASCII (ANSI |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
430 |
X3.4-1986) and Latin-1 (ISO 8859-1), and all the US-ASCII/Latin-1 |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
431 |
characters are available at the same code positions. |
0 | 432 |
|
433 |
Behind the scenes, QString uses \l{implicit sharing} |
|
434 |
(copy-on-write) to reduce memory usage and to avoid the needless |
|
435 |
copying of data. This also helps reduce the inherent overhead of |
|
436 |
storing 16-bit characters instead of 8-bit characters. |
|
437 |
||
438 |
In addition to QString, Qt also provides the QByteArray class to |
|
439 |
store raw bytes and traditional 8-bit '\\0'-terminated strings. |
|
440 |
For most purposes, QString is the class you want to use. It is |
|
441 |
used throughout the Qt API, and the Unicode support ensures that |
|
442 |
your applications will be easy to translate if you want to expand |
|
443 |
your application's market at some point. The two main cases where |
|
444 |
QByteArray is appropriate are when you need to store raw binary |
|
445 |
data, and when memory conservation is critical (e.g., with |
|
446 |
\l{Qt for Embedded Linux}). |
|
447 |
||
448 |
\tableofcontents |
|
449 |
||
450 |
\section1 Initializing a String |
|
451 |
||
452 |
One way to initialize a QString is simply to pass a \c{const char |
|
453 |
*} to its constructor. For example, the following code creates a |
|
454 |
QString of size 5 containing the data "Hello": |
|
455 |
||
456 |
\snippet doc/src/snippets/qstring/main.cpp 0 |
|
457 |
||
458 |
QString converts the \c{const char *} data into Unicode using the |
|
459 |
fromAscii() function. By default, fromAscii() treats character |
|
460 |
above 128 as Latin-1 characters, but this can be changed by |
|
461 |
calling QTextCodec::setCodecForCStrings(). |
|
462 |
||
463 |
In all of the QString functions that take \c{const char *} |
|
464 |
parameters, the \c{const char *} is interpreted as a classic |
|
465 |
C-style '\\0'-terminated string. It is legal for the \c{const char |
|
466 |
*} parameter to be 0. |
|
467 |
||
468 |
You can also provide string data as an array of \l{QChar}s: |
|
469 |
||
470 |
\snippet doc/src/snippets/qstring/main.cpp 1 |
|
471 |
||
472 |
QString makes a deep copy of the QChar data, so you can modify it |
|
473 |
later without experiencing side effects. (If for performance |
|
474 |
reasons you don't want to take a deep copy of the character data, |
|
475 |
use QString::fromRawData() instead.) |
|
476 |
||
477 |
Another approach is to set the size of the string using resize() |
|
478 |
and to initialize the data character per character. QString uses |
|
479 |
0-based indexes, just like C++ arrays. To access the character at |
|
480 |
a particular index position, you can use \l operator[](). On |
|
481 |
non-const strings, \l operator[]() returns a reference to a |
|
482 |
character that can be used on the left side of an assignment. For |
|
483 |
example: |
|
484 |
||
485 |
\snippet doc/src/snippets/qstring/main.cpp 2 |
|
486 |
||
487 |
For read-only access, an alternative syntax is to use the at() |
|
488 |
function: |
|
489 |
||
490 |
\snippet doc/src/snippets/qstring/main.cpp 3 |
|
491 |
||
492 |
The at() function can be faster than \l operator[](), because it |
|
493 |
never causes a \l{deep copy} to occur. Alternatively, use the |
|
494 |
left(), right(), or mid() functions to extract several characters |
|
495 |
at a time. |
|
496 |
||
497 |
A QString can embed '\\0' characters (QChar::Null). The size() |
|
498 |
function always returns the size of the whole string, including |
|
499 |
embedded '\\0' characters. |
|
500 |
||
501 |
After a call to the resize() function, newly allocated characters |
|
502 |
have undefined values. To set all the characters in the string to |
|
503 |
a particular value, use the fill() function. |
|
504 |
||
505 |
QString provides dozens of overloads designed to simplify string |
|
506 |
usage. For example, if you want to compare a QString with a string |
|
507 |
literal, you can write code like this and it will work as expected: |
|
508 |
||
509 |
\snippet doc/src/snippets/qstring/main.cpp 4 |
|
510 |
||
511 |
You can also pass string literals to functions that take QStrings |
|
512 |
as arguments, invoking the QString(const char *) |
|
513 |
constructor. Similarly, you can pass a QString to a function that |
|
514 |
takes a \c{const char *} argument using the \l qPrintable() macro |
|
515 |
which returns the given QString as a \c{const char *}. This is |
|
516 |
equivalent to calling <QString>.toLocal8Bit().constData(). |
|
517 |
||
518 |
\section1 Manipulating String Data |
|
519 |
||
520 |
QString provides the following basic functions for modifying the |
|
521 |
character data: append(), prepend(), insert(), replace(), and |
|
522 |
remove(). For example: |
|
523 |
||
524 |
\snippet doc/src/snippets/qstring/main.cpp 5 |
|
525 |
||
526 |
If you are building a QString gradually and know in advance |
|
527 |
approximately how many characters the QString will contain, you |
|
528 |
can call reserve(), asking QString to preallocate a certain amount |
|
529 |
of memory. You can also call capacity() to find out how much |
|
530 |
memory QString actually allocated. |
|
531 |
||
532 |
The replace() and remove() functions' first two arguments are the |
|
533 |
position from which to start erasing and the number of characters |
|
534 |
that should be erased. If you want to replace all occurrences of |
|
535 |
a particular substring with another, use one of the two-parameter |
|
536 |
replace() overloads. |
|
537 |
||
538 |
A frequent requirement is to remove whitespace characters from a |
|
539 |
string ('\\n', '\\t', ' ', etc.). If you want to remove whitespace |
|
540 |
from both ends of a QString, use the trimmed() function. If you |
|
541 |
want to remove whitespace from both ends and replace multiple |
|
542 |
consecutive whitespaces with a single space character within the |
|
543 |
string, use simplified(). |
|
544 |
||
545 |
If you want to find all occurrences of a particular character or |
|
546 |
substring in a QString, use the indexOf() or lastIndexOf() |
|
547 |
functions. The former searches forward starting from a given index |
|
548 |
position, the latter searches backward. Both return the index |
|
549 |
position of the character or substring if they find it; otherwise, |
|
550 |
they return -1. For example, here's a typical loop that finds all |
|
551 |
occurrences of a particular substring: |
|
552 |
||
553 |
\snippet doc/src/snippets/qstring/main.cpp 6 |
|
554 |
||
555 |
QString provides many functions for converting numbers into |
|
556 |
strings and strings into numbers. See the arg() functions, the |
|
557 |
setNum() functions, the number() static functions, and the |
|
558 |
toInt(), toDouble(), and similar functions. |
|
559 |
||
560 |
To get an upper- or lowercase version of a string use toUpper() or |
|
561 |
toLower(). |
|
562 |
||
563 |
Lists of strings are handled by the QStringList class. You can |
|
564 |
split a string into a list of strings using the split() function, |
|
565 |
and join a list of strings into a single string with an optional |
|
566 |
separator using QStringList::join(). You can obtain a list of |
|
567 |
strings from a string list that contain a particular substring or |
|
568 |
that match a particular QRegExp using the QStringList::find() |
|
569 |
function. |
|
570 |
: |
|
571 |
\section1 Querying String Data |
|
572 |
||
573 |
If you want to see if a QString starts or ends with a particular |
|
574 |
substring use startsWith() or endsWith(). If you simply want to |
|
575 |
check whether a QString contains a particular character or |
|
576 |
substring, use the contains() function. If you want to find out |
|
577 |
how many times a particular character or substring occurs in the |
|
578 |
string, use count(). |
|
579 |
||
580 |
QStrings can be compared using overloaded operators such as \l |
|
581 |
operator<(), \l operator<=(), \l operator==(), \l operator>=(), |
|
582 |
and so on. Note that the comparison is based exclusively on the |
|
583 |
numeric Unicode values of the characters. It is very fast, but is |
|
584 |
not what a human would expect; the QString::localeAwareCompare() |
|
585 |
function is a better choice for sorting user-interface strings. |
|
586 |
||
587 |
To obtain a pointer to the actual character data, call data() or |
|
588 |
constData(). These functions return a pointer to the beginning of |
|
589 |
the QChar data. The pointer is guaranteed to remain valid until a |
|
590 |
non-const function is called on the QString. |
|
591 |
||
592 |
\section1 Converting Between 8-Bit Strings and Unicode Strings |
|
593 |
||
594 |
QString provides the following four functions that return a |
|
595 |
\c{const char *} version of the string as QByteArray: toAscii(), |
|
596 |
toLatin1(), toUtf8(), and toLocal8Bit(). |
|
597 |
||
598 |
\list |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
599 |
\o toAscii() returns an 8-bit string encoded using the codec |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
600 |
specified by QTextCodec::codecForCStrings (by default, that is |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
601 |
Latin 1). |
0 | 602 |
\o toLatin1() returns a Latin-1 (ISO 8859-1) encoded 8-bit string. |
603 |
\o toUtf8() returns a UTF-8 encoded 8-bit string. UTF-8 is a |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
604 |
superset of US-ASCII (ANSI X3.4-1986) that supports the entire |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
605 |
Unicode character set through multibyte sequences. |
0 | 606 |
\o toLocal8Bit() returns an 8-bit string using the system's local |
607 |
encoding. |
|
608 |
\endlist |
|
609 |
||
610 |
To convert from one of these encodings, QString provides |
|
611 |
fromAscii(), fromLatin1(), fromUtf8(), and fromLocal8Bit(). Other |
|
612 |
encodings are supported through the QTextCodec class. |
|
613 |
||
614 |
As mentioned above, QString provides a lot of functions and |
|
615 |
operators that make it easy to interoperate with \c{const char *} |
|
616 |
strings. But this functionality is a double-edged sword: It makes |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
617 |
QString more convenient to use if all strings are US-ASCII or |
0 | 618 |
Latin-1, but there is always the risk that an implicit conversion |
619 |
from or to \c{const char *} is done using the wrong 8-bit |
|
620 |
encoding. To minimize these risks, you can turn off these implicit |
|
621 |
conversions by defining the following two preprocessor symbols: |
|
622 |
||
623 |
\list |
|
624 |
\o \c QT_NO_CAST_FROM_ASCII disables automatic conversions from |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
625 |
C string literals and pointers to Unicode. |
0 | 626 |
\o \c QT_NO_CAST_TO_ASCII disables automatic conversion from QString |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
627 |
to C strings. |
0 | 628 |
\endlist |
629 |
||
630 |
One way to define these preprocessor symbols globally for your |
|
631 |
application is to add the following entry to your |
|
632 |
\l{qmake Project Files}{qmake project file}: |
|
633 |
||
634 |
\snippet doc/src/snippets/code/src_corelib_tools_qstring.cpp 0 |
|
635 |
||
636 |
You then need to explicitly call fromAscii(), fromLatin1(), |
|
637 |
fromUtf8(), or fromLocal8Bit() to construct a QString from an |
|
638 |
8-bit string, or use the lightweight QLatin1String class, for |
|
639 |
example: |
|
640 |
||
641 |
\snippet doc/src/snippets/code/src_corelib_tools_qstring.cpp 1 |
|
642 |
||
643 |
Similarly, you must call toAscii(), toLatin1(), toUtf8(), or |
|
644 |
toLocal8Bit() explicitly to convert the QString to an 8-bit |
|
645 |
string. (Other encodings are supported through the QTextCodec |
|
646 |
class.) |
|
647 |
||
648 |
\table 100 % |
|
649 |
\row |
|
650 |
\o |
|
651 |
\section1 Note for C Programmers |
|
652 |
||
653 |
Due to C++'s type system and the fact that QString is |
|
654 |
\l{implicitly shared}, QStrings may be treated like \c{int}s or |
|
655 |
other basic types. For example: |
|
656 |
||
657 |
\snippet doc/src/snippets/qstring/main.cpp 7 |
|
658 |
||
659 |
The \c result variable, is a normal variable allocated on the |
|
660 |
stack. When \c return is called, and because we're returning by |
|
661 |
value, the copy constructor is called and a copy of the string is |
|
662 |
returned. No actual copying takes place thanks to the implicit |
|
663 |
sharing. |
|
664 |
||
665 |
\endtable |
|
666 |
||
667 |
\section1 Distinction Between Null and Empty Strings |
|
668 |
||
669 |
For historical reasons, QString distinguishes between a null |
|
670 |
string and an empty string. A \e null string is a string that is |
|
671 |
initialized using QString's default constructor or by passing |
|
672 |
(const char *)0 to the constructor. An \e empty string is any |
|
673 |
string with size 0. A null string is always empty, but an empty |
|
674 |
string isn't necessarily null: |
|
675 |
||
676 |
\snippet doc/src/snippets/qstring/main.cpp 8 |
|
677 |
||
678 |
All functions except isNull() treat null strings the same as empty |
|
679 |
strings. For example, toAscii().constData() returns a pointer to a |
|
680 |
'\\0' character for a null string (\e not a null pointer), and |
|
681 |
QString() compares equal to QString(""). We recommend that you |
|
682 |
always use the isEmpty() function and avoid isNull(). |
|
683 |
||
684 |
\section1 Argument Formats |
|
685 |
||
686 |
In member functions where an argument \e format can be specified |
|
687 |
(e.g., arg(), number()), the argument \e format can be one of the |
|
688 |
following: |
|
689 |
||
690 |
\table |
|
691 |
\header \o Format \o Meaning |
|
692 |
\row \o \c e \o format as [-]9.9e[+|-]999 |
|
693 |
\row \o \c E \o format as [-]9.9E[+|-]999 |
|
694 |
\row \o \c f \o format as [-]9.9 |
|
695 |
\row \o \c g \o use \c e or \c f format, whichever is the most concise |
|
696 |
\row \o \c G \o use \c E or \c f format, whichever is the most concise |
|
697 |
\endtable |
|
698 |
||
699 |
A \e precision is also specified with the argument \e format. For |
|
700 |
the 'e', 'E', and 'f' formats, the \e precision represents the |
|
701 |
number of digits \e after the decimal point. For the 'g' and 'G' |
|
702 |
formats, the \e precision represents the maximum number of |
|
703 |
significant digits (trailing zeroes are omitted). |
|
704 |
||
705 |
\section1 More Efficient String Construction |
|
706 |
||
707 |
Using the QString \c{'+'} operator, it is easy to construct a |
|
708 |
complex string from multiple substrings. You will often write code |
|
709 |
like this: |
|
710 |
||
711 |
\snippet doc/src/snippets/qstring/stringbuilder.cpp 0 |
|
712 |
||
713 |
There is nothing wrong with either of these string constructions, |
|
714 |
but there are a few hidden inefficiencies. Beginning with Qt 4.6, |
|
715 |
you can eliminate them. |
|
716 |
||
717 |
First, multiple uses of the \c{'+'} operator usually means |
|
718 |
multiple memory allocations. When concatenating \e{n} substrings, |
|
719 |
where \e{n > 2}, there can be as many as \e{n - 1} calls to the |
|
720 |
memory allocator. |
|
721 |
||
722 |
Second, QLatin1String does not store its length internally but |
|
723 |
calls qstrlen() when it needs to know its length. |
|
724 |
||
725 |
In 4.6, an internal template class \c{QStringBuilder} has been |
|
726 |
added along with a few helper functions. This class is marked |
|
727 |
internal and does not appear in the documentation, because you |
|
728 |
aren't meant to instantiate it in your code. Its use will be |
|
729 |
automatic, as described below. The class is found in |
|
730 |
\c {src/corelib/tools/qstringbuilder.cpp} if you want to have a |
|
731 |
look at it. |
|
732 |
||
733 |
\c{QStringBuilder} uses expression templates and reimplements the |
|
734 |
\c{'%'} operator so that when you use \c{'%'} for string |
|
735 |
concatenation instead of \c{'+'}, multiple substring |
|
736 |
concatenations will be postponed until the final result is about |
|
737 |
to be assigned to a QString. At this point, the amount of memory |
|
738 |
required for the final result is known. The memory allocator is |
|
739 |
then called \e{once} to get the required space, and the substrings |
|
740 |
are copied into it one by one. |
|
741 |
||
742 |
\c{QLatin1Literal} is a second internal class that can replace |
|
743 |
QLatin1String, which can't be changed for compatibility reasons. |
|
744 |
\c{QLatin1Literal} stores its length, thereby saving time when |
|
745 |
\c{QStringBuilder} computes the amount of memory required for the |
|
746 |
final string. |
|
747 |
||
748 |
Additional efficiency is gained by inlining and reduced reference |
|
749 |
counting (the QString created from a \c{QStringBuilder} typically |
|
750 |
has a ref count of 1, whereas QString::append() needs an extra |
|
751 |
test). |
|
752 |
||
753 |
There are three ways you can access this improved method of string |
|
754 |
construction. The straightforward way is to include |
|
755 |
\c{QStringBuilder} wherever you want to use it, and use the |
|
756 |
\c{'%'} operator instead of \c{'+'} when concatenating strings: |
|
757 |
||
758 |
\snippet doc/src/snippets/qstring/stringbuilder.cpp 5 |
|
759 |
||
760 |
A more global approach is to include this define: |
|
761 |
||
762 |
\snippet doc/src/snippets/qstring/stringbuilder.cpp 3 |
|
763 |
||
764 |
and use \c{'%'} instead of \c{'+'} for string concatenation |
|
765 |
everywhere. The third approach, which is the most convenient but |
|
766 |
not entirely source compatible, is to include two defines: |
|
767 |
||
768 |
\snippet doc/src/snippets/qstring/stringbuilder.cpp 4 |
|
769 |
||
770 |
and the \c{'+'} will automatically be performed as the |
|
771 |
\c{QStringBuilder} \c{'%'} everywhere. |
|
772 |
||
773 |
\sa fromRawData(), QChar, QLatin1String, QByteArray, QStringRef |
|
774 |
*/ |
|
775 |
||
776 |
/*! |
|
777 |
\enum QString::SplitBehavior |
|
778 |
||
779 |
This enum specifies how the split() function should behave with |
|
780 |
respect to empty strings. |
|
781 |
||
782 |
\value KeepEmptyParts If a field is empty, keep it in the result. |
|
783 |
\value SkipEmptyParts If a field is empty, don't include it in the result. |
|
784 |
||
785 |
\sa split() |
|
786 |
*/ |
|
787 |
||
788 |
QString::Data QString::shared_null = { Q_BASIC_ATOMIC_INITIALIZER(1), |
|
789 |
0, 0, shared_null.array, 0, 0, 0, 0, 0, 0, {0} }; |
|
790 |
QString::Data QString::shared_empty = { Q_BASIC_ATOMIC_INITIALIZER(1), |
|
791 |
0, 0, shared_empty.array, 0, 0, 0, 0, 0, 0, {0} }; |
|
792 |
||
793 |
int QString::grow(int size) |
|
794 |
{ |
|
795 |
return qAllocMore(size * sizeof(QChar), sizeof(Data)) / sizeof(QChar); |
|
796 |
} |
|
797 |
||
798 |
/*! \typedef QString::ConstIterator |
|
799 |
||
800 |
Qt-style synonym for QString::const_iterator. |
|
801 |
*/ |
|
802 |
||
803 |
/*! \typedef QString::Iterator |
|
804 |
||
805 |
Qt-style synonym for QString::iterator. |
|
806 |
*/ |
|
807 |
||
808 |
/*! \typedef QString::const_iterator |
|
809 |
||
810 |
The QString::const_iterator typedef provides an STL-style const |
|
811 |
iterator for QString. |
|
812 |
||
813 |
\sa QString::iterator |
|
814 |
*/ |
|
815 |
||
816 |
/*! \typedef QString::iterator |
|
817 |
||
818 |
The QString::iterator typedef provides an STL-style non-const |
|
819 |
iterator for QString. |
|
820 |
||
821 |
\sa QString::const_iterator |
|
822 |
*/ |
|
823 |
||
824 |
/*! \fn QString::iterator QString::begin() |
|
825 |
||
826 |
Returns an \l{STL-style iterator} pointing to the first character in |
|
827 |
the string. |
|
828 |
||
829 |
\sa constBegin(), end() |
|
830 |
*/ |
|
831 |
||
832 |
/*! \fn QString::const_iterator QString::begin() const |
|
833 |
||
834 |
\overload begin() |
|
835 |
*/ |
|
836 |
||
837 |
/*! \fn QString::const_iterator QString::constBegin() const |
|
838 |
||
839 |
Returns a const \l{STL-style iterator} pointing to the first character |
|
840 |
in the string. |
|
841 |
||
842 |
\sa begin(), constEnd() |
|
843 |
*/ |
|
844 |
||
845 |
/*! \fn QString::iterator QString::end() |
|
846 |
||
847 |
Returns an \l{STL-style iterator} pointing to the imaginary character |
|
848 |
after the last character in the string. |
|
849 |
||
850 |
\sa begin(), constEnd() |
|
851 |
*/ |
|
852 |
||
853 |
/*! \fn QString::const_iterator QString::end() const |
|
854 |
||
855 |
\overload end() |
|
856 |
*/ |
|
857 |
||
858 |
/*! \fn QString::const_iterator QString::constEnd() const |
|
859 |
||
860 |
Returns a const \l{STL-style iterator} pointing to the imaginary |
|
861 |
item after the last item in the list. |
|
862 |
||
863 |
\sa constBegin(), end() |
|
864 |
*/ |
|
865 |
||
866 |
/*! |
|
867 |
\fn QString::QString() |
|
868 |
||
869 |
Constructs a null string. Null strings are also empty. |
|
870 |
||
871 |
\sa isEmpty() |
|
872 |
*/ |
|
873 |
||
874 |
/*! \fn QString::QString(const char *str) |
|
875 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
876 |
Constructs a string initialized with the 8-bit string \a str. The |
0 | 877 |
given const char pointer is converted to Unicode using the |
878 |
fromAscii() function. |
|
879 |
||
880 |
You can disable this constructor by defining \c |
|
881 |
QT_NO_CAST_FROM_ASCII when you compile your applications. This |
|
882 |
can be useful if you want to ensure that all user-visible strings |
|
883 |
go through QObject::tr(), for example. |
|
884 |
||
885 |
\sa fromAscii(), fromLatin1(), fromLocal8Bit(), fromUtf8() |
|
886 |
*/ |
|
887 |
||
888 |
/*! \fn QString QString::fromStdString(const std::string &str) |
|
889 |
||
890 |
Returns a copy of the \a str string. The given string is converted |
|
891 |
to Unicode using the fromAscii() function. |
|
892 |
||
893 |
This constructor is only available if Qt is configured with STL |
|
894 |
compatibility enabled. |
|
895 |
||
896 |
\sa fromAscii(), fromLatin1(), fromLocal8Bit(), fromUtf8() |
|
897 |
*/ |
|
898 |
||
899 |
/*! \fn QString QString::fromStdWString(const std::wstring &str) |
|
900 |
||
901 |
Returns a copy of the \a str string. The given string is assumed |
|
902 |
to be encoded in utf16 if the size of wchar_t is 2 bytes (e.g. on |
|
903 |
windows) and ucs4 if the size of wchar_t is 4 bytes (most Unix |
|
904 |
systems). |
|
905 |
||
906 |
This method is only available if Qt is configured with STL |
|
907 |
compatibility enabled. |
|
908 |
||
909 |
\sa fromUtf16(), fromLatin1(), fromLocal8Bit(), fromUtf8(), fromUcs4() |
|
910 |
*/ |
|
911 |
||
912 |
/*! |
|
913 |
\since 4.2 |
|
914 |
||
915 |
Returns a copy of the \a string, where the encoding of \a string depends on |
|
916 |
the size of wchar. If wchar is 4 bytes, the \a string is interpreted as ucs-4, |
|
917 |
if wchar is 2 bytes it is interpreted as ucs-2. |
|
918 |
||
919 |
If \a size is -1 (default), the \a string has to be 0 terminated. |
|
920 |
||
921 |
\sa fromUtf16(), fromLatin1(), fromLocal8Bit(), fromUtf8(), fromUcs4(), fromStdWString() |
|
922 |
*/ |
|
923 |
QString QString::fromWCharArray(const wchar_t *string, int size) |
|
924 |
{ |
|
925 |
if (sizeof(wchar_t) == sizeof(QChar)) { |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
926 |
return fromUtf16((const ushort *)string, size); |
0 | 927 |
} else { |
928 |
return fromUcs4((uint *)string, size); |
|
929 |
} |
|
930 |
} |
|
931 |
||
932 |
/*! \fn std::wstring QString::toStdWString() const |
|
933 |
||
934 |
Returns a std::wstring object with the data contained in this |
|
935 |
QString. The std::wstring is encoded in utf16 on platforms where |
|
936 |
wchar_t is 2 bytes wide (e.g. windows) and in ucs4 on platforms |
|
937 |
where wchar_t is 4 bytes wide (most Unix systems). |
|
938 |
||
939 |
This operator is mostly useful to pass a QString to a function |
|
940 |
that accepts a std::wstring object. |
|
941 |
||
942 |
This operator is only available if Qt is configured with STL |
|
943 |
compatibility enabled. |
|
944 |
||
945 |
\sa utf16(), toAscii(), toLatin1(), toUtf8(), toLocal8Bit() |
|
946 |
*/ |
|
947 |
||
948 |
/*! |
|
949 |
\since 4.2 |
|
950 |
||
951 |
Fills the \a array with the data contained in this QString object. |
|
952 |
The array is encoded in utf16 on platforms where |
|
953 |
wchar_t is 2 bytes wide (e.g. windows) and in ucs4 on platforms |
|
954 |
where wchar_t is 4 bytes wide (most Unix systems). |
|
955 |
||
956 |
\a array has to be allocated by the caller and contain enough space to |
|
957 |
hold the complete string (allocating the array with the same length as the |
|
958 |
string is always sufficient). |
|
959 |
||
960 |
returns the actual length of the string in \a array. |
|
961 |
||
962 |
\note This function does not append a null character to the array. |
|
963 |
||
964 |
\sa utf16(), toUcs4(), toAscii(), toLatin1(), toUtf8(), toLocal8Bit(), toStdWString() |
|
965 |
*/ |
|
966 |
int QString::toWCharArray(wchar_t *array) const |
|
967 |
{ |
|
968 |
if (sizeof(wchar_t) == sizeof(QChar)) { |
|
969 |
memcpy(array, utf16(), sizeof(wchar_t)*length()); |
|
970 |
return length(); |
|
971 |
} else { |
|
972 |
wchar_t *a = array; |
|
973 |
const unsigned short *uc = utf16(); |
|
974 |
for (int i = 0; i < length(); ++i) { |
|
975 |
uint u = uc[i]; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
976 |
if (QChar::isHighSurrogate(u) && i + 1 < length()) { |
0 | 977 |
ushort low = uc[i+1]; |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
978 |
if (QChar::isLowSurrogate(low)) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
979 |
u = QChar::surrogateToUcs4(u, low); |
0 | 980 |
++i; |
981 |
} |
|
982 |
} |
|
983 |
*a = wchar_t(u); |
|
984 |
++a; |
|
985 |
} |
|
986 |
return a - array; |
|
987 |
} |
|
988 |
} |
|
989 |
||
990 |
/*! \fn QString::QString(const QString &other) |
|
991 |
||
992 |
Constructs a copy of \a other. |
|
993 |
||
994 |
This operation takes \l{constant time}, because QString is |
|
995 |
\l{implicitly shared}. This makes returning a QString from a |
|
996 |
function very fast. If a shared instance is modified, it will be |
|
997 |
copied (copy-on-write), and that takes \l{linear time}. |
|
998 |
||
999 |
\sa operator=() |
|
1000 |
*/ |
|
1001 |
||
1002 |
/*! |
|
1003 |
Constructs a string initialized with the first \a size characters |
|
1004 |
of the QChar array \a unicode. |
|
1005 |
||
1006 |
QString makes a deep copy of the string data. The unicode data is copied as |
|
1007 |
is and the Byte Order Mark is preserved if present. |
|
1008 |
*/ |
|
1009 |
QString::QString(const QChar *unicode, int size) |
|
1010 |
{ |
|
1011 |
if (!unicode) { |
|
1012 |
d = &shared_null; |
|
1013 |
d->ref.ref(); |
|
1014 |
} else if (size <= 0) { |
|
1015 |
d = &shared_empty; |
|
1016 |
d->ref.ref(); |
|
1017 |
} else { |
|
1018 |
d = (Data*) qMalloc(sizeof(Data)+size*sizeof(QChar)); |
|
1019 |
Q_CHECK_PTR(d); |
|
1020 |
d->ref = 1; |
|
1021 |
d->alloc = d->size = size; |
|
1022 |
d->clean = d->asciiCache = d->simpletext = d->righttoleft = d->capacity = 0; |
|
1023 |
d->data = d->array; |
|
1024 |
memcpy(d->array, unicode, size * sizeof(QChar)); |
|
1025 |
d->array[size] = '\0'; |
|
1026 |
} |
|
1027 |
} |
|
1028 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1029 |
/*! |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1030 |
\since 4.7 |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1031 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1032 |
Constructs a string initialized with the characters of the QChar array |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1033 |
\a unicode, which must be terminated with a 0. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1034 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1035 |
QString makes a deep copy of the string data. The unicode data is copied as |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1036 |
is and the Byte Order Mark is preserved if present. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1037 |
*/ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1038 |
QString::QString(const QChar *unicode) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1039 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1040 |
if (!unicode) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1041 |
d = &shared_null; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1042 |
d->ref.ref(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1043 |
} else { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1044 |
int size = 0; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1045 |
while (unicode[size] != 0) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1046 |
++size; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1047 |
if (!size) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1048 |
d = &shared_empty; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1049 |
d->ref.ref(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1050 |
} else { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1051 |
d = (Data*) qMalloc(sizeof(Data)+size*sizeof(QChar)); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1052 |
Q_CHECK_PTR(d); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1053 |
d->ref = 1; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1054 |
d->alloc = d->size = size; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1055 |
d->clean = d->asciiCache = d->simpletext = d->righttoleft = d->capacity = 0; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1056 |
d->data = d->array; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1057 |
memcpy(d->array, unicode, size * sizeof(QChar)); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1058 |
d->array[size] = '\0'; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1059 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1060 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1061 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1062 |
|
0 | 1063 |
|
1064 |
/*! |
|
1065 |
Constructs a string of the given \a size with every character set |
|
1066 |
to \a ch. |
|
1067 |
||
1068 |
\sa fill() |
|
1069 |
*/ |
|
1070 |
QString::QString(int size, QChar ch) |
|
1071 |
{ |
|
1072 |
if (size <= 0) { |
|
1073 |
d = &shared_empty; |
|
1074 |
d->ref.ref(); |
|
1075 |
} else { |
|
1076 |
d = (Data*) qMalloc(sizeof(Data)+size*sizeof(QChar)); |
|
1077 |
Q_CHECK_PTR(d); |
|
1078 |
d->ref = 1; |
|
1079 |
d->alloc = d->size = size; |
|
1080 |
d->clean = d->asciiCache = d->simpletext = d->righttoleft = d->capacity = 0; |
|
1081 |
d->data = d->array; |
|
1082 |
d->array[size] = '\0'; |
|
1083 |
ushort *i = d->array + size; |
|
1084 |
ushort *b = d->array; |
|
1085 |
const ushort value = ch.unicode(); |
|
1086 |
while (i != b) |
|
1087 |
*--i = value; |
|
1088 |
} |
|
1089 |
} |
|
1090 |
||
1091 |
/*! \fn QString::QString(int size, Qt::Initialization) |
|
1092 |
\internal |
|
1093 |
||
1094 |
Constructs a string of the given \a size without initializing the |
|
1095 |
characters. This is only used in \c QStringBuilder::toString(). |
|
1096 |
*/ |
|
1097 |
QString::QString(int size, Qt::Initialization) |
|
1098 |
{ |
|
1099 |
d = (Data*) qMalloc(sizeof(Data)+size*sizeof(QChar)); |
|
1100 |
Q_CHECK_PTR(d); |
|
1101 |
d->ref = 1; |
|
1102 |
d->alloc = d->size = size; |
|
1103 |
d->clean = d->asciiCache = d->simpletext = d->righttoleft = d->capacity = 0; |
|
1104 |
d->data = d->array; |
|
1105 |
d->array[size] = '\0'; |
|
1106 |
} |
|
1107 |
||
1108 |
/*! \fn QString::QString(const QLatin1String &str) |
|
1109 |
||
1110 |
Constructs a copy of the Latin-1 string \a str. |
|
1111 |
||
1112 |
\sa fromLatin1() |
|
1113 |
*/ |
|
1114 |
||
1115 |
/*! |
|
1116 |
Constructs a string of size 1 containing the character \a ch. |
|
1117 |
*/ |
|
1118 |
QString::QString(QChar ch) |
|
1119 |
{ |
|
1120 |
void *buf = qMalloc(sizeof(Data) + sizeof(QChar)); |
|
1121 |
Q_CHECK_PTR(buf); |
|
1122 |
d = reinterpret_cast<Data *>(buf); |
|
1123 |
d->ref = 1; |
|
1124 |
d->alloc = d->size = 1; |
|
1125 |
d->clean = d->asciiCache = d->simpletext = d->righttoleft = d->capacity = 0; |
|
1126 |
d->data = d->array; |
|
1127 |
d->array[0] = ch.unicode(); |
|
1128 |
d->array[1] = '\0'; |
|
1129 |
} |
|
1130 |
||
1131 |
/*! \fn QString::QString(const QByteArray &ba) |
|
1132 |
||
1133 |
Constructs a string initialized with the byte array \a ba. The |
|
1134 |
given byte array is converted to Unicode using fromAscii(). Stops |
|
1135 |
copying at the first 0 character, otherwise copies the entire byte |
|
1136 |
array. |
|
1137 |
||
1138 |
You can disable this constructor by defining \c |
|
1139 |
QT_NO_CAST_FROM_ASCII when you compile your applications. This |
|
1140 |
can be useful if you want to ensure that all user-visible strings |
|
1141 |
go through QObject::tr(), for example. |
|
1142 |
||
1143 |
\sa fromAscii(), fromLatin1(), fromLocal8Bit(), fromUtf8() |
|
1144 |
*/ |
|
1145 |
||
1146 |
/*! \fn QString::QString(const Null &) |
|
1147 |
\internal |
|
1148 |
*/ |
|
1149 |
||
1150 |
/*! \fn QString &QString::operator=(const Null &) |
|
1151 |
\internal |
|
1152 |
*/ |
|
1153 |
||
1154 |
/*! |
|
1155 |
\fn QString::~QString() |
|
1156 |
||
1157 |
Destroys the string. |
|
1158 |
*/ |
|
1159 |
||
1160 |
||
1161 |
/*! \fn void QString::detach() |
|
1162 |
||
1163 |
\internal |
|
1164 |
*/ |
|
1165 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1166 |
/*! \fn bool QString::isDetached() const |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1167 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1168 |
\internal |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1169 |
*/ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1170 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1171 |
/*! \fn bool QString::isSharedWith(const QString &other) const |
0 | 1172 |
|
1173 |
\internal |
|
1174 |
*/ |
|
1175 |
||
1176 |
// ### Qt 5: rename freeData() to avoid confusion. See task 197625. |
|
1177 |
void QString::free(Data *d) |
|
1178 |
{ |
|
1179 |
#ifdef QT3_SUPPORT |
|
1180 |
if (d->asciiCache) { |
|
1181 |
Q_ASSERT(asciiCache); |
|
1182 |
asciiCache->remove(d); |
|
1183 |
} |
|
1184 |
#endif |
|
1185 |
qFree(d); |
|
1186 |
} |
|
1187 |
||
1188 |
/*! |
|
1189 |
Sets the size of the string to \a size characters. |
|
1190 |
||
1191 |
If \a size is greater than the current size, the string is |
|
1192 |
extended to make it \a size characters long with the extra |
|
1193 |
characters added to the end. The new characters are uninitialized. |
|
1194 |
||
1195 |
If \a size is less than the current size, characters are removed |
|
1196 |
from the end. |
|
1197 |
||
1198 |
Example: |
|
1199 |
||
1200 |
\snippet doc/src/snippets/qstring/main.cpp 45 |
|
1201 |
||
1202 |
If you want to append a certain number of identical characters to |
|
1203 |
the string, use \l operator+=() as follows rather than resize(): |
|
1204 |
||
1205 |
\snippet doc/src/snippets/qstring/main.cpp 46 |
|
1206 |
||
1207 |
If you want to expand the string so that it reaches a certain |
|
1208 |
width and fill the new positions with a particular character, use |
|
1209 |
the leftJustified() function: |
|
1210 |
||
1211 |
If \a size is negative, it is equivalent to passing zero. |
|
1212 |
||
1213 |
\snippet doc/src/snippets/qstring/main.cpp 47 |
|
1214 |
||
1215 |
\sa truncate(), reserve() |
|
1216 |
*/ |
|
1217 |
||
1218 |
void QString::resize(int size) |
|
1219 |
{ |
|
1220 |
if (size < 0) |
|
1221 |
size = 0; |
|
1222 |
||
1223 |
if (size == 0 && !d->capacity) { |
|
1224 |
Data *x = &shared_empty; |
|
1225 |
x->ref.ref(); |
|
1226 |
if (!d->ref.deref()) |
|
1227 |
QString::free(d); |
|
1228 |
d = x; |
|
1229 |
} else { |
|
1230 |
if (d->ref != 1 || size > d->alloc || |
|
1231 |
(!d->capacity && size < d->size && size < d->alloc >> 1)) |
|
1232 |
realloc(grow(size)); |
|
1233 |
if (d->alloc >= size) { |
|
1234 |
d->size = size; |
|
1235 |
if (d->data == d->array) { |
|
1236 |
d->array[size] = '\0'; |
|
1237 |
} |
|
1238 |
} |
|
1239 |
} |
|
1240 |
} |
|
1241 |
||
1242 |
/*! \fn int QString::capacity() const |
|
1243 |
||
1244 |
Returns the maximum number of characters that can be stored in |
|
1245 |
the string without forcing a reallocation. |
|
1246 |
||
1247 |
The sole purpose of this function is to provide a means of fine |
|
1248 |
tuning QString's memory usage. In general, you will rarely ever |
|
1249 |
need to call this function. If you want to know how many |
|
1250 |
characters are in the string, call size(). |
|
1251 |
||
1252 |
\sa reserve(), squeeze() |
|
1253 |
*/ |
|
1254 |
||
1255 |
/*! |
|
1256 |
\fn void QString::reserve(int size) |
|
1257 |
||
1258 |
Attempts to allocate memory for at least \a size characters. If |
|
1259 |
you know in advance how large the string will be, you can call |
|
1260 |
this function, and if you resize the string often you are likely |
|
1261 |
to get better performance. If \a size is an underestimate, the |
|
1262 |
worst that will happen is that the QString will be a bit slower. |
|
1263 |
||
1264 |
The sole purpose of this function is to provide a means of fine |
|
1265 |
tuning QString's memory usage. In general, you will rarely ever |
|
1266 |
need to call this function. If you want to change the size of the |
|
1267 |
string, call resize(). |
|
1268 |
||
1269 |
This function is useful for code that needs to build up a long |
|
1270 |
string and wants to avoid repeated reallocation. In this example, |
|
1271 |
we want to add to the string until some condition is true, and |
|
1272 |
we're fairly sure that size is large enough to make a call to |
|
1273 |
reserve() worthwhile: |
|
1274 |
||
1275 |
\snippet doc/src/snippets/qstring/main.cpp 44 |
|
1276 |
||
1277 |
\sa squeeze(), capacity() |
|
1278 |
*/ |
|
1279 |
||
1280 |
/*! |
|
1281 |
\fn void QString::squeeze() |
|
1282 |
||
1283 |
Releases any memory not required to store the character data. |
|
1284 |
||
1285 |
The sole purpose of this function is to provide a means of fine |
|
1286 |
tuning QString's memory usage. In general, you will rarely ever |
|
1287 |
need to call this function. |
|
1288 |
||
1289 |
\sa reserve(), capacity() |
|
1290 |
*/ |
|
1291 |
||
1292 |
// ### Qt 5: rename reallocData() to avoid confusion. 197625 |
|
1293 |
void QString::realloc(int alloc) |
|
1294 |
{ |
|
1295 |
if (d->ref != 1 || d->data != d->array) { |
|
1296 |
Data *x = static_cast<Data *>(qMalloc(sizeof(Data) + alloc * sizeof(QChar))); |
|
1297 |
Q_CHECK_PTR(x); |
|
1298 |
x->size = qMin(alloc, d->size); |
|
1299 |
::memcpy(x->array, d->data, x->size * sizeof(QChar)); |
|
1300 |
x->array[x->size] = 0; |
|
1301 |
x->asciiCache = 0; |
|
1302 |
x->ref = 1; |
|
1303 |
x->alloc = alloc; |
|
1304 |
x->clean = d->clean; |
|
1305 |
x->simpletext = d->simpletext; |
|
1306 |
x->righttoleft = d->righttoleft; |
|
1307 |
x->capacity = d->capacity; |
|
1308 |
x->data = x->array; |
|
1309 |
if (!d->ref.deref()) |
|
1310 |
QString::free(d); |
|
1311 |
d = x; |
|
1312 |
} else { |
|
1313 |
#ifdef QT3_SUPPORT |
|
1314 |
if (d->asciiCache) { |
|
1315 |
Q_ASSERT(asciiCache); |
|
1316 |
asciiCache->remove(d); |
|
1317 |
} |
|
1318 |
#endif |
|
1319 |
d = static_cast<Data *>(q_check_ptr(qRealloc(d, sizeof(Data) + alloc * sizeof(QChar)))); |
|
1320 |
d->alloc = alloc; |
|
1321 |
d->data = d->array; |
|
1322 |
} |
|
1323 |
} |
|
1324 |
||
1325 |
void QString::realloc() |
|
1326 |
{ |
|
1327 |
realloc(d->size); |
|
1328 |
} |
|
1329 |
||
1330 |
void QString::expand(int i) |
|
1331 |
{ |
|
1332 |
int sz = d->size; |
|
1333 |
resize(qMax(i + 1, sz)); |
|
1334 |
if (d->size - 1 > sz) { |
|
1335 |
ushort *n = d->data + d->size - 1; |
|
1336 |
ushort *e = d->data + sz; |
|
1337 |
while (n != e) |
|
1338 |
* --n = ' '; |
|
1339 |
} |
|
1340 |
} |
|
1341 |
||
1342 |
/*! \fn void QString::clear() |
|
1343 |
||
1344 |
Clears the contents of the string and makes it empty. |
|
1345 |
||
1346 |
\sa resize(), isEmpty() |
|
1347 |
*/ |
|
1348 |
||
1349 |
/*! \fn QString &QString::operator=(const QString &other) |
|
1350 |
||
1351 |
Assigns \a other to this string and returns a reference to this |
|
1352 |
string. |
|
1353 |
*/ |
|
1354 |
||
1355 |
QString &QString::operator=(const QString &other) |
|
1356 |
{ |
|
1357 |
other.d->ref.ref(); |
|
1358 |
if (!d->ref.deref()) |
|
1359 |
QString::free(d); |
|
1360 |
d = other.d; |
|
1361 |
return *this; |
|
1362 |
} |
|
1363 |
||
1364 |
||
1365 |
/*! \fn QString &QString::operator=(const QLatin1String &str) |
|
1366 |
||
1367 |
\overload operator=() |
|
1368 |
||
1369 |
Assigns the Latin-1 string \a str to this string. |
|
1370 |
*/ |
|
1371 |
||
1372 |
/*! \fn QString &QString::operator=(const QByteArray &ba) |
|
1373 |
||
1374 |
\overload operator=() |
|
1375 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1376 |
Assigns \a ba to this string. The byte array is converted to Unicode |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1377 |
using the fromAscii() function. This function stops conversion at the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1378 |
first NUL character found, or the end of the \a ba byte array. |
0 | 1379 |
|
1380 |
You can disable this operator by defining \c |
|
1381 |
QT_NO_CAST_FROM_ASCII when you compile your applications. This |
|
1382 |
can be useful if you want to ensure that all user-visible strings |
|
1383 |
go through QObject::tr(), for example. |
|
1384 |
*/ |
|
1385 |
||
1386 |
/*! \fn QString &QString::operator=(const char *str) |
|
1387 |
||
1388 |
\overload operator=() |
|
1389 |
||
1390 |
Assigns \a str to this string. The const char pointer is converted |
|
1391 |
to Unicode using the fromAscii() function. |
|
1392 |
||
1393 |
You can disable this operator by defining \c |
|
1394 |
QT_NO_CAST_FROM_ASCII when you compile your applications. This |
|
1395 |
can be useful if you want to ensure that all user-visible strings |
|
1396 |
go through QObject::tr(), for example. |
|
1397 |
*/ |
|
1398 |
||
1399 |
/*! \fn QString &QString::operator=(char ch) |
|
1400 |
||
1401 |
\overload operator=() |
|
1402 |
||
1403 |
Assigns character \a ch to this string. The character is converted |
|
1404 |
to Unicode using the fromAscii() function. |
|
1405 |
||
1406 |
You can disable this operator by defining \c |
|
1407 |
QT_NO_CAST_FROM_ASCII when you compile your applications. This |
|
1408 |
can be useful if you want to ensure that all user-visible strings |
|
1409 |
go through QObject::tr(), for example. |
|
1410 |
*/ |
|
1411 |
||
1412 |
/*! |
|
1413 |
\overload operator=() |
|
1414 |
||
1415 |
Sets the string to contain the single character \a ch. |
|
1416 |
*/ |
|
1417 |
QString &QString::operator=(QChar ch) |
|
1418 |
{ |
|
1419 |
return operator=(QString(ch)); |
|
1420 |
} |
|
1421 |
||
1422 |
/*! |
|
1423 |
\fn QString& QString::insert(int position, const QString &str) |
|
1424 |
||
1425 |
Inserts the string \a str at the given index \a position and |
|
1426 |
returns a reference to this string. |
|
1427 |
||
1428 |
Example: |
|
1429 |
||
1430 |
\snippet doc/src/snippets/qstring/main.cpp 26 |
|
1431 |
||
1432 |
If the given \a position is greater than size(), the array is |
|
1433 |
first extended using resize(). |
|
1434 |
||
1435 |
\sa append(), prepend(), replace(), remove() |
|
1436 |
*/ |
|
1437 |
||
1438 |
||
1439 |
/*! |
|
1440 |
\fn QString &QString::insert(int position, const QLatin1String &str) |
|
1441 |
\overload insert() |
|
1442 |
||
1443 |
Inserts the Latin-1 string \a str at the given index \a position. |
|
1444 |
*/ |
|
1445 |
QString &QString::insert(int i, const QLatin1String &str) |
|
1446 |
{ |
|
1447 |
const uchar *s = (const uchar *)str.latin1(); |
|
1448 |
if (i < 0 || !s || !(*s)) |
|
1449 |
return *this; |
|
1450 |
||
1451 |
int len = qstrlen(str.latin1()); |
|
1452 |
expand(qMax(d->size, i) + len - 1); |
|
1453 |
||
1454 |
::memmove(d->data + i + len, d->data + i, (d->size - i - len) * sizeof(QChar)); |
|
1455 |
for (int j = 0; j < len; ++j) |
|
1456 |
d->data[i + j] = s[j]; |
|
1457 |
return *this; |
|
1458 |
} |
|
1459 |
||
1460 |
/*! |
|
1461 |
\fn QString& QString::insert(int position, const QChar *unicode, int size) |
|
1462 |
\overload insert() |
|
1463 |
||
1464 |
Inserts the first \a size characters of the QChar array \a unicode |
|
1465 |
at the given index \a position in the string. |
|
1466 |
*/ |
|
1467 |
QString& QString::insert(int i, const QChar *unicode, int size) |
|
1468 |
{ |
|
1469 |
if (i < 0 || size <= 0) |
|
1470 |
return *this; |
|
1471 |
||
1472 |
const ushort *s = (const ushort *)unicode; |
|
1473 |
if (s >= d->data && s < d->data + d->alloc) { |
|
1474 |
// Part of me - take a copy |
|
1475 |
ushort *tmp = static_cast<ushort *>(qMalloc(size * sizeof(QChar))); |
|
1476 |
Q_CHECK_PTR(tmp); |
|
1477 |
memcpy(tmp, s, size * sizeof(QChar)); |
|
1478 |
insert(i, reinterpret_cast<const QChar *>(tmp), size); |
|
1479 |
qFree(tmp); |
|
1480 |
return *this; |
|
1481 |
} |
|
1482 |
||
1483 |
expand(qMax(d->size, i) + size - 1); |
|
1484 |
||
1485 |
::memmove(d->data + i + size, d->data + i, (d->size - i - size) * sizeof(QChar)); |
|
1486 |
memcpy(d->data + i, s, size * sizeof(QChar)); |
|
1487 |
return *this; |
|
1488 |
} |
|
1489 |
||
1490 |
/*! |
|
1491 |
\fn QString& QString::insert(int position, QChar ch) |
|
1492 |
\overload insert() |
|
1493 |
||
1494 |
Inserts \a ch at the given index \a position in the string. |
|
1495 |
*/ |
|
1496 |
||
1497 |
QString& QString::insert(int i, QChar ch) |
|
1498 |
{ |
|
1499 |
if (i < 0) |
|
1500 |
i += d->size; |
|
1501 |
if (i < 0) |
|
1502 |
return *this; |
|
1503 |
expand(qMax(i, d->size)); |
|
1504 |
::memmove(d->data + i + 1, d->data + i, (d->size - i) * sizeof(QChar)); |
|
1505 |
d->data[i] = ch.unicode(); |
|
1506 |
return *this; |
|
1507 |
} |
|
1508 |
||
1509 |
/*! |
|
1510 |
Appends the string \a str onto the end of this string. |
|
1511 |
||
1512 |
Example: |
|
1513 |
||
1514 |
\snippet doc/src/snippets/qstring/main.cpp 9 |
|
1515 |
||
1516 |
This is the same as using the insert() function: |
|
1517 |
||
1518 |
\snippet doc/src/snippets/qstring/main.cpp 10 |
|
1519 |
||
1520 |
The append() function is typically very fast (\l{constant time}), |
|
1521 |
because QString preallocates extra space at the end of the string |
|
1522 |
data so it can grow without reallocating the entire string each |
|
1523 |
time. |
|
1524 |
||
1525 |
\sa operator+=(), prepend(), insert() |
|
1526 |
*/ |
|
1527 |
QString &QString::append(const QString &str) |
|
1528 |
{ |
|
1529 |
if (str.d != &shared_null) { |
|
1530 |
if (d == &shared_null) { |
|
1531 |
operator=(str); |
|
1532 |
} else { |
|
1533 |
if (d->ref != 1 || d->size + str.d->size > d->alloc) |
|
1534 |
realloc(grow(d->size + str.d->size)); |
|
1535 |
memcpy(d->data + d->size, str.d->data, str.d->size * sizeof(QChar)); |
|
1536 |
d->size += str.d->size; |
|
1537 |
d->data[d->size] = '\0'; |
|
1538 |
} |
|
1539 |
} |
|
1540 |
return *this; |
|
1541 |
} |
|
1542 |
||
1543 |
/*! |
|
1544 |
\overload append() |
|
1545 |
||
1546 |
Appends the Latin-1 string \a str to this string. |
|
1547 |
*/ |
|
1548 |
QString &QString::append(const QLatin1String &str) |
|
1549 |
{ |
|
1550 |
const uchar *s = (const uchar *)str.latin1(); |
|
1551 |
if (s) { |
|
1552 |
int len = qstrlen((char *)s); |
|
1553 |
if (d->ref != 1 || d->size + len > d->alloc) |
|
1554 |
realloc(grow(d->size + len)); |
|
1555 |
ushort *i = d->data + d->size; |
|
1556 |
while ((*i++ = *s++)) |
|
1557 |
; |
|
1558 |
d->size += len; |
|
1559 |
} |
|
1560 |
return *this; |
|
1561 |
} |
|
1562 |
||
1563 |
/*! \fn QString &QString::append(const QByteArray &ba) |
|
1564 |
||
1565 |
\overload append() |
|
1566 |
||
1567 |
Appends the byte array \a ba to this string. The given byte array |
|
1568 |
is converted to Unicode using the fromAscii() function. |
|
1569 |
||
1570 |
You can disable this function by defining \c QT_NO_CAST_FROM_ASCII |
|
1571 |
when you compile your applications. This can be useful if you want |
|
1572 |
to ensure that all user-visible strings go through QObject::tr(), |
|
1573 |
for example. |
|
1574 |
*/ |
|
1575 |
||
1576 |
/*! \fn QString &QString::append(const char *str) |
|
1577 |
||
1578 |
\overload append() |
|
1579 |
||
1580 |
Appends the string \a str to this string. The given const char |
|
1581 |
pointer is converted to Unicode using the fromAscii() function. |
|
1582 |
||
1583 |
You can disable this function by defining \c QT_NO_CAST_FROM_ASCII |
|
1584 |
when you compile your applications. This can be useful if you want |
|
1585 |
to ensure that all user-visible strings go through QObject::tr(), |
|
1586 |
for example. |
|
1587 |
*/ |
|
1588 |
||
1589 |
/*! |
|
1590 |
\overload append() |
|
1591 |
||
1592 |
Appends the character \a ch to this string. |
|
1593 |
*/ |
|
1594 |
QString &QString::append(QChar ch) |
|
1595 |
{ |
|
1596 |
if (d->ref != 1 || d->size + 1 > d->alloc) |
|
1597 |
realloc(grow(d->size + 1)); |
|
1598 |
d->data[d->size++] = ch.unicode(); |
|
1599 |
d->data[d->size] = '\0'; |
|
1600 |
return *this; |
|
1601 |
} |
|
1602 |
||
1603 |
/*! \fn QString &QString::prepend(const QString &str) |
|
1604 |
||
1605 |
Prepends the string \a str to the beginning of this string and |
|
1606 |
returns a reference to this string. |
|
1607 |
||
1608 |
Example: |
|
1609 |
||
1610 |
\snippet doc/src/snippets/qstring/main.cpp 36 |
|
1611 |
||
1612 |
\sa append(), insert() |
|
1613 |
*/ |
|
1614 |
||
1615 |
/*! \fn QString &QString::prepend(const QLatin1String &str) |
|
1616 |
||
1617 |
\overload prepend() |
|
1618 |
||
1619 |
Prepends the Latin-1 string \a str to this string. |
|
1620 |
*/ |
|
1621 |
||
1622 |
/*! \fn QString &QString::prepend(const QByteArray &ba) |
|
1623 |
||
1624 |
\overload prepend() |
|
1625 |
||
1626 |
Prepends the byte array \a ba to this string. The byte array is |
|
1627 |
converted to Unicode using the fromAscii() function. |
|
1628 |
||
1629 |
You can disable this function by defining \c |
|
1630 |
QT_NO_CAST_FROM_ASCII when you compile your applications. This |
|
1631 |
can be useful if you want to ensure that all user-visible strings |
|
1632 |
go through QObject::tr(), for example. |
|
1633 |
*/ |
|
1634 |
||
1635 |
/*! \fn QString &QString::prepend(const char *str) |
|
1636 |
||
1637 |
\overload prepend() |
|
1638 |
||
1639 |
Prepends the string \a str to this string. The const char pointer |
|
1640 |
is converted to Unicode using the fromAscii() function. |
|
1641 |
||
1642 |
You can disable this function by defining \c |
|
1643 |
QT_NO_CAST_FROM_ASCII when you compile your applications. This |
|
1644 |
can be useful if you want to ensure that all user-visible strings |
|
1645 |
go through QObject::tr(), for example. |
|
1646 |
*/ |
|
1647 |
||
1648 |
/*! \fn QString &QString::prepend(QChar ch) |
|
1649 |
||
1650 |
\overload prepend() |
|
1651 |
||
1652 |
Prepends the character \a ch to this string. |
|
1653 |
*/ |
|
1654 |
||
1655 |
/*! |
|
1656 |
\fn QString &QString::remove(int position, int n) |
|
1657 |
||
1658 |
Removes \a n characters from the string, starting at the given \a |
|
1659 |
position index, and returns a reference to the string. |
|
1660 |
||
1661 |
If the specified \a position index is within the string, but \a |
|
1662 |
position + \a n is beyond the end of the string, the string is |
|
1663 |
truncated at the specified \a position. |
|
1664 |
||
1665 |
\snippet doc/src/snippets/qstring/main.cpp 37 |
|
1666 |
||
1667 |
\sa insert(), replace() |
|
1668 |
*/ |
|
1669 |
QString &QString::remove(int pos, int len) |
|
1670 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1671 |
if (pos < 0) // count from end of string |
0 | 1672 |
pos += d->size; |
1673 |
if (pos < 0 || pos >= d->size) { |
|
1674 |
// range problems |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1675 |
} else if (len >= d->size - pos) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1676 |
resize(pos); // truncate |
0 | 1677 |
} else if (len > 0) { |
1678 |
detach(); |
|
1679 |
memmove(d->data + pos, d->data + pos + len, |
|
1680 |
(d->size - pos - len + 1) * sizeof(ushort)); |
|
1681 |
d->size -= len; |
|
1682 |
} |
|
1683 |
return *this; |
|
1684 |
} |
|
1685 |
||
1686 |
/*! |
|
1687 |
Removes every occurrence of the given \a str string in this |
|
1688 |
string, and returns a reference to this string. |
|
1689 |
||
1690 |
If \a cs is Qt::CaseSensitive (default), the search is |
|
1691 |
case sensitive; otherwise the search is case insensitive. |
|
1692 |
||
1693 |
This is the same as \c replace(str, "", cs). |
|
1694 |
||
1695 |
\sa replace() |
|
1696 |
*/ |
|
1697 |
QString &QString::remove(const QString &str, Qt::CaseSensitivity cs) |
|
1698 |
{ |
|
1699 |
if (str.d->size) { |
|
1700 |
int i = 0; |
|
1701 |
while ((i = indexOf(str, i, cs)) != -1) |
|
1702 |
remove(i, str.d->size); |
|
1703 |
} |
|
1704 |
return *this; |
|
1705 |
} |
|
1706 |
||
1707 |
/*! |
|
1708 |
Removes every occurrence of the character \a ch in this string, and |
|
1709 |
returns a reference to this string. |
|
1710 |
||
1711 |
If \a cs is Qt::CaseSensitive (default), the search is case |
|
1712 |
sensitive; otherwise the search is case insensitive. |
|
1713 |
||
1714 |
Example: |
|
1715 |
||
1716 |
\snippet doc/src/snippets/qstring/main.cpp 38 |
|
1717 |
||
1718 |
This is the same as \c replace(ch, "", cs). |
|
1719 |
||
1720 |
\sa replace() |
|
1721 |
*/ |
|
1722 |
QString &QString::remove(QChar ch, Qt::CaseSensitivity cs) |
|
1723 |
{ |
|
1724 |
int i = 0; |
|
1725 |
ushort c = ch.unicode(); |
|
1726 |
if (cs == Qt::CaseSensitive) { |
|
1727 |
while (i < d->size) |
|
1728 |
if (d->data[i] == ch) |
|
1729 |
remove(i, 1); |
|
1730 |
else |
|
1731 |
i++; |
|
1732 |
} else { |
|
1733 |
c = foldCase(c); |
|
1734 |
while (i < d->size) |
|
1735 |
if (foldCase(d->data[i]) == c) |
|
1736 |
remove(i, 1); |
|
1737 |
else |
|
1738 |
i++; |
|
1739 |
} |
|
1740 |
return *this; |
|
1741 |
} |
|
1742 |
||
1743 |
/*! |
|
1744 |
\fn QString &QString::remove(const QRegExp &rx) |
|
1745 |
||
1746 |
Removes every occurrence of the regular expression \a rx in the |
|
1747 |
string, and returns a reference to the string. For example: |
|
1748 |
||
1749 |
\snippet doc/src/snippets/qstring/main.cpp 39 |
|
1750 |
||
1751 |
\sa indexOf(), lastIndexOf(), replace() |
|
1752 |
*/ |
|
1753 |
||
1754 |
/*! |
|
1755 |
\fn QString &QString::replace(int position, int n, const QString &after) |
|
1756 |
||
1757 |
Replaces \a n characters beginning at index \a position with |
|
1758 |
the string \a after and returns a reference to this string. |
|
1759 |
||
1760 |
Example: |
|
1761 |
||
1762 |
\snippet doc/src/snippets/qstring/main.cpp 40 |
|
1763 |
||
1764 |
\sa insert(), remove() |
|
1765 |
*/ |
|
1766 |
QString &QString::replace(int pos, int len, const QString &after) |
|
1767 |
{ |
|
1768 |
QString copy = after; |
|
1769 |
return replace(pos, len, copy.constData(), copy.length()); |
|
1770 |
} |
|
1771 |
||
1772 |
/*! |
|
1773 |
\fn QString &QString::replace(int position, int n, const QChar *unicode, int size) |
|
1774 |
\overload replace() |
|
1775 |
Replaces \a n characters beginning at index \a position with the |
|
1776 |
first \a size characters of the QChar array \a unicode and returns a |
|
1777 |
reference to this string. |
|
1778 |
*/ |
|
1779 |
QString &QString::replace(int pos, int len, const QChar *unicode, int size) |
|
1780 |
{ |
|
1781 |
if (pos < 0 || pos > d->size) |
|
1782 |
return *this; |
|
1783 |
if (pos + len > d->size) |
|
1784 |
len = d->size - pos; |
|
1785 |
||
1786 |
uint index = pos; |
|
1787 |
replace_helper(&index, 1, len, unicode, size); |
|
1788 |
return *this; |
|
1789 |
} |
|
1790 |
||
1791 |
/*! |
|
1792 |
\fn QString &QString::replace(int position, int n, QChar after) |
|
1793 |
\overload replace() |
|
1794 |
||
1795 |
Replaces \a n characters beginning at index \a position with the |
|
1796 |
character \a after and returns a reference to this string. |
|
1797 |
*/ |
|
1798 |
QString &QString::replace(int pos, int len, QChar after) |
|
1799 |
{ |
|
1800 |
return replace(pos, len, &after, 1); |
|
1801 |
} |
|
1802 |
||
1803 |
/*! |
|
1804 |
\overload replace() |
|
1805 |
Replaces every occurrence of the string \a before with the string \a |
|
1806 |
after and returns a reference to this string. |
|
1807 |
||
1808 |
If \a cs is Qt::CaseSensitive (default), the search is case |
|
1809 |
sensitive; otherwise the search is case insensitive. |
|
1810 |
||
1811 |
Example: |
|
1812 |
||
1813 |
\snippet doc/src/snippets/qstring/main.cpp 41 |
|
1814 |
||
1815 |
\note The replacement text is not rescanned after it is inserted. |
|
1816 |
||
1817 |
Example: |
|
1818 |
||
1819 |
\snippet doc/src/snippets/qstring/main.cpp 86 |
|
1820 |
*/ |
|
1821 |
QString &QString::replace(const QString &before, const QString &after, Qt::CaseSensitivity cs) |
|
1822 |
{ |
|
1823 |
return replace(before.constData(), before.size(), after.constData(), after.size(), cs); |
|
1824 |
} |
|
1825 |
||
1826 |
/*! |
|
1827 |
\internal |
|
1828 |
*/ |
|
1829 |
void QString::replace_helper(uint *indices, int nIndices, int blen, const QChar *after, int alen) |
|
1830 |
{ |
|
1831 |
// copy *after in case it lies inside our own d->data area |
|
1832 |
// (which we could possibly invalidate via a realloc or corrupt via memcpy operations.) |
|
1833 |
QChar *afterBuffer = const_cast<QChar *>(after); |
|
1834 |
if (after >= reinterpret_cast<QChar *>(d->data) && after < reinterpret_cast<QChar *>(d->data) + d->size) { |
|
1835 |
afterBuffer = static_cast<QChar *>(qMalloc(alen*sizeof(QChar))); |
|
1836 |
Q_CHECK_PTR(afterBuffer); |
|
1837 |
::memcpy(afterBuffer, after, alen*sizeof(QChar)); |
|
1838 |
} |
|
1839 |
||
1840 |
QT_TRY { |
|
1841 |
if (blen == alen) { |
|
1842 |
// replace in place |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1843 |
detach(); |
0 | 1844 |
for (int i = 0; i < nIndices; ++i) |
1845 |
memcpy(d->data + indices[i], afterBuffer, alen * sizeof(QChar)); |
|
1846 |
} else if (alen < blen) { |
|
1847 |
// replace from front |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1848 |
detach(); |
0 | 1849 |
uint to = indices[0]; |
1850 |
if (alen) |
|
1851 |
memcpy(d->data+to, after, alen*sizeof(QChar)); |
|
1852 |
to += alen; |
|
1853 |
uint movestart = indices[0] + blen; |
|
1854 |
for (int i = 1; i < nIndices; ++i) { |
|
1855 |
int msize = indices[i] - movestart; |
|
1856 |
if (msize > 0) { |
|
1857 |
memmove(d->data + to, d->data + movestart, msize * sizeof(QChar)); |
|
1858 |
to += msize; |
|
1859 |
} |
|
1860 |
if (alen) { |
|
1861 |
memcpy(d->data + to, afterBuffer, alen*sizeof(QChar)); |
|
1862 |
to += alen; |
|
1863 |
} |
|
1864 |
movestart = indices[i] + blen; |
|
1865 |
} |
|
1866 |
int msize = d->size - movestart; |
|
1867 |
if (msize > 0) |
|
1868 |
memmove(d->data + to, d->data + movestart, msize * sizeof(QChar)); |
|
1869 |
resize(d->size - nIndices*(blen-alen)); |
|
1870 |
} else { |
|
1871 |
// replace from back |
|
1872 |
int adjust = nIndices*(alen-blen); |
|
1873 |
int newLen = d->size + adjust; |
|
1874 |
int moveend = d->size; |
|
1875 |
resize(newLen); |
|
1876 |
||
1877 |
while (nIndices) { |
|
1878 |
--nIndices; |
|
1879 |
int movestart = indices[nIndices] + blen; |
|
1880 |
int insertstart = indices[nIndices] + nIndices*(alen-blen); |
|
1881 |
int moveto = insertstart + alen; |
|
1882 |
memmove(d->data + moveto, d->data + movestart, |
|
1883 |
(moveend - movestart)*sizeof(QChar)); |
|
1884 |
memcpy(d->data + insertstart, afterBuffer, alen*sizeof(QChar)); |
|
1885 |
moveend = movestart-blen; |
|
1886 |
} |
|
1887 |
} |
|
1888 |
} QT_CATCH(const std::bad_alloc &) { |
|
1889 |
if (afterBuffer != after) |
|
1890 |
qFree(afterBuffer); |
|
1891 |
QT_RETHROW; |
|
1892 |
} |
|
1893 |
if (afterBuffer != after) |
|
1894 |
qFree(afterBuffer); |
|
1895 |
} |
|
1896 |
||
1897 |
/*! |
|
1898 |
\since 4.5 |
|
1899 |
\overload replace() |
|
1900 |
||
1901 |
Replaces each occurrence in this string of the first \a blen |
|
1902 |
characters of \a before with the first \a alen characters of \a |
|
1903 |
after and returns a reference to this string. |
|
1904 |
||
1905 |
If \a cs is Qt::CaseSensitive (default), the search is case |
|
1906 |
sensitive; otherwise the search is case insensitive. |
|
1907 |
*/ |
|
1908 |
QString &QString::replace(const QChar *before, int blen, |
|
1909 |
const QChar *after, int alen, |
|
1910 |
Qt::CaseSensitivity cs) |
|
1911 |
{ |
|
1912 |
if (d->size == 0) { |
|
1913 |
if (blen) |
|
1914 |
return *this; |
|
1915 |
} else { |
|
1916 |
if (cs == Qt::CaseSensitive && before == after && blen == alen) |
|
1917 |
return *this; |
|
1918 |
} |
|
1919 |
if (alen == 0 && blen == 0) |
|
1920 |
return *this; |
|
1921 |
||
1922 |
QStringMatcher matcher(before, blen, cs); |
|
1923 |
||
1924 |
int index = 0; |
|
1925 |
while (1) { |
|
1926 |
uint indices[1024]; |
|
1927 |
uint pos = 0; |
|
1928 |
while (pos < 1023) { |
|
1929 |
index = matcher.indexIn(*this, index); |
|
1930 |
if (index == -1) |
|
1931 |
break; |
|
1932 |
indices[pos++] = index; |
|
1933 |
index += blen; |
|
1934 |
// avoid infinite loop |
|
1935 |
if (!blen) |
|
1936 |
index++; |
|
1937 |
} |
|
1938 |
if (!pos) |
|
1939 |
break; |
|
1940 |
||
1941 |
replace_helper(indices, pos, blen, after, alen); |
|
1942 |
||
1943 |
if (index == -1) |
|
1944 |
break; |
|
1945 |
// index has to be adjusted in case we get back into the loop above. |
|
1946 |
index += pos*(alen-blen); |
|
1947 |
} |
|
1948 |
||
1949 |
return *this; |
|
1950 |
} |
|
1951 |
||
1952 |
/*! |
|
1953 |
\overload replace() |
|
1954 |
Replaces every occurrence of the character \a ch in the string with |
|
1955 |
\a after and returns a reference to this string. |
|
1956 |
||
1957 |
If \a cs is Qt::CaseSensitive (default), the search is case |
|
1958 |
sensitive; otherwise the search is case insensitive. |
|
1959 |
*/ |
|
1960 |
QString& QString::replace(QChar ch, const QString &after, Qt::CaseSensitivity cs) |
|
1961 |
{ |
|
1962 |
if (after.d->size == 0) |
|
1963 |
return remove(ch, cs); |
|
1964 |
||
1965 |
if (after.d->size == 1) |
|
1966 |
return replace(ch, after.d->data[0], cs); |
|
1967 |
||
1968 |
if (d->size == 0) |
|
1969 |
return *this; |
|
1970 |
||
1971 |
ushort cc = (cs == Qt::CaseSensitive ? ch.unicode() : ch.toCaseFolded().unicode()); |
|
1972 |
||
1973 |
int index = 0; |
|
1974 |
while (1) { |
|
1975 |
uint indices[1024]; |
|
1976 |
uint pos = 0; |
|
1977 |
if (cs == Qt::CaseSensitive) { |
|
1978 |
while (pos < 1023 && index < d->size) { |
|
1979 |
if (d->data[index] == cc) |
|
1980 |
indices[pos++] = index; |
|
1981 |
index++; |
|
1982 |
} |
|
1983 |
} else { |
|
1984 |
while (pos < 1023 && index < d->size) { |
|
1985 |
if (QChar::toCaseFolded(d->data[index]) == cc) |
|
1986 |
indices[pos++] = index; |
|
1987 |
index++; |
|
1988 |
} |
|
1989 |
} |
|
1990 |
if (!pos) |
|
1991 |
break; |
|
1992 |
||
1993 |
replace_helper(indices, pos, 1, after.constData(), after.d->size); |
|
1994 |
||
1995 |
if (index == -1) |
|
1996 |
break; |
|
1997 |
// index has to be adjusted in case we get back into the loop above. |
|
1998 |
index += pos*(after.d->size - 1); |
|
1999 |
} |
|
2000 |
return *this; |
|
2001 |
} |
|
2002 |
||
2003 |
/*! |
|
2004 |
\overload replace() |
|
2005 |
Replaces every occurrence of the character \a before with the |
|
2006 |
character \a after and returns a reference to this string. |
|
2007 |
||
2008 |
If \a cs is Qt::CaseSensitive (default), the search is case |
|
2009 |
sensitive; otherwise the search is case insensitive. |
|
2010 |
*/ |
|
2011 |
QString& QString::replace(QChar before, QChar after, Qt::CaseSensitivity cs) |
|
2012 |
{ |
|
2013 |
ushort a = after.unicode(); |
|
2014 |
ushort b = before.unicode(); |
|
2015 |
if (d->size) { |
|
2016 |
detach(); |
|
2017 |
ushort *i = d->data; |
|
2018 |
const ushort *e = i + d->size; |
|
2019 |
if (cs == Qt::CaseSensitive) { |
|
2020 |
for (; i != e; ++i) |
|
2021 |
if (*i == b) |
|
2022 |
*i = a; |
|
2023 |
} else { |
|
2024 |
b = foldCase(b); |
|
2025 |
for (; i != e; ++i) |
|
2026 |
if (foldCase(*i) == b) |
|
2027 |
*i = a; |
|
2028 |
} |
|
2029 |
} |
|
2030 |
return *this; |
|
2031 |
} |
|
2032 |
||
2033 |
/*! |
|
2034 |
\since 4.5 |
|
2035 |
\overload replace() |
|
2036 |
||
2037 |
Replaces every occurrence of the string \a before with the string \a |
|
2038 |
after and returns a reference to this string. |
|
2039 |
||
2040 |
If \a cs is Qt::CaseSensitive (default), the search is case |
|
2041 |
sensitive; otherwise the search is case insensitive. |
|
2042 |
||
2043 |
\note The text is not rescanned after a replacement. |
|
2044 |
*/ |
|
2045 |
QString &QString::replace(const QLatin1String &before, |
|
2046 |
const QLatin1String &after, |
|
2047 |
Qt::CaseSensitivity cs) |
|
2048 |
{ |
|
2049 |
int alen = qstrlen(after.latin1()); |
|
2050 |
QVarLengthArray<ushort> a(alen); |
|
2051 |
for (int i = 0; i < alen; ++i) |
|
2052 |
a[i] = (uchar)after.latin1()[i]; |
|
2053 |
int blen = qstrlen(before.latin1()); |
|
2054 |
QVarLengthArray<ushort> b(blen); |
|
2055 |
for (int i = 0; i < blen; ++i) |
|
2056 |
b[i] = (uchar)before.latin1()[i]; |
|
2057 |
return replace((const QChar *)b.data(), blen, (const QChar *)a.data(), alen, cs); |
|
2058 |
} |
|
2059 |
||
2060 |
/*! |
|
2061 |
\since 4.5 |
|
2062 |
\overload replace() |
|
2063 |
||
2064 |
Replaces every occurrence of the string \a before with the string \a |
|
2065 |
after and returns a reference to this string. |
|
2066 |
||
2067 |
If \a cs is Qt::CaseSensitive (default), the search is case |
|
2068 |
sensitive; otherwise the search is case insensitive. |
|
2069 |
||
2070 |
\note The text is not rescanned after a replacement. |
|
2071 |
*/ |
|
2072 |
QString &QString::replace(const QLatin1String &before, |
|
2073 |
const QString &after, |
|
2074 |
Qt::CaseSensitivity cs) |
|
2075 |
{ |
|
2076 |
int blen = qstrlen(before.latin1()); |
|
2077 |
QVarLengthArray<ushort> b(blen); |
|
2078 |
for (int i = 0; i < blen; ++i) |
|
2079 |
b[i] = (uchar)before.latin1()[i]; |
|
2080 |
return replace((const QChar *)b.data(), blen, after.constData(), after.d->size, cs); |
|
2081 |
} |
|
2082 |
||
2083 |
/*! |
|
2084 |
\since 4.5 |
|
2085 |
\overload replace() |
|
2086 |
||
2087 |
Replaces every occurrence of the string \a before with the string \a |
|
2088 |
after and returns a reference to this string. |
|
2089 |
||
2090 |
If \a cs is Qt::CaseSensitive (default), the search is case |
|
2091 |
sensitive; otherwise the search is case insensitive. |
|
2092 |
||
2093 |
\note The text is not rescanned after a replacement. |
|
2094 |
*/ |
|
2095 |
QString &QString::replace(const QString &before, |
|
2096 |
const QLatin1String &after, |
|
2097 |
Qt::CaseSensitivity cs) |
|
2098 |
{ |
|
2099 |
int alen = qstrlen(after.latin1()); |
|
2100 |
QVarLengthArray<ushort> a(alen); |
|
2101 |
for (int i = 0; i < alen; ++i) |
|
2102 |
a[i] = (uchar)after.latin1()[i]; |
|
2103 |
return replace(before.constData(), before.d->size, (const QChar *)a.data(), alen, cs); |
|
2104 |
} |
|
2105 |
||
2106 |
/*! |
|
2107 |
\since 4.5 |
|
2108 |
\overload replace() |
|
2109 |
||
2110 |
Replaces every occurrence of the character \a c with the string \a |
|
2111 |
after and returns a reference to this string. |
|
2112 |
||
2113 |
If \a cs is Qt::CaseSensitive (default), the search is case |
|
2114 |
sensitive; otherwise the search is case insensitive. |
|
2115 |
||
2116 |
\note The text is not rescanned after a replacement. |
|
2117 |
*/ |
|
2118 |
QString &QString::replace(QChar c, const QLatin1String &after, Qt::CaseSensitivity cs) |
|
2119 |
{ |
|
2120 |
int alen = qstrlen(after.latin1()); |
|
2121 |
QVarLengthArray<ushort> a(alen); |
|
2122 |
for (int i = 0; i < alen; ++i) |
|
2123 |
a[i] = (uchar)after.latin1()[i]; |
|
2124 |
return replace(&c, 1, (const QChar *)a.data(), alen, cs); |
|
2125 |
} |
|
2126 |
||
2127 |
||
2128 |
/*! |
|
2129 |
Returns true if string \a other is equal to this string; otherwise |
|
2130 |
returns false. |
|
2131 |
||
2132 |
The comparison is based exclusively on the numeric Unicode values of |
|
2133 |
the characters and is very fast, but is not what a human would |
|
2134 |
expect. Consider sorting user-interface strings with |
|
2135 |
localeAwareCompare(). |
|
2136 |
*/ |
|
2137 |
bool QString::operator==(const QString &other) const |
|
2138 |
{ |
|
2139 |
if (d->size != other.d->size) |
|
2140 |
return false; |
|
2141 |
||
2142 |
return qMemEquals(d->data, other.d->data, d->size); |
|
2143 |
} |
|
2144 |
||
2145 |
/*! |
|
2146 |
\overload operator==() |
|
2147 |
*/ |
|
2148 |
bool QString::operator==(const QLatin1String &other) const |
|
2149 |
{ |
|
2150 |
const ushort *uc = d->data; |
|
2151 |
const ushort *e = uc + d->size; |
|
2152 |
const uchar *c = (uchar *)other.latin1(); |
|
2153 |
||
2154 |
if (!c) |
|
2155 |
return isEmpty(); |
|
2156 |
||
2157 |
while (*c) { |
|
2158 |
if (uc == e || *uc != *c) |
|
2159 |
return false; |
|
2160 |
++uc; |
|
2161 |
++c; |
|
2162 |
} |
|
2163 |
return (uc == e); |
|
2164 |
} |
|
2165 |
||
2166 |
/*! \fn bool QString::operator==(const QByteArray &other) const |
|
2167 |
||
2168 |
\overload operator==() |
|
2169 |
||
2170 |
The \a other byte array is converted to a QString using the |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
2171 |
fromAscii() function. This function stops conversion at the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
2172 |
first NUL character found, or the end of the byte array. |
0 | 2173 |
|
2174 |
You can disable this operator by defining \c |
|
2175 |
QT_NO_CAST_FROM_ASCII when you compile your applications. This |
|
2176 |
can be useful if you want to ensure that all user-visible strings |
|
2177 |
go through QObject::tr(), for example. |
|
2178 |
*/ |
|
2179 |
||
2180 |
/*! \fn bool QString::operator==(const char *other) const |
|
2181 |
||
2182 |
\overload operator==() |
|
2183 |
||
2184 |
The \a other const char pointer is converted to a QString using |
|
2185 |
the fromAscii() function. |
|
2186 |
||
2187 |
You can disable this operator by defining \c |
|
2188 |
QT_NO_CAST_FROM_ASCII when you compile your applications. This |
|
2189 |
can be useful if you want to ensure that all user-visible strings |
|
2190 |
go through QObject::tr(), for example. |
|
2191 |
*/ |
|
2192 |
||
2193 |
/*! |
|
2194 |
Returns true if this string is lexically less than string \a |
|
2195 |
other; otherwise returns false. |
|
2196 |
||
2197 |
The comparison is based exclusively on the numeric Unicode values |
|
2198 |
of the characters and is very fast, but is not what a human would |
|
2199 |
expect. Consider sorting user-interface strings using the |
|
2200 |
QString::localeAwareCompare() function. |
|
2201 |
*/ |
|
2202 |
bool QString::operator<(const QString &other) const |
|
2203 |
{ |
|
2204 |
return ucstrcmp(constData(), length(), other.constData(), other.length()) < 0; |
|
2205 |
} |
|
2206 |
||
2207 |
/*! |
|
2208 |
\overload operator<() |
|
2209 |
*/ |
|
2210 |
bool QString::operator<(const QLatin1String &other) const |
|
2211 |
{ |
|
2212 |
const ushort *uc = d->data; |
|
2213 |
const ushort *e = uc + d->size; |
|
2214 |
const uchar *c = (uchar *) other.latin1(); |
|
2215 |
||
2216 |
if (!c || *c == 0) |
|
2217 |
return false; |
|
2218 |
||
2219 |
while (*c) { |
|
2220 |
if (uc == e || *uc != *c) |
|
2221 |
break; |
|
2222 |
++uc; |
|
2223 |
++c; |
|
2224 |
} |
|
2225 |
return (uc == e ? *c : *uc < *c); |
|
2226 |
} |
|
2227 |
||
2228 |
/*! \fn bool QString::operator<(const QByteArray &other) const |
|
2229 |
||
2230 |
\overload operator<() |
|
2231 |
||
2232 |
The \a other byte array is converted to a QString using the |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
2233 |
fromAscii() function. If any NUL characters ('\0') are embedded |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
2234 |
in the byte array, they will be included in the transformation. |
0 | 2235 |
|
2236 |
You can disable this operator by defining \c |
|
2237 |
QT_NO_CAST_FROM_ASCII when you compile your applications. This |
|
2238 |
can be useful if you want to ensure that all user-visible strings |
|
2239 |
go through QObject::tr(), for example. |
|
2240 |
*/ |
|
2241 |
||
2242 |
/*! \fn bool QString::operator<(const char *other) const |
|
2243 |
||
2244 |
\overload operator<() |
|
2245 |
||
2246 |
The \a other const char pointer is converted to a QString using |
|
2247 |
the fromAscii() function. |
|
2248 |
||
2249 |
You can disable this operator by defining \c |
|
2250 |
QT_NO_CAST_FROM_ASCII when you compile your applications. This |
|
2251 |
can be useful if you want to ensure that all user-visible strings |
|
2252 |
go through QObject::tr(), for example. |
|
2253 |
*/ |
|
2254 |
||
2255 |
/*! \fn bool QString::operator<=(const QString &other) const |
|
2256 |
||
2257 |
Returns true if this string is lexically less than or equal to |
|
2258 |
string \a other; otherwise returns false. |
|
2259 |
||
2260 |
The comparison is based exclusively on the numeric Unicode values |
|
2261 |
of the characters and is very fast, but is not what a human would |
|
2262 |
expect. Consider sorting user-interface strings with |
|
2263 |
localeAwareCompare(). |
|
2264 |
*/ |
|
2265 |
||
2266 |
/*! \fn bool QString::operator<=(const QLatin1String &other) const |
|
2267 |
||
2268 |
\overload operator<=() |
|
2269 |
*/ |
|
2270 |
||
2271 |
/*! \fn bool QString::operator<=(const QByteArray &other) const |
|
2272 |
||
2273 |
\overload operator<=() |
|
2274 |
||
2275 |
The \a other byte array is converted to a QString using the |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
2276 |
fromAscii() function. If any NUL characters ('\0') are embedded |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
2277 |
in the byte array, they will be included in the transformation. |
0 | 2278 |
|
2279 |
You can disable this operator by defining \c |
|
2280 |
QT_NO_CAST_FROM_ASCII when you compile your applications. This |
|
2281 |
can be useful if you want to ensure that all user-visible strings |
|
2282 |
go through QObject::tr(), for example. |
|
2283 |
*/ |
|
2284 |
||
2285 |
/*! \fn bool QString::operator<=(const char *other) const |
|
2286 |
||
2287 |
\overload operator<=() |
|
2288 |
||
2289 |
The \a other const char pointer is converted to a QString using |
|
2290 |
the fromAscii() function. |
|
2291 |
||
2292 |
You can disable this operator by defining \c |
|
2293 |
QT_NO_CAST_FROM_ASCII when you compile your applications. This |
|
2294 |
can be useful if you want to ensure that all user-visible strings |
|
2295 |
go through QObject::tr(), for example. |
|
2296 |
*/ |
|
2297 |
||
2298 |
/*! \fn bool QString::operator>(const QString &other) const |
|
2299 |
||
2300 |
Returns true if this string is lexically greater than string \a |
|
2301 |
other; otherwise returns false. |
|
2302 |
||
2303 |
The comparison is based exclusively on the numeric Unicode values |
|
2304 |
of the characters and is very fast, but is not what a human would |
|
2305 |
expect. Consider sorting user-interface strings with |
|
2306 |
localeAwareCompare(). |
|
2307 |
*/ |
|
2308 |
||
2309 |
/*! |
|
2310 |
\overload operator>() |
|
2311 |
*/ |
|
2312 |
bool QString::operator>(const QLatin1String &other) const |
|
2313 |
{ |
|
2314 |
const ushort *uc = d->data;; |
|
2315 |
const ushort *e = uc + d->size; |
|
2316 |
const uchar *c = (uchar *) other.latin1(); |
|
2317 |
||
2318 |
if (!c || *c == '\0') |
|
2319 |
return !isEmpty(); |
|
2320 |
||
2321 |
while (*c) { |
|
2322 |
if (uc == e || *uc != *c) |
|
2323 |
break; |
|
2324 |
++uc; |
|
2325 |
++c; |
|
2326 |
} |
|
2327 |
return (uc == e ? false : *uc > *c); |
|
2328 |
} |
|
2329 |
||
2330 |
/*! \fn bool QString::operator>(const QByteArray &other) const |
|
2331 |
||
2332 |
\overload operator>() |
|
2333 |
||
2334 |
The \a other byte array is converted to a QString using the |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
2335 |
fromAscii() function. If any NUL characters ('\0') are embedded |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
2336 |
in the byte array, they will be included in the transformation. |
0 | 2337 |
|
2338 |
You can disable this operator by defining \c |
|
2339 |
QT_NO_CAST_FROM_ASCII when you compile your applications. This |
|
2340 |
can be useful if you want to ensure that all user-visible strings |
|
2341 |
go through QObject::tr(), for example. |
|
2342 |
*/ |
|
2343 |
||
2344 |
/*! \fn bool QString::operator>(const char *other) const |
|
2345 |
||
2346 |
\overload operator>() |
|
2347 |
||
2348 |
The \a other const char pointer is converted to a QString using |
|
2349 |
the fromAscii() function. |
|
2350 |
||
2351 |
You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII |
|
2352 |
when you compile your applications. This can be useful if you want |
|
2353 |
to ensure that all user-visible strings go through QObject::tr(), |
|
2354 |
for example. |
|
2355 |
*/ |
|
2356 |
||
2357 |
/*! \fn bool QString::operator>=(const QString &other) const |
|
2358 |
||
2359 |
Returns true if this string is lexically greater than or equal to |
|
2360 |
string \a other; otherwise returns false. |
|
2361 |
||
2362 |
The comparison is based exclusively on the numeric Unicode values |
|
2363 |
of the characters and is very fast, but is not what a human would |
|
2364 |
expect. Consider sorting user-interface strings with |
|
2365 |
localeAwareCompare(). |
|
2366 |
*/ |
|
2367 |
||
2368 |
/*! \fn bool QString::operator>=(const QLatin1String &other) const |
|
2369 |
||
2370 |
\overload operator>=() |
|
2371 |
*/ |
|
2372 |
||
2373 |
/*! \fn bool QString::operator>=(const QByteArray &other) const |
|
2374 |
||
2375 |
\overload operator>=() |
|
2376 |
||
2377 |
The \a other byte array is converted to a QString using the |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
2378 |
fromAscii() function. If any NUL characters ('\0') are embedded in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
2379 |
the byte array, they will be included in the transformation. |
0 | 2380 |
|
2381 |
You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII |
|
2382 |
when you compile your applications. This can be useful if you want |
|
2383 |
to ensure that all user-visible strings go through QObject::tr(), |
|
2384 |
for example. |
|
2385 |
*/ |
|
2386 |
||
2387 |
/*! \fn bool QString::operator>=(const char *other) const |
|
2388 |
||
2389 |
\overload operator>=() |
|
2390 |
||
2391 |
The \a other const char pointer is converted to a QString using |
|
2392 |
the fromAscii() function. |
|
2393 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
2394 |
You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
2395 |
when you compile your applications. This can be useful if you want |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
2396 |
to ensure that all user-visible strings go through QObject::tr(), |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
2397 |
for example. |
0 | 2398 |
*/ |
2399 |
||
2400 |
/*! \fn bool QString::operator!=(const QString &other) const |
|
2401 |
||
2402 |
Returns true if this string is not equal to string \a other; |
|
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!=(const 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 |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
2421 |
fromAscii() function. If any NUL characters ('\0') are embedded |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
2422 |
in the byte array, they will be included in the transformation. |
0 | 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 fromAscii() 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 doc/src/snippets/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 |
*/ |
|
2460 |
int 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 doc/src/snippets/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 |
int QString::indexOf(const QLatin1String &str, int from, Qt::CaseSensitivity cs) const |
|
2484 |
{ |
|
2485 |
int len = qstrlen(str.latin1()); |
|
2486 |
QVarLengthArray<ushort> s(len); |
|
2487 |
for (int i = 0; i < len; ++i) |
|
2488 |
s[i] = str.latin1()[i]; |
|
2489 |
||
2490 |
return qFindString(unicode(), length(), from, (const QChar *)s.data(), len, cs); |
|
2491 |
} |
|
2492 |
||
2493 |
int qFindString( |
|
2494 |
const QChar *haystack0, int haystackLen, int from, |
|
2495 |
const QChar *needle0, int needleLen, Qt::CaseSensitivity cs) |
|
2496 |
{ |
|
2497 |
const int l = haystackLen; |
|
2498 |
const int sl = needleLen; |
|
2499 |
if (from < 0) |
|
2500 |
from += l; |
|
2501 |
if (uint(sl + from) > (uint)l) |
|
2502 |
return -1; |
|
2503 |
if (!sl) |
|
2504 |
return from; |
|
2505 |
if (!l) |
|
2506 |
return -1; |
|
2507 |
||
2508 |
if (sl == 1) |
|
2509 |
return findChar(haystack0, haystackLen, needle0[0], from, cs); |
|
2510 |
||
2511 |
/* |
|
2512 |
We use the Boyer-Moore algorithm in cases where the overhead |
|
2513 |
for the skip table should pay off, otherwise we use a simple |
|
2514 |
hash function. |
|
2515 |
*/ |
|
2516 |
if (l > 500 && sl > 5) |
|
2517 |
return qFindStringBoyerMoore(haystack0, haystackLen, from, |
|
2518 |
needle0, needleLen, cs); |
|
2519 |
||
2520 |
/* |
|
2521 |
We use some hashing for efficiency's sake. Instead of |
|
2522 |
comparing strings, we compare the hash value of str with that |
|
2523 |
of a part of this QString. Only if that matches, we call |
|
2524 |
ucstrncmp() or ucstrnicmp(). |
|
2525 |
*/ |
|
2526 |
const ushort *needle = (const ushort *)needle0; |
|
2527 |
const ushort *haystack = (const ushort *)haystack0 + from; |
|
2528 |
const ushort *end = (const ushort *)haystack0 + (l-sl); |
|
2529 |
const int sl_minus_1 = sl-1; |
|
2530 |
int hashNeedle = 0, hashHaystack = 0, idx; |
|
2531 |
||
2532 |
if (cs == Qt::CaseSensitive) { |
|
2533 |
for (idx = 0; idx < sl; ++idx) { |
|
2534 |
hashNeedle = ((hashNeedle<<1) + needle[idx]); |
|
2535 |
hashHaystack = ((hashHaystack<<1) + haystack[idx]); |
|
2536 |
} |
|
2537 |
hashHaystack -= haystack[sl_minus_1]; |
|
2538 |
||
2539 |
while (haystack <= end) { |
|
2540 |
hashHaystack += haystack[sl_minus_1]; |
|
2541 |
if (hashHaystack == hashNeedle |
|
2542 |
&& ucstrncmp((const QChar *)needle, (const QChar *)haystack, sl) == 0) |
|
2543 |
return haystack - (const ushort *)haystack0; |
|
2544 |
||
2545 |
REHASH(*haystack); |
|
2546 |
++haystack; |
|
2547 |
} |
|
2548 |
} else { |
|
2549 |
const ushort *haystack_start = (const ushort *)haystack0; |
|
2550 |
for (idx = 0; idx < sl; ++idx) { |
|
2551 |
hashNeedle = (hashNeedle<<1) + foldCase(needle + idx, needle); |
|
2552 |
hashHaystack = (hashHaystack<<1) + foldCase(haystack + idx, haystack_start); |
|
2553 |
} |
|
2554 |
hashHaystack -= foldCase(haystack + sl_minus_1, haystack_start); |
|
2555 |
||
2556 |
while (haystack <= end) { |
|
2557 |
hashHaystack += foldCase(haystack + sl_minus_1, haystack_start); |
|
2558 |
if (hashHaystack == hashNeedle && ucstrnicmp(needle, haystack, sl) == 0) |
|
2559 |
return haystack - (const ushort *)haystack0; |
|
2560 |
||
2561 |
REHASH(foldCase(haystack, haystack_start)); |
|
2562 |
++haystack; |
|
2563 |
} |
|
2564 |
} |
|
2565 |
return -1; |
|
2566 |
} |
|
2567 |
||
2568 |
/*! |
|
2569 |
\overload indexOf() |
|
2570 |
||
2571 |
Returns the index position of the first occurrence of the |
|
2572 |
character \a ch in the string, searching forward from index |
|
2573 |
position \a from. Returns -1 if \a ch could not be found. |
|
2574 |
*/ |
|
2575 |
int QString::indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const |
|
2576 |
{ |
|
2577 |
return findChar(unicode(), length(), ch, from, cs); |
|
2578 |
} |
|
2579 |
||
2580 |
static int lastIndexOfHelper(const ushort *haystack, int from, const ushort *needle, int sl, Qt::CaseSensitivity cs) |
|
2581 |
{ |
|
2582 |
/* |
|
2583 |
See indexOf() for explanations. |
|
2584 |
*/ |
|
2585 |
||
2586 |
const ushort *end = haystack; |
|
2587 |
haystack += from; |
|
2588 |
const int sl_minus_1 = sl-1; |
|
2589 |
const ushort *n = needle+sl_minus_1; |
|
2590 |
const ushort *h = haystack+sl_minus_1; |
|
2591 |
int hashNeedle = 0, hashHaystack = 0, idx; |
|
2592 |
||
2593 |
if (cs == Qt::CaseSensitive) { |
|
2594 |
for (idx = 0; idx < sl; ++idx) { |
|
2595 |
hashNeedle = ((hashNeedle<<1) + *(n-idx)); |
|
2596 |
hashHaystack = ((hashHaystack<<1) + *(h-idx)); |
|
2597 |
} |
|
2598 |
hashHaystack -= *haystack; |
|
2599 |
||
2600 |
while (haystack >= end) { |
|
2601 |
hashHaystack += *haystack; |
|
2602 |
if (hashHaystack == hashNeedle |
|
2603 |
&& ucstrncmp((const QChar *)needle, (const QChar *)haystack, sl) == 0) |
|
2604 |
return haystack - end; |
|
2605 |
--haystack; |
|
2606 |
REHASH(haystack[sl]); |
|
2607 |
} |
|
2608 |
} else { |
|
2609 |
for (idx = 0; idx < sl; ++idx) { |
|
2610 |
hashNeedle = ((hashNeedle<<1) + foldCase(n-idx, needle)); |
|
2611 |
hashHaystack = ((hashHaystack<<1) + foldCase(h-idx, end)); |
|
2612 |
} |
|
2613 |
hashHaystack -= foldCase(haystack, end); |
|
2614 |
||
2615 |
while (haystack >= end) { |
|
2616 |
hashHaystack += foldCase(haystack, end); |
|
2617 |
if (hashHaystack == hashNeedle && ucstrnicmp(needle, haystack, sl) == 0) |
|
2618 |
return haystack - end; |
|
2619 |
--haystack; |
|
2620 |
REHASH(foldCase(haystack + sl, end)); |
|
2621 |
} |
|
2622 |
} |
|
2623 |
return -1; |
|
2624 |
} |
|
2625 |
||
2626 |
/*! |
|
2627 |
Returns the index position of the last occurrence of the string \a |
|
2628 |
str in this string, searching backward from index position \a |
|
2629 |
from. If \a from is -1 (default), the search starts at the last |
|
2630 |
character; if \a from is -2, at the next to last character and so |
|
2631 |
on. Returns -1 if \a str is not found. |
|
2632 |
||
2633 |
If \a cs is Qt::CaseSensitive (default), the search is case |
|
2634 |
sensitive; otherwise the search is case insensitive. |
|
2635 |
||
2636 |
Example: |
|
2637 |
||
2638 |
\snippet doc/src/snippets/qstring/main.cpp 29 |
|
2639 |
||
2640 |
\sa indexOf(), contains(), count() |
|
2641 |
*/ |
|
2642 |
int QString::lastIndexOf(const QString &str, int from, Qt::CaseSensitivity cs) const |
|
2643 |
{ |
|
2644 |
const int sl = str.d->size; |
|
2645 |
if (sl == 1) |
|
2646 |
return lastIndexOf(QChar(str.d->data[0]), from, cs); |
|
2647 |
||
2648 |
const int l = d->size; |
|
2649 |
if (from < 0) |
|
2650 |
from += l; |
|
2651 |
int delta = l-sl; |
|
2652 |
if (from == l && sl == 0) |
|
2653 |
return from; |
|
2654 |
if (from < 0 || from >= l || delta < 0) |
|
2655 |
return -1; |
|
2656 |
if (from > delta) |
|
2657 |
from = delta; |
|
2658 |
||
2659 |
||
2660 |
return lastIndexOfHelper(d->data, from, str.d->data, str.d->size, cs); |
|
2661 |
} |
|
2662 |
||
2663 |
/*! |
|
2664 |
\since 4.5 |
|
2665 |
Returns the index position of the last occurrence of the string \a |
|
2666 |
str in this string, searching backward from index position \a |
|
2667 |
from. If \a from is -1 (default), the search starts at the last |
|
2668 |
character; if \a from is -2, at the next to last character and so |
|
2669 |
on. Returns -1 if \a str is not found. |
|
2670 |
||
2671 |
If \a cs is Qt::CaseSensitive (default), the search is case |
|
2672 |
sensitive; otherwise the search is case insensitive. |
|
2673 |
||
2674 |
Example: |
|
2675 |
||
2676 |
\snippet doc/src/snippets/qstring/main.cpp 29 |
|
2677 |
||
2678 |
\sa indexOf(), contains(), count() |
|
2679 |
*/ |
|
2680 |
int QString::lastIndexOf(const QLatin1String &str, int from, Qt::CaseSensitivity cs) const |
|
2681 |
{ |
|
2682 |
const int sl = qstrlen(str.latin1()); |
|
2683 |
if (sl == 1) |
|
2684 |
return lastIndexOf(QLatin1Char(str.latin1()[0]), from, cs); |
|
2685 |
||
2686 |
const int l = d->size; |
|
2687 |
if (from < 0) |
|
2688 |
from += l; |
|
2689 |
int delta = l-sl; |
|
2690 |
if (from == l && sl == 0) |
|
2691 |
return from; |
|
2692 |
if (from < 0 || from >= l || delta < 0) |
|
2693 |
return -1; |
|
2694 |
if (from > delta) |
|
2695 |
from = delta; |
|
2696 |
||
2697 |
QVarLengthArray<ushort> s(sl); |
|
2698 |
for (int i = 0; i < sl; ++i) |
|
2699 |
s[i] = str.latin1()[i]; |
|
2700 |
||
2701 |
return lastIndexOfHelper(d->data, from, s.data(), sl, cs); |
|
2702 |
} |
|
2703 |
||
2704 |
/*! |
|
2705 |
\overload lastIndexOf() |
|
2706 |
||
2707 |
Returns the index position of the last occurrence of the character |
|
2708 |
\a ch, searching backward from position \a from. |
|
2709 |
*/ |
|
2710 |
int QString::lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const |
|
2711 |
{ |
|
2712 |
ushort c = ch.unicode(); |
|
2713 |
if (from < 0) |
|
2714 |
from += d->size; |
|
2715 |
if (from < 0 || from >= d->size) |
|
2716 |
return -1; |
|
2717 |
if (from >= 0) { |
|
2718 |
const ushort *n = d->data + from; |
|
2719 |
const ushort *b = d->data; |
|
2720 |
if (cs == Qt::CaseSensitive) { |
|
2721 |
for (; n >= b; --n) |
|
2722 |
if (*n == c) |
|
2723 |
return n - b; |
|
2724 |
} else { |
|
2725 |
c = foldCase(c); |
|
2726 |
for (; n >= b; --n) |
|
2727 |
if (foldCase(*n) == c) |
|
2728 |
return n - b; |
|
2729 |
} |
|
2730 |
} |
|
2731 |
return -1; |
|
2732 |
} |
|
2733 |
||
2734 |
#ifndef QT_NO_REGEXP |
|
2735 |
struct QStringCapture |
|
2736 |
{ |
|
2737 |
int pos; |
|
2738 |
int len; |
|
2739 |
int no; |
|
2740 |
}; |
|
2741 |
||
2742 |
/*! |
|
2743 |
\overload replace() |
|
2744 |
||
2745 |
Replaces every occurrence of the regular expression \a rx in the |
|
2746 |
string with \a after. Returns a reference to the string. For |
|
2747 |
example: |
|
2748 |
||
2749 |
\snippet doc/src/snippets/qstring/main.cpp 42 |
|
2750 |
||
2751 |
For regular expressions containing \l{capturing parentheses}, |
|
2752 |
occurrences of \bold{\\1}, \bold{\\2}, ..., in \a after are replaced |
|
2753 |
with \a{rx}.cap(1), cap(2), ... |
|
2754 |
||
2755 |
\snippet doc/src/snippets/qstring/main.cpp 43 |
|
2756 |
||
2757 |
\sa indexOf(), lastIndexOf(), remove(), QRegExp::cap() |
|
2758 |
*/ |
|
2759 |
QString& QString::replace(const QRegExp &rx, const QString &after) |
|
2760 |
{ |
|
2761 |
QRegExp rx2(rx); |
|
2762 |
||
2763 |
if (isEmpty() && rx2.indexIn(*this) == -1) |
|
2764 |
return *this; |
|
2765 |
||
2766 |
realloc(); |
|
2767 |
||
2768 |
int index = 0; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2769 |
int numCaptures = rx2.captureCount(); |
0 | 2770 |
int al = after.length(); |
2771 |
QRegExp::CaretMode caretMode = QRegExp::CaretAtZero; |
|
2772 |
||
2773 |
if (numCaptures > 0) { |
|
2774 |
const QChar *uc = after.unicode(); |
|
2775 |
int numBackRefs = 0; |
|
2776 |
||
2777 |
for (int i = 0; i < al - 1; i++) { |
|
2778 |
if (uc[i] == QLatin1Char('\\')) { |
|
2779 |
int no = uc[i + 1].digitValue(); |
|
2780 |
if (no > 0 && no <= numCaptures) |
|
2781 |
numBackRefs++; |
|
2782 |
} |
|
2783 |
} |
|
2784 |
||
2785 |
/* |
|
2786 |
This is the harder case where we have back-references. |
|
2787 |
*/ |
|
2788 |
if (numBackRefs > 0) { |
|
2789 |
QVarLengthArray<QStringCapture, 16> captures(numBackRefs); |
|
2790 |
int j = 0; |
|
2791 |
||
2792 |
for (int i = 0; i < al - 1; i++) { |
|
2793 |
if (uc[i] == QLatin1Char('\\')) { |
|
2794 |
int no = uc[i + 1].digitValue(); |
|
2795 |
if (no > 0 && no <= numCaptures) { |
|
2796 |
QStringCapture capture; |
|
2797 |
capture.pos = i; |
|
2798 |
capture.len = 2; |
|
2799 |
||
2800 |
if (i < al - 2) { |
|
2801 |
int secondDigit = uc[i + 2].digitValue(); |
|
2802 |
if (secondDigit != -1 && ((no * 10) + secondDigit) <= numCaptures) { |
|
2803 |
no = (no * 10) + secondDigit; |
|
2804 |
++capture.len; |
|
2805 |
} |
|
2806 |
} |
|
2807 |
||
2808 |
capture.no = no; |
|
2809 |
captures[j++] = capture; |
|
2810 |
} |
|
2811 |
} |
|
2812 |
} |
|
2813 |
||
2814 |
while (index <= length()) { |
|
2815 |
index = rx2.indexIn(*this, index, caretMode); |
|
2816 |
if (index == -1) |
|
2817 |
break; |
|
2818 |
||
2819 |
QString after2(after); |
|
2820 |
for (j = numBackRefs - 1; j >= 0; j--) { |
|
2821 |
const QStringCapture &capture = captures[j]; |
|
2822 |
after2.replace(capture.pos, capture.len, rx2.cap(capture.no)); |
|
2823 |
} |
|
2824 |
||
2825 |
replace(index, rx2.matchedLength(), after2); |
|
2826 |
index += after2.length(); |
|
2827 |
||
2828 |
// avoid infinite loop on 0-length matches (e.g., QRegExp("[a-z]*")) |
|
2829 |
if (rx2.matchedLength() == 0) |
|
2830 |
++index; |
|
2831 |
||
2832 |
caretMode = QRegExp::CaretWontMatch; |
|
2833 |
} |
|
2834 |
return *this; |
|
2835 |
} |
|
2836 |
} |
|
2837 |
||
2838 |
/* |
|
2839 |
This is the simple and optimized case where we don't have |
|
2840 |
back-references. |
|
2841 |
*/ |
|
2842 |
while (index != -1) { |
|
2843 |
struct { |
|
2844 |
int pos; |
|
2845 |
int length; |
|
2846 |
} replacements[2048]; |
|
2847 |
||
2848 |
int pos = 0; |
|
2849 |
int adjust = 0; |
|
2850 |
while (pos < 2047) { |
|
2851 |
index = rx2.indexIn(*this, index, caretMode); |
|
2852 |
if (index == -1) |
|
2853 |
break; |
|
2854 |
int ml = rx2.matchedLength(); |
|
2855 |
replacements[pos].pos = index; |
|
2856 |
replacements[pos++].length = ml; |
|
2857 |
index += ml; |
|
2858 |
adjust += al - ml; |
|
2859 |
// avoid infinite loop |
|
2860 |
if (!ml) |
|
2861 |
index++; |
|
2862 |
} |
|
2863 |
if (!pos) |
|
2864 |
break; |
|
2865 |
replacements[pos].pos = d->size; |
|
2866 |
int newlen = d->size + adjust; |
|
2867 |
||
2868 |
// to continue searching at the right position after we did |
|
2869 |
// the first round of replacements |
|
2870 |
if (index != -1) |
|
2871 |
index += adjust; |
|
2872 |
QString newstring; |
|
2873 |
newstring.reserve(newlen + 1); |
|
2874 |
QChar *newuc = newstring.data(); |
|
2875 |
QChar *uc = newuc; |
|
2876 |
int copystart = 0; |
|
2877 |
int i = 0; |
|
2878 |
while (i < pos) { |
|
2879 |
int copyend = replacements[i].pos; |
|
2880 |
int size = copyend - copystart; |
|
2881 |
memcpy(uc, d->data + copystart, size * sizeof(QChar)); |
|
2882 |
uc += size; |
|
2883 |
memcpy(uc, after.d->data, al * sizeof(QChar)); |
|
2884 |
uc += al; |
|
2885 |
copystart = copyend + replacements[i].length; |
|
2886 |
i++; |
|
2887 |
} |
|
2888 |
memcpy(uc, d->data + copystart, (d->size - copystart) * sizeof(QChar)); |
|
2889 |
newstring.resize(newlen); |
|
2890 |
*this = newstring; |
|
2891 |
caretMode = QRegExp::CaretWontMatch; |
|
2892 |
} |
|
2893 |
return *this; |
|
2894 |
} |
|
2895 |
#endif |
|
2896 |
||
2897 |
/*! |
|
2898 |
Returns the number of (potentially overlapping) occurrences of |
|
2899 |
the string \a str in this string. |
|
2900 |
||
2901 |
If \a cs is Qt::CaseSensitive (default), the search is |
|
2902 |
case sensitive; otherwise the search is case insensitive. |
|
2903 |
||
2904 |
\sa contains(), indexOf() |
|
2905 |
*/ |
|
2906 |
int QString::count(const QString &str, Qt::CaseSensitivity cs) const |
|
2907 |
{ |
|
2908 |
int num = 0; |
|
2909 |
int i = -1; |
|
2910 |
if (d->size > 500 && str.d->size > 5) { |
|
2911 |
QStringMatcher matcher(str, cs); |
|
2912 |
while ((i = matcher.indexIn(*this, i + 1)) != -1) |
|
2913 |
++num; |
|
2914 |
} else { |
|
2915 |
while ((i = indexOf(str, i + 1, cs)) != -1) |
|
2916 |
++num; |
|
2917 |
} |
|
2918 |
return num; |
|
2919 |
} |
|
2920 |
||
2921 |
/*! |
|
2922 |
\overload count() |
|
2923 |
||
2924 |
Returns the number of occurrences of character \a ch in the string. |
|
2925 |
*/ |
|
2926 |
int QString::count(QChar ch, Qt::CaseSensitivity cs) const |
|
2927 |
{ |
|
2928 |
ushort c = ch.unicode(); |
|
2929 |
int num = 0; |
|
2930 |
const ushort *i = d->data + d->size; |
|
2931 |
const ushort *b = d->data; |
|
2932 |
if (cs == Qt::CaseSensitive) { |
|
2933 |
while (i != b) |
|
2934 |
if (*--i == c) |
|
2935 |
++num; |
|
2936 |
} else { |
|
2937 |
c = foldCase(c); |
|
2938 |
while (i != b) |
|
2939 |
if (foldCase(*(--i)) == c) |
|
2940 |
++num; |
|
2941 |
} |
|
2942 |
return num; |
|
2943 |
} |
|
2944 |
||
2945 |
/*! \fn bool QString::contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
|
2946 |
||
2947 |
Returns true if this string contains an occurrence of the string |
|
2948 |
\a str; otherwise returns false. |
|
2949 |
||
2950 |
If \a cs is Qt::CaseSensitive (default), the search is |
|
2951 |
case sensitive; otherwise the search is case insensitive. |
|
2952 |
||
2953 |
Example: |
|
2954 |
\snippet doc/src/snippets/qstring/main.cpp 17 |
|
2955 |
||
2956 |
\sa indexOf(), count() |
|
2957 |
*/ |
|
2958 |
||
2959 |
/*! \fn bool QString::contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
|
2960 |
||
2961 |
\overload contains() |
|
2962 |
||
2963 |
Returns true if this string contains an occurrence of the |
|
2964 |
character \a ch; otherwise returns false. |
|
2965 |
*/ |
|
2966 |
||
2967 |
/*! \fn bool QString::contains(const QRegExp &rx) const |
|
2968 |
||
2969 |
\overload contains() |
|
2970 |
||
2971 |
Returns true if the regular expression \a rx matches somewhere in |
|
2972 |
this string; otherwise returns false. |
|
2973 |
*/ |
|
2974 |
||
2975 |
/*! \fn bool QString::contains(QRegExp &rx) const |
|
2976 |
\overload contains() |
|
2977 |
\since 4.5 |
|
2978 |
||
2979 |
Returns true if the regular expression \a rx matches somewhere in |
|
2980 |
this string; otherwise returns false. |
|
2981 |
||
2982 |
If there is a match, the \a rx regular expression will contain the |
|
2983 |
matched captures (see QRegExp::matchedLength, QRegExp::cap). |
|
2984 |
*/ |
|
2985 |
||
2986 |
#ifndef QT_NO_REGEXP |
|
2987 |
/*! |
|
2988 |
\overload indexOf() |
|
2989 |
||
2990 |
Returns the index position of the first match of the regular |
|
2991 |
expression \a rx in the string, searching forward from index |
|
2992 |
position \a from. Returns -1 if \a rx didn't match anywhere. |
|
2993 |
||
2994 |
Example: |
|
2995 |
||
2996 |
\snippet doc/src/snippets/qstring/main.cpp 25 |
|
2997 |
*/ |
|
2998 |
int QString::indexOf(const QRegExp& rx, int from) const |
|
2999 |
{ |
|
3000 |
QRegExp rx2(rx); |
|
3001 |
return rx2.indexIn(*this, from); |
|
3002 |
} |
|
3003 |
||
3004 |
/*! |
|
3005 |
\overload indexOf() |
|
3006 |
\since 4.5 |
|
3007 |
||
3008 |
Returns the index position of the first match of the regular |
|
3009 |
expression \a rx in the string, searching forward from index |
|
3010 |
position \a from. Returns -1 if \a rx didn't match anywhere. |
|
3011 |
||
3012 |
If there is a match, the \a rx regular expression will contain the |
|
3013 |
matched captures (see QRegExp::matchedLength, QRegExp::cap). |
|
3014 |
||
3015 |
Example: |
|
3016 |
||
3017 |
\snippet doc/src/snippets/qstring/main.cpp 25 |
|
3018 |
*/ |
|
3019 |
int QString::indexOf(QRegExp& rx, int from) const |
|
3020 |
{ |
|
3021 |
return rx.indexIn(*this, from); |
|
3022 |
} |
|
3023 |
||
3024 |
/*! |
|
3025 |
\overload lastIndexOf() |
|
3026 |
||
3027 |
Returns the index position of the last match of the regular |
|
3028 |
expression \a rx in the string, searching backward from index |
|
3029 |
position \a from. Returns -1 if \a rx didn't match anywhere. |
|
3030 |
||
3031 |
Example: |
|
3032 |
||
3033 |
\snippet doc/src/snippets/qstring/main.cpp 30 |
|
3034 |
*/ |
|
3035 |
int QString::lastIndexOf(const QRegExp& rx, int from) const |
|
3036 |
{ |
|
3037 |
QRegExp rx2(rx); |
|
3038 |
return rx2.lastIndexIn(*this, from); |
|
3039 |
} |
|
3040 |
||
3041 |
/*! |
|
3042 |
\overload lastIndexOf() |
|
3043 |
\since 4.5 |
|
3044 |
||
3045 |
Returns the index position of the last match of the regular |
|
3046 |
expression \a rx in the string, searching backward from index |
|
3047 |
position \a from. Returns -1 if \a rx didn't match anywhere. |
|
3048 |
||
3049 |
If there is a match, the \a rx regular expression will contain the |
|
3050 |
matched captures (see QRegExp::matchedLength, QRegExp::cap). |
|
3051 |
||
3052 |
Example: |
|
3053 |
||
3054 |
\snippet doc/src/snippets/qstring/main.cpp 30 |
|
3055 |
*/ |
|
3056 |
int QString::lastIndexOf(QRegExp& rx, int from) const |
|
3057 |
{ |
|
3058 |
return rx.lastIndexIn(*this, from); |
|
3059 |
} |
|
3060 |
||
3061 |
/*! |
|
3062 |
\overload count() |
|
3063 |
||
3064 |
Returns the number of times the regular expression \a rx matches |
|
3065 |
in the string. |
|
3066 |
||
3067 |
This function counts overlapping matches, so in the example |
|
3068 |
below, there are four instances of "ana" or "ama": |
|
3069 |
||
3070 |
\snippet doc/src/snippets/qstring/main.cpp 18 |
|
3071 |
||
3072 |
*/ |
|
3073 |
int QString::count(const QRegExp& rx) const |
|
3074 |
{ |
|
3075 |
QRegExp rx2(rx); |
|
3076 |
int count = 0; |
|
3077 |
int index = -1; |
|
3078 |
int len = length(); |
|
3079 |
while (index < len - 1) { // count overlapping matches |
|
3080 |
index = rx2.indexIn(*this, index + 1); |
|
3081 |
if (index == -1) |
|
3082 |
break; |
|
3083 |
count++; |
|
3084 |
} |
|
3085 |
return count; |
|
3086 |
} |
|
3087 |
#endif // QT_NO_REGEXP |
|
3088 |
||
3089 |
/*! \fn int QString::count() const |
|
3090 |
||
3091 |
\overload count() |
|
3092 |
||
3093 |
Same as size(). |
|
3094 |
*/ |
|
3095 |
||
3096 |
||
3097 |
/*! |
|
3098 |
\enum QString::SectionFlag |
|
3099 |
||
3100 |
This enum specifies flags that can be used to affect various |
|
3101 |
aspects of the section() function's behavior with respect to |
|
3102 |
separators and empty fields. |
|
3103 |
||
3104 |
\value SectionDefault Empty fields are counted, leading and |
|
3105 |
trailing separators are not included, and the separator is |
|
3106 |
compared case sensitively. |
|
3107 |
||
3108 |
\value SectionSkipEmpty Treat empty fields as if they don't exist, |
|
3109 |
i.e. they are not considered as far as \e start and \e end are |
|
3110 |
concerned. |
|
3111 |
||
3112 |
\value SectionIncludeLeadingSep Include the leading separator (if |
|
3113 |
any) in the result string. |
|
3114 |
||
3115 |
\value SectionIncludeTrailingSep Include the trailing separator |
|
3116 |
(if any) in the result string. |
|
3117 |
||
3118 |
\value SectionCaseInsensitiveSeps Compare the separator |
|
3119 |
case-insensitively. |
|
3120 |
||
3121 |
\sa section() |
|
3122 |
*/ |
|
3123 |
||
3124 |
/*! |
|
3125 |
\fn QString QString::section(QChar sep, int start, int end = -1, SectionFlags flags) const |
|
3126 |
||
3127 |
This function returns a section of the string. |
|
3128 |
||
3129 |
This string is treated as a sequence of fields separated by the |
|
3130 |
character, \a sep. The returned string consists of the fields from |
|
3131 |
position \a start to position \a end inclusive. If \a end is not |
|
3132 |
specified, all fields from position \a start to the end of the |
|
3133 |
string are included. Fields are numbered 0, 1, 2, etc., counting |
|
3134 |
from the left, and -1, -2, etc., counting from right to left. |
|
3135 |
||
3136 |
The \a flags argument can be used to affect some aspects of the |
|
3137 |
function's behavior, e.g. whether to be case sensitive, whether |
|
3138 |
to skip empty fields and how to deal with leading and trailing |
|
3139 |
separators; see \l{SectionFlags}. |
|
3140 |
||
3141 |
\snippet doc/src/snippets/qstring/main.cpp 52 |
|
3142 |
||
3143 |
If \a start or \a end is negative, we count fields from the right |
|
3144 |
of the string, the right-most field being -1, the one from |
|
3145 |
right-most field being -2, and so on. |
|
3146 |
||
3147 |
\snippet doc/src/snippets/qstring/main.cpp 53 |
|
3148 |
||
3149 |
\sa split() |
|
3150 |
*/ |
|
3151 |
||
3152 |
/*! |
|
3153 |
\overload section() |
|
3154 |
||
3155 |
\snippet doc/src/snippets/qstring/main.cpp 51 |
|
3156 |
\snippet doc/src/snippets/qstring/main.cpp 54 |
|
3157 |
||
3158 |
\sa split() |
|
3159 |
*/ |
|
3160 |
||
3161 |
QString QString::section(const QString &sep, int start, int end, SectionFlags flags) const |
|
3162 |
{ |
|
3163 |
QStringList sections = split(sep, KeepEmptyParts, |
|
3164 |
(flags & SectionCaseInsensitiveSeps) ? Qt::CaseInsensitive : Qt::CaseSensitive); |
|
3165 |
if (sections.isEmpty()) |
|
3166 |
return QString(); |
|
3167 |
if (!(flags & SectionSkipEmpty)) { |
|
3168 |
if (start < 0) |
|
3169 |
start += sections.count(); |
|
3170 |
if (end < 0) |
|
3171 |
end += sections.count(); |
|
3172 |
} else { |
|
3173 |
int skip = 0; |
|
3174 |
for (int k=0; k<sections.size(); ++k) { |
|
3175 |
if (sections.at(k).isEmpty()) |
|
3176 |
skip++; |
|
3177 |
} |
|
3178 |
if (start < 0) |
|
3179 |
start += sections.count() - skip; |
|
3180 |
if (end < 0) |
|
3181 |
end += sections.count() - skip; |
|
3182 |
} |
|
3183 |
int x = 0; |
|
3184 |
QString ret; |
|
3185 |
int first_i = start, last_i = end; |
|
3186 |
for (int i = 0; x <= end && i < sections.size(); ++i) { |
|
3187 |
QString section = sections.at(i); |
|
3188 |
const bool empty = section.isEmpty(); |
|
3189 |
if (x >= start) { |
|
3190 |
if(x == start) |
|
3191 |
first_i = i; |
|
3192 |
if(x == end) |
|
3193 |
last_i = i; |
|
3194 |
if(x > start) |
|
3195 |
ret += sep; |
|
3196 |
ret += section; |
|
3197 |
} |
|
3198 |
if (!empty || !(flags & SectionSkipEmpty)) |
|
3199 |
x++; |
|
3200 |
} |
|
3201 |
if((flags & SectionIncludeLeadingSep) && first_i) |
|
3202 |
ret.prepend(sep); |
|
3203 |
if((flags & SectionIncludeTrailingSep) && last_i < sections.size()-1) |
|
3204 |
ret += sep; |
|
3205 |
return ret; |
|
3206 |
} |
|
3207 |
||
3208 |
#ifndef QT_NO_REGEXP |
|
3209 |
class qt_section_chunk { |
|
3210 |
public: |
|
3211 |
qt_section_chunk(int l, QString s) { length = l; string = s; } |
|
3212 |
int length; |
|
3213 |
QString string; |
|
3214 |
}; |
|
3215 |
||
3216 |
/*! |
|
3217 |
\overload section() |
|
3218 |
||
3219 |
This string is treated as a sequence of fields separated by the |
|
3220 |
regular expression, \a reg. |
|
3221 |
||
3222 |
\snippet doc/src/snippets/qstring/main.cpp 55 |
|
3223 |
||
3224 |
\warning Using this QRegExp version is much more expensive than |
|
3225 |
the overloaded string and character versions. |
|
3226 |
||
3227 |
\sa split() simplified() |
|
3228 |
*/ |
|
3229 |
QString QString::section(const QRegExp ®, int start, int end, SectionFlags flags) const |
|
3230 |
{ |
|
3231 |
const QChar *uc = unicode(); |
|
3232 |
if(!uc) |
|
3233 |
return QString(); |
|
3234 |
||
3235 |
QRegExp sep(reg); |
|
3236 |
sep.setCaseSensitivity((flags & SectionCaseInsensitiveSeps) ? Qt::CaseInsensitive |
|
3237 |
: Qt::CaseSensitive); |
|
3238 |
||
3239 |
QList<qt_section_chunk> sections; |
|
3240 |
int n = length(), m = 0, last_m = 0, last_len = 0; |
|
3241 |
while ((m = sep.indexIn(*this, m)) != -1) { |
|
3242 |
sections.append(qt_section_chunk(last_len, QString(uc + last_m, m - last_m))); |
|
3243 |
last_m = m; |
|
3244 |
last_len = sep.matchedLength(); |
|
3245 |
m += qMax(sep.matchedLength(), 1); |
|
3246 |
} |
|
3247 |
sections.append(qt_section_chunk(last_len, QString(uc + last_m, n - last_m))); |
|
3248 |
||
3249 |
if(start < 0) |
|
3250 |
start += sections.count(); |
|
3251 |
if(end < 0) |
|
3252 |
end += sections.count(); |
|
3253 |
||
3254 |
QString ret; |
|
3255 |
int x = 0; |
|
3256 |
int first_i = start, last_i = end; |
|
3257 |
for (int i = 0; x <= end && i < sections.size(); ++i) { |
|
3258 |
const qt_section_chunk §ion = sections.at(i); |
|
3259 |
const bool empty = (section.length == section.string.length()); |
|
3260 |
if (x >= start) { |
|
3261 |
if(x == start) |
|
3262 |
first_i = i; |
|
3263 |
if(x == end) |
|
3264 |
last_i = i; |
|
3265 |
if(x != start) |
|
3266 |
ret += section.string; |
|
3267 |
else |
|
3268 |
ret += section.string.mid(section.length); |
|
3269 |
} |
|
3270 |
if (!empty || !(flags & SectionSkipEmpty)) |
|
3271 |
x++; |
|
3272 |
} |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
3273 |
if((flags & SectionIncludeLeadingSep) && first_i < sections.size()) { |
0 | 3274 |
const qt_section_chunk §ion = sections.at(first_i); |
3275 |
ret.prepend(section.string.left(section.length)); |
|
3276 |
} |
|
3277 |
if((flags & SectionIncludeTrailingSep) && last_i+1 <= sections.size()-1) { |
|
3278 |
const qt_section_chunk §ion = sections.at(last_i+1); |
|
3279 |
ret += section.string.left(section.length); |
|
3280 |
} |
|
3281 |
return ret; |
|
3282 |
} |
|
3283 |
#endif |
|
3284 |
||
3285 |
/*! |
|
3286 |
Returns a substring that contains the \a n leftmost characters |
|
3287 |
of the string. |
|
3288 |
||
3289 |
The entire string is returned if \a n is greater than size() or |
|
3290 |
less than zero. |
|
3291 |
||
3292 |
\snippet doc/src/snippets/qstring/main.cpp 31 |
|
3293 |
||
3294 |
\sa right(), mid(), startsWith() |
|
3295 |
*/ |
|
3296 |
QString QString::left(int n) const |
|
3297 |
{ |
|
3298 |
if (n >= d->size || n < 0) |
|
3299 |
return *this; |
|
3300 |
return QString((const QChar*) d->data, n); |
|
3301 |
} |
|
3302 |
||
3303 |
/*! |
|
3304 |
Returns a substring that contains the \a n rightmost characters |
|
3305 |
of the string. |
|
3306 |
||
3307 |
The entire string is returned if \a n is greater than size() or |
|
3308 |
less than zero. |
|
3309 |
||
3310 |
\snippet doc/src/snippets/qstring/main.cpp 48 |
|
3311 |
||
3312 |
\sa left(), mid(), endsWith() |
|
3313 |
*/ |
|
3314 |
QString QString::right(int n) const |
|
3315 |
{ |
|
3316 |
if (n >= d->size || n < 0) |
|
3317 |
return *this; |
|
3318 |
return QString((const QChar*) d->data + d->size - n, n); |
|
3319 |
} |
|
3320 |
||
3321 |
/*! |
|
3322 |
Returns a string that contains \a n characters of this string, |
|
3323 |
starting at the specified \a position index. |
|
3324 |
||
3325 |
Returns a null string if the \a position index exceeds the |
|
3326 |
length of the string. If there are less than \a n characters |
|
3327 |
available in the string starting at the given \a position, or if |
|
3328 |
\a n is -1 (default), the function returns all characters that |
|
3329 |
are available from the specified \a position. |
|
3330 |
||
3331 |
Example: |
|
3332 |
||
3333 |
\snippet doc/src/snippets/qstring/main.cpp 34 |
|
3334 |
||
3335 |
\sa left(), right() |
|
3336 |
*/ |
|
3337 |
||
3338 |
QString QString::mid(int position, int n) const |
|
3339 |
{ |
|
3340 |
if (d == &shared_null || position >= d->size) |
|
3341 |
return QString(); |
|
3342 |
if (n < 0) |
|
3343 |
n = d->size - position; |
|
3344 |
if (position < 0) { |
|
3345 |
n += position; |
|
3346 |
position = 0; |
|
3347 |
} |
|
3348 |
if (n + position > d->size) |
|
3349 |
n = d->size - position; |
|
3350 |
if (position == 0 && n == d->size) |
|
3351 |
return *this; |
|
3352 |
return QString((const QChar*) d->data + position, n); |
|
3353 |
} |
|
3354 |
||
3355 |
/*! |
|
3356 |
Returns true if the string starts with \a s; otherwise returns |
|
3357 |
false. |
|
3358 |
||
3359 |
If \a cs is Qt::CaseSensitive (default), the search is |
|
3360 |
case sensitive; otherwise the search is case insensitive. |
|
3361 |
||
3362 |
\snippet doc/src/snippets/qstring/main.cpp 65 |
|
3363 |
||
3364 |
\sa endsWith() |
|
3365 |
*/ |
|
3366 |
bool QString::startsWith(const QString& s, Qt::CaseSensitivity cs) const |
|
3367 |
{ |
|
3368 |
if (d == &shared_null) |
|
3369 |
return (s.d == &shared_null); |
|
3370 |
if (d->size == 0) |
|
3371 |
return s.d->size == 0; |
|
3372 |
if (s.d->size > d->size) |
|
3373 |
return false; |
|
3374 |
if (cs == Qt::CaseSensitive) { |
|
3375 |
return qMemEquals(d->data, s.d->data, s.d->size); |
|
3376 |
} else { |
|
3377 |
uint last = 0; |
|
3378 |
uint olast = 0; |
|
3379 |
for (int i = 0; i < s.d->size; ++i) |
|
3380 |
if (foldCase(d->data[i], last) != foldCase(s.d->data[i], olast)) |
|
3381 |
return false; |
|
3382 |
} |
|
3383 |
return true; |
|
3384 |
} |
|
3385 |
||
3386 |
/*! |
|
3387 |
\overload startsWith() |
|
3388 |
*/ |
|
3389 |
bool QString::startsWith(const QLatin1String& s, Qt::CaseSensitivity cs) const |
|
3390 |
{ |
|
3391 |
if (d == &shared_null) |
|
3392 |
return (s.latin1() == 0); |
|
3393 |
if (d->size == 0) |
|
3394 |
return !s.latin1() || *s.latin1() == 0; |
|
3395 |
int slen = qstrlen(s.latin1()); |
|
3396 |
if (slen > d->size) |
|
3397 |
return false; |
|
3398 |
const uchar *latin = (const uchar *)s.latin1(); |
|
3399 |
if (cs == Qt::CaseSensitive) { |
|
3400 |
for (int i = 0; i < slen; ++i) |
|
3401 |
if (d->data[i] != latin[i]) |
|
3402 |
return false; |
|
3403 |
} else { |
|
3404 |
for (int i = 0; i < slen; ++i) |
|
3405 |
if (foldCase(d->data[i]) != foldCase((ushort)latin[i])) |
|
3406 |
return false; |
|
3407 |
} |
|
3408 |
return true; |
|
3409 |
} |
|
3410 |
||
3411 |
/*! |
|
3412 |
\overload startsWith() |
|
3413 |
||
3414 |
Returns true if the string starts with \a c; otherwise returns |
|
3415 |
false. |
|
3416 |
*/ |
|
3417 |
bool QString::startsWith(const QChar &c, Qt::CaseSensitivity cs) const |
|
3418 |
{ |
|
3419 |
return d->size |
|
3420 |
&& (cs == Qt::CaseSensitive |
|
3421 |
? d->data[0] == c |
|
3422 |
: foldCase(d->data[0]) == foldCase(c.unicode())); |
|
3423 |
} |
|
3424 |
||
3425 |
/*! |
|
3426 |
Returns true if the string ends with \a s; otherwise returns |
|
3427 |
false. |
|
3428 |
||
3429 |
If \a cs is Qt::CaseSensitive (default), the search is case |
|
3430 |
sensitive; otherwise the search is case insensitive. |
|
3431 |
||
3432 |
\snippet doc/src/snippets/qstring/main.cpp 20 |
|
3433 |
||
3434 |
\sa startsWith() |
|
3435 |
*/ |
|
3436 |
bool QString::endsWith(const QString& s, Qt::CaseSensitivity cs) const |
|
3437 |
{ |
|
3438 |
if (d == &shared_null) |
|
3439 |
return (s.d == &shared_null); |
|
3440 |
if (d->size == 0) |
|
3441 |
return s.d->size == 0; |
|
3442 |
int pos = d->size - s.d->size; |
|
3443 |
if (pos < 0) |
|
3444 |
return false; |
|
3445 |
if (cs == Qt::CaseSensitive) { |
|
3446 |
return qMemEquals(d->data + pos, s.d->data, s.d->size); |
|
3447 |
} else { |
|
3448 |
uint last = 0; |
|
3449 |
uint olast = 0; |
|
3450 |
for (int i = 0; i < s.length(); i++) |
|
3451 |
if (foldCase(d->data[pos+i], last) != foldCase(s.d->data[i], olast)) |
|
3452 |
return false; |
|
3453 |
} |
|
3454 |
return true; |
|
3455 |
} |
|
3456 |
||
3457 |
/*! |
|
3458 |
\overload endsWith() |
|
3459 |
*/ |
|
3460 |
bool QString::endsWith(const QLatin1String& s, Qt::CaseSensitivity cs) const |
|
3461 |
{ |
|
3462 |
if (d == &shared_null) |
|
3463 |
return (s.latin1() == 0); |
|
3464 |
if (d->size == 0) |
|
3465 |
return !s.latin1() || *s.latin1() == 0; |
|
3466 |
int slen = qstrlen(s.latin1()); |
|
3467 |
int pos = d->size - slen; |
|
3468 |
const uchar *latin = (const uchar *)s.latin1(); |
|
3469 |
if (pos < 0) |
|
3470 |
return false; |
|
3471 |
if (cs == Qt::CaseSensitive) { |
|
3472 |
for (int i = 0; i < slen; i++) |
|
3473 |
if (d->data[pos+i] != latin[i]) |
|
3474 |
return false; |
|
3475 |
} else { |
|
3476 |
for (int i = 0; i < slen; i++) |
|
3477 |
if (foldCase(d->data[pos+i]) != foldCase((ushort)latin[i])) |
|
3478 |
return false; |
|
3479 |
} |
|
3480 |
return true; |
|
3481 |
} |
|
3482 |
||
3483 |
/*! |
|
3484 |
Returns true if the string ends with \a c; otherwise returns |
|
3485 |
false. |
|
3486 |
||
3487 |
\overload endsWith() |
|
3488 |
*/ |
|
3489 |
bool QString::endsWith(const QChar &c, Qt::CaseSensitivity cs) const |
|
3490 |
{ |
|
3491 |
return d->size |
|
3492 |
&& (cs == Qt::CaseSensitive |
|
3493 |
? d->data[d->size - 1] == c |
|
3494 |
: foldCase(d->data[d->size - 1]) == foldCase(c.unicode())); |
|
3495 |
} |
|
3496 |
||
3497 |
/*! \fn const char *QString::ascii() const |
|
3498 |
\nonreentrant |
|
3499 |
||
3500 |
Use toAscii() instead. |
|
3501 |
*/ |
|
3502 |
||
3503 |
/*! \fn const char *QString::latin1() const |
|
3504 |
\nonreentrant |
|
3505 |
||
3506 |
Use toLatin1() instead. |
|
3507 |
*/ |
|
3508 |
||
3509 |
/*! \fn const char *QString::utf8() const |
|
3510 |
\nonreentrant |
|
3511 |
||
3512 |
Use toUtf8() instead. |
|
3513 |
*/ |
|
3514 |
||
3515 |
/*! \fn const char *QString::local8Bit() const |
|
3516 |
\nonreentrant |
|
3517 |
||
3518 |
Use toLocal8Bit() instead. |
|
3519 |
*/ |
|
3520 |
||
3521 |
static QByteArray toLatin1_helper(const QChar *data, int length) |
|
3522 |
{ |
|
3523 |
QByteArray ba; |
|
3524 |
if (length) { |
|
3525 |
ba.resize(length); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3526 |
const ushort *src = reinterpret_cast<const ushort *>(data); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3527 |
uchar *dst = (uchar*) ba.data(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3528 |
#if defined(QT_ALWAYS_HAVE_SSE2) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3529 |
if (length >= 16) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3530 |
const int chunkCount = length >> 4; // divided by 16 |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3531 |
const __m128i questionMark = _mm_set1_epi16('?'); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3532 |
// SSE has no compare instruction for unsigned comparison. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3533 |
// The variables must be shiffted + 0x8000 to be compared |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3534 |
const __m128i signedBitOffset = _mm_set1_epi16(0x8000); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3535 |
const __m128i thresholdMask = _mm_set1_epi16(0xff + 0x8000); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3536 |
for (int i = 0; i < chunkCount; ++i) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3537 |
__m128i chunk1 = _mm_loadu_si128((__m128i*)src); // load |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3538 |
src += 8; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3539 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3540 |
// each 16 bit is equal to 0xFF if the source is outside latin 1 (>0xff) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3541 |
const __m128i signedChunk = _mm_add_epi16(chunk1, signedBitOffset); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3542 |
const __m128i offLimitMask = _mm_cmpgt_epi16(signedChunk, thresholdMask); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3543 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3544 |
// offLimitQuestionMark contains '?' for each 16 bits that was off-limit |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3545 |
// the 16 bits that were correct contains zeros |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3546 |
const __m128i offLimitQuestionMark = _mm_and_si128(offLimitMask, questionMark); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3547 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3548 |
// correctBytes contains the bytes that were in limit |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3549 |
// the 16 bits that were off limits contains zeros |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3550 |
const __m128i correctBytes = _mm_andnot_si128(offLimitMask, chunk1); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3551 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3552 |
// merge offLimitQuestionMark and correctBytes to have the result |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3553 |
chunk1 = _mm_or_si128(correctBytes, offLimitQuestionMark); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3554 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3555 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3556 |
__m128i chunk2 = _mm_loadu_si128((__m128i*)src); // load |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3557 |
src += 8; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3558 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3559 |
// exactly the same operations as for the previous chunk of data |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3560 |
const __m128i signedChunk = _mm_add_epi16(chunk2, signedBitOffset); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3561 |
const __m128i offLimitMask = _mm_cmpgt_epi16(signedChunk, thresholdMask); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3562 |
const __m128i offLimitQuestionMark = _mm_and_si128(offLimitMask, questionMark); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3563 |
const __m128i correctBytes = _mm_andnot_si128(offLimitMask, chunk2); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3564 |
chunk2 = _mm_or_si128(correctBytes, offLimitQuestionMark); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3565 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3566 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3567 |
// pack the two vector to 16 x 8bits elements |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3568 |
const __m128i result = _mm_packus_epi16(chunk1, chunk2); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3569 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3570 |
_mm_storeu_si128((__m128i*)dst, result); // store |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3571 |
dst += 16; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3572 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3573 |
length = length % 16; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3574 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3575 |
#elif QT_HAVE_NEON |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3576 |
// Refer to the documentation of the SSE2 implementation |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3577 |
// this use eactly the same method as for SSE except: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3578 |
// 1) neon has unsigned comparison |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3579 |
// 2) packing is done to 64 bits (8 x 8bits component). |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3580 |
if (length >= 16) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3581 |
const int chunkCount = length >> 3; // divided by 8 |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3582 |
const uint16x8_t questionMark = vdupq_n_u16('?'); // set |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3583 |
const uint16x8_t thresholdMask = vdupq_n_u16(0xff); // set |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3584 |
for (int i = 0; i < chunkCount; ++i) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3585 |
uint16x8_t chunk = vld1q_u16((uint16_t *)src); // load |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3586 |
src += 8; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3587 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3588 |
const uint16x8_t offLimitMask = vcgtq_u16(chunk, thresholdMask); // chunk > thresholdMask |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3589 |
const uint16x8_t offLimitQuestionMark = vandq_u16(offLimitMask, questionMark); // offLimitMask & questionMark |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3590 |
const uint16x8_t correctBytes = vbicq_u16(chunk, offLimitMask); // !offLimitMask & chunk |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3591 |
chunk = vorrq_u16(correctBytes, offLimitQuestionMark); // correctBytes | offLimitQuestionMark |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3592 |
const uint8x8_t result = vmovn_u16(chunk); // narrowing move->packing |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3593 |
vst1_u8(dst, result); // store |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3594 |
dst += 8; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3595 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3596 |
length = length % 8; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3597 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3598 |
#endif |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3599 |
while (length--) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3600 |
*dst++ = (*src>0xff) ? '?' : (uchar) *src; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3601 |
++src; |
0 | 3602 |
} |
3603 |
} |
|
3604 |
return ba; |
|
3605 |
} |
|
3606 |
||
3607 |
/*! |
|
3608 |
Returns a Latin-1 representation of the string as a QByteArray. |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3609 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3610 |
The returned byte array is undefined if the string contains non-Latin1 |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3611 |
characters. Those characters may be suppressed or replaced with a |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3612 |
question mark. |
0 | 3613 |
|
3614 |
\sa fromLatin1(), toAscii(), toUtf8(), toLocal8Bit(), QTextCodec |
|
3615 |
*/ |
|
3616 |
QByteArray QString::toLatin1() const |
|
3617 |
{ |
|
3618 |
return toLatin1_helper(unicode(), length()); |
|
3619 |
} |
|
3620 |
||
3621 |
// ### Qt 5: Change the return type of at least toAscii(), |
|
3622 |
// toLatin1() and unicode() such that the use of Q_COMPILER_MANGLES_RETURN_TYPE |
|
3623 |
// isn't necessary in the header. See task 177402. |
|
3624 |
||
3625 |
/*! |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3626 |
Returns an 8-bit representation of the string as a QByteArray. |
0 | 3627 |
|
3628 |
If a codec has been set using QTextCodec::setCodecForCStrings(), |
|
3629 |
it is used to convert Unicode to 8-bit char; otherwise this |
|
3630 |
function does the same as toLatin1(). |
|
3631 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3632 |
Note that, despite the name, this function does not necessarily return an US-ASCII |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3633 |
(ANSI X3.4-1986) string and its result may not be US-ASCII compatible. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3634 |
|
0 | 3635 |
\sa fromAscii(), toLatin1(), toUtf8(), toLocal8Bit(), QTextCodec |
3636 |
*/ |
|
3637 |
QByteArray QString::toAscii() const |
|
3638 |
{ |
|
3639 |
#ifndef QT_NO_TEXTCODEC |
|
3640 |
if (codecForCStrings) |
|
3641 |
return codecForCStrings->fromUnicode(*this); |
|
3642 |
#endif // QT_NO_TEXTCODEC |
|
3643 |
return toLatin1(); |
|
3644 |
} |
|
3645 |
||
3646 |
#if !defined(Q_WS_MAC) && defined(Q_OS_UNIX) |
|
3647 |
static QByteArray toLocal8Bit_helper(const QChar *data, int length) |
|
3648 |
{ |
|
3649 |
#ifndef QT_NO_TEXTCODEC |
|
3650 |
if (QTextCodec::codecForLocale()) |
|
3651 |
return QTextCodec::codecForLocale()->fromUnicode(data, length); |
|
3652 |
#endif // QT_NO_TEXTCODEC |
|
3653 |
return toLatin1_helper(data, length); |
|
3654 |
} |
|
3655 |
#endif |
|
3656 |
||
3657 |
/*! |
|
3658 |
Returns the local 8-bit representation of the string as a |
|
3659 |
QByteArray. The returned byte array is undefined if the string |
|
3660 |
contains characters not supported by the local 8-bit encoding. |
|
3661 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3662 |
QTextCodec::codecForLocale() is used to perform the conversion from |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3663 |
Unicode. If the locale encoding could not be determined, this function |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3664 |
does the same as toLatin1(). |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3665 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3666 |
If this string contains any characters that cannot be encoded in the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3667 |
locale, the returned byte array is undefined. Those characters may be |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3668 |
suppressed or replaced by another. |
0 | 3669 |
|
3670 |
\sa fromLocal8Bit(), toAscii(), toLatin1(), toUtf8(), QTextCodec |
|
3671 |
*/ |
|
3672 |
QByteArray QString::toLocal8Bit() const |
|
3673 |
{ |
|
3674 |
#ifndef QT_NO_TEXTCODEC |
|
3675 |
if (QTextCodec::codecForLocale()) |
|
3676 |
return QTextCodec::codecForLocale()->fromUnicode(*this); |
|
3677 |
#endif // QT_NO_TEXTCODEC |
|
3678 |
return toLatin1(); |
|
3679 |
} |
|
3680 |
||
3681 |
/*! |
|
3682 |
Returns a UTF-8 representation of the string as a QByteArray. |
|
3683 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3684 |
UTF-8 is a Unicode codec and can represent all characters in a Unicode |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3685 |
string like QString. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3686 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3687 |
However, in the Unicode range, there are certain codepoints that are not |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3688 |
considered characters. The Unicode standard reserves the last two |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3689 |
codepoints in each Unicode Plane (U+FFFE, U+FFFF, U+1FFFE, U+1FFFF, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3690 |
U+2FFFE, etc.), as well as 16 codepoints in the range U+FDD0..U+FDDF, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3691 |
inclusive, as non-characters. If any of those appear in the string, they |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3692 |
may be discarded and will not appear in the UTF-8 representation, or they |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3693 |
may be replaced by one or more replacement characters. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3694 |
|
0 | 3695 |
\sa fromUtf8(), toAscii(), toLatin1(), toLocal8Bit(), QTextCodec |
3696 |
*/ |
|
3697 |
QByteArray QString::toUtf8() const |
|
3698 |
{ |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3699 |
if (isNull()) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3700 |
return QByteArray(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3701 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3702 |
return QUtf8::convertFromUnicode(constData(), length(), 0); |
0 | 3703 |
} |
3704 |
||
3705 |
/*! |
|
3706 |
\since 4.2 |
|
3707 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3708 |
Returns a UCS-4/UTF-32 representation of the string as a QVector<uint>. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3709 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3710 |
UCS-4 is a Unicode codec and is lossless. All characters from this string |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3711 |
can be encoded in UCS-4. |
0 | 3712 |
|
3713 |
\sa fromUtf8(), toAscii(), toLatin1(), toLocal8Bit(), QTextCodec, fromUcs4(), toWCharArray() |
|
3714 |
*/ |
|
3715 |
QVector<uint> QString::toUcs4() const |
|
3716 |
{ |
|
3717 |
QVector<uint> v(length()); |
|
3718 |
uint *a = v.data(); |
|
3719 |
const unsigned short *uc = utf16(); |
|
3720 |
for (int i = 0; i < length(); ++i) { |
|
3721 |
uint u = uc[i]; |
|
3722 |
if (QChar(u).isHighSurrogate() && i < length()-1) { |
|
3723 |
ushort low = uc[i+1]; |
|
3724 |
if (QChar(low).isLowSurrogate()) { |
|
3725 |
++i; |
|
3726 |
u = QChar::surrogateToUcs4(u, low); |
|
3727 |
} |
|
3728 |
} |
|
3729 |
*a = u; |
|
3730 |
++a; |
|
3731 |
} |
|
3732 |
v.resize(a - v.data()); |
|
3733 |
return v; |
|
3734 |
} |
|
3735 |
||
3736 |
QString::Data *QString::fromLatin1_helper(const char *str, int size) |
|
3737 |
{ |
|
3738 |
Data *d; |
|
3739 |
if (!str) { |
|
3740 |
d = &shared_null; |
|
3741 |
d->ref.ref(); |
|
3742 |
} else if (size == 0 || (!*str && size < 0)) { |
|
3743 |
d = &shared_empty; |
|
3744 |
d->ref.ref(); |
|
3745 |
} else { |
|
3746 |
if (size < 0) |
|
3747 |
size = qstrlen(str); |
|
3748 |
d = static_cast<Data *>(qMalloc(sizeof(Data) + size * sizeof(QChar))); |
|
3749 |
Q_CHECK_PTR(d); |
|
3750 |
d->ref = 1; |
|
3751 |
d->alloc = d->size = size; |
|
3752 |
d->clean = d->asciiCache = d->simpletext = d->righttoleft = d->capacity = 0; |
|
3753 |
d->data = d->array; |
|
3754 |
d->array[size] = '\0'; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3755 |
ushort *dst = d->data; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3756 |
/* SIMD: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3757 |
* Unpacking with SSE has been shown to improve performance on recent CPUs |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3758 |
* The same method gives no improvement with NEON. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3759 |
*/ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3760 |
#if defined(QT_ALWAYS_HAVE_SSE2) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3761 |
if (size >= 16) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3762 |
int chunkCount = size >> 4; // divided by 16 |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3763 |
const __m128i nullMask = _mm_set1_epi32(0); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3764 |
for (int i = 0; i < chunkCount; ++i) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3765 |
const __m128i chunk = _mm_loadu_si128((__m128i*)str); // load |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3766 |
str += 16; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3767 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3768 |
// unpack the first 8 bytes, padding with zeros |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3769 |
const __m128i firstHalf = _mm_unpacklo_epi8(chunk, nullMask); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3770 |
_mm_storeu_si128((__m128i*)dst, firstHalf); // store |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3771 |
dst += 8; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3772 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3773 |
// unpack the last 8 bytes, padding with zeros |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3774 |
const __m128i secondHalf = _mm_unpackhi_epi8 (chunk, nullMask); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3775 |
_mm_storeu_si128((__m128i*)dst, secondHalf); // store |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3776 |
dst += 8; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3777 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3778 |
size = size % 16; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3779 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3780 |
#endif |
0 | 3781 |
while (size--) |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3782 |
*dst++ = (uchar)*str++; |
0 | 3783 |
} |
3784 |
return d; |
|
3785 |
} |
|
3786 |
||
3787 |
QString::Data *QString::fromAscii_helper(const char *str, int size) |
|
3788 |
{ |
|
3789 |
#ifndef QT_NO_TEXTCODEC |
|
3790 |
if (codecForCStrings) { |
|
3791 |
Data *d; |
|
3792 |
if (!str) { |
|
3793 |
d = &shared_null; |
|
3794 |
d->ref.ref(); |
|
3795 |
} else if (size == 0 || (!*str && size < 0)) { |
|
3796 |
d = &shared_empty; |
|
3797 |
d->ref.ref(); |
|
3798 |
} else { |
|
3799 |
if (size < 0) |
|
3800 |
size = qstrlen(str); |
|
3801 |
QString s = codecForCStrings->toUnicode(str, size); |
|
3802 |
d = s.d; |
|
3803 |
d->ref.ref(); |
|
3804 |
} |
|
3805 |
return d; |
|
3806 |
} |
|
3807 |
#endif |
|
3808 |
return fromLatin1_helper(str, size); |
|
3809 |
} |
|
3810 |
||
3811 |
/*! |
|
3812 |
Returns a QString initialized with the first \a size characters |
|
3813 |
of the Latin-1 string \a str. |
|
3814 |
||
3815 |
If \a size is -1 (default), it is taken to be qstrlen(\a |
|
3816 |
str). |
|
3817 |
||
3818 |
\sa toLatin1(), fromAscii(), fromUtf8(), fromLocal8Bit() |
|
3819 |
*/ |
|
3820 |
QString QString::fromLatin1(const char *str, int size) |
|
3821 |
{ |
|
3822 |
return QString(fromLatin1_helper(str, size), 0); |
|
3823 |
} |
|
3824 |
||
3825 |
||
3826 |
#ifdef QT3_SUPPORT |
|
3827 |
||
3828 |
/*! |
|
3829 |
\internal |
|
3830 |
*/ |
|
3831 |
const char *QString::ascii_helper() const |
|
3832 |
{ |
|
3833 |
if (!asciiCache) |
|
3834 |
asciiCache = new QHash<void *, QByteArray>(); |
|
3835 |
||
3836 |
d->asciiCache = true; |
|
3837 |
QByteArray ascii = toAscii(); |
|
3838 |
QByteArray old = asciiCache->value(d); |
|
3839 |
if (old == ascii) |
|
3840 |
return old.constData(); |
|
3841 |
asciiCache->insert(d, ascii); |
|
3842 |
return ascii.constData(); |
|
3843 |
} |
|
3844 |
||
3845 |
/*! |
|
3846 |
\internal |
|
3847 |
*/ |
|
3848 |
const char *QString::latin1_helper() const |
|
3849 |
{ |
|
3850 |
if (!asciiCache) |
|
3851 |
asciiCache = new QHash<void *, QByteArray>(); |
|
3852 |
||
3853 |
d->asciiCache = true; |
|
3854 |
QByteArray ascii = toLatin1(); |
|
3855 |
QByteArray old = asciiCache->value(d); |
|
3856 |
if (old == ascii) |
|
3857 |
return old.constData(); |
|
3858 |
asciiCache->insert(d, ascii); |
|
3859 |
return ascii.constData(); |
|
3860 |
} |
|
3861 |
||
3862 |
#endif |
|
3863 |
||
3864 |
/*! |
|
3865 |
Returns a QString initialized with the first \a size characters |
|
3866 |
of the 8-bit string \a str. |
|
3867 |
||
3868 |
If \a size is -1 (default), it is taken to be qstrlen(\a |
|
3869 |
str). |
|
3870 |
||
3871 |
QTextCodec::codecForLocale() is used to perform the conversion |
|
3872 |
from Unicode. |
|
3873 |
||
3874 |
\sa toLocal8Bit(), fromAscii(), fromLatin1(), fromUtf8() |
|
3875 |
*/ |
|
3876 |
QString QString::fromLocal8Bit(const char *str, int size) |
|
3877 |
{ |
|
3878 |
if (!str) |
|
3879 |
return QString(); |
|
3880 |
if (size == 0 || (!*str && size < 0)) |
|
3881 |
return QLatin1String(""); |
|
3882 |
#if !defined(QT_NO_TEXTCODEC) |
|
3883 |
if (size < 0) |
|
3884 |
size = qstrlen(str); |
|
3885 |
QTextCodec *codec = QTextCodec::codecForLocale(); |
|
3886 |
if (codec) |
|
3887 |
return codec->toUnicode(str, size); |
|
3888 |
#endif // !QT_NO_TEXTCODEC |
|
3889 |
return fromLatin1(str, size); |
|
3890 |
} |
|
3891 |
||
3892 |
/*! |
|
3893 |
Returns a QString initialized with the first \a size characters |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3894 |
of the 8-bit string \a str. |
0 | 3895 |
|
3896 |
If \a size is -1 (default), it is taken to be qstrlen(\a |
|
3897 |
str). |
|
3898 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3899 |
Note that, despite the name, this function actually uses the codec |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3900 |
defined by QTextCodec::setCodecForCStrings() to convert \a str to |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3901 |
Unicode. Depending on the codec, it may not accept valid US-ASCII (ANSI |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3902 |
X3.4-1986) input. If no codec has been set, this function does the same |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3903 |
as fromLatin1(). |
0 | 3904 |
|
3905 |
\sa toAscii(), fromLatin1(), fromUtf8(), fromLocal8Bit() |
|
3906 |
*/ |
|
3907 |
QString QString::fromAscii(const char *str, int size) |
|
3908 |
{ |
|
3909 |
return QString(fromAscii_helper(str, size), 0); |
|
3910 |
} |
|
3911 |
||
3912 |
/*! |
|
3913 |
Returns a QString initialized with the first \a size bytes |
|
3914 |
of the UTF-8 string \a str. |
|
3915 |
||
3916 |
If \a size is -1 (default), it is taken to be qstrlen(\a |
|
3917 |
str). |
|
3918 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3919 |
UTF-8 is a Unicode codec and can represent all characters in a Unicode |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3920 |
string like QString. However, invalid sequences are possible with UTF-8 |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3921 |
and, if any such are found, they will be replaced with one or more |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3922 |
"replacement characters", or suppressed. These include non-Unicode |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3923 |
sequences, non-characters, overlong sequences or surrogate codepoints |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3924 |
encoded into UTF-8. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3925 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3926 |
Non-characters are codepoints that the Unicode standard reserves and must |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3927 |
not be used in text interchange. They are the last two codepoints in each |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3928 |
Unicode Plane (U+FFFE, U+FFFF, U+1FFFE, U+1FFFF, U+2FFFE, etc.), as well |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3929 |
as 16 codepoints in the range U+FDD0..U+FDDF, inclusive. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3930 |
|
0 | 3931 |
\sa toUtf8(), fromAscii(), fromLatin1(), fromLocal8Bit() |
3932 |
*/ |
|
3933 |
QString QString::fromUtf8(const char *str, int size) |
|
3934 |
{ |
|
3935 |
if (!str) |
|
3936 |
return QString(); |
|
3937 |
if (size < 0) |
|
3938 |
size = qstrlen(str); |
|
3939 |
||
3940 |
return QUtf8::convertToUnicode(str, size, 0); |
|
3941 |
} |
|
3942 |
||
3943 |
/*! |
|
3944 |
Returns a QString initialized with the first \a size characters |
|
3945 |
of the Unicode string \a unicode (ISO-10646-UTF-16 encoded). |
|
3946 |
||
3947 |
If \a size is -1 (default), \a unicode must be terminated |
|
3948 |
with a 0. |
|
3949 |
||
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3950 |
This function checks for a Byte Order Mark (BOM). If it is missing, |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3951 |
host byte order is assumed. |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3952 |
|
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3953 |
This function is comparatively slow. |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
3954 |
Use QString(const ushort *, int) or QString(const ushort *) if possible. |
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3955 |
|
0 | 3956 |
QString makes a deep copy of the Unicode data. |
3957 |
||
3958 |
\sa utf16(), setUtf16() |
|
3959 |
*/ |
|
3960 |
QString QString::fromUtf16(const ushort *unicode, int size) |
|
3961 |
{ |
|
3962 |
if (!unicode) |
|
3963 |
return QString(); |
|
3964 |
if (size < 0) { |
|
3965 |
size = 0; |
|
3966 |
while (unicode[size] != 0) |
|
3967 |
++size; |
|
3968 |
} |
|
3969 |
return QUtf16::convertToUnicode((const char *)unicode, size*2, 0); |
|
3970 |
} |
|
3971 |
||
3972 |
||
3973 |
/*! |
|
3974 |
\since 4.2 |
|
3975 |
||
3976 |
Returns a QString initialized with the first \a size characters |
|
3977 |
of the Unicode string \a unicode (ISO-10646-UCS-4 encoded). |
|
3978 |
||
3979 |
If \a size is -1 (default), \a unicode must be terminated |
|
3980 |
with a 0. |
|
3981 |
||
3982 |
\sa toUcs4(), fromUtf16(), utf16(), setUtf16(), fromWCharArray() |
|
3983 |
*/ |
|
3984 |
QString QString::fromUcs4(const uint *unicode, int size) |
|
3985 |
{ |
|
3986 |
if (!unicode) |
|
3987 |
return QString(); |
|
3988 |
if (size < 0) { |
|
3989 |
size = 0; |
|
3990 |
while (unicode[size] != 0) |
|
3991 |
++size; |
|
3992 |
} |
|
3993 |
return QUtf32::convertToUnicode((const char *)unicode, size*4, 0); |
|
3994 |
} |
|
3995 |
||
3996 |
/*! |
|
3997 |
Resizes the string to \a size characters and copies \a unicode |
|
3998 |
into the string. |
|
3999 |
||
4000 |
If \a unicode is 0, nothing is copied, but the string is still |
|
4001 |
resized to \a size. |
|
4002 |
||
4003 |
\sa unicode(), setUtf16() |
|
4004 |
*/ |
|
4005 |
QString& QString::setUnicode(const QChar *unicode, int size) |
|
4006 |
{ |
|
4007 |
resize(size); |
|
4008 |
if (unicode && size) |
|
4009 |
memcpy(d->data, unicode, size * sizeof(QChar)); |
|
4010 |
return *this; |
|
4011 |
} |
|
4012 |
||
4013 |
/*! |
|
4014 |
\fn QString &QString::setUtf16(const ushort *unicode, int size) |
|
4015 |
||
4016 |
Resizes the string to \a size characters and copies \a unicode |
|
4017 |
into the string. |
|
4018 |
||
4019 |
If \a unicode is 0, nothing is copied, but the string is still |
|
4020 |
resized to \a size. |
|
4021 |
||
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
4022 |
Note that unlike fromUtf16(), this function does not consider BOMs and |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
4023 |
possibly differing byte ordering. |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
4024 |
|
0 | 4025 |
\sa utf16(), setUnicode() |
4026 |
*/ |
|
4027 |
||
4028 |
/*! |
|
4029 |
Returns a string that has whitespace removed from the start |
|
4030 |
and the end, and that has each sequence of internal whitespace |
|
4031 |
replaced with a single space. |
|
4032 |
||
4033 |
Whitespace means any character for which QChar::isSpace() returns |
|
4034 |
true. This includes the ASCII characters '\\t', '\\n', '\\v', |
|
4035 |
'\\f', '\\r', and ' '. |
|
4036 |
||
4037 |
Example: |
|
4038 |
||
4039 |
\snippet doc/src/snippets/qstring/main.cpp 57 |
|
4040 |
||
4041 |
\sa trimmed() |
|
4042 |
*/ |
|
4043 |
QString QString::simplified() const |
|
4044 |
{ |
|
4045 |
if (d->size == 0) |
|
4046 |
return *this; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4047 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4048 |
const QChar * const start = reinterpret_cast<QChar *>(d->data); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4049 |
const QChar *from = start; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4050 |
const QChar *fromEnd = start + d->size; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4051 |
forever { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4052 |
QChar ch = *from; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4053 |
if (!ch.isSpace()) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4054 |
break; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4055 |
if (++from == fromEnd) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4056 |
// All-whitespace string |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4057 |
shared_empty.ref.ref(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4058 |
return QString(&shared_empty, 0); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4059 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4060 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4061 |
// This loop needs no underflow check, as we already determined that |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4062 |
// the string contains non-whitespace. If the string has exactly one |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4063 |
// non-whitespace, it will be checked twice - we can live with that. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4064 |
while (fromEnd[-1].isSpace()) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4065 |
fromEnd--; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4066 |
// The rest of the function depends on the fact that we already know |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4067 |
// that the last character in the source is no whitespace. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4068 |
const QChar *copyFrom = from; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4069 |
int copyCount; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4070 |
forever { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4071 |
if (++from == fromEnd) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4072 |
// Only leading and/or trailing whitespace, if any at all |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4073 |
return mid(copyFrom - start, from - copyFrom); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4074 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4075 |
QChar ch = *from; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4076 |
if (!ch.isSpace()) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4077 |
continue; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4078 |
if (ch != QLatin1Char(' ')) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4079 |
copyCount = from - copyFrom; |
0 | 4080 |
break; |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4081 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4082 |
ch = *++from; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4083 |
if (ch.isSpace()) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4084 |
copyCount = from - copyFrom - 1; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4085 |
break; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4086 |
} |
0 | 4087 |
} |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4088 |
// 'from' now points at the non-trailing whitespace which made the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4089 |
// string not simplified in the first place. 'copyCount' is the number |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4090 |
// of already simplified characters - at least one, obviously - |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4091 |
// without a trailing space. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4092 |
QString result((fromEnd - from) + copyCount, Qt::Uninitialized); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4093 |
QChar *to = reinterpret_cast<QChar *>(result.d->data); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4094 |
::memcpy(to, copyFrom, copyCount * 2); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4095 |
to += copyCount; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4096 |
fromEnd--; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4097 |
QChar ch; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4098 |
forever { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4099 |
*to++ = QLatin1Char(' '); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4100 |
do { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4101 |
ch = *++from; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4102 |
} while (ch.isSpace()); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4103 |
if (from == fromEnd) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4104 |
break; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4105 |
do { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4106 |
*to++ = ch; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4107 |
ch = *++from; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4108 |
if (from == fromEnd) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4109 |
goto done; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4110 |
} while (!ch.isSpace()); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4111 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4112 |
done: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4113 |
*to++ = ch; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4114 |
result.truncate(to - reinterpret_cast<QChar *>(result.d->data)); |
0 | 4115 |
return result; |
4116 |
} |
|
4117 |
||
4118 |
/*! |
|
4119 |
Returns a string that has whitespace removed from the start and |
|
4120 |
the end. |
|
4121 |
||
4122 |
Whitespace means any character for which QChar::isSpace() returns |
|
4123 |
true. This includes the ASCII characters '\\t', '\\n', '\\v', |
|
4124 |
'\\f', '\\r', and ' '. |
|
4125 |
||
4126 |
Example: |
|
4127 |
||
4128 |
\snippet doc/src/snippets/qstring/main.cpp 82 |
|
4129 |
||
4130 |
Unlike simplified(), trimmed() leaves internal whitespace alone. |
|
4131 |
||
4132 |
\sa simplified() |
|
4133 |
*/ |
|
4134 |
QString QString::trimmed() const |
|
4135 |
{ |
|
4136 |
if (d->size == 0) |
|
4137 |
return *this; |
|
4138 |
const QChar *s = (const QChar*)d->data; |
|
4139 |
if (!s->isSpace() && !s[d->size-1].isSpace()) |
|
4140 |
return *this; |
|
4141 |
int start = 0; |
|
4142 |
int end = d->size - 1; |
|
4143 |
while (start<=end && s[start].isSpace()) // skip white space from start |
|
4144 |
start++; |
|
4145 |
if (start <= end) { // only white space |
|
4146 |
while (end && s[end].isSpace()) // skip white space from end |
|
4147 |
end--; |
|
4148 |
} |
|
4149 |
int l = end - start + 1; |
|
4150 |
if (l <= 0) { |
|
4151 |
shared_empty.ref.ref(); |
|
4152 |
return QString(&shared_empty, 0); |
|
4153 |
} |
|
4154 |
return QString(s + start, l); |
|
4155 |
} |
|
4156 |
||
4157 |
/*! \fn const QChar QString::at(int position) const |
|
4158 |
||
4159 |
Returns the character at the given index \a position in the |
|
4160 |
string. |
|
4161 |
||
4162 |
The \a position must be a valid index position in the string |
|
4163 |
(i.e., 0 <= \a position < size()). |
|
4164 |
||
4165 |
\sa operator[]() |
|
4166 |
*/ |
|
4167 |
||
4168 |
/*! |
|
4169 |
\fn QCharRef QString::operator[](int position) |
|
4170 |
||
4171 |
Returns the character at the specified \a position in the string as a |
|
4172 |
modifiable reference. |
|
4173 |
||
4174 |
Example: |
|
4175 |
||
4176 |
\snippet doc/src/snippets/qstring/main.cpp 85 |
|
4177 |
||
4178 |
The return value is of type QCharRef, a helper class for QString. |
|
4179 |
When you get an object of type QCharRef, you can use it as if it |
|
4180 |
were a QChar &. If you assign to it, the assignment will apply to |
|
4181 |
the character in the QString from which you got the reference. |
|
4182 |
||
4183 |
\sa at() |
|
4184 |
*/ |
|
4185 |
||
4186 |
/*! |
|
4187 |
\fn const QChar QString::operator[](int position) const |
|
4188 |
||
4189 |
\overload operator[]() |
|
4190 |
*/ |
|
4191 |
||
4192 |
/*! \fn QCharRef QString::operator[](uint position) |
|
4193 |
||
4194 |
\overload operator[]() |
|
4195 |
||
4196 |
Returns the character at the specified \a position in the string as a |
|
4197 |
modifiable reference. Equivalent to \c at(position). |
|
4198 |
*/ |
|
4199 |
||
4200 |
/*! \fn const QChar QString::operator[](uint position) const |
|
4201 |
||
4202 |
\overload operator[]() |
|
4203 |
*/ |
|
4204 |
||
4205 |
/*! |
|
4206 |
\fn void QString::truncate(int position) |
|
4207 |
||
4208 |
Truncates the string at the given \a position index. |
|
4209 |
||
4210 |
If the specified \a position index is beyond the end of the |
|
4211 |
string, nothing happens. |
|
4212 |
||
4213 |
Example: |
|
4214 |
||
4215 |
\snippet doc/src/snippets/qstring/main.cpp 83 |
|
4216 |
||
4217 |
If \a position is negative, it is equivalent to passing zero. |
|
4218 |
||
4219 |
\sa chop(), resize(), left() |
|
4220 |
*/ |
|
4221 |
||
4222 |
void QString::truncate(int pos) |
|
4223 |
{ |
|
4224 |
if (pos < d->size) |
|
4225 |
resize(pos); |
|
4226 |
} |
|
4227 |
||
4228 |
||
4229 |
/*! |
|
4230 |
Removes \a n characters from the end of the string. |
|
4231 |
||
4232 |
If \a n is greater than size(), the result is an empty string. |
|
4233 |
||
4234 |
Example: |
|
4235 |
\snippet doc/src/snippets/qstring/main.cpp 15 |
|
4236 |
||
4237 |
If you want to remove characters from the \e beginning of the |
|
4238 |
string, use remove() instead. |
|
4239 |
||
4240 |
\sa truncate(), resize(), remove() |
|
4241 |
*/ |
|
4242 |
void QString::chop(int n) |
|
4243 |
{ |
|
4244 |
if (n > 0) |
|
4245 |
resize(d->size - n); |
|
4246 |
} |
|
4247 |
||
4248 |
/*! |
|
4249 |
Sets every character in the string to character \a ch. If \a size |
|
4250 |
is different from -1 (default), the string is resized to \a |
|
4251 |
size beforehand. |
|
4252 |
||
4253 |
Example: |
|
4254 |
||
4255 |
\snippet doc/src/snippets/qstring/main.cpp 21 |
|
4256 |
||
4257 |
\sa resize() |
|
4258 |
*/ |
|
4259 |
||
4260 |
QString& QString::fill(QChar ch, int size) |
|
4261 |
{ |
|
4262 |
resize(size < 0 ? d->size : size); |
|
4263 |
if (d->size) { |
|
4264 |
QChar *i = (QChar*)d->data + d->size; |
|
4265 |
QChar *b = (QChar*)d->data; |
|
4266 |
while (i != b) |
|
4267 |
*--i = ch; |
|
4268 |
} |
|
4269 |
return *this; |
|
4270 |
} |
|
4271 |
||
4272 |
/*! |
|
4273 |
\fn int QString::length() const |
|
4274 |
||
4275 |
Returns the number of characters in this string. Equivalent to |
|
4276 |
size(). |
|
4277 |
||
4278 |
\sa setLength() |
|
4279 |
*/ |
|
4280 |
||
4281 |
/*! |
|
4282 |
\fn int QString::size() const |
|
4283 |
||
4284 |
Returns the number of characters in this string. |
|
4285 |
||
4286 |
The last character in the string is at position size() - 1. In |
|
4287 |
addition, QString ensures that the character at position size() |
|
4288 |
is always '\\0', so that you can use the return value of data() |
|
4289 |
and constData() as arguments to functions that expect |
|
4290 |
'\\0'-terminated strings. |
|
4291 |
||
4292 |
Example: |
|
4293 |
||
4294 |
\snippet doc/src/snippets/qstring/main.cpp 58 |
|
4295 |
||
4296 |
\sa isEmpty(), resize() |
|
4297 |
*/ |
|
4298 |
||
4299 |
/*! \fn bool QString::isNull() const |
|
4300 |
||
4301 |
Returns true if this string is null; otherwise returns false. |
|
4302 |
||
4303 |
Example: |
|
4304 |
||
4305 |
\snippet doc/src/snippets/qstring/main.cpp 28 |
|
4306 |
||
4307 |
Qt makes a distinction between null strings and empty strings for |
|
4308 |
historical reasons. For most applications, what matters is |
|
4309 |
whether or not a string contains any data, and this can be |
|
4310 |
determined using the isEmpty() function. |
|
4311 |
||
4312 |
\sa isEmpty() |
|
4313 |
*/ |
|
4314 |
||
4315 |
/*! \fn bool QString::isEmpty() const |
|
4316 |
||
4317 |
Returns true if the string has no characters; otherwise returns |
|
4318 |
false. |
|
4319 |
||
4320 |
Example: |
|
4321 |
||
4322 |
\snippet doc/src/snippets/qstring/main.cpp 27 |
|
4323 |
||
4324 |
\sa size() |
|
4325 |
*/ |
|
4326 |
||
4327 |
/*! \fn QString &QString::operator+=(const QString &other) |
|
4328 |
||
4329 |
Appends the string \a other onto the end of this string and |
|
4330 |
returns a reference to this string. |
|
4331 |
||
4332 |
Example: |
|
4333 |
||
4334 |
\snippet doc/src/snippets/qstring/main.cpp 84 |
|
4335 |
||
4336 |
This operation is typically very fast (\l{constant time}), |
|
4337 |
because QString preallocates extra space at the end of the string |
|
4338 |
data so it can grow without reallocating the entire string each |
|
4339 |
time. |
|
4340 |
||
4341 |
\sa append(), prepend() |
|
4342 |
*/ |
|
4343 |
||
4344 |
/*! \fn QString &QString::operator+=(const QLatin1String &str) |
|
4345 |
||
4346 |
\overload operator+=() |
|
4347 |
||
4348 |
Appends the Latin-1 string \a str to this string. |
|
4349 |
*/ |
|
4350 |
||
4351 |
/*! \fn QString &QString::operator+=(const QByteArray &ba) |
|
4352 |
||
4353 |
\overload operator+=() |
|
4354 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4355 |
Appends the byte array \a ba to this string. The byte array is converted |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4356 |
to Unicode using the fromAscii() function. If any NUL characters ('\0') |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4357 |
are embedded in the \a ba byte array, they will be included in the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4358 |
transformation. |
0 | 4359 |
|
4360 |
You can disable this function by defining \c |
|
4361 |
QT_NO_CAST_FROM_ASCII when you compile your applications. This |
|
4362 |
can be useful if you want to ensure that all user-visible strings |
|
4363 |
go through QObject::tr(), for example. |
|
4364 |
*/ |
|
4365 |
||
4366 |
/*! \fn QString &QString::operator+=(const char *str) |
|
4367 |
||
4368 |
\overload operator+=() |
|
4369 |
||
4370 |
Appends the string \a str to this string. The const char pointer |
|
4371 |
is converted to Unicode using the fromAscii() function. |
|
4372 |
||
4373 |
You can disable this function by defining \c QT_NO_CAST_FROM_ASCII |
|
4374 |
when you compile your applications. This can be useful if you want |
|
4375 |
to ensure that all user-visible strings go through QObject::tr(), |
|
4376 |
for example. |
|
4377 |
*/ |
|
4378 |
||
4379 |
/*! \fn QString &QString::operator+=(const QStringRef &str) |
|
4380 |
||
4381 |
\overload operator+=() |
|
4382 |
||
4383 |
Appends the string section referenced by \a str to this string. |
|
4384 |
*/ |
|
4385 |
||
4386 |
/*! \fn QString &QString::operator+=(char ch) |
|
4387 |
||
4388 |
\overload operator+=() |
|
4389 |
||
4390 |
Appends the character \a ch to this string. The character is |
|
4391 |
converted to Unicode using the fromAscii() function. |
|
4392 |
||
4393 |
You can disable this function by defining \c QT_NO_CAST_FROM_ASCII |
|
4394 |
when you compile your applications. This can be useful if you want |
|
4395 |
to ensure that all user-visible strings go through QObject::tr(), |
|
4396 |
for example. |
|
4397 |
*/ |
|
4398 |
||
4399 |
/*! \fn QString &QString::operator+=(QChar ch) |
|
4400 |
||
4401 |
\overload operator+=() |
|
4402 |
||
4403 |
Appends the character \a ch to the string. |
|
4404 |
*/ |
|
4405 |
||
4406 |
/*! \fn QString &QString::operator+=(QChar::SpecialCharacter c) |
|
4407 |
||
4408 |
\overload operator+=() |
|
4409 |
||
4410 |
\internal |
|
4411 |
*/ |
|
4412 |
||
4413 |
/*! |
|
4414 |
\fn bool operator==(const char *s1, const QString &s2) |
|
4415 |
||
4416 |
\overload operator==() |
|
4417 |
\relates QString |
|
4418 |
||
4419 |
Returns true if \a s1 is equal to \a s2; otherwise returns false. |
|
4420 |
Note that no string is equal to \a s1 being 0. |
|
4421 |
||
4422 |
Equivalent to \c {s1 != 0 && compare(s1, s2) == 0}. |
|
4423 |
||
4424 |
\sa QString::compare() |
|
4425 |
*/ |
|
4426 |
||
4427 |
/*! |
|
4428 |
\fn bool operator!=(const char *s1, const QString &s2) |
|
4429 |
\relates QString |
|
4430 |
||
4431 |
Returns true if \a s1 is not equal to \a s2; otherwise returns |
|
4432 |
false. |
|
4433 |
||
4434 |
For \a s1 != 0, this is equivalent to \c {compare(} \a s1, \a s2 |
|
4435 |
\c {) != 0}. Note that no string is equal to \a s1 being 0. |
|
4436 |
||
4437 |
\sa QString::compare() |
|
4438 |
*/ |
|
4439 |
||
4440 |
/*! |
|
4441 |
\fn bool operator<(const char *s1, const QString &s2) |
|
4442 |
\relates QString |
|
4443 |
||
4444 |
Returns true if \a s1 is lexically less than \a s2; otherwise |
|
4445 |
returns false. For \a s1 != 0, this is equivalent to \c |
|
4446 |
{compare(s1, s2) < 0}. |
|
4447 |
||
4448 |
The comparison is based exclusively on the numeric Unicode values |
|
4449 |
of the characters and is very fast, but is not what a human would |
|
4450 |
expect. Consider sorting user-interface strings using the |
|
4451 |
QString::localeAwareCompare() function. |
|
4452 |
||
4453 |
\sa QString::compare() |
|
4454 |
*/ |
|
4455 |
||
4456 |
/*! |
|
4457 |
\fn bool operator<=(const char *s1, const QString &s2) |
|
4458 |
\relates QString |
|
4459 |
||
4460 |
Returns true if \a s1 is lexically less than or equal to \a s2; |
|
4461 |
otherwise returns false. For \a s1 != 0, this is equivalent to \c |
|
4462 |
{compare(s1, s2) <= 0}. |
|
4463 |
||
4464 |
The comparison is based exclusively on the numeric Unicode values |
|
4465 |
of the characters and is very fast, but is not what a human would |
|
4466 |
expect. Consider sorting user-interface strings with |
|
4467 |
QString::localeAwareCompare(). |
|
4468 |
||
4469 |
\sa QString::compare() |
|
4470 |
*/ |
|
4471 |
||
4472 |
/*! |
|
4473 |
\fn bool operator>(const char *s1, const QString &s2) |
|
4474 |
\relates QString |
|
4475 |
||
4476 |
Returns true if \a s1 is lexically greater than \a s2; otherwise |
|
4477 |
returns false. Equivalent to \c {compare(s1, s2) > 0}. |
|
4478 |
||
4479 |
The comparison is based exclusively on the numeric Unicode values |
|
4480 |
of the characters and is very fast, but is not what a human would |
|
4481 |
expect. Consider sorting user-interface strings using the |
|
4482 |
QString::localeAwareCompare() function. |
|
4483 |
||
4484 |
\sa QString::compare() |
|
4485 |
*/ |
|
4486 |
||
4487 |
/*! |
|
4488 |
\fn bool operator>=(const char *s1, const QString &s2) |
|
4489 |
\relates QString |
|
4490 |
||
4491 |
Returns true if \a s1 is lexically greater than or equal to \a s2; |
|
4492 |
otherwise returns false. For \a s1 != 0, this is equivalent to \c |
|
4493 |
{compare(s1, s2) >= 0}. |
|
4494 |
||
4495 |
The comparison is based exclusively on the numeric Unicode values |
|
4496 |
of the characters and is very fast, but is not what a human would |
|
4497 |
expect. Consider sorting user-interface strings using the |
|
4498 |
QString::localeAwareCompare() function. |
|
4499 |
*/ |
|
4500 |
||
4501 |
/*! |
|
4502 |
\fn const QString operator+(const QString &s1, const QString &s2) |
|
4503 |
\relates QString |
|
4504 |
||
4505 |
Returns a string which is the result of concatenating \a s1 and \a |
|
4506 |
s2. |
|
4507 |
*/ |
|
4508 |
||
4509 |
/*! |
|
4510 |
\fn const QString operator+(const QString &s1, const char *s2) |
|
4511 |
\relates QString |
|
4512 |
||
4513 |
Returns a string which is the result of concatenating \a s1 and \a |
|
4514 |
s2 (\a s2 is converted to Unicode using the QString::fromAscii() |
|
4515 |
function). |
|
4516 |
||
4517 |
\sa QString::fromAscii() |
|
4518 |
*/ |
|
4519 |
||
4520 |
/*! |
|
4521 |
\fn const QString operator+(const char *s1, const QString &s2) |
|
4522 |
\relates QString |
|
4523 |
||
4524 |
Returns a string which is the result of concatenating \a s1 and \a |
|
4525 |
s2 (\a s1 is converted to Unicode using the QString::fromAscii() |
|
4526 |
function). |
|
4527 |
||
4528 |
\sa QString::fromAscii() |
|
4529 |
*/ |
|
4530 |
||
4531 |
/*! |
|
4532 |
\fn const QString operator+(const QString &s, char ch) |
|
4533 |
\relates QString |
|
4534 |
||
4535 |
Returns a string which is the result of concatenating the string |
|
4536 |
\a s and the character \a ch. |
|
4537 |
*/ |
|
4538 |
||
4539 |
/*! |
|
4540 |
\fn const QString operator+(char ch, const QString &s) |
|
4541 |
\relates QString |
|
4542 |
||
4543 |
Returns a string which is the result of concatenating the |
|
4544 |
character \a ch and the string \a s. |
|
4545 |
*/ |
|
4546 |
||
4547 |
/*! |
|
4548 |
\fn int QString::compare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs) |
|
4549 |
\since 4.2 |
|
4550 |
||
4551 |
Compares \a s1 with \a s2 and returns an integer less than, equal |
|
4552 |
to, or greater than zero if \a s1 is less than, equal to, or |
|
4553 |
greater than \a s2. |
|
4554 |
||
4555 |
If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
|
4556 |
otherwise the comparison is case insensitive. |
|
4557 |
||
4558 |
Case sensitive comparison is based exclusively on the numeric |
|
4559 |
Unicode values of the characters and is very fast, but is not what |
|
4560 |
a human would expect. Consider sorting user-visible strings with |
|
4561 |
localeAwareCompare(). |
|
4562 |
||
4563 |
\snippet doc/src/snippets/qstring/main.cpp 16 |
|
4564 |
||
4565 |
\sa operator==(), operator<(), operator>() |
|
4566 |
*/ |
|
4567 |
||
4568 |
/*! |
|
4569 |
\fn int QString::compare(const QString & s1, const QString & s2) |
|
4570 |
||
4571 |
\overload compare() |
|
4572 |
||
4573 |
Performs a case sensitive compare of \a s1 and \a s2. |
|
4574 |
*/ |
|
4575 |
||
4576 |
/*! |
|
4577 |
\fn int QString::compare(const QString &s1, const QLatin1String &s2, Qt::CaseSensitivity cs) |
|
4578 |
\since 4.2 |
|
4579 |
\overload compare() |
|
4580 |
||
4581 |
Performs a comparison of \a s1 and \a s2, using the case |
|
4582 |
sensitivity setting \a cs. |
|
4583 |
*/ |
|
4584 |
||
4585 |
/*! |
|
4586 |
\fn int QString::compare(const QLatin1String &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
|
4587 |
||
4588 |
\since 4.2 |
|
4589 |
\overload compare() |
|
4590 |
||
4591 |
Performs a comparison of \a s1 and \a s2, using the case |
|
4592 |
sensitivity setting \a cs. |
|
4593 |
*/ |
|
4594 |
||
4595 |
/*! |
|
4596 |
\overload compare() |
|
4597 |
||
4598 |
Lexically compares this string with the \a other string and |
|
4599 |
returns an integer less than, equal to, or greater than zero if |
|
4600 |
this string is less than, equal to, or greater than the other |
|
4601 |
string. |
|
4602 |
||
4603 |
Equivalent to \c {compare(*this, other)}. |
|
4604 |
*/ |
|
4605 |
int QString::compare(const QString &other) const |
|
4606 |
{ |
|
4607 |
return ucstrcmp(constData(), length(), other.constData(), other.length()); |
|
4608 |
} |
|
4609 |
||
4610 |
/*! |
|
4611 |
\overload compare() |
|
4612 |
\since 4.2 |
|
4613 |
||
4614 |
Same as compare(*this, \a other, \a cs). |
|
4615 |
*/ |
|
4616 |
int QString::compare(const QString &other, Qt::CaseSensitivity cs) const |
|
4617 |
{ |
|
4618 |
if (cs == Qt::CaseSensitive) |
|
4619 |
return ucstrcmp(constData(), length(), other.constData(), other.length()); |
|
4620 |
return ucstricmp(d->data, d->data + d->size, other.d->data, other.d->data + other.d->size); |
|
4621 |
} |
|
4622 |
||
4623 |
/*! |
|
4624 |
\internal |
|
4625 |
\since 4.5 |
|
4626 |
*/ |
|
4627 |
int QString::compare_helper(const QChar *data1, int length1, const QChar *data2, int length2, |
|
4628 |
Qt::CaseSensitivity cs) |
|
4629 |
{ |
|
4630 |
if (cs == Qt::CaseSensitive) |
|
4631 |
return ucstrcmp(data1, length1, data2, length2); |
|
4632 |
register const ushort *s1 = reinterpret_cast<const ushort *>(data1); |
|
4633 |
register const ushort *s2 = reinterpret_cast<const ushort *>(data2); |
|
4634 |
return ucstricmp(s1, s1 + length1, s2, s2 + length2); |
|
4635 |
} |
|
4636 |
||
4637 |
/*! |
|
4638 |
\overload compare() |
|
4639 |
\since 4.2 |
|
4640 |
||
4641 |
Same as compare(*this, \a other, \a cs). |
|
4642 |
*/ |
|
4643 |
int QString::compare(const QLatin1String &other, Qt::CaseSensitivity cs) const |
|
4644 |
{ |
|
4645 |
return compare_helper(unicode(), length(), other, cs); |
|
4646 |
} |
|
4647 |
||
4648 |
/*! |
|
4649 |
\fn int QString::compare(const QStringRef &ref, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
|
4650 |
\overload compare() |
|
4651 |
||
4652 |
Compares the string reference, \a ref, with the string and returns |
|
4653 |
an integer less than, equal to, or greater than zero if the string |
|
4654 |
is less than, equal to, or greater than \a ref. |
|
4655 |
*/ |
|
4656 |
||
4657 |
/*! |
|
4658 |
\fn int QString::compare(const QString &s1, const QStringRef &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
|
4659 |
\overload compare() |
|
4660 |
*/ |
|
4661 |
||
4662 |
/*! |
|
4663 |
\internal |
|
4664 |
\since 4.5 |
|
4665 |
*/ |
|
4666 |
int QString::compare_helper(const QChar *data1, int length1, QLatin1String s2, |
|
4667 |
Qt::CaseSensitivity cs) |
|
4668 |
{ |
|
4669 |
const ushort *uc = reinterpret_cast<const ushort *>(data1); |
|
4670 |
const ushort *e = uc + length1; |
|
4671 |
const uchar *c = (uchar *)s2.latin1(); |
|
4672 |
||
4673 |
if (!c) |
|
4674 |
return length1; |
|
4675 |
||
4676 |
if (cs == Qt::CaseSensitive) { |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
4677 |
while (uc < e && *c && *uc == *c) |
0 | 4678 |
uc++, c++; |
4679 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
4680 |
if (uc == e) |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
4681 |
return -*c; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
4682 |
|
0 | 4683 |
return *uc - *c; |
4684 |
} else { |
|
4685 |
return ucstricmp(uc, e, c); |
|
4686 |
} |
|
4687 |
} |
|
4688 |
||
4689 |
/*! |
|
4690 |
\fn int QString::localeAwareCompare(const QString & s1, const QString & s2) |
|
4691 |
||
4692 |
Compares \a s1 with \a s2 and returns an integer less than, equal |
|
4693 |
to, or greater than zero if \a s1 is less than, equal to, or |
|
4694 |
greater than \a s2. |
|
4695 |
||
4696 |
The comparison is performed in a locale- and also |
|
4697 |
platform-dependent manner. Use this function to present sorted |
|
4698 |
lists of strings to the user. |
|
4699 |
||
4700 |
On Mac OS X since Qt 4.3, this function compares according the |
|
4701 |
"Order for sorted lists" setting in the International prefereces panel. |
|
4702 |
||
4703 |
\sa compare(), QTextCodec::locale() |
|
4704 |
*/ |
|
4705 |
||
4706 |
/*! |
|
4707 |
\fn int QString::localeAwareCompare(const QStringRef &other) const |
|
4708 |
\since 4.5 |
|
4709 |
\overload localeAwareCompare() |
|
4710 |
||
4711 |
Compares this string with the \a other string and returns an |
|
4712 |
integer less than, equal to, or greater than zero if this string |
|
4713 |
is less than, equal to, or greater than the \a other string. |
|
4714 |
||
4715 |
The comparison is performed in a locale- and also |
|
4716 |
platform-dependent manner. Use this function to present sorted |
|
4717 |
lists of strings to the user. |
|
4718 |
||
4719 |
Same as \c {localeAwareCompare(*this, other)}. |
|
4720 |
*/ |
|
4721 |
||
4722 |
/*! |
|
4723 |
\fn int QString::localeAwareCompare(const QString &s1, const QStringRef &s2) |
|
4724 |
\since 4.5 |
|
4725 |
\overload localeAwareCompare() |
|
4726 |
||
4727 |
Compares \a s1 with \a s2 and returns an integer less than, equal |
|
4728 |
to, or greater than zero if \a s1 is less than, equal to, or |
|
4729 |
greater than \a s2. |
|
4730 |
||
4731 |
The comparison is performed in a locale- and also |
|
4732 |
platform-dependent manner. Use this function to present sorted |
|
4733 |
lists of strings to the user. |
|
4734 |
*/ |
|
4735 |
||
4736 |
||
4737 |
#if !defined(CSTR_LESS_THAN) |
|
4738 |
#define CSTR_LESS_THAN 1 |
|
4739 |
#define CSTR_EQUAL 2 |
|
4740 |
#define CSTR_GREATER_THAN 3 |
|
4741 |
#endif |
|
4742 |
||
4743 |
/*! |
|
4744 |
\overload localeAwareCompare() |
|
4745 |
||
4746 |
Compares this string with the \a other string and returns an |
|
4747 |
integer less than, equal to, or greater than zero if this string |
|
4748 |
is less than, equal to, or greater than the \a other string. |
|
4749 |
||
4750 |
The comparison is performed in a locale- and also |
|
4751 |
platform-dependent manner. Use this function to present sorted |
|
4752 |
lists of strings to the user. |
|
4753 |
||
4754 |
Same as \c {localeAwareCompare(*this, other)}. |
|
4755 |
*/ |
|
4756 |
int QString::localeAwareCompare(const QString &other) const |
|
4757 |
{ |
|
4758 |
return localeAwareCompare_helper(constData(), length(), other.constData(), other.length()); |
|
4759 |
} |
|
4760 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4761 |
#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4762 |
QT_END_NAMESPACE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4763 |
#include "qt_windows.h" |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4764 |
QT_BEGIN_NAMESPACE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4765 |
#endif |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
4766 |
|
0 | 4767 |
/*! |
4768 |
\internal |
|
4769 |
\since 4.5 |
|
4770 |
*/ |
|
4771 |
int QString::localeAwareCompare_helper(const QChar *data1, int length1, |
|
4772 |
const QChar *data2, int length2) |
|
4773 |
{ |
|
4774 |
// do the right thing for null and empty |
|
4775 |
if (length1 == 0 || length2 == 0) |
|
4776 |
return ucstrcmp(data1, length1, data2, length2); |
|
4777 |
||
4778 |
#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE) |
|
4779 |
int res = CompareString(GetUserDefaultLCID(), 0, (wchar_t*)data1, length1, (wchar_t*)data2, length2); |
|
4780 |
||
4781 |
switch (res) { |
|
4782 |
case CSTR_LESS_THAN: |
|
4783 |
return -1; |
|
4784 |
case CSTR_GREATER_THAN: |
|
4785 |
return 1; |
|
4786 |
default: |
|
4787 |
return 0; |
|
4788 |
} |
|
4789 |
#elif defined (Q_OS_MAC) |
|
4790 |
// Use CFStringCompare for comparing strings on Mac. This makes Qt order |
|
4791 |
// strings the same way as native applications do, and also respects |
|
4792 |
// the "Order for sorted lists" setting in the International preferences |
|
4793 |
// panel. |
|
4794 |
const CFStringRef thisString = |
|
4795 |
CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, |
|
4796 |
reinterpret_cast<const UniChar *>(data1), length1, kCFAllocatorNull); |
|
4797 |
const CFStringRef otherString = |
|
4798 |
CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, |
|
4799 |
reinterpret_cast<const UniChar *>(data2), length2, kCFAllocatorNull); |
|
4800 |
||
4801 |
const int result = CFStringCompare(thisString, otherString, kCFCompareLocalized); |
|
4802 |
CFRelease(thisString); |
|
4803 |
CFRelease(otherString); |
|
4804 |
return result; |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
4805 |
#elif defined(Q_OS_SYMBIAN) |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
4806 |
TPtrC p1 = TPtrC16(reinterpret_cast<const TUint16 *>(data1), length1); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
4807 |
TPtrC p2 = TPtrC16(reinterpret_cast<const TUint16 *>(data2), length2); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
4808 |
return p1.CompareC(p2); |
0 | 4809 |
#elif defined(Q_OS_UNIX) |
4810 |
// declared in <string.h> |
|
4811 |
int delta = strcoll(toLocal8Bit_helper(data1, length1), toLocal8Bit_helper(data2, length2)); |
|
4812 |
if (delta == 0) |
|
4813 |
delta = ucstrcmp(data1, length1, data2, length2); |
|
4814 |
return delta; |
|
4815 |
#else |
|
4816 |
return ucstrcmp(data1, length1, data2, length2); |
|
4817 |
#endif |
|
4818 |
} |
|
4819 |
||
4820 |
||
4821 |
/*! |
|
4822 |
\fn const QChar *QString::unicode() const |
|
4823 |
||
4824 |
Returns a '\\0'-terminated Unicode representation of the string. |
|
4825 |
The result remains valid until the string is modified. |
|
4826 |
||
4827 |
\sa utf16() |
|
4828 |
*/ |
|
4829 |
||
4830 |
/*! |
|
4831 |
\fn const ushort *QString::utf16() const |
|
4832 |
||
4833 |
Returns the QString as a '\\0\'-terminated array of unsigned |
|
4834 |
shorts. The result remains valid until the string is modified. |
|
4835 |
||
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
4836 |
The returned string is in host byte order. |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
4837 |
|
0 | 4838 |
\sa unicode() |
4839 |
*/ |
|
4840 |
||
4841 |
const ushort *QString::utf16() const |
|
4842 |
{ |
|
4843 |
if (d->data != d->array) { |
|
4844 |
QString *that = const_cast<QString*>(this); |
|
4845 |
that->realloc(); // ensure '\\0'-termination for ::fromRawData strings |
|
4846 |
return that->d->data; |
|
4847 |
} |
|
4848 |
return d->array; |
|
4849 |
} |
|
4850 |
||
4851 |
/*! |
|
4852 |
Returns a string of size \a width that contains this string |
|
4853 |
padded by the \a fill character. |
|
4854 |
||
4855 |
If \a truncate is false and the size() of the string is more than |
|
4856 |
\a width, then the returned string is a copy of the string. |
|
4857 |
||
4858 |
\snippet doc/src/snippets/qstring/main.cpp 32 |
|
4859 |
||
4860 |
If \a truncate is true and the size() of the string is more than |
|
4861 |
\a width, then any characters in a copy of the string after |
|
4862 |
position \a width are removed, and the copy is returned. |
|
4863 |
||
4864 |
\snippet doc/src/snippets/qstring/main.cpp 33 |
|
4865 |
||
4866 |
\sa rightJustified() |
|
4867 |
*/ |
|
4868 |
||
4869 |
QString QString::leftJustified(int width, QChar fill, bool truncate) const |
|
4870 |
{ |
|
4871 |
QString result; |
|
4872 |
int len = length(); |
|
4873 |
int padlen = width - len; |
|
4874 |
if (padlen > 0) { |
|
4875 |
result.resize(len+padlen); |
|
4876 |
if (len) |
|
4877 |
memcpy(result.d->data, d->data, sizeof(QChar)*len); |
|
4878 |
QChar *uc = (QChar*)result.d->data + len; |
|
4879 |
while (padlen--) |
|
4880 |
* uc++ = fill; |
|
4881 |
} else { |
|
4882 |
if (truncate) |
|
4883 |
result = left(width); |
|
4884 |
else |
|
4885 |
result = *this; |
|
4886 |
} |
|
4887 |
return result; |
|
4888 |
} |
|
4889 |
||
4890 |
/*! |
|
4891 |
Returns a string of size() \a width that contains the \a fill |
|
4892 |
character followed by the string. For example: |
|
4893 |
||
4894 |
\snippet doc/src/snippets/qstring/main.cpp 49 |
|
4895 |
||
4896 |
If \a truncate is false and the size() of the string is more than |
|
4897 |
\a width, then the returned string is a copy of the string. |
|
4898 |
||
4899 |
If \a truncate is true and the size() of the string is more than |
|
4900 |
\a width, then the resulting string is truncated at position \a |
|
4901 |
width. |
|
4902 |
||
4903 |
\snippet doc/src/snippets/qstring/main.cpp 50 |
|
4904 |
||
4905 |
\sa leftJustified() |
|
4906 |
*/ |
|
4907 |
||
4908 |
QString QString::rightJustified(int width, QChar fill, bool truncate) const |
|
4909 |
{ |
|
4910 |
QString result; |
|
4911 |
int len = length(); |
|
4912 |
int padlen = width - len; |
|
4913 |
if (padlen > 0) { |
|
4914 |
result.resize(len+padlen); |
|
4915 |
QChar *uc = (QChar*)result.d->data; |
|
4916 |
while (padlen--) |
|
4917 |
* uc++ = fill; |
|
4918 |
if (len) |
|
4919 |
memcpy(uc, d->data, sizeof(QChar)*len); |
|
4920 |
} else { |
|
4921 |
if (truncate) |
|
4922 |
result = left(width); |
|
4923 |
else |
|
4924 |
result = *this; |
|
4925 |
} |
|
4926 |
return result; |
|
4927 |
} |
|
4928 |
||
4929 |
/*! |
|
4930 |
Returns a lowercase copy of the string. |
|
4931 |
||
4932 |
\snippet doc/src/snippets/qstring/main.cpp 75 |
|
4933 |
||
4934 |
\sa toUpper() |
|
4935 |
*/ |
|
4936 |
||
4937 |
QString QString::toLower() const |
|
4938 |
{ |
|
4939 |
const ushort *p = d->data; |
|
4940 |
if (!p) |
|
4941 |
return *this; |
|
4942 |
if (!d->size) |
|
4943 |
return *this; |
|
4944 |
||
4945 |
const ushort *e = d->data + d->size; |
|
4946 |
||
4947 |
// this avoids one out of bounds check in the loop |
|
4948 |
if (QChar(*p).isLowSurrogate()) |
|
4949 |
++p; |
|
4950 |
||
4951 |
while (p != e) { |
|
4952 |
uint c = *p; |
|
4953 |
if (QChar(c).isLowSurrogate() && QChar(*(p - 1)).isHighSurrogate()) |
|
4954 |
c = QChar::surrogateToUcs4(*(p - 1), c); |
|
4955 |
const QUnicodeTables::Properties *prop = qGetProp(c); |
|
4956 |
if (prop->lowerCaseDiff || prop->lowerCaseSpecial) { |
|
4957 |
QString s(d->size, Qt::Uninitialized); |
|
4958 |
memcpy(s.d->data, d->data, (p - d->data)*sizeof(ushort)); |
|
4959 |
ushort *pp = s.d->data + (p - d->data); |
|
4960 |
while (p < e) { |
|
4961 |
uint c = *p; |
|
4962 |
if (QChar(c).isLowSurrogate() && QChar(*(p - 1)).isHighSurrogate()) |
|
4963 |
c = QChar::surrogateToUcs4(*(p - 1), c); |
|
4964 |
prop = qGetProp(c); |
|
4965 |
if (prop->lowerCaseSpecial) { |
|
4966 |
int pos = pp - s.d->data; |
|
4967 |
s.resize(s.d->size + SPECIAL_CASE_MAX_LEN); |
|
4968 |
pp = s.d->data + pos; |
|
4969 |
const ushort *specialCase = specialCaseMap + prop->lowerCaseDiff; |
|
4970 |
while (*specialCase) |
|
4971 |
*pp++ = *specialCase++; |
|
4972 |
} else { |
|
4973 |
*pp++ = *p + prop->lowerCaseDiff; |
|
4974 |
} |
|
4975 |
++p; |
|
4976 |
} |
|
4977 |
s.truncate(pp - s.d->data); |
|
4978 |
return s; |
|
4979 |
} |
|
4980 |
++p; |
|
4981 |
} |
|
4982 |
return *this; |
|
4983 |
} |
|
4984 |
||
4985 |
/*! |
|
4986 |
Returns the case folded equivalent of the string. For most Unicode |
|
4987 |
characters this is the same as toLower(). |
|
4988 |
*/ |
|
4989 |
QString QString::toCaseFolded() const |
|
4990 |
{ |
|
4991 |
if (!d->size) |
|
4992 |
return *this; |
|
4993 |
||
4994 |
const ushort *p = d->data; |
|
4995 |
if (!p) |
|
4996 |
return *this; |
|
4997 |
||
4998 |
const ushort *e = d->data + d->size; |
|
4999 |
||
5000 |
uint last = 0; |
|
5001 |
while (p < e) { |
|
5002 |
ushort folded = foldCase(*p, last); |
|
5003 |
if (folded != *p) { |
|
5004 |
QString s(*this); |
|
5005 |
s.detach(); |
|
5006 |
ushort *pp = s.d->data + (p - d->data); |
|
5007 |
const ushort *ppe = s.d->data + s.d->size; |
|
5008 |
last = pp > s.d->data ? *(pp - 1) : 0; |
|
5009 |
while (pp < ppe) { |
|
5010 |
*pp = foldCase(*pp, last); |
|
5011 |
++pp; |
|
5012 |
} |
|
5013 |
return s; |
|
5014 |
} |
|
5015 |
p++; |
|
5016 |
} |
|
5017 |
return *this; |
|
5018 |
} |
|
5019 |
||
5020 |
/*! |
|
5021 |
Returns an uppercase copy of the string. |
|
5022 |
||
5023 |
\snippet doc/src/snippets/qstring/main.cpp 81 |
|
5024 |
||
5025 |
\sa toLower() |
|
5026 |
*/ |
|
5027 |
||
5028 |
QString QString::toUpper() const |
|
5029 |
{ |
|
5030 |
const ushort *p = d->data; |
|
5031 |
if (!p) |
|
5032 |
return *this; |
|
5033 |
if (!d->size) |
|
5034 |
return *this; |
|
5035 |
||
5036 |
const ushort *e = d->data + d->size; |
|
5037 |
||
5038 |
// this avoids one out of bounds check in the loop |
|
5039 |
if (QChar(*p).isLowSurrogate()) |
|
5040 |
++p; |
|
5041 |
||
5042 |
while (p != e) { |
|
5043 |
uint c = *p; |
|
5044 |
if (QChar(c).isLowSurrogate() && QChar(*(p - 1)).isHighSurrogate()) |
|
5045 |
c = QChar::surrogateToUcs4(*(p - 1), c); |
|
5046 |
const QUnicodeTables::Properties *prop = qGetProp(c); |
|
5047 |
if (prop->upperCaseDiff || prop->upperCaseSpecial) { |
|
5048 |
QString s(d->size, Qt::Uninitialized); |
|
5049 |
memcpy(s.d->data, d->data, (p - d->data)*sizeof(ushort)); |
|
5050 |
ushort *pp = s.d->data + (p - d->data); |
|
5051 |
while (p < e) { |
|
5052 |
uint c = *p; |
|
5053 |
if (QChar(c).isLowSurrogate() && QChar(*(p - 1)).isHighSurrogate()) |
|
5054 |
c = QChar::surrogateToUcs4(*(p - 1), c); |
|
5055 |
prop = qGetProp(c); |
|
5056 |
if (prop->upperCaseSpecial) { |
|
5057 |
int pos = pp - s.d->data; |
|
5058 |
s.resize(s.d->size + SPECIAL_CASE_MAX_LEN); |
|
5059 |
pp = s.d->data + pos; |
|
5060 |
const ushort *specialCase = specialCaseMap + prop->upperCaseDiff; |
|
5061 |
while (*specialCase) |
|
5062 |
*pp++ = *specialCase++; |
|
5063 |
} else { |
|
5064 |
*pp++ = *p + prop->upperCaseDiff; |
|
5065 |
} |
|
5066 |
++p; |
|
5067 |
} |
|
5068 |
s.truncate(pp - s.d->data); |
|
5069 |
return s; |
|
5070 |
} |
|
5071 |
++p; |
|
5072 |
} |
|
5073 |
return *this; |
|
5074 |
} |
|
5075 |
||
5076 |
// ### Qt 5: Consider whether this function shouldn't be removed See task 202871. |
|
5077 |
/*! |
|
5078 |
Safely builds a formatted string from the format string \a cformat |
|
5079 |
and an arbitrary list of arguments. |
|
5080 |
||
5081 |
The %lc escape sequence expects a unicode character of type ushort |
|
5082 |
(as returned by QChar::unicode()). The %ls escape sequence expects |
|
5083 |
a pointer to a zero-terminated array of unicode characters of type |
|
5084 |
ushort (as returned by QString::utf16()). |
|
5085 |
||
5086 |
\note This function expects a UTF-8 string for %s and Latin-1 for |
|
5087 |
the format string. |
|
5088 |
||
5089 |
The format string supports most of the conversion specifiers |
|
5090 |
provided by printf() in the standard C++ library. It doesn't |
|
5091 |
honor the length modifiers (e.g. \c h for \c short, \c ll for |
|
5092 |
\c{long long}). If you need those, use the standard snprintf() |
|
5093 |
function instead: |
|
5094 |
||
5095 |
\snippet doc/src/snippets/qstring/main.cpp 63 |
|
5096 |
||
5097 |
\warning We do not recommend using QString::sprintf() in new Qt |
|
5098 |
code. Instead, consider using QTextStream or arg(), both of |
|
5099 |
which support Unicode strings seamlessly and are type-safe. |
|
5100 |
Here's an example that uses QTextStream: |
|
5101 |
||
5102 |
\snippet doc/src/snippets/qstring/main.cpp 64 |
|
5103 |
||
5104 |
For \l {QObject::tr()}{translations}, especially if the strings |
|
5105 |
contains more than one escape sequence, you should consider using |
|
5106 |
the arg() function instead. This allows the order of the |
|
5107 |
replacements to be controlled by the translator. |
|
5108 |
||
5109 |
\sa arg() |
|
5110 |
*/ |
|
5111 |
||
5112 |
QString &QString::sprintf(const char *cformat, ...) |
|
5113 |
{ |
|
5114 |
va_list ap; |
|
5115 |
va_start(ap, cformat); |
|
5116 |
QString &s = vsprintf(cformat, ap); |
|
5117 |
va_end(ap); |
|
5118 |
return s; |
|
5119 |
} |
|
5120 |
||
5121 |
/*! |
|
5122 |
Equivalent method to sprintf(), but takes a va_list \a ap |
|
5123 |
instead a list of variable arguments. See the sprintf() |
|
5124 |
documentation for an explanation of \a cformat. |
|
5125 |
||
5126 |
This method does not call the va_end macro, the caller |
|
5127 |
is responsible to call va_end on \a ap. |
|
5128 |
||
5129 |
\sa sprintf() |
|
5130 |
*/ |
|
5131 |
||
5132 |
QString &QString::vsprintf(const char* cformat, va_list ap) |
|
5133 |
{ |
|
5134 |
QLocale locale(QLocale::C); |
|
5135 |
||
5136 |
if (!cformat || !*cformat) { |
|
5137 |
// Qt 1.x compat |
|
5138 |
*this = fromLatin1(""); |
|
5139 |
return *this; |
|
5140 |
} |
|
5141 |
||
5142 |
// Parse cformat |
|
5143 |
||
5144 |
QString result; |
|
5145 |
const char *c = cformat; |
|
5146 |
for (;;) { |
|
5147 |
// Copy non-escape chars to result |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
5148 |
#ifndef QT_NO_TEXTCODEC |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
5149 |
int i = 0; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
5150 |
while (*(c + i) != '\0' && *(c + i) != '%') |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
5151 |
++i; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
5152 |
if (codecForCStrings) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
5153 |
result.append(codecForCStrings->toUnicode(c, i)); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
5154 |
else |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
5155 |
result.append(fromLatin1(c, i)); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
5156 |
c += i; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
5157 |
#else |
0 | 5158 |
while (*c != '\0' && *c != '%') |
5159 |
result.append(QLatin1Char(*c++)); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
5160 |
#endif |
0 | 5161 |
|
5162 |
if (*c == '\0') |
|
5163 |
break; |
|
5164 |
||
5165 |
// Found '%' |
|
5166 |
const char *escape_start = c; |
|
5167 |
++c; |
|
5168 |
||
5169 |
if (*c == '\0') { |
|
5170 |
result.append(QLatin1Char('%')); // a % at the end of the string - treat as non-escape text |
|
5171 |
break; |
|
5172 |
} |
|
5173 |
if (*c == '%') { |
|
5174 |
result.append(QLatin1Char('%')); // %% |
|
5175 |
++c; |
|
5176 |
continue; |
|
5177 |
} |
|
5178 |
||
5179 |
// Parse flag characters |
|
5180 |
uint flags = 0; |
|
5181 |
bool no_more_flags = false; |
|
5182 |
do { |
|
5183 |
switch (*c) { |
|
5184 |
case '#': flags |= QLocalePrivate::Alternate; break; |
|
5185 |
case '0': flags |= QLocalePrivate::ZeroPadded; break; |
|
5186 |
case '-': flags |= QLocalePrivate::LeftAdjusted; break; |
|
5187 |
case ' ': flags |= QLocalePrivate::BlankBeforePositive; break; |
|
5188 |
case '+': flags |= QLocalePrivate::AlwaysShowSign; break; |
|
5189 |
case '\'': flags |= QLocalePrivate::ThousandsGroup; break; |
|
5190 |
default: no_more_flags = true; break; |
|
5191 |
} |
|
5192 |
||
5193 |
if (!no_more_flags) |
|
5194 |
++c; |
|
5195 |
} while (!no_more_flags); |
|
5196 |
||
5197 |
if (*c == '\0') { |
|
5198 |
result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text |
|
5199 |
break; |
|
5200 |
} |
|
5201 |
||
5202 |
// Parse field width |
|
5203 |
int width = -1; // -1 means unspecified |
|
5204 |
if (qIsDigit(*c)) { |
|
5205 |
QString width_str; |
|
5206 |
while (*c != '\0' && qIsDigit(*c)) |
|
5207 |
width_str.append(QLatin1Char(*c++)); |
|
5208 |
||
5209 |
// can't be negative - started with a digit |
|
5210 |
// contains at least one digit |
|
5211 |
width = width_str.toInt(); |
|
5212 |
} |
|
5213 |
else if (*c == '*') { |
|
5214 |
width = va_arg(ap, int); |
|
5215 |
if (width < 0) |
|
5216 |
width = -1; // treat all negative numbers as unspecified |
|
5217 |
++c; |
|
5218 |
} |
|
5219 |
||
5220 |
if (*c == '\0') { |
|
5221 |
result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text |
|
5222 |
break; |
|
5223 |
} |
|
5224 |
||
5225 |
// Parse precision |
|
5226 |
int precision = -1; // -1 means unspecified |
|
5227 |
if (*c == '.') { |
|
5228 |
++c; |
|
5229 |
if (qIsDigit(*c)) { |
|
5230 |
QString precision_str; |
|
5231 |
while (*c != '\0' && qIsDigit(*c)) |
|
5232 |
precision_str.append(QLatin1Char(*c++)); |
|
5233 |
||
5234 |
// can't be negative - started with a digit |
|
5235 |
// contains at least one digit |
|
5236 |
precision = precision_str.toInt(); |
|
5237 |
} |
|
5238 |
else if (*c == '*') { |
|
5239 |
precision = va_arg(ap, int); |
|
5240 |
if (precision < 0) |
|
5241 |
precision = -1; // treat all negative numbers as unspecified |
|
5242 |
++c; |
|
5243 |
} |
|
5244 |
} |
|
5245 |
||
5246 |
if (*c == '\0') { |
|
5247 |
result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text |
|
5248 |
break; |
|
5249 |
} |
|
5250 |
||
5251 |
// Parse the length modifier |
|
5252 |
enum LengthMod { lm_none, lm_hh, lm_h, lm_l, lm_ll, lm_L, lm_j, lm_z, lm_t }; |
|
5253 |
LengthMod length_mod = lm_none; |
|
5254 |
switch (*c) { |
|
5255 |
case 'h': |
|
5256 |
++c; |
|
5257 |
if (*c == 'h') { |
|
5258 |
length_mod = lm_hh; |
|
5259 |
++c; |
|
5260 |
} |
|
5261 |
else |
|
5262 |
length_mod = lm_h; |
|
5263 |
break; |
|
5264 |
||
5265 |
case 'l': |
|
5266 |
++c; |
|
5267 |
if (*c == 'l') { |
|
5268 |
length_mod = lm_ll; |
|
5269 |
++c; |
|
5270 |
} |
|
5271 |
else |
|
5272 |
length_mod = lm_l; |
|
5273 |
break; |
|
5274 |
||
5275 |
case 'L': |
|
5276 |
++c; |
|
5277 |
length_mod = lm_L; |
|
5278 |
break; |
|
5279 |
||
5280 |
case 'j': |
|
5281 |
++c; |
|
5282 |
length_mod = lm_j; |
|
5283 |
break; |
|
5284 |
||
5285 |
case 'z': |
|
5286 |
case 'Z': |
|
5287 |
++c; |
|
5288 |
length_mod = lm_z; |
|
5289 |
break; |
|
5290 |
||
5291 |
case 't': |
|
5292 |
++c; |
|
5293 |
length_mod = lm_t; |
|
5294 |
break; |
|
5295 |
||
5296 |
default: break; |
|
5297 |
} |
|
5298 |
||
5299 |
if (*c == '\0') { |
|
5300 |
result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text |
|
5301 |
break; |
|
5302 |
} |
|
5303 |
||
5304 |
// Parse the conversion specifier and do the conversion |
|
5305 |
QString subst; |
|
5306 |
switch (*c) { |
|
5307 |
case 'd': |
|
5308 |
case 'i': { |
|
5309 |
qint64 i; |
|
5310 |
switch (length_mod) { |
|
5311 |
case lm_none: i = va_arg(ap, int); break; |
|
5312 |
case lm_hh: i = va_arg(ap, int); break; |
|
5313 |
case lm_h: i = va_arg(ap, int); break; |
|
5314 |
case lm_l: i = va_arg(ap, long int); break; |
|
5315 |
case lm_ll: i = va_arg(ap, qint64); break; |
|
5316 |
case lm_j: i = va_arg(ap, long int); break; |
|
5317 |
case lm_z: i = va_arg(ap, size_t); break; |
|
5318 |
case lm_t: i = va_arg(ap, int); break; |
|
5319 |
default: i = 0; break; |
|
5320 |
} |
|
5321 |
subst = locale.d()->longLongToString(i, precision, 10, width, flags); |
|
5322 |
++c; |
|
5323 |
break; |
|
5324 |
} |
|
5325 |
case 'o': |
|
5326 |
case 'u': |
|
5327 |
case 'x': |
|
5328 |
case 'X': { |
|
5329 |
quint64 u; |
|
5330 |
switch (length_mod) { |
|
5331 |
case lm_none: u = va_arg(ap, uint); break; |
|
5332 |
case lm_hh: u = va_arg(ap, uint); break; |
|
5333 |
case lm_h: u = va_arg(ap, uint); break; |
|
5334 |
case lm_l: u = va_arg(ap, ulong); break; |
|
5335 |
case lm_ll: u = va_arg(ap, quint64); break; |
|
5336 |
case lm_z: u = va_arg(ap, size_t); break; |
|
5337 |
default: u = 0; break; |
|
5338 |
} |
|
5339 |
||
5340 |
if (qIsUpper(*c)) |
|
5341 |
flags |= QLocalePrivate::CapitalEorX; |
|
5342 |
||
5343 |
int base = 10; |
|
5344 |
switch (qToLower(*c)) { |
|
5345 |
case 'o': |
|
5346 |
base = 8; break; |
|
5347 |
case 'u': |
|
5348 |
base = 10; break; |
|
5349 |
case 'x': |
|
5350 |
base = 16; break; |
|
5351 |
default: break; |
|
5352 |
} |
|
5353 |
subst = locale.d()->unsLongLongToString(u, precision, base, width, flags); |
|
5354 |
++c; |
|
5355 |
break; |
|
5356 |
} |
|
5357 |
case 'E': |
|
5358 |
case 'e': |
|
5359 |
case 'F': |
|
5360 |
case 'f': |
|
5361 |
case 'G': |
|
5362 |
case 'g': |
|
5363 |
case 'A': |
|
5364 |
case 'a': { |
|
5365 |
double d; |
|
5366 |
if (length_mod == lm_L) |
|
5367 |
d = va_arg(ap, long double); // not supported - converted to a double |
|
5368 |
else |
|
5369 |
d = va_arg(ap, double); |
|
5370 |
||
5371 |
if (qIsUpper(*c)) |
|
5372 |
flags |= QLocalePrivate::CapitalEorX; |
|
5373 |
||
5374 |
QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal; |
|
5375 |
switch (qToLower(*c)) { |
|
5376 |
case 'e': form = QLocalePrivate::DFExponent; break; |
|
5377 |
case 'a': // not supported - decimal form used instead |
|
5378 |
case 'f': form = QLocalePrivate::DFDecimal; break; |
|
5379 |
case 'g': form = QLocalePrivate::DFSignificantDigits; break; |
|
5380 |
default: break; |
|
5381 |
} |
|
5382 |
subst = locale.d()->doubleToString(d, precision, form, width, flags); |
|
5383 |
++c; |
|
5384 |
break; |
|
5385 |
} |
|
5386 |
case 'c': { |
|
5387 |
if (length_mod == lm_l) |
|
5388 |
subst = QChar((ushort) va_arg(ap, int)); |
|
5389 |
else |
|
5390 |
subst = QLatin1Char((uchar) va_arg(ap, int)); |
|
5391 |
++c; |
|
5392 |
break; |
|
5393 |
} |
|
5394 |
case 's': { |
|
5395 |
if (length_mod == lm_l) { |
|
5396 |
const ushort *buff = va_arg(ap, const ushort*); |
|
5397 |
const ushort *ch = buff; |
|
5398 |
while (*ch != 0) |
|
5399 |
++ch; |
|
5400 |
subst.setUtf16(buff, ch - buff); |
|
5401 |
} else |
|
5402 |
subst = QString::fromUtf8(va_arg(ap, const char*)); |
|
5403 |
if (precision != -1) |
|
5404 |
subst.truncate(precision); |
|
5405 |
++c; |
|
5406 |
break; |
|
5407 |
} |
|
5408 |
case 'p': { |
|
5409 |
void *arg = va_arg(ap, void*); |
|
5410 |
#ifdef Q_OS_WIN64 |
|
5411 |
quint64 i = reinterpret_cast<quint64>(arg); |
|
5412 |
#else |
|
5413 |
quint64 i = reinterpret_cast<unsigned long>(arg); |
|
5414 |
#endif |
|
5415 |
flags |= QLocalePrivate::Alternate; |
|
5416 |
subst = locale.d()->unsLongLongToString(i, precision, 16, width, flags); |
|
5417 |
++c; |
|
5418 |
break; |
|
5419 |
} |
|
5420 |
case 'n': |
|
5421 |
switch (length_mod) { |
|
5422 |
case lm_hh: { |
|
5423 |
signed char *n = va_arg(ap, signed char*); |
|
5424 |
*n = result.length(); |
|
5425 |
break; |
|
5426 |
} |
|
5427 |
case lm_h: { |
|
5428 |
short int *n = va_arg(ap, short int*); |
|
5429 |
*n = result.length(); |
|
5430 |
break; |
|
5431 |
} |
|
5432 |
case lm_l: { |
|
5433 |
long int *n = va_arg(ap, long int*); |
|
5434 |
*n = result.length(); |
|
5435 |
break; |
|
5436 |
} |
|
5437 |
case lm_ll: { |
|
5438 |
qint64 *n = va_arg(ap, qint64*); |
|
5439 |
volatile uint tmp = result.length(); // egcs-2.91.66 gets internal |
|
5440 |
*n = tmp; // compiler error without volatile |
|
5441 |
break; |
|
5442 |
} |
|
5443 |
default: { |
|
5444 |
int *n = va_arg(ap, int*); |
|
5445 |
*n = result.length(); |
|
5446 |
break; |
|
5447 |
} |
|
5448 |
} |
|
5449 |
++c; |
|
5450 |
break; |
|
5451 |
||
5452 |
default: // bad escape, treat as non-escape text |
|
5453 |
for (const char *cc = escape_start; cc != c; ++cc) |
|
5454 |
result.append(QLatin1Char(*cc)); |
|
5455 |
continue; |
|
5456 |
} |
|
5457 |
||
5458 |
if (flags & QLocalePrivate::LeftAdjusted) |
|
5459 |
result.append(subst.leftJustified(width)); |
|
5460 |
else |
|
5461 |
result.append(subst.rightJustified(width)); |
|
5462 |
} |
|
5463 |
||
5464 |
*this = result; |
|
5465 |
||
5466 |
return *this; |
|
5467 |
} |
|
5468 |
||
5469 |
/*! |
|
5470 |
Returns the string converted to a \c{long long} using base \a |
|
5471 |
base, which is 10 by default and must be between 2 and 36, or 0. |
|
5472 |
Returns 0 if the conversion fails. |
|
5473 |
||
5474 |
If a conversion error occurs, *\a{ok} is set to false; otherwise |
|
5475 |
*\a{ok} is set to true. |
|
5476 |
||
5477 |
If \a base is 0, the C language convention is used: If the string |
|
5478 |
begins with "0x", base 16 is used; if the string begins with "0", |
|
5479 |
base 8 is used; otherwise, base 10 is used. |
|
5480 |
||
5481 |
Example: |
|
5482 |
||
5483 |
\snippet doc/src/snippets/qstring/main.cpp 74 |
|
5484 |
||
5485 |
\sa number(), toULongLong(), toInt() |
|
5486 |
*/ |
|
5487 |
||
5488 |
qint64 QString::toLongLong(bool *ok, int base) const |
|
5489 |
{ |
|
5490 |
#if defined(QT_CHECK_RANGE) |
|
5491 |
if (base != 0 && (base < 2 || base > 36)) { |
|
5492 |
qWarning("QString::toLongLong: Invalid base (%d)", base); |
|
5493 |
base = 10; |
|
5494 |
} |
|
5495 |
#endif |
|
5496 |
||
5497 |
bool my_ok; |
|
5498 |
QLocale def_locale; |
|
5499 |
qint64 result = def_locale.d()->stringToLongLong(*this, base, &my_ok, QLocalePrivate::FailOnGroupSeparators); |
|
5500 |
if (my_ok) { |
|
5501 |
if (ok != 0) |
|
5502 |
*ok = true; |
|
5503 |
return result; |
|
5504 |
} |
|
5505 |
||
5506 |
QLocale c_locale(QLocale::C); |
|
5507 |
return c_locale.d()->stringToLongLong(*this, base, ok, QLocalePrivate::FailOnGroupSeparators); |
|
5508 |
} |
|
5509 |
||
5510 |
/*! |
|
5511 |
Returns the string converted to an \c{unsigned long long} using base \a |
|
5512 |
base, which is 10 by default and must be between 2 and 36, or 0. |
|
5513 |
Returns 0 if the conversion fails. |
|
5514 |
||
5515 |
If a conversion error occurs, *\a{ok} is set to false; otherwise |
|
5516 |
*\a{ok} is set to true. |
|
5517 |
||
5518 |
If \a base is 0, the C language convention is used: If the string |
|
5519 |
begins with "0x", base 16 is used; if the string begins with "0", |
|
5520 |
base 8 is used; otherwise, base 10 is used. |
|
5521 |
||
5522 |
Example: |
|
5523 |
||
5524 |
\snippet doc/src/snippets/qstring/main.cpp 79 |
|
5525 |
||
5526 |
\sa number(), toLongLong() |
|
5527 |
*/ |
|
5528 |
||
5529 |
quint64 QString::toULongLong(bool *ok, int base) const |
|
5530 |
{ |
|
5531 |
#if defined(QT_CHECK_RANGE) |
|
5532 |
if (base != 0 && (base < 2 || base > 36)) { |
|
5533 |
qWarning("QString::toULongLong: Invalid base (%d)", base); |
|
5534 |
base = 10; |
|
5535 |
} |
|
5536 |
#endif |
|
5537 |
||
5538 |
bool my_ok; |
|
5539 |
QLocale def_locale; |
|
5540 |
quint64 result = def_locale.d()->stringToUnsLongLong(*this, base, &my_ok, QLocalePrivate::FailOnGroupSeparators); |
|
5541 |
if (my_ok) { |
|
5542 |
if (ok != 0) |
|
5543 |
*ok = true; |
|
5544 |
return result; |
|
5545 |
} |
|
5546 |
||
5547 |
QLocale c_locale(QLocale::C); |
|
5548 |
return c_locale.d()->stringToUnsLongLong(*this, base, ok, QLocalePrivate::FailOnGroupSeparators); |
|
5549 |
} |
|
5550 |
||
5551 |
/*! |
|
5552 |
\fn long QString::toLong(bool *ok, int base) const |
|
5553 |
||
5554 |
Returns the string converted to a \c long using base \a |
|
5555 |
base, which is 10 by default and must be between 2 and 36, or 0. |
|
5556 |
Returns 0 if the conversion fails. |
|
5557 |
||
5558 |
If a conversion error occurs, *\a{ok} is set to false; otherwise |
|
5559 |
*\a{ok} is set to true. |
|
5560 |
||
5561 |
If \a base is 0, the C language convention is used: If the string |
|
5562 |
begins with "0x", base 16 is used; if the string begins with "0", |
|
5563 |
base 8 is used; otherwise, base 10 is used. |
|
5564 |
||
5565 |
Example: |
|
5566 |
||
5567 |
\snippet doc/src/snippets/qstring/main.cpp 73 |
|
5568 |
||
5569 |
\sa number(), toULong(), toInt() |
|
5570 |
*/ |
|
5571 |
||
5572 |
long QString::toLong(bool *ok, int base) const |
|
5573 |
{ |
|
5574 |
qint64 v = toLongLong(ok, base); |
|
5575 |
if (v < LONG_MIN || v > LONG_MAX) { |
|
5576 |
if (ok) |
|
5577 |
*ok = false; |
|
5578 |
v = 0; |
|
5579 |
} |
|
5580 |
return (long)v; |
|
5581 |
} |
|
5582 |
||
5583 |
/*! |
|
5584 |
\fn ulong QString::toULong(bool *ok, int base) const |
|
5585 |
||
5586 |
Returns the string converted to an \c{unsigned long} using base \a |
|
5587 |
base, which is 10 by default and must be between 2 and 36, or 0. |
|
5588 |
Returns 0 if the conversion fails. |
|
5589 |
||
5590 |
If a conversion error occurs, *\a{ok} is set to false; otherwise |
|
5591 |
*\a{ok} is set to true. |
|
5592 |
||
5593 |
If \a base is 0, the C language convention is used: If the string |
|
5594 |
begins with "0x", base 16 is used; if the string begins with "0", |
|
5595 |
base 8 is used; otherwise, base 10 is used. |
|
5596 |
||
5597 |
Example: |
|
5598 |
||
5599 |
\snippet doc/src/snippets/qstring/main.cpp 78 |
|
5600 |
||
5601 |
\sa number() |
|
5602 |
*/ |
|
5603 |
||
5604 |
ulong QString::toULong(bool *ok, int base) const |
|
5605 |
{ |
|
5606 |
quint64 v = toULongLong(ok, base); |
|
5607 |
if (v > ULONG_MAX) { |
|
5608 |
if (ok) |
|
5609 |
*ok = false; |
|
5610 |
v = 0; |
|
5611 |
} |
|
5612 |
return (ulong)v; |
|
5613 |
} |
|
5614 |
||
5615 |
||
5616 |
/*! |
|
5617 |
Returns the string converted to an \c int using base \a |
|
5618 |
base, which is 10 by default and must be between 2 and 36, or 0. |
|
5619 |
Returns 0 if the conversion fails. |
|
5620 |
||
5621 |
If a conversion error occurs, *\a{ok} is set to false; otherwise |
|
5622 |
*\a{ok} is set to true. |
|
5623 |
||
5624 |
If \a base is 0, the C language convention is used: If the string |
|
5625 |
begins with "0x", base 16 is used; if the string begins with "0", |
|
5626 |
base 8 is used; otherwise, base 10 is used. |
|
5627 |
||
5628 |
Example: |
|
5629 |
||
5630 |
\snippet doc/src/snippets/qstring/main.cpp 72 |
|
5631 |
||
5632 |
\sa number(), toUInt(), toDouble() |
|
5633 |
*/ |
|
5634 |
||
5635 |
int QString::toInt(bool *ok, int base) const |
|
5636 |
{ |
|
5637 |
qint64 v = toLongLong(ok, base); |
|
5638 |
if (v < INT_MIN || v > INT_MAX) { |
|
5639 |
if (ok) |
|
5640 |
*ok = false; |
|
5641 |
v = 0; |
|
5642 |
} |
|
5643 |
return v; |
|
5644 |
} |
|
5645 |
||
5646 |
/*! |
|
5647 |
Returns the string converted to an \c{unsigned int} using base \a |
|
5648 |
base, which is 10 by default and must be between 2 and 36, or 0. |
|
5649 |
Returns 0 if the conversion fails. |
|
5650 |
||
5651 |
If a conversion error occurs, *\a{ok} is set to false; otherwise |
|
5652 |
*\a{ok} is set to true. |
|
5653 |
||
5654 |
If \a base is 0, the C language convention is used: If the string |
|
5655 |
begins with "0x", base 16 is used; if the string begins with "0", |
|
5656 |
base 8 is used; otherwise, base 10 is used. |
|
5657 |
||
5658 |
Example: |
|
5659 |
||
5660 |
\snippet doc/src/snippets/qstring/main.cpp 77 |
|
5661 |
||
5662 |
\sa number(), toInt() |
|
5663 |
*/ |
|
5664 |
||
5665 |
uint QString::toUInt(bool *ok, int base) const |
|
5666 |
{ |
|
5667 |
quint64 v = toULongLong(ok, base); |
|
5668 |
if (v > UINT_MAX) { |
|
5669 |
if (ok) |
|
5670 |
*ok = false; |
|
5671 |
v = 0; |
|
5672 |
} |
|
5673 |
return (uint)v; |
|
5674 |
} |
|
5675 |
||
5676 |
/*! |
|
5677 |
Returns the string converted to a \c short using base \a |
|
5678 |
base, which is 10 by default and must be between 2 and 36, or 0. |
|
5679 |
Returns 0 if the conversion fails. |
|
5680 |
||
5681 |
If a conversion error occurs, *\a{ok} is set to false; otherwise |
|
5682 |
*\a{ok} is set to true. |
|
5683 |
||
5684 |
If \a base is 0, the C language convention is used: If the string |
|
5685 |
begins with "0x", base 16 is used; if the string begins with "0", |
|
5686 |
base 8 is used; otherwise, base 10 is used. |
|
5687 |
||
5688 |
Example: |
|
5689 |
||
5690 |
\snippet doc/src/snippets/qstring/main.cpp 76 |
|
5691 |
||
5692 |
\sa number(), toUShort(), toInt() |
|
5693 |
*/ |
|
5694 |
||
5695 |
short QString::toShort(bool *ok, int base) const |
|
5696 |
{ |
|
5697 |
long v = toLongLong(ok, base); |
|
5698 |
if (v < SHRT_MIN || v > SHRT_MAX) { |
|
5699 |
if (ok) |
|
5700 |
*ok = false; |
|
5701 |
v = 0; |
|
5702 |
} |
|
5703 |
return (short)v; |
|
5704 |
} |
|
5705 |
||
5706 |
/*! |
|
5707 |
Returns the string converted to an \c{unsigned short} using base \a |
|
5708 |
base, which is 10 by default and must be between 2 and 36, or 0. |
|
5709 |
Returns 0 if the conversion fails. |
|
5710 |
||
5711 |
If a conversion error occurs, *\a{ok} is set to false; otherwise |
|
5712 |
*\a{ok} is set to true. |
|
5713 |
||
5714 |
If \a base is 0, the C language convention is used: If the string |
|
5715 |
begins with "0x", base 16 is used; if the string begins with "0", |
|
5716 |
base 8 is used; otherwise, base 10 is used. |
|
5717 |
||
5718 |
Example: |
|
5719 |
||
5720 |
\snippet doc/src/snippets/qstring/main.cpp 80 |
|
5721 |
||
5722 |
\sa number(), toShort() |
|
5723 |
*/ |
|
5724 |
||
5725 |
ushort QString::toUShort(bool *ok, int base) const |
|
5726 |
{ |
|
5727 |
ulong v = toULongLong(ok, base); |
|
5728 |
if (v > USHRT_MAX) { |
|
5729 |
if (ok) |
|
5730 |
*ok = false; |
|
5731 |
v = 0; |
|
5732 |
} |
|
5733 |
return (ushort)v; |
|
5734 |
} |
|
5735 |
||
5736 |
||
5737 |
/*! |
|
5738 |
Returns the string converted to a \c double value. |
|
5739 |
||
5740 |
Returns 0.0 if the conversion fails. |
|
5741 |
||
5742 |
If a conversion error occurs, \c{*}\a{ok} is set to false; |
|
5743 |
otherwise \c{*}\a{ok} is set to true. |
|
5744 |
||
5745 |
\snippet doc/src/snippets/qstring/main.cpp 66 |
|
5746 |
||
5747 |
Various string formats for floating point numbers can be converted |
|
5748 |
to double values: |
|
5749 |
||
5750 |
\snippet doc/src/snippets/qstring/main.cpp 67 |
|
5751 |
||
5752 |
This function tries to interpret the string according to the |
|
5753 |
current locale. The current locale is determined from the |
|
5754 |
system at application startup and can be changed by calling |
|
5755 |
QLocale::setDefault(). If the string cannot be interpreted |
|
5756 |
according to the current locale, this function falls back |
|
5757 |
on the "C" locale. |
|
5758 |
||
5759 |
\snippet doc/src/snippets/qstring/main.cpp 69 |
|
5760 |
\snippet doc/src/snippets/qstring/main.cpp 70 |
|
5761 |
||
5762 |
Due to the ambiguity between the decimal point and thousands group |
|
5763 |
separator in various locales, this function does not handle |
|
5764 |
thousands group separators. If you need to convert such numbers, |
|
5765 |
see QLocale::toDouble(). |
|
5766 |
||
5767 |
\snippet doc/src/snippets/qstring/main.cpp 68 |
|
5768 |
||
5769 |
\sa number() QLocale::setDefault() QLocale::toDouble() trimmed() |
|
5770 |
*/ |
|
5771 |
||
5772 |
double QString::toDouble(bool *ok) const |
|
5773 |
{ |
|
5774 |
bool my_ok; |
|
5775 |
QLocale def_locale; |
|
5776 |
double result = def_locale.d()->stringToDouble(*this, &my_ok, QLocalePrivate::FailOnGroupSeparators); |
|
5777 |
if (my_ok) { |
|
5778 |
if (ok != 0) |
|
5779 |
*ok = true; |
|
5780 |
return result; |
|
5781 |
} |
|
5782 |
||
5783 |
QLocale c_locale(QLocale::C); |
|
5784 |
return c_locale.d()->stringToDouble(*this, ok, QLocalePrivate::FailOnGroupSeparators); |
|
5785 |
} |
|
5786 |
||
5787 |
/*! |
|
5788 |
Returns the string converted to a \c float value. |
|
5789 |
||
5790 |
If a conversion error occurs, *\a{ok} is set to false; otherwise |
|
5791 |
*\a{ok} is set to true. Returns 0.0 if the conversion fails. |
|
5792 |
||
5793 |
Example: |
|
5794 |
||
5795 |
\snippet doc/src/snippets/qstring/main.cpp 71 |
|
5796 |
||
5797 |
\sa number(), toDouble(), toInt() |
|
5798 |
*/ |
|
5799 |
||
5800 |
#define QT_MAX_FLOAT 3.4028234663852886e+38 |
|
5801 |
||
5802 |
float QString::toFloat(bool *ok) const |
|
5803 |
{ |
|
5804 |
bool myOk; |
|
5805 |
double d = toDouble(&myOk); |
|
5806 |
if (!myOk || d > QT_MAX_FLOAT || d < -QT_MAX_FLOAT) { |
|
5807 |
if (ok != 0) |
|
5808 |
*ok = false; |
|
5809 |
return 0.0; |
|
5810 |
} |
|
5811 |
if (ok != 0) |
|
5812 |
*ok = true; |
|
5813 |
return (float) d; |
|
5814 |
} |
|
5815 |
||
5816 |
/*! \fn QString &QString::setNum(int n, int base) |
|
5817 |
||
5818 |
Sets the string to the printed value of \a n in the specified \a |
|
5819 |
base, and returns a reference to the string. |
|
5820 |
||
5821 |
The base is 10 by default and must be between 2 and 36. For bases |
|
5822 |
other than 10, \a n is treated as an unsigned integer. |
|
5823 |
||
5824 |
\snippet doc/src/snippets/qstring/main.cpp 56 |
|
5825 |
||
5826 |
The formatting always uses QLocale::C, i.e., English/UnitedStates. |
|
5827 |
To get a localized string representation of a number, use |
|
5828 |
QLocale::toString() with the appropriate locale. |
|
5829 |
*/ |
|
5830 |
||
5831 |
/*! \fn QString &QString::setNum(uint n, int base) |
|
5832 |
||
5833 |
\overload |
|
5834 |
*/ |
|
5835 |
||
5836 |
/*! \fn QString &QString::setNum(long n, int base) |
|
5837 |
||
5838 |
\overload |
|
5839 |
*/ |
|
5840 |
||
5841 |
/*! \fn QString &QString::setNum(ulong n, int base) |
|
5842 |
||
5843 |
\overload |
|
5844 |
*/ |
|
5845 |
||
5846 |
/*! |
|
5847 |
\overload |
|
5848 |
*/ |
|
5849 |
QString &QString::setNum(qlonglong n, int base) |
|
5850 |
{ |
|
5851 |
#if defined(QT_CHECK_RANGE) |
|
5852 |
if (base < 2 || base > 36) { |
|
5853 |
qWarning("QString::setNum: Invalid base (%d)", base); |
|
5854 |
base = 10; |
|
5855 |
} |
|
5856 |
#endif |
|
5857 |
QLocale locale(QLocale::C); |
|
5858 |
*this = locale.d()->longLongToString(n, -1, base); |
|
5859 |
return *this; |
|
5860 |
} |
|
5861 |
||
5862 |
/*! |
|
5863 |
\overload |
|
5864 |
*/ |
|
5865 |
QString &QString::setNum(qulonglong n, int base) |
|
5866 |
{ |
|
5867 |
#if defined(QT_CHECK_RANGE) |
|
5868 |
if (base < 2 || base > 36) { |
|
5869 |
qWarning("QString::setNum: Invalid base (%d)", base); |
|
5870 |
base = 10; |
|
5871 |
} |
|
5872 |
#endif |
|
5873 |
QLocale locale(QLocale::C); |
|
5874 |
*this = locale.d()->unsLongLongToString(n, -1, base); |
|
5875 |
return *this; |
|
5876 |
} |
|
5877 |
||
5878 |
/*! \fn QString &QString::setNum(short n, int base) |
|
5879 |
||
5880 |
\overload |
|
5881 |
*/ |
|
5882 |
||
5883 |
/*! \fn QString &QString::setNum(ushort n, int base) |
|
5884 |
||
5885 |
\overload |
|
5886 |
*/ |
|
5887 |
||
5888 |
/*! |
|
5889 |
\fn QString &QString::setNum(double n, char format, int precision) |
|
5890 |
\overload |
|
5891 |
||
5892 |
Sets the string to the printed value of \a n, formatted according |
|
5893 |
to the given \a format and \a precision, and returns a reference |
|
5894 |
to the string. |
|
5895 |
||
5896 |
The \a format can be 'f', 'F', 'e', 'E', 'g' or 'G' (see the |
|
5897 |
arg() function documentation for an explanation of the formats). |
|
5898 |
||
5899 |
Unlike QLocale::toString(), this function doesn't honor the |
|
5900 |
user's locale settings. |
|
5901 |
*/ |
|
5902 |
||
5903 |
QString &QString::setNum(double n, char f, int prec) |
|
5904 |
{ |
|
5905 |
QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal; |
|
5906 |
uint flags = 0; |
|
5907 |
||
5908 |
if (qIsUpper(f)) |
|
5909 |
flags = QLocalePrivate::CapitalEorX; |
|
5910 |
f = qToLower(f); |
|
5911 |
||
5912 |
switch (f) { |
|
5913 |
case 'f': |
|
5914 |
form = QLocalePrivate::DFDecimal; |
|
5915 |
break; |
|
5916 |
case 'e': |
|
5917 |
form = QLocalePrivate::DFExponent; |
|
5918 |
break; |
|
5919 |
case 'g': |
|
5920 |
form = QLocalePrivate::DFSignificantDigits; |
|
5921 |
break; |
|
5922 |
default: |
|
5923 |
#if defined(QT_CHECK_RANGE) |
|
5924 |
qWarning("QString::setNum: Invalid format char '%c'", f); |
|
5925 |
#endif |
|
5926 |
break; |
|
5927 |
} |
|
5928 |
||
5929 |
QLocale locale(QLocale::C); |
|
5930 |
*this = locale.d()->doubleToString(n, prec, form, -1, flags); |
|
5931 |
return *this; |
|
5932 |
} |
|
5933 |
||
5934 |
/*! |
|
5935 |
\fn QString &QString::setNum(float n, char format, int precision) |
|
5936 |
\overload |
|
5937 |
||
5938 |
Sets the string to the printed value of \a n, formatted according |
|
5939 |
to the given \a format and \a precision, and returns a reference |
|
5940 |
to the string. |
|
5941 |
*/ |
|
5942 |
||
5943 |
||
5944 |
/*! |
|
5945 |
\fn QString QString::number(long n, int base) |
|
5946 |
||
5947 |
Returns a string equivalent of the number \a n according to the |
|
5948 |
specified \a base. |
|
5949 |
||
5950 |
The base is 10 by default and must be between 2 |
|
5951 |
and 36. For bases other than 10, \a n is treated as an |
|
5952 |
unsigned integer. |
|
5953 |
||
5954 |
\snippet doc/src/snippets/qstring/main.cpp 35 |
|
5955 |
||
5956 |
\sa setNum() |
|
5957 |
*/ |
|
5958 |
||
5959 |
QString QString::number(long n, int base) |
|
5960 |
{ |
|
5961 |
QString s; |
|
5962 |
s.setNum(n, base); |
|
5963 |
return s; |
|
5964 |
} |
|
5965 |
||
5966 |
/*! |
|
5967 |
\fn QString QString::number(ulong n, int base) |
|
5968 |
||
5969 |
\overload |
|
5970 |
*/ |
|
5971 |
QString QString::number(ulong n, int base) |
|
5972 |
{ |
|
5973 |
QString s; |
|
5974 |
s.setNum(n, base); |
|
5975 |
return s; |
|
5976 |
} |
|
5977 |
||
5978 |
/*! |
|
5979 |
\overload |
|
5980 |
*/ |
|
5981 |
QString QString::number(int n, int base) |
|
5982 |
{ |
|
5983 |
QString s; |
|
5984 |
s.setNum(n, base); |
|
5985 |
return s; |
|
5986 |
} |
|
5987 |
||
5988 |
/*! |
|
5989 |
\overload |
|
5990 |
*/ |
|
5991 |
QString QString::number(uint n, int base) |
|
5992 |
{ |
|
5993 |
QString s; |
|
5994 |
s.setNum(n, base); |
|
5995 |
return s; |
|
5996 |
} |
|
5997 |
||
5998 |
/*! |
|
5999 |
\overload |
|
6000 |
*/ |
|
6001 |
QString QString::number(qlonglong n, int base) |
|
6002 |
{ |
|
6003 |
QString s; |
|
6004 |
s.setNum(n, base); |
|
6005 |
return s; |
|
6006 |
} |
|
6007 |
||
6008 |
/*! |
|
6009 |
\overload |
|
6010 |
*/ |
|
6011 |
QString QString::number(qulonglong n, int base) |
|
6012 |
{ |
|
6013 |
QString s; |
|
6014 |
s.setNum(n, base); |
|
6015 |
return s; |
|
6016 |
} |
|
6017 |
||
6018 |
||
6019 |
/*! |
|
6020 |
\fn QString QString::number(double n, char format, int precision) |
|
6021 |
||
6022 |
Returns a string equivalent of the number \a n, formatted |
|
6023 |
according to the specified \a format and \a precision. See |
|
6024 |
\l{Argument Formats} for details. |
|
6025 |
||
6026 |
Unlike QLocale::toString(), this function does not honor the |
|
6027 |
user's locale settings. |
|
6028 |
||
6029 |
\sa setNum(), QLocale::toString() |
|
6030 |
*/ |
|
6031 |
QString QString::number(double n, char f, int prec) |
|
6032 |
{ |
|
6033 |
QString s; |
|
6034 |
s.setNum(n, f, prec); |
|
6035 |
return s; |
|
6036 |
} |
|
6037 |
||
6038 |
/*! |
|
6039 |
Splits the string into substrings wherever \a sep occurs, and |
|
6040 |
returns the list of those strings. If \a sep does not match |
|
6041 |
anywhere in the string, split() returns a single-element list |
|
6042 |
containing this string. |
|
6043 |
||
6044 |
\a cs specifies whether \a sep should be matched case |
|
6045 |
sensitively or case insensitively. |
|
6046 |
||
6047 |
If \a behavior is QString::SkipEmptyParts, empty entries don't |
|
6048 |
appear in the result. By default, empty entries are kept. |
|
6049 |
||
6050 |
Example: |
|
6051 |
||
6052 |
\snippet doc/src/snippets/qstring/main.cpp 62 |
|
6053 |
||
6054 |
\sa QStringList::join(), section() |
|
6055 |
*/ |
|
6056 |
QStringList QString::split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const |
|
6057 |
{ |
|
6058 |
QStringList list; |
|
6059 |
int start = 0; |
|
6060 |
int extra = 0; |
|
6061 |
int end; |
|
6062 |
while ((end = indexOf(sep, start + extra, cs)) != -1) { |
|
6063 |
if (start != end || behavior == KeepEmptyParts) |
|
6064 |
list.append(mid(start, end - start)); |
|
6065 |
start = end + sep.size(); |
|
6066 |
extra = (sep.size() == 0 ? 1 : 0); |
|
6067 |
} |
|
6068 |
if (start != size() || behavior == KeepEmptyParts) |
|
6069 |
list.append(mid(start)); |
|
6070 |
return list; |
|
6071 |
} |
|
6072 |
||
6073 |
/*! |
|
6074 |
\overload |
|
6075 |
*/ |
|
6076 |
QStringList QString::split(const QChar &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const |
|
6077 |
{ |
|
6078 |
QStringList list; |
|
6079 |
int start = 0; |
|
6080 |
int end; |
|
6081 |
while ((end = indexOf(sep, start, cs)) != -1) { |
|
6082 |
if (start != end || behavior == KeepEmptyParts) |
|
6083 |
list.append(mid(start, end - start)); |
|
6084 |
start = end + 1; |
|
6085 |
} |
|
6086 |
if (start != size() || behavior == KeepEmptyParts) |
|
6087 |
list.append(mid(start)); |
|
6088 |
return list; |
|
6089 |
} |
|
6090 |
||
6091 |
#ifndef QT_NO_REGEXP |
|
6092 |
/*! |
|
6093 |
\overload |
|
6094 |
||
6095 |
Splits the string into substrings wherever the regular expression |
|
6096 |
\a rx matches, and returns the list of those strings. If \a rx |
|
6097 |
does not match anywhere in the string, split() returns a |
|
6098 |
single-element list containing this string. |
|
6099 |
||
6100 |
Here's an example where we extract the words in a sentence |
|
6101 |
using one or more whitespace characters as the separator: |
|
6102 |
||
6103 |
\snippet doc/src/snippets/qstring/main.cpp 59 |
|
6104 |
||
6105 |
Here's a similar example, but this time we use any sequence of |
|
6106 |
non-word characters as the separator: |
|
6107 |
||
6108 |
\snippet doc/src/snippets/qstring/main.cpp 60 |
|
6109 |
||
6110 |
Here's a third example where we use a zero-length assertion, |
|
6111 |
\bold{\\b} (word boundary), to split the string into an |
|
6112 |
alternating sequence of non-word and word tokens: |
|
6113 |
||
6114 |
\snippet doc/src/snippets/qstring/main.cpp 61 |
|
6115 |
||
6116 |
\sa QStringList::join(), section() |
|
6117 |
*/ |
|
6118 |
QStringList QString::split(const QRegExp &rx, SplitBehavior behavior) const |
|
6119 |
{ |
|
6120 |
QRegExp rx2(rx); |
|
6121 |
QStringList list; |
|
6122 |
int start = 0; |
|
6123 |
int extra = 0; |
|
6124 |
int end; |
|
6125 |
while ((end = rx2.indexIn(*this, start + extra)) != -1) { |
|
6126 |
int matchedLen = rx2.matchedLength(); |
|
6127 |
if (start != end || behavior == KeepEmptyParts) |
|
6128 |
list.append(mid(start, end - start)); |
|
6129 |
start = end + matchedLen; |
|
6130 |
extra = (matchedLen == 0) ? 1 : 0; |
|
6131 |
} |
|
6132 |
if (start != size() || behavior == KeepEmptyParts) |
|
6133 |
list.append(mid(start)); |
|
6134 |
return list; |
|
6135 |
} |
|
6136 |
#endif |
|
6137 |
||
6138 |
/*! |
|
6139 |
\enum QString::NormalizationForm |
|
6140 |
||
6141 |
This enum describes the various normalized forms of Unicode text. |
|
6142 |
||
6143 |
\value NormalizationForm_D Canonical Decomposition |
|
6144 |
\value NormalizationForm_C Canonical Decomposition followed by Canonical Composition |
|
6145 |
\value NormalizationForm_KD Compatibility Decomposition |
|
6146 |
\value NormalizationForm_KC Compatibility Decomposition followed by Canonical Composition |
|
6147 |
||
6148 |
\sa normalized(), |
|
6149 |
{http://www.unicode.org/reports/tr15/}{Unicode Standard Annex #15} |
|
6150 |
*/ |
|
6151 |
||
6152 |
/*! |
|
6153 |
\fn QString QString::normalized(NormalizationForm mode) const |
|
6154 |
Returns the string in the given Unicode normalization \a mode. |
|
6155 |
*/ |
|
6156 |
QString QString::normalized(QString::NormalizationForm mode) const |
|
6157 |
{ |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
6158 |
return normalized(mode, UNICODE_DATA_VERSION); |
0 | 6159 |
} |
6160 |
||
6161 |
/*! |
|
6162 |
\since 4.5 |
|
6163 |
||
6164 |
Returns a copy of this string repeated the specified number of \a times. |
|
6165 |
||
6166 |
If \a times is less than 1, an empty string is returned. |
|
6167 |
||
6168 |
Example: |
|
6169 |
||
6170 |
\code |
|
6171 |
QString str("ab"); |
|
6172 |
str.repeated(4); // returns "abababab" |
|
6173 |
\endcode |
|
6174 |
*/ |
|
6175 |
QString QString::repeated(int times) const |
|
6176 |
{ |
|
6177 |
if (d->size == 0) |
|
6178 |
return *this; |
|
6179 |
||
6180 |
if (times <= 1) { |
|
6181 |
if (times == 1) |
|
6182 |
return *this; |
|
6183 |
return QString(); |
|
6184 |
} |
|
6185 |
||
6186 |
const int resultSize = times * d->size; |
|
6187 |
||
6188 |
QString result; |
|
6189 |
result.reserve(resultSize); |
|
6190 |
if (result.d->alloc != resultSize) |
|
6191 |
return QString(); // not enough memory |
|
6192 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
6193 |
memcpy(result.d->data, d->data, d->size * sizeof(ushort)); |
0 | 6194 |
|
6195 |
int sizeSoFar = d->size; |
|
6196 |
ushort *end = result.d->data + sizeSoFar; |
|
6197 |
||
6198 |
const int halfResultSize = resultSize >> 1; |
|
6199 |
while (sizeSoFar <= halfResultSize) { |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
6200 |
memcpy(end, result.d->data, sizeSoFar * sizeof(ushort)); |
0 | 6201 |
end += sizeSoFar; |
6202 |
sizeSoFar <<= 1; |
|
6203 |
} |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
6204 |
memcpy(end, result.d->data, (resultSize - sizeSoFar) * sizeof(ushort)); |
0 | 6205 |
result.d->data[resultSize] = '\0'; |
6206 |
result.d->size = resultSize; |
|
6207 |
return result; |
|
6208 |
} |
|
6209 |
||
6210 |
void qt_string_normalize(QString *data, QString::NormalizationForm mode, QChar::UnicodeVersion version, int from); |
|
6211 |
/*! |
|
6212 |
\overload |
|
6213 |
\fn QString QString::normalized(NormalizationForm mode, QChar::UnicodeVersion version) const |
|
6214 |
||
6215 |
Returns the string in the given Unicode normalization \a mode, |
|
6216 |
according to the given \a version of the Unicode standard. |
|
6217 |
*/ |
|
6218 |
QString QString::normalized(QString::NormalizationForm mode, QChar::UnicodeVersion version) const |
|
6219 |
{ |
|
6220 |
QString copy = *this; |
|
6221 |
qt_string_normalize(©, mode, version, 0); |
|
6222 |
return copy; |
|
6223 |
} |
|
6224 |
||
6225 |
void qt_string_normalize(QString *data, QString::NormalizationForm mode, QChar::UnicodeVersion version, int from) |
|
6226 |
{ |
|
6227 |
bool simple = true; |
|
6228 |
const QChar *p = data->constData(); |
|
6229 |
int len = data->length(); |
|
6230 |
for (int i = from; i < len; ++i) { |
|
6231 |
if (p[i].unicode() >= 0x80) { |
|
6232 |
simple = false; |
|
6233 |
break; |
|
6234 |
} |
|
6235 |
} |
|
6236 |
if (simple) |
|
6237 |
return; |
|
6238 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
6239 |
if (version == QChar::Unicode_Unassigned) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
6240 |
version = UNICODE_DATA_VERSION; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
6241 |
} else if (version != UNICODE_DATA_VERSION) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
6242 |
QString &s = *data; |
0 | 6243 |
for (int i = 0; i < NumNormalizationCorrections; ++i) { |
6244 |
const NormalizationCorrection &n = uc_normalization_corrections[i]; |
|
6245 |
if (n.version > version) { |
|
6246 |
int pos = from; |
|
6247 |
if (n.ucs4 > 0xffff) { |
|
6248 |
ushort ucs4High = QChar::highSurrogate(n.ucs4); |
|
6249 |
ushort ucs4Low = QChar::lowSurrogate(n.ucs4); |
|
6250 |
ushort oldHigh = QChar::highSurrogate(n.old_mapping); |
|
6251 |
ushort oldLow = QChar::lowSurrogate(n.old_mapping); |
|
6252 |
while (pos < s.length() - 1) { |
|
6253 |
if (s.at(pos).unicode() == ucs4High && s.at(pos + 1).unicode() == ucs4Low) { |
|
6254 |
s[pos] = oldHigh; |
|
6255 |
s[pos + 1] = oldLow; |
|
6256 |
++pos; |
|
6257 |
} |
|
6258 |
++pos; |
|
6259 |
} |
|
6260 |
} else { |
|
6261 |
while (pos < s.length()) { |
|
6262 |
if (s.at(pos).unicode() == n.ucs4) { |
|
6263 |
s[pos] = n.old_mapping; |
|
6264 |
} |
|
6265 |
++pos; |
|
6266 |
} |
|
6267 |
} |
|
6268 |
} |
|
6269 |
} |
|
6270 |
} |
|
6271 |
decomposeHelper(data, mode < QString::NormalizationForm_KD, version, from); |
|
6272 |
||
6273 |
canonicalOrderHelper(data, version, from); |
|
6274 |
||
6275 |
if (mode == QString::NormalizationForm_D || mode == QString::NormalizationForm_KD) |
|
6276 |
return; |
|
6277 |
||
6278 |
composeHelper(data, from); |
|
6279 |
} |
|
6280 |
||
6281 |
||
6282 |
struct ArgEscapeData |
|
6283 |
{ |
|
6284 |
int min_escape; // lowest escape sequence number |
|
6285 |
int occurrences; // number of occurrences of the lowest escape sequence number |
|
6286 |
int locale_occurrences; // number of occurrences of the lowest escape sequence number that |
|
6287 |
// contain 'L' |
|
6288 |
int escape_len; // total length of escape sequences which will be replaced |
|
6289 |
}; |
|
6290 |
||
6291 |
static ArgEscapeData findArgEscapes(const QString &s) |
|
6292 |
{ |
|
6293 |
const QChar *uc_begin = s.unicode(); |
|
6294 |
const QChar *uc_end = uc_begin + s.length(); |
|
6295 |
||
6296 |
ArgEscapeData d; |
|
6297 |
||
6298 |
d.min_escape = INT_MAX; |
|
6299 |
d.occurrences = 0; |
|
6300 |
d.escape_len = 0; |
|
6301 |
d.locale_occurrences = 0; |
|
6302 |
||
6303 |
const QChar *c = uc_begin; |
|
6304 |
while (c != uc_end) { |
|
6305 |
while (c != uc_end && c->unicode() != '%') |
|
6306 |
++c; |
|
6307 |
||
6308 |
if (c == uc_end) |
|
6309 |
break; |
|
6310 |
const QChar *escape_start = c; |
|
6311 |
if (++c == uc_end) |
|
6312 |
break; |
|
6313 |
||
6314 |
bool locale_arg = false; |
|
6315 |
if (c->unicode() == 'L') { |
|
6316 |
locale_arg = true; |
|
6317 |
if (++c == uc_end) |
|
6318 |
break; |
|
6319 |
} |
|
6320 |
||
6321 |
if (c->digitValue() == -1) |
|
6322 |
continue; |
|
6323 |
||
6324 |
int escape = c->digitValue(); |
|
6325 |
++c; |
|
6326 |
||
6327 |
if (c != uc_end && c->digitValue() != -1) { |
|
6328 |
escape = (10 * escape) + c->digitValue(); |
|
6329 |
++c; |
|
6330 |
} |
|
6331 |
||
6332 |
if (escape > d.min_escape) |
|
6333 |
continue; |
|
6334 |
||
6335 |
if (escape < d.min_escape) { |
|
6336 |
d.min_escape = escape; |
|
6337 |
d.occurrences = 0; |
|
6338 |
d.escape_len = 0; |
|
6339 |
d.locale_occurrences = 0; |
|
6340 |
} |
|
6341 |
||
6342 |
++d.occurrences; |
|
6343 |
if (locale_arg) |
|
6344 |
++d.locale_occurrences; |
|
6345 |
d.escape_len += c - escape_start; |
|
6346 |
} |
|
6347 |
return d; |
|
6348 |
} |
|
6349 |
||
6350 |
static QString replaceArgEscapes(const QString &s, const ArgEscapeData &d, int field_width, |
|
6351 |
const QString &arg, const QString &larg, const QChar &fillChar = QLatin1Char(' ')) |
|
6352 |
{ |
|
6353 |
const QChar *uc_begin = s.unicode(); |
|
6354 |
const QChar *uc_end = uc_begin + s.length(); |
|
6355 |
||
6356 |
int abs_field_width = qAbs(field_width); |
|
6357 |
int result_len = s.length() |
|
6358 |
- d.escape_len |
|
6359 |
+ (d.occurrences - d.locale_occurrences) |
|
6360 |
*qMax(abs_field_width, arg.length()) |
|
6361 |
+ d.locale_occurrences |
|
6362 |
*qMax(abs_field_width, larg.length()); |
|
6363 |
||
6364 |
QString result(result_len, Qt::Uninitialized); |
|
6365 |
QChar *result_buff = (QChar*) result.unicode(); |
|
6366 |
||
6367 |
QChar *rc = result_buff; |
|
6368 |
const QChar *c = uc_begin; |
|
6369 |
int repl_cnt = 0; |
|
6370 |
while (c != uc_end) { |
|
6371 |
/* We don't have to check if we run off the end of the string with c, |
|
6372 |
because as long as d.occurrences > 0 we KNOW there are valid escape |
|
6373 |
sequences. */ |
|
6374 |
||
6375 |
const QChar *text_start = c; |
|
6376 |
||
6377 |
while (c->unicode() != '%') |
|
6378 |
++c; |
|
6379 |
||
6380 |
const QChar *escape_start = c++; |
|
6381 |
||
6382 |
bool locale_arg = false; |
|
6383 |
if (c->unicode() == 'L') { |
|
6384 |
locale_arg = true; |
|
6385 |
++c; |
|
6386 |
} |
|
6387 |
||
6388 |
int escape = c->digitValue(); |
|
6389 |
if (escape != -1) { |
|
6390 |
if (c + 1 != uc_end && (c + 1)->digitValue() != -1) { |
|
6391 |
escape = (10 * escape) + (c + 1)->digitValue(); |
|
6392 |
++c; |
|
6393 |
} |
|
6394 |
} |
|
6395 |
||
6396 |
if (escape != d.min_escape) { |
|
6397 |
memcpy(rc, text_start, (c - text_start)*sizeof(QChar)); |
|
6398 |
rc += c - text_start; |
|
6399 |
} |
|
6400 |
else { |
|
6401 |
++c; |
|
6402 |
||
6403 |
memcpy(rc, text_start, (escape_start - text_start)*sizeof(QChar)); |
|
6404 |
rc += escape_start - text_start; |
|
6405 |
||
6406 |
uint pad_chars; |
|
6407 |
if (locale_arg) |
|
6408 |
pad_chars = qMax(abs_field_width, larg.length()) - larg.length(); |
|
6409 |
else |
|
6410 |
pad_chars = qMax(abs_field_width, arg.length()) - arg.length(); |
|
6411 |
||
6412 |
if (field_width > 0) { // left padded |
|
6413 |
for (uint i = 0; i < pad_chars; ++i) |
|
6414 |
(rc++)->unicode() = fillChar.unicode(); |
|
6415 |
} |
|
6416 |
||
6417 |
if (locale_arg) { |
|
6418 |
memcpy(rc, larg.unicode(), larg.length()*sizeof(QChar)); |
|
6419 |
rc += larg.length(); |
|
6420 |
} |
|
6421 |
else { |
|
6422 |
memcpy(rc, arg.unicode(), arg.length()*sizeof(QChar)); |
|
6423 |
rc += arg.length(); |
|
6424 |
} |
|
6425 |
||
6426 |
if (field_width < 0) { // right padded |
|
6427 |
for (uint i = 0; i < pad_chars; ++i) |
|
6428 |
(rc++)->unicode() = fillChar.unicode(); |
|
6429 |
} |
|
6430 |
||
6431 |
if (++repl_cnt == d.occurrences) { |
|
6432 |
memcpy(rc, c, (uc_end - c)*sizeof(QChar)); |
|
6433 |
rc += uc_end - c; |
|
6434 |
Q_ASSERT(rc - result_buff == result_len); |
|
6435 |
c = uc_end; |
|
6436 |
} |
|
6437 |
} |
|
6438 |
} |
|
6439 |
Q_ASSERT(rc == result_buff + result_len); |
|
6440 |
||
6441 |
return result; |
|
6442 |
} |
|
6443 |
||
6444 |
/*! |
|
6445 |
Returns a copy of this string with the lowest numbered place marker |
|
6446 |
replaced by string \a a, i.e., \c %1, \c %2, ..., \c %99. |
|
6447 |
||
6448 |
\a fieldWidth specifies the minimum amount of space that argument \a |
|
6449 |
a shall occupy. If \a a requires less space than \a fieldWidth, it |
|
6450 |
is padded to \a fieldWidth with character \a fillChar. A positive |
|
6451 |
\a fieldWidth produces right-aligned text. A negative \a fieldWidth |
|
6452 |
produces left-aligned text. |
|
6453 |
||
6454 |
This example shows how we might create a \c status string for |
|
6455 |
reporting progress while processing a list of files: |
|
6456 |
||
6457 |
\snippet doc/src/snippets/qstring/main.cpp 11 |
|
6458 |
||
6459 |
First, \c arg(i) replaces \c %1. Then \c arg(total) replaces \c |
|
6460 |
%2. Finally, \c arg(fileName) replaces \c %3. |
|
6461 |
||
6462 |
One advantage of using arg() over sprintf() is that the order of the |
|
6463 |
numbered place markers can change, if the application's strings are |
|
6464 |
translated into other languages, but each arg() will still replace |
|
6465 |
the lowest numbered unreplaced place marker, no matter where it |
|
6466 |
appears. Also, if place marker \c %i appears more than once in the |
|
6467 |
string, the arg() replaces all of them. |
|
6468 |
||
6469 |
If there is no unreplaced place marker remaining, a warning message |
|
6470 |
is output and the result is undefined. Place marker numbers must be |
|
6471 |
in the range 1 to 99. |
|
6472 |
*/ |
|
6473 |
QString QString::arg(const QString &a, int fieldWidth, const QChar &fillChar) const |
|
6474 |
{ |
|
6475 |
ArgEscapeData d = findArgEscapes(*this); |
|
6476 |
||
6477 |
if (d.occurrences == 0) { |
|
6478 |
qWarning("QString::arg: Argument missing: %s, %s", toLocal8Bit().data(), |
|
6479 |
a.toLocal8Bit().data()); |
|
6480 |
return *this; |
|
6481 |
} |
|
6482 |
return replaceArgEscapes(*this, d, fieldWidth, a, a, fillChar); |
|
6483 |
} |
|
6484 |
||
6485 |
/*! |
|
6486 |
\fn QString QString::arg(const QString& a1, const QString& a2) const |
|
6487 |
\overload arg() |
|
6488 |
||
6489 |
This is the same as \c {str.arg(a1).arg(a2)}, except that the |
|
6490 |
strings \a a1 and \a a2 are replaced in one pass. This can make a |
|
6491 |
difference if \a a1 contains e.g. \c{%1}: |
|
6492 |
||
6493 |
\snippet doc/src/snippets/qstring/main.cpp 13 |
|
6494 |
*/ |
|
6495 |
||
6496 |
/*! |
|
6497 |
\fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3) const |
|
6498 |
\overload arg() |
|
6499 |
||
6500 |
This is the same as calling \c str.arg(a1).arg(a2).arg(a3), except |
|
6501 |
that the strings \a a1, \a a2 and \a a3 are replaced in one pass. |
|
6502 |
*/ |
|
6503 |
||
6504 |
/*! |
|
6505 |
\fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4) const |
|
6506 |
\overload arg() |
|
6507 |
||
6508 |
This is the same as calling \c |
|
6509 |
{str.arg(a1).arg(a2).arg(a3).arg(a4)}, except that the strings \a |
|
6510 |
a1, \a a2, \a a3 and \a a4 are replaced in one pass. |
|
6511 |
*/ |
|
6512 |
||
6513 |
/*! |
|
6514 |
\fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4, const QString& a5) const |
|
6515 |
\overload arg() |
|
6516 |
||
6517 |
This is the same as calling \c |
|
6518 |
{str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5)}, except that the strings |
|
6519 |
\a a1, \a a2, \a a3, \a a4, and \a a5 are replaced in one pass. |
|
6520 |
*/ |
|
6521 |
||
6522 |
/*! |
|
6523 |
\fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4, const QString& a5, const QString& a6) const |
|
6524 |
\overload arg() |
|
6525 |
||
6526 |
This is the same as calling \c |
|
6527 |
{str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5).arg(a6))}, except that |
|
6528 |
the strings \a a1, \a a2, \a a3, \a a4, \a a5, and \a a6 are |
|
6529 |
replaced in one pass. |
|
6530 |
*/ |
|
6531 |
||
6532 |
/*! |
|
6533 |
\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 |
|
6534 |
\overload arg() |
|
6535 |
||
6536 |
This is the same as calling \c |
|
6537 |
{str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5).arg(a6).arg(a7)}, |
|
6538 |
except that the strings \a a1, \a a2, \a a3, \a a4, \a a5, \a a6, |
|
6539 |
and \a a7 are replaced in one pass. |
|
6540 |
*/ |
|
6541 |
||
6542 |
/*! |
|
6543 |
\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 |
|
6544 |
\overload arg() |
|
6545 |
||
6546 |
This is the same as calling \c |
|
6547 |
{str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5).arg(a6).arg(a7).arg(a8)}, |
|
6548 |
except that the strings \a a1, \a a2, \a a3, \a a4, \a a5, \a a6, \a |
|
6549 |
a7, and \a a8 are replaced in one pass. |
|
6550 |
*/ |
|
6551 |
||
6552 |
/*! |
|
6553 |
\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 |
|
6554 |
\overload arg() |
|
6555 |
||
6556 |
This is the same as calling \c |
|
6557 |
{str.arg(a1).arg(a2).arg(a3).arg(a4).arg(a5).arg(a6).arg(a7).arg(a8).arg(a9)}, |
|
6558 |
except that the strings \a a1, \a a2, \a a3, \a a4, \a a5, \a a6, \a |
|
6559 |
a7, \a a8, and \a a9 are replaced in one pass. |
|
6560 |
*/ |
|
6561 |
||
6562 |
/*! \fn QString QString::arg(int a, int fieldWidth, int base, const QChar &fillChar) const |
|
6563 |
\overload arg() |
|
6564 |
||
6565 |
The \a a argument is expressed in base \a base, which is 10 by |
|
6566 |
default and must be between 2 and 36. For bases other than 10, \a a |
|
6567 |
is treated as an unsigned integer. |
|
6568 |
||
6569 |
\a fieldWidth specifies the minimum amount of space that \a a is |
|
6570 |
padded to and filled with the character \a fillChar. A positive |
|
6571 |
value produces right-aligned text; a negative value produces |
|
6572 |
left-aligned text. |
|
6573 |
||
6574 |
The '%' can be followed by an 'L', in which case the sequence is |
|
6575 |
replaced with a localized representation of \a a. The conversion |
|
6576 |
uses the default locale, set by QLocale::setDefault(). If no default |
|
6577 |
locale was specified, the "C" locale is used. The 'L' flag is |
|
6578 |
ignored if \a base is not 10. |
|
6579 |
||
6580 |
\snippet doc/src/snippets/qstring/main.cpp 12 |
|
6581 |
\snippet doc/src/snippets/qstring/main.cpp 14 |
|
6582 |
||
6583 |
If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is |
|
6584 |
used. For negative numbers, zero padding might appear before the |
|
6585 |
minus sign. |
|
6586 |
*/ |
|
6587 |
||
6588 |
/*! \fn QString QString::arg(uint a, int fieldWidth, int base, const QChar &fillChar) const |
|
6589 |
\overload arg() |
|
6590 |
||
6591 |
The \a base argument specifies the base to use when converting the |
|
6592 |
integer \a a into a string. The base must be between 2 and 36. |
|
6593 |
||
6594 |
If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is |
|
6595 |
used. For negative numbers, zero padding might appear before the |
|
6596 |
minus sign. |
|
6597 |
*/ |
|
6598 |
||
6599 |
/*! \fn QString QString::arg(long a, int fieldWidth, int base, const QChar &fillChar) const |
|
6600 |
\overload arg() |
|
6601 |
||
6602 |
\a fieldWidth specifies the minimum amount of space that \a a is |
|
6603 |
padded to and filled with the character \a fillChar. A positive |
|
6604 |
value produces right-aligned text; a negative value produces |
|
6605 |
left-aligned text. |
|
6606 |
||
6607 |
The \a a argument is expressed in the given \a base, which is 10 by |
|
6608 |
default and must be between 2 and 36. |
|
6609 |
||
6610 |
The '%' can be followed by an 'L', in which case the sequence is |
|
6611 |
replaced with a localized representation of \a a. The conversion |
|
6612 |
uses the default locale. The default locale is determined from the |
|
6613 |
system's locale settings at application startup. It can be changed |
|
6614 |
using QLocale::setDefault(). The 'L' flag is ignored if \a base is |
|
6615 |
not 10. |
|
6616 |
||
6617 |
\snippet doc/src/snippets/qstring/main.cpp 12 |
|
6618 |
\snippet doc/src/snippets/qstring/main.cpp 14 |
|
6619 |
||
6620 |
If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is |
|
6621 |
used. For negative numbers, zero padding might appear before the |
|
6622 |
minus sign. |
|
6623 |
*/ |
|
6624 |
||
6625 |
/*! \fn QString QString::arg(ulong a, int fieldWidth, int base, const QChar &fillChar) const |
|
6626 |
\overload arg() |
|
6627 |
||
6628 |
\a fieldWidth specifies the minimum amount of space that \a a is |
|
6629 |
padded to and filled with the character \a fillChar. A positive |
|
6630 |
value produces right-aligned text; a negative value produces |
|
6631 |
left-aligned text. |
|
6632 |
||
6633 |
The \a base argument specifies the base to use when converting the |
|
6634 |
integer \a a to a string. The base must be between 2 and 36, with 8 |
|
6635 |
giving octal, 10 decimal, and 16 hexadecimal numbers. |
|
6636 |
||
6637 |
If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is |
|
6638 |
used. For negative numbers, zero padding might appear before the |
|
6639 |
minus sign. |
|
6640 |
*/ |
|
6641 |
||
6642 |
/*! |
|
6643 |
\overload arg() |
|
6644 |
||
6645 |
\a fieldWidth specifies the minimum amount of space that \a a is |
|
6646 |
padded to and filled with the character \a fillChar. A positive |
|
6647 |
value produces right-aligned text; a negative value produces |
|
6648 |
left-aligned text. |
|
6649 |
||
6650 |
The \a base argument specifies the base to use when converting the |
|
6651 |
integer \a a into a string. The base must be between 2 and 36, with |
|
6652 |
8 giving octal, 10 decimal, and 16 hexadecimal numbers. |
|
6653 |
||
6654 |
If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is |
|
6655 |
used. For negative numbers, zero padding might appear before the |
|
6656 |
minus sign. |
|
6657 |
*/ |
|
6658 |
QString QString::arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const |
|
6659 |
{ |
|
6660 |
ArgEscapeData d = findArgEscapes(*this); |
|
6661 |
||
6662 |
if (d.occurrences == 0) { |
|
6663 |
qWarning() << "QString::arg: Argument missing:" << *this << ',' << a; |
|
6664 |
return *this; |
|
6665 |
} |
|
6666 |
||
6667 |
unsigned flags = QLocalePrivate::NoFlags; |
|
6668 |
if (fillChar == QLatin1Char('0')) |
|
6669 |
flags = QLocalePrivate::ZeroPadded; |
|
6670 |
||
6671 |
QString arg; |
|
6672 |
if (d.occurrences > d.locale_occurrences) |
|
6673 |
arg = QLocale::c().d()->longLongToString(a, -1, base, fieldWidth, flags); |
|
6674 |
||
6675 |
QString locale_arg; |
|
6676 |
if (d.locale_occurrences > 0) { |
|
6677 |
QLocale locale; |
|
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
6678 |
if (!locale.numberOptions() & QLocale::OmitGroupSeparator) |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
6679 |
flags |= QLocalePrivate::ThousandsGroup; |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
6680 |
locale_arg = locale.d()->longLongToString(a, -1, base, fieldWidth, flags); |
0 | 6681 |
} |
6682 |
||
6683 |
return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg, fillChar); |
|
6684 |
} |
|
6685 |
||
6686 |
/*! |
|
6687 |
\overload arg() |
|
6688 |
||
6689 |
\a fieldWidth specifies the minimum amount of space that \a a is |
|
6690 |
padded to and filled with the character \a fillChar. A positive |
|
6691 |
value produces right-aligned text; a negative value produces |
|
6692 |
left-aligned text. |
|
6693 |
||
6694 |
The \a base argument specifies the base to use when converting the |
|
6695 |
integer \a a into a string. \a base must be between 2 and 36, with 8 |
|
6696 |
giving octal, 10 decimal, and 16 hexadecimal numbers. |
|
6697 |
||
6698 |
If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is |
|
6699 |
used. For negative numbers, zero padding might appear before the |
|
6700 |
minus sign. |
|
6701 |
*/ |
|
6702 |
QString QString::arg(qulonglong a, int fieldWidth, int base, const QChar &fillChar) const |
|
6703 |
{ |
|
6704 |
ArgEscapeData d = findArgEscapes(*this); |
|
6705 |
||
6706 |
if (d.occurrences == 0) { |
|
6707 |
qWarning() << "QString::arg: Argument missing:" << *this << ',' << a; |
|
6708 |
return *this; |
|
6709 |
} |
|
6710 |
||
6711 |
unsigned flags = QLocalePrivate::NoFlags; |
|
6712 |
if (fillChar == QLatin1Char('0')) |
|
6713 |
flags = QLocalePrivate::ZeroPadded; |
|
6714 |
||
6715 |
QString arg; |
|
6716 |
if (d.occurrences > d.locale_occurrences) |
|
6717 |
arg = QLocale::c().d()->unsLongLongToString(a, -1, base, fieldWidth, flags); |
|
6718 |
||
6719 |
QString locale_arg; |
|
6720 |
if (d.locale_occurrences > 0) { |
|
6721 |
QLocale locale; |
|
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
6722 |
if (!locale.numberOptions() & QLocale::OmitGroupSeparator) |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
6723 |
flags |= QLocalePrivate::ThousandsGroup; |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
6724 |
locale_arg = locale.d()->unsLongLongToString(a, -1, base, fieldWidth, flags); |
0 | 6725 |
} |
6726 |
||
6727 |
return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg, fillChar); |
|
6728 |
} |
|
6729 |
||
6730 |
/*! |
|
6731 |
\overload arg() |
|
6732 |
||
6733 |
\fn QString QString::arg(short a, int fieldWidth, int base, const QChar &fillChar) const |
|
6734 |
||
6735 |
\a fieldWidth specifies the minimum amount of space that \a a is |
|
6736 |
padded to and filled with the character \a fillChar. A positive |
|
6737 |
value produces right-aligned text; a negative value produces |
|
6738 |
left-aligned text. |
|
6739 |
||
6740 |
The \a base argument specifies the base to use when converting the |
|
6741 |
integer \a a into a string. The base must be between 2 and 36, with |
|
6742 |
8 giving octal, 10 decimal, and 16 hexadecimal numbers. |
|
6743 |
||
6744 |
If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is |
|
6745 |
used. For negative numbers, zero padding might appear before the |
|
6746 |
minus sign. |
|
6747 |
*/ |
|
6748 |
||
6749 |
/*! |
|
6750 |
\fn QString QString::arg(ushort a, int fieldWidth, int base, const QChar &fillChar) const |
|
6751 |
\overload arg() |
|
6752 |
||
6753 |
\a fieldWidth specifies the minimum amount of space that \a a is |
|
6754 |
padded to and filled with the character \a fillChar. A positive |
|
6755 |
value produces right-aligned text; a negative value produces |
|
6756 |
left-aligned text. |
|
6757 |
||
6758 |
The \a base argument specifies the base to use when converting the |
|
6759 |
integer \a a into a string. The base must be between 2 and 36, with |
|
6760 |
8 giving octal, 10 decimal, and 16 hexadecimal numbers. |
|
6761 |
||
6762 |
If \a fillChar is '0' (the number 0, ASCII 48), the locale's zero is |
|
6763 |
used. For negative numbers, zero padding might appear before the |
|
6764 |
minus sign. |
|
6765 |
*/ |
|
6766 |
||
6767 |
/*! |
|
6768 |
\overload arg() |
|
6769 |
*/ |
|
6770 |
QString QString::arg(QChar a, int fieldWidth, const QChar &fillChar) const |
|
6771 |
{ |
|
6772 |
QString c; |
|
6773 |
c += a; |
|
6774 |
return arg(c, fieldWidth, fillChar); |
|
6775 |
} |
|
6776 |
||
6777 |
/*! |
|
6778 |
\overload arg() |
|
6779 |
||
6780 |
The \a a argument is interpreted as a Latin-1 character. |
|
6781 |
*/ |
|
6782 |
QString QString::arg(char a, int fieldWidth, const QChar &fillChar) const |
|
6783 |
{ |
|
6784 |
QString c; |
|
6785 |
c += QLatin1Char(a); |
|
6786 |
return arg(c, fieldWidth, fillChar); |
|
6787 |
} |
|
6788 |
||
6789 |
/*! |
|
6790 |
\fn QString QString::arg(double a, int fieldWidth, char format, int precision, const QChar &fillChar) const |
|
6791 |
\overload arg() |
|
6792 |
||
6793 |
Argument \a a is formatted according to the specified \a format and |
|
6794 |
\a precision. See \l{Argument Formats} for details. |
|
6795 |
||
6796 |
\a fieldWidth specifies the minimum amount of space that \a a is |
|
6797 |
padded to and filled with the character \a fillChar. A positive |
|
6798 |
value produces right-aligned text; a negative value produces |
|
6799 |
left-aligned text. |
|
6800 |
||
6801 |
\snippet doc/src/snippets/code/src_corelib_tools_qstring.cpp 2 |
|
6802 |
||
6803 |
The '%' can be followed by an 'L', in which case the sequence is |
|
6804 |
replaced with a localized representation of \a a. The conversion |
|
6805 |
uses the default locale, set by QLocale::setDefaultLocale(). If no |
|
6806 |
default locale was specified, the "C" locale is used. |
|
6807 |
||
6808 |
If \a fillChar is '0' (the number 0, ASCII 48), this function will |
|
6809 |
use the locale's zero to pad. For negative numbers, the zero padding |
|
6810 |
will probably appear before the minus sign. |
|
6811 |
||
6812 |
\sa QLocale::toString() |
|
6813 |
*/ |
|
6814 |
QString QString::arg(double a, int fieldWidth, char fmt, int prec, const QChar &fillChar) const |
|
6815 |
{ |
|
6816 |
ArgEscapeData d = findArgEscapes(*this); |
|
6817 |
||
6818 |
if (d.occurrences == 0) { |
|
6819 |
qWarning("QString::arg: Argument missing: %s, %g", toLocal8Bit().data(), a); |
|
6820 |
return *this; |
|
6821 |
} |
|
6822 |
||
6823 |
unsigned flags = QLocalePrivate::NoFlags; |
|
6824 |
if (fillChar == QLatin1Char('0')) |
|
6825 |
flags = QLocalePrivate::ZeroPadded; |
|
6826 |
||
6827 |
if (qIsUpper(fmt)) |
|
6828 |
flags |= QLocalePrivate::CapitalEorX; |
|
6829 |
fmt = qToLower(fmt); |
|
6830 |
||
6831 |
QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal; |
|
6832 |
switch (fmt) { |
|
6833 |
case 'f': |
|
6834 |
form = QLocalePrivate::DFDecimal; |
|
6835 |
break; |
|
6836 |
case 'e': |
|
6837 |
form = QLocalePrivate::DFExponent; |
|
6838 |
break; |
|
6839 |
case 'g': |
|
6840 |
form = QLocalePrivate::DFSignificantDigits; |
|
6841 |
break; |
|
6842 |
default: |
|
6843 |
#if defined(QT_CHECK_RANGE) |
|
6844 |
qWarning("QString::arg: Invalid format char '%c'", fmt); |
|
6845 |
#endif |
|
6846 |
break; |
|
6847 |
} |
|
6848 |
||
6849 |
QString arg; |
|
6850 |
if (d.occurrences > d.locale_occurrences) |
|
6851 |
arg = QLocale::c().d()->doubleToString(a, prec, form, fieldWidth, flags); |
|
6852 |
||
6853 |
QString locale_arg; |
|
6854 |
if (d.locale_occurrences > 0) { |
|
6855 |
QLocale locale; |
|
6856 |
||
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
6857 |
if (!locale.numberOptions() & QLocale::OmitGroupSeparator) |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
6858 |
flags |= QLocalePrivate::ThousandsGroup; |
0 | 6859 |
locale_arg = locale.d()->doubleToString(a, prec, form, fieldWidth, flags); |
6860 |
} |
|
6861 |
||
6862 |
return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg, fillChar); |
|
6863 |
} |
|
6864 |
||
6865 |
static int getEscape(const QChar *uc, int *pos, int len, int maxNumber = 999) |
|
6866 |
{ |
|
6867 |
int i = *pos; |
|
6868 |
++i; |
|
6869 |
if (i < len && uc[i] == QLatin1Char('L')) |
|
6870 |
++i; |
|
6871 |
if (i < len) { |
|
6872 |
int escape = uc[i].unicode() - '0'; |
|
6873 |
if (uint(escape) >= 10U) |
|
6874 |
return -1; |
|
6875 |
++i; |
|
6876 |
while (i < len) { |
|
6877 |
int digit = uc[i].unicode() - '0'; |
|
6878 |
if (uint(digit) >= 10U) |
|
6879 |
break; |
|
6880 |
escape = (escape * 10) + digit; |
|
6881 |
++i; |
|
6882 |
} |
|
6883 |
if (escape <= maxNumber) { |
|
6884 |
*pos = i; |
|
6885 |
return escape; |
|
6886 |
} |
|
6887 |
} |
|
6888 |
return -1; |
|
6889 |
} |
|
6890 |
||
6891 |
QString QString::multiArg(int numArgs, const QString **args) const |
|
6892 |
{ |
|
6893 |
QString result; |
|
6894 |
QMap<int, int> numbersUsed; |
|
6895 |
const QChar *uc = (const QChar *) d->data; |
|
6896 |
const int len = d->size; |
|
6897 |
const int end = len - 1; |
|
6898 |
int lastNumber = -1; |
|
6899 |
int i = 0; |
|
6900 |
||
6901 |
// populate the numbersUsed map with the %n's that actually occur in the string |
|
6902 |
while (i < end) { |
|
6903 |
if (uc[i] == QLatin1Char('%')) { |
|
6904 |
int number = getEscape(uc, &i, len); |
|
6905 |
if (number != -1) { |
|
6906 |
numbersUsed.insert(number, -1); |
|
6907 |
continue; |
|
6908 |
} |
|
6909 |
} |
|
6910 |
++i; |
|
6911 |
} |
|
6912 |
||
6913 |
// assign an argument number to each of the %n's |
|
6914 |
QMap<int, int>::iterator j = numbersUsed.begin(); |
|
6915 |
QMap<int, int>::iterator jend = numbersUsed.end(); |
|
6916 |
int arg = 0; |
|
6917 |
while (j != jend && arg < numArgs) { |
|
6918 |
*j = arg++; |
|
6919 |
lastNumber = j.key(); |
|
6920 |
++j; |
|
6921 |
} |
|
6922 |
||
6923 |
// sanity |
|
6924 |
if (numArgs > arg) { |
|
6925 |
qWarning("QString::arg: %d argument(s) missing in %s", numArgs - arg, toLocal8Bit().data()); |
|
6926 |
numArgs = arg; |
|
6927 |
} |
|
6928 |
||
6929 |
i = 0; |
|
6930 |
while (i < len) { |
|
6931 |
if (uc[i] == QLatin1Char('%') && i != end) { |
|
6932 |
int number = getEscape(uc, &i, len, lastNumber); |
|
6933 |
int arg = numbersUsed[number]; |
|
6934 |
if (number != -1 && arg != -1) { |
|
6935 |
result += *args[arg]; |
|
6936 |
continue; |
|
6937 |
} |
|
6938 |
} |
|
6939 |
result += uc[i++]; |
|
6940 |
} |
|
6941 |
return result; |
|
6942 |
} |
|
6943 |
||
6944 |
/*! \internal |
|
6945 |
*/ |
|
6946 |
void QString::updateProperties() const |
|
6947 |
{ |
|
6948 |
ushort *p = d->data; |
|
6949 |
ushort *end = p + d->size; |
|
6950 |
d->simpletext = true; |
|
6951 |
while (p < end) { |
|
6952 |
ushort uc = *p; |
|
6953 |
// sort out regions of complex text formatting |
|
6954 |
if (uc > 0x058f && (uc < 0x1100 || uc > 0xfb0f)) { |
|
6955 |
d->simpletext = false; |
|
6956 |
} |
|
6957 |
p++; |
|
6958 |
} |
|
6959 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
6960 |
d->righttoleft = isRightToLeft(); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
6961 |
d->clean = true; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
6962 |
} |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
6963 |
|
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
6964 |
bool QString::isRightToLeft() const |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
6965 |
{ |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
6966 |
ushort *p = d->data; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
6967 |
const ushort * const end = p + d->size; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
6968 |
bool righttoleft = false; |
0 | 6969 |
while (p < end) { |
6970 |
switch(QChar::direction(*p)) |
|
6971 |
{ |
|
6972 |
case QChar::DirL: |
|
6973 |
goto end; |
|
6974 |
case QChar::DirR: |
|
6975 |
case QChar::DirAL: |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
6976 |
righttoleft = true; |
0 | 6977 |
goto end; |
6978 |
default: |
|
6979 |
break; |
|
6980 |
} |
|
6981 |
++p; |
|
6982 |
} |
|
6983 |
end: |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
6984 |
return righttoleft; |
0 | 6985 |
} |
6986 |
||
6987 |
/*! \fn bool QString::isSimpleText() const |
|
6988 |
||
6989 |
\internal |
|
6990 |
*/ |
|
6991 |
||
6992 |
/*! \fn bool QString::isRightToLeft() const |
|
6993 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
6994 |
Returns true if the string is read right to left. |
0 | 6995 |
*/ |
6996 |
||
6997 |
||
6998 |
/*! \fn QChar *QString::data() |
|
6999 |
||
7000 |
Returns a pointer to the data stored in the QString. The pointer |
|
7001 |
can be used to access and modify the characters that compose the |
|
7002 |
string. For convenience, the data is '\\0'-terminated. |
|
7003 |
||
7004 |
Example: |
|
7005 |
||
7006 |
\snippet doc/src/snippets/qstring/main.cpp 19 |
|
7007 |
||
7008 |
Note that the pointer remains valid only as long as the string is |
|
7009 |
not modified by other means. For read-only access, constData() is |
|
7010 |
faster because it never causes a \l{deep copy} to occur. |
|
7011 |
||
7012 |
\sa constData(), operator[]() |
|
7013 |
*/ |
|
7014 |
||
7015 |
/*! \fn const QChar *QString::data() const |
|
7016 |
||
7017 |
\overload |
|
7018 |
*/ |
|
7019 |
||
7020 |
/*! \fn const QChar *QString::constData() const |
|
7021 |
||
7022 |
Returns a pointer to the data stored in the QString. The pointer |
|
7023 |
can be used to access the characters that compose the string. For |
|
7024 |
convenience, the data is '\\0'-terminated. |
|
7025 |
||
7026 |
Note that the pointer remains valid only as long as the string is |
|
7027 |
not modified. |
|
7028 |
||
7029 |
\sa data(), operator[]() |
|
7030 |
*/ |
|
7031 |
||
7032 |
/*! \fn void QString::push_front(const QString &other) |
|
7033 |
||
7034 |
This function is provided for STL compatibility, prepending the |
|
7035 |
given \a other string to the beginning of this string. It is |
|
7036 |
equivalent to \c prepend(other). |
|
7037 |
||
7038 |
\sa prepend() |
|
7039 |
*/ |
|
7040 |
||
7041 |
/*! \fn void QString::push_front(QChar ch) |
|
7042 |
||
7043 |
\overload |
|
7044 |
||
7045 |
Prepends the given \a ch character to the beginning of this string. |
|
7046 |
*/ |
|
7047 |
||
7048 |
/*! \fn void QString::push_back(const QString &other) |
|
7049 |
||
7050 |
This function is provided for STL compatibility, appending the |
|
7051 |
given \a other string onto the end of this string. It is |
|
7052 |
equivalent to \c append(other). |
|
7053 |
||
7054 |
\sa append() |
|
7055 |
*/ |
|
7056 |
||
7057 |
/*! \fn void QString::push_back(QChar ch) |
|
7058 |
||
7059 |
\overload |
|
7060 |
||
7061 |
Appends the given \a ch character onto the end of this string. |
|
7062 |
*/ |
|
7063 |
||
7064 |
/*! |
|
7065 |
\fn std::string QString::toStdString() const |
|
7066 |
||
7067 |
Returns a std::string object with the data contained in this |
|
7068 |
QString. The Unicode data is converted into 8-bit characters using |
|
7069 |
the toAscii() function. |
|
7070 |
||
7071 |
This operator is mostly useful to pass a QString to a function |
|
7072 |
that accepts a std::string object. |
|
7073 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7074 |
If the QString contains Unicode characters that the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7075 |
QTextCodec::codecForCStrings() codec cannot handle, using this operator |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7076 |
can lead to loss of information. |
0 | 7077 |
|
7078 |
This operator is only available if Qt is configured with STL |
|
7079 |
compatibility enabled. |
|
7080 |
||
7081 |
\sa toAscii(), toLatin1(), toUtf8(), toLocal8Bit() |
|
7082 |
*/ |
|
7083 |
||
7084 |
/*! |
|
7085 |
Constructs a QString that uses the first \a size Unicode characters |
|
7086 |
in the array \a unicode. The data in \a unicode is \e not |
|
7087 |
copied. The caller must be able to guarantee that \a unicode will |
|
7088 |
not be deleted or modified as long as the QString (or an |
|
7089 |
unmodified copy of it) exists. |
|
7090 |
||
7091 |
Any attempts to modify the QString or copies of it will cause it |
|
7092 |
to create a deep copy of the data, ensuring that the raw data |
|
7093 |
isn't modified. |
|
7094 |
||
7095 |
Here's an example of how we can use a QRegExp on raw data in |
|
7096 |
memory without requiring to copy the data into a QString: |
|
7097 |
||
7098 |
\snippet doc/src/snippets/qstring/main.cpp 22 |
|
7099 |
\snippet doc/src/snippets/qstring/main.cpp 23 |
|
7100 |
||
7101 |
\warning A string created with fromRawData() is \e not |
|
7102 |
'\\0'-terminated, unless the raw data contains a '\\0' character |
|
7103 |
at position \a size. This means unicode() will \e not return a |
|
7104 |
'\\0'-terminated string (although utf16() does, at the cost of |
|
7105 |
copying the raw data). |
|
7106 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7107 |
\sa fromUtf16(), setRawData() |
0 | 7108 |
*/ |
7109 |
QString QString::fromRawData(const QChar *unicode, int size) |
|
7110 |
{ |
|
7111 |
Data *x = static_cast<Data *>(qMalloc(sizeof(Data))); |
|
7112 |
Q_CHECK_PTR(x); |
|
7113 |
if (unicode) { |
|
7114 |
x->data = (ushort *)unicode; |
|
7115 |
} else { |
|
7116 |
x->data = x->array; |
|
7117 |
size = 0; |
|
7118 |
} |
|
7119 |
x->ref = 1; |
|
7120 |
x->alloc = x->size = size; |
|
7121 |
*x->array = '\0'; |
|
7122 |
x->clean = x->asciiCache = x->simpletext = x->righttoleft = x->capacity = 0; |
|
7123 |
return QString(x, 0); |
|
7124 |
} |
|
7125 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7126 |
/*! |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7127 |
\since 4.7 |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7128 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7129 |
Resets the QString to use the first \a size Unicode characters |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7130 |
in the array \a unicode. The data in \a unicode is \e not |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7131 |
copied. The caller must be able to guarantee that \a unicode will |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7132 |
not be deleted or modified as long as the QString (or an |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7133 |
unmodified copy of it) exists. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7134 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7135 |
This function can be used instead of fromRawData() to re-use |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7136 |
existings QString objects to save memory re-allocations. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7137 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7138 |
\sa fromRawData() |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7139 |
*/ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7140 |
QString &QString::setRawData(const QChar *unicode, int size) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7141 |
{ |
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
7142 |
if (d->ref != 1 || (d->data == d->array && d->alloc)) { |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7143 |
*this = fromRawData(unicode, size); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7144 |
} else { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7145 |
#ifdef QT3_SUPPORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7146 |
if (d->asciiCache) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7147 |
Q_ASSERT(asciiCache); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7148 |
asciiCache->remove(d); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7149 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7150 |
#endif |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7151 |
if (unicode) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7152 |
d->data = (ushort *)unicode; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7153 |
} else { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7154 |
d->data = d->array; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7155 |
size = 0; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7156 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7157 |
d->alloc = d->size = size; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7158 |
*d->array = '\0'; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7159 |
d->clean = d->asciiCache = d->simpletext = d->righttoleft = d->capacity = 0; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7160 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7161 |
return *this; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7162 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7163 |
|
0 | 7164 |
/*! \class QLatin1String |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7165 |
\brief The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal. |
0 | 7166 |
|
7167 |
\ingroup string-processing |
|
7168 |
\reentrant |
|
7169 |
||
7170 |
Many of QString's member functions are overloaded to accept |
|
7171 |
\c{const char *} instead of QString. This includes the copy |
|
7172 |
constructor, the assignment operator, the comparison operators, |
|
7173 |
and various other functions such as \link QString::insert() |
|
7174 |
insert() \endlink, \link QString::replace() replace()\endlink, |
|
7175 |
and \link QString::indexOf() indexOf()\endlink. These functions |
|
7176 |
are usually optimized to avoid constructing a QString object for |
|
7177 |
the \c{const char *} data. For example, assuming \c str is a |
|
7178 |
QString, |
|
7179 |
||
7180 |
\snippet doc/src/snippets/code/src_corelib_tools_qstring.cpp 3 |
|
7181 |
||
7182 |
is much faster than |
|
7183 |
||
7184 |
\snippet doc/src/snippets/code/src_corelib_tools_qstring.cpp 4 |
|
7185 |
||
7186 |
because it doesn't construct four temporary QString objects and |
|
7187 |
make a deep copy of the character data. |
|
7188 |
||
7189 |
Applications that define \c QT_NO_CAST_FROM_ASCII (as explained |
|
7190 |
in the QString documentation) don't have access to QString's |
|
7191 |
\c{const char *} API. To provide an efficient way of specifying |
|
7192 |
constant Latin-1 strings, Qt provides the QLatin1String, which is |
|
7193 |
just a very thin wrapper around a \c{const char *}. Using |
|
7194 |
QLatin1String, the example code above becomes |
|
7195 |
||
7196 |
\snippet doc/src/snippets/code/src_corelib_tools_qstring.cpp 5 |
|
7197 |
||
7198 |
This is a bit longer to type, but it provides exactly the same |
|
7199 |
benefits as the first version of the code, and is faster than |
|
7200 |
converting the Latin-1 strings using QString::fromLatin1(). |
|
7201 |
||
7202 |
Thanks to the QString(const QLatin1String &) constructor, |
|
7203 |
QLatin1String can be used everywhere a QString is expected. For |
|
7204 |
example: |
|
7205 |
||
7206 |
\snippet doc/src/snippets/code/src_corelib_tools_qstring.cpp 6 |
|
7207 |
||
7208 |
\sa QString, QLatin1Char |
|
7209 |
*/ |
|
7210 |
||
7211 |
/*! \fn QLatin1String::QLatin1String(const char *str) |
|
7212 |
||
7213 |
Constructs a QLatin1String object that stores \a str. Note that if |
|
7214 |
\a str is 0, an empty string is created; this case is handled by |
|
7215 |
QString. |
|
7216 |
||
7217 |
The string data is \e not copied. The caller must be able to |
|
7218 |
guarantee that \a str will not be deleted or modified as long as |
|
7219 |
the QLatin1String object exists. |
|
7220 |
||
7221 |
\sa latin1() |
|
7222 |
*/ |
|
7223 |
||
7224 |
/*! |
|
7225 |
\since 4.1 |
|
7226 |
\fn QLatin1String &QLatin1String::operator=(const QLatin1String &other) |
|
7227 |
||
7228 |
Constructs a copy of \a other. |
|
7229 |
*/ |
|
7230 |
||
7231 |
/*! \fn const char *QLatin1String::latin1() const |
|
7232 |
||
7233 |
Returns the Latin-1 string stored in this object. |
|
7234 |
*/ |
|
7235 |
||
7236 |
/*! \fn bool QLatin1String::operator==(const QString &other) const |
|
7237 |
||
7238 |
Returns true if this string is equal to string \a other; |
|
7239 |
otherwise returns false. |
|
7240 |
||
7241 |
The comparison is based exclusively on the numeric Unicode values |
|
7242 |
of the characters and is very fast, but is not what a human would |
|
7243 |
expect. Consider sorting user-interface strings with |
|
7244 |
QString::localeAwareCompare(). |
|
7245 |
*/ |
|
7246 |
||
7247 |
/*! |
|
7248 |
\fn bool QLatin1String::operator==(const char *other) const |
|
7249 |
\since 4.3 |
|
7250 |
\overload |
|
7251 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7252 |
The \a other const char pointer is converted to a QString using |
0 | 7253 |
the QString::fromAscii() function. |
7254 |
||
7255 |
You can disable this operator by defining \c |
|
7256 |
QT_NO_CAST_FROM_ASCII when you compile your applications. This |
|
7257 |
can be useful if you want to ensure that all user-visible strings |
|
7258 |
go through QObject::tr(), for example. |
|
7259 |
*/ |
|
7260 |
||
7261 |
/*! \fn bool QLatin1String::operator!=(const QString &other) const |
|
7262 |
||
7263 |
Returns true if this string is not equal to string \a other; |
|
7264 |
otherwise returns false. |
|
7265 |
||
7266 |
The comparison is based exclusively on the numeric Unicode values |
|
7267 |
of the characters and is very fast, but is not what a human would |
|
7268 |
expect. Consider sorting user-interface strings with |
|
7269 |
QString::localeAwareCompare(). |
|
7270 |
*/ |
|
7271 |
||
7272 |
/*! |
|
7273 |
\fn bool QLatin1String::operator!=(const char *other) const |
|
7274 |
\since 4.3 |
|
7275 |
\overload operator!=() |
|
7276 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7277 |
The \a other const char pointer is converted to a QString using |
0 | 7278 |
the QString::fromAscii() function. |
7279 |
||
7280 |
You can disable this operator by defining \c |
|
7281 |
QT_NO_CAST_FROM_ASCII when you compile your applications. This |
|
7282 |
can be useful if you want to ensure that all user-visible strings |
|
7283 |
go through QObject::tr(), for example. |
|
7284 |
*/ |
|
7285 |
||
7286 |
/*! |
|
7287 |
\fn bool QLatin1String::operator>(const QString &other) const |
|
7288 |
||
7289 |
Returns true if this string is lexically greater than string \a |
|
7290 |
other; otherwise returns false. |
|
7291 |
||
7292 |
The comparison is based exclusively on the numeric Unicode values |
|
7293 |
of the characters and is very fast, but is not what a human would |
|
7294 |
expect. Consider sorting user-interface strings with |
|
7295 |
QString::localeAwareCompare(). |
|
7296 |
*/ |
|
7297 |
||
7298 |
/*! |
|
7299 |
\fn bool QLatin1String::operator>(const char *other) const |
|
7300 |
\since 4.3 |
|
7301 |
\overload |
|
7302 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7303 |
The \a other const char pointer is converted to a QString using |
0 | 7304 |
the QString::fromAscii() function. |
7305 |
||
7306 |
You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII |
|
7307 |
when you compile your applications. This can be useful if you want |
|
7308 |
to ensure that all user-visible strings go through QObject::tr(), |
|
7309 |
for example. |
|
7310 |
*/ |
|
7311 |
||
7312 |
/*! |
|
7313 |
\fn bool QLatin1String::operator<(const QString &other) const |
|
7314 |
||
7315 |
Returns true if this string is lexically less than the \a other |
|
7316 |
string; otherwise returns false. |
|
7317 |
||
7318 |
The comparison is based exclusively on the numeric Unicode values |
|
7319 |
of the characters and is very fast, but is not what a human would |
|
7320 |
expect. Consider sorting user-interface strings using the |
|
7321 |
QString::localeAwareCompare() function. |
|
7322 |
*/ |
|
7323 |
||
7324 |
/*! |
|
7325 |
\fn bool QLatin1String::operator<(const char *other) const |
|
7326 |
\since 4.3 |
|
7327 |
\overload |
|
7328 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7329 |
The \a other const char pointer is converted to a QString using |
0 | 7330 |
the QString::fromAscii() function. |
7331 |
||
7332 |
You can disable this operator by defining \c |
|
7333 |
QT_NO_CAST_FROM_ASCII when you compile your applications. This |
|
7334 |
can be useful if you want to ensure that all user-visible strings |
|
7335 |
go through QObject::tr(), for example. |
|
7336 |
*/ |
|
7337 |
||
7338 |
/*! |
|
7339 |
\fn bool QLatin1String::operator>=(const QString &other) const |
|
7340 |
||
7341 |
Returns true if this string is lexically greater than or equal |
|
7342 |
to string \a other; otherwise returns false. |
|
7343 |
||
7344 |
The comparison is based exclusively on the numeric Unicode values |
|
7345 |
of the characters and is very fast, but is not what a human would |
|
7346 |
expect. Consider sorting user-interface strings with |
|
7347 |
QString::localeAwareCompare(). |
|
7348 |
*/ |
|
7349 |
||
7350 |
/*! |
|
7351 |
\fn bool QLatin1String::operator>=(const char *other) const |
|
7352 |
\since 4.3 |
|
7353 |
\overload |
|
7354 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7355 |
The \a other const char pointer is converted to a QString using |
0 | 7356 |
the QString::fromAscii() function. |
7357 |
||
7358 |
You can disable this operator by defining \c |
|
7359 |
QT_NO_CAST_FROM_ASCII when you compile your applications. This |
|
7360 |
can be useful if you want to ensure that all user-visible strings |
|
7361 |
go through QObject::tr(), for example. |
|
7362 |
*/ |
|
7363 |
||
7364 |
/*! \fn bool QLatin1String::operator<=(const QString &other) const |
|
7365 |
||
7366 |
Returns true if this string is lexically less than or equal |
|
7367 |
to string \a other; otherwise returns false. |
|
7368 |
||
7369 |
The comparison is based exclusively on the numeric Unicode values |
|
7370 |
of the characters and is very fast, but is not what a human would |
|
7371 |
expect. Consider sorting user-interface strings with |
|
7372 |
QString::localeAwareCompare(). |
|
7373 |
*/ |
|
7374 |
||
7375 |
/*! |
|
7376 |
\fn bool QLatin1String::operator<=(const char *other) const |
|
7377 |
\since 4.3 |
|
7378 |
\overload |
|
7379 |
||
7380 |
The \a other const char pointer is converted to a QString using |
|
7381 |
the QString::fromAscii() function. |
|
7382 |
||
7383 |
You can disable this operator by defining \c |
|
7384 |
QT_NO_CAST_FROM_ASCII when you compile your applications. This |
|
7385 |
can be useful if you want to ensure that all user-visible strings |
|
7386 |
go through QObject::tr(), for example. |
|
7387 |
*/ |
|
7388 |
||
7389 |
||
7390 |
||
7391 |
/* \fn bool operator==(const QLatin1String &s1, const QLatin1String &s2) |
|
7392 |
\relates QLatin1String |
|
7393 |
||
7394 |
Returns true if string \a s1 is lexically equal to string \a s2; otherwise |
|
7395 |
returns false. |
|
7396 |
*/ |
|
7397 |
/* \fn bool operator!=(const QLatin1String &s1, const QLatin1String &s2) |
|
7398 |
\relates QLatin1String |
|
7399 |
||
7400 |
Returns true if string \a s1 is lexically unequal to string \a s2; otherwise |
|
7401 |
returns false. |
|
7402 |
*/ |
|
7403 |
/* \fn bool operator<(const QLatin1String &s1, const QLatin1String &s2) |
|
7404 |
\relates QLatin1String |
|
7405 |
||
7406 |
Returns true if string \a s1 is lexically smaller than string \a s2; otherwise |
|
7407 |
returns false. |
|
7408 |
*/ |
|
7409 |
/* \fn bool operator<=(const QLatin1String &s1, const QLatin1String &s2) |
|
7410 |
\relates QLatin1String |
|
7411 |
||
7412 |
Returns true if string \a s1 is lexically smaller than or equal to string \a s2; otherwise |
|
7413 |
returns false. |
|
7414 |
*/ |
|
7415 |
/* \fn bool operator>(const QLatin1String &s1, const QLatin1String &s2) |
|
7416 |
\relates QLatin1String |
|
7417 |
||
7418 |
Returns true if string \a s1 is lexically greater than string \a s2; otherwise |
|
7419 |
returns false. |
|
7420 |
*/ |
|
7421 |
/* \fn bool operator>=(const QLatin1String &s1, const QLatin1String &s2) |
|
7422 |
\relates QLatin1String |
|
7423 |
||
7424 |
Returns true if string \a s1 is lexically greater than or equal to |
|
7425 |
string \a s2; otherwise returns false. |
|
7426 |
*/ |
|
7427 |
||
7428 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
7429 |
#if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE)) |
0 | 7430 |
/*! |
7431 |
\fn QDataStream &operator<<(QDataStream &stream, const QString &string) |
|
7432 |
\relates QString |
|
7433 |
||
7434 |
Writes the given \a string to the specified \a stream. |
|
7435 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7436 |
\sa {Serializing Qt Data Types} |
0 | 7437 |
*/ |
7438 |
||
7439 |
QDataStream &operator<<(QDataStream &out, const QString &str) |
|
7440 |
{ |
|
7441 |
if (out.version() == 1) { |
|
7442 |
out << str.toLatin1(); |
|
7443 |
} else { |
|
7444 |
if (!str.isNull() || out.version() < 3) { |
|
7445 |
int byteOrder = out.byteOrder(); |
|
7446 |
const QChar* ub = str.unicode(); |
|
7447 |
static const uint auto_size = 1024; |
|
7448 |
char t[auto_size]; |
|
7449 |
char *b; |
|
7450 |
if (str.length()*sizeof(QChar) > auto_size) { |
|
7451 |
b = new char[str.length()*sizeof(QChar)]; |
|
7452 |
} else { |
|
7453 |
b = t; |
|
7454 |
} |
|
7455 |
int l = str.length(); |
|
7456 |
char *c=b; |
|
7457 |
while (l--) { |
|
7458 |
if (byteOrder == QDataStream::BigEndian) { |
|
7459 |
*c++ = (char)ub->row(); |
|
7460 |
*c++ = (char)ub->cell(); |
|
7461 |
} else { |
|
7462 |
*c++ = (char)ub->cell(); |
|
7463 |
*c++ = (char)ub->row(); |
|
7464 |
} |
|
7465 |
ub++; |
|
7466 |
} |
|
7467 |
out.writeBytes(b, sizeof(QChar)*str.length()); |
|
7468 |
if (str.length()*sizeof(QChar) > auto_size) |
|
7469 |
delete [] b; |
|
7470 |
} else { |
|
7471 |
// write null marker |
|
7472 |
out << (quint32)0xffffffff; |
|
7473 |
} |
|
7474 |
} |
|
7475 |
return out; |
|
7476 |
} |
|
7477 |
||
7478 |
/*! |
|
7479 |
\fn QDataStream &operator>>(QDataStream &stream, QString &string) |
|
7480 |
\relates QString |
|
7481 |
||
7482 |
Reads a string from the specified \a stream into the given \a string. |
|
7483 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7484 |
\sa {Serializing Qt Data Types} |
0 | 7485 |
*/ |
7486 |
||
7487 |
QDataStream &operator>>(QDataStream &in, QString &str) |
|
7488 |
{ |
|
7489 |
#ifdef QT_QSTRING_UCS_4 |
|
7490 |
#if defined(Q_CC_GNU) |
|
7491 |
#warning "operator>> not working properly" |
|
7492 |
#endif |
|
7493 |
#endif |
|
7494 |
||
7495 |
if (in.version() == 1) { |
|
7496 |
QByteArray l; |
|
7497 |
in >> l; |
|
7498 |
str = QString::fromLatin1(l); |
|
7499 |
} else { |
|
7500 |
quint32 bytes = 0; |
|
7501 |
in >> bytes; // read size of string |
|
7502 |
if (bytes == 0xffffffff) { // null string |
|
7503 |
str.clear(); |
|
7504 |
} else if (bytes > 0) { // not empty |
|
7505 |
if (bytes & 0x1) { |
|
7506 |
str.clear(); |
|
7507 |
in.setStatus(QDataStream::ReadCorruptData); |
|
7508 |
return in; |
|
7509 |
} |
|
7510 |
||
7511 |
const quint32 Step = 1024 * 1024; |
|
7512 |
quint32 len = bytes / 2; |
|
7513 |
quint32 allocated = 0; |
|
7514 |
||
7515 |
while (allocated < len) { |
|
7516 |
int blockSize = qMin(Step, len - allocated); |
|
7517 |
str.resize(allocated + blockSize); |
|
7518 |
if (in.readRawData(reinterpret_cast<char *>(str.data()) + allocated * 2, |
|
7519 |
blockSize * 2) != blockSize * 2) { |
|
7520 |
str.clear(); |
|
7521 |
in.setStatus(QDataStream::ReadPastEnd); |
|
7522 |
return in; |
|
7523 |
} |
|
7524 |
allocated += blockSize; |
|
7525 |
} |
|
7526 |
||
7527 |
if ((in.byteOrder() == QDataStream::BigEndian) |
|
7528 |
!= (QSysInfo::ByteOrder == QSysInfo::BigEndian)) { |
|
7529 |
ushort *data = reinterpret_cast<ushort *>(str.data()); |
|
7530 |
while (len--) { |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
7531 |
*data = qbswap(*data); |
0 | 7532 |
++data; |
7533 |
} |
|
7534 |
} |
|
7535 |
} else { |
|
7536 |
str = QLatin1String(""); |
|
7537 |
} |
|
7538 |
} |
|
7539 |
return in; |
|
7540 |
} |
|
7541 |
#endif // QT_NO_DATASTREAM |
|
7542 |
||
7543 |
/*! |
|
7544 |
\fn void QString::setLength(int nl) |
|
7545 |
||
7546 |
Use resize() instead. |
|
7547 |
*/ |
|
7548 |
||
7549 |
/*! |
|
7550 |
\fn QString QString::copy() const |
|
7551 |
||
7552 |
Use simple assignment instead. QString is implicitly shared so if |
|
7553 |
a copy is modified only the copy is changed. |
|
7554 |
*/ |
|
7555 |
||
7556 |
/*! |
|
7557 |
\fn QString &QString::remove(QChar c, bool cs) |
|
7558 |
||
7559 |
Use the remove(QChar, Qt::CaseSensitive) overload instead. |
|
7560 |
*/ |
|
7561 |
||
7562 |
/*! |
|
7563 |
\fn QString &QString::remove(const QString &s, bool cs) |
|
7564 |
||
7565 |
Use the remove(QString, Qt::CaseSensitive) overload instead. |
|
7566 |
*/ |
|
7567 |
||
7568 |
/*! |
|
7569 |
\fn QString &QString::replace(QChar c, const QString &after, bool cs) |
|
7570 |
||
7571 |
Use the replace(QChar, QString, Qt::CaseSensitive) overload instead. |
|
7572 |
*/ |
|
7573 |
||
7574 |
/*! |
|
7575 |
\fn QString &QString::replace(const QString &before, const QString &after, bool cs) |
|
7576 |
||
7577 |
Use the replace(QString, QString, Qt::CaseSensitive) overload instead. |
|
7578 |
*/ |
|
7579 |
||
7580 |
/*! |
|
7581 |
\fn QString &QString::replace(char c, const QString &after, bool cs) |
|
7582 |
||
7583 |
Use the replace(QChar, QString, Qt::CaseSensitive) overload instead. |
|
7584 |
*/ |
|
7585 |
||
7586 |
/*! |
|
7587 |
\fn QString &QString::replace(char c, const QString &after, Qt::CaseSensitivity cs) |
|
7588 |
||
7589 |
Use the replace(QChar, QString, Qt::CaseSensitive) overload instead. |
|
7590 |
*/ |
|
7591 |
||
7592 |
/*! |
|
7593 |
\fn int QString::find(QChar c, int i = 0, bool cs = true) const |
|
7594 |
||
7595 |
Use indexOf() instead. |
|
7596 |
*/ |
|
7597 |
||
7598 |
/*! |
|
7599 |
\fn int QString::find(const QString &s, int i = 0, bool cs = true) const |
|
7600 |
||
7601 |
Use indexOf() instead. |
|
7602 |
*/ |
|
7603 |
||
7604 |
/*! |
|
7605 |
\fn int QString::findRev(QChar c, int i = -1, bool cs = true) const |
|
7606 |
||
7607 |
Use lastIndexOf() instead. |
|
7608 |
*/ |
|
7609 |
||
7610 |
/*! |
|
7611 |
\fn int QString::findRev(const QString &s, int i = -1, bool cs = true) const |
|
7612 |
||
7613 |
Use lastIndexOf() instead. |
|
7614 |
*/ |
|
7615 |
||
7616 |
/*! |
|
7617 |
\fn int QString::find(const QRegExp &rx, int i=0) const |
|
7618 |
||
7619 |
Use indexOf() instead. |
|
7620 |
*/ |
|
7621 |
||
7622 |
/*! |
|
7623 |
\fn int QString::find(QRegExp &rx, int i=0) const |
|
7624 |
\internal |
|
7625 |
\since 4.5 |
|
7626 |
||
7627 |
Use indexOf() instead. |
|
7628 |
*/ |
|
7629 |
||
7630 |
/*! |
|
7631 |
\fn int QString::findRev(const QRegExp &rx, int i=-1) const |
|
7632 |
||
7633 |
Use lastIndexOf() instead. |
|
7634 |
*/ |
|
7635 |
||
7636 |
/*! |
|
7637 |
\fn int QString::findRev(QRegExp &rx, int i=0) const |
|
7638 |
\internal |
|
7639 |
\since 4.5 |
|
7640 |
||
7641 |
Use lastIndexOf() instead. |
|
7642 |
*/ |
|
7643 |
||
7644 |
/*! |
|
7645 |
\fn QBool QString::contains(QChar c, bool cs) const |
|
7646 |
||
7647 |
Use the contains(QChar, Qt::CaseSensitive) overload instead. |
|
7648 |
*/ |
|
7649 |
||
7650 |
/*! |
|
7651 |
\fn QBool QString::contains(const QString &s, bool cs) const |
|
7652 |
||
7653 |
Use the contains(QString, Qt::CaseSensitive) overload instead. |
|
7654 |
*/ |
|
7655 |
||
7656 |
/*! |
|
7657 |
\fn bool QString::startsWith(const QString &s, bool cs) const |
|
7658 |
||
7659 |
Use the startsWith(QString, Qt::CaseSensitive) overload instead. |
|
7660 |
*/ |
|
7661 |
||
7662 |
/*! |
|
7663 |
\fn bool QString::endsWith(const QString &s, bool cs) const |
|
7664 |
||
7665 |
Use the endsWith(QString, Qt::CaseSensitive) overload instead. |
|
7666 |
*/ |
|
7667 |
||
7668 |
/*! |
|
7669 |
\fn QString QString::leftJustify(int width, QChar fill = QLatin1Char(' '), bool trunc=false) const |
|
7670 |
||
7671 |
Use leftJustified() instead. |
|
7672 |
*/ |
|
7673 |
||
7674 |
/*! |
|
7675 |
\fn QString QString::rightJustify(int width, QChar fill = QLatin1Char(' '), bool trunc=false) const |
|
7676 |
||
7677 |
Use rightJustified() instead. |
|
7678 |
*/ |
|
7679 |
||
7680 |
/*! |
|
7681 |
\fn QString QString::lower() const |
|
7682 |
||
7683 |
Use toLower() instead. |
|
7684 |
*/ |
|
7685 |
||
7686 |
/*! |
|
7687 |
\fn QString QString::upper() const |
|
7688 |
||
7689 |
Use toUpper() instead. |
|
7690 |
*/ |
|
7691 |
||
7692 |
/*! |
|
7693 |
\fn QString QString::stripWhiteSpace() const |
|
7694 |
||
7695 |
Use trimmed() instead. |
|
7696 |
*/ |
|
7697 |
||
7698 |
/*! |
|
7699 |
\fn QString QString::simplifyWhiteSpace() const |
|
7700 |
||
7701 |
Use simplified() instead. |
|
7702 |
*/ |
|
7703 |
||
7704 |
/*! |
|
7705 |
\fn QString &QString::setUnicodeCodes(const ushort *unicode_as_ushorts, int size) |
|
7706 |
||
7707 |
Use setUtf16() instead. |
|
7708 |
*/ |
|
7709 |
||
7710 |
/*! |
|
7711 |
\fn ushort *QString::ucs2() const |
|
7712 |
||
7713 |
Use utf16() instead. |
|
7714 |
*/ |
|
7715 |
||
7716 |
/*! |
|
7717 |
\fn QString QString::fromUcs2(const ushort *unicode, int size = -1) |
|
7718 |
||
7719 |
Use fromUtf16() instead. |
|
7720 |
*/ |
|
7721 |
||
7722 |
/*! |
|
7723 |
\fn QString &QString::setAscii(const char *str, int len = -1) |
|
7724 |
||
7725 |
Use fromAscii() instead. |
|
7726 |
*/ |
|
7727 |
||
7728 |
/*! |
|
7729 |
\fn QString &QString::setLatin1(const char *str, int len = -1) |
|
7730 |
||
7731 |
Use fromLatin1() instead. |
|
7732 |
*/ |
|
7733 |
||
7734 |
/*! |
|
7735 |
\fn QChar QString::constref(uint i) const |
|
7736 |
||
7737 |
Use at() instead. |
|
7738 |
*/ |
|
7739 |
||
7740 |
/*! |
|
7741 |
\fn QChar &QString::ref(uint i); |
|
7742 |
||
7743 |
Use operator[]() instead. |
|
7744 |
*/ |
|
7745 |
||
7746 |
/*! |
|
7747 |
\fn QString::operator const char *() const |
|
7748 |
||
7749 |
Use toAscii().constData() instead. |
|
7750 |
*/ |
|
7751 |
||
7752 |
/*! |
|
7753 |
\class QConstString |
|
7754 |
\brief The QConstString class is a wrapper for constant Unicode string data. |
|
7755 |
\compat |
|
7756 |
||
7757 |
In Qt 4, QConstString is replaced by QString::fromRawData(), a |
|
7758 |
static function that constructs a QString object based on Unicode |
|
7759 |
string data. |
|
7760 |
||
7761 |
Because QString::fromRawData() has slightly more stringent |
|
7762 |
constraints than QConstString had in Qt 3, the new QConstString |
|
7763 |
class takes a deep copy of the string data. |
|
7764 |
||
7765 |
\sa QString::fromRawData() |
|
7766 |
*/ |
|
7767 |
||
7768 |
/*! |
|
7769 |
\fn QConstString::QConstString(const QChar *unicode, int size) |
|
7770 |
||
7771 |
Use QString(\a unicode, \a size) or |
|
7772 |
QString::fromRawData(\a unicode, \a size) instead. |
|
7773 |
*/ |
|
7774 |
||
7775 |
/*! |
|
7776 |
\fn const QString &QConstString::string() const |
|
7777 |
||
7778 |
Returns \c *this. Not necessary in Qt 4. |
|
7779 |
*/ |
|
7780 |
||
7781 |
||
7782 |
||
7783 |
/*! |
|
7784 |
\class QStringRef |
|
7785 |
\since 4.3 |
|
7786 |
\brief The QStringRef class provides a thin wrapper around QString substrings. |
|
7787 |
\reentrant |
|
7788 |
\ingroup tools |
|
7789 |
\ingroup string-processing |
|
7790 |
||
7791 |
QStringRef provides a read-only subset of the QString API. |
|
7792 |
||
7793 |
A string reference explicitly references a portion of a string() |
|
7794 |
with a given size(), starting at a specific position(). Calling |
|
7795 |
toString() returns a copy of the data as a real QString instance. |
|
7796 |
||
7797 |
This class is designed to improve the performance of substring |
|
7798 |
handling when manipulating substrings obtained from existing QString |
|
7799 |
instances. QStringRef avoids the memory allocation and reference |
|
7800 |
counting overhead of a standard QString by simply referencing a |
|
7801 |
part of the original string. This can prove to be advantageous in |
|
7802 |
low level code, such as that used in a parser, at the expense of |
|
7803 |
potentially more complex code. |
|
7804 |
||
7805 |
For most users, there are no semantic benefits to using QStringRef |
|
7806 |
instead of QString since QStringRef requires attention to be paid |
|
7807 |
to memory management issues, potentially making code more complex |
|
7808 |
to write and maintain. |
|
7809 |
||
7810 |
\warning A QStringRef is only valid as long as the referenced |
|
7811 |
string exists. If the original string is deleted, the string |
|
7812 |
reference points to an invalid memory location. |
|
7813 |
||
7814 |
We suggest that you only use this class in stable code where profiling |
|
7815 |
has clearly identified that performance improvements can be made by |
|
7816 |
replacing standard string operations with the optimized substring |
|
7817 |
handling provided by this class. |
|
7818 |
||
7819 |
\sa {Implicitly Shared Classes} |
|
7820 |
*/ |
|
7821 |
||
7822 |
||
7823 |
/*! |
|
7824 |
\fn QStringRef::QStringRef() |
|
7825 |
||
7826 |
Constructs an empty string reference. |
|
7827 |
*/ |
|
7828 |
||
7829 |
/*! \fn QStringRef::QStringRef(const QString *string, int position, int length) |
|
7830 |
||
7831 |
Constructs a string reference to the range of characters in the given |
|
7832 |
\a string specified by the starting \a position and \a length in characters. |
|
7833 |
||
7834 |
\warning This function exists to improve performance as much as possible, |
|
7835 |
and performs no bounds checking. For program correctness, \a position and |
|
7836 |
\a length must describe a valid substring of \a string. |
|
7837 |
||
7838 |
This means that the starting \a position must be positive or 0 and smaller |
|
7839 |
than \a string's length, and \a length must be positive or 0 but smaller than |
|
7840 |
the string's length minus the starting \a position; |
|
7841 |
i.e, 0 <= position < string->length() and |
|
7842 |
0 <= length <= string->length() - position must both be satisfied. |
|
7843 |
*/ |
|
7844 |
||
7845 |
/*! \fn QStringRef::QStringRef(const QString *string) |
|
7846 |
||
7847 |
Constructs a string reference to the given \a string. |
|
7848 |
*/ |
|
7849 |
||
7850 |
/*! \fn QStringRef::QStringRef(const QStringRef &other) |
|
7851 |
||
7852 |
Constructs a copy of the \a other string reference. |
|
7853 |
*/ |
|
7854 |
/*! |
|
7855 |
\fn QStringRef::~QStringRef() |
|
7856 |
||
7857 |
Destroys the string reference. |
|
7858 |
||
7859 |
Since this class is only used to refer to string data, and does not take |
|
7860 |
ownership of it, no memory is freed when instances are destroyed. |
|
7861 |
*/ |
|
7862 |
||
7863 |
||
7864 |
/*! |
|
7865 |
\fn int QStringRef::position() const |
|
7866 |
||
7867 |
Returns the starting position in the referenced string that is referred to |
|
7868 |
by the string reference. |
|
7869 |
||
7870 |
\sa size(), string() |
|
7871 |
*/ |
|
7872 |
||
7873 |
/*! |
|
7874 |
\fn int QStringRef::size() const |
|
7875 |
||
7876 |
Returns the number of characters referred to by the string reference. |
|
7877 |
Equivalent to length() and count(). |
|
7878 |
||
7879 |
\sa position(), string() |
|
7880 |
*/ |
|
7881 |
/*! |
|
7882 |
\fn int QStringRef::count() const |
|
7883 |
Returns the number of characters referred to by the string reference. |
|
7884 |
Equivalent to size() and length(). |
|
7885 |
||
7886 |
\sa position(), string() |
|
7887 |
*/ |
|
7888 |
/*! |
|
7889 |
\fn int QStringRef::length() const |
|
7890 |
Returns the number of characters referred to by the string reference. |
|
7891 |
Equivalent to size() and count(). |
|
7892 |
||
7893 |
\sa position(), string() |
|
7894 |
*/ |
|
7895 |
||
7896 |
||
7897 |
/*! |
|
7898 |
\fn bool QStringRef::isEmpty() const |
|
7899 |
||
7900 |
Returns true if the string reference has no characters; otherwise returns |
|
7901 |
false. |
|
7902 |
||
7903 |
A string reference is empty if its size is zero. |
|
7904 |
||
7905 |
\sa size() |
|
7906 |
*/ |
|
7907 |
||
7908 |
/*! |
|
7909 |
\fn bool QStringRef::isNull() const |
|
7910 |
||
7911 |
Returns true if string() returns a null pointer or a pointer to a |
|
7912 |
null string; otherwise returns true. |
|
7913 |
||
7914 |
\sa size() |
|
7915 |
*/ |
|
7916 |
||
7917 |
/*! |
|
7918 |
\fn const QString *QStringRef::string() const |
|
7919 |
||
7920 |
Returns a pointer to the string referred to by the string reference, or |
|
7921 |
0 if it does not reference a string. |
|
7922 |
||
7923 |
\sa unicode() |
|
7924 |
*/ |
|
7925 |
||
7926 |
||
7927 |
/*! |
|
7928 |
\fn const QChar *QStringRef::unicode() const |
|
7929 |
||
7930 |
Returns a Unicode representation of the string reference. Since |
|
7931 |
the data stems directly from the referenced string, it is not |
|
7932 |
null-terminated unless the string reference includes the string's |
|
7933 |
null terminator. |
|
7934 |
||
7935 |
\sa string() |
|
7936 |
*/ |
|
7937 |
||
7938 |
/*! |
|
7939 |
\fn const QChar *QStringRef::data() const |
|
7940 |
||
7941 |
Same as unicode(). |
|
7942 |
*/ |
|
7943 |
||
7944 |
/*! |
|
7945 |
\fn const QChar *QStringRef::constData() const |
|
7946 |
||
7947 |
Same as unicode(). |
|
7948 |
*/ |
|
7949 |
||
7950 |
/*! |
|
7951 |
Returns a copy of the string reference as a QString object. |
|
7952 |
||
7953 |
If the string reference is not a complete reference of the string |
|
7954 |
(meaning that position() is 0 and size() equals string()->size()), |
|
7955 |
this function will allocate a new string to return. |
|
7956 |
||
7957 |
\sa string() |
|
7958 |
*/ |
|
7959 |
||
7960 |
QString QStringRef::toString() const { |
|
7961 |
if (!m_string) |
|
7962 |
return QString(); |
|
7963 |
if (m_size && m_position == 0 && m_size == m_string->size()) |
|
7964 |
return *m_string; |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
7965 |
return QString(m_string->unicode() + m_position, m_size); |
0 | 7966 |
} |
7967 |
||
7968 |
||
7969 |
/*! \relates QStringRef |
|
7970 |
||
7971 |
Returns true if string reference \a s1 is lexically equal to string reference \a s2; otherwise |
|
7972 |
returns false. |
|
7973 |
*/ |
|
7974 |
bool operator==(const QStringRef &s1,const QStringRef &s2) |
|
7975 |
{ return (s1.size() == s2.size() && |
|
7976 |
qMemEquals((const ushort *)s1.unicode(), (const ushort *)s2.unicode(), s1.size())); |
|
7977 |
} |
|
7978 |
||
7979 |
/*! \relates QStringRef |
|
7980 |
||
7981 |
Returns true if string \a s1 is lexically equal to string reference \a s2; otherwise |
|
7982 |
returns false. |
|
7983 |
*/ |
|
7984 |
bool operator==(const QString &s1,const QStringRef &s2) |
|
7985 |
{ return (s1.size() == s2.size() && |
|
7986 |
qMemEquals((const ushort *)s1.unicode(), (const ushort *)s2.unicode(), s1.size())); |
|
7987 |
} |
|
7988 |
||
7989 |
/*! \relates QStringRef |
|
7990 |
||
7991 |
Returns true if string \a s1 is lexically equal to string reference \a s2; otherwise |
|
7992 |
returns false. |
|
7993 |
*/ |
|
7994 |
bool operator==(const QLatin1String &s1, const QStringRef &s2) |
|
7995 |
{ |
|
7996 |
const ushort *uc = reinterpret_cast<const ushort *>(s2.unicode()); |
|
7997 |
const ushort *e = uc + s2.size(); |
|
7998 |
const uchar *c = reinterpret_cast<const uchar *>(s1.latin1()); |
|
7999 |
if (!c) |
|
8000 |
return s2.isEmpty(); |
|
8001 |
||
8002 |
while (*c) { |
|
8003 |
if (uc == e || *uc != *c) |
|
8004 |
return false; |
|
8005 |
++uc; |
|
8006 |
++c; |
|
8007 |
} |
|
8008 |
return (uc == e); |
|
8009 |
} |
|
8010 |
||
8011 |
/*! |
|
8012 |
\relates QStringRef |
|
8013 |
||
8014 |
Returns true if string reference \a s1 is lexically less than |
|
8015 |
string reference \a s2; otherwise returns false. |
|
8016 |
||
8017 |
The comparison is based exclusively on the numeric Unicode values |
|
8018 |
of the characters and is very fast, but is not what a human would |
|
8019 |
expect. Consider sorting user-interface strings using the |
|
8020 |
QString::localeAwareCompare() function. |
|
8021 |
*/ |
|
8022 |
bool operator<(const QStringRef &s1,const QStringRef &s2) |
|
8023 |
{ |
|
8024 |
return ucstrcmp(s1.constData(), s1.length(), s2.constData(), s2.length()) < 0; |
|
8025 |
} |
|
8026 |
||
8027 |
/*!\fn bool operator<=(const QStringRef &s1,const QStringRef &s2) |
|
8028 |
||
8029 |
\relates QStringRef |
|
8030 |
||
8031 |
Returns true if string reference \a s1 is lexically less than |
|
8032 |
or equal to string reference \a s2; otherwise returns false. |
|
8033 |
||
8034 |
The comparison is based exclusively on the numeric Unicode values |
|
8035 |
of the characters and is very fast, but is not what a human would |
|
8036 |
expect. Consider sorting user-interface strings using the |
|
8037 |
QString::localeAwareCompare() function. |
|
8038 |
*/ |
|
8039 |
||
8040 |
/*!\fn bool operator>=(const QStringRef &s1,const QStringRef &s2) |
|
8041 |
||
8042 |
\relates QStringRef |
|
8043 |
||
8044 |
Returns true if string reference \a s1 is lexically greater than |
|
8045 |
or equal to string reference \a s2; otherwise returns false. |
|
8046 |
||
8047 |
The comparison is based exclusively on the numeric Unicode values |
|
8048 |
of the characters and is very fast, but is not what a human would |
|
8049 |
expect. Consider sorting user-interface strings using the |
|
8050 |
QString::localeAwareCompare() function. |
|
8051 |
*/ |
|
8052 |
||
8053 |
/*!\fn bool operator>(const QStringRef &s1,const QStringRef &s2) |
|
8054 |
||
8055 |
\relates QStringRef |
|
8056 |
||
8057 |
Returns true if string reference \a s1 is lexically greater than |
|
8058 |
string reference \a s2; otherwise returns false. |
|
8059 |
||
8060 |
The comparison is based exclusively on the numeric Unicode values |
|
8061 |
of the characters and is very fast, but is not what a human would |
|
8062 |
expect. Consider sorting user-interface strings using the |
|
8063 |
QString::localeAwareCompare() function. |
|
8064 |
*/ |
|
8065 |
||
8066 |
||
8067 |
/*! |
|
8068 |
\fn const QChar QStringRef::at(int position) const |
|
8069 |
||
8070 |
Returns the character at the given index \a position in the |
|
8071 |
string reference. |
|
8072 |
||
8073 |
The \a position must be a valid index position in the string |
|
8074 |
(i.e., 0 <= \a position < size()). |
|
8075 |
*/ |
|
8076 |
||
8077 |
/*! |
|
8078 |
\fn void QStringRef::clear() |
|
8079 |
||
8080 |
Clears the contents of the string reference by making it null and empty. |
|
8081 |
||
8082 |
\sa isEmpty(), isNull() |
|
8083 |
*/ |
|
8084 |
||
8085 |
/*! |
|
8086 |
\fn QStringRef &QStringRef::operator=(const QStringRef &other) |
|
8087 |
||
8088 |
Assigns the \a other string reference to this string reference, and |
|
8089 |
returns the result. |
|
8090 |
*/ |
|
8091 |
||
8092 |
/*! |
|
8093 |
\fn QStringRef &QStringRef::operator=(const QString *string) |
|
8094 |
||
8095 |
Constructs a string reference to the given \a string and assigns it to |
|
8096 |
this string reference, returning the result. |
|
8097 |
*/ |
|
8098 |
||
8099 |
/*! |
|
8100 |
\typedef QString::DataPtr |
|
8101 |
\internal |
|
8102 |
*/ |
|
8103 |
||
8104 |
/*! |
|
8105 |
\fn DataPtr & QString::data_ptr() |
|
8106 |
\internal |
|
8107 |
*/ |
|
8108 |
||
8109 |
||
8110 |
||
8111 |
/*! Appends the string reference to \a string, and returns a new |
|
8112 |
reference to the combined string data. |
|
8113 |
*/ |
|
8114 |
QStringRef QStringRef::appendTo(QString *string) const |
|
8115 |
{ |
|
8116 |
if (!string) |
|
8117 |
return QStringRef(); |
|
8118 |
int pos = string->size(); |
|
8119 |
string->insert(pos, unicode(), size()); |
|
8120 |
return QStringRef(string, pos, size()); |
|
8121 |
} |
|
8122 |
||
8123 |
/*! |
|
8124 |
\fn int QStringRef::compare(const QStringRef &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
|
8125 |
\since 4.5 |
|
8126 |
||
8127 |
Compares the string \a s1 with the string \a s2 and returns an |
|
8128 |
integer less than, equal to, or greater than zero if \a s1 |
|
8129 |
is less than, equal to, or greater than \a s2. |
|
8130 |
||
8131 |
If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
|
8132 |
otherwise the comparison is case insensitive. |
|
8133 |
*/ |
|
8134 |
||
8135 |
/*! |
|
8136 |
\fn int QStringRef::compare(const QStringRef &s1, const QStringRef &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
|
8137 |
\since 4.5 |
|
8138 |
\overload |
|
8139 |
||
8140 |
Compares the string \a s1 with the string \a s2 and returns an |
|
8141 |
integer less than, equal to, or greater than zero if \a s1 |
|
8142 |
is less than, equal to, or greater than \a s2. |
|
8143 |
||
8144 |
If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
|
8145 |
otherwise the comparison is case insensitive. |
|
8146 |
*/ |
|
8147 |
||
8148 |
/*! |
|
8149 |
\fn int QStringRef::compare(const QStringRef &s1, QLatin1String s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
|
8150 |
\since 4.5 |
|
8151 |
\overload |
|
8152 |
||
8153 |
Compares the string \a s1 with the string \a s2 and returns an |
|
8154 |
integer less than, equal to, or greater than zero if \a s1 |
|
8155 |
is less than, equal to, or greater than \a s2. |
|
8156 |
||
8157 |
If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
|
8158 |
otherwise the comparison is case insensitive. |
|
8159 |
*/ |
|
8160 |
||
8161 |
/*! |
|
8162 |
\overload |
|
8163 |
\fn int QStringRef::compare(const QString &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
|
8164 |
\since 4.5 |
|
8165 |
||
8166 |
Compares this string with the \a other string and returns an |
|
8167 |
integer less than, equal to, or greater than zero if this string |
|
8168 |
is less than, equal to, or greater than the \a other string. |
|
8169 |
||
8170 |
If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
|
8171 |
otherwise the comparison is case insensitive. |
|
8172 |
||
8173 |
Equivalent to \c {compare(*this, other, cs)}. |
|
8174 |
||
8175 |
\sa QString::compare() |
|
8176 |
*/ |
|
8177 |
||
8178 |
/*! |
|
8179 |
\overload |
|
8180 |
\fn int QStringRef::compare(const QStringRef &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
|
8181 |
\since 4.5 |
|
8182 |
||
8183 |
Compares this string with the \a other string and returns an |
|
8184 |
integer less than, equal to, or greater than zero if this string |
|
8185 |
is less than, equal to, or greater than the \a other string. |
|
8186 |
||
8187 |
If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
|
8188 |
otherwise the comparison is case insensitive. |
|
8189 |
||
8190 |
Equivalent to \c {compare(*this, other, cs)}. |
|
8191 |
||
8192 |
\sa QString::compare() |
|
8193 |
*/ |
|
8194 |
||
8195 |
/*! |
|
8196 |
\overload |
|
8197 |
\fn int QStringRef::compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
|
8198 |
\since 4.5 |
|
8199 |
||
8200 |
Compares this string with the \a other string and returns an |
|
8201 |
integer less than, equal to, or greater than zero if this string |
|
8202 |
is less than, equal to, or greater than the \a other string. |
|
8203 |
||
8204 |
If \a cs is Qt::CaseSensitive, the comparison is case sensitive; |
|
8205 |
otherwise the comparison is case insensitive. |
|
8206 |
||
8207 |
Equivalent to \c {compare(*this, other, cs)}. |
|
8208 |
||
8209 |
\sa QString::compare() |
|
8210 |
*/ |
|
8211 |
||
8212 |
/*! |
|
8213 |
\fn int QStringRef::localeAwareCompare(const QStringRef &s1, const QString & s2) |
|
8214 |
\since 4.5 |
|
8215 |
||
8216 |
Compares \a s1 with \a s2 and returns an integer less than, equal |
|
8217 |
to, or greater than zero if \a s1 is less than, equal to, or |
|
8218 |
greater than \a s2. |
|
8219 |
||
8220 |
The comparison is performed in a locale- and also |
|
8221 |
platform-dependent manner. Use this function to present sorted |
|
8222 |
lists of strings to the user. |
|
8223 |
||
8224 |
On Mac OS X, this function compares according the |
|
8225 |
"Order for sorted lists" setting in the International prefereces panel. |
|
8226 |
||
8227 |
\sa compare(), QTextCodec::locale() |
|
8228 |
*/ |
|
8229 |
||
8230 |
/*! |
|
8231 |
\fn int QStringRef::localeAwareCompare(const QStringRef &s1, const QStringRef & s2) |
|
8232 |
\since 4.5 |
|
8233 |
\overload |
|
8234 |
||
8235 |
Compares \a s1 with \a s2 and returns an integer less than, equal |
|
8236 |
to, or greater than zero if \a s1 is less than, equal to, or |
|
8237 |
greater than \a s2. |
|
8238 |
||
8239 |
The comparison is performed in a locale- and also |
|
8240 |
platform-dependent manner. Use this function to present sorted |
|
8241 |
lists of strings to the user. |
|
8242 |
||
8243 |
*/ |
|
8244 |
||
8245 |
/*! |
|
8246 |
\fn int QStringRef::localeAwareCompare(const QString &other) const |
|
8247 |
\since 4.5 |
|
8248 |
\overload |
|
8249 |
||
8250 |
Compares this string with the \a other string and returns an |
|
8251 |
integer less than, equal to, or greater than zero if this string |
|
8252 |
is less than, equal to, or greater than the \a other string. |
|
8253 |
||
8254 |
The comparison is performed in a locale- and also |
|
8255 |
platform-dependent manner. Use this function to present sorted |
|
8256 |
lists of strings to the user. |
|
8257 |
*/ |
|
8258 |
||
8259 |
/*! |
|
8260 |
\fn int QStringRef::localeAwareCompare(const QStringRef &other) const |
|
8261 |
\since 4.5 |
|
8262 |
\overload |
|
8263 |
||
8264 |
Compares this string with the \a other string and returns an |
|
8265 |
integer less than, equal to, or greater than zero if this string |
|
8266 |
is less than, equal to, or greater than the \a other string. |
|
8267 |
||
8268 |
The comparison is performed in a locale- and also |
|
8269 |
platform-dependent manner. Use this function to present sorted |
|
8270 |
lists of strings to the user. |
|
8271 |
*/ |
|
8272 |
||
8273 |
/*! |
|
8274 |
\fn QString &QString::append(const QStringRef &reference) |
|
8275 |
\since 4.4 |
|
8276 |
||
8277 |
Appends the given string \a reference to this string and returns the result. |
|
8278 |
*/ |
|
8279 |
QString &QString::append(const QStringRef &str) |
|
8280 |
{ |
|
8281 |
if (str.string() == this) { |
|
8282 |
str.appendTo(this); |
|
8283 |
} else if (str.string()) { |
|
8284 |
int oldSize = size(); |
|
8285 |
resize(oldSize + str.size()); |
|
8286 |
memcpy(data() + oldSize, str.unicode(), str.size() * sizeof(QChar)); |
|
8287 |
} |
|
8288 |
return *this; |
|
8289 |
} |
|
8290 |
||
8291 |
/*! |
|
8292 |
\since 4.4 |
|
8293 |
||
8294 |
Returns a substring reference to the \a n leftmost characters |
|
8295 |
of the string. |
|
8296 |
||
8297 |
If \a n is greater than size() or less than zero, a reference to the entire |
|
8298 |
string is returned. |
|
8299 |
||
8300 |
\snippet doc/src/snippets/qstring/main.cpp leftRef |
|
8301 |
||
8302 |
\sa left(), rightRef(), midRef(), startsWith() |
|
8303 |
*/ |
|
8304 |
QStringRef QString::leftRef(int n) const |
|
8305 |
{ |
|
8306 |
if (n >= d->size || n < 0) |
|
8307 |
n = d->size; |
|
8308 |
return QStringRef(this, 0, n); |
|
8309 |
} |
|
8310 |
||
8311 |
/*! |
|
8312 |
\since 4.4 |
|
8313 |
||
8314 |
Returns a substring reference to the \a n rightmost characters |
|
8315 |
of the string. |
|
8316 |
||
8317 |
If \a n is greater than size() or less than zero, a reference to the entire |
|
8318 |
string is returned. |
|
8319 |
||
8320 |
\snippet doc/src/snippets/qstring/main.cpp rightRef |
|
8321 |
||
8322 |
\sa right(), leftRef(), midRef(), endsWith() |
|
8323 |
*/ |
|
8324 |
QStringRef QString::rightRef(int n) const |
|
8325 |
{ |
|
8326 |
if (n >= d->size || n < 0) |
|
8327 |
n = d->size; |
|
8328 |
return QStringRef(this, d->size - n, n); |
|
8329 |
} |
|
8330 |
||
8331 |
/*! |
|
8332 |
\since 4.4 |
|
8333 |
||
8334 |
Returns a substring reference to \a n characters of this string, |
|
8335 |
starting at the specified \a position. |
|
8336 |
||
8337 |
If the \a position exceeds the length of the string, an empty |
|
8338 |
reference is returned. |
|
8339 |
||
8340 |
If there are less than \a n characters available in the string, |
|
8341 |
starting at the given \a position, or if \a n is -1 (default), the |
|
8342 |
function returns all characters from the specified \a position |
|
8343 |
onwards. |
|
8344 |
||
8345 |
Example: |
|
8346 |
||
8347 |
\snippet doc/src/snippets/qstring/main.cpp midRef |
|
8348 |
||
8349 |
\sa mid(), leftRef(), rightRef() |
|
8350 |
*/ |
|
8351 |
||
8352 |
QStringRef QString::midRef(int position, int n) const |
|
8353 |
{ |
|
8354 |
if (d == &shared_null || position >= d->size) |
|
8355 |
return QStringRef(); |
|
8356 |
if (n < 0) |
|
8357 |
n = d->size - position; |
|
8358 |
if (position < 0) { |
|
8359 |
n += position; |
|
8360 |
position = 0; |
|
8361 |
} |
|
8362 |
if (n + position > d->size) |
|
8363 |
n = d->size - position; |
|
8364 |
return QStringRef(this, position, n); |
|
8365 |
} |
|
8366 |
||
8367 |
QT_END_NAMESPACE |