author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Wed, 21 Apr 2010 11:15:19 +0300 | |
branch | RCL_3 |
changeset 11 | 25a739ee40f4 |
parent 5 | d3bac044e0f0 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
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 "qbytearray.h" |
|
43 |
#include "qbytearraymatcher.h" |
|
44 |
#include "qtools_p.h" |
|
45 |
#include "qstring.h" |
|
46 |
#include "qlist.h" |
|
47 |
#include "qlocale.h" |
|
48 |
#include "qlocale_p.h" |
|
49 |
#include "qunicodetables_p.h" |
|
50 |
#include "qscopedpointer.h" |
|
51 |
#include <qdatastream.h> |
|
52 |
||
53 |
#ifndef QT_NO_COMPRESS |
|
54 |
#include <zlib.h> |
|
55 |
#endif |
|
56 |
#include <ctype.h> |
|
57 |
#include <limits.h> |
|
58 |
#include <string.h> |
|
59 |
#include <stdlib.h> |
|
60 |
||
61 |
#define IS_RAW_DATA(d) ((d)->data != (d)->array) |
|
62 |
||
63 |
QT_BEGIN_NAMESPACE |
|
64 |
||
65 |
||
66 |
int qFindByteArray( |
|
67 |
const char *haystack0, int haystackLen, int from, |
|
68 |
const char *needle0, int needleLen); |
|
69 |
||
70 |
||
71 |
int qAllocMore(int alloc, int extra) |
|
72 |
{ |
|
73 |
if (alloc == 0 && extra == 0) |
|
74 |
return 0; |
|
75 |
const int page = 1 << 12; |
|
76 |
int nalloc; |
|
77 |
alloc += extra; |
|
78 |
if (alloc < 1<<6) { |
|
79 |
nalloc = (1<<3) + ((alloc >>3) << 3); |
|
80 |
} else { |
|
81 |
// don't do anything if the loop will overflow signed int. |
|
82 |
if (alloc >= INT_MAX/2) |
|
83 |
return INT_MAX; |
|
84 |
nalloc = (alloc < page) ? 1 << 3 : page; |
|
85 |
while (nalloc < alloc) { |
|
86 |
if (nalloc <= 0) |
|
87 |
return INT_MAX; |
|
88 |
nalloc *= 2; |
|
89 |
} |
|
90 |
} |
|
91 |
return nalloc - extra; |
|
92 |
} |
|
93 |
||
94 |
/***************************************************************************** |
|
95 |
Safe and portable C string functions; extensions to standard string.h |
|
96 |
*****************************************************************************/ |
|
97 |
||
98 |
/*! \relates QByteArray |
|
99 |
||
100 |
Returns a duplicate string. |
|
101 |
||
102 |
Allocates space for a copy of \a src, copies it, and returns a |
|
103 |
pointer to the copy. If \a src is 0, it immediately returns 0. |
|
104 |
||
105 |
Ownership is passed to the caller, so the returned string must be |
|
106 |
deleted using \c delete[]. |
|
107 |
*/ |
|
108 |
||
109 |
char *qstrdup(const char *src) |
|
110 |
{ |
|
111 |
if (!src) |
|
112 |
return 0; |
|
113 |
char *dst = new char[strlen(src) + 1]; |
|
114 |
return qstrcpy(dst, src); |
|
115 |
} |
|
116 |
||
117 |
/*! \relates QByteArray |
|
118 |
||
119 |
Copies all the characters up to and including the '\\0' from \a |
|
120 |
src into \a dst and returns a pointer to \a dst. If \a src is 0, |
|
121 |
it immediately returns 0. |
|
122 |
||
123 |
This function assumes that \a dst is large enough to hold the |
|
124 |
contents of \a src. |
|
125 |
||
126 |
\sa qstrncpy() |
|
127 |
*/ |
|
128 |
||
129 |
char *qstrcpy(char *dst, const char *src) |
|
130 |
{ |
|
131 |
if (!src) |
|
132 |
return 0; |
|
133 |
#if defined(_MSC_VER) && _MSC_VER >= 1400 |
|
134 |
int len = qstrlen(src); |
|
135 |
// This is actually not secure!!! It will be fixed |
|
136 |
// properly in a later release! |
|
137 |
if (len >= 0 && strcpy_s(dst, len+1, src) == 0) |
|
138 |
return dst; |
|
139 |
return 0; |
|
140 |
#else |
|
141 |
return strcpy(dst, src); |
|
142 |
#endif |
|
143 |
} |
|
144 |
||
145 |
/*! \relates QByteArray |
|
146 |
||
147 |
A safe \c strncpy() function. |
|
148 |
||
149 |
Copies at most \a len bytes from \a src (stopping at \a len or the |
|
150 |
terminating '\\0' whichever comes first) into \a dst and returns a |
|
151 |
pointer to \a dst. Guarantees that \a dst is '\\0'-terminated. If |
|
152 |
\a src or \a dst is 0, returns 0 immediately. |
|
153 |
||
154 |
This function assumes that \a dst is at least \a len characters |
|
155 |
long. |
|
156 |
||
157 |
\sa qstrcpy() |
|
158 |
*/ |
|
159 |
||
160 |
char *qstrncpy(char *dst, const char *src, uint len) |
|
161 |
{ |
|
162 |
if (!src || !dst) |
|
163 |
return 0; |
|
164 |
#if defined(_MSC_VER) && _MSC_VER >= 1400 |
|
165 |
strncpy_s(dst, len, src, len-1); |
|
166 |
#else |
|
167 |
strncpy(dst, src, len); |
|
168 |
#endif |
|
169 |
if (len > 0) |
|
170 |
dst[len-1] = '\0'; |
|
171 |
return dst; |
|
172 |
} |
|
173 |
||
174 |
/*! \fn uint qstrlen(const char *str) |
|
175 |
\relates QByteArray |
|
176 |
||
177 |
A safe \c strlen() function. |
|
178 |
||
179 |
Returns the number of characters that precede the terminating '\\0', |
|
180 |
or 0 if \a str is 0. |
|
181 |
||
182 |
\sa qstrnlen() |
|
183 |
*/ |
|
184 |
||
185 |
/*! \fn uint qstrnlen(const char *str, uint maxlen) |
|
186 |
\relates QByteArray |
|
187 |
\since 4.2 |
|
188 |
||
189 |
A safe \c strnlen() function. |
|
190 |
||
191 |
Returns the number of characters that precede the terminating '\\0', but |
|
192 |
at most \a maxlen. If \a str is 0, returns 0. |
|
193 |
||
194 |
\sa qstrlen() |
|
195 |
*/ |
|
196 |
||
197 |
/*! |
|
198 |
\relates QByteArray |
|
199 |
||
200 |
A safe \c strcmp() function. |
|
201 |
||
202 |
Compares \a str1 and \a str2. Returns a negative value if \a str1 |
|
203 |
is less than \a str2, 0 if \a str1 is equal to \a str2 or a |
|
204 |
positive value if \a str1 is greater than \a str2. |
|
205 |
||
206 |
Special case 1: Returns 0 if \a str1 and \a str2 are both 0. |
|
207 |
||
208 |
Special case 2: Returns an arbitrary non-zero value if \a str1 is 0 |
|
209 |
or \a str2 is 0 (but not both). |
|
210 |
||
211 |
\sa qstrncmp(), qstricmp(), qstrnicmp(), {8-bit Character Comparisons} |
|
212 |
*/ |
|
213 |
int qstrcmp(const char *str1, const char *str2) |
|
214 |
{ |
|
215 |
return (str1 && str2) ? strcmp(str1, str2) |
|
216 |
: (str1 ? 1 : (str2 ? -1 : 0)); |
|
217 |
} |
|
218 |
||
219 |
/*! \fn int qstrncmp(const char *str1, const char *str2, uint len); |
|
220 |
||
221 |
\relates QByteArray |
|
222 |
||
223 |
A safe \c strncmp() function. |
|
224 |
||
225 |
Compares at most \a len bytes of \a str1 and \a str2. |
|
226 |
||
227 |
Returns a negative value if \a str1 is less than \a str2, 0 if \a |
|
228 |
str1 is equal to \a str2 or a positive value if \a str1 is greater |
|
229 |
than \a str2. |
|
230 |
||
231 |
Special case 1: Returns 0 if \a str1 and \a str2 are both 0. |
|
232 |
||
233 |
Special case 2: Returns a random non-zero value if \a str1 is 0 |
|
234 |
or \a str2 is 0 (but not both). |
|
235 |
||
236 |
\sa qstrcmp(), qstricmp(), qstrnicmp(), {8-bit Character Comparisons} |
|
237 |
*/ |
|
238 |
||
239 |
/*! \relates QByteArray |
|
240 |
||
241 |
A safe \c stricmp() function. |
|
242 |
||
243 |
Compares \a str1 and \a str2 ignoring the case of the |
|
244 |
characters. The encoding of the strings is assumed to be Latin-1. |
|
245 |
||
246 |
Returns a negative value if \a str1 is less than \a str2, 0 if \a |
|
247 |
str1 is equal to \a str2 or a positive value if \a str1 is greater |
|
248 |
than \a str2. |
|
249 |
||
250 |
Special case 1: Returns 0 if \a str1 and \a str2 are both 0. |
|
251 |
||
252 |
Special case 2: Returns a random non-zero value if \a str1 is 0 |
|
253 |
or \a str2 is 0 (but not both). |
|
254 |
||
255 |
\sa qstrcmp(), qstrncmp(), qstrnicmp(), {8-bit Character Comparisons} |
|
256 |
*/ |
|
257 |
||
258 |
int qstricmp(const char *str1, const char *str2) |
|
259 |
{ |
|
260 |
register const uchar *s1 = reinterpret_cast<const uchar *>(str1); |
|
261 |
register const uchar *s2 = reinterpret_cast<const uchar *>(str2); |
|
262 |
int res; |
|
263 |
uchar c; |
|
264 |
if (!s1 || !s2) |
|
265 |
return s1 ? 1 : (s2 ? -1 : 0); |
|
266 |
for (; !(res = (c = QChar::toLower((ushort)*s1)) - QChar::toLower((ushort)*s2)); s1++, s2++) |
|
267 |
if (!c) // strings are equal |
|
268 |
break; |
|
269 |
return res; |
|
270 |
} |
|
271 |
||
272 |
/*! \relates QByteArray |
|
273 |
||
274 |
A safe \c strnicmp() function. |
|
275 |
||
276 |
Compares at most \a len bytes of \a str1 and \a str2 ignoring the |
|
277 |
case of the characters. The encoding of the strings is assumed to |
|
278 |
be Latin-1. |
|
279 |
||
280 |
Returns a negative value if \a str1 is less than \a str2, 0 if \a str1 |
|
281 |
is equal to \a str2 or a positive value if \a str1 is greater than \a |
|
282 |
str2. |
|
283 |
||
284 |
Special case 1: Returns 0 if \a str1 and \a str2 are both 0. |
|
285 |
||
286 |
Special case 2: Returns a random non-zero value if \a str1 is 0 |
|
287 |
or \a str2 is 0 (but not both). |
|
288 |
||
289 |
\sa qstrcmp(), qstrncmp(), qstricmp(), {8-bit Character Comparisons} |
|
290 |
*/ |
|
291 |
||
292 |
int qstrnicmp(const char *str1, const char *str2, uint len) |
|
293 |
{ |
|
294 |
register const uchar *s1 = reinterpret_cast<const uchar *>(str1); |
|
295 |
register const uchar *s2 = reinterpret_cast<const uchar *>(str2); |
|
296 |
int res; |
|
297 |
uchar c; |
|
298 |
if (!s1 || !s2) |
|
299 |
return s1 ? 1 : (s2 ? -1 : 0); |
|
300 |
for (; len--; s1++, s2++) { |
|
301 |
if ((res = (c = QChar::toLower((ushort)*s1)) - QChar::toLower((ushort)*s2))) |
|
302 |
return res; |
|
303 |
if (!c) // strings are equal |
|
304 |
break; |
|
305 |
} |
|
306 |
return 0; |
|
307 |
} |
|
308 |
||
309 |
/*! |
|
310 |
\internal |
|
311 |
*/ |
|
312 |
int qstrcmp(const QByteArray &str1, const char *str2) |
|
313 |
{ |
|
314 |
if (!str2) |
|
315 |
return str1.isEmpty() ? 0 : +1; |
|
316 |
||
317 |
const char *str1data = str1.constData(); |
|
318 |
const char *str1end = str1data + str1.length(); |
|
319 |
for ( ; str1data < str1end && *str2; ++str1data, ++str2) { |
|
320 |
register int diff = int(uchar(*str1data)) - uchar(*str2); |
|
321 |
if (diff) |
|
322 |
// found a difference |
|
323 |
return diff; |
|
324 |
} |
|
325 |
||
326 |
// Why did we stop? |
|
327 |
if (*str2 != '\0') |
|
328 |
// not the null, so we stopped because str1 is shorter |
|
329 |
return -1; |
|
330 |
if (str1data < str1end) |
|
331 |
// we haven't reached the end, so str1 must be longer |
|
332 |
return +1; |
|
333 |
return 0; |
|
334 |
} |
|
335 |
||
336 |
/*! |
|
337 |
\internal |
|
338 |
*/ |
|
339 |
int qstrcmp(const QByteArray &str1, const QByteArray &str2) |
|
340 |
{ |
|
341 |
int l1 = str1.length(); |
|
342 |
int l2 = str2.length(); |
|
343 |
int ret = memcmp(str1, str2, qMin(l1, l2)); |
|
344 |
if (ret != 0) |
|
345 |
return ret; |
|
346 |
||
347 |
// they matched qMin(l1, l2) bytes |
|
348 |
// so the longer one is lexically after the shorter one |
|
349 |
return l1 - l2; |
|
350 |
} |
|
351 |
||
352 |
// the CRC table below is created by the following piece of code |
|
353 |
#if 0 |
|
354 |
static void createCRC16Table() // build CRC16 lookup table |
|
355 |
{ |
|
356 |
register unsigned int i; |
|
357 |
register unsigned int j; |
|
358 |
unsigned short crc_tbl[16]; |
|
359 |
unsigned int v0, v1, v2, v3; |
|
360 |
for (i = 0; i < 16; i++) { |
|
361 |
v0 = i & 1; |
|
362 |
v1 = (i >> 1) & 1; |
|
363 |
v2 = (i >> 2) & 1; |
|
364 |
v3 = (i >> 3) & 1; |
|
365 |
j = 0; |
|
366 |
#undef SET_BIT |
|
367 |
#define SET_BIT(x, b, v) (x) |= (v) << (b) |
|
368 |
SET_BIT(j, 0, v0); |
|
369 |
SET_BIT(j, 7, v0); |
|
370 |
SET_BIT(j, 12, v0); |
|
371 |
SET_BIT(j, 1, v1); |
|
372 |
SET_BIT(j, 8, v1); |
|
373 |
SET_BIT(j, 13, v1); |
|
374 |
SET_BIT(j, 2, v2); |
|
375 |
SET_BIT(j, 9, v2); |
|
376 |
SET_BIT(j, 14, v2); |
|
377 |
SET_BIT(j, 3, v3); |
|
378 |
SET_BIT(j, 10, v3); |
|
379 |
SET_BIT(j, 15, v3); |
|
380 |
crc_tbl[i] = j; |
|
381 |
} |
|
382 |
printf("static const quint16 crc_tbl[16] = {\n"); |
|
383 |
for (int i = 0; i < 16; i +=4) |
|
384 |
printf(" 0x%04x, 0x%04x, 0x%04x, 0x%04x,\n", crc_tbl[i], crc_tbl[i+1], crc_tbl[i+2], crc_tbl[i+3]); |
|
385 |
printf("};\n"); |
|
386 |
} |
|
387 |
#endif |
|
388 |
||
389 |
static const quint16 crc_tbl[16] = { |
|
390 |
0x0000, 0x1081, 0x2102, 0x3183, |
|
391 |
0x4204, 0x5285, 0x6306, 0x7387, |
|
392 |
0x8408, 0x9489, 0xa50a, 0xb58b, |
|
393 |
0xc60c, 0xd68d, 0xe70e, 0xf78f |
|
394 |
}; |
|
395 |
||
396 |
/*! |
|
397 |
\relates QByteArray |
|
398 |
||
399 |
Returns the CRC-16 checksum of the first \a len bytes of \a data. |
|
400 |
||
401 |
The checksum is independent of the byte order (endianness). |
|
402 |
||
403 |
\note This function is a 16-bit cache conserving (16 entry table) |
|
404 |
implementation of the CRC-16-CCITT algorithm. |
|
405 |
*/ |
|
406 |
||
407 |
quint16 qChecksum(const char *data, uint len) |
|
408 |
{ |
|
409 |
register quint16 crc = 0xffff; |
|
410 |
uchar c; |
|
411 |
const uchar *p = reinterpret_cast<const uchar *>(data); |
|
412 |
while (len--) { |
|
413 |
c = *p++; |
|
414 |
crc = ((crc >> 4) & 0x0fff) ^ crc_tbl[((crc ^ c) & 15)]; |
|
415 |
c >>= 4; |
|
416 |
crc = ((crc >> 4) & 0x0fff) ^ crc_tbl[((crc ^ c) & 15)]; |
|
417 |
} |
|
418 |
return ~crc & 0xffff; |
|
419 |
} |
|
420 |
||
421 |
/*! |
|
422 |
\fn QByteArray qCompress(const QByteArray& data, int compressionLevel) |
|
423 |
||
424 |
\relates QByteArray |
|
425 |
||
426 |
Compresses the \a data byte array and returns the compressed data |
|
427 |
in a new byte array. |
|
428 |
||
429 |
The \a compressionLevel parameter specifies how much compression |
|
430 |
should be used. Valid values are between 0 and 9, with 9 |
|
431 |
corresponding to the greatest compression (i.e. smaller compressed |
|
432 |
data) at the cost of using a slower algorithm. Smaller values (8, |
|
433 |
7, ..., 1) provide successively less compression at slightly |
|
434 |
faster speeds. The value 0 corresponds to no compression at all. |
|
435 |
The default value is -1, which specifies zlib's default |
|
436 |
compression. |
|
437 |
||
438 |
\sa qUncompress() |
|
439 |
*/ |
|
440 |
||
441 |
/*! \relates QByteArray |
|
442 |
||
443 |
\overload |
|
444 |
||
445 |
Compresses the first \a nbytes of \a data and returns the |
|
446 |
compressed data in a new byte array. |
|
447 |
*/ |
|
448 |
||
449 |
#ifndef QT_NO_COMPRESS |
|
450 |
QByteArray qCompress(const uchar* data, int nbytes, int compressionLevel) |
|
451 |
{ |
|
452 |
if (nbytes == 0) { |
|
453 |
return QByteArray(4, '\0'); |
|
454 |
} |
|
455 |
if (!data) { |
|
456 |
qWarning("qCompress: Data is null"); |
|
457 |
return QByteArray(); |
|
458 |
} |
|
459 |
if (compressionLevel < -1 || compressionLevel > 9) |
|
460 |
compressionLevel = -1; |
|
461 |
||
462 |
ulong len = nbytes + nbytes / 100 + 13; |
|
463 |
QByteArray bazip; |
|
464 |
int res; |
|
465 |
do { |
|
466 |
bazip.resize(len + 4); |
|
467 |
res = ::compress2((uchar*)bazip.data()+4, &len, (uchar*)data, nbytes, compressionLevel); |
|
468 |
||
469 |
switch (res) { |
|
470 |
case Z_OK: |
|
471 |
bazip.resize(len + 4); |
|
472 |
bazip[0] = (nbytes & 0xff000000) >> 24; |
|
473 |
bazip[1] = (nbytes & 0x00ff0000) >> 16; |
|
474 |
bazip[2] = (nbytes & 0x0000ff00) >> 8; |
|
475 |
bazip[3] = (nbytes & 0x000000ff); |
|
476 |
break; |
|
477 |
case Z_MEM_ERROR: |
|
478 |
qWarning("qCompress: Z_MEM_ERROR: Not enough memory"); |
|
479 |
bazip.resize(0); |
|
480 |
break; |
|
481 |
case Z_BUF_ERROR: |
|
482 |
len *= 2; |
|
483 |
break; |
|
484 |
} |
|
485 |
} while (res == Z_BUF_ERROR); |
|
486 |
||
487 |
return bazip; |
|
488 |
} |
|
489 |
#endif |
|
490 |
||
491 |
/*! |
|
492 |
\fn QByteArray qUncompress(const QByteArray& data) |
|
493 |
||
494 |
\relates QByteArray |
|
495 |
||
496 |
Uncompresses the \a data byte array and returns a new byte array |
|
497 |
with the uncompressed data. |
|
498 |
||
499 |
Returns an empty QByteArray if the input data was corrupt. |
|
500 |
||
501 |
This function will uncompress data compressed with qCompress() |
|
502 |
from this and any earlier Qt version, back to Qt 3.1 when this |
|
503 |
feature was added. |
|
504 |
||
505 |
\bold{Note:} If you want to use this function to uncompress external |
|
506 |
data compressed using zlib, you first need to prepend four bytes to the |
|
507 |
byte array that contain the expected length (as an unsigned integer) |
|
508 |
of the uncompressed data encoded in big-endian order (most significant |
|
509 |
byte first). |
|
510 |
||
511 |
\sa qCompress() |
|
512 |
*/ |
|
513 |
||
514 |
/*! \relates QByteArray |
|
515 |
||
516 |
\overload |
|
517 |
||
518 |
Uncompresses the first \a nbytes of \a data and returns a new byte |
|
519 |
array with the uncompressed data. |
|
520 |
*/ |
|
521 |
||
522 |
#ifndef QT_NO_COMPRESS |
|
523 |
QByteArray qUncompress(const uchar* data, int nbytes) |
|
524 |
{ |
|
525 |
if (!data) { |
|
526 |
qWarning("qUncompress: Data is null"); |
|
527 |
return QByteArray(); |
|
528 |
} |
|
529 |
if (nbytes <= 4) { |
|
530 |
if (nbytes < 4 || (data[0]!=0 || data[1]!=0 || data[2]!=0 || data[3]!=0)) |
|
531 |
qWarning("qUncompress: Input data is corrupted"); |
|
532 |
return QByteArray(); |
|
533 |
} |
|
534 |
ulong expectedSize = (data[0] << 24) | (data[1] << 16) | |
|
535 |
(data[2] << 8) | (data[3] ); |
|
536 |
ulong len = qMax(expectedSize, 1ul); |
|
537 |
QScopedPointer<QByteArray::Data, QScopedPointerPodDeleter> d; |
|
538 |
||
539 |
forever { |
|
540 |
ulong alloc = len; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
541 |
d.reset(q_check_ptr(static_cast<QByteArray::Data *>(qRealloc(d.take(), sizeof(QByteArray::Data) + alloc)))); |
0 | 542 |
if (!d) { |
543 |
// we are not allowed to crash here when compiling with QT_NO_EXCEPTIONS |
|
544 |
qWarning("qUncompress: could not allocate enough memory to uncompress data"); |
|
545 |
return QByteArray(); |
|
546 |
} |
|
547 |
||
548 |
int res = ::uncompress((uchar*)d->array, &len, |
|
549 |
(uchar*)data+4, nbytes-4); |
|
550 |
||
551 |
switch (res) { |
|
552 |
case Z_OK: |
|
553 |
if (len != alloc) { |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
554 |
d.reset(q_check_ptr(static_cast<QByteArray::Data *>(qRealloc(d.take(), sizeof(QByteArray::Data) + len)))); |
0 | 555 |
if (!d) { |
556 |
// we are not allowed to crash here when compiling with QT_NO_EXCEPTIONS |
|
557 |
qWarning("qUncompress: could not allocate enough memory to uncompress data"); |
|
558 |
return QByteArray(); |
|
559 |
} |
|
560 |
} |
|
561 |
d->ref = 1; |
|
562 |
d->alloc = d->size = len; |
|
563 |
d->data = d->array; |
|
5
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
564 |
d->array[len] = 0; |
0 | 565 |
|
566 |
return QByteArray(d.take(), 0, 0); |
|
567 |
||
568 |
case Z_MEM_ERROR: |
|
569 |
qWarning("qUncompress: Z_MEM_ERROR: Not enough memory"); |
|
570 |
return QByteArray(); |
|
571 |
||
572 |
case Z_BUF_ERROR: |
|
573 |
len *= 2; |
|
574 |
continue; |
|
575 |
||
576 |
case Z_DATA_ERROR: |
|
577 |
qWarning("qUncompress: Z_DATA_ERROR: Input data is corrupted"); |
|
578 |
return QByteArray(); |
|
579 |
} |
|
580 |
} |
|
581 |
} |
|
582 |
#endif |
|
583 |
||
584 |
static inline bool qIsUpper(char c) |
|
585 |
{ |
|
586 |
return c >= 'A' && c <= 'Z'; |
|
587 |
} |
|
588 |
||
589 |
static inline char qToLower(char c) |
|
590 |
{ |
|
591 |
if (c >= 'A' && c <= 'Z') |
|
592 |
return c - 'A' + 'a'; |
|
593 |
else |
|
594 |
return c; |
|
595 |
} |
|
596 |
||
597 |
QByteArray::Data QByteArray::shared_null = {Q_BASIC_ATOMIC_INITIALIZER(1), |
|
598 |
0, 0, shared_null.array, {0} }; |
|
599 |
QByteArray::Data QByteArray::shared_empty = { Q_BASIC_ATOMIC_INITIALIZER(1), |
|
600 |
0, 0, shared_empty.array, {0} }; |
|
601 |
||
602 |
/*! |
|
603 |
\class QByteArray |
|
604 |
\brief The QByteArray class provides an array of bytes. |
|
605 |
||
606 |
\ingroup tools |
|
607 |
\ingroup shared |
|
608 |
\ingroup string-processing |
|
609 |
||
610 |
\reentrant |
|
611 |
||
612 |
QByteArray can be used to store both raw bytes (including '\\0's) |
|
613 |
and traditional 8-bit '\\0'-terminated strings. Using QByteArray |
|
614 |
is much more convenient than using \c{const char *}. Behind the |
|
615 |
scenes, it always ensures that the data is followed by a '\\0' |
|
616 |
terminator, and uses \l{implicit sharing} (copy-on-write) to |
|
617 |
reduce memory usage and avoid needless copying of data. |
|
618 |
||
619 |
In addition to QByteArray, Qt also provides the QString class to |
|
620 |
store string data. For most purposes, QString is the class you |
|
621 |
want to use. It stores 16-bit Unicode characters, making it easy |
|
622 |
to store non-ASCII/non-Latin-1 characters in your application. |
|
623 |
Furthermore, QString is used throughout in the Qt API. The two |
|
624 |
main cases where QByteArray is appropriate are when you need to |
|
625 |
store raw binary data, and when memory conservation is critical |
|
626 |
(e.g., with Qt for Embedded Linux). |
|
627 |
||
628 |
One way to initialize a QByteArray is simply to pass a \c{const |
|
629 |
char *} to its constructor. For example, the following code |
|
630 |
creates a byte array of size 5 containing the data "Hello": |
|
631 |
||
632 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 0 |
|
633 |
||
634 |
Although the size() is 5, the byte array also maintains an extra |
|
635 |
'\\0' character at the end so that if a function is used that |
|
636 |
asks for a pointer to the underlying data (e.g. a call to |
|
637 |
data()), the data pointed to is guaranteed to be |
|
638 |
'\\0'-terminated. |
|
639 |
||
640 |
QByteArray makes a deep copy of the \c{const char *} data, so you |
|
641 |
can modify it later without experiencing side effects. (If for |
|
642 |
performance reasons you don't want to take a deep copy of the |
|
643 |
character data, use QByteArray::fromRawData() instead.) |
|
644 |
||
645 |
Another approach is to set the size of the array using resize() |
|
646 |
and to initialize the data byte per byte. QByteArray uses 0-based |
|
647 |
indexes, just like C++ arrays. To access the byte at a particular |
|
648 |
index position, you can use operator[](). On non-const byte |
|
649 |
arrays, operator[]() returns a reference to a byte that can be |
|
650 |
used on the left side of an assignment. For example: |
|
651 |
||
652 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 1 |
|
653 |
||
654 |
For read-only access, an alternative syntax is to use at(): |
|
655 |
||
656 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 2 |
|
657 |
||
658 |
at() can be faster than operator[](), because it never causes a |
|
659 |
\l{deep copy} to occur. |
|
660 |
||
661 |
To extract many bytes at a time, use left(), right(), or mid(). |
|
662 |
||
663 |
A QByteArray can embed '\\0' bytes. The size() function always |
|
664 |
returns the size of the whole array, including embedded '\\0' |
|
665 |
bytes. If you want to obtain the length of the data up to and |
|
666 |
excluding the first '\\0' character, call qstrlen() on the byte |
|
667 |
array. |
|
668 |
||
669 |
After a call to resize(), newly allocated bytes have undefined |
|
670 |
values. To set all the bytes to a particular value, call fill(). |
|
671 |
||
672 |
To obtain a pointer to the actual character data, call data() or |
|
673 |
constData(). These functions return a pointer to the beginning of |
|
674 |
the data. The pointer is guaranteed to remain valid until a |
|
675 |
non-const function is called on the QByteArray. It is also |
|
676 |
guaranteed that the data ends with a '\\0' byte. This '\\0' byte |
|
677 |
is automatically provided by QByteArray and is not counted in |
|
678 |
size(). |
|
679 |
||
680 |
QByteArray provides the following basic functions for modifying |
|
681 |
the byte data: append(), prepend(), insert(), replace(), and |
|
682 |
remove(). For example: |
|
683 |
||
684 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 3 |
|
685 |
||
686 |
The replace() and remove() functions' first two arguments are the |
|
687 |
position from which to start erasing and the number of bytes that |
|
688 |
should be erased. |
|
689 |
||
690 |
When you append() data to a non-empty array, the array will be |
|
691 |
reallocated and the new data copied to it. You can avoid this |
|
692 |
behavior by calling reserve(), which preallocates a certain amount |
|
693 |
of memory. You can also call capacity() to find out how much |
|
694 |
memory QByteArray actually allocated. Data appended to an empty |
|
695 |
array is not copied. |
|
696 |
||
697 |
A frequent requirement is to remove whitespace characters from a |
|
698 |
byte array ('\\n', '\\t', ' ', etc.). If you want to remove |
|
699 |
whitespace from both ends of a QByteArray, use trimmed(). If you |
|
700 |
want to remove whitespace from both ends and replace multiple |
|
701 |
consecutive whitespaces with a single space character within the |
|
702 |
byte array, use simplified(). |
|
703 |
||
704 |
If you want to find all occurrences of a particular character or |
|
705 |
substring in a QByteArray, use indexOf() or lastIndexOf(). The |
|
706 |
former searches forward starting from a given index position, the |
|
707 |
latter searches backward. Both return the index position of the |
|
708 |
character or substring if they find it; otherwise, they return -1. |
|
709 |
For example, here's a typical loop that finds all occurrences of a |
|
710 |
particular substring: |
|
711 |
||
712 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 4 |
|
713 |
||
714 |
If you simply want to check whether a QByteArray contains a |
|
715 |
particular character or substring, use contains(). If you want to |
|
716 |
find out how many times a particular character or substring |
|
717 |
occurs in the byte array, use count(). If you want to replace all |
|
718 |
occurrences of a particular value with another, use one of the |
|
719 |
two-parameter replace() overloads. |
|
720 |
||
721 |
QByteArrays can be compared using overloaded operators such as |
|
722 |
operator<(), operator<=(), operator==(), operator>=(), and so on. |
|
723 |
The comparison is based exclusively on the numeric values |
|
724 |
of the characters and is very fast, but is not what a human would |
|
725 |
expect. QString::localeAwareCompare() is a better choice for |
|
726 |
sorting user-interface strings. |
|
727 |
||
728 |
For historical reasons, QByteArray distinguishes between a null |
|
729 |
byte array and an empty byte array. A \e null byte array is a |
|
730 |
byte array that is initialized using QByteArray's default |
|
731 |
constructor or by passing (const char *)0 to the constructor. An |
|
732 |
\e empty byte array is any byte array with size 0. A null byte |
|
733 |
array is always empty, but an empty byte array isn't necessarily |
|
734 |
null: |
|
735 |
||
736 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 5 |
|
737 |
||
738 |
All functions except isNull() treat null byte arrays the same as |
|
739 |
empty byte arrays. For example, data() returns a pointer to a |
|
740 |
'\\0' character for a null byte array (\e not a null pointer), |
|
741 |
and QByteArray() compares equal to QByteArray(""). We recommend |
|
742 |
that you always use isEmpty() and avoid isNull(). |
|
743 |
||
744 |
\section1 Notes on Locale |
|
745 |
||
746 |
\section2 Number-String Conversions |
|
747 |
||
748 |
Functions that perform conversions between numeric data types and |
|
749 |
strings are performed in the C locale, irrespective of the user's |
|
750 |
locale settings. Use QString to perform locale-aware conversions |
|
751 |
between numbers and strings. |
|
752 |
||
753 |
\section2 8-bit Character Comparisons |
|
754 |
||
755 |
In QByteArray, the notion of uppercase and lowercase and of which |
|
756 |
character is greater than or less than another character is |
|
757 |
locale dependent. This affects functions that support a case |
|
758 |
insensitive option or that compare or lowercase or uppercase |
|
759 |
their arguments. Case insensitive operations and comparisons will |
|
760 |
be accurate if both strings contain only ASCII characters. (If \c |
|
761 |
$LC_CTYPE is set, most Unix systems do "the right thing".) |
|
762 |
Functions that this affects include contains(), indexOf(), |
|
763 |
lastIndexOf(), operator<(), operator<=(), operator>(), |
|
764 |
operator>=(), toLower() and toUpper(). |
|
765 |
||
766 |
This issue does not apply to QStrings since they represent |
|
767 |
characters using Unicode. |
|
768 |
||
769 |
\sa QString, QBitArray |
|
770 |
*/ |
|
771 |
||
772 |
/*! \fn QByteArray::iterator QByteArray::begin() |
|
773 |
||
774 |
\internal |
|
775 |
*/ |
|
776 |
||
777 |
/*! \fn QByteArray::const_iterator QByteArray::begin() const |
|
778 |
||
779 |
\internal |
|
780 |
*/ |
|
781 |
||
782 |
/*! \fn QByteArray::const_iterator QByteArray::constBegin() const |
|
783 |
||
784 |
\internal |
|
785 |
*/ |
|
786 |
||
787 |
/*! \fn QByteArray::iterator QByteArray::end() |
|
788 |
||
789 |
\internal |
|
790 |
*/ |
|
791 |
||
792 |
/*! \fn QByteArray::const_iterator QByteArray::end() const |
|
793 |
||
794 |
\internal |
|
795 |
*/ |
|
796 |
||
797 |
/*! \fn QByteArray::const_iterator QByteArray::constEnd() const |
|
798 |
||
799 |
\internal |
|
800 |
*/ |
|
801 |
||
802 |
/*! \fn void QByteArray::push_back(const QByteArray &other) |
|
803 |
||
804 |
This function is provided for STL compatibility. It is equivalent |
|
805 |
to append(\a other). |
|
806 |
*/ |
|
807 |
||
808 |
/*! \fn void QByteArray::push_back(const char *str) |
|
809 |
||
810 |
\overload |
|
811 |
||
812 |
Same as append(\a str). |
|
813 |
*/ |
|
814 |
||
815 |
/*! \fn void QByteArray::push_back(char ch) |
|
816 |
||
817 |
\overload |
|
818 |
||
819 |
Same as append(\a ch). |
|
820 |
*/ |
|
821 |
||
822 |
/*! \fn void QByteArray::push_front(const QByteArray &other) |
|
823 |
||
824 |
This function is provided for STL compatibility. It is equivalent |
|
825 |
to prepend(\a other). |
|
826 |
*/ |
|
827 |
||
828 |
/*! \fn void QByteArray::push_front(const char *str) |
|
829 |
||
830 |
\overload |
|
831 |
||
832 |
Same as prepend(\a str). |
|
833 |
*/ |
|
834 |
||
835 |
/*! \fn void QByteArray::push_front(char ch) |
|
836 |
||
837 |
\overload |
|
838 |
||
839 |
Same as prepend(\a ch). |
|
840 |
*/ |
|
841 |
||
842 |
/*! \fn QByteArray::QByteArray(const QByteArray &other) |
|
843 |
||
844 |
Constructs a copy of \a other. |
|
845 |
||
846 |
This operation takes \l{constant time}, because QByteArray is |
|
847 |
\l{implicitly shared}. This makes returning a QByteArray from a |
|
848 |
function very fast. If a shared instance is modified, it will be |
|
849 |
copied (copy-on-write), and that takes \l{linear time}. |
|
850 |
||
851 |
\sa operator=() |
|
852 |
*/ |
|
853 |
||
854 |
/*! \fn QByteArray::~QByteArray() |
|
855 |
Destroys the byte array. |
|
856 |
*/ |
|
857 |
||
858 |
/*! |
|
859 |
Assigns \a other to this byte array and returns a reference to |
|
860 |
this byte array. |
|
861 |
*/ |
|
862 |
QByteArray &QByteArray::operator=(const QByteArray & other) |
|
863 |
{ |
|
864 |
other.d->ref.ref(); |
|
865 |
if (!d->ref.deref()) |
|
866 |
qFree(d); |
|
867 |
d = other.d; |
|
868 |
return *this; |
|
869 |
} |
|
870 |
||
871 |
||
872 |
/*! |
|
873 |
\overload |
|
874 |
||
875 |
Assigns \a str to this byte array. |
|
876 |
*/ |
|
877 |
||
878 |
QByteArray &QByteArray::operator=(const char *str) |
|
879 |
{ |
|
880 |
Data *x; |
|
881 |
if (!str) { |
|
882 |
x = &shared_null; |
|
883 |
} else if (!*str) { |
|
884 |
x = &shared_empty; |
|
885 |
} else { |
|
886 |
int len = qstrlen(str); |
|
887 |
if (d->ref != 1 || len > d->alloc || (len < d->size && len < d->alloc >> 1)) |
|
888 |
realloc(len); |
|
889 |
x = d; |
|
890 |
memcpy(x->data, str, len + 1); // include null terminator |
|
891 |
x->size = len; |
|
892 |
} |
|
893 |
x->ref.ref(); |
|
894 |
if (!d->ref.deref()) |
|
895 |
qFree(d); |
|
896 |
d = x; |
|
897 |
return *this; |
|
898 |
} |
|
899 |
||
900 |
/*! \fn int QByteArray::size() const |
|
901 |
||
902 |
Returns the number of bytes in this byte array. |
|
903 |
||
904 |
The last byte in the byte array is at position size() - 1. In |
|
905 |
addition, QByteArray ensures that the byte at position size() is |
|
906 |
always '\\0', so that you can use the return value of data() and |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
907 |
constData() as arguments to functions that expect '\\0'-terminated |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
908 |
strings. |
0 | 909 |
|
910 |
Example: |
|
911 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 6 |
|
912 |
||
913 |
\sa isEmpty(), resize() |
|
914 |
*/ |
|
915 |
||
916 |
/*! \fn bool QByteArray::isEmpty() const |
|
917 |
||
918 |
Returns true if the byte array has size 0; otherwise returns false. |
|
919 |
||
920 |
Example: |
|
921 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 7 |
|
922 |
||
923 |
\sa size() |
|
924 |
*/ |
|
925 |
||
926 |
/*! \fn int QByteArray::capacity() const |
|
927 |
||
928 |
Returns the maximum number of bytes that can be stored in the |
|
929 |
byte array without forcing a reallocation. |
|
930 |
||
931 |
The sole purpose of this function is to provide a means of fine |
|
932 |
tuning QByteArray's memory usage. In general, you will rarely |
|
933 |
ever need to call this function. If you want to know how many |
|
934 |
bytes are in the byte array, call size(). |
|
935 |
||
936 |
\sa reserve(), squeeze() |
|
937 |
*/ |
|
938 |
||
939 |
/*! \fn void QByteArray::reserve(int size) |
|
940 |
||
941 |
Attempts to allocate memory for at least \a size bytes. If you |
|
942 |
know in advance how large the byte array will be, you can call |
|
943 |
this function, and if you call resize() often you are likely to |
|
944 |
get better performance. If \a size is an underestimate, the worst |
|
945 |
that will happen is that the QByteArray will be a bit slower. |
|
946 |
||
947 |
The sole purpose of this function is to provide a means of fine |
|
948 |
tuning QByteArray's memory usage. In general, you will rarely |
|
949 |
ever need to call this function. If you want to change the size |
|
950 |
of the byte array, call resize(). |
|
951 |
||
952 |
\sa squeeze(), capacity() |
|
953 |
*/ |
|
954 |
||
955 |
/*! \fn void QByteArray::squeeze() |
|
956 |
||
957 |
Releases any memory not required to store the array's data. |
|
958 |
||
959 |
The sole purpose of this function is to provide a means of fine |
|
960 |
tuning QByteArray's memory usage. In general, you will rarely |
|
961 |
ever need to call this function. |
|
962 |
||
963 |
\sa reserve(), capacity() |
|
964 |
*/ |
|
965 |
||
966 |
/*! \fn QByteArray::operator const char *() const |
|
967 |
\fn QByteArray::operator const void *() const |
|
968 |
||
969 |
Returns a pointer to the data stored in the byte array. The |
|
970 |
pointer can be used to access the bytes that compose the array. |
|
971 |
The data is '\\0'-terminated. The pointer remains valid as long |
|
972 |
as the array isn't reallocated or destroyed. |
|
973 |
||
974 |
This operator is mostly useful to pass a byte array to a function |
|
975 |
that accepts a \c{const char *}. |
|
976 |
||
977 |
You can disable this operator by defining \c |
|
978 |
QT_NO_CAST_FROM_BYTEARRAY when you compile your applications. |
|
979 |
||
980 |
Note: A QByteArray can store any byte values including '\\0's, |
|
981 |
but most functions that take \c{char *} arguments assume that the |
|
982 |
data ends at the first '\\0' they encounter. |
|
983 |
||
984 |
\sa constData() |
|
985 |
*/ |
|
986 |
||
987 |
/*! |
|
988 |
\macro QT_NO_CAST_FROM_BYTEARRAY |
|
989 |
\relates QByteArray |
|
990 |
||
991 |
Disables automatic conversions from QByteArray to |
|
992 |
const char * or const void *. |
|
993 |
||
994 |
\sa QT_NO_CAST_TO_ASCII, QT_NO_CAST_FROM_ASCII |
|
995 |
*/ |
|
996 |
||
997 |
/*! \fn char *QByteArray::data() |
|
998 |
||
999 |
Returns a pointer to the data stored in the byte array. The |
|
1000 |
pointer can be used to access and modify the bytes that compose |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1001 |
the array. The data is '\\0'-terminated, i.e. the number of |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1002 |
bytes in the returned character string is size() + 1 for the |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1003 |
'\\0' terminator. |
0 | 1004 |
|
1005 |
Example: |
|
1006 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 8 |
|
1007 |
||
1008 |
The pointer remains valid as long as the byte array isn't |
|
1009 |
reallocated or destroyed. For read-only access, constData() is |
|
1010 |
faster because it never causes a \l{deep copy} to occur. |
|
1011 |
||
1012 |
This function is mostly useful to pass a byte array to a function |
|
1013 |
that accepts a \c{const char *}. |
|
1014 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1015 |
The following example makes a copy of the char* returned by |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1016 |
data(), but it will corrupt the heap and cause a crash because it |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1017 |
does not allocate a byte for the '\\0' at the end: |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1018 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1019 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 46 |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1020 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1021 |
This one allocates the correct amount of space: |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1022 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1023 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 47 |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1024 |
|
0 | 1025 |
Note: A QByteArray can store any byte values including '\\0's, |
1026 |
but most functions that take \c{char *} arguments assume that the |
|
1027 |
data ends at the first '\\0' they encounter. |
|
1028 |
||
1029 |
\sa constData(), operator[]() |
|
1030 |
*/ |
|
1031 |
||
1032 |
/*! \fn const char *QByteArray::data() const |
|
1033 |
||
1034 |
\overload |
|
1035 |
*/ |
|
1036 |
||
1037 |
/*! \fn const char *QByteArray::constData() const |
|
1038 |
||
1039 |
Returns a pointer to the data stored in the byte array. The |
|
1040 |
pointer can be used to access the bytes that compose the array. |
|
1041 |
The data is '\\0'-terminated. The pointer remains valid as long |
|
1042 |
as the byte array isn't reallocated or destroyed. |
|
1043 |
||
1044 |
This function is mostly useful to pass a byte array to a function |
|
1045 |
that accepts a \c{const char *}. |
|
1046 |
||
1047 |
Note: A QByteArray can store any byte values including '\\0's, |
|
1048 |
but most functions that take \c{char *} arguments assume that the |
|
1049 |
data ends at the first '\\0' they encounter. |
|
1050 |
||
1051 |
\sa data(), operator[]() |
|
1052 |
*/ |
|
1053 |
||
1054 |
/*! \fn void QByteArray::detach() |
|
1055 |
||
1056 |
\internal |
|
1057 |
*/ |
|
1058 |
||
1059 |
/*! \fn bool QByteArray::isDetached() const |
|
1060 |
||
1061 |
\internal |
|
1062 |
*/ |
|
1063 |
||
1064 |
/*! \fn char QByteArray::at(int i) const |
|
1065 |
||
1066 |
Returns the character at index position \a i in the byte array. |
|
1067 |
||
1068 |
\a i must be a valid index position in the byte array (i.e., 0 <= |
|
1069 |
\a i < size()). |
|
1070 |
||
1071 |
\sa operator[]() |
|
1072 |
*/ |
|
1073 |
||
1074 |
/*! \fn QByteRef QByteArray::operator[](int i) |
|
1075 |
||
1076 |
Returns the byte at index position \a i as a modifiable reference. |
|
1077 |
||
1078 |
If an assignment is made beyond the end of the byte array, the |
|
1079 |
array is extended with resize() before the assignment takes |
|
1080 |
place. |
|
1081 |
||
1082 |
Example: |
|
1083 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 9 |
|
1084 |
||
1085 |
The return value is of type QByteRef, a helper class for |
|
1086 |
QByteArray. When you get an object of type QByteRef, you can use |
|
1087 |
it as if it were a char &. If you assign to it, the assignment |
|
1088 |
will apply to the character in the QByteArray from which you got |
|
1089 |
the reference. |
|
1090 |
||
1091 |
\sa at() |
|
1092 |
*/ |
|
1093 |
||
1094 |
/*! \fn char QByteArray::operator[](int i) const |
|
1095 |
||
1096 |
\overload |
|
1097 |
||
1098 |
Same as at(\a i). |
|
1099 |
*/ |
|
1100 |
||
1101 |
/*! \fn QByteRef QByteArray::operator[](uint i) |
|
1102 |
||
1103 |
\overload |
|
1104 |
*/ |
|
1105 |
||
1106 |
/*! \fn char QByteArray::operator[](uint i) const |
|
1107 |
||
1108 |
\overload |
|
1109 |
*/ |
|
1110 |
||
1111 |
/*! \fn QBool QByteArray::contains(const QByteArray &ba) const |
|
1112 |
||
1113 |
Returns true if the byte array contains an occurrence of the byte |
|
1114 |
array \a ba; otherwise returns false. |
|
1115 |
||
1116 |
\sa indexOf(), count() |
|
1117 |
*/ |
|
1118 |
||
1119 |
/*! \fn QBool QByteArray::contains(const char *str) const |
|
1120 |
||
1121 |
\overload |
|
1122 |
||
1123 |
Returns true if the byte array contains the string \a str; |
|
1124 |
otherwise returns false. |
|
1125 |
*/ |
|
1126 |
||
1127 |
/*! \fn QBool QByteArray::contains(char ch) const |
|
1128 |
||
1129 |
\overload |
|
1130 |
||
1131 |
Returns true if the byte array contains the character \a ch; |
|
1132 |
otherwise returns false. |
|
1133 |
*/ |
|
1134 |
||
1135 |
/*! |
|
1136 |
||
1137 |
Truncates the byte array at index position \a pos. |
|
1138 |
||
1139 |
If \a pos is beyond the end of the array, nothing happens. |
|
1140 |
||
1141 |
Example: |
|
1142 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 10 |
|
1143 |
||
1144 |
\sa chop(), resize(), left() |
|
1145 |
*/ |
|
1146 |
void QByteArray::truncate(int pos) |
|
1147 |
{ |
|
1148 |
if (pos < d->size) |
|
1149 |
resize(pos); |
|
1150 |
} |
|
1151 |
||
1152 |
/*! |
|
1153 |
||
1154 |
Removes \a n bytes from the end of the byte array. |
|
1155 |
||
1156 |
If \a n is greater than size(), the result is an empty byte |
|
1157 |
array. |
|
1158 |
||
1159 |
Example: |
|
1160 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 11 |
|
1161 |
||
1162 |
\sa truncate(), resize(), left() |
|
1163 |
*/ |
|
1164 |
||
1165 |
void QByteArray::chop(int n) |
|
1166 |
{ |
|
1167 |
if (n > 0) |
|
1168 |
resize(d->size - n); |
|
1169 |
} |
|
1170 |
||
1171 |
||
1172 |
/*! \fn QByteArray &QByteArray::operator+=(const QByteArray &ba) |
|
1173 |
||
1174 |
Appends the byte array \a ba onto the end of this byte array and |
|
1175 |
returns a reference to this byte array. |
|
1176 |
||
1177 |
Example: |
|
1178 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 12 |
|
1179 |
||
1180 |
This operation is typically very fast (\l{constant time}), |
|
1181 |
because QByteArray preallocates extra space at the end of the |
|
1182 |
character data so it can grow without reallocating the entire |
|
1183 |
data each time. |
|
1184 |
||
1185 |
\sa append(), prepend() |
|
1186 |
*/ |
|
1187 |
||
1188 |
/*! \fn QByteArray &QByteArray::operator+=(const QString &str) |
|
1189 |
||
1190 |
\overload |
|
1191 |
||
1192 |
Appends the string \a str onto the end of this byte array and |
|
1193 |
returns a reference to this byte array. The Unicode data is |
|
1194 |
converted into 8-bit characters using QString::toAscii(). |
|
1195 |
||
1196 |
If the QString contains non-ASCII Unicode characters, using this |
|
1197 |
operator can lead to loss of information. You can disable this |
|
1198 |
operator by defining \c QT_NO_CAST_TO_ASCII when you compile your |
|
1199 |
applications. You then need to call QString::toAscii() (or |
|
1200 |
QString::toLatin1() or QString::toUtf8() or QString::toLocal8Bit()) |
|
1201 |
explicitly if you want to convert the data to \c{const char *}. |
|
1202 |
*/ |
|
1203 |
||
1204 |
/*! \fn QByteArray &QByteArray::operator+=(const char *str) |
|
1205 |
||
1206 |
\overload |
|
1207 |
||
1208 |
Appends the string \a str onto the end of this byte array and |
|
1209 |
returns a reference to this byte array. |
|
1210 |
*/ |
|
1211 |
||
1212 |
/*! \fn QByteArray &QByteArray::operator+=(char ch) |
|
1213 |
||
1214 |
\overload |
|
1215 |
||
1216 |
Appends the character \a ch onto the end of this byte array and |
|
1217 |
returns a reference to this byte array. |
|
1218 |
*/ |
|
1219 |
||
1220 |
/*! \fn int QByteArray::length() const |
|
1221 |
||
1222 |
Same as size(). |
|
1223 |
*/ |
|
1224 |
||
1225 |
/*! \fn bool QByteArray::isNull() const |
|
1226 |
||
1227 |
Returns true if this byte array is null; otherwise returns false. |
|
1228 |
||
1229 |
Example: |
|
1230 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 13 |
|
1231 |
||
1232 |
Qt makes a distinction between null byte arrays and empty byte |
|
1233 |
arrays for historical reasons. For most applications, what |
|
1234 |
matters is whether or not a byte array contains any data, |
|
1235 |
and this can be determined using isEmpty(). |
|
1236 |
||
1237 |
\sa isEmpty() |
|
1238 |
*/ |
|
1239 |
||
1240 |
/*! \fn QByteArray::QByteArray() |
|
1241 |
||
1242 |
Constructs an empty byte array. |
|
1243 |
||
1244 |
\sa isEmpty() |
|
1245 |
*/ |
|
1246 |
||
1247 |
/*! \fn QByteArray::QByteArray(const char *str) |
|
1248 |
||
1249 |
Constructs a byte array initialized with the string \a str. |
|
1250 |
||
1251 |
QByteArray makes a deep copy of the string data. |
|
1252 |
*/ |
|
1253 |
||
1254 |
QByteArray::QByteArray(const char *str) |
|
1255 |
{ |
|
1256 |
if (!str) { |
|
1257 |
d = &shared_null; |
|
1258 |
} else if (!*str) { |
|
1259 |
d = &shared_empty; |
|
1260 |
} else { |
|
1261 |
int len = qstrlen(str); |
|
1262 |
d = static_cast<Data *>(qMalloc(sizeof(Data)+len)); |
|
1263 |
Q_CHECK_PTR(d); |
|
1264 |
d->ref = 0;; |
|
1265 |
d->alloc = d->size = len; |
|
1266 |
d->data = d->array; |
|
1267 |
memcpy(d->array, str, len+1); // include null terminator |
|
1268 |
} |
|
1269 |
d->ref.ref(); |
|
1270 |
} |
|
1271 |
||
1272 |
/*! |
|
1273 |
Constructs a byte array containing the first \a size bytes of |
|
1274 |
array \a data. |
|
1275 |
||
1276 |
If \a data is 0, a null byte array is constructed. |
|
1277 |
||
1278 |
QByteArray makes a deep copy of the string data. |
|
1279 |
||
1280 |
\sa fromRawData() |
|
1281 |
*/ |
|
1282 |
||
1283 |
QByteArray::QByteArray(const char *data, int size) |
|
1284 |
{ |
|
1285 |
if (!data) { |
|
1286 |
d = &shared_null; |
|
1287 |
} else if (size <= 0) { |
|
1288 |
d = &shared_empty; |
|
1289 |
} else { |
|
1290 |
d = static_cast<Data *>(qMalloc(sizeof(Data) + size)); |
|
1291 |
Q_CHECK_PTR(d); |
|
1292 |
d->ref = 0; |
|
1293 |
d->alloc = d->size = size; |
|
1294 |
d->data = d->array; |
|
1295 |
memcpy(d->array, data, size); |
|
1296 |
d->array[size] = '\0'; |
|
1297 |
} |
|
1298 |
d->ref.ref(); |
|
1299 |
} |
|
1300 |
||
1301 |
/*! |
|
1302 |
Constructs a byte array of size \a size with every byte set to |
|
1303 |
character \a ch. |
|
1304 |
||
1305 |
\sa fill() |
|
1306 |
*/ |
|
1307 |
||
1308 |
QByteArray::QByteArray(int size, char ch) |
|
1309 |
{ |
|
1310 |
if (size <= 0) { |
|
1311 |
d = &shared_null; |
|
1312 |
} else { |
|
1313 |
d = static_cast<Data *>(qMalloc(sizeof(Data)+size)); |
|
1314 |
Q_CHECK_PTR(d); |
|
1315 |
d->ref = 0; |
|
1316 |
d->alloc = d->size = size; |
|
1317 |
d->data = d->array; |
|
1318 |
d->array[size] = '\0'; |
|
1319 |
memset(d->array, ch, size); |
|
1320 |
} |
|
1321 |
d->ref.ref(); |
|
1322 |
} |
|
1323 |
||
1324 |
/*! |
|
1325 |
\internal |
|
1326 |
||
1327 |
Constructs a byte array of size \a size with uninitialized contents. |
|
1328 |
*/ |
|
1329 |
||
1330 |
QByteArray::QByteArray(int size, Qt::Initialization) |
|
1331 |
{ |
|
1332 |
d = static_cast<Data *>(qMalloc(sizeof(Data)+size)); |
|
1333 |
Q_CHECK_PTR(d); |
|
1334 |
d->ref = 1; |
|
1335 |
d->alloc = d->size = size; |
|
1336 |
d->data = d->array; |
|
1337 |
d->array[size] = '\0'; |
|
1338 |
} |
|
1339 |
||
1340 |
/*! |
|
1341 |
Sets the size of the byte array to \a size bytes. |
|
1342 |
||
1343 |
If \a size is greater than the current size, the byte array is |
|
1344 |
extended to make it \a size bytes with the extra bytes added to |
|
1345 |
the end. The new bytes are uninitialized. |
|
1346 |
||
1347 |
If \a size is less than the current size, bytes are removed from |
|
1348 |
the end. |
|
1349 |
||
1350 |
\sa size() |
|
1351 |
*/ |
|
1352 |
||
1353 |
void QByteArray::resize(int size) |
|
1354 |
{ |
|
1355 |
if (size <= 0) { |
|
1356 |
Data *x = &shared_empty; |
|
1357 |
x->ref.ref(); |
|
1358 |
if (!d->ref.deref()) |
|
1359 |
qFree(d); |
|
1360 |
d = x; |
|
1361 |
} else if (d == &shared_null) { |
|
1362 |
// |
|
1363 |
// Optimize the idiom: |
|
1364 |
// QByteArray a; |
|
1365 |
// a.resize(sz); |
|
1366 |
// ... |
|
1367 |
// which is used in place of the Qt 3 idiom: |
|
1368 |
// QByteArray a(sz); |
|
1369 |
// |
|
1370 |
Data *x = static_cast<Data *>(qMalloc(sizeof(Data)+size)); |
|
1371 |
Q_CHECK_PTR(x); |
|
1372 |
x->ref = 1; |
|
1373 |
x->alloc = x->size = size; |
|
1374 |
x->data = x->array; |
|
1375 |
x->array[size] = '\0'; |
|
1376 |
(void) d->ref.deref(); // cannot be 0, x points to shared_null |
|
1377 |
d = x; |
|
1378 |
} else { |
|
1379 |
if (d->ref != 1 || size > d->alloc || (size < d->size && size < d->alloc >> 1)) |
|
1380 |
realloc(qAllocMore(size, sizeof(Data))); |
|
1381 |
if (d->alloc >= size) { |
|
1382 |
d->size = size; |
|
1383 |
if (d->data == d->array) { |
|
1384 |
d->array[size] = '\0'; |
|
1385 |
} |
|
1386 |
} |
|
1387 |
} |
|
1388 |
} |
|
1389 |
||
1390 |
/*! |
|
1391 |
Sets every byte in the byte array to character \a ch. If \a size |
|
1392 |
is different from -1 (the default), the byte array is resized to |
|
1393 |
size \a size beforehand. |
|
1394 |
||
1395 |
Example: |
|
1396 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 14 |
|
1397 |
||
1398 |
\sa resize() |
|
1399 |
*/ |
|
1400 |
||
1401 |
QByteArray &QByteArray::fill(char ch, int size) |
|
1402 |
{ |
|
1403 |
resize(size < 0 ? d->size : size); |
|
1404 |
if (d->size) |
|
1405 |
memset(d->data, ch, d->size); |
|
1406 |
return *this; |
|
1407 |
} |
|
1408 |
||
1409 |
void QByteArray::realloc(int alloc) |
|
1410 |
{ |
|
1411 |
if (d->ref != 1 || d->data != d->array) { |
|
1412 |
Data *x = static_cast<Data *>(qMalloc(sizeof(Data) + alloc)); |
|
1413 |
Q_CHECK_PTR(x); |
|
1414 |
x->size = qMin(alloc, d->size); |
|
1415 |
::memcpy(x->array, d->data, x->size); |
|
1416 |
x->array[x->size] = '\0'; |
|
1417 |
x->ref = 1; |
|
1418 |
x->alloc = alloc; |
|
1419 |
x->data = x->array; |
|
1420 |
if (!d->ref.deref()) |
|
1421 |
qFree(d); |
|
1422 |
d = x; |
|
1423 |
} else { |
|
1424 |
Data *x = static_cast<Data *>(qRealloc(d, sizeof(Data) + alloc)); |
|
1425 |
Q_CHECK_PTR(x); |
|
1426 |
x->alloc = alloc; |
|
1427 |
x->data = x->array; |
|
1428 |
d = x; |
|
1429 |
} |
|
1430 |
} |
|
1431 |
||
1432 |
void QByteArray::expand(int i) |
|
1433 |
{ |
|
1434 |
resize(qMax(i + 1, d->size)); |
|
1435 |
} |
|
1436 |
||
1437 |
/*! |
|
1438 |
\internal |
|
1439 |
Return a QByteArray that is sure to be NUL-terminated. |
|
1440 |
||
1441 |
By default, all QByteArray have an extra NUL at the end, |
|
1442 |
guaranteeing that assumption. However, if QByteArray::fromRawData |
|
1443 |
is used, then the NUL is there only if the user put it there. We |
|
1444 |
can't be sure. |
|
1445 |
*/ |
|
1446 |
QByteArray QByteArray::nulTerminated() const |
|
1447 |
{ |
|
1448 |
// is this fromRawData? |
|
1449 |
if (d->data == d->array) |
|
1450 |
return *this; // no, then we're sure we're zero terminated |
|
1451 |
||
1452 |
QByteArray copy(*this); |
|
1453 |
copy.detach(); |
|
1454 |
return copy; |
|
1455 |
} |
|
1456 |
||
1457 |
/*! |
|
1458 |
Prepends the byte array \a ba to this byte array and returns a |
|
1459 |
reference to this byte array. |
|
1460 |
||
1461 |
Example: |
|
1462 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 15 |
|
1463 |
||
1464 |
This is the same as insert(0, \a ba). |
|
1465 |
||
1466 |
Note: QByteArray is an \l{implicitly shared} class. Consequently, |
|
1467 |
if \e this is an empty QByteArray, then \e this will just share |
|
1468 |
the data held in \a ba. In this case, no copying of data is done. |
|
1469 |
||
1470 |
\sa append(), insert() |
|
1471 |
*/ |
|
1472 |
||
1473 |
QByteArray &QByteArray::prepend(const QByteArray &ba) |
|
1474 |
{ |
|
1475 |
if ((d == &shared_null || d == &shared_empty) && !IS_RAW_DATA(ba.d)) { |
|
1476 |
*this = ba; |
|
1477 |
} else if (ba.d != &shared_null) { |
|
1478 |
QByteArray tmp = *this; |
|
1479 |
*this = ba; |
|
1480 |
append(tmp); |
|
1481 |
} |
|
1482 |
return *this; |
|
1483 |
} |
|
1484 |
||
1485 |
/*! |
|
1486 |
\overload |
|
1487 |
||
1488 |
Prepends the string \a str to this byte array. |
|
1489 |
*/ |
|
1490 |
||
1491 |
QByteArray &QByteArray::prepend(const char *str) |
|
1492 |
{ |
|
1493 |
return prepend(str, qstrlen(str)); |
|
1494 |
} |
|
1495 |
||
1496 |
/*! |
|
1497 |
\overload |
|
1498 |
\since 4.6 |
|
1499 |
||
1500 |
Prepends \a len bytes of the string \a str to this byte array. |
|
1501 |
*/ |
|
1502 |
||
1503 |
QByteArray &QByteArray::prepend(const char *str, int len) |
|
1504 |
{ |
|
1505 |
if (str) { |
|
1506 |
if (d->ref != 1 || d->size + len > d->alloc) |
|
1507 |
realloc(qAllocMore(d->size + len, sizeof(Data))); |
|
1508 |
memmove(d->data+len, d->data, d->size); |
|
1509 |
memcpy(d->data, str, len); |
|
1510 |
d->size += len; |
|
1511 |
d->data[d->size] = '\0'; |
|
1512 |
} |
|
1513 |
return *this; |
|
1514 |
} |
|
1515 |
||
1516 |
/*! |
|
1517 |
\overload |
|
1518 |
||
1519 |
Prepends the character \a ch to this byte array. |
|
1520 |
*/ |
|
1521 |
||
1522 |
QByteArray &QByteArray::prepend(char ch) |
|
1523 |
{ |
|
1524 |
if (d->ref != 1 || d->size + 1 > d->alloc) |
|
1525 |
realloc(qAllocMore(d->size + 1, sizeof(Data))); |
|
1526 |
memmove(d->data+1, d->data, d->size); |
|
1527 |
d->data[0] = ch; |
|
1528 |
++d->size; |
|
1529 |
d->data[d->size] = '\0'; |
|
1530 |
return *this; |
|
1531 |
} |
|
1532 |
||
1533 |
/*! |
|
1534 |
Appends the byte array \a ba onto the end of this byte array. |
|
1535 |
||
1536 |
Example: |
|
1537 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 16 |
|
1538 |
||
1539 |
This is the same as insert(size(), \a ba). |
|
1540 |
||
1541 |
This operation is typically very fast (\l{constant time}), |
|
1542 |
because QByteArray preallocates extra space at the end of the |
|
1543 |
character data so it can grow without reallocating the entire |
|
1544 |
data each time. |
|
1545 |
||
1546 |
Note: QByteArray is an \l{implicitly shared} class. Consequently, |
|
1547 |
if \e this is an empty QByteArray, then \e this will just share |
|
1548 |
the data held in \a ba. In this case, no copying of data is done. |
|
1549 |
||
1550 |
\sa operator+=(), prepend(), insert() |
|
1551 |
*/ |
|
1552 |
||
1553 |
QByteArray &QByteArray::append(const QByteArray &ba) |
|
1554 |
{ |
|
1555 |
if ((d == &shared_null || d == &shared_empty) && !IS_RAW_DATA(ba.d)) { |
|
1556 |
*this = ba; |
|
1557 |
} else if (ba.d != &shared_null) { |
|
1558 |
if (d->ref != 1 || d->size + ba.d->size > d->alloc) |
|
1559 |
realloc(qAllocMore(d->size + ba.d->size, sizeof(Data))); |
|
1560 |
memcpy(d->data + d->size, ba.d->data, ba.d->size); |
|
1561 |
d->size += ba.d->size; |
|
1562 |
d->data[d->size] = '\0'; |
|
1563 |
} |
|
1564 |
return *this; |
|
1565 |
} |
|
1566 |
||
1567 |
/*! \fn QByteArray &QByteArray::append(const QString &str) |
|
1568 |
||
1569 |
\overload |
|
1570 |
||
1571 |
Appends the string \a str to this byte array. The Unicode data is |
|
1572 |
converted into 8-bit characters using QString::toAscii(). |
|
1573 |
||
1574 |
If the QString contains non-ASCII Unicode characters, using this |
|
1575 |
function can lead to loss of information. You can disable this |
|
1576 |
function by defining \c QT_NO_CAST_TO_ASCII when you compile your |
|
1577 |
applications. You then need to call QString::toAscii() (or |
|
1578 |
QString::toLatin1() or QString::toUtf8() or QString::toLocal8Bit()) |
|
1579 |
explicitly if you want to convert the data to \c{const char *}. |
|
1580 |
*/ |
|
1581 |
||
1582 |
/*! |
|
1583 |
\overload |
|
1584 |
||
1585 |
Appends the string \a str to this byte array. |
|
1586 |
*/ |
|
1587 |
||
1588 |
QByteArray& QByteArray::append(const char *str) |
|
1589 |
{ |
|
1590 |
if (str) { |
|
1591 |
int len = qstrlen(str); |
|
1592 |
if (d->ref != 1 || d->size + len > d->alloc) |
|
1593 |
realloc(qAllocMore(d->size + len, sizeof(Data))); |
|
1594 |
memcpy(d->data + d->size, str, len + 1); // include null terminator |
|
1595 |
d->size += len; |
|
1596 |
} |
|
1597 |
return *this; |
|
1598 |
} |
|
1599 |
||
1600 |
/*! |
|
1601 |
\overload append() |
|
1602 |
||
1603 |
Appends the first \a len characters of the string \a str to this byte |
|
1604 |
array and returns a reference to this byte array. |
|
1605 |
||
1606 |
If \a len is negative, the length of the string will be determined |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1607 |
automatically using qstrlen(). If \a len is zero or \a str is |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1608 |
null, nothing is appended to the byte array. Ensure that \a len is |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1609 |
\e not longer than \a str. |
0 | 1610 |
*/ |
1611 |
||
1612 |
QByteArray &QByteArray::append(const char *str, int len) |
|
1613 |
{ |
|
1614 |
if (len < 0) |
|
1615 |
len = qstrlen(str); |
|
1616 |
if (str && len) { |
|
1617 |
if (d->ref != 1 || d->size + len > d->alloc) |
|
1618 |
realloc(qAllocMore(d->size + len, sizeof(Data))); |
|
1619 |
memcpy(d->data + d->size, str, len); // include null terminator |
|
1620 |
d->size += len; |
|
1621 |
d->data[d->size] = '\0'; |
|
1622 |
} |
|
1623 |
return *this; |
|
1624 |
} |
|
1625 |
||
1626 |
/*! |
|
1627 |
\overload |
|
1628 |
||
1629 |
Appends the character \a ch to this byte array. |
|
1630 |
*/ |
|
1631 |
||
1632 |
QByteArray& QByteArray::append(char ch) |
|
1633 |
{ |
|
1634 |
if (d->ref != 1 || d->size + 1 > d->alloc) |
|
1635 |
realloc(qAllocMore(d->size + 1, sizeof(Data))); |
|
1636 |
d->data[d->size++] = ch; |
|
1637 |
d->data[d->size] = '\0'; |
|
1638 |
return *this; |
|
1639 |
} |
|
1640 |
||
1641 |
/*! |
|
1642 |
\internal |
|
1643 |
Inserts \a len bytes from the array \a arr at position \a pos and returns a |
|
1644 |
reference the modified byte array. |
|
1645 |
*/ |
|
1646 |
static inline QByteArray &qbytearray_insert(QByteArray *ba, |
|
1647 |
int pos, const char *arr, int len) |
|
1648 |
{ |
|
1649 |
Q_ASSERT(pos >= 0); |
|
1650 |
||
1651 |
if (pos < 0 || len <= 0 || arr == 0) |
|
1652 |
return *ba; |
|
1653 |
||
1654 |
int oldsize = ba->size(); |
|
1655 |
ba->resize(qMax(pos, oldsize) + len); |
|
1656 |
char *dst = ba->data(); |
|
1657 |
if (pos > oldsize) |
|
1658 |
::memset(dst + oldsize, 0x20, pos - oldsize); |
|
1659 |
else |
|
1660 |
::memmove(dst + pos + len, dst + pos, oldsize - pos); |
|
1661 |
memcpy(dst + pos, arr, len); |
|
1662 |
return *ba; |
|
1663 |
} |
|
1664 |
||
1665 |
/*! |
|
1666 |
Inserts the byte array \a ba at index position \a i and returns a |
|
1667 |
reference to this byte array. |
|
1668 |
||
1669 |
Example: |
|
1670 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 17 |
|
1671 |
||
1672 |
\sa append(), prepend(), replace(), remove() |
|
1673 |
*/ |
|
1674 |
||
1675 |
QByteArray &QByteArray::insert(int i, const QByteArray &ba) |
|
1676 |
{ |
|
1677 |
QByteArray copy(ba); |
|
1678 |
return qbytearray_insert(this, i, copy.d->data, copy.d->size); |
|
1679 |
} |
|
1680 |
||
1681 |
/*! |
|
1682 |
\fn QByteArray &QByteArray::insert(int i, const QString &str) |
|
1683 |
||
1684 |
\overload |
|
1685 |
||
1686 |
Inserts the string \a str at index position \a i in the byte |
|
1687 |
array. The Unicode data is converted into 8-bit characters using |
|
1688 |
QString::toAscii(). |
|
1689 |
||
1690 |
If \a i is greater than size(), the array is first extended using |
|
1691 |
resize(). |
|
1692 |
||
1693 |
If the QString contains non-ASCII Unicode characters, using this |
|
1694 |
function can lead to loss of information. You can disable this |
|
1695 |
function by defining \c QT_NO_CAST_TO_ASCII when you compile your |
|
1696 |
applications. You then need to call QString::toAscii() (or |
|
1697 |
QString::toLatin1() or QString::toUtf8() or QString::toLocal8Bit()) |
|
1698 |
explicitly if you want to convert the data to \c{const char *}. |
|
1699 |
*/ |
|
1700 |
||
1701 |
/*! |
|
1702 |
\overload |
|
1703 |
||
1704 |
Inserts the string \a str at position \a i in the byte array. |
|
1705 |
||
1706 |
If \a i is greater than size(), the array is first extended using |
|
1707 |
resize(). |
|
1708 |
*/ |
|
1709 |
||
1710 |
QByteArray &QByteArray::insert(int i, const char *str) |
|
1711 |
{ |
|
1712 |
return qbytearray_insert(this, i, str, qstrlen(str)); |
|
1713 |
} |
|
1714 |
||
1715 |
/*! |
|
1716 |
\overload |
|
1717 |
\since 4.6 |
|
1718 |
||
1719 |
Inserts \a len bytes of the string \a str at position |
|
1720 |
\a i in the byte array. |
|
1721 |
||
1722 |
If \a i is greater than size(), the array is first extended using |
|
1723 |
resize(). |
|
1724 |
*/ |
|
1725 |
||
1726 |
QByteArray &QByteArray::insert(int i, const char *str, int len) |
|
1727 |
{ |
|
1728 |
return qbytearray_insert(this, i, str, len); |
|
1729 |
} |
|
1730 |
||
1731 |
/*! |
|
1732 |
\overload |
|
1733 |
||
1734 |
Inserts character \a ch at index position \a i in the byte array. |
|
1735 |
If \a i is greater than size(), the array is first extended using |
|
1736 |
resize(). |
|
1737 |
*/ |
|
1738 |
||
1739 |
QByteArray &QByteArray::insert(int i, char ch) |
|
1740 |
{ |
|
1741 |
return qbytearray_insert(this, i, &ch, 1); |
|
1742 |
} |
|
1743 |
||
1744 |
/*! |
|
1745 |
Removes \a len bytes from the array, starting at index position \a |
|
1746 |
pos, and returns a reference to the array. |
|
1747 |
||
1748 |
If \a pos is out of range, nothing happens. If \a pos is valid, |
|
1749 |
but \a pos + \a len is larger than the size of the array, the |
|
1750 |
array is truncated at position \a pos. |
|
1751 |
||
1752 |
Example: |
|
1753 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 18 |
|
1754 |
||
1755 |
\sa insert(), replace() |
|
1756 |
*/ |
|
1757 |
||
1758 |
QByteArray &QByteArray::remove(int pos, int len) |
|
1759 |
{ |
|
1760 |
if (len <= 0 || pos >= d->size || pos < 0) |
|
1761 |
return *this; |
|
1762 |
detach(); |
|
1763 |
if (pos + len >= d->size) { |
|
1764 |
resize(pos); |
|
1765 |
} else { |
|
1766 |
memmove(d->data + pos, d->data + pos + len, d->size - pos - len); |
|
1767 |
resize(d->size - len); |
|
1768 |
} |
|
1769 |
return *this; |
|
1770 |
} |
|
1771 |
||
1772 |
/*! |
|
1773 |
Replaces \a len bytes from index position \a pos with the byte |
|
1774 |
array \a after, and returns a reference to this byte array. |
|
1775 |
||
1776 |
Example: |
|
1777 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 19 |
|
1778 |
||
1779 |
\sa insert(), remove() |
|
1780 |
*/ |
|
1781 |
||
1782 |
QByteArray &QByteArray::replace(int pos, int len, const QByteArray &after) |
|
1783 |
{ |
|
1784 |
if (len == after.d->size && (pos + len <= d->size)) { |
|
1785 |
detach(); |
|
1786 |
memmove(d->data + pos, after.d->data, len*sizeof(char)); |
|
1787 |
return *this; |
|
1788 |
} else { |
|
1789 |
QByteArray copy(after); |
|
1790 |
// ### optimise me |
|
1791 |
remove(pos, len); |
|
1792 |
return insert(pos, copy); |
|
1793 |
} |
|
1794 |
} |
|
1795 |
||
1796 |
/*! \fn QByteArray &QByteArray::replace(int pos, int len, const char *after) |
|
1797 |
||
1798 |
\overload |
|
1799 |
*/ |
|
1800 |
QByteArray &QByteArray::replace(int pos, int len, const char *after) |
|
1801 |
{ |
|
1802 |
int alen = qstrlen(after); |
|
1803 |
if (len == alen && (pos + len <= d->size)) { |
|
1804 |
detach(); |
|
1805 |
memcpy(d->data + pos, after, len*sizeof(char)); |
|
1806 |
return *this; |
|
1807 |
} else { |
|
1808 |
remove(pos, len); |
|
1809 |
return qbytearray_insert(this, pos, after, alen); |
|
1810 |
} |
|
1811 |
} |
|
1812 |
||
1813 |
// ### optimise all other replace method, by offering |
|
1814 |
// QByteArray::replace(const char *before, int blen, const char *after, int alen) |
|
1815 |
||
1816 |
/*! |
|
1817 |
\overload |
|
1818 |
||
1819 |
Replaces every occurrence of the byte array \a before with the |
|
1820 |
byte array \a after. |
|
1821 |
||
1822 |
Example: |
|
1823 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 20 |
|
1824 |
*/ |
|
1825 |
||
1826 |
QByteArray &QByteArray::replace(const QByteArray &before, const QByteArray &after) |
|
1827 |
{ |
|
1828 |
if (isNull() || before.d == after.d) |
|
1829 |
return *this; |
|
1830 |
||
1831 |
QByteArray aft = after; |
|
1832 |
if (after.d == d) |
|
1833 |
aft.detach(); |
|
1834 |
||
1835 |
return replace(before.constData(), before.size(), aft.constData(), aft.size()); |
|
1836 |
} |
|
1837 |
||
1838 |
/*! |
|
1839 |
\fn QByteArray &QByteArray::replace(const char *before, const QByteArray &after) |
|
1840 |
\overload |
|
1841 |
||
1842 |
Replaces every occurrence of the string \a before with the |
|
1843 |
byte array \a after. |
|
1844 |
*/ |
|
1845 |
||
1846 |
QByteArray &QByteArray::replace(const char *c, const QByteArray &after) |
|
1847 |
{ |
|
1848 |
QByteArray aft = after; |
|
1849 |
if (after.d == d) |
|
1850 |
aft.detach(); |
|
1851 |
||
1852 |
return replace(c, qstrlen(c), aft.constData(), aft.size()); |
|
1853 |
} |
|
1854 |
||
1855 |
/*! |
|
1856 |
\fn QByteArray &QByteArray::replace(const char *before, int bsize, const char *after, int asize) |
|
1857 |
\overload |
|
1858 |
||
1859 |
Replaces every occurrence of the string \a before with the string \a after. |
|
1860 |
Since the sizes of the strings are given by \a bsize and \a asize, they |
|
1861 |
may contain zero characters and do not need to be zero-terminated. |
|
1862 |
*/ |
|
1863 |
||
1864 |
QByteArray &QByteArray::replace(const char *before, int bsize, const char *after, int asize) |
|
1865 |
{ |
|
1866 |
if (isNull() || (before == after && bsize == asize)) |
|
1867 |
return *this; |
|
1868 |
||
1869 |
// protect against before or after being part of this |
|
1870 |
const char *a = after; |
|
1871 |
const char *b = before; |
|
1872 |
if (after >= d->data && after < d->data + d->size) { |
|
1873 |
char *copy = (char *)malloc(asize); |
|
1874 |
Q_CHECK_PTR(copy); |
|
1875 |
memcpy(copy, after, asize); |
|
1876 |
a = copy; |
|
1877 |
} |
|
1878 |
if (before >= d->data && before < d->data + d->size) { |
|
1879 |
char *copy = (char *)malloc(bsize); |
|
1880 |
Q_CHECK_PTR(copy); |
|
1881 |
memcpy(copy, before, bsize); |
|
1882 |
b = copy; |
|
1883 |
} |
|
1884 |
||
1885 |
QByteArrayMatcher matcher(before, bsize); |
|
1886 |
int index = 0; |
|
1887 |
int len = d->size; |
|
1888 |
char *d = data(); |
|
1889 |
||
1890 |
if (bsize == asize) { |
|
1891 |
if (bsize) { |
|
1892 |
while ((index = matcher.indexIn(*this, index)) != -1) { |
|
1893 |
memcpy(d + index, after, asize); |
|
1894 |
index += bsize; |
|
1895 |
} |
|
1896 |
} |
|
1897 |
} else if (asize < bsize) { |
|
1898 |
uint to = 0; |
|
1899 |
uint movestart = 0; |
|
1900 |
uint num = 0; |
|
1901 |
while ((index = matcher.indexIn(*this, index)) != -1) { |
|
1902 |
if (num) { |
|
1903 |
int msize = index - movestart; |
|
1904 |
if (msize > 0) { |
|
1905 |
memmove(d + to, d + movestart, msize); |
|
1906 |
to += msize; |
|
1907 |
} |
|
1908 |
} else { |
|
1909 |
to = index; |
|
1910 |
} |
|
1911 |
if (asize) { |
|
1912 |
memcpy(d + to, after, asize); |
|
1913 |
to += asize; |
|
1914 |
} |
|
1915 |
index += bsize; |
|
1916 |
movestart = index; |
|
1917 |
num++; |
|
1918 |
} |
|
1919 |
if (num) { |
|
1920 |
int msize = len - movestart; |
|
1921 |
if (msize > 0) |
|
1922 |
memmove(d + to, d + movestart, msize); |
|
1923 |
resize(len - num*(bsize-asize)); |
|
1924 |
} |
|
1925 |
} else { |
|
1926 |
// the most complex case. We don't want to lose performance by doing repeated |
|
1927 |
// copies and reallocs of the string. |
|
1928 |
while (index != -1) { |
|
1929 |
uint indices[4096]; |
|
1930 |
uint pos = 0; |
|
1931 |
while(pos < 4095) { |
|
1932 |
index = matcher.indexIn(*this, index); |
|
1933 |
if (index == -1) |
|
1934 |
break; |
|
1935 |
indices[pos++] = index; |
|
1936 |
index += bsize; |
|
1937 |
// avoid infinite loop |
|
1938 |
if (!bsize) |
|
1939 |
index++; |
|
1940 |
} |
|
1941 |
if (!pos) |
|
1942 |
break; |
|
1943 |
||
1944 |
// we have a table of replacement positions, use them for fast replacing |
|
1945 |
int adjust = pos*(asize-bsize); |
|
1946 |
// index has to be adjusted in case we get back into the loop above. |
|
1947 |
if (index != -1) |
|
1948 |
index += adjust; |
|
1949 |
int newlen = len + adjust; |
|
1950 |
int moveend = len; |
|
1951 |
if (newlen > len) { |
|
1952 |
resize(newlen); |
|
1953 |
len = newlen; |
|
1954 |
} |
|
1955 |
d = this->d->data; |
|
1956 |
||
1957 |
while(pos) { |
|
1958 |
pos--; |
|
1959 |
int movestart = indices[pos] + bsize; |
|
1960 |
int insertstart = indices[pos] + pos*(asize-bsize); |
|
1961 |
int moveto = insertstart + asize; |
|
1962 |
memmove(d + moveto, d + movestart, (moveend - movestart)); |
|
1963 |
if (asize) |
|
1964 |
memcpy(d + insertstart, after, asize); |
|
1965 |
moveend = movestart - bsize; |
|
1966 |
} |
|
1967 |
} |
|
1968 |
} |
|
1969 |
||
1970 |
if (a != after) |
|
1971 |
::free((char *)a); |
|
1972 |
if (b != before) |
|
1973 |
::free((char *)b); |
|
1974 |
||
1975 |
||
1976 |
return *this; |
|
1977 |
} |
|
1978 |
||
1979 |
||
1980 |
/*! |
|
1981 |
\fn QByteArray &QByteArray::replace(const QByteArray &before, const char *after) |
|
1982 |
\overload |
|
1983 |
||
1984 |
Replaces every occurrence of the byte array \a before with the |
|
1985 |
string \a after. |
|
1986 |
*/ |
|
1987 |
||
1988 |
/*! \fn QByteArray &QByteArray::replace(const QString &before, const QByteArray &after) |
|
1989 |
||
1990 |
\overload |
|
1991 |
||
1992 |
Replaces every occurrence of the string \a before with the byte |
|
1993 |
array \a after. The Unicode data is converted into 8-bit |
|
1994 |
characters using QString::toAscii(). |
|
1995 |
||
1996 |
If the QString contains non-ASCII Unicode characters, using this |
|
1997 |
function can lead to loss of information. You can disable this |
|
1998 |
function by defining \c QT_NO_CAST_TO_ASCII when you compile your |
|
1999 |
applications. You then need to call QString::toAscii() (or |
|
2000 |
QString::toLatin1() or QString::toUtf8() or QString::toLocal8Bit()) |
|
2001 |
explicitly if you want to convert the data to \c{const char *}. |
|
2002 |
*/ |
|
2003 |
||
2004 |
/*! \fn QByteArray &QByteArray::replace(const QString &before, const char *after) |
|
2005 |
\overload |
|
2006 |
||
2007 |
Replaces every occurrence of the string \a before with the string |
|
2008 |
\a after. |
|
2009 |
*/ |
|
2010 |
||
2011 |
/*! \fn QByteArray &QByteArray::replace(const char *before, const char *after) |
|
2012 |
||
2013 |
\overload |
|
2014 |
||
2015 |
Replaces every occurrence of the string \a before with the string |
|
2016 |
\a after. |
|
2017 |
*/ |
|
2018 |
||
2019 |
/*! |
|
2020 |
\overload |
|
2021 |
||
2022 |
Replaces every occurrence of the character \a before with the |
|
2023 |
byte array \a after. |
|
2024 |
*/ |
|
2025 |
||
2026 |
QByteArray &QByteArray::replace(char before, const QByteArray &after) |
|
2027 |
{ |
|
2028 |
char b[2] = { before, '\0' }; |
|
2029 |
QByteArray cb = fromRawData(b, 1); |
|
2030 |
return replace(cb, after); |
|
2031 |
} |
|
2032 |
||
2033 |
/*! \fn QByteArray &QByteArray::replace(char before, const QString &after) |
|
2034 |
||
2035 |
\overload |
|
2036 |
||
2037 |
Replaces every occurrence of the character \a before with the |
|
2038 |
string \a after. The Unicode data is converted into 8-bit |
|
2039 |
characters using QString::toAscii(). |
|
2040 |
||
2041 |
If the QString contains non-ASCII Unicode characters, using this |
|
2042 |
function can lead to loss of information. You can disable this |
|
2043 |
function by defining \c QT_NO_CAST_TO_ASCII when you compile your |
|
2044 |
applications. You then need to call QString::toAscii() (or |
|
2045 |
QString::toLatin1() or QString::toUtf8() or QString::toLocal8Bit()) |
|
2046 |
explicitly if you want to convert the data to \c{const char *}. |
|
2047 |
*/ |
|
2048 |
||
2049 |
/*! \fn QByteArray &QByteArray::replace(char before, const char *after) |
|
2050 |
||
2051 |
\overload |
|
2052 |
||
2053 |
Replaces every occurrence of the character \a before with the |
|
2054 |
string \a after. |
|
2055 |
*/ |
|
2056 |
||
2057 |
/*! |
|
2058 |
\overload |
|
2059 |
||
2060 |
Replaces every occurrence of the character \a before with the |
|
2061 |
character \a after. |
|
2062 |
*/ |
|
2063 |
||
2064 |
QByteArray &QByteArray::replace(char before, char after) |
|
2065 |
{ |
|
2066 |
if (d->size) { |
|
2067 |
char *i = data(); |
|
2068 |
char *e = i + d->size; |
|
2069 |
for (; i != e; ++i) |
|
2070 |
if (*i == before) |
|
2071 |
* i = after; |
|
2072 |
} |
|
2073 |
return *this; |
|
2074 |
} |
|
2075 |
||
2076 |
/*! |
|
2077 |
Splits the byte array into subarrays wherever \a sep occurs, and |
|
2078 |
returns the list of those arrays. If \a sep does not match |
|
2079 |
anywhere in the byte array, split() returns a single-element list |
|
2080 |
containing this byte array. |
|
2081 |
*/ |
|
2082 |
||
2083 |
QList<QByteArray> QByteArray::split(char sep) const |
|
2084 |
{ |
|
2085 |
QList<QByteArray> list; |
|
2086 |
int start = 0; |
|
2087 |
int end; |
|
2088 |
while ((end = indexOf(sep, start)) != -1) { |
|
2089 |
list.append(mid(start, end - start)); |
|
2090 |
start = end + 1; |
|
2091 |
} |
|
2092 |
list.append(mid(start)); |
|
2093 |
return list; |
|
2094 |
} |
|
2095 |
||
2096 |
/*! |
|
2097 |
\since 4.5 |
|
2098 |
||
2099 |
Returns a copy of this byte array repeated the specified number of \a times. |
|
2100 |
||
2101 |
If \a times is less than 1, an empty byte array is returned. |
|
2102 |
||
2103 |
Example: |
|
2104 |
||
2105 |
\code |
|
2106 |
QByteArray ba("ab"); |
|
2107 |
ba.repeated(4); // returns "abababab" |
|
2108 |
\endcode |
|
2109 |
*/ |
|
2110 |
QByteArray QByteArray::repeated(int times) const |
|
2111 |
{ |
|
2112 |
if (d->size == 0) |
|
2113 |
return *this; |
|
2114 |
||
2115 |
if (times <= 1) { |
|
2116 |
if (times == 1) |
|
2117 |
return *this; |
|
2118 |
return QByteArray(); |
|
2119 |
} |
|
2120 |
||
2121 |
const int resultSize = times * d->size; |
|
2122 |
||
2123 |
QByteArray result; |
|
2124 |
result.reserve(resultSize); |
|
2125 |
if (result.d->alloc != resultSize) |
|
2126 |
return QByteArray(); // not enough memory |
|
2127 |
||
2128 |
qMemCopy(result.d->data, d->data, d->size); |
|
2129 |
||
2130 |
int sizeSoFar = d->size; |
|
2131 |
char *end = result.d->data + sizeSoFar; |
|
2132 |
||
2133 |
const int halfResultSize = resultSize >> 1; |
|
2134 |
while (sizeSoFar <= halfResultSize) { |
|
2135 |
qMemCopy(end, result.d->data, sizeSoFar); |
|
2136 |
end += sizeSoFar; |
|
2137 |
sizeSoFar <<= 1; |
|
2138 |
} |
|
2139 |
qMemCopy(end, result.d->data, resultSize - sizeSoFar); |
|
2140 |
result.d->data[resultSize] = '\0'; |
|
2141 |
result.d->size = resultSize; |
|
2142 |
return result; |
|
2143 |
} |
|
2144 |
||
2145 |
#define REHASH(a) \ |
|
2146 |
if (ol_minus_1 < sizeof(uint) * CHAR_BIT) \ |
|
2147 |
hashHaystack -= (a) << ol_minus_1; \ |
|
2148 |
hashHaystack <<= 1 |
|
2149 |
||
2150 |
/*! |
|
2151 |
Returns the index position of the first occurrence of the byte |
|
2152 |
array \a ba in this byte array, searching forward from index |
|
2153 |
position \a from. Returns -1 if \a ba could not be found. |
|
2154 |
||
2155 |
Example: |
|
2156 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 21 |
|
2157 |
||
2158 |
\sa lastIndexOf(), contains(), count() |
|
2159 |
*/ |
|
2160 |
||
2161 |
int QByteArray::indexOf(const QByteArray &ba, int from) const |
|
2162 |
{ |
|
2163 |
const int ol = ba.d->size; |
|
2164 |
if (ol == 0) |
|
2165 |
return from; |
|
2166 |
if (ol == 1) |
|
2167 |
return indexOf(*ba.d->data, from); |
|
2168 |
||
2169 |
const int l = d->size; |
|
2170 |
if (from > d->size || ol + from > l) |
|
2171 |
return -1; |
|
2172 |
||
2173 |
return qFindByteArray(d->data, d->size, from, ba.d->data, ol); |
|
2174 |
} |
|
2175 |
||
2176 |
/*! \fn int QByteArray::indexOf(const QString &str, int from) const |
|
2177 |
||
2178 |
\overload |
|
2179 |
||
2180 |
Returns the index position of the first occurrence of the string |
|
2181 |
\a str in the byte array, searching forward from index position |
|
2182 |
\a from. Returns -1 if \a str could not be found. |
|
2183 |
||
2184 |
The Unicode data is converted into 8-bit characters using |
|
2185 |
QString::toAscii(). |
|
2186 |
||
2187 |
If the QString contains non-ASCII Unicode characters, using this |
|
2188 |
function can lead to loss of information. You can disable this |
|
2189 |
function by defining \c QT_NO_CAST_TO_ASCII when you compile your |
|
2190 |
applications. You then need to call QString::toAscii() (or |
|
2191 |
QString::toLatin1() or QString::toUtf8() or QString::toLocal8Bit()) |
|
2192 |
explicitly if you want to convert the data to \c{const char *}. |
|
2193 |
*/ |
|
2194 |
||
2195 |
/*! \fn int QByteArray::indexOf(const char *str, int from) const |
|
2196 |
||
2197 |
\overload |
|
2198 |
||
2199 |
Returns the index position of the first occurrence of the string |
|
2200 |
\a str in the byte array, searching forward from index position \a |
|
2201 |
from. Returns -1 if \a str could not be found. |
|
2202 |
*/ |
|
2203 |
int QByteArray::indexOf(const char *c, int from) const |
|
2204 |
{ |
|
2205 |
const int ol = qstrlen(c); |
|
2206 |
if (ol == 1) |
|
2207 |
return indexOf(*c, from); |
|
2208 |
||
2209 |
const int l = d->size; |
|
2210 |
if (from > d->size || ol + from > l) |
|
2211 |
return -1; |
|
2212 |
if (ol == 0) |
|
2213 |
return from; |
|
2214 |
||
2215 |
return qFindByteArray(d->data, d->size, from, c, ol); |
|
2216 |
} |
|
2217 |
||
2218 |
/*! |
|
2219 |
\overload |
|
2220 |
||
2221 |
Returns the index position of the first occurrence of the |
|
2222 |
character \a ch in the byte array, searching forward from index |
|
2223 |
position \a from. Returns -1 if \a ch could not be found. |
|
2224 |
||
2225 |
Example: |
|
2226 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 22 |
|
2227 |
||
2228 |
\sa lastIndexOf(), contains() |
|
2229 |
*/ |
|
2230 |
||
2231 |
int QByteArray::indexOf(char ch, int from) const |
|
2232 |
{ |
|
2233 |
if (from < 0) |
|
2234 |
from = qMax(from + d->size, 0); |
|
2235 |
if (from < d->size) { |
|
2236 |
const char *n = d->data + from - 1; |
|
2237 |
const char *e = d->data + d->size; |
|
2238 |
while (++n != e) |
|
2239 |
if (*n == ch) |
|
2240 |
return n - d->data; |
|
2241 |
} |
|
2242 |
return -1; |
|
2243 |
} |
|
2244 |
||
2245 |
||
2246 |
static int lastIndexOfHelper(const char *haystack, int l, const char *needle, int ol, int from) |
|
2247 |
{ |
|
2248 |
int delta = l - ol; |
|
2249 |
if (from < 0) |
|
2250 |
from = delta; |
|
2251 |
if (from < 0 || from > l) |
|
2252 |
return -1; |
|
2253 |
if (from > delta) |
|
2254 |
from = delta; |
|
2255 |
||
2256 |
const char *end = haystack; |
|
2257 |
haystack += from; |
|
2258 |
const uint ol_minus_1 = ol - 1; |
|
2259 |
const char *n = needle + ol_minus_1; |
|
2260 |
const char *h = haystack + ol_minus_1; |
|
2261 |
uint hashNeedle = 0, hashHaystack = 0; |
|
2262 |
int idx; |
|
2263 |
for (idx = 0; idx < ol; ++idx) { |
|
2264 |
hashNeedle = ((hashNeedle<<1) + *(n-idx)); |
|
2265 |
hashHaystack = ((hashHaystack<<1) + *(h-idx)); |
|
2266 |
} |
|
2267 |
hashHaystack -= *haystack; |
|
2268 |
while (haystack >= end) { |
|
2269 |
hashHaystack += *haystack; |
|
2270 |
if (hashHaystack == hashNeedle && memcmp(needle, haystack, ol) == 0) |
|
2271 |
return haystack - end; |
|
2272 |
--haystack; |
|
2273 |
REHASH(*(haystack + ol)); |
|
2274 |
} |
|
2275 |
return -1; |
|
2276 |
||
2277 |
} |
|
2278 |
||
2279 |
/*! |
|
2280 |
\fn int QByteArray::lastIndexOf(const QByteArray &ba, int from) const |
|
2281 |
||
2282 |
Returns the index position of the last occurrence of the byte |
|
2283 |
array \a ba in this byte array, searching backward from index |
|
2284 |
position \a from. If \a from is -1 (the default), the search |
|
2285 |
starts at the last byte. Returns -1 if \a ba could not be found. |
|
2286 |
||
2287 |
Example: |
|
2288 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 23 |
|
2289 |
||
2290 |
\sa indexOf(), contains(), count() |
|
2291 |
*/ |
|
2292 |
||
2293 |
int QByteArray::lastIndexOf(const QByteArray &ba, int from) const |
|
2294 |
{ |
|
2295 |
const int ol = ba.d->size; |
|
2296 |
if (ol == 1) |
|
2297 |
return lastIndexOf(*ba.d->data, from); |
|
2298 |
||
2299 |
return lastIndexOfHelper(d->data, d->size, ba.d->data, ol, from); |
|
2300 |
} |
|
2301 |
||
2302 |
/*! \fn int QByteArray::lastIndexOf(const QString &str, int from) const |
|
2303 |
||
2304 |
\overload |
|
2305 |
||
2306 |
Returns the index position of the last occurrence of the string \a |
|
2307 |
str in the byte array, searching backward from index position \a |
|
2308 |
from. If \a from is -1 (the default), the search starts at the |
|
2309 |
last (size() - 1) byte. Returns -1 if \a str could not be found. |
|
2310 |
||
2311 |
The Unicode data is converted into 8-bit characters using |
|
2312 |
QString::toAscii(). |
|
2313 |
||
2314 |
If the QString contains non-ASCII Unicode characters, using this |
|
2315 |
function can lead to loss of information. You can disable this |
|
2316 |
function by defining \c QT_NO_CAST_TO_ASCII when you compile your |
|
2317 |
applications. You then need to call QString::toAscii() (or |
|
2318 |
QString::toLatin1() or QString::toUtf8() or QString::toLocal8Bit()) |
|
2319 |
explicitly if you want to convert the data to \c{const char *}. |
|
2320 |
*/ |
|
2321 |
||
2322 |
/*! \fn int QByteArray::lastIndexOf(const char *str, int from) const |
|
2323 |
\overload |
|
2324 |
||
2325 |
Returns the index position of the last occurrence of the string \a |
|
2326 |
str in the byte array, searching backward from index position \a |
|
2327 |
from. If \a from is -1 (the default), the search starts at the |
|
2328 |
last (size() - 1) byte. Returns -1 if \a str could not be found. |
|
2329 |
*/ |
|
2330 |
int QByteArray::lastIndexOf(const char *str, int from) const |
|
2331 |
{ |
|
2332 |
const int ol = qstrlen(str); |
|
2333 |
if (ol == 1) |
|
2334 |
return lastIndexOf(*str, from); |
|
2335 |
||
2336 |
return lastIndexOfHelper(d->data, d->size, str, ol, from); |
|
2337 |
} |
|
2338 |
||
2339 |
/*! |
|
2340 |
\overload |
|
2341 |
||
2342 |
Returns the index position of the last occurrence of character \a |
|
2343 |
ch in the byte array, searching backward from index position \a |
|
2344 |
from. If \a from is -1 (the default), the search starts at the |
|
2345 |
last (size() - 1) byte. Returns -1 if \a ch could not be found. |
|
2346 |
||
2347 |
Example: |
|
2348 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 24 |
|
2349 |
||
2350 |
\sa indexOf(), contains() |
|
2351 |
*/ |
|
2352 |
||
2353 |
int QByteArray::lastIndexOf(char ch, int from) const |
|
2354 |
{ |
|
2355 |
if (from < 0) |
|
2356 |
from += d->size; |
|
2357 |
else if (from > d->size) |
|
2358 |
from = d->size-1; |
|
2359 |
if (from >= 0) { |
|
2360 |
const char *b = d->data; |
|
2361 |
const char *n = d->data + from + 1; |
|
2362 |
while (n-- != b) |
|
2363 |
if (*n == ch) |
|
2364 |
return n - b; |
|
2365 |
} |
|
2366 |
return -1; |
|
2367 |
} |
|
2368 |
||
2369 |
/*! |
|
2370 |
Returns the number of (potentially overlapping) occurrences of |
|
2371 |
byte array \a ba in this byte array. |
|
2372 |
||
2373 |
\sa contains(), indexOf() |
|
2374 |
*/ |
|
2375 |
||
2376 |
int QByteArray::count(const QByteArray &ba) const |
|
2377 |
{ |
|
2378 |
int num = 0; |
|
2379 |
int i = -1; |
|
2380 |
if (d->size > 500 && ba.d->size > 5) { |
|
2381 |
QByteArrayMatcher matcher(ba); |
|
2382 |
while ((i = matcher.indexIn(*this, i + 1)) != -1) |
|
2383 |
++num; |
|
2384 |
} else { |
|
2385 |
while ((i = indexOf(ba, i + 1)) != -1) |
|
2386 |
++num; |
|
2387 |
} |
|
2388 |
return num; |
|
2389 |
} |
|
2390 |
||
2391 |
/*! |
|
2392 |
\overload |
|
2393 |
||
2394 |
Returns the number of (potentially overlapping) occurrences of |
|
2395 |
string \a str in the byte array. |
|
2396 |
*/ |
|
2397 |
||
2398 |
int QByteArray::count(const char *str) const |
|
2399 |
{ |
|
2400 |
return count(fromRawData(str, qstrlen(str))); |
|
2401 |
} |
|
2402 |
||
2403 |
/*! |
|
2404 |
\overload |
|
2405 |
||
2406 |
Returns the number of occurrences of character \a ch in the byte |
|
2407 |
array. |
|
2408 |
||
2409 |
\sa contains(), indexOf() |
|
2410 |
*/ |
|
2411 |
||
2412 |
int QByteArray::count(char ch) const |
|
2413 |
{ |
|
2414 |
int num = 0; |
|
2415 |
const char *i = d->data + d->size; |
|
2416 |
const char *b = d->data; |
|
2417 |
while (i != b) |
|
2418 |
if (*--i == ch) |
|
2419 |
++num; |
|
2420 |
return num; |
|
2421 |
} |
|
2422 |
||
2423 |
/*! \fn int QByteArray::count() const |
|
2424 |
||
2425 |
\overload |
|
2426 |
||
2427 |
Same as size(). |
|
2428 |
*/ |
|
2429 |
||
2430 |
/*! |
|
2431 |
Returns true if this byte array starts with byte array \a ba; |
|
2432 |
otherwise returns false. |
|
2433 |
||
2434 |
Example: |
|
2435 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 25 |
|
2436 |
||
2437 |
\sa endsWith(), left() |
|
2438 |
*/ |
|
2439 |
bool QByteArray::startsWith(const QByteArray &ba) const |
|
2440 |
{ |
|
2441 |
if (d == ba.d || ba.d->size == 0) |
|
2442 |
return true; |
|
2443 |
if (d->size < ba.d->size) |
|
2444 |
return false; |
|
2445 |
return memcmp(d->data, ba.d->data, ba.d->size) == 0; |
|
2446 |
} |
|
2447 |
||
2448 |
/*! \overload |
|
2449 |
||
2450 |
Returns true if this byte array starts with string \a str; |
|
2451 |
otherwise returns false. |
|
2452 |
*/ |
|
2453 |
bool QByteArray::startsWith(const char *str) const |
|
2454 |
{ |
|
2455 |
if (!str || !*str) |
|
2456 |
return true; |
|
2457 |
int len = qstrlen(str); |
|
2458 |
if (d->size < len) |
|
2459 |
return false; |
|
2460 |
return qstrncmp(d->data, str, len) == 0; |
|
2461 |
} |
|
2462 |
||
2463 |
/*! \overload |
|
2464 |
||
2465 |
Returns true if this byte array starts with character \a ch; |
|
2466 |
otherwise returns false. |
|
2467 |
*/ |
|
2468 |
bool QByteArray::startsWith(char ch) const |
|
2469 |
{ |
|
2470 |
if (d->size == 0) |
|
2471 |
return false; |
|
2472 |
return d->data[0] == ch; |
|
2473 |
} |
|
2474 |
||
2475 |
/*! |
|
2476 |
Returns true if this byte array ends with byte array \a ba; |
|
2477 |
otherwise returns false. |
|
2478 |
||
2479 |
Example: |
|
2480 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 26 |
|
2481 |
||
2482 |
\sa startsWith(), right() |
|
2483 |
*/ |
|
2484 |
bool QByteArray::endsWith(const QByteArray &ba) const |
|
2485 |
{ |
|
2486 |
if (d == ba.d || ba.d->size == 0) |
|
2487 |
return true; |
|
2488 |
if (d->size < ba.d->size) |
|
2489 |
return false; |
|
2490 |
return memcmp(d->data + d->size - ba.d->size, ba.d->data, ba.d->size) == 0; |
|
2491 |
} |
|
2492 |
||
2493 |
/*! \overload |
|
2494 |
||
2495 |
Returns true if this byte array ends with string \a str; otherwise |
|
2496 |
returns false. |
|
2497 |
*/ |
|
2498 |
bool QByteArray::endsWith(const char *str) const |
|
2499 |
{ |
|
2500 |
if (!str || !*str) |
|
2501 |
return true; |
|
2502 |
int len = qstrlen(str); |
|
2503 |
if (d->size < len) |
|
2504 |
return false; |
|
2505 |
return qstrncmp(d->data + d->size - len, str, len) == 0; |
|
2506 |
} |
|
2507 |
||
2508 |
/*! \overload |
|
2509 |
||
2510 |
Returns true if this byte array ends with character \a ch; |
|
2511 |
otherwise returns false. |
|
2512 |
*/ |
|
2513 |
bool QByteArray::endsWith(char ch) const |
|
2514 |
{ |
|
2515 |
if (d->size == 0) |
|
2516 |
return false; |
|
2517 |
return d->data[d->size - 1] == ch; |
|
2518 |
} |
|
2519 |
||
2520 |
/*! |
|
2521 |
Returns a byte array that contains the leftmost \a len bytes of |
|
2522 |
this byte array. |
|
2523 |
||
2524 |
The entire byte array is returned if \a len is greater than |
|
2525 |
size(). |
|
2526 |
||
2527 |
Example: |
|
2528 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 27 |
|
2529 |
||
2530 |
\sa right(), mid(), startsWith(), truncate() |
|
2531 |
*/ |
|
2532 |
||
2533 |
QByteArray QByteArray::left(int len) const |
|
2534 |
{ |
|
2535 |
if (len >= d->size) |
|
2536 |
return *this; |
|
2537 |
if (len < 0) |
|
2538 |
len = 0; |
|
2539 |
return QByteArray(d->data, len); |
|
2540 |
} |
|
2541 |
||
2542 |
/*! |
|
2543 |
Returns a byte array that contains the rightmost \a len bytes of |
|
2544 |
this byte array. |
|
2545 |
||
2546 |
The entire byte array is returned if \a len is greater than |
|
2547 |
size(). |
|
2548 |
||
2549 |
Example: |
|
2550 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 28 |
|
2551 |
||
2552 |
\sa endsWith(), left(), mid() |
|
2553 |
*/ |
|
2554 |
||
2555 |
QByteArray QByteArray::right(int len) const |
|
2556 |
{ |
|
2557 |
if (len >= d->size) |
|
2558 |
return *this; |
|
2559 |
if (len < 0) |
|
2560 |
len = 0; |
|
2561 |
return QByteArray(d->data + d->size - len, len); |
|
2562 |
} |
|
2563 |
||
2564 |
/*! |
|
2565 |
Returns a byte array containing \a len bytes from this byte array, |
|
2566 |
starting at position \a pos. |
|
2567 |
||
2568 |
If \a len is -1 (the default), or \a pos + \a len >= size(), |
|
2569 |
returns a byte array containing all bytes starting at position \a |
|
2570 |
pos until the end of the byte array. |
|
2571 |
||
2572 |
Example: |
|
2573 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 29 |
|
2574 |
||
2575 |
\sa left(), right() |
|
2576 |
*/ |
|
2577 |
||
2578 |
QByteArray QByteArray::mid(int pos, int len) const |
|
2579 |
{ |
|
2580 |
if (d == &shared_null || d == &shared_empty || pos >= d->size) |
|
2581 |
return QByteArray(); |
|
2582 |
if (len < 0) |
|
2583 |
len = d->size - pos; |
|
2584 |
if (pos < 0) { |
|
2585 |
len += pos; |
|
2586 |
pos = 0; |
|
2587 |
} |
|
2588 |
if (len + pos > d->size) |
|
2589 |
len = d->size - pos; |
|
2590 |
if (pos == 0 && len == d->size) |
|
2591 |
return *this; |
|
2592 |
return QByteArray(d->data + pos, len); |
|
2593 |
} |
|
2594 |
||
2595 |
/*! |
|
2596 |
Returns a lowercase copy of the byte array. The bytearray is |
|
2597 |
interpreted as a Latin-1 encoded string. |
|
2598 |
||
2599 |
Example: |
|
2600 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 30 |
|
2601 |
||
2602 |
\sa toUpper(), {8-bit Character Comparisons} |
|
2603 |
*/ |
|
2604 |
QByteArray QByteArray::toLower() const |
|
2605 |
{ |
|
2606 |
QByteArray s(*this); |
|
2607 |
register uchar *p = reinterpret_cast<uchar *>(s.data()); |
|
2608 |
if (p) { |
|
2609 |
while (*p) { |
|
2610 |
*p = QChar::toLower((ushort)*p); |
|
2611 |
p++; |
|
2612 |
} |
|
2613 |
} |
|
2614 |
return s; |
|
2615 |
} |
|
2616 |
||
2617 |
/*! |
|
2618 |
Returns an uppercase copy of the byte array. The bytearray is |
|
2619 |
interpreted as a Latin-1 encoded string. |
|
2620 |
||
2621 |
Example: |
|
2622 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 31 |
|
2623 |
||
2624 |
\sa toLower(), {8-bit Character Comparisons} |
|
2625 |
*/ |
|
2626 |
||
2627 |
QByteArray QByteArray::toUpper() const |
|
2628 |
{ |
|
2629 |
QByteArray s(*this); |
|
2630 |
register uchar *p = reinterpret_cast<uchar *>(s.data()); |
|
2631 |
if (p) { |
|
2632 |
while (*p) { |
|
2633 |
*p = QChar::toUpper((ushort)*p); |
|
2634 |
p++; |
|
2635 |
} |
|
2636 |
} |
|
2637 |
return s; |
|
2638 |
} |
|
2639 |
||
2640 |
/*! \fn void QByteArray::clear() |
|
2641 |
||
2642 |
Clears the contents of the byte array and makes it empty. |
|
2643 |
||
2644 |
\sa resize(), isEmpty() |
|
2645 |
*/ |
|
2646 |
||
2647 |
void QByteArray::clear() |
|
2648 |
{ |
|
2649 |
if (!d->ref.deref()) |
|
2650 |
qFree(d); |
|
2651 |
d = &shared_null; |
|
2652 |
d->ref.ref(); |
|
2653 |
} |
|
2654 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2655 |
#if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE)) |
0 | 2656 |
|
2657 |
/*! \relates QByteArray |
|
2658 |
||
2659 |
Writes byte array \a ba to the stream \a out and returns a reference |
|
2660 |
to the stream. |
|
2661 |
||
2662 |
\sa {Format of the QDataStream operators} |
|
2663 |
*/ |
|
2664 |
||
2665 |
QDataStream &operator<<(QDataStream &out, const QByteArray &ba) |
|
2666 |
{ |
|
2667 |
if (ba.isNull() && out.version() >= 6) { |
|
2668 |
out << (quint32)0xffffffff; |
|
2669 |
return out; |
|
2670 |
} |
|
2671 |
return out.writeBytes(ba, ba.size()); |
|
2672 |
} |
|
2673 |
||
2674 |
/*! \relates QByteArray |
|
2675 |
||
2676 |
Reads a byte array into \a ba from the stream \a in and returns a |
|
2677 |
reference to the stream. |
|
2678 |
||
2679 |
\sa {Format of the QDataStream operators} |
|
2680 |
*/ |
|
2681 |
||
2682 |
QDataStream &operator>>(QDataStream &in, QByteArray &ba) |
|
2683 |
{ |
|
2684 |
ba.clear(); |
|
2685 |
quint32 len; |
|
2686 |
in >> len; |
|
2687 |
if (len == 0xffffffff) |
|
2688 |
return in; |
|
2689 |
||
2690 |
const quint32 Step = 1024 * 1024; |
|
2691 |
quint32 allocated = 0; |
|
2692 |
||
2693 |
do { |
|
2694 |
int blockSize = qMin(Step, len - allocated); |
|
2695 |
ba.resize(allocated + blockSize); |
|
2696 |
if (in.readRawData(ba.data() + allocated, blockSize) != blockSize) { |
|
2697 |
ba.clear(); |
|
2698 |
in.setStatus(QDataStream::ReadPastEnd); |
|
2699 |
return in; |
|
2700 |
} |
|
2701 |
allocated += blockSize; |
|
2702 |
} while (allocated < len); |
|
2703 |
||
2704 |
return in; |
|
2705 |
} |
|
2706 |
#endif // QT_NO_DATASTREAM |
|
2707 |
||
2708 |
/*! \fn bool QByteArray::operator==(const QString &str) const |
|
2709 |
||
2710 |
Returns true if this byte array is equal to string \a str; |
|
2711 |
otherwise returns false. |
|
2712 |
||
2713 |
The Unicode data is converted into 8-bit characters using |
|
2714 |
QString::toAscii(). |
|
2715 |
||
2716 |
The comparison is case sensitive. |
|
2717 |
||
2718 |
You can disable this operator by defining \c |
|
2719 |
QT_NO_CAST_FROM_ASCII when you compile your applications. You |
|
2720 |
then need to call QString::fromAscii(), QString::fromLatin1(), |
|
2721 |
QString::fromUtf8(), or QString::fromLocal8Bit() explicitly if |
|
2722 |
you want to convert the byte array to a QString before doing the |
|
2723 |
comparison. |
|
2724 |
*/ |
|
2725 |
||
2726 |
/*! \fn bool QByteArray::operator!=(const QString &str) const |
|
2727 |
||
2728 |
Returns true if this byte array is not equal to string \a str; |
|
2729 |
otherwise returns false. |
|
2730 |
||
2731 |
The Unicode data is converted into 8-bit characters using |
|
2732 |
QString::toAscii(). |
|
2733 |
||
2734 |
The comparison is case sensitive. |
|
2735 |
||
2736 |
You can disable this operator by defining \c |
|
2737 |
QT_NO_CAST_FROM_ASCII when you compile your applications. You |
|
2738 |
then need to call QString::fromAscii(), QString::fromLatin1(), |
|
2739 |
QString::fromUtf8(), or QString::fromLocal8Bit() explicitly if |
|
2740 |
you want to convert the byte array to a QString before doing the |
|
2741 |
comparison. |
|
2742 |
*/ |
|
2743 |
||
2744 |
/*! \fn bool QByteArray::operator<(const QString &str) const |
|
2745 |
||
2746 |
Returns true if this byte array is lexically less than string \a |
|
2747 |
str; otherwise returns false. |
|
2748 |
||
2749 |
The Unicode data is converted into 8-bit characters using |
|
2750 |
QString::toAscii(). |
|
2751 |
||
2752 |
The comparison is case sensitive. |
|
2753 |
||
2754 |
You can disable this operator by defining \c |
|
2755 |
QT_NO_CAST_FROM_ASCII when you compile your applications. You |
|
2756 |
then need to call QString::fromAscii(), QString::fromLatin1(), |
|
2757 |
QString::fromUtf8(), or QString::fromLocal8Bit() explicitly if |
|
2758 |
you want to convert the byte array to a QString before doing the |
|
2759 |
comparison. |
|
2760 |
*/ |
|
2761 |
||
2762 |
/*! \fn bool QByteArray::operator>(const QString &str) const |
|
2763 |
||
2764 |
Returns true if this byte array is lexically greater than string |
|
2765 |
\a str; otherwise returns false. |
|
2766 |
||
2767 |
The Unicode data is converted into 8-bit characters using |
|
2768 |
QString::toAscii(). |
|
2769 |
||
2770 |
The comparison is case sensitive. |
|
2771 |
||
2772 |
You can disable this operator by defining \c |
|
2773 |
QT_NO_CAST_FROM_ASCII when you compile your applications. You |
|
2774 |
then need to call QString::fromAscii(), QString::fromLatin1(), |
|
2775 |
QString::fromUtf8(), or QString::fromLocal8Bit() explicitly if |
|
2776 |
you want to convert the byte array to a QString before doing the |
|
2777 |
comparison. |
|
2778 |
*/ |
|
2779 |
||
2780 |
/*! \fn bool QByteArray::operator<=(const QString &str) const |
|
2781 |
||
2782 |
Returns true if this byte array is lexically less than or equal |
|
2783 |
to string \a str; otherwise returns false. |
|
2784 |
||
2785 |
The Unicode data is converted into 8-bit characters using |
|
2786 |
QString::toAscii(). |
|
2787 |
||
2788 |
The comparison is case sensitive. |
|
2789 |
||
2790 |
You can disable this operator by defining \c |
|
2791 |
QT_NO_CAST_FROM_ASCII when you compile your applications. You |
|
2792 |
then need to call QString::fromAscii(), QString::fromLatin1(), |
|
2793 |
QString::fromUtf8(), or QString::fromLocal8Bit() explicitly if |
|
2794 |
you want to convert the byte array to a QString before doing the |
|
2795 |
comparison. |
|
2796 |
*/ |
|
2797 |
||
2798 |
/*! \fn bool QByteArray::operator>=(const QString &str) const |
|
2799 |
||
2800 |
Returns true if this byte array is greater than or equal to string |
|
2801 |
\a str; otherwise returns false. |
|
2802 |
||
2803 |
The Unicode data is converted into 8-bit characters using |
|
2804 |
QString::toAscii(). |
|
2805 |
||
2806 |
The comparison is case sensitive. |
|
2807 |
||
2808 |
You can disable this operator by defining \c |
|
2809 |
QT_NO_CAST_FROM_ASCII when you compile your applications. You |
|
2810 |
then need to call QString::fromAscii(), QString::fromLatin1(), |
|
2811 |
QString::fromUtf8(), or QString::fromLocal8Bit() explicitly if |
|
2812 |
you want to convert the byte array to a QString before doing the |
|
2813 |
comparison. |
|
2814 |
*/ |
|
2815 |
||
2816 |
/*! \fn bool operator==(const QByteArray &a1, const QByteArray &a2) |
|
2817 |
\relates QByteArray |
|
2818 |
||
2819 |
\overload |
|
2820 |
||
2821 |
Returns true if byte array \a a1 is equal to byte array \a a2; |
|
2822 |
otherwise returns false. |
|
2823 |
*/ |
|
2824 |
||
2825 |
/*! \fn bool operator==(const QByteArray &a1, const char *a2) |
|
2826 |
\relates QByteArray |
|
2827 |
||
2828 |
\overload |
|
2829 |
||
2830 |
Returns true if byte array \a a1 is equal to string \a a2; |
|
2831 |
otherwise returns false. |
|
2832 |
*/ |
|
2833 |
||
2834 |
/*! \fn bool operator==(const char *a1, const QByteArray &a2) |
|
2835 |
\relates QByteArray |
|
2836 |
||
2837 |
\overload |
|
2838 |
||
2839 |
Returns true if string \a a1 is equal to byte array \a a2; |
|
2840 |
otherwise returns false. |
|
2841 |
*/ |
|
2842 |
||
2843 |
/*! \fn bool operator!=(const QByteArray &a1, const QByteArray &a2) |
|
2844 |
\relates QByteArray |
|
2845 |
||
2846 |
\overload |
|
2847 |
||
2848 |
Returns true if byte array \a a1 is not equal to byte array \a a2; |
|
2849 |
otherwise returns false. |
|
2850 |
*/ |
|
2851 |
||
2852 |
/*! \fn bool operator!=(const QByteArray &a1, const char *a2) |
|
2853 |
\relates QByteArray |
|
2854 |
||
2855 |
\overload |
|
2856 |
||
2857 |
Returns true if byte array \a a1 is not equal to string \a a2; |
|
2858 |
otherwise returns false. |
|
2859 |
*/ |
|
2860 |
||
2861 |
/*! \fn bool operator!=(const char *a1, const QByteArray &a2) |
|
2862 |
\relates QByteArray |
|
2863 |
||
2864 |
\overload |
|
2865 |
||
2866 |
Returns true if string \a a1 is not equal to byte array \a a2; |
|
2867 |
otherwise returns false. |
|
2868 |
*/ |
|
2869 |
||
2870 |
/*! \fn bool operator<(const QByteArray &a1, const QByteArray &a2) |
|
2871 |
\relates QByteArray |
|
2872 |
||
2873 |
\overload |
|
2874 |
||
2875 |
Returns true if byte array \a a1 is lexically less than byte array |
|
2876 |
\a a2; otherwise returns false. |
|
2877 |
*/ |
|
2878 |
||
2879 |
/*! \fn inline bool operator<(const QByteArray &a1, const char *a2) |
|
2880 |
\relates QByteArray |
|
2881 |
||
2882 |
\overload |
|
2883 |
||
2884 |
Returns true if byte array \a a1 is lexically less than string |
|
2885 |
\a a2; otherwise returns false. |
|
2886 |
*/ |
|
2887 |
||
2888 |
/*! \fn bool operator<(const char *a1, const QByteArray &a2) |
|
2889 |
\relates QByteArray |
|
2890 |
||
2891 |
\overload |
|
2892 |
||
2893 |
Returns true if string \a a1 is lexically less than byte array |
|
2894 |
\a a2; otherwise returns false. |
|
2895 |
*/ |
|
2896 |
||
2897 |
/*! \fn bool operator<=(const QByteArray &a1, const QByteArray &a2) |
|
2898 |
\relates QByteArray |
|
2899 |
||
2900 |
\overload |
|
2901 |
||
2902 |
Returns true if byte array \a a1 is lexically less than or equal |
|
2903 |
to byte array \a a2; otherwise returns false. |
|
2904 |
*/ |
|
2905 |
||
2906 |
/*! \fn bool operator<=(const QByteArray &a1, const char *a2) |
|
2907 |
\relates QByteArray |
|
2908 |
||
2909 |
\overload |
|
2910 |
||
2911 |
Returns true if byte array \a a1 is lexically less than or equal |
|
2912 |
to string \a a2; otherwise returns false. |
|
2913 |
*/ |
|
2914 |
||
2915 |
/*! \fn bool operator<=(const char *a1, const QByteArray &a2) |
|
2916 |
\relates QByteArray |
|
2917 |
||
2918 |
\overload |
|
2919 |
||
2920 |
Returns true if string \a a1 is lexically less than or equal |
|
2921 |
to byte array \a a2; otherwise returns false. |
|
2922 |
*/ |
|
2923 |
||
2924 |
/*! \fn bool operator>(const QByteArray &a1, const QByteArray &a2) |
|
2925 |
\relates QByteArray |
|
2926 |
||
2927 |
\overload |
|
2928 |
||
2929 |
Returns true if byte array \a a1 is lexically greater than byte |
|
2930 |
array \a a2; otherwise returns false. |
|
2931 |
*/ |
|
2932 |
||
2933 |
/*! \fn bool operator>(const QByteArray &a1, const char *a2) |
|
2934 |
\relates QByteArray |
|
2935 |
||
2936 |
\overload |
|
2937 |
||
2938 |
Returns true if byte array \a a1 is lexically greater than string |
|
2939 |
\a a2; otherwise returns false. |
|
2940 |
*/ |
|
2941 |
||
2942 |
/*! \fn bool operator>(const char *a1, const QByteArray &a2) |
|
2943 |
\relates QByteArray |
|
2944 |
||
2945 |
\overload |
|
2946 |
||
2947 |
Returns true if string \a a1 is lexically greater than byte array |
|
2948 |
\a a2; otherwise returns false. |
|
2949 |
*/ |
|
2950 |
||
2951 |
/*! \fn bool operator>=(const QByteArray &a1, const QByteArray &a2) |
|
2952 |
\relates QByteArray |
|
2953 |
||
2954 |
\overload |
|
2955 |
||
2956 |
Returns true if byte array \a a1 is lexically greater than or |
|
2957 |
equal to byte array \a a2; otherwise returns false. |
|
2958 |
*/ |
|
2959 |
||
2960 |
/*! \fn bool operator>=(const QByteArray &a1, const char *a2) |
|
2961 |
\relates QByteArray |
|
2962 |
||
2963 |
\overload |
|
2964 |
||
2965 |
Returns true if byte array \a a1 is lexically greater than or |
|
2966 |
equal to string \a a2; otherwise returns false. |
|
2967 |
*/ |
|
2968 |
||
2969 |
/*! \fn bool operator>=(const char *a1, const QByteArray &a2) |
|
2970 |
\relates QByteArray |
|
2971 |
||
2972 |
\overload |
|
2973 |
||
2974 |
Returns true if string \a a1 is lexically greater than or |
|
2975 |
equal to byte array \a a2; otherwise returns false. |
|
2976 |
*/ |
|
2977 |
||
2978 |
/*! \fn const QByteArray operator+(const QByteArray &a1, const QByteArray &a2) |
|
2979 |
\relates QByteArray |
|
2980 |
||
2981 |
Returns a byte array that is the result of concatenating byte |
|
2982 |
array \a a1 and byte array \a a2. |
|
2983 |
||
2984 |
\sa QByteArray::operator+=() |
|
2985 |
*/ |
|
2986 |
||
2987 |
/*! \fn const QByteArray operator+(const QByteArray &a1, const char *a2) |
|
2988 |
\relates QByteArray |
|
2989 |
||
2990 |
\overload |
|
2991 |
||
2992 |
Returns a byte array that is the result of concatenating byte |
|
2993 |
array \a a1 and string \a a2. |
|
2994 |
*/ |
|
2995 |
||
2996 |
/*! \fn const QByteArray operator+(const QByteArray &a1, char a2) |
|
2997 |
\relates QByteArray |
|
2998 |
||
2999 |
\overload |
|
3000 |
||
3001 |
Returns a byte array that is the result of concatenating byte |
|
3002 |
array \a a1 and character \a a2. |
|
3003 |
*/ |
|
3004 |
||
3005 |
/*! \fn const QByteArray operator+(const char *a1, const QByteArray &a2) |
|
3006 |
\relates QByteArray |
|
3007 |
||
3008 |
\overload |
|
3009 |
||
3010 |
Returns a byte array that is the result of concatenating string |
|
3011 |
\a a1 and byte array \a a2. |
|
3012 |
*/ |
|
3013 |
||
3014 |
/*! \fn const QByteArray operator+(char a1, const QByteArray &a2) |
|
3015 |
\relates QByteArray |
|
3016 |
||
3017 |
\overload |
|
3018 |
||
3019 |
Returns a byte array that is the result of concatenating character |
|
3020 |
\a a1 and byte array \a a2. |
|
3021 |
*/ |
|
3022 |
||
3023 |
/*! |
|
3024 |
Returns a byte array that has whitespace removed from the start |
|
3025 |
and the end, and which has each sequence of internal whitespace |
|
3026 |
replaced with a single space. |
|
3027 |
||
3028 |
Whitespace means any character for which the standard C++ |
|
3029 |
isspace() function returns true. This includes the ASCII |
|
3030 |
characters '\\t', '\\n', '\\v', '\\f', '\\r', and ' '. |
|
3031 |
||
3032 |
Example: |
|
3033 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 32 |
|
3034 |
||
3035 |
\sa trimmed() |
|
3036 |
*/ |
|
3037 |
QByteArray QByteArray::simplified() const |
|
3038 |
{ |
|
3039 |
if (d->size == 0) |
|
3040 |
return *this; |
|
3041 |
QByteArray result(d->size, Qt::Uninitialized); |
|
3042 |
const char *from = d->data; |
|
3043 |
const char *fromend = from + d->size; |
|
3044 |
int outc=0; |
|
3045 |
char *to = result.d->data; |
|
3046 |
for (;;) { |
|
3047 |
while (from!=fromend && isspace(uchar(*from))) |
|
3048 |
from++; |
|
3049 |
while (from!=fromend && !isspace(uchar(*from))) |
|
3050 |
to[outc++] = *from++; |
|
3051 |
if (from!=fromend) |
|
3052 |
to[outc++] = ' '; |
|
3053 |
else |
|
3054 |
break; |
|
3055 |
} |
|
3056 |
if (outc > 0 && to[outc-1] == ' ') |
|
3057 |
outc--; |
|
3058 |
result.resize(outc); |
|
3059 |
return result; |
|
3060 |
} |
|
3061 |
||
3062 |
/*! |
|
3063 |
Returns a byte array that has whitespace removed from the start |
|
3064 |
and the end. |
|
3065 |
||
3066 |
Whitespace means any character for which the standard C++ |
|
3067 |
isspace() function returns true. This includes the ASCII |
|
3068 |
characters '\\t', '\\n', '\\v', '\\f', '\\r', and ' '. |
|
3069 |
||
3070 |
Example: |
|
3071 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 33 |
|
3072 |
||
3073 |
Unlike simplified(), trimmed() leaves internal whitespace alone. |
|
3074 |
||
3075 |
\sa simplified() |
|
3076 |
*/ |
|
3077 |
QByteArray QByteArray::trimmed() const |
|
3078 |
{ |
|
3079 |
if (d->size == 0) |
|
3080 |
return *this; |
|
3081 |
const char *s = d->data; |
|
3082 |
if (!isspace(uchar(*s)) && !isspace(uchar(s[d->size-1]))) |
|
3083 |
return *this; |
|
3084 |
int start = 0; |
|
3085 |
int end = d->size - 1; |
|
3086 |
while (start<=end && isspace(uchar(s[start]))) // skip white space from start |
|
3087 |
start++; |
|
3088 |
if (start <= end) { // only white space |
|
3089 |
while (end && isspace(uchar(s[end]))) // skip white space from end |
|
3090 |
end--; |
|
3091 |
} |
|
3092 |
int l = end - start + 1; |
|
3093 |
if (l <= 0) { |
|
3094 |
shared_empty.ref.ref(); |
|
3095 |
return QByteArray(&shared_empty, 0, 0); |
|
3096 |
} |
|
3097 |
return QByteArray(s+start, l); |
|
3098 |
} |
|
3099 |
||
3100 |
/*! |
|
3101 |
Returns a byte array of size \a width that contains this byte |
|
3102 |
array padded by the \a fill character. |
|
3103 |
||
3104 |
If \a truncate is false and the size() of the byte array is more |
|
3105 |
than \a width, then the returned byte array is a copy of this byte |
|
3106 |
array. |
|
3107 |
||
3108 |
If \a truncate is true and the size() of the byte array is more |
|
3109 |
than \a width, then any bytes in a copy of the byte array |
|
3110 |
after position \a width are removed, and the copy is returned. |
|
3111 |
||
3112 |
Example: |
|
3113 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 34 |
|
3114 |
||
3115 |
\sa rightJustified() |
|
3116 |
*/ |
|
3117 |
||
3118 |
QByteArray QByteArray::leftJustified(int width, char fill, bool truncate) const |
|
3119 |
{ |
|
3120 |
QByteArray result; |
|
3121 |
int len = d->size; |
|
3122 |
int padlen = width - len; |
|
3123 |
if (padlen > 0) { |
|
3124 |
result.resize(len+padlen); |
|
3125 |
if (len) |
|
3126 |
memcpy(result.d->data, d->data, len); |
|
3127 |
memset(result.d->data+len, fill, padlen); |
|
3128 |
} else { |
|
3129 |
if (truncate) |
|
3130 |
result = left(width); |
|
3131 |
else |
|
3132 |
result = *this; |
|
3133 |
} |
|
3134 |
return result; |
|
3135 |
} |
|
3136 |
||
3137 |
/*! |
|
3138 |
Returns a byte array of size \a width that contains the \a fill |
|
3139 |
character followed by this byte array. |
|
3140 |
||
3141 |
If \a truncate is false and the size of the byte array is more |
|
3142 |
than \a width, then the returned byte array is a copy of this byte |
|
3143 |
array. |
|
3144 |
||
3145 |
If \a truncate is true and the size of the byte array is more |
|
3146 |
than \a width, then the resulting byte array is truncated at |
|
3147 |
position \a width. |
|
3148 |
||
3149 |
Example: |
|
3150 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 35 |
|
3151 |
||
3152 |
\sa leftJustified() |
|
3153 |
*/ |
|
3154 |
||
3155 |
QByteArray QByteArray::rightJustified(int width, char fill, bool truncate) const |
|
3156 |
{ |
|
3157 |
QByteArray result; |
|
3158 |
int len = d->size; |
|
3159 |
int padlen = width - len; |
|
3160 |
if (padlen > 0) { |
|
3161 |
result.resize(len+padlen); |
|
3162 |
if (len) |
|
3163 |
memcpy(result.d->data+padlen, data(), len); |
|
3164 |
memset(result.d->data, fill, padlen); |
|
3165 |
} else { |
|
3166 |
if (truncate) |
|
3167 |
result = left(width); |
|
3168 |
else |
|
3169 |
result = *this; |
|
3170 |
} |
|
3171 |
return result; |
|
3172 |
} |
|
3173 |
||
3174 |
bool QByteArray::isNull() const { return d == &shared_null; } |
|
3175 |
||
3176 |
||
3177 |
/*! |
|
3178 |
Returns the byte array converted to a \c {long long} using base \a |
|
3179 |
base, which is 10 by default and must be between 2 and 36, or 0. |
|
3180 |
||
3181 |
If \a base is 0, the base is determined automatically using the |
|
3182 |
following rules: If the byte array begins with "0x", it is assumed to |
|
3183 |
be hexadecimal; if it begins with "0", it is assumed to be octal; |
|
3184 |
otherwise it is assumed to be decimal. |
|
3185 |
||
3186 |
Returns 0 if the conversion fails. |
|
3187 |
||
3188 |
If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to |
|
3189 |
false; otherwise *\a{ok} is set to true. |
|
3190 |
||
3191 |
\note The conversion of the number is performed in the default C locale, |
|
3192 |
irrespective of the user's locale. |
|
3193 |
||
3194 |
\sa number() |
|
3195 |
*/ |
|
3196 |
||
3197 |
qlonglong QByteArray::toLongLong(bool *ok, int base) const |
|
3198 |
{ |
|
3199 |
#if defined(QT_CHECK_RANGE) |
|
3200 |
if (base != 0 && (base < 2 || base > 36)) { |
|
3201 |
qWarning("QByteArray::toLongLong: Invalid base %d", base); |
|
3202 |
base = 10; |
|
3203 |
} |
|
3204 |
#endif |
|
3205 |
||
3206 |
return QLocalePrivate::bytearrayToLongLong(nulTerminated().constData(), base, ok); |
|
3207 |
} |
|
3208 |
||
3209 |
/*! |
|
3210 |
Returns the byte array converted to an \c {unsigned long long} |
|
3211 |
using base \a base, which is 10 by default and must be between 2 |
|
3212 |
and 36, or 0. |
|
3213 |
||
3214 |
If \a base is 0, the base is determined automatically using the |
|
3215 |
following rules: If the byte array begins with "0x", it is assumed to |
|
3216 |
be hexadecimal; if it begins with "0", it is assumed to be octal; |
|
3217 |
otherwise it is assumed to be decimal. |
|
3218 |
||
3219 |
Returns 0 if the conversion fails. |
|
3220 |
||
3221 |
If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to |
|
3222 |
false; otherwise *\a{ok} is set to true. |
|
3223 |
||
3224 |
\note The conversion of the number is performed in the default C locale, |
|
3225 |
irrespective of the user's locale. |
|
3226 |
||
3227 |
\sa number() |
|
3228 |
*/ |
|
3229 |
||
3230 |
qulonglong QByteArray::toULongLong(bool *ok, int base) const |
|
3231 |
{ |
|
3232 |
#if defined(QT_CHECK_RANGE) |
|
3233 |
if (base != 0 && (base < 2 || base > 36)) { |
|
3234 |
qWarning("QByteArray::toULongLong: Invalid base %d", base); |
|
3235 |
base = 10; |
|
3236 |
} |
|
3237 |
#endif |
|
3238 |
||
3239 |
return QLocalePrivate::bytearrayToUnsLongLong(nulTerminated().constData(), base, ok); |
|
3240 |
} |
|
3241 |
||
3242 |
||
3243 |
/*! |
|
3244 |
Returns the byte array converted to an \c int using base \a |
|
3245 |
base, which is 10 by default and must be between 2 and 36, or 0. |
|
3246 |
||
3247 |
If \a base is 0, the base is determined automatically using the |
|
3248 |
following rules: If the byte array begins with "0x", it is assumed to |
|
3249 |
be hexadecimal; if it begins with "0", it is assumed to be octal; |
|
3250 |
otherwise it is assumed to be decimal. |
|
3251 |
||
3252 |
Returns 0 if the conversion fails. |
|
3253 |
||
3254 |
If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to |
|
3255 |
false; otherwise *\a{ok} is set to true. |
|
3256 |
||
3257 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 36 |
|
3258 |
||
3259 |
\note The conversion of the number is performed in the default C locale, |
|
3260 |
irrespective of the user's locale. |
|
3261 |
||
3262 |
\sa number() |
|
3263 |
*/ |
|
3264 |
||
3265 |
int QByteArray::toInt(bool *ok, int base) const |
|
3266 |
{ |
|
3267 |
qlonglong v = toLongLong(ok, base); |
|
3268 |
if (v < INT_MIN || v > INT_MAX) { |
|
3269 |
if (ok) |
|
3270 |
*ok = false; |
|
3271 |
v = 0; |
|
3272 |
} |
|
3273 |
return int(v); |
|
3274 |
} |
|
3275 |
||
3276 |
/*! |
|
3277 |
Returns the byte array converted to an \c {unsigned int} using base \a |
|
3278 |
base, which is 10 by default and must be between 2 and 36, or 0. |
|
3279 |
||
3280 |
If \a base is 0, the base is determined automatically using the |
|
3281 |
following rules: If the byte array begins with "0x", it is assumed to |
|
3282 |
be hexadecimal; if it begins with "0", it is assumed to be octal; |
|
3283 |
otherwise it is assumed to be decimal. |
|
3284 |
||
3285 |
Returns 0 if the conversion fails. |
|
3286 |
||
3287 |
If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to |
|
3288 |
false; otherwise *\a{ok} is set to true. |
|
3289 |
||
3290 |
\note The conversion of the number is performed in the default C locale, |
|
3291 |
irrespective of the user's locale. |
|
3292 |
||
3293 |
\sa number() |
|
3294 |
*/ |
|
3295 |
||
3296 |
uint QByteArray::toUInt(bool *ok, int base) const |
|
3297 |
{ |
|
3298 |
qulonglong v = toULongLong(ok, base); |
|
3299 |
if (v > UINT_MAX) { |
|
3300 |
if (ok) |
|
3301 |
*ok = false; |
|
3302 |
v = 0; |
|
3303 |
} |
|
3304 |
return uint(v); |
|
3305 |
} |
|
3306 |
||
3307 |
/*! |
|
3308 |
\since 4.1 |
|
3309 |
||
3310 |
Returns the byte array converted to a \c long int using base \a |
|
3311 |
base, which is 10 by default and must be between 2 and 36, or 0. |
|
3312 |
||
3313 |
If \a base is 0, the base is determined automatically using the |
|
3314 |
following rules: If the byte array begins with "0x", it is assumed to |
|
3315 |
be hexadecimal; if it begins with "0", it is assumed to be octal; |
|
3316 |
otherwise it is assumed to be decimal. |
|
3317 |
||
3318 |
Returns 0 if the conversion fails. |
|
3319 |
||
3320 |
If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to |
|
3321 |
false; otherwise *\a{ok} is set to true. |
|
3322 |
||
3323 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 37 |
|
3324 |
||
3325 |
\note The conversion of the number is performed in the default C locale, |
|
3326 |
irrespective of the user's locale. |
|
3327 |
||
3328 |
\sa number() |
|
3329 |
*/ |
|
3330 |
long QByteArray::toLong(bool *ok, int base) const |
|
3331 |
{ |
|
3332 |
qlonglong v = toLongLong(ok, base); |
|
3333 |
if (v < LONG_MIN || v > LONG_MAX) { |
|
3334 |
if (ok) |
|
3335 |
*ok = false; |
|
3336 |
v = 0; |
|
3337 |
} |
|
3338 |
return long(v); |
|
3339 |
} |
|
3340 |
||
3341 |
/*! |
|
3342 |
\since 4.1 |
|
3343 |
||
3344 |
Returns the byte array converted to an \c {unsigned long int} using base \a |
|
3345 |
base, which is 10 by default and must be between 2 and 36, or 0. |
|
3346 |
||
3347 |
If \a base is 0, the base is determined automatically using the |
|
3348 |
following rules: If the byte array begins with "0x", it is assumed to |
|
3349 |
be hexadecimal; if it begins with "0", it is assumed to be octal; |
|
3350 |
otherwise it is assumed to be decimal. |
|
3351 |
||
3352 |
Returns 0 if the conversion fails. |
|
3353 |
||
3354 |
If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to |
|
3355 |
false; otherwise *\a{ok} is set to true. |
|
3356 |
||
3357 |
\note The conversion of the number is performed in the default C locale, |
|
3358 |
irrespective of the user's locale. |
|
3359 |
||
3360 |
\sa number() |
|
3361 |
*/ |
|
3362 |
ulong QByteArray::toULong(bool *ok, int base) const |
|
3363 |
{ |
|
3364 |
qulonglong v = toULongLong(ok, base); |
|
3365 |
if (v > ULONG_MAX) { |
|
3366 |
if (ok) |
|
3367 |
*ok = false; |
|
3368 |
v = 0; |
|
3369 |
} |
|
3370 |
return ulong(v); |
|
3371 |
} |
|
3372 |
||
3373 |
/*! |
|
3374 |
Returns the byte array converted to a \c short using base \a |
|
3375 |
base, which is 10 by default and must be between 2 and 36, or 0. |
|
3376 |
||
3377 |
If \a base is 0, the base is determined automatically using the |
|
3378 |
following rules: If the byte array begins with "0x", it is assumed to |
|
3379 |
be hexadecimal; if it begins with "0", it is assumed to be octal; |
|
3380 |
otherwise it is assumed to be decimal. |
|
3381 |
||
3382 |
Returns 0 if the conversion fails. |
|
3383 |
||
3384 |
If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to |
|
3385 |
false; otherwise *\a{ok} is set to true. |
|
3386 |
||
3387 |
\note The conversion of the number is performed in the default C locale, |
|
3388 |
irrespective of the user's locale. |
|
3389 |
||
3390 |
\sa number() |
|
3391 |
*/ |
|
3392 |
||
3393 |
short QByteArray::toShort(bool *ok, int base) const |
|
3394 |
{ |
|
3395 |
qlonglong v = toLongLong(ok, base); |
|
3396 |
if (v < SHRT_MIN || v > SHRT_MAX) { |
|
3397 |
if (ok) |
|
3398 |
*ok = false; |
|
3399 |
v = 0; |
|
3400 |
} |
|
3401 |
return short(v); |
|
3402 |
} |
|
3403 |
||
3404 |
/*! |
|
3405 |
Returns the byte array converted to an \c {unsigned short} using base \a |
|
3406 |
base, which is 10 by default and must be between 2 and 36, or 0. |
|
3407 |
||
3408 |
If \a base is 0, the base is determined automatically using the |
|
3409 |
following rules: If the byte array begins with "0x", it is assumed to |
|
3410 |
be hexadecimal; if it begins with "0", it is assumed to be octal; |
|
3411 |
otherwise it is assumed to be decimal. |
|
3412 |
||
3413 |
Returns 0 if the conversion fails. |
|
3414 |
||
3415 |
If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to |
|
3416 |
false; otherwise *\a{ok} is set to true. |
|
3417 |
||
3418 |
\note The conversion of the number is performed in the default C locale, |
|
3419 |
irrespective of the user's locale. |
|
3420 |
||
3421 |
\sa number() |
|
3422 |
*/ |
|
3423 |
||
3424 |
ushort QByteArray::toUShort(bool *ok, int base) const |
|
3425 |
{ |
|
3426 |
qulonglong v = toULongLong(ok, base); |
|
3427 |
if (v > USHRT_MAX) { |
|
3428 |
if (ok) |
|
3429 |
*ok = false; |
|
3430 |
v = 0; |
|
3431 |
} |
|
3432 |
return ushort(v); |
|
3433 |
} |
|
3434 |
||
3435 |
||
3436 |
/*! |
|
3437 |
Returns the byte array converted to a \c double value. |
|
3438 |
||
3439 |
Returns 0.0 if the conversion fails. |
|
3440 |
||
3441 |
If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to |
|
3442 |
false; otherwise *\a{ok} is set to true. |
|
3443 |
||
3444 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 38 |
|
3445 |
||
3446 |
\note The conversion of the number is performed in the default C locale, |
|
3447 |
irrespective of the user's locale. |
|
3448 |
||
3449 |
\sa number() |
|
3450 |
*/ |
|
3451 |
||
3452 |
double QByteArray::toDouble(bool *ok) const |
|
3453 |
{ |
|
3454 |
return QLocalePrivate::bytearrayToDouble(nulTerminated().constData(), ok); |
|
3455 |
} |
|
3456 |
||
3457 |
/*! |
|
3458 |
Returns the byte array converted to a \c float value. |
|
3459 |
||
3460 |
Returns 0.0 if the conversion fails. |
|
3461 |
||
3462 |
If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to |
|
3463 |
false; otherwise *\a{ok} is set to true. |
|
3464 |
||
3465 |
\note The conversion of the number is performed in the default C locale, |
|
3466 |
irrespective of the user's locale. |
|
3467 |
||
3468 |
\sa number() |
|
3469 |
*/ |
|
3470 |
||
3471 |
float QByteArray::toFloat(bool *ok) const |
|
3472 |
{ |
|
3473 |
return float(toDouble(ok)); |
|
3474 |
} |
|
3475 |
||
3476 |
/*! |
|
3477 |
Returns a copy of the byte array, encoded as Base64. |
|
3478 |
||
3479 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 39 |
|
3480 |
||
3481 |
The algorithm used to encode Base64-encoded data is defined in \l{RFC 2045}. |
|
3482 |
||
3483 |
\sa fromBase64() |
|
3484 |
*/ |
|
3485 |
QByteArray QByteArray::toBase64() const |
|
3486 |
{ |
|
3487 |
const char alphabet[] = "ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef" |
|
3488 |
"ghijklmn" "opqrstuv" "wxyz0123" "456789+/"; |
|
3489 |
const char padchar = '='; |
|
3490 |
int padlen = 0; |
|
3491 |
||
3492 |
QByteArray tmp((d->size * 4) / 3 + 3, Qt::Uninitialized); |
|
3493 |
||
3494 |
int i = 0; |
|
3495 |
char *out = tmp.data(); |
|
3496 |
while (i < d->size) { |
|
3497 |
int chunk = 0; |
|
3498 |
chunk |= int(uchar(d->data[i++])) << 16; |
|
3499 |
if (i == d->size) { |
|
3500 |
padlen = 2; |
|
3501 |
} else { |
|
3502 |
chunk |= int(uchar(d->data[i++])) << 8; |
|
3503 |
if (i == d->size) padlen = 1; |
|
3504 |
else chunk |= int(uchar(d->data[i++])); |
|
3505 |
} |
|
3506 |
||
3507 |
int j = (chunk & 0x00fc0000) >> 18; |
|
3508 |
int k = (chunk & 0x0003f000) >> 12; |
|
3509 |
int l = (chunk & 0x00000fc0) >> 6; |
|
3510 |
int m = (chunk & 0x0000003f); |
|
3511 |
*out++ = alphabet[j]; |
|
3512 |
*out++ = alphabet[k]; |
|
3513 |
if (padlen > 1) *out++ = padchar; |
|
3514 |
else *out++ = alphabet[l]; |
|
3515 |
if (padlen > 0) *out++ = padchar; |
|
3516 |
else *out++ = alphabet[m]; |
|
3517 |
} |
|
3518 |
||
3519 |
tmp.truncate(out - tmp.data()); |
|
3520 |
return tmp; |
|
3521 |
} |
|
3522 |
||
3523 |
/*! |
|
3524 |
\fn QByteArray &QByteArray::setNum(int n, int base) |
|
3525 |
||
3526 |
Sets the byte array to the printed value of \a n in base \a base (10 |
|
3527 |
by default) and returns a reference to the byte array. The \a base can |
|
3528 |
be any value between 2 and 36. |
|
3529 |
||
3530 |
Example: |
|
3531 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 40 |
|
3532 |
||
3533 |
\note The format of the number is not localized; the default C locale |
|
3534 |
is used irrespective of the user's locale. |
|
3535 |
||
3536 |
\sa number(), toInt() |
|
3537 |
*/ |
|
3538 |
||
3539 |
/*! |
|
3540 |
\fn QByteArray &QByteArray::setNum(uint n, int base) |
|
3541 |
\overload |
|
3542 |
||
3543 |
\sa toUInt() |
|
3544 |
*/ |
|
3545 |
||
3546 |
/*! |
|
3547 |
\fn QByteArray &QByteArray::setNum(short n, int base) |
|
3548 |
\overload |
|
3549 |
||
3550 |
\sa toShort() |
|
3551 |
*/ |
|
3552 |
||
3553 |
/*! |
|
3554 |
\fn QByteArray &QByteArray::setNum(ushort n, int base) |
|
3555 |
\overload |
|
3556 |
||
3557 |
\sa toUShort() |
|
3558 |
*/ |
|
3559 |
||
3560 |
/*! |
|
3561 |
\overload |
|
3562 |
||
3563 |
\sa toLongLong() |
|
3564 |
*/ |
|
3565 |
||
3566 |
QByteArray &QByteArray::setNum(qlonglong n, int base) |
|
3567 |
{ |
|
3568 |
#if defined(QT_CHECK_RANGE) |
|
3569 |
if (base < 2 || base > 36) { |
|
3570 |
qWarning("QByteArray::setNum: Invalid base %d", base); |
|
3571 |
base = 10; |
|
3572 |
} |
|
3573 |
#endif |
|
3574 |
QLocale locale(QLocale::C); |
|
3575 |
*this = locale.d()->longLongToString(n, -1, base).toLatin1(); |
|
3576 |
return *this; |
|
3577 |
} |
|
3578 |
||
3579 |
/*! |
|
3580 |
\overload |
|
3581 |
||
3582 |
\sa toULongLong() |
|
3583 |
*/ |
|
3584 |
||
3585 |
QByteArray &QByteArray::setNum(qulonglong n, int base) |
|
3586 |
{ |
|
3587 |
#if defined(QT_CHECK_RANGE) |
|
3588 |
if (base < 2 || base > 36) { |
|
3589 |
qWarning("QByteArray::setNum: Invalid base %d", base); |
|
3590 |
base = 10; |
|
3591 |
} |
|
3592 |
#endif |
|
3593 |
QLocale locale(QLocale::C); |
|
3594 |
*this = locale.d()->unsLongLongToString(n, -1, base).toLatin1(); |
|
3595 |
return *this; |
|
3596 |
} |
|
3597 |
||
3598 |
/*! |
|
3599 |
\overload |
|
3600 |
||
3601 |
Sets the byte array to the printed value of \a n, formatted in format |
|
3602 |
\a f with precision \a prec, and returns a reference to the |
|
3603 |
byte array. |
|
3604 |
||
3605 |
The format \a f can be any of the following: |
|
3606 |
||
3607 |
\table |
|
3608 |
\header \i Format \i Meaning |
|
3609 |
\row \i \c e \i format as [-]9.9e[+|-]999 |
|
3610 |
\row \i \c E \i format as [-]9.9E[+|-]999 |
|
3611 |
\row \i \c f \i format as [-]9.9 |
|
3612 |
\row \i \c g \i use \c e or \c f format, whichever is the most concise |
|
3613 |
\row \i \c G \i use \c E or \c f format, whichever is the most concise |
|
3614 |
\endtable |
|
3615 |
||
3616 |
With 'e', 'E', and 'f', \a prec is the number of digits after the |
|
3617 |
decimal point. With 'g' and 'G', \a prec is the maximum number of |
|
3618 |
significant digits (trailing zeroes are omitted). |
|
3619 |
||
3620 |
\note The format of the number is not localized; the default C locale |
|
3621 |
is used irrespective of the user's locale. |
|
3622 |
||
3623 |
\sa toDouble() |
|
3624 |
*/ |
|
3625 |
||
3626 |
QByteArray &QByteArray::setNum(double n, char f, int prec) |
|
3627 |
{ |
|
3628 |
QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal; |
|
3629 |
uint flags = 0; |
|
3630 |
||
3631 |
if (qIsUpper(f)) |
|
3632 |
flags = QLocalePrivate::CapitalEorX; |
|
3633 |
f = qToLower(f); |
|
3634 |
||
3635 |
switch (f) { |
|
3636 |
case 'f': |
|
3637 |
form = QLocalePrivate::DFDecimal; |
|
3638 |
break; |
|
3639 |
case 'e': |
|
3640 |
form = QLocalePrivate::DFExponent; |
|
3641 |
break; |
|
3642 |
case 'g': |
|
3643 |
form = QLocalePrivate::DFSignificantDigits; |
|
3644 |
break; |
|
3645 |
default: |
|
3646 |
#if defined(QT_CHECK_RANGE) |
|
3647 |
qWarning("QByteArray::setNum: Invalid format char '%c'", f); |
|
3648 |
#endif |
|
3649 |
break; |
|
3650 |
} |
|
3651 |
||
3652 |
QLocale locale(QLocale::C); |
|
3653 |
*this = locale.d()->doubleToString(n, prec, form, -1, flags).toLatin1(); |
|
3654 |
return *this; |
|
3655 |
} |
|
3656 |
||
3657 |
/*! |
|
3658 |
\fn QByteArray &QByteArray::setNum(float n, char f, int prec) |
|
3659 |
\overload |
|
3660 |
||
3661 |
Sets the byte array to the printed value of \a n, formatted in format |
|
3662 |
\a f with precision \a prec, and returns a reference to the |
|
3663 |
byte array. |
|
3664 |
||
3665 |
\note The format of the number is not localized; the default C locale |
|
3666 |
is used irrespective of the user's locale. |
|
3667 |
||
3668 |
\sa toFloat() |
|
3669 |
*/ |
|
3670 |
||
3671 |
/*! |
|
3672 |
Returns a byte array containing the string equivalent of the |
|
3673 |
number \a n to base \a base (10 by default). The \a base can be |
|
3674 |
any value between 2 and 36. |
|
3675 |
||
3676 |
Example: |
|
3677 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 41 |
|
3678 |
||
3679 |
\note The format of the number is not localized; the default C locale |
|
3680 |
is used irrespective of the user's locale. |
|
3681 |
||
3682 |
\sa setNum(), toInt() |
|
3683 |
*/ |
|
3684 |
QByteArray QByteArray::number(int n, int base) |
|
3685 |
{ |
|
3686 |
QByteArray s; |
|
3687 |
s.setNum(n, base); |
|
3688 |
return s; |
|
3689 |
} |
|
3690 |
||
3691 |
/*! |
|
3692 |
\overload |
|
3693 |
||
3694 |
\sa toUInt() |
|
3695 |
*/ |
|
3696 |
QByteArray QByteArray::number(uint n, int base) |
|
3697 |
{ |
|
3698 |
QByteArray s; |
|
3699 |
s.setNum(n, base); |
|
3700 |
return s; |
|
3701 |
} |
|
3702 |
||
3703 |
/*! |
|
3704 |
\overload |
|
3705 |
||
3706 |
\sa toLongLong() |
|
3707 |
*/ |
|
3708 |
QByteArray QByteArray::number(qlonglong n, int base) |
|
3709 |
{ |
|
3710 |
QByteArray s; |
|
3711 |
s.setNum(n, base); |
|
3712 |
return s; |
|
3713 |
} |
|
3714 |
||
3715 |
/*! |
|
3716 |
\overload |
|
3717 |
||
3718 |
\sa toULongLong() |
|
3719 |
*/ |
|
3720 |
QByteArray QByteArray::number(qulonglong n, int base) |
|
3721 |
{ |
|
3722 |
QByteArray s; |
|
3723 |
s.setNum(n, base); |
|
3724 |
return s; |
|
3725 |
} |
|
3726 |
||
3727 |
/*! |
|
3728 |
\overload |
|
3729 |
||
3730 |
Returns a byte array that contains the printed value of \a n, |
|
3731 |
formatted in format \a f with precision \a prec. |
|
3732 |
||
3733 |
Argument \a n is formatted according to the \a f format specified, |
|
3734 |
which is \c g by default, and can be any of the following: |
|
3735 |
||
3736 |
\table |
|
3737 |
\header \i Format \i Meaning |
|
3738 |
\row \i \c e \i format as [-]9.9e[+|-]999 |
|
3739 |
\row \i \c E \i format as [-]9.9E[+|-]999 |
|
3740 |
\row \i \c f \i format as [-]9.9 |
|
3741 |
\row \i \c g \i use \c e or \c f format, whichever is the most concise |
|
3742 |
\row \i \c G \i use \c E or \c f format, whichever is the most concise |
|
3743 |
\endtable |
|
3744 |
||
3745 |
With 'e', 'E', and 'f', \a prec is the number of digits after the |
|
3746 |
decimal point. With 'g' and 'G', \a prec is the maximum number of |
|
3747 |
significant digits (trailing zeroes are omitted). |
|
3748 |
||
3749 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 42 |
|
3750 |
||
3751 |
\note The format of the number is not localized; the default C locale |
|
3752 |
is used irrespective of the user's locale. |
|
3753 |
||
3754 |
\sa toDouble() |
|
3755 |
*/ |
|
3756 |
QByteArray QByteArray::number(double n, char f, int prec) |
|
3757 |
{ |
|
3758 |
QByteArray s; |
|
3759 |
s.setNum(n, f, prec); |
|
3760 |
return s; |
|
3761 |
} |
|
3762 |
||
3763 |
/*! |
|
3764 |
Constructs a QByteArray that uses the first \a size bytes of the |
|
3765 |
\a data array. The bytes are \e not copied. The QByteArray will |
|
3766 |
contain the \a data pointer. The caller guarantees that \a data |
|
3767 |
will not be deleted or modified as long as this QByteArray and any |
|
3768 |
copies of it exist that have not been modified. In other words, |
|
3769 |
because QByteArray is an \l{implicitly shared} class and the |
|
3770 |
instance returned by this function contains the \a data pointer, |
|
3771 |
the caller must not delete \a data or modify it directly as long |
|
3772 |
as the returned QByteArray and any copies exist. However, |
|
3773 |
QByteArray does not take ownership of \a data, so the QByteArray |
|
3774 |
destructor will never delete the raw \a data, even when the |
|
3775 |
last QByteArray referring to \a data is destroyed. |
|
3776 |
||
3777 |
A subsequent attempt to modify the contents of the returned |
|
3778 |
QByteArray or any copy made from it will cause it to create a deep |
|
3779 |
copy of the \a data array before doing the modification. This |
|
3780 |
ensures that the raw \a data array itself will never be modified |
|
3781 |
by QByteArray. |
|
3782 |
||
3783 |
Here is an example of how to read data using a QDataStream on raw |
|
3784 |
data in memory without copying the raw data into a QByteArray: |
|
3785 |
||
3786 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 43 |
|
3787 |
||
3788 |
\warning A byte array created with fromRawData() is \e not |
|
3789 |
null-terminated, unless the raw data contains a 0 character at |
|
3790 |
position \a size. While that does not matter for QDataStream or |
|
3791 |
functions like indexOf(), passing the byte array to a function |
|
3792 |
accepting a \c{const char *} expected to be '\\0'-terminated will |
|
3793 |
fail. |
|
3794 |
||
3795 |
\sa data(), constData() |
|
3796 |
*/ |
|
3797 |
||
3798 |
QByteArray QByteArray::fromRawData(const char *data, int size) |
|
3799 |
{ |
|
3800 |
Data *x = static_cast<Data *>(qMalloc(sizeof(Data))); |
|
3801 |
Q_CHECK_PTR(x); |
|
3802 |
if (data) { |
|
3803 |
x->data = const_cast<char *>(data); |
|
3804 |
} else { |
|
3805 |
x->data = x->array; |
|
3806 |
size = 0; |
|
3807 |
} |
|
3808 |
x->ref = 1; |
|
3809 |
x->alloc = x->size = size; |
|
3810 |
*x->array = '\0'; |
|
3811 |
return QByteArray(x, 0, 0); |
|
3812 |
} |
|
3813 |
||
3814 |
/*! |
|
3815 |
Returns a decoded copy of the Base64 array \a base64. Input is not checked |
|
3816 |
for validity; invalid characters in the input are skipped, enabling the |
|
3817 |
decoding process to continue with subsequent characters. |
|
3818 |
||
3819 |
For example: |
|
3820 |
||
3821 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 44 |
|
3822 |
||
3823 |
The algorithm used to decode Base64-encoded data is defined in \l{RFC 2045}. |
|
3824 |
||
3825 |
\sa toBase64() |
|
3826 |
*/ |
|
3827 |
QByteArray QByteArray::fromBase64(const QByteArray &base64) |
|
3828 |
{ |
|
3829 |
unsigned int buf = 0; |
|
3830 |
int nbits = 0; |
|
3831 |
QByteArray tmp((base64.size() * 3) / 4, Qt::Uninitialized); |
|
3832 |
||
3833 |
int offset = 0; |
|
3834 |
for (int i = 0; i < base64.size(); ++i) { |
|
3835 |
int ch = base64.at(i); |
|
3836 |
int d; |
|
3837 |
||
3838 |
if (ch >= 'A' && ch <= 'Z') |
|
3839 |
d = ch - 'A'; |
|
3840 |
else if (ch >= 'a' && ch <= 'z') |
|
3841 |
d = ch - 'a' + 26; |
|
3842 |
else if (ch >= '0' && ch <= '9') |
|
3843 |
d = ch - '0' + 52; |
|
3844 |
else if (ch == '+') |
|
3845 |
d = 62; |
|
3846 |
else if (ch == '/') |
|
3847 |
d = 63; |
|
3848 |
else |
|
3849 |
d = -1; |
|
3850 |
||
3851 |
if (d != -1) { |
|
3852 |
buf = (buf << 6) | d; |
|
3853 |
nbits += 6; |
|
3854 |
if (nbits >= 8) { |
|
3855 |
nbits -= 8; |
|
3856 |
tmp[offset++] = buf >> nbits; |
|
3857 |
buf &= (1 << nbits) - 1; |
|
3858 |
} |
|
3859 |
} |
|
3860 |
} |
|
3861 |
||
3862 |
tmp.truncate(offset); |
|
3863 |
return tmp; |
|
3864 |
} |
|
3865 |
||
3866 |
/*! |
|
3867 |
Returns a decoded copy of the hex encoded array \a hexEncoded. Input is not checked |
|
3868 |
for validity; invalid characters in the input are skipped, enabling the |
|
3869 |
decoding process to continue with subsequent characters. |
|
3870 |
||
3871 |
For example: |
|
3872 |
||
3873 |
\snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 45 |
|
3874 |
||
3875 |
\sa toHex() |
|
3876 |
*/ |
|
3877 |
QByteArray QByteArray::fromHex(const QByteArray &hexEncoded) |
|
3878 |
{ |
|
3879 |
QByteArray res((hexEncoded.size() + 1)/ 2, Qt::Uninitialized); |
|
3880 |
uchar *result = (uchar *)res.data() + res.size(); |
|
3881 |
||
3882 |
bool odd_digit = true; |
|
3883 |
for (int i = hexEncoded.size() - 1; i >= 0; --i) { |
|
3884 |
int ch = hexEncoded.at(i); |
|
3885 |
int tmp; |
|
3886 |
if (ch >= '0' && ch <= '9') |
|
3887 |
tmp = ch - '0'; |
|
3888 |
else if (ch >= 'a' && ch <= 'f') |
|
3889 |
tmp = ch - 'a' + 10; |
|
3890 |
else if (ch >= 'A' && ch <= 'F') |
|
3891 |
tmp = ch - 'A' + 10; |
|
3892 |
else |
|
3893 |
continue; |
|
3894 |
if (odd_digit) { |
|
3895 |
--result; |
|
3896 |
*result = tmp; |
|
3897 |
odd_digit = false; |
|
3898 |
} else { |
|
3899 |
*result |= tmp << 4; |
|
3900 |
odd_digit = true; |
|
3901 |
} |
|
3902 |
} |
|
3903 |
||
3904 |
res.remove(0, result - (const uchar *)res.constData()); |
|
3905 |
return res; |
|
3906 |
} |
|
3907 |
||
3908 |
/*! |
|
3909 |
Returns a hex encoded copy of the byte array. The hex encoding uses the numbers 0-9 and |
|
3910 |
the letters a-f. |
|
3911 |
||
3912 |
\sa fromHex() |
|
3913 |
*/ |
|
3914 |
QByteArray QByteArray::toHex() const |
|
3915 |
{ |
|
3916 |
QByteArray hex(d->size * 2, Qt::Uninitialized); |
|
3917 |
char *hexData = hex.data(); |
|
3918 |
const uchar *data = (const uchar *)d->data; |
|
3919 |
for (int i = 0; i < d->size; ++i) { |
|
3920 |
int j = (data[i] >> 4) & 0xf; |
|
3921 |
if (j <= 9) |
|
3922 |
hexData[i*2] = (j + '0'); |
|
3923 |
else |
|
3924 |
hexData[i*2] = (j + 'a' - 10); |
|
3925 |
j = data[i] & 0xf; |
|
3926 |
if (j <= 9) |
|
3927 |
hexData[i*2+1] = (j + '0'); |
|
3928 |
else |
|
3929 |
hexData[i*2+1] = (j + 'a' - 10); |
|
3930 |
} |
|
3931 |
return hex; |
|
3932 |
} |
|
3933 |
||
3934 |
static void q_fromPercentEncoding(QByteArray *ba, char percent) |
|
3935 |
{ |
|
3936 |
if (ba->isEmpty()) |
|
3937 |
return; |
|
3938 |
||
3939 |
char *data = ba->data(); |
|
3940 |
const char *inputPtr = data; |
|
3941 |
||
3942 |
int i = 0; |
|
3943 |
int len = ba->count(); |
|
3944 |
int outlen = 0; |
|
3945 |
int a, b; |
|
3946 |
char c; |
|
3947 |
while (i < len) { |
|
3948 |
c = inputPtr[i]; |
|
3949 |
if (c == percent && i + 2 < len) { |
|
3950 |
a = inputPtr[++i]; |
|
3951 |
b = inputPtr[++i]; |
|
3952 |
||
3953 |
if (a >= '0' && a <= '9') a -= '0'; |
|
3954 |
else if (a >= 'a' && a <= 'f') a = a - 'a' + 10; |
|
3955 |
else if (a >= 'A' && a <= 'F') a = a - 'A' + 10; |
|
3956 |
||
3957 |
if (b >= '0' && b <= '9') b -= '0'; |
|
3958 |
else if (b >= 'a' && b <= 'f') b = b - 'a' + 10; |
|
3959 |
else if (b >= 'A' && b <= 'F') b = b - 'A' + 10; |
|
3960 |
||
3961 |
*data++ = (char)((a << 4) | b); |
|
3962 |
} else { |
|
3963 |
*data++ = c; |
|
3964 |
} |
|
3965 |
||
3966 |
++i; |
|
3967 |
++outlen; |
|
3968 |
} |
|
3969 |
||
3970 |
if (outlen != len) |
|
3971 |
ba->truncate(outlen); |
|
3972 |
} |
|
3973 |
||
3974 |
void q_fromPercentEncoding(QByteArray *ba) |
|
3975 |
{ |
|
3976 |
q_fromPercentEncoding(ba, '%'); |
|
3977 |
} |
|
3978 |
||
3979 |
/*! |
|
3980 |
\since 4.4 |
|
3981 |
||
3982 |
Returns a decoded copy of the URI/URL-style percent-encoded \a input. |
|
3983 |
The \a percent parameter allows you to replace the '%' character for |
|
3984 |
another (for instance, '_' or '='). |
|
3985 |
||
3986 |
For example: |
|
3987 |
\code |
|
3988 |
QByteArray text = QByteArray::fromPercentEncoding("Qt%20is%20great%33"); |
|
3989 |
text.data(); // returns "Qt is great!" |
|
3990 |
\endcode |
|
3991 |
||
3992 |
\sa toPercentEncoding(), QUrl::fromPercentEncoding() |
|
3993 |
*/ |
|
3994 |
QByteArray QByteArray::fromPercentEncoding(const QByteArray &input, char percent) |
|
3995 |
{ |
|
3996 |
if (input.isNull()) |
|
3997 |
return QByteArray(); // preserve null |
|
3998 |
if (input.isEmpty()) |
|
3999 |
return QByteArray(input.data(), 0); |
|
4000 |
||
4001 |
QByteArray tmp = input; |
|
4002 |
q_fromPercentEncoding(&tmp, percent); |
|
4003 |
return tmp; |
|
4004 |
} |
|
4005 |
||
4006 |
static inline bool q_strchr(const char str[], char chr) |
|
4007 |
{ |
|
4008 |
if (!str) return false; |
|
4009 |
||
4010 |
const char *ptr = str; |
|
4011 |
char c; |
|
4012 |
while ((c = *ptr++)) |
|
4013 |
if (c == chr) |
|
4014 |
return true; |
|
4015 |
return false; |
|
4016 |
} |
|
4017 |
||
4018 |
static inline char toHexHelper(char c) |
|
4019 |
{ |
|
4020 |
static const char hexnumbers[] = "0123456789ABCDEF"; |
|
4021 |
return hexnumbers[c & 0xf]; |
|
4022 |
} |
|
4023 |
||
4024 |
static void q_toPercentEncoding(QByteArray *ba, const char *dontEncode, const char *alsoEncode, char percent) |
|
4025 |
{ |
|
4026 |
if (ba->isEmpty()) |
|
4027 |
return; |
|
4028 |
||
4029 |
QByteArray input = *ba; |
|
4030 |
int len = input.count(); |
|
4031 |
const char *inputData = input.constData(); |
|
4032 |
char *output = 0; |
|
4033 |
int length = 0; |
|
4034 |
||
4035 |
for (int i = 0; i < len; ++i) { |
|
4036 |
unsigned char c = *inputData++; |
|
4037 |
if (((c >= 0x61 && c <= 0x7A) // ALPHA |
|
4038 |
|| (c >= 0x41 && c <= 0x5A) // ALPHA |
|
4039 |
|| (c >= 0x30 && c <= 0x39) // DIGIT |
|
4040 |
|| c == 0x2D // - |
|
4041 |
|| c == 0x2E // . |
|
4042 |
|| c == 0x5F // _ |
|
4043 |
|| c == 0x7E // ~ |
|
4044 |
|| q_strchr(dontEncode, c)) |
|
4045 |
&& !q_strchr(alsoEncode, c)) { |
|
4046 |
if (output) |
|
4047 |
output[length] = c; |
|
4048 |
++length; |
|
4049 |
} else { |
|
4050 |
if (!output) { |
|
4051 |
// detach now |
|
4052 |
ba->resize(len*3); // worst case |
|
4053 |
output = ba->data(); |
|
4054 |
} |
|
4055 |
output[length++] = percent; |
|
4056 |
output[length++] = toHexHelper((c & 0xf0) >> 4); |
|
4057 |
output[length++] = toHexHelper(c & 0xf); |
|
4058 |
} |
|
4059 |
} |
|
4060 |
if (output) |
|
4061 |
ba->truncate(length); |
|
4062 |
} |
|
4063 |
||
4064 |
void q_toPercentEncoding(QByteArray *ba, const char *exclude, const char *include) |
|
4065 |
{ |
|
4066 |
q_toPercentEncoding(ba, exclude, include, '%'); |
|
4067 |
} |
|
4068 |
||
4069 |
void q_normalizePercentEncoding(QByteArray *ba, const char *exclude) |
|
4070 |
{ |
|
4071 |
q_fromPercentEncoding(ba, '%'); |
|
4072 |
q_toPercentEncoding(ba, exclude, 0, '%'); |
|
4073 |
} |
|
4074 |
||
4075 |
/*! |
|
4076 |
\since 4.4 |
|
4077 |
||
4078 |
Returns a URI/URL-style percent-encoded copy of this byte array. The |
|
4079 |
\a percent parameter allows you to override the default '%' |
|
4080 |
character for another. |
|
4081 |
||
4082 |
By default, this function will encode all characters that are not |
|
4083 |
one of the following: |
|
4084 |
||
4085 |
ALPHA ("a" to "z" and "A" to "Z") / DIGIT (0 to 9) / "-" / "." / "_" / "~" |
|
4086 |
||
4087 |
To prevent characters from being encoded pass them to \a |
|
4088 |
exclude. To force characters to be encoded pass them to \a |
|
4089 |
include. The \a percent character is always encoded. |
|
4090 |
||
4091 |
Example: |
|
4092 |
||
4093 |
\code |
|
4094 |
QByteArray text = "{a fishy string?}"; |
|
4095 |
QByteArray ba = text.toPercentEncoding("{}", "s"); |
|
4096 |
qDebug(ba.constData()); |
|
4097 |
// prints "{a fi%73hy %73tring%3F}" |
|
4098 |
\endcode |
|
4099 |
||
4100 |
The hex encoding uses the numbers 0-9 and the uppercase letters A-F. |
|
4101 |
||
4102 |
\sa fromPercentEncoding(), QUrl::toPercentEncoding() |
|
4103 |
*/ |
|
4104 |
QByteArray QByteArray::toPercentEncoding(const QByteArray &exclude, const QByteArray &include, |
|
4105 |
char percent) const |
|
4106 |
{ |
|
4107 |
if (isNull()) |
|
4108 |
return QByteArray(); // preserve null |
|
4109 |
if (isEmpty()) |
|
4110 |
return QByteArray(data(), 0); |
|
4111 |
||
4112 |
QByteArray include2 = include; |
|
4113 |
if (percent != '%') // the default |
|
4114 |
if ((percent >= 0x61 && percent <= 0x7A) // ALPHA |
|
4115 |
|| (percent >= 0x41 && percent <= 0x5A) // ALPHA |
|
4116 |
|| (percent >= 0x30 && percent <= 0x39) // DIGIT |
|
4117 |
|| percent == 0x2D // - |
|
4118 |
|| percent == 0x2E // . |
|
4119 |
|| percent == 0x5F // _ |
|
4120 |
|| percent == 0x7E) // ~ |
|
4121 |
include2 += percent; |
|
4122 |
||
4123 |
QByteArray result = *this; |
|
4124 |
q_toPercentEncoding(&result, exclude.nulTerminated().constData(), include2.nulTerminated().constData(), percent); |
|
4125 |
||
4126 |
return result; |
|
4127 |
} |
|
4128 |
||
4129 |
/*! \typedef QByteArray::ConstIterator |
|
4130 |
\internal |
|
4131 |
*/ |
|
4132 |
||
4133 |
/*! \typedef QByteArray::Iterator |
|
4134 |
\internal |
|
4135 |
*/ |
|
4136 |
||
4137 |
/*! \typedef QByteArray::const_iterator |
|
4138 |
\internal |
|
4139 |
*/ |
|
4140 |
||
4141 |
/*! \typedef QByteArray::iterator |
|
4142 |
\internal |
|
4143 |
*/ |
|
4144 |
||
4145 |
/*! \typedef QByteArray::const_reference |
|
4146 |
\internal |
|
4147 |
*/ |
|
4148 |
||
4149 |
/*! \typedef QByteArray::reference |
|
4150 |
\internal |
|
4151 |
*/ |
|
4152 |
||
4153 |
/*! \typedef QByteArray::value_type |
|
4154 |
\internal |
|
4155 |
*/ |
|
4156 |
||
4157 |
/*! |
|
4158 |
\fn QByteArray::QByteArray(int size) |
|
4159 |
||
4160 |
Use QByteArray(int, char) instead. |
|
4161 |
*/ |
|
4162 |
||
4163 |
||
4164 |
/*! |
|
4165 |
\fn QByteArray QByteArray::leftJustify(uint width, char fill, bool truncate) const |
|
4166 |
||
4167 |
Use leftJustified() instead. |
|
4168 |
*/ |
|
4169 |
||
4170 |
/*! |
|
4171 |
\fn QByteArray QByteArray::rightJustify(uint width, char fill, bool truncate) const |
|
4172 |
||
4173 |
Use rightJustified() instead. |
|
4174 |
*/ |
|
4175 |
||
4176 |
/*! |
|
4177 |
\fn QByteArray& QByteArray::duplicate(const QByteArray& a) |
|
4178 |
||
4179 |
\oldcode |
|
4180 |
QByteArray bdata; |
|
4181 |
bdata.duplicate(original); |
|
4182 |
\newcode |
|
4183 |
QByteArray bdata; |
|
4184 |
bdata = original; |
|
4185 |
\endcode |
|
4186 |
||
4187 |
\note QByteArray uses implicit sharing so if you modify a copy, only the |
|
4188 |
copy is changed. |
|
4189 |
*/ |
|
4190 |
||
4191 |
/*! |
|
4192 |
\fn QByteArray& QByteArray::duplicate(const char *a, uint n) |
|
4193 |
||
4194 |
\overload |
|
4195 |
||
4196 |
\oldcode |
|
4197 |
QByteArray bdata; |
|
4198 |
bdata.duplicate(ptr, size); |
|
4199 |
\newcode |
|
4200 |
QByteArray bdata; |
|
4201 |
bdata = QByteArray(ptr, size); |
|
4202 |
\endcode |
|
4203 |
||
4204 |
\note QByteArray uses implicit sharing so if you modify a copy, only the |
|
4205 |
copy is changed. |
|
4206 |
*/ |
|
4207 |
||
4208 |
/*! |
|
4209 |
\fn QByteArray& QByteArray::setRawData(const char *a, uint n) |
|
4210 |
||
4211 |
Use fromRawData() instead. |
|
4212 |
*/ |
|
4213 |
||
4214 |
/*! |
|
4215 |
\fn void QByteArray::resetRawData(const char *data, uint n) |
|
4216 |
||
4217 |
Use clear() instead. |
|
4218 |
*/ |
|
4219 |
||
4220 |
/*! |
|
4221 |
\fn QByteArray QByteArray::lower() const |
|
4222 |
||
4223 |
Use toLower() instead. |
|
4224 |
*/ |
|
4225 |
||
4226 |
/*! |
|
4227 |
\fn QByteArray QByteArray::upper() const |
|
4228 |
||
4229 |
Use toUpper() instead. |
|
4230 |
*/ |
|
4231 |
||
4232 |
/*! |
|
4233 |
\fn QByteArray QByteArray::stripWhiteSpace() const |
|
4234 |
||
4235 |
Use trimmed() instead. |
|
4236 |
*/ |
|
4237 |
||
4238 |
/*! |
|
4239 |
\fn QByteArray QByteArray::simplifyWhiteSpace() const |
|
4240 |
||
4241 |
Use simplified() instead. |
|
4242 |
*/ |
|
4243 |
||
4244 |
/*! |
|
4245 |
\fn int QByteArray::find(char c, int from = 0) const |
|
4246 |
||
4247 |
Use indexOf() instead. |
|
4248 |
*/ |
|
4249 |
||
4250 |
/*! |
|
4251 |
\fn int QByteArray::find(const char *c, int from = 0) const |
|
4252 |
||
4253 |
Use indexOf() instead. |
|
4254 |
*/ |
|
4255 |
||
4256 |
/*! |
|
4257 |
\fn int QByteArray::find(const QByteArray &ba, int from = 0) const |
|
4258 |
||
4259 |
Use indexOf() instead. |
|
4260 |
*/ |
|
4261 |
||
4262 |
/*! |
|
4263 |
\fn int QByteArray::findRev(char c, int from = -1) const |
|
4264 |
||
4265 |
Use lastIndexOf() instead. |
|
4266 |
*/ |
|
4267 |
||
4268 |
/*! |
|
4269 |
\fn int QByteArray::findRev(const char *c, int from = -1) const |
|
4270 |
||
4271 |
Use lastIndexOf() instead. |
|
4272 |
*/ |
|
4273 |
||
4274 |
/*! |
|
4275 |
\fn int QByteArray::findRev(const QByteArray &ba, int from = -1) const |
|
4276 |
||
4277 |
Use lastIndexOf() instead. |
|
4278 |
*/ |
|
4279 |
||
4280 |
/*! |
|
4281 |
\fn int QByteArray::find(const QString &s, int from = 0) const |
|
4282 |
||
4283 |
Use indexOf() instead. |
|
4284 |
*/ |
|
4285 |
||
4286 |
/*! |
|
4287 |
\fn int QByteArray::findRev(const QString &s, int from = -1) const |
|
4288 |
||
4289 |
Use lastIndexOf() instead. |
|
4290 |
*/ |
|
4291 |
||
4292 |
/*! |
|
4293 |
\fn DataPtr &QByteArray::data_ptr() |
|
4294 |
\internal |
|
4295 |
*/ |
|
4296 |
||
4297 |
/*! |
|
4298 |
\typedef QByteArray::DataPtr |
|
4299 |
\internal |
|
4300 |
*/ |
|
4301 |
||
4302 |
QT_END_NAMESPACE |