Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/tools/qbytearray.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||
4 | ** Copyright (C) 2016 Intel Corporation. | - | ||||||||||||
5 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
6 | ** | - | ||||||||||||
7 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||
8 | ** | - | ||||||||||||
9 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||
10 | ** Commercial License Usage | - | ||||||||||||
11 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
12 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
13 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
14 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
15 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
16 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||
17 | ** | - | ||||||||||||
18 | ** GNU Lesser General Public License Usage | - | ||||||||||||
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
20 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||
21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||
22 | ** packaging of this file. Please review the following information to | - | ||||||||||||
23 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||
24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||
25 | ** | - | ||||||||||||
26 | ** GNU General Public License Usage | - | ||||||||||||
27 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
28 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||
29 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
30 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
32 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
33 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
35 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
36 | ** | - | ||||||||||||
37 | ** $QT_END_LICENSE$ | - | ||||||||||||
38 | ** | - | ||||||||||||
39 | ****************************************************************************/ | - | ||||||||||||
40 | - | |||||||||||||
41 | #include "qbytearray.h" | - | ||||||||||||
42 | #include "qbytearraymatcher.h" | - | ||||||||||||
43 | #include "qtools_p.h" | - | ||||||||||||
44 | #include "qstring.h" | - | ||||||||||||
45 | #include "qlist.h" | - | ||||||||||||
46 | #include "qlocale.h" | - | ||||||||||||
47 | #include "qlocale_p.h" | - | ||||||||||||
48 | #include "qlocale_tools_p.h" | - | ||||||||||||
49 | #include "private/qnumeric_p.h" | - | ||||||||||||
50 | #include "qstringalgorithms_p.h" | - | ||||||||||||
51 | #include "qscopedpointer.h" | - | ||||||||||||
52 | #include "qbytearray_p.h" | - | ||||||||||||
53 | #include <qdatastream.h> | - | ||||||||||||
54 | #include <qmath.h> | - | ||||||||||||
55 | - | |||||||||||||
56 | #ifndef QT_NO_COMPRESS | - | ||||||||||||
57 | #include <zconf.h> | - | ||||||||||||
58 | #include <zlib.h> | - | ||||||||||||
59 | #endif | - | ||||||||||||
60 | #include <ctype.h> | - | ||||||||||||
61 | #include <limits.h> | - | ||||||||||||
62 | #include <string.h> | - | ||||||||||||
63 | #include <stdlib.h> | - | ||||||||||||
64 | - | |||||||||||||
65 | #define IS_RAW_DATA(d) ((d)->offset != sizeof(QByteArrayData)) | - | ||||||||||||
66 | - | |||||||||||||
67 | QT_BEGIN_NAMESPACE | - | ||||||||||||
68 | - | |||||||||||||
69 | // Latin 1 case system, used by QByteArray::to{Upper,Lower}() and qstr(n)icmp(): | - | ||||||||||||
70 | /* | - | ||||||||||||
71 | #!/usr/bin/perl -l | - | ||||||||||||
72 | use feature "unicode_strings"; | - | ||||||||||||
73 | for (0..255) { | - | ||||||||||||
74 | $up = uc(chr($_)); | - | ||||||||||||
75 | $up = chr($_) if ord($up) > 0x100 || length $up > 1; | - | ||||||||||||
76 | printf "0x%02x,", ord($up); | - | ||||||||||||
77 | print "" if ($_ & 0xf) == 0xf; | - | ||||||||||||
78 | } | - | ||||||||||||
79 | */ | - | ||||||||||||
80 | static const uchar latin1_uppercased[256] = { | - | ||||||||||||
81 | 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f, | - | ||||||||||||
82 | 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f, | - | ||||||||||||
83 | 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f, | - | ||||||||||||
84 | 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f, | - | ||||||||||||
85 | 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f, | - | ||||||||||||
86 | 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f, | - | ||||||||||||
87 | 0x60,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f, | - | ||||||||||||
88 | 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x7b,0x7c,0x7d,0x7e,0x7f, | - | ||||||||||||
89 | 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f, | - | ||||||||||||
90 | 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f, | - | ||||||||||||
91 | 0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf, | - | ||||||||||||
92 | 0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf, | - | ||||||||||||
93 | 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf, | - | ||||||||||||
94 | 0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf, | - | ||||||||||||
95 | 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf, | - | ||||||||||||
96 | 0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xf7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xff | - | ||||||||||||
97 | }; | - | ||||||||||||
98 | - | |||||||||||||
99 | /* | - | ||||||||||||
100 | #!/usr/bin/perl -l | - | ||||||||||||
101 | use feature "unicode_strings"; | - | ||||||||||||
102 | for (0..255) { | - | ||||||||||||
103 | $up = lc(chr($_)); | - | ||||||||||||
104 | $up = chr($_) if ord($up) > 0x100 || length $up > 1; | - | ||||||||||||
105 | printf "0x%02x,", ord($up); | - | ||||||||||||
106 | print "" if ($_ & 0xf) == 0xf; | - | ||||||||||||
107 | } | - | ||||||||||||
108 | */ | - | ||||||||||||
109 | static const uchar latin1_lowercased[256] = { | - | ||||||||||||
110 | 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f, | - | ||||||||||||
111 | 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f, | - | ||||||||||||
112 | 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f, | - | ||||||||||||
113 | 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f, | - | ||||||||||||
114 | 0x40,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f, | - | ||||||||||||
115 | 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x5b,0x5c,0x5d,0x5e,0x5f, | - | ||||||||||||
116 | 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f, | - | ||||||||||||
117 | 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f, | - | ||||||||||||
118 | 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f, | - | ||||||||||||
119 | 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f, | - | ||||||||||||
120 | 0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf, | - | ||||||||||||
121 | 0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf, | - | ||||||||||||
122 | 0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef, | - | ||||||||||||
123 | 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xd7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xdf, | - | ||||||||||||
124 | 0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef, | - | ||||||||||||
125 | 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff | - | ||||||||||||
126 | }; | - | ||||||||||||
127 | - | |||||||||||||
128 | int qFindByteArray( | - | ||||||||||||
129 | const char *haystack0, int haystackLen, int from, | - | ||||||||||||
130 | const char *needle0, int needleLen); | - | ||||||||||||
131 | - | |||||||||||||
int qAllocMore/* | ||||||||||||||
133 | * This pair of functions is declared in qtools_p.h and is used by the Qt | - | ||||||||||||
134 | * containers to allocate memory and grow the memory block during append | - | ||||||||||||
135 | * operations. | - | ||||||||||||
136 | * | - | ||||||||||||
137 | * They take size_t parameters and return size_t so they will change sizes | - | ||||||||||||
138 | * according to the pointer width. However, knowing Qt containers store the | - | ||||||||||||
139 | * container size and element indexes in ints, these functions never return a | - | ||||||||||||
140 | * size larger than INT_MAX. This is done by casting the element count and | - | ||||||||||||
141 | * memory block size to int in several comparisons: the check for negative is | - | ||||||||||||
142 | * very fast on most platforms as the code only needs to check the sign bit. | - | ||||||||||||
143 | * | - | ||||||||||||
144 | * These functions return SIZE_MAX on overflow, which can be passed to malloc() | - | ||||||||||||
145 | * and will surely cause a NULL return (there's no way you can allocate a | - | ||||||||||||
146 | * memory block the size of your entire VM space). | - | ||||||||||||
147 | */ | - | ||||||||||||
148 | - | |||||||||||||
149 | /*! | - | ||||||||||||
150 | \internal | - | ||||||||||||
151 | \since 5.7 | - | ||||||||||||
152 | - | |||||||||||||
153 | Returns the memory block size for a container containing \a elementCount | - | ||||||||||||
154 | elements, each of \a elementSize bytes, plus a header of \a headerSize | - | ||||||||||||
155 | bytes. That is, this function returns \c | - | ||||||||||||
156 | {elementCount * elementSize + headerSize} | - | ||||||||||||
157 | - | |||||||||||||
158 | but unlike the simple calculation, it checks for overflows during the | - | ||||||||||||
159 | multiplication and the addition. | - | ||||||||||||
160 | - | |||||||||||||
161 | Both \a elementCount and \a headerSize can be zero, but \a elementSize | - | ||||||||||||
162 | cannot. | - | ||||||||||||
163 | - | |||||||||||||
164 | This function returns SIZE_MAX (~0) on overflow or if the memory block size | - | ||||||||||||
165 | would not fit an int. | - | ||||||||||||
166 | */ | - | ||||||||||||
167 | size_t qCalculateBlockSize(int allocsize_t elementCount, int extrasize_t elementSize, size_t headerSize) Q_DECL_NOTHROW | - | ||||||||||||
168 | { | - | ||||||||||||
169 | Q_ASSERTunsigned count = unsigned(alloc >= 0 && extra >= 0 && extra <= MaxAllocSizeelementCount); | - | ||||||||||||
170 | Q_ASSERT_X(alloc <= MaxAllocSize - extra, "qAllocMore", "Requested unsigned size is too large!"= unsigned(elementSize); | - | ||||||||||||
171 | unsigned nallocheader = qNextPowerOfTwounsigned(alloc + extraheaderSize); | - | ||||||||||||
172 | Q_ASSERT(nalloc >elementSize); | - | ||||||||||||
173 | Q_ASSERT(size == elementSize); | - | ||||||||||||
174 | Q_ASSERT(header == headerSize); | - | ||||||||||||
175 | - | |||||||||||||
176 | if (Q_UNLIKELY(count != elementCount))
| 1-114251311 | ||||||||||||
177 | return std::numeric_limits<size_t>::max(); executed 1 time by 1 test: return std::numeric_limits<size_t>::max(); Executed by:
| 1 | ||||||||||||
178 | - | |||||||||||||
179 | unsigned bytes; | - | ||||||||||||
180 | if
| 2-114251309 | ||||||||||||
181 | Q_UNLIKELY(add_overflow(bytes, header, &bytes)))
| 1-114251308 | ||||||||||||
182 | return std::numeric_limits<size_t>::max(); executed 3 times by 1 test: return std::numeric_limits<size_t>::max(); Executed by:
| 3 | ||||||||||||
183 | if (Q_UNLIKELY(int(bytes) < 0))
| 13-114251295 | ||||||||||||
184 | executed 13 times by 1 test: return std::numeric_limits<size_t>::max();return std::numeric_limits<size_t>::max(); Executed by:
executed 13 times by 1 test: return std::numeric_limits<size_t>::max(); Executed by:
| 13 | ||||||||||||
185 | - | |||||||||||||
186 | return bytes; executed 114250814 times by 781 tests: return bytes; Executed by:
| 114250814 | ||||||||||||
187 | } | - | ||||||||||||
188 | - | |||||||||||||
189 | /*! | - | ||||||||||||
190 | \internal | - | ||||||||||||
191 | \since 5.7 | - | ||||||||||||
192 | - | |||||||||||||
193 | Returns the memory block size and the number of elements that will fit in | - | ||||||||||||
194 | that block for a container containing \a elementCount elements, each of \a | - | ||||||||||||
195 | elementSize bytes, plus a header of \a headerSize bytes. This function | - | ||||||||||||
196 | assumes the container will grow and pre-allocates a growth factor. | - | ||||||||||||
197 | - | |||||||||||||
198 | Both \a elementCount and \a headerSize can be zero, but \a elementSize | - | ||||||||||||
199 | cannot. | - | ||||||||||||
200 | - | |||||||||||||
201 | This function returns SIZE_MAX (~0) on overflow or if the memory block size | - | ||||||||||||
202 | would not fit an int. | - | ||||||||||||
203 | - | |||||||||||||
204 | \note The memory block may contain up to \a elementSize - 1 bytes more than | - | ||||||||||||
205 | needed. | - | ||||||||||||
206 | */ | - | ||||||||||||
207 | CalculateGrowingBlockSizeResult | - | ||||||||||||
208 | qCalculateGrowingBlockSize(size_t elementCount, size_t elementSize, size_t headerSize) Q_DECL_NOTHROW | - | ||||||||||||
209 | { | - | ||||||||||||
210 | CalculateGrowingBlockSizeResult result = { | - | ||||||||||||
211 | std::numeric_limits<size_t>::max(),std::numeric_limits<size_t>::max() | - | ||||||||||||
212 | }; | - | ||||||||||||
213 | - | |||||||||||||
214 | unsigned bytes = unsigned(qCalculateBlockSize(elementCount, elementSize, headerSize)); | - | ||||||||||||
215 | if (int(bytes) < 0) // catches std::numeric_limits<size_t>::max()
| 10-30345637 | ||||||||||||
216 | return nallocresult; executed 10 times by 1 test: return result; Executed by:
| 10 | ||||||||||||
217 | - | |||||||||||||
218 | unsigned morebytes = qNextPowerOfTwo(bytes); | - | ||||||||||||
219 | if (Q_UNLIKELY(int(morebytes) < 0)) {
| 444-30345193 | ||||||||||||
220 | // catches morebytes == 2GB | - | ||||||||||||
221 | // grow by half the difference between bytes and morebytes | - | ||||||||||||
222 | bytes += (morebytes - extrabytes) / 2; | - | ||||||||||||
223 | } else { executed 444 times by 1 test: end of block Executed by:
| 444 | ||||||||||||
224 | bytes = morebytes; | - | ||||||||||||
225 | } executed 30345193 times by 724 tests: end of block Executed by:
| 30345193 | ||||||||||||
226 | - | |||||||||||||
227 | result.elementCount = (bytes - unsigned(headerSize)) / unsigned(elementSize); | - | ||||||||||||
228 | result.size = bytes; | - | ||||||||||||
229 | return result executed 30345637 times by 724 tests: ;return result; Executed by:
executed 30345637 times by 724 tests: return result; Executed by:
| 30345637 | ||||||||||||
230 | } | - | ||||||||||||
231 | - | |||||||||||||
232 | /***************************************************************************** | - | ||||||||||||
233 | Safe and portable C string functions; extensions to standard string.h | - | ||||||||||||
234 | *****************************************************************************/ | - | ||||||||||||
235 | - | |||||||||||||
236 | /*! \relates QByteArray | - | ||||||||||||
237 | - | |||||||||||||
238 | Returns a duplicate string. | - | ||||||||||||
239 | - | |||||||||||||
240 | Allocates space for a copy of \a src, copies it, and returns a | - | ||||||||||||
241 | pointer to the copy. If \a src is 0, it immediately returns 0. | - | ||||||||||||
242 | - | |||||||||||||
243 | Ownership is passed to the caller, so the returned string must be | - | ||||||||||||
244 | deleted using \c delete[]. | - | ||||||||||||
245 | */ | - | ||||||||||||
246 | - | |||||||||||||
247 | char *qstrdup(const char *src) | - | ||||||||||||
248 | { | - | ||||||||||||
249 | if (!src) | - | ||||||||||||
250 | return 0; | - | ||||||||||||
251 | char *dst = new char[strlen(src) + 1]; | - | ||||||||||||
252 | return qstrcpy(dst, src); | - | ||||||||||||
253 | } | - | ||||||||||||
254 | - | |||||||||||||
255 | /*! \relates QByteArray | - | ||||||||||||
256 | - | |||||||||||||
257 | Copies all the characters up to and including the '\\0' from \a | - | ||||||||||||
258 | src into \a dst and returns a pointer to \a dst. If \a src is 0, | - | ||||||||||||
259 | it immediately returns 0. | - | ||||||||||||
260 | - | |||||||||||||
261 | This function assumes that \a dst is large enough to hold the | - | ||||||||||||
262 | contents of \a src. | - | ||||||||||||
263 | - | |||||||||||||
264 | \sa qstrncpy() | - | ||||||||||||
265 | */ | - | ||||||||||||
266 | - | |||||||||||||
267 | char *qstrcpy(char *dst, const char *src) | - | ||||||||||||
268 | { | - | ||||||||||||
269 | if (!src) | - | ||||||||||||
270 | return 0; | - | ||||||||||||
271 | #if defined(_MSC_VER) && _MSC_VER >= 1400 | - | ||||||||||||
272 | const int len = int(strlen(src)); | - | ||||||||||||
273 | // This is actually not secure!!! It will be fixed | - | ||||||||||||
274 | // properly in a later release! | - | ||||||||||||
275 | if (len >= 0 && strcpy_s(dst, len+1, src) == 0) | - | ||||||||||||
276 | return dst; | - | ||||||||||||
277 | return 0; | - | ||||||||||||
278 | #else | - | ||||||||||||
279 | return strcpy(dst, src); | - | ||||||||||||
280 | #endif | - | ||||||||||||
281 | } | - | ||||||||||||
282 | - | |||||||||||||
283 | /*! \relates QByteArray | - | ||||||||||||
284 | - | |||||||||||||
285 | A safe \c strncpy() function. | - | ||||||||||||
286 | - | |||||||||||||
287 | Copies at most \a len bytes from \a src (stopping at \a len or the | - | ||||||||||||
288 | terminating '\\0' whichever comes first) into \a dst and returns a | - | ||||||||||||
289 | pointer to \a dst. Guarantees that \a dst is '\\0'-terminated. If | - | ||||||||||||
290 | \a src or \a dst is 0, returns 0 immediately. | - | ||||||||||||
291 | - | |||||||||||||
292 | This function assumes that \a dst is at least \a len characters | - | ||||||||||||
293 | long. | - | ||||||||||||
294 | - | |||||||||||||
295 | \note When compiling with Visual C++ compiler version 14.00 | - | ||||||||||||
296 | (Visual C++ 2005) or later, internally the function strncpy_s | - | ||||||||||||
297 | will be used. | - | ||||||||||||
298 | - | |||||||||||||
299 | \sa qstrcpy() | - | ||||||||||||
300 | */ | - | ||||||||||||
301 | - | |||||||||||||
302 | char *qstrncpy(char *dst, const char *src, uint len) | - | ||||||||||||
303 | { | - | ||||||||||||
304 | if (!src || !dst) | - | ||||||||||||
305 | return 0; | - | ||||||||||||
306 | if (len > 0) { | - | ||||||||||||
307 | #if defined(_MSC_VER) && _MSC_VER >= 1400 | - | ||||||||||||
308 | strncpy_s(dst, len, src, len - 1); | - | ||||||||||||
309 | #else | - | ||||||||||||
310 | strncpy(dst, src, len); | - | ||||||||||||
311 | #endif | - | ||||||||||||
312 | dst[len-1] = '\0'; | - | ||||||||||||
313 | } | - | ||||||||||||
314 | return dst; | - | ||||||||||||
315 | } | - | ||||||||||||
316 | - | |||||||||||||
317 | /*! \fn uint qstrlen(const char *str) | - | ||||||||||||
318 | \relates QByteArray | - | ||||||||||||
319 | - | |||||||||||||
320 | A safe \c strlen() function. | - | ||||||||||||
321 | - | |||||||||||||
322 | Returns the number of characters that precede the terminating '\\0', | - | ||||||||||||
323 | or 0 if \a str is 0. | - | ||||||||||||
324 | - | |||||||||||||
325 | \sa qstrnlen() | - | ||||||||||||
326 | */ | - | ||||||||||||
327 | - | |||||||||||||
328 | /*! \fn uint qstrnlen(const char *str, uint maxlen) | - | ||||||||||||
329 | \relates QByteArray | - | ||||||||||||
330 | \since 4.2 | - | ||||||||||||
331 | - | |||||||||||||
332 | A safe \c strnlen() function. | - | ||||||||||||
333 | - | |||||||||||||
334 | Returns the number of characters that precede the terminating '\\0', but | - | ||||||||||||
335 | at most \a maxlen. If \a str is 0, returns 0. | - | ||||||||||||
336 | - | |||||||||||||
337 | \sa qstrlen() | - | ||||||||||||
338 | */ | - | ||||||||||||
339 | - | |||||||||||||
340 | /*! | - | ||||||||||||
341 | \relates QByteArray | - | ||||||||||||
342 | - | |||||||||||||
343 | A safe \c strcmp() function. | - | ||||||||||||
344 | - | |||||||||||||
345 | Compares \a str1 and \a str2. Returns a negative value if \a str1 | - | ||||||||||||
346 | is less than \a str2, 0 if \a str1 is equal to \a str2 or a | - | ||||||||||||
347 | positive value if \a str1 is greater than \a str2. | - | ||||||||||||
348 | - | |||||||||||||
349 | Special case 1: Returns 0 if \a str1 and \a str2 are both 0. | - | ||||||||||||
350 | - | |||||||||||||
351 | Special case 2: Returns an arbitrary non-zero value if \a str1 is 0 | - | ||||||||||||
352 | or \a str2 is 0 (but not both). | - | ||||||||||||
353 | - | |||||||||||||
354 | \sa qstrncmp(), qstricmp(), qstrnicmp(), {8-bit Character Comparisons} | - | ||||||||||||
355 | */ | - | ||||||||||||
356 | int qstrcmp(const char *str1, const char *str2) | - | ||||||||||||
357 | { | - | ||||||||||||
358 | return (str1 && str2) ? strcmp(str1, str2) | - | ||||||||||||
359 | : (str1 ? 1 : (str2 ? -1 : 0)); | - | ||||||||||||
360 | } | - | ||||||||||||
361 | - | |||||||||||||
362 | /*! \fn int qstrncmp(const char *str1, const char *str2, uint len); | - | ||||||||||||
363 | - | |||||||||||||
364 | \relates QByteArray | - | ||||||||||||
365 | - | |||||||||||||
366 | A safe \c strncmp() function. | - | ||||||||||||
367 | - | |||||||||||||
368 | Compares at most \a len bytes of \a str1 and \a str2. | - | ||||||||||||
369 | - | |||||||||||||
370 | Returns a negative value if \a str1 is less than \a str2, 0 if \a | - | ||||||||||||
371 | str1 is equal to \a str2 or a positive value if \a str1 is greater | - | ||||||||||||
372 | than \a str2. | - | ||||||||||||
373 | - | |||||||||||||
374 | Special case 1: Returns 0 if \a str1 and \a str2 are both 0. | - | ||||||||||||
375 | - | |||||||||||||
376 | Special case 2: Returns a random non-zero value if \a str1 is 0 | - | ||||||||||||
377 | or \a str2 is 0 (but not both). | - | ||||||||||||
378 | - | |||||||||||||
379 | \sa qstrcmp(), qstricmp(), qstrnicmp(), {8-bit Character Comparisons} | - | ||||||||||||
380 | */ | - | ||||||||||||
381 | - | |||||||||||||
382 | /*! \relates QByteArray | - | ||||||||||||
383 | - | |||||||||||||
384 | A safe \c stricmp() function. | - | ||||||||||||
385 | - | |||||||||||||
386 | Compares \a str1 and \a str2 ignoring the case of the | - | ||||||||||||
387 | characters. The encoding of the strings is assumed to be Latin-1. | - | ||||||||||||
388 | - | |||||||||||||
389 | Returns a negative value if \a str1 is less than \a str2, 0 if \a | - | ||||||||||||
390 | str1 is equal to \a str2 or a positive value if \a str1 is greater | - | ||||||||||||
391 | than \a str2. | - | ||||||||||||
392 | - | |||||||||||||
393 | Special case 1: Returns 0 if \a str1 and \a str2 are both 0. | - | ||||||||||||
394 | - | |||||||||||||
395 | Special case 2: Returns a random non-zero value if \a str1 is 0 | - | ||||||||||||
396 | or \a str2 is 0 (but not both). | - | ||||||||||||
397 | - | |||||||||||||
398 | \sa qstrcmp(), qstrncmp(), qstrnicmp(), {8-bit Character Comparisons} | - | ||||||||||||
399 | */ | - | ||||||||||||
400 | - | |||||||||||||
401 | int qstricmp(const char *str1, const char *str2) | - | ||||||||||||
402 | { | - | ||||||||||||
403 | const uchar *s1 = reinterpret_cast<const uchar *>(str1); | - | ||||||||||||
404 | const uchar *s2 = reinterpret_cast<const uchar *>(str2); | - | ||||||||||||
405 | int res; | - | ||||||||||||
406 | uchar c; | - | ||||||||||||
407 | if (!s1 || !s2) | - | ||||||||||||
408 | return s1 ? 1 : (s2 ? -1 : 0); | - | ||||||||||||
409 | for (; !(res = (c = latin1_lowercased[*s1]) - latin1_lowercased[*s2]); s1++, s2++) | - | ||||||||||||
410 | if (!c) // strings are equal | - | ||||||||||||
411 | break; | - | ||||||||||||
412 | return res; | - | ||||||||||||
413 | } | - | ||||||||||||
414 | - | |||||||||||||
415 | /*! \relates QByteArray | - | ||||||||||||
416 | - | |||||||||||||
417 | A safe \c strnicmp() function. | - | ||||||||||||
418 | - | |||||||||||||
419 | Compares at most \a len bytes of \a str1 and \a str2 ignoring the | - | ||||||||||||
420 | case of the characters. The encoding of the strings is assumed to | - | ||||||||||||
421 | be Latin-1. | - | ||||||||||||
422 | - | |||||||||||||
423 | Returns a negative value if \a str1 is less than \a str2, 0 if \a str1 | - | ||||||||||||
424 | is equal to \a str2 or a positive value if \a str1 is greater than \a | - | ||||||||||||
425 | str2. | - | ||||||||||||
426 | - | |||||||||||||
427 | Special case 1: Returns 0 if \a str1 and \a str2 are both 0. | - | ||||||||||||
428 | - | |||||||||||||
429 | Special case 2: Returns a random non-zero value if \a str1 is 0 | - | ||||||||||||
430 | or \a str2 is 0 (but not both). | - | ||||||||||||
431 | - | |||||||||||||
432 | \sa qstrcmp(), qstrncmp(), qstricmp(), {8-bit Character Comparisons} | - | ||||||||||||
433 | */ | - | ||||||||||||
434 | - | |||||||||||||
435 | int qstrnicmp(const char *str1, const char *str2, uint len) | - | ||||||||||||
436 | { | - | ||||||||||||
437 | const uchar *s1 = reinterpret_cast<const uchar *>(str1); | - | ||||||||||||
438 | const uchar *s2 = reinterpret_cast<const uchar *>(str2); | - | ||||||||||||
439 | int res; | - | ||||||||||||
440 | uchar c; | - | ||||||||||||
441 | if (!s1 || !s2) | - | ||||||||||||
442 | return s1 ? 1 : (s2 ? -1 : 0); | - | ||||||||||||
443 | for (; len--; s1++, s2++) { | - | ||||||||||||
444 | if ((res = (c = latin1_lowercased[*s1]) - latin1_lowercased[*s2])) | - | ||||||||||||
445 | return res; | - | ||||||||||||
446 | if (!c) // strings are equal | - | ||||||||||||
447 | break; | - | ||||||||||||
448 | } | - | ||||||||||||
449 | return 0; | - | ||||||||||||
450 | } | - | ||||||||||||
451 | - | |||||||||||||
452 | /*! | - | ||||||||||||
453 | \internal | - | ||||||||||||
454 | */ | - | ||||||||||||
455 | int qstrcmp(const QByteArray &str1, const char *str2) | - | ||||||||||||
456 | { | - | ||||||||||||
457 | if (!str2) | - | ||||||||||||
458 | return str1.isEmpty() ? 0 : +1; | - | ||||||||||||
459 | - | |||||||||||||
460 | const char *str1data = str1.constData(); | - | ||||||||||||
461 | const char *str1end = str1data + str1.length(); | - | ||||||||||||
462 | for ( ; str1data < str1end && *str2; ++str1data, ++str2) { | - | ||||||||||||
463 | int diff = int(uchar(*str1data)) - uchar(*str2); | - | ||||||||||||
464 | if (diff) | - | ||||||||||||
465 | // found a difference | - | ||||||||||||
466 | return diff; | - | ||||||||||||
467 | } | - | ||||||||||||
468 | - | |||||||||||||
469 | // Why did we stop? | - | ||||||||||||
470 | if (*str2 != '\0') | - | ||||||||||||
471 | // not the null, so we stopped because str1 is shorter | - | ||||||||||||
472 | return -1; | - | ||||||||||||
473 | if (str1data < str1end) | - | ||||||||||||
474 | // we haven't reached the end, so str1 must be longer | - | ||||||||||||
475 | return +1; | - | ||||||||||||
476 | return 0; | - | ||||||||||||
477 | } | - | ||||||||||||
478 | - | |||||||||||||
479 | /*! | - | ||||||||||||
480 | \internal | - | ||||||||||||
481 | */ | - | ||||||||||||
482 | int qstrcmp(const QByteArray &str1, const QByteArray &str2) | - | ||||||||||||
483 | { | - | ||||||||||||
484 | int l1 = str1.length(); | - | ||||||||||||
485 | int l2 = str2.length(); | - | ||||||||||||
486 | int ret = memcmp(str1.constData(), str2.constData(), qMin(l1, l2)); | - | ||||||||||||
487 | if (ret != 0) | - | ||||||||||||
488 | return ret; | - | ||||||||||||
489 | - | |||||||||||||
490 | // they matched qMin(l1, l2) bytes | - | ||||||||||||
491 | // so the longer one is lexically after the shorter one | - | ||||||||||||
492 | return l1 - l2; | - | ||||||||||||
493 | } | - | ||||||||||||
494 | - | |||||||||||||
495 | // the CRC table below is created by the following piece of code | - | ||||||||||||
496 | #if 0 | - | ||||||||||||
497 | static void createCRC16Table() // build CRC16 lookup table | - | ||||||||||||
498 | { | - | ||||||||||||
499 | unsigned int i; | - | ||||||||||||
500 | unsigned int j; | - | ||||||||||||
501 | unsigned short crc_tbl[16]; | - | ||||||||||||
502 | unsigned int v0, v1, v2, v3; | - | ||||||||||||
503 | for (i = 0; i < 16; i++) { | - | ||||||||||||
504 | v0 = i & 1; | - | ||||||||||||
505 | v1 = (i >> 1) & 1; | - | ||||||||||||
506 | v2 = (i >> 2) & 1; | - | ||||||||||||
507 | v3 = (i >> 3) & 1; | - | ||||||||||||
508 | j = 0; | - | ||||||||||||
509 | #undef SET_BIT | - | ||||||||||||
510 | #define SET_BIT(x, b, v) (x) |= (v) << (b) | - | ||||||||||||
511 | SET_BIT(j, 0, v0); | - | ||||||||||||
512 | SET_BIT(j, 7, v0); | - | ||||||||||||
513 | SET_BIT(j, 12, v0); | - | ||||||||||||
514 | SET_BIT(j, 1, v1); | - | ||||||||||||
515 | SET_BIT(j, 8, v1); | - | ||||||||||||
516 | SET_BIT(j, 13, v1); | - | ||||||||||||
517 | SET_BIT(j, 2, v2); | - | ||||||||||||
518 | SET_BIT(j, 9, v2); | - | ||||||||||||
519 | SET_BIT(j, 14, v2); | - | ||||||||||||
520 | SET_BIT(j, 3, v3); | - | ||||||||||||
521 | SET_BIT(j, 10, v3); | - | ||||||||||||
522 | SET_BIT(j, 15, v3); | - | ||||||||||||
523 | crc_tbl[i] = j; | - | ||||||||||||
524 | } | - | ||||||||||||
525 | printf("static const quint16 crc_tbl[16] = {\n"); | - | ||||||||||||
526 | for (int i = 0; i < 16; i +=4) | - | ||||||||||||
527 | 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]); | - | ||||||||||||
528 | printf("};\n"); | - | ||||||||||||
529 | } | - | ||||||||||||
530 | #endif | - | ||||||||||||
531 | - | |||||||||||||
532 | static const quint16 crc_tbl[16] = { | - | ||||||||||||
533 | 0x0000, 0x1081, 0x2102, 0x3183, | - | ||||||||||||
534 | 0x4204, 0x5285, 0x6306, 0x7387, | - | ||||||||||||
535 | 0x8408, 0x9489, 0xa50a, 0xb58b, | - | ||||||||||||
536 | 0xc60c, 0xd68d, 0xe70e, 0xf78f | - | ||||||||||||
537 | }; | - | ||||||||||||
538 | - | |||||||||||||
539 | /*! | - | ||||||||||||
540 | \relates QByteArray | - | ||||||||||||
541 | - | |||||||||||||
542 | Returns the CRC-16 checksum of the first \a len bytes of \a data. | - | ||||||||||||
543 | - | |||||||||||||
544 | The checksum is independent of the byte order (endianness). | - | ||||||||||||
545 | - | |||||||||||||
546 | \note This function is a 16-bit cache conserving (16 entry table) | - | ||||||||||||
547 | implementation of the CRC-16-CCITT algorithm. | - | ||||||||||||
548 | */ | - | ||||||||||||
549 | - | |||||||||||||
550 | quint16 qChecksum(const char *data, uint len) | - | ||||||||||||
551 | { | - | ||||||||||||
552 | quint16 crc = 0xffff; | - | ||||||||||||
553 | uchar c; | - | ||||||||||||
554 | const uchar *p = reinterpret_cast<const uchar *>(data); | - | ||||||||||||
555 | while (len--) { | - | ||||||||||||
556 | c = *p++; | - | ||||||||||||
557 | crc = ((crc >> 4) & 0x0fff) ^ crc_tbl[((crc ^ c) & 15)]; | - | ||||||||||||
558 | c >>= 4; | - | ||||||||||||
559 | crc = ((crc >> 4) & 0x0fff) ^ crc_tbl[((crc ^ c) & 15)]; | - | ||||||||||||
560 | } | - | ||||||||||||
561 | return ~crc & 0xffff; | - | ||||||||||||
562 | } | - | ||||||||||||
563 | - | |||||||||||||
564 | /*! | - | ||||||||||||
565 | \fn QByteArray qCompress(const QByteArray& data, int compressionLevel) | - | ||||||||||||
566 | - | |||||||||||||
567 | \relates QByteArray | - | ||||||||||||
568 | - | |||||||||||||
569 | Compresses the \a data byte array and returns the compressed data | - | ||||||||||||
570 | in a new byte array. | - | ||||||||||||
571 | - | |||||||||||||
572 | The \a compressionLevel parameter specifies how much compression | - | ||||||||||||
573 | should be used. Valid values are between 0 and 9, with 9 | - | ||||||||||||
574 | corresponding to the greatest compression (i.e. smaller compressed | - | ||||||||||||
575 | data) at the cost of using a slower algorithm. Smaller values (8, | - | ||||||||||||
576 | 7, ..., 1) provide successively less compression at slightly | - | ||||||||||||
577 | faster speeds. The value 0 corresponds to no compression at all. | - | ||||||||||||
578 | The default value is -1, which specifies zlib's default | - | ||||||||||||
579 | compression. | - | ||||||||||||
580 | - | |||||||||||||
581 | \sa qUncompress() | - | ||||||||||||
582 | */ | - | ||||||||||||
583 | - | |||||||||||||
584 | /*! \relates QByteArray | - | ||||||||||||
585 | - | |||||||||||||
586 | \overload | - | ||||||||||||
587 | - | |||||||||||||
588 | Compresses the first \a nbytes of \a data at compression level | - | ||||||||||||
589 | \a compressionLevel and returns the compressed data in a new byte array. | - | ||||||||||||
590 | */ | - | ||||||||||||
591 | - | |||||||||||||
592 | #ifndef QT_NO_COMPRESS | - | ||||||||||||
593 | QByteArray qCompress(const uchar* data, int nbytes, int compressionLevel) | - | ||||||||||||
594 | { | - | ||||||||||||
595 | if (nbytes == 0) { | - | ||||||||||||
596 | return QByteArray(4, '\0'); | - | ||||||||||||
597 | } | - | ||||||||||||
598 | if (!data) { | - | ||||||||||||
599 | qWarning("qCompress: Data is null"); | - | ||||||||||||
600 | return QByteArray(); | - | ||||||||||||
601 | } | - | ||||||||||||
602 | if (compressionLevel < -1 || compressionLevel > 9) | - | ||||||||||||
603 | compressionLevel = -1; | - | ||||||||||||
604 | - | |||||||||||||
605 | ulong len = nbytes + nbytes / 100 + 13; | - | ||||||||||||
606 | QByteArray bazip; | - | ||||||||||||
607 | int res; | - | ||||||||||||
608 | do { | - | ||||||||||||
609 | bazip.resize(len + 4); | - | ||||||||||||
610 | res = ::compress2((uchar*)bazip.data()+4, &len, data, nbytes, compressionLevel); | - | ||||||||||||
611 | - | |||||||||||||
612 | switch (res) { | - | ||||||||||||
613 | case Z_OK: | - | ||||||||||||
614 | bazip.resize(len + 4); | - | ||||||||||||
615 | bazip[0] = (nbytes & 0xff000000) >> 24; | - | ||||||||||||
616 | bazip[1] = (nbytes & 0x00ff0000) >> 16; | - | ||||||||||||
617 | bazip[2] = (nbytes & 0x0000ff00) >> 8; | - | ||||||||||||
618 | bazip[3] = (nbytes & 0x000000ff); | - | ||||||||||||
619 | break; | - | ||||||||||||
620 | case Z_MEM_ERROR: | - | ||||||||||||
621 | qWarning("qCompress: Z_MEM_ERROR: Not enough memory"); | - | ||||||||||||
622 | bazip.resize(0); | - | ||||||||||||
623 | break; | - | ||||||||||||
624 | case Z_BUF_ERROR: | - | ||||||||||||
625 | len *= 2; | - | ||||||||||||
626 | break; | - | ||||||||||||
627 | } | - | ||||||||||||
628 | } while (res == Z_BUF_ERROR); | - | ||||||||||||
629 | - | |||||||||||||
630 | return bazip; | - | ||||||||||||
631 | } | - | ||||||||||||
632 | #endif | - | ||||||||||||
633 | - | |||||||||||||
634 | /*! | - | ||||||||||||
635 | \fn QByteArray qUncompress(const QByteArray &data) | - | ||||||||||||
636 | - | |||||||||||||
637 | \relates QByteArray | - | ||||||||||||
638 | - | |||||||||||||
639 | Uncompresses the \a data byte array and returns a new byte array | - | ||||||||||||
640 | with the uncompressed data. | - | ||||||||||||
641 | - | |||||||||||||
642 | Returns an empty QByteArray if the input data was corrupt. | - | ||||||||||||
643 | - | |||||||||||||
644 | This function will uncompress data compressed with qCompress() | - | ||||||||||||
645 | from this and any earlier Qt version, back to Qt 3.1 when this | - | ||||||||||||
646 | feature was added. | - | ||||||||||||
647 | - | |||||||||||||
648 | \b{Note:} If you want to use this function to uncompress external | - | ||||||||||||
649 | data that was compressed using zlib, you first need to prepend a four | - | ||||||||||||
650 | byte header to the byte array containing the data. The header must | - | ||||||||||||
651 | contain the expected length (in bytes) of the uncompressed data, | - | ||||||||||||
652 | expressed as an unsigned, big-endian, 32-bit integer. | - | ||||||||||||
653 | - | |||||||||||||
654 | \sa qCompress() | - | ||||||||||||
655 | */ | - | ||||||||||||
656 | - | |||||||||||||
657 | /*! \relates QByteArray | - | ||||||||||||
658 | - | |||||||||||||
659 | \overload | - | ||||||||||||
660 | - | |||||||||||||
661 | Uncompresses the first \a nbytes of \a data and returns a new byte | - | ||||||||||||
662 | array with the uncompressed data. | - | ||||||||||||
663 | */ | - | ||||||||||||
664 | - | |||||||||||||
665 | #ifndef QT_NO_COMPRESS | - | ||||||||||||
666 | QByteArray qUncompress(const uchar* data, int nbytes) | - | ||||||||||||
667 | { | - | ||||||||||||
668 | if (!data) { | - | ||||||||||||
669 | qWarning("qUncompress: Data is null"); | - | ||||||||||||
670 | return QByteArray(); | - | ||||||||||||
671 | } | - | ||||||||||||
672 | if (nbytes <= 4) { | - | ||||||||||||
673 | if (nbytes < 4 || (data[0]!=0 || data[1]!=0 || data[2]!=0 || data[3]!=0)) | - | ||||||||||||
674 | qWarning("qUncompress: Input data is corrupted"); | - | ||||||||||||
675 | return QByteArray(); | - | ||||||||||||
676 | } | - | ||||||||||||
677 | ulong expectedSize = uint((data[0] << 24) | (data[1] << 16) | | - | ||||||||||||
678 | (data[2] << 8) | (data[3] )); | - | ||||||||||||
679 | ulong len = qMax(expectedSize, 1ul); | - | ||||||||||||
680 | QScopedPointer<QByteArray::Data, QScopedPointerPodDeleter> d; | - | ||||||||||||
681 | - | |||||||||||||
682 | forever { | - | ||||||||||||
683 | ulong alloc = len; | - | ||||||||||||
684 | if (len >= (1u << 31u) - sizeof(QByteArray::Data)) { | - | ||||||||||||
685 | //QByteArray does not support that huge size anyway. | - | ||||||||||||
686 | qWarning("qUncompress: Input data is corrupted"); | - | ||||||||||||
687 | return QByteArray(); | - | ||||||||||||
688 | } | - | ||||||||||||
689 | QByteArray::Data *p = static_cast<QByteArray::Data *>(::realloc(d.data(), sizeof(QByteArray::Data) + alloc + 1)); | - | ||||||||||||
690 | if (!p) { | - | ||||||||||||
691 | // we are not allowed to crash here when compiling with QT_NO_EXCEPTIONS | - | ||||||||||||
692 | qWarning("qUncompress: could not allocate enough memory to uncompress data"); | - | ||||||||||||
693 | return QByteArray(); | - | ||||||||||||
694 | } | - | ||||||||||||
695 | d.take(); // realloc was successful | - | ||||||||||||
696 | d.reset(p); | - | ||||||||||||
697 | d->offset = sizeof(QByteArrayData); | - | ||||||||||||
698 | d->size = 0; // Shut up valgrind "uninitialized variable" warning | - | ||||||||||||
699 | - | |||||||||||||
700 | int res = ::uncompress((uchar*)d->data(), &len, | - | ||||||||||||
701 | data+4, nbytes-4); | - | ||||||||||||
702 | - | |||||||||||||
703 | switch (res) { | - | ||||||||||||
704 | case Z_OK: | - | ||||||||||||
705 | if (len != alloc) { | - | ||||||||||||
706 | if (len >= (1u << 31u) - sizeof(QByteArray::Data)) { | - | ||||||||||||
707 | //QByteArray does not support that huge size anyway. | - | ||||||||||||
708 | qWarning("qUncompress: Input data is corrupted"); | - | ||||||||||||
709 | return QByteArray(); | - | ||||||||||||
710 | } | - | ||||||||||||
711 | QByteArray::Data *p = static_cast<QByteArray::Data *>(::realloc(d.data(), sizeof(QByteArray::Data) + len + 1)); | - | ||||||||||||
712 | if (!p) { | - | ||||||||||||
713 | // we are not allowed to crash here when compiling with QT_NO_EXCEPTIONS | - | ||||||||||||
714 | qWarning("qUncompress: could not allocate enough memory to uncompress data"); | - | ||||||||||||
715 | return QByteArray(); | - | ||||||||||||
716 | } | - | ||||||||||||
717 | d.take(); // realloc was successful | - | ||||||||||||
718 | d.reset(p); | - | ||||||||||||
719 | } | - | ||||||||||||
720 | d->ref.initializeOwned(); | - | ||||||||||||
721 | d->size = len; | - | ||||||||||||
722 | d->alloc = uint(len) + 1u; | - | ||||||||||||
723 | d->capacityReserved = false; | - | ||||||||||||
724 | d->offset = sizeof(QByteArrayData); | - | ||||||||||||
725 | d->data()[len] = 0; | - | ||||||||||||
726 | - | |||||||||||||
727 | { | - | ||||||||||||
728 | QByteArrayDataPtr dataPtr = { d.take() }; | - | ||||||||||||
729 | return QByteArray(dataPtr); | - | ||||||||||||
730 | } | - | ||||||||||||
731 | - | |||||||||||||
732 | case Z_MEM_ERROR: | - | ||||||||||||
733 | qWarning("qUncompress: Z_MEM_ERROR: Not enough memory"); | - | ||||||||||||
734 | return QByteArray(); | - | ||||||||||||
735 | - | |||||||||||||
736 | case Z_BUF_ERROR: | - | ||||||||||||
737 | len *= 2; | - | ||||||||||||
738 | continue; | - | ||||||||||||
739 | - | |||||||||||||
740 | case Z_DATA_ERROR: | - | ||||||||||||
741 | qWarning("qUncompress: Z_DATA_ERROR: Input data is corrupted"); | - | ||||||||||||
742 | return QByteArray(); | - | ||||||||||||
743 | } | - | ||||||||||||
744 | } | - | ||||||||||||
745 | } | - | ||||||||||||
746 | #endif | - | ||||||||||||
747 | - | |||||||||||||
748 | static inline bool qIsUpper(char c) | - | ||||||||||||
749 | { | - | ||||||||||||
750 | return c >= 'A' && c <= 'Z'; | - | ||||||||||||
751 | } | - | ||||||||||||
752 | - | |||||||||||||
753 | static inline char qToLower(char c) | - | ||||||||||||
754 | { | - | ||||||||||||
755 | if (c >= 'A' && c <= 'Z') | - | ||||||||||||
756 | return c - 'A' + 'a'; | - | ||||||||||||
757 | else | - | ||||||||||||
758 | return c; | - | ||||||||||||
759 | } | - | ||||||||||||
760 | - | |||||||||||||
761 | /*! | - | ||||||||||||
762 | \class QByteArray | - | ||||||||||||
763 | \inmodule QtCore | - | ||||||||||||
764 | \brief The QByteArray class provides an array of bytes. | - | ||||||||||||
765 | - | |||||||||||||
766 | \ingroup tools | - | ||||||||||||
767 | \ingroup shared | - | ||||||||||||
768 | \ingroup string-processing | - | ||||||||||||
769 | - | |||||||||||||
770 | \reentrant | - | ||||||||||||
771 | - | |||||||||||||
772 | QByteArray can be used to store both raw bytes (including '\\0's) | - | ||||||||||||
773 | and traditional 8-bit '\\0'-terminated strings. Using QByteArray | - | ||||||||||||
774 | is much more convenient than using \c{const char *}. Behind the | - | ||||||||||||
775 | scenes, it always ensures that the data is followed by a '\\0' | - | ||||||||||||
776 | terminator, and uses \l{implicit sharing} (copy-on-write) to | - | ||||||||||||
777 | reduce memory usage and avoid needless copying of data. | - | ||||||||||||
778 | - | |||||||||||||
779 | In addition to QByteArray, Qt also provides the QString class to | - | ||||||||||||
780 | store string data. For most purposes, QString is the class you | - | ||||||||||||
781 | want to use. It stores 16-bit Unicode characters, making it easy | - | ||||||||||||
782 | to store non-ASCII/non-Latin-1 characters in your application. | - | ||||||||||||
783 | Furthermore, QString is used throughout in the Qt API. The two | - | ||||||||||||
784 | main cases where QByteArray is appropriate are when you need to | - | ||||||||||||
785 | store raw binary data, and when memory conservation is critical | - | ||||||||||||
786 | (e.g., with Qt for Embedded Linux). | - | ||||||||||||
787 | - | |||||||||||||
788 | One way to initialize a QByteArray is simply to pass a \c{const | - | ||||||||||||
789 | char *} to its constructor. For example, the following code | - | ||||||||||||
790 | creates a byte array of size 5 containing the data "Hello": | - | ||||||||||||
791 | - | |||||||||||||
792 | \snippet code/src_corelib_tools_qbytearray.cpp 0 | - | ||||||||||||
793 | - | |||||||||||||
794 | Although the size() is 5, the byte array also maintains an extra | - | ||||||||||||
795 | '\\0' character at the end so that if a function is used that | - | ||||||||||||
796 | asks for a pointer to the underlying data (e.g. a call to | - | ||||||||||||
797 | data()), the data pointed to is guaranteed to be | - | ||||||||||||
798 | '\\0'-terminated. | - | ||||||||||||
799 | - | |||||||||||||
800 | QByteArray makes a deep copy of the \c{const char *} data, so you | - | ||||||||||||
801 | can modify it later without experiencing side effects. (If for | - | ||||||||||||
802 | performance reasons you don't want to take a deep copy of the | - | ||||||||||||
803 | character data, use QByteArray::fromRawData() instead.) | - | ||||||||||||
804 | - | |||||||||||||
805 | Another approach is to set the size of the array using resize() | - | ||||||||||||
806 | and to initialize the data byte per byte. QByteArray uses 0-based | - | ||||||||||||
807 | indexes, just like C++ arrays. To access the byte at a particular | - | ||||||||||||
808 | index position, you can use operator[](). On non-const byte | - | ||||||||||||
809 | arrays, operator[]() returns a reference to a byte that can be | - | ||||||||||||
810 | used on the left side of an assignment. For example: | - | ||||||||||||
811 | - | |||||||||||||
812 | \snippet code/src_corelib_tools_qbytearray.cpp 1 | - | ||||||||||||
813 | - | |||||||||||||
814 | For read-only access, an alternative syntax is to use at(): | - | ||||||||||||
815 | - | |||||||||||||
816 | \snippet code/src_corelib_tools_qbytearray.cpp 2 | - | ||||||||||||
817 | - | |||||||||||||
818 | at() can be faster than operator[](), because it never causes a | - | ||||||||||||
819 | \l{deep copy} to occur. | - | ||||||||||||
820 | - | |||||||||||||
821 | To extract many bytes at a time, use left(), right(), or mid(). | - | ||||||||||||
822 | - | |||||||||||||
823 | A QByteArray can embed '\\0' bytes. The size() function always | - | ||||||||||||
824 | returns the size of the whole array, including embedded '\\0' | - | ||||||||||||
825 | bytes, but excluding the terminating '\\0' added by QByteArray. | - | ||||||||||||
826 | For example: | - | ||||||||||||
827 | - | |||||||||||||
828 | \snippet code/src_corelib_tools_qbytearray.cpp 48 | - | ||||||||||||
829 | - | |||||||||||||
830 | If you want to obtain the length of the data up to and | - | ||||||||||||
831 | excluding the first '\\0' character, call qstrlen() on the byte | - | ||||||||||||
832 | array. | - | ||||||||||||
833 | - | |||||||||||||
834 | After a call to resize(), newly allocated bytes have undefined | - | ||||||||||||
835 | values. To set all the bytes to a particular value, call fill(). | - | ||||||||||||
836 | - | |||||||||||||
837 | To obtain a pointer to the actual character data, call data() or | - | ||||||||||||
838 | constData(). These functions return a pointer to the beginning of the data. | - | ||||||||||||
839 | The pointer is guaranteed to remain valid until a non-const function is | - | ||||||||||||
840 | called on the QByteArray. It is also guaranteed that the data ends with a | - | ||||||||||||
841 | '\\0' byte unless the QByteArray was created from a \l{fromRawData()}{raw | - | ||||||||||||
842 | data}. This '\\0' byte is automatically provided by QByteArray and is not | - | ||||||||||||
843 | counted in size(). | - | ||||||||||||
844 | - | |||||||||||||
845 | QByteArray provides the following basic functions for modifying | - | ||||||||||||
846 | the byte data: append(), prepend(), insert(), replace(), and | - | ||||||||||||
847 | remove(). For example: | - | ||||||||||||
848 | - | |||||||||||||
849 | \snippet code/src_corelib_tools_qbytearray.cpp 3 | - | ||||||||||||
850 | - | |||||||||||||
851 | The replace() and remove() functions' first two arguments are the | - | ||||||||||||
852 | position from which to start erasing and the number of bytes that | - | ||||||||||||
853 | should be erased. | - | ||||||||||||
854 | - | |||||||||||||
855 | When you append() data to a non-empty array, the array will be | - | ||||||||||||
856 | reallocated and the new data copied to it. You can avoid this | - | ||||||||||||
857 | behavior by calling reserve(), which preallocates a certain amount | - | ||||||||||||
858 | of memory. You can also call capacity() to find out how much | - | ||||||||||||
859 | memory QByteArray actually allocated. Data appended to an empty | - | ||||||||||||
860 | array is not copied. | - | ||||||||||||
861 | - | |||||||||||||
862 | A frequent requirement is to remove whitespace characters from a | - | ||||||||||||
863 | byte array ('\\n', '\\t', ' ', etc.). If you want to remove | - | ||||||||||||
864 | whitespace from both ends of a QByteArray, use trimmed(). If you | - | ||||||||||||
865 | want to remove whitespace from both ends and replace multiple | - | ||||||||||||
866 | consecutive whitespaces with a single space character within the | - | ||||||||||||
867 | byte array, use simplified(). | - | ||||||||||||
868 | - | |||||||||||||
869 | If you want to find all occurrences of a particular character or | - | ||||||||||||
870 | substring in a QByteArray, use indexOf() or lastIndexOf(). The | - | ||||||||||||
871 | former searches forward starting from a given index position, the | - | ||||||||||||
872 | latter searches backward. Both return the index position of the | - | ||||||||||||
873 | character or substring if they find it; otherwise, they return -1. | - | ||||||||||||
874 | For example, here's a typical loop that finds all occurrences of a | - | ||||||||||||
875 | particular substring: | - | ||||||||||||
876 | - | |||||||||||||
877 | \snippet code/src_corelib_tools_qbytearray.cpp 4 | - | ||||||||||||
878 | - | |||||||||||||
879 | If you simply want to check whether a QByteArray contains a | - | ||||||||||||
880 | particular character or substring, use contains(). If you want to | - | ||||||||||||
881 | find out how many times a particular character or substring | - | ||||||||||||
882 | occurs in the byte array, use count(). If you want to replace all | - | ||||||||||||
883 | occurrences of a particular value with another, use one of the | - | ||||||||||||
884 | two-parameter replace() overloads. | - | ||||||||||||
885 | - | |||||||||||||
886 | \l{QByteArray}s can be compared using overloaded operators such as | - | ||||||||||||
887 | operator<(), operator<=(), operator==(), operator>=(), and so on. | - | ||||||||||||
888 | The comparison is based exclusively on the numeric values | - | ||||||||||||
889 | of the characters and is very fast, but is not what a human would | - | ||||||||||||
890 | expect. QString::localeAwareCompare() is a better choice for | - | ||||||||||||
891 | sorting user-interface strings. | - | ||||||||||||
892 | - | |||||||||||||
893 | For historical reasons, QByteArray distinguishes between a null | - | ||||||||||||
894 | byte array and an empty byte array. A \e null byte array is a | - | ||||||||||||
895 | byte array that is initialized using QByteArray's default | - | ||||||||||||
896 | constructor or by passing (const char *)0 to the constructor. An | - | ||||||||||||
897 | \e empty byte array is any byte array with size 0. A null byte | - | ||||||||||||
898 | array is always empty, but an empty byte array isn't necessarily | - | ||||||||||||
899 | null: | - | ||||||||||||
900 | - | |||||||||||||
901 | \snippet code/src_corelib_tools_qbytearray.cpp 5 | - | ||||||||||||
902 | - | |||||||||||||
903 | All functions except isNull() treat null byte arrays the same as | - | ||||||||||||
904 | empty byte arrays. For example, data() returns a pointer to a | - | ||||||||||||
905 | '\\0' character for a null byte array (\e not a null pointer), | - | ||||||||||||
906 | and QByteArray() compares equal to QByteArray(""). We recommend | - | ||||||||||||
907 | that you always use isEmpty() and avoid isNull(). | - | ||||||||||||
908 | - | |||||||||||||
909 | \section1 Notes on Locale | - | ||||||||||||
910 | - | |||||||||||||
911 | \section2 Number-String Conversions | - | ||||||||||||
912 | - | |||||||||||||
913 | Functions that perform conversions between numeric data types and | - | ||||||||||||
914 | strings are performed in the C locale, irrespective of the user's | - | ||||||||||||
915 | locale settings. Use QString to perform locale-aware conversions | - | ||||||||||||
916 | between numbers and strings. | - | ||||||||||||
917 | - | |||||||||||||
918 | \section2 8-bit Character Comparisons | - | ||||||||||||
919 | - | |||||||||||||
920 | In QByteArray, the notion of uppercase and lowercase and of which | - | ||||||||||||
921 | character is greater than or less than another character is | - | ||||||||||||
922 | locale dependent. This affects functions that support a case | - | ||||||||||||
923 | insensitive option or that compare or lowercase or uppercase | - | ||||||||||||
924 | their arguments. Case insensitive operations and comparisons will | - | ||||||||||||
925 | be accurate if both strings contain only ASCII characters. (If \c | - | ||||||||||||
926 | $LC_CTYPE is set, most Unix systems do "the right thing".) | - | ||||||||||||
927 | Functions that this affects include contains(), indexOf(), | - | ||||||||||||
928 | lastIndexOf(), operator<(), operator<=(), operator>(), | - | ||||||||||||
929 | operator>=(), toLower() and toUpper(). | - | ||||||||||||
930 | - | |||||||||||||
931 | This issue does not apply to \l{QString}s since they represent | - | ||||||||||||
932 | characters using Unicode. | - | ||||||||||||
933 | - | |||||||||||||
934 | \sa QString, QBitArray | - | ||||||||||||
935 | */ | - | ||||||||||||
936 | - | |||||||||||||
937 | /*! | - | ||||||||||||
938 | \enum QByteArray::Base64Option | - | ||||||||||||
939 | \since 5.2 | - | ||||||||||||
940 | - | |||||||||||||
941 | This enum contains the options available for encoding and decoding Base64. | - | ||||||||||||
942 | Base64 is defined by \l{RFC 4648}, with the following options: | - | ||||||||||||
943 | - | |||||||||||||
944 | \value Base64Encoding (default) The regular Base64 alphabet, called simply "base64" | - | ||||||||||||
945 | \value Base64UrlEncoding An alternate alphabet, called "base64url", which replaces two | - | ||||||||||||
946 | characters in the alphabet to be more friendly to URLs. | - | ||||||||||||
947 | \value KeepTrailingEquals (default) Keeps the trailing padding equal signs at the end | - | ||||||||||||
948 | of the encoded data, so the data is always a size multiple of | - | ||||||||||||
949 | four. | - | ||||||||||||
950 | \value OmitTrailingEquals Omits adding the padding equal signs at the end of the encoded | - | ||||||||||||
951 | data. | - | ||||||||||||
952 | - | |||||||||||||
953 | QByteArray::fromBase64() ignores the KeepTrailingEquals and | - | ||||||||||||
954 | OmitTrailingEquals options and will not flag errors in case they are | - | ||||||||||||
955 | missing or if there are too many of them. | - | ||||||||||||
956 | */ | - | ||||||||||||
957 | - | |||||||||||||
958 | /*! \fn QByteArray::iterator QByteArray::begin() | - | ||||||||||||
959 | - | |||||||||||||
960 | Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first character in | - | ||||||||||||
961 | the byte-array. | - | ||||||||||||
962 | - | |||||||||||||
963 | \sa constBegin(), end() | - | ||||||||||||
964 | */ | - | ||||||||||||
965 | - | |||||||||||||
966 | /*! \fn QByteArray::const_iterator QByteArray::begin() const | - | ||||||||||||
967 | - | |||||||||||||
968 | \overload begin() | - | ||||||||||||
969 | */ | - | ||||||||||||
970 | - | |||||||||||||
971 | /*! \fn QByteArray::const_iterator QByteArray::cbegin() const | - | ||||||||||||
972 | \since 5.0 | - | ||||||||||||
973 | - | |||||||||||||
974 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first character | - | ||||||||||||
975 | in the byte-array. | - | ||||||||||||
976 | - | |||||||||||||
977 | \sa begin(), cend() | - | ||||||||||||
978 | */ | - | ||||||||||||
979 | - | |||||||||||||
980 | /*! \fn QByteArray::const_iterator QByteArray::constBegin() const | - | ||||||||||||
981 | - | |||||||||||||
982 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first character | - | ||||||||||||
983 | in the byte-array. | - | ||||||||||||
984 | - | |||||||||||||
985 | \sa begin(), constEnd() | - | ||||||||||||
986 | */ | - | ||||||||||||
987 | - | |||||||||||||
988 | /*! \fn QByteArray::iterator QByteArray::end() | - | ||||||||||||
989 | - | |||||||||||||
990 | Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary character | - | ||||||||||||
991 | after the last character in the byte-array. | - | ||||||||||||
992 | - | |||||||||||||
993 | \sa begin(), constEnd() | - | ||||||||||||
994 | */ | - | ||||||||||||
995 | - | |||||||||||||
996 | /*! \fn QByteArray::const_iterator QByteArray::end() const | - | ||||||||||||
997 | - | |||||||||||||
998 | \overload end() | - | ||||||||||||
999 | */ | - | ||||||||||||
1000 | - | |||||||||||||
1001 | /*! \fn QByteArray::const_iterator QByteArray::cend() const | - | ||||||||||||
1002 | \since 5.0 | - | ||||||||||||
1003 | - | |||||||||||||
1004 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary | - | ||||||||||||
1005 | character after the last character in the list. | - | ||||||||||||
1006 | - | |||||||||||||
1007 | \sa cbegin(), end() | - | ||||||||||||
1008 | */ | - | ||||||||||||
1009 | - | |||||||||||||
1010 | /*! \fn QByteArray::const_iterator QByteArray::constEnd() const | - | ||||||||||||
1011 | - | |||||||||||||
1012 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary | - | ||||||||||||
1013 | character after the last character in the list. | - | ||||||||||||
1014 | - | |||||||||||||
1015 | \sa constBegin(), end() | - | ||||||||||||
1016 | */ | - | ||||||||||||
1017 | - | |||||||||||||
1018 | /*! \fn QByteArray::reverse_iterator QByteArray::rbegin() | - | ||||||||||||
1019 | \since 5.6 | - | ||||||||||||
1020 | - | |||||||||||||
1021 | Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to the first | - | ||||||||||||
1022 | character in the byte-array, in reverse order. | - | ||||||||||||
1023 | - | |||||||||||||
1024 | \sa begin(), crbegin(), rend() | - | ||||||||||||
1025 | */ | - | ||||||||||||
1026 | - | |||||||||||||
1027 | /*! \fn QByteArray::const_reverse_iterator QByteArray::rbegin() const | - | ||||||||||||
1028 | \since 5.6 | - | ||||||||||||
1029 | \overload | - | ||||||||||||
1030 | */ | - | ||||||||||||
1031 | - | |||||||||||||
1032 | /*! \fn QByteArray::const_reverse_iterator QByteArray::crbegin() const | - | ||||||||||||
1033 | \since 5.6 | - | ||||||||||||
1034 | - | |||||||||||||
1035 | Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to the first | - | ||||||||||||
1036 | character in the byte-array, in reverse order. | - | ||||||||||||
1037 | - | |||||||||||||
1038 | \sa begin(), rbegin(), rend() | - | ||||||||||||
1039 | */ | - | ||||||||||||
1040 | - | |||||||||||||
1041 | /*! \fn QByteArray::reverse_iterator QByteArray::rend() | - | ||||||||||||
1042 | \since 5.6 | - | ||||||||||||
1043 | - | |||||||||||||
1044 | Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past | - | ||||||||||||
1045 | the last character in the byte-array, in reverse order. | - | ||||||||||||
1046 | - | |||||||||||||
1047 | \sa end(), crend(), rbegin() | - | ||||||||||||
1048 | */ | - | ||||||||||||
1049 | - | |||||||||||||
1050 | /*! \fn QByteArray::const_reverse_iterator QByteArray::rend() const | - | ||||||||||||
1051 | \since 5.6 | - | ||||||||||||
1052 | \overload | - | ||||||||||||
1053 | */ | - | ||||||||||||
1054 | - | |||||||||||||
1055 | /*! \fn QByteArray::const_reverse_iterator QByteArray::crend() const | - | ||||||||||||
1056 | \since 5.6 | - | ||||||||||||
1057 | - | |||||||||||||
1058 | Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to one | - | ||||||||||||
1059 | past the last character in the byte-array, in reverse order. | - | ||||||||||||
1060 | - | |||||||||||||
1061 | \sa end(), rend(), rbegin() | - | ||||||||||||
1062 | */ | - | ||||||||||||
1063 | - | |||||||||||||
1064 | /*! \fn void QByteArray::push_back(const QByteArray &other) | - | ||||||||||||
1065 | - | |||||||||||||
1066 | This function is provided for STL compatibility. It is equivalent | - | ||||||||||||
1067 | to append(\a other). | - | ||||||||||||
1068 | */ | - | ||||||||||||
1069 | - | |||||||||||||
1070 | /*! \fn void QByteArray::push_back(const char *str) | - | ||||||||||||
1071 | - | |||||||||||||
1072 | \overload | - | ||||||||||||
1073 | - | |||||||||||||
1074 | Same as append(\a str). | - | ||||||||||||
1075 | */ | - | ||||||||||||
1076 | - | |||||||||||||
1077 | /*! \fn void QByteArray::push_back(char ch) | - | ||||||||||||
1078 | - | |||||||||||||
1079 | \overload | - | ||||||||||||
1080 | - | |||||||||||||
1081 | Same as append(\a ch). | - | ||||||||||||
1082 | */ | - | ||||||||||||
1083 | - | |||||||||||||
1084 | /*! \fn void QByteArray::push_front(const QByteArray &other) | - | ||||||||||||
1085 | - | |||||||||||||
1086 | This function is provided for STL compatibility. It is equivalent | - | ||||||||||||
1087 | to prepend(\a other). | - | ||||||||||||
1088 | */ | - | ||||||||||||
1089 | - | |||||||||||||
1090 | /*! \fn void QByteArray::push_front(const char *str) | - | ||||||||||||
1091 | - | |||||||||||||
1092 | \overload | - | ||||||||||||
1093 | - | |||||||||||||
1094 | Same as prepend(\a str). | - | ||||||||||||
1095 | */ | - | ||||||||||||
1096 | - | |||||||||||||
1097 | /*! \fn void QByteArray::push_front(char ch) | - | ||||||||||||
1098 | - | |||||||||||||
1099 | \overload | - | ||||||||||||
1100 | - | |||||||||||||
1101 | Same as prepend(\a ch). | - | ||||||||||||
1102 | */ | - | ||||||||||||
1103 | - | |||||||||||||
1104 | /*! \fn QByteArray::QByteArray(const QByteArray &other) | - | ||||||||||||
1105 | - | |||||||||||||
1106 | Constructs a copy of \a other. | - | ||||||||||||
1107 | - | |||||||||||||
1108 | This operation takes \l{constant time}, because QByteArray is | - | ||||||||||||
1109 | \l{implicitly shared}. This makes returning a QByteArray from a | - | ||||||||||||
1110 | function very fast. If a shared instance is modified, it will be | - | ||||||||||||
1111 | copied (copy-on-write), taking \l{linear time}. | - | ||||||||||||
1112 | - | |||||||||||||
1113 | \sa operator=() | - | ||||||||||||
1114 | */ | - | ||||||||||||
1115 | - | |||||||||||||
1116 | /*! | - | ||||||||||||
1117 | \fn QByteArray::QByteArray(QByteArray &&other) | - | ||||||||||||
1118 | - | |||||||||||||
1119 | Move-constructs a QByteArray instance, making it point at the same | - | ||||||||||||
1120 | object that \a other was pointing to. | - | ||||||||||||
1121 | - | |||||||||||||
1122 | \since 5.2 | - | ||||||||||||
1123 | */ | - | ||||||||||||
1124 | - | |||||||||||||
1125 | /*! \fn QByteArray::QByteArray(QByteArrayDataPtr dd) | - | ||||||||||||
1126 | - | |||||||||||||
1127 | \internal | - | ||||||||||||
1128 | - | |||||||||||||
1129 | Constructs a byte array pointing to the same data as \a dd. | - | ||||||||||||
1130 | */ | - | ||||||||||||
1131 | - | |||||||||||||
1132 | /*! \fn QByteArray::~QByteArray() | - | ||||||||||||
1133 | Destroys the byte array. | - | ||||||||||||
1134 | */ | - | ||||||||||||
1135 | - | |||||||||||||
1136 | /*! | - | ||||||||||||
1137 | Assigns \a other to this byte array and returns a reference to | - | ||||||||||||
1138 | this byte array. | - | ||||||||||||
1139 | */ | - | ||||||||||||
1140 | QByteArray &QByteArray::operator=(const QByteArray & other) Q_DECL_NOTHROW | - | ||||||||||||
1141 | { | - | ||||||||||||
1142 | other.d->ref.ref(); | - | ||||||||||||
1143 | if (!d->ref.deref())
| 118697-795264 | ||||||||||||
1144 | Data::deallocate(d); executed 118697 times by 77 tests: Data::deallocate(d); Executed by:
| 118697 | ||||||||||||
1145 | d = other.d; | - | ||||||||||||
1146 | return *this; executed 913961 times by 422 tests: return *this; Executed by:
| 913961 | ||||||||||||
1147 | } | - | ||||||||||||
1148 | - | |||||||||||||
1149 | - | |||||||||||||
1150 | /*! | - | ||||||||||||
1151 | \overload | - | ||||||||||||
1152 | - | |||||||||||||
1153 | Assigns \a str to this byte array. | - | ||||||||||||
1154 | */ | - | ||||||||||||
1155 | - | |||||||||||||
1156 | QByteArray &QByteArray::operator=(const char *str) | - | ||||||||||||
1157 | { | - | ||||||||||||
1158 | Data *x; | - | ||||||||||||
1159 | if (!str) { | - | ||||||||||||
1160 | x = Data::sharedNull(); | - | ||||||||||||
1161 | } else if (!*str) { | - | ||||||||||||
1162 | x = Data::allocate(0); | - | ||||||||||||
1163 | } else { | - | ||||||||||||
1164 | const int len = int(strlen(str)); | - | ||||||||||||
1165 | const uint fullLen = len + 1; | - | ||||||||||||
1166 | if (d->ref.isShared() || fullLen > d->alloc | - | ||||||||||||
1167 | || (len < d->size && fullLen < uint(d->alloc >> 1))) | - | ||||||||||||
1168 | reallocData(fullLen, d->detachFlags()); | - | ||||||||||||
1169 | x = d; | - | ||||||||||||
1170 | memcpy(x->data(), str, fullLen); // include null terminator | - | ||||||||||||
1171 | x->size = len; | - | ||||||||||||
1172 | } | - | ||||||||||||
1173 | x->ref.ref(); | - | ||||||||||||
1174 | if (!d->ref.deref()) | - | ||||||||||||
1175 | Data::deallocate(d); | - | ||||||||||||
1176 | d = x; | - | ||||||||||||
1177 | return *this; | - | ||||||||||||
1178 | } | - | ||||||||||||
1179 | - | |||||||||||||
1180 | /*! | - | ||||||||||||
1181 | \fn QByteArray &QByteArray::operator=(QByteArray &&other) | - | ||||||||||||
1182 | - | |||||||||||||
1183 | Move-assigns \a other to this QByteArray instance. | - | ||||||||||||
1184 | - | |||||||||||||
1185 | \since 5.2 | - | ||||||||||||
1186 | */ | - | ||||||||||||
1187 | - | |||||||||||||
1188 | /*! \fn void QByteArray::swap(QByteArray &other) | - | ||||||||||||
1189 | \since 4.8 | - | ||||||||||||
1190 | - | |||||||||||||
1191 | Swaps byte array \a other with this byte array. This operation is very | - | ||||||||||||
1192 | fast and never fails. | - | ||||||||||||
1193 | */ | - | ||||||||||||
1194 | - | |||||||||||||
1195 | /*! \fn int QByteArray::size() const | - | ||||||||||||
1196 | - | |||||||||||||
1197 | Returns the number of bytes in this byte array. | - | ||||||||||||
1198 | - | |||||||||||||
1199 | The last byte in the byte array is at position size() - 1. In addition, | - | ||||||||||||
1200 | QByteArray ensures that the byte at position size() is always '\\0', so | - | ||||||||||||
1201 | that you can use the return value of data() and constData() as arguments to | - | ||||||||||||
1202 | functions that expect '\\0'-terminated strings. If the QByteArray object | - | ||||||||||||
1203 | was created from a \l{fromRawData()}{raw data} that didn't include the | - | ||||||||||||
1204 | trailing null-termination character then QByteArray doesn't add it | - | ||||||||||||
1205 | automaticall unless the \l{deep copy} is created. | - | ||||||||||||
1206 | - | |||||||||||||
1207 | Example: | - | ||||||||||||
1208 | \snippet code/src_corelib_tools_qbytearray.cpp 6 | - | ||||||||||||
1209 | - | |||||||||||||
1210 | \sa isEmpty(), resize() | - | ||||||||||||
1211 | */ | - | ||||||||||||
1212 | - | |||||||||||||
1213 | /*! \fn bool QByteArray::isEmpty() const | - | ||||||||||||
1214 | - | |||||||||||||
1215 | Returns \c true if the byte array has size 0; otherwise returns \c false. | - | ||||||||||||
1216 | - | |||||||||||||
1217 | Example: | - | ||||||||||||
1218 | \snippet code/src_corelib_tools_qbytearray.cpp 7 | - | ||||||||||||
1219 | - | |||||||||||||
1220 | \sa size() | - | ||||||||||||
1221 | */ | - | ||||||||||||
1222 | - | |||||||||||||
1223 | /*! \fn int QByteArray::capacity() const | - | ||||||||||||
1224 | - | |||||||||||||
1225 | Returns the maximum number of bytes that can be stored in the | - | ||||||||||||
1226 | byte array without forcing a reallocation. | - | ||||||||||||
1227 | - | |||||||||||||
1228 | The sole purpose of this function is to provide a means of fine | - | ||||||||||||
1229 | tuning QByteArray's memory usage. In general, you will rarely | - | ||||||||||||
1230 | ever need to call this function. If you want to know how many | - | ||||||||||||
1231 | bytes are in the byte array, call size(). | - | ||||||||||||
1232 | - | |||||||||||||
1233 | \sa reserve(), squeeze() | - | ||||||||||||
1234 | */ | - | ||||||||||||
1235 | - | |||||||||||||
1236 | /*! \fn void QByteArray::reserve(int size) | - | ||||||||||||
1237 | - | |||||||||||||
1238 | Attempts to allocate memory for at least \a size bytes. If you | - | ||||||||||||
1239 | know in advance how large the byte array will be, you can call | - | ||||||||||||
1240 | this function, and if you call resize() often you are likely to | - | ||||||||||||
1241 | get better performance. If \a size is an underestimate, the worst | - | ||||||||||||
1242 | that will happen is that the QByteArray will be a bit slower. | - | ||||||||||||
1243 | - | |||||||||||||
1244 | The sole purpose of this function is to provide a means of fine | - | ||||||||||||
1245 | tuning QByteArray's memory usage. In general, you will rarely | - | ||||||||||||
1246 | ever need to call this function. If you want to change the size | - | ||||||||||||
1247 | of the byte array, call resize(). | - | ||||||||||||
1248 | - | |||||||||||||
1249 | \sa squeeze(), capacity() | - | ||||||||||||
1250 | */ | - | ||||||||||||
1251 | - | |||||||||||||
1252 | /*! \fn void QByteArray::squeeze() | - | ||||||||||||
1253 | - | |||||||||||||
1254 | Releases any memory not required to store the array's data. | - | ||||||||||||
1255 | - | |||||||||||||
1256 | The sole purpose of this function is to provide a means of fine | - | ||||||||||||
1257 | tuning QByteArray's memory usage. In general, you will rarely | - | ||||||||||||
1258 | ever need to call this function. | - | ||||||||||||
1259 | - | |||||||||||||
1260 | \sa reserve(), capacity() | - | ||||||||||||
1261 | */ | - | ||||||||||||
1262 | - | |||||||||||||
1263 | /*! \fn QByteArray::operator const char *() const | - | ||||||||||||
1264 | \fn QByteArray::operator const void *() const | - | ||||||||||||
1265 | - | |||||||||||||
1266 | \obsolete Use constData() instead. | - | ||||||||||||
1267 | - | |||||||||||||
1268 | Returns a pointer to the data stored in the byte array. The | - | ||||||||||||
1269 | pointer can be used to access the bytes that compose the array. | - | ||||||||||||
1270 | The data is '\\0'-terminated. The pointer remains valid as long | - | ||||||||||||
1271 | as the array isn't reallocated or destroyed. | - | ||||||||||||
1272 | - | |||||||||||||
1273 | This operator is mostly useful to pass a byte array to a function | - | ||||||||||||
1274 | that accepts a \c{const char *}. | - | ||||||||||||
1275 | - | |||||||||||||
1276 | You can disable this operator by defining \c | - | ||||||||||||
1277 | QT_NO_CAST_FROM_BYTEARRAY when you compile your applications. | - | ||||||||||||
1278 | - | |||||||||||||
1279 | Note: A QByteArray can store any byte values including '\\0's, | - | ||||||||||||
1280 | but most functions that take \c{char *} arguments assume that the | - | ||||||||||||
1281 | data ends at the first '\\0' they encounter. | - | ||||||||||||
1282 | - | |||||||||||||
1283 | \sa constData() | - | ||||||||||||
1284 | */ | - | ||||||||||||
1285 | - | |||||||||||||
1286 | /*! | - | ||||||||||||
1287 | \macro QT_NO_CAST_FROM_BYTEARRAY | - | ||||||||||||
1288 | \relates QByteArray | - | ||||||||||||
1289 | - | |||||||||||||
1290 | Disables automatic conversions from QByteArray to | - | ||||||||||||
1291 | const char * or const void *. | - | ||||||||||||
1292 | - | |||||||||||||
1293 | \sa QT_NO_CAST_TO_ASCII, QT_NO_CAST_FROM_ASCII | - | ||||||||||||
1294 | */ | - | ||||||||||||
1295 | - | |||||||||||||
1296 | /*! \fn char *QByteArray::data() | - | ||||||||||||
1297 | - | |||||||||||||
1298 | Returns a pointer to the data stored in the byte array. The | - | ||||||||||||
1299 | pointer can be used to access and modify the bytes that compose | - | ||||||||||||
1300 | the array. The data is '\\0'-terminated, i.e. the number of | - | ||||||||||||
1301 | bytes in the returned character string is size() + 1 for the | - | ||||||||||||
1302 | '\\0' terminator. | - | ||||||||||||
1303 | - | |||||||||||||
1304 | Example: | - | ||||||||||||
1305 | \snippet code/src_corelib_tools_qbytearray.cpp 8 | - | ||||||||||||
1306 | - | |||||||||||||
1307 | The pointer remains valid as long as the byte array isn't | - | ||||||||||||
1308 | reallocated or destroyed. For read-only access, constData() is | - | ||||||||||||
1309 | faster because it never causes a \l{deep copy} to occur. | - | ||||||||||||
1310 | - | |||||||||||||
1311 | This function is mostly useful to pass a byte array to a function | - | ||||||||||||
1312 | that accepts a \c{const char *}. | - | ||||||||||||
1313 | - | |||||||||||||
1314 | The following example makes a copy of the char* returned by | - | ||||||||||||
1315 | data(), but it will corrupt the heap and cause a crash because it | - | ||||||||||||
1316 | does not allocate a byte for the '\\0' at the end: | - | ||||||||||||
1317 | - | |||||||||||||
1318 | \snippet code/src_corelib_tools_qbytearray.cpp 46 | - | ||||||||||||
1319 | - | |||||||||||||
1320 | This one allocates the correct amount of space: | - | ||||||||||||
1321 | - | |||||||||||||
1322 | \snippet code/src_corelib_tools_qbytearray.cpp 47 | - | ||||||||||||
1323 | - | |||||||||||||
1324 | Note: A QByteArray can store any byte values including '\\0's, | - | ||||||||||||
1325 | but most functions that take \c{char *} arguments assume that the | - | ||||||||||||
1326 | data ends at the first '\\0' they encounter. | - | ||||||||||||
1327 | - | |||||||||||||
1328 | \sa constData(), operator[]() | - | ||||||||||||
1329 | */ | - | ||||||||||||
1330 | - | |||||||||||||
1331 | /*! \fn const char *QByteArray::data() const | - | ||||||||||||
1332 | - | |||||||||||||
1333 | \overload | - | ||||||||||||
1334 | */ | - | ||||||||||||
1335 | - | |||||||||||||
1336 | /*! \fn const char *QByteArray::constData() const | - | ||||||||||||
1337 | - | |||||||||||||
1338 | Returns a pointer to the data stored in the byte array. The pointer can be | - | ||||||||||||
1339 | used to access the bytes that compose the array. The data is | - | ||||||||||||
1340 | '\\0'-terminated unless the QByteArray object was created from raw data. | - | ||||||||||||
1341 | The pointer remains valid as long as the byte array isn't reallocated or | - | ||||||||||||
1342 | destroyed. | - | ||||||||||||
1343 | - | |||||||||||||
1344 | This function is mostly useful to pass a byte array to a function | - | ||||||||||||
1345 | that accepts a \c{const char *}. | - | ||||||||||||
1346 | - | |||||||||||||
1347 | Note: A QByteArray can store any byte values including '\\0's, | - | ||||||||||||
1348 | but most functions that take \c{char *} arguments assume that the | - | ||||||||||||
1349 | data ends at the first '\\0' they encounter. | - | ||||||||||||
1350 | - | |||||||||||||
1351 | \sa data(), operator[](), fromRawData() | - | ||||||||||||
1352 | */ | - | ||||||||||||
1353 | - | |||||||||||||
1354 | /*! \fn void QByteArray::detach() | - | ||||||||||||
1355 | - | |||||||||||||
1356 | \internal | - | ||||||||||||
1357 | */ | - | ||||||||||||
1358 | - | |||||||||||||
1359 | /*! \fn bool QByteArray::isDetached() const | - | ||||||||||||
1360 | - | |||||||||||||
1361 | \internal | - | ||||||||||||
1362 | */ | - | ||||||||||||
1363 | - | |||||||||||||
1364 | /*! \fn bool QByteArray::isSharedWith(const QByteArray &other) const | - | ||||||||||||
1365 | - | |||||||||||||
1366 | \internal | - | ||||||||||||
1367 | */ | - | ||||||||||||
1368 | - | |||||||||||||
1369 | /*! \fn char QByteArray::at(int i) const | - | ||||||||||||
1370 | - | |||||||||||||
1371 | Returns the character at index position \a i in the byte array. | - | ||||||||||||
1372 | - | |||||||||||||
1373 | \a i must be a valid index position in the byte array (i.e., 0 <= | - | ||||||||||||
1374 | \a i < size()). | - | ||||||||||||
1375 | - | |||||||||||||
1376 | \sa operator[]() | - | ||||||||||||
1377 | */ | - | ||||||||||||
1378 | - | |||||||||||||
1379 | /*! \fn QByteRef QByteArray::operator[](int i) | - | ||||||||||||
1380 | - | |||||||||||||
1381 | Returns the byte at index position \a i as a modifiable reference. | - | ||||||||||||
1382 | - | |||||||||||||
1383 | If an assignment is made beyond the end of the byte array, the | - | ||||||||||||
1384 | array is extended with resize() before the assignment takes | - | ||||||||||||
1385 | place. | - | ||||||||||||
1386 | - | |||||||||||||
1387 | Example: | - | ||||||||||||
1388 | \snippet code/src_corelib_tools_qbytearray.cpp 9 | - | ||||||||||||
1389 | - | |||||||||||||
1390 | The return value is of type QByteRef, a helper class for | - | ||||||||||||
1391 | QByteArray. When you get an object of type QByteRef, you can use | - | ||||||||||||
1392 | it as if it were a char &. If you assign to it, the assignment | - | ||||||||||||
1393 | will apply to the character in the QByteArray from which you got | - | ||||||||||||
1394 | the reference. | - | ||||||||||||
1395 | - | |||||||||||||
1396 | \sa at() | - | ||||||||||||
1397 | */ | - | ||||||||||||
1398 | - | |||||||||||||
1399 | /*! \fn char QByteArray::operator[](int i) const | - | ||||||||||||
1400 | - | |||||||||||||
1401 | \overload | - | ||||||||||||
1402 | - | |||||||||||||
1403 | Same as at(\a i). | - | ||||||||||||
1404 | */ | - | ||||||||||||
1405 | - | |||||||||||||
1406 | /*! \fn QByteRef QByteArray::operator[](uint i) | - | ||||||||||||
1407 | - | |||||||||||||
1408 | \overload | - | ||||||||||||
1409 | */ | - | ||||||||||||
1410 | - | |||||||||||||
1411 | /*! \fn char QByteArray::operator[](uint i) const | - | ||||||||||||
1412 | - | |||||||||||||
1413 | \overload | - | ||||||||||||
1414 | */ | - | ||||||||||||
1415 | - | |||||||||||||
1416 | /*! \fn bool QByteArray::contains(const QByteArray &ba) const | - | ||||||||||||
1417 | - | |||||||||||||
1418 | Returns \c true if the byte array contains an occurrence of the byte | - | ||||||||||||
1419 | array \a ba; otherwise returns \c false. | - | ||||||||||||
1420 | - | |||||||||||||
1421 | \sa indexOf(), count() | - | ||||||||||||
1422 | */ | - | ||||||||||||
1423 | - | |||||||||||||
1424 | /*! \fn bool QByteArray::contains(const char *str) const | - | ||||||||||||
1425 | - | |||||||||||||
1426 | \overload | - | ||||||||||||
1427 | - | |||||||||||||
1428 | Returns \c true if the byte array contains the string \a str; | - | ||||||||||||
1429 | otherwise returns \c false. | - | ||||||||||||
1430 | */ | - | ||||||||||||
1431 | - | |||||||||||||
1432 | /*! \fn bool QByteArray::contains(char ch) const | - | ||||||||||||
1433 | - | |||||||||||||
1434 | \overload | - | ||||||||||||
1435 | - | |||||||||||||
1436 | Returns \c true if the byte array contains the character \a ch; | - | ||||||||||||
1437 | otherwise returns \c false. | - | ||||||||||||
1438 | */ | - | ||||||||||||
1439 | - | |||||||||||||
1440 | /*! | - | ||||||||||||
1441 | - | |||||||||||||
1442 | Truncates the byte array at index position \a pos. | - | ||||||||||||
1443 | - | |||||||||||||
1444 | If \a pos is beyond the end of the array, nothing happens. | - | ||||||||||||
1445 | - | |||||||||||||
1446 | Example: | - | ||||||||||||
1447 | \snippet code/src_corelib_tools_qbytearray.cpp 10 | - | ||||||||||||
1448 | - | |||||||||||||
1449 | \sa chop(), resize(), left() | - | ||||||||||||
1450 | */ | - | ||||||||||||
1451 | void QByteArray::truncate(int pos) | - | ||||||||||||
1452 | { | - | ||||||||||||
1453 | if (pos < d->size) | - | ||||||||||||
1454 | resize(pos); | - | ||||||||||||
1455 | } | - | ||||||||||||
1456 | - | |||||||||||||
1457 | /*! | - | ||||||||||||
1458 | - | |||||||||||||
1459 | Removes \a n bytes from the end of the byte array. | - | ||||||||||||
1460 | - | |||||||||||||
1461 | If \a n is greater than size(), the result is an empty byte | - | ||||||||||||
1462 | array. | - | ||||||||||||
1463 | - | |||||||||||||
1464 | Example: | - | ||||||||||||
1465 | \snippet code/src_corelib_tools_qbytearray.cpp 11 | - | ||||||||||||
1466 | - | |||||||||||||
1467 | \sa truncate(), resize(), left() | - | ||||||||||||
1468 | */ | - | ||||||||||||
1469 | - | |||||||||||||
1470 | void QByteArray::chop(int n) | - | ||||||||||||
1471 | { | - | ||||||||||||
1472 | if (n > 0) | - | ||||||||||||
1473 | resize(d->size - n); | - | ||||||||||||
1474 | } | - | ||||||||||||
1475 | - | |||||||||||||
1476 | - | |||||||||||||
1477 | /*! \fn QByteArray &QByteArray::operator+=(const QByteArray &ba) | - | ||||||||||||
1478 | - | |||||||||||||
1479 | Appends the byte array \a ba onto the end of this byte array and | - | ||||||||||||
1480 | returns a reference to this byte array. | - | ||||||||||||
1481 | - | |||||||||||||
1482 | Example: | - | ||||||||||||
1483 | \snippet code/src_corelib_tools_qbytearray.cpp 12 | - | ||||||||||||
1484 | - | |||||||||||||
1485 | Note: QByteArray is an \l{implicitly shared} class. Consequently, | - | ||||||||||||
1486 | if \e this is an empty QByteArray, then \e this will just share | - | ||||||||||||
1487 | the data held in \a ba. In this case, no copying of data is done, | - | ||||||||||||
1488 | taking \l{constant time}. If a shared instance is modified, it will | - | ||||||||||||
1489 | be copied (copy-on-write), taking \l{linear time}. | - | ||||||||||||
1490 | - | |||||||||||||
1491 | If \e this is not an empty QByteArray, a deep copy of the data is | - | ||||||||||||
1492 | performed, taking \l{linear time}. | - | ||||||||||||
1493 | - | |||||||||||||
1494 | This operation typically does not suffer from allocation overhead, | - | ||||||||||||
1495 | because QByteArray preallocates extra space at the end of the data | - | ||||||||||||
1496 | so that it may grow without reallocating for each append operation. | - | ||||||||||||
1497 | - | |||||||||||||
1498 | \sa append(), prepend() | - | ||||||||||||
1499 | */ | - | ||||||||||||
1500 | - | |||||||||||||
1501 | /*! \fn QByteArray &QByteArray::operator+=(const QString &str) | - | ||||||||||||
1502 | - | |||||||||||||
1503 | \overload | - | ||||||||||||
1504 | - | |||||||||||||
1505 | Appends the string \a str onto the end of this byte array and | - | ||||||||||||
1506 | returns a reference to this byte array. The Unicode data is | - | ||||||||||||
1507 | converted into 8-bit characters using QString::toUtf8(). | - | ||||||||||||
1508 | - | |||||||||||||
1509 | You can disable this function by defining \c QT_NO_CAST_TO_ASCII when you | - | ||||||||||||
1510 | compile your applications. You then need to call QString::toUtf8() (or | - | ||||||||||||
1511 | QString::toLatin1() or QString::toLocal8Bit()) explicitly if you want to | - | ||||||||||||
1512 | convert the data to \c{const char *}. | - | ||||||||||||
1513 | */ | - | ||||||||||||
1514 | - | |||||||||||||
1515 | /*! \fn QByteArray &QByteArray::operator+=(const char *str) | - | ||||||||||||
1516 | - | |||||||||||||
1517 | \overload | - | ||||||||||||
1518 | - | |||||||||||||
1519 | Appends the string \a str onto the end of this byte array and | - | ||||||||||||
1520 | returns a reference to this byte array. | - | ||||||||||||
1521 | */ | - | ||||||||||||
1522 | - | |||||||||||||
1523 | /*! \fn QByteArray &QByteArray::operator+=(char ch) | - | ||||||||||||
1524 | - | |||||||||||||
1525 | \overload | - | ||||||||||||
1526 | - | |||||||||||||
1527 | Appends the character \a ch onto the end of this byte array and | - | ||||||||||||
1528 | returns a reference to this byte array. | - | ||||||||||||
1529 | */ | - | ||||||||||||
1530 | - | |||||||||||||
1531 | /*! \fn int QByteArray::length() const | - | ||||||||||||
1532 | - | |||||||||||||
1533 | Same as size(). | - | ||||||||||||
1534 | */ | - | ||||||||||||
1535 | - | |||||||||||||
1536 | /*! \fn bool QByteArray::isNull() const | - | ||||||||||||
1537 | - | |||||||||||||
1538 | Returns \c true if this byte array is null; otherwise returns \c false. | - | ||||||||||||
1539 | - | |||||||||||||
1540 | Example: | - | ||||||||||||
1541 | \snippet code/src_corelib_tools_qbytearray.cpp 13 | - | ||||||||||||
1542 | - | |||||||||||||
1543 | Qt makes a distinction between null byte arrays and empty byte | - | ||||||||||||
1544 | arrays for historical reasons. For most applications, what | - | ||||||||||||
1545 | matters is whether or not a byte array contains any data, | - | ||||||||||||
1546 | and this can be determined using isEmpty(). | - | ||||||||||||
1547 | - | |||||||||||||
1548 | \sa isEmpty() | - | ||||||||||||
1549 | */ | - | ||||||||||||
1550 | - | |||||||||||||
1551 | /*! \fn QByteArray::QByteArray() | - | ||||||||||||
1552 | - | |||||||||||||
1553 | Constructs an empty byte array. | - | ||||||||||||
1554 | - | |||||||||||||
1555 | \sa isEmpty() | - | ||||||||||||
1556 | */ | - | ||||||||||||
1557 | - | |||||||||||||
1558 | /*! | - | ||||||||||||
1559 | Constructs a byte array containing the first \a size bytes of | - | ||||||||||||
1560 | array \a data. | - | ||||||||||||
1561 | - | |||||||||||||
1562 | If \a data is 0, a null byte array is constructed. | - | ||||||||||||
1563 | - | |||||||||||||
1564 | If \a size is negative, \a data is assumed to point to a nul-terminated | - | ||||||||||||
1565 | string and its length is determined dynamically. The terminating | - | ||||||||||||
1566 | nul-character is not considered part of the byte array. | - | ||||||||||||
1567 | - | |||||||||||||
1568 | QByteArray makes a deep copy of the string data. | - | ||||||||||||
1569 | - | |||||||||||||
1570 | \sa fromRawData() | - | ||||||||||||
1571 | */ | - | ||||||||||||
1572 | - | |||||||||||||
1573 | QByteArray::QByteArray(const char *data, int size) | - | ||||||||||||
1574 | { | - | ||||||||||||
1575 | if (!data) { | - | ||||||||||||
1576 | d = Data::sharedNull(); | - | ||||||||||||
1577 | } else { | - | ||||||||||||
1578 | if (size < 0) | - | ||||||||||||
1579 | size = int(strlen(data)); | - | ||||||||||||
1580 | if (!size) { | - | ||||||||||||
1581 | d = Data::allocate(0); | - | ||||||||||||
1582 | } else { | - | ||||||||||||
1583 | d = Data::allocate(uint(size) + 1u); | - | ||||||||||||
1584 | Q_CHECK_PTR(d); | - | ||||||||||||
1585 | d->size = size; | - | ||||||||||||
1586 | memcpy(d->data(), data, size); | - | ||||||||||||
1587 | d->data()[size] = '\0'; | - | ||||||||||||
1588 | } | - | ||||||||||||
1589 | } | - | ||||||||||||
1590 | } | - | ||||||||||||
1591 | - | |||||||||||||
1592 | /*! | - | ||||||||||||
1593 | Constructs a byte array of size \a size with every byte set to | - | ||||||||||||
1594 | character \a ch. | - | ||||||||||||
1595 | - | |||||||||||||
1596 | \sa fill() | - | ||||||||||||
1597 | */ | - | ||||||||||||
1598 | - | |||||||||||||
1599 | QByteArray::QByteArray(int size, char ch) | - | ||||||||||||
1600 | { | - | ||||||||||||
1601 | if (size <= 0) { | - | ||||||||||||
1602 | d = Data::allocate(0); | - | ||||||||||||
1603 | } else { | - | ||||||||||||
1604 | d = Data::allocate(uint(size) + 1u); | - | ||||||||||||
1605 | Q_CHECK_PTR(d); | - | ||||||||||||
1606 | d->size = size; | - | ||||||||||||
1607 | memset(d->data(), ch, size); | - | ||||||||||||
1608 | d->data()[size] = '\0'; | - | ||||||||||||
1609 | } | - | ||||||||||||
1610 | } | - | ||||||||||||
1611 | - | |||||||||||||
1612 | /*! | - | ||||||||||||
1613 | \internal | - | ||||||||||||
1614 | - | |||||||||||||
1615 | Constructs a byte array of size \a size with uninitialized contents. | - | ||||||||||||
1616 | */ | - | ||||||||||||
1617 | - | |||||||||||||
1618 | QByteArray::QByteArray(int size, Qt::Initialization) | - | ||||||||||||
1619 | { | - | ||||||||||||
1620 | d = Data::allocate(uint(size) + 1u); | - | ||||||||||||
1621 | Q_CHECK_PTR(d); | - | ||||||||||||
1622 | d->size = size; | - | ||||||||||||
1623 | d->data()[size] = '\0'; | - | ||||||||||||
1624 | } | - | ||||||||||||
1625 | - | |||||||||||||
1626 | /*! | - | ||||||||||||
1627 | Sets the size of the byte array to \a size bytes. | - | ||||||||||||
1628 | - | |||||||||||||
1629 | If \a size is greater than the current size, the byte array is | - | ||||||||||||
1630 | extended to make it \a size bytes with the extra bytes added to | - | ||||||||||||
1631 | the end. The new bytes are uninitialized. | - | ||||||||||||
1632 | - | |||||||||||||
1633 | If \a size is less than the current size, bytes are removed from | - | ||||||||||||
1634 | the end. | - | ||||||||||||
1635 | - | |||||||||||||
1636 | \sa size(), truncate() | - | ||||||||||||
1637 | */ | - | ||||||||||||
1638 | void QByteArray::resize(int size) | - | ||||||||||||
1639 | { | - | ||||||||||||
1640 | if (size < 0) | - | ||||||||||||
1641 | size = 0; | - | ||||||||||||
1642 | - | |||||||||||||
1643 | if (IS_RAW_DATA(d) && !d->ref.isShared() && size < d->size) { | - | ||||||||||||
1644 | d->size = size; | - | ||||||||||||
1645 | return; | - | ||||||||||||
1646 | } | - | ||||||||||||
1647 | - | |||||||||||||
1648 | if (size == 0 && !d->capacityReserved) { | - | ||||||||||||
1649 | Data *x = Data::allocate(0); | - | ||||||||||||
1650 | if (!d->ref.deref()) | - | ||||||||||||
1651 | Data::deallocate(d); | - | ||||||||||||
1652 | d = x; | - | ||||||||||||
1653 | } else if (d->size == 0 && d->ref.isStatic()) { | - | ||||||||||||
1654 | // | - | ||||||||||||
1655 | // Optimize the idiom: | - | ||||||||||||
1656 | // QByteArray a; | - | ||||||||||||
1657 | // a.resize(sz); | - | ||||||||||||
1658 | // ... | - | ||||||||||||
1659 | // which is used in place of the Qt 3 idiom: | - | ||||||||||||
1660 | // QByteArray a(sz); | - | ||||||||||||
1661 | // | - | ||||||||||||
1662 | Data *x = Data::allocate(uint(size) + 1u); | - | ||||||||||||
1663 | Q_CHECK_PTR(x); | - | ||||||||||||
1664 | x->size = size; | - | ||||||||||||
1665 | x->data()[size] = '\0'; | - | ||||||||||||
1666 | d = x; | - | ||||||||||||
1667 | } else { | - | ||||||||||||
1668 | if (d->ref.isShared() || uint(size) + 1u > d->alloc | - | ||||||||||||
1669 | || (!d->capacityReserved && size < d->size | - | ||||||||||||
1670 | && uint(size) + 1u < uint(d->alloc >> 1))) | - | ||||||||||||
1671 | reallocData(uint(size) + 1u, d->detachFlags() | Data::Grow); | - | ||||||||||||
1672 | if (d->alloc) { | - | ||||||||||||
1673 | d->size = size; | - | ||||||||||||
1674 | d->data()[size] = '\0'; | - | ||||||||||||
1675 | } | - | ||||||||||||
1676 | } | - | ||||||||||||
1677 | } | - | ||||||||||||
1678 | - | |||||||||||||
1679 | /*! | - | ||||||||||||
1680 | Sets every byte in the byte array to character \a ch. If \a size | - | ||||||||||||
1681 | is different from -1 (the default), the byte array is resized to | - | ||||||||||||
1682 | size \a size beforehand. | - | ||||||||||||
1683 | - | |||||||||||||
1684 | Example: | - | ||||||||||||
1685 | \snippet code/src_corelib_tools_qbytearray.cpp 14 | - | ||||||||||||
1686 | - | |||||||||||||
1687 | \sa resize() | - | ||||||||||||
1688 | */ | - | ||||||||||||
1689 | - | |||||||||||||
1690 | QByteArray &QByteArray::fill(char ch, int size) | - | ||||||||||||
1691 | { | - | ||||||||||||
1692 | resize(size < 0 ? d->size : size); | - | ||||||||||||
1693 | if (d->size) | - | ||||||||||||
1694 | memset(d->data(), ch, d->size); | - | ||||||||||||
1695 | return *this; | - | ||||||||||||
1696 | } | - | ||||||||||||
1697 | - | |||||||||||||
1698 | void QByteArray::reallocData(uint alloc, Data::AllocationOptions options) | - | ||||||||||||
1699 | { | - | ||||||||||||
1700 | if (d->ref.isShared() || IS_RAW_DATA(d)) {
| 5-1652549 | ||||||||||||
1701 | Data *x = Data::allocate(alloc, options); | - | ||||||||||||
1702 | Q_CHECK_PTR(x); never executed: qBadAlloc();
| 0-1652554 | ||||||||||||
1703 | x->size = qMin(int(alloc) - 1, d->size); | - | ||||||||||||
1704 | ::memcpy(x->data(), d->data(), x->size); | - | ||||||||||||
1705 | x->data()[x->size] = '\0'; | - | ||||||||||||
1706 | if (!d->ref.deref())
| 5-1652549 | ||||||||||||
1707 | Data::deallocate(d); executed 5 times by 3 tests: Data::deallocate(d); Executed by:
| 5 | ||||||||||||
1708 | d = x; | - | ||||||||||||
1709 | } else { executed 1652554 times by 421 tests: end of block Executed by:
| 1652554 | ||||||||||||
1710 | size_t blockSize; | - | ||||||||||||
1711 | if (options & Data::Grow) {
| 41870-1485537 | ||||||||||||
1712 | ifauto r = qCalculateGrowingBlockSize(alloc> MaxByteArraySize) | - | ||||||||||||
qBadAlloc();, sizeof(QChar), sizeof(Data)); | ||||||||||||||
1713 | blockSize = r.size; | - | ||||||||||||
1714 | alloc = qAllocMoreuint(r.elementCount); | - | ||||||||||||
1715 | } else { executed 1485537 times by 438 tests: end of block Executed by:
| 1485537 | ||||||||||||
1716 | blockSize = qCalculateBlockSize(alloc, sizeof(QChar), sizeof(Data)); | - | ||||||||||||
1717 | } executed 41870 times by 56 tests: end of block Executed by:
| 41870 | ||||||||||||
1718 | - | |||||||||||||
1719 | Data *x = static_cast<Data *>(::realloc(d, sizeof(Data) + allocblockSize)); | - | ||||||||||||
1720 | Q_CHECK_PTR(x); never executed: qBadAlloc();
| 0-1527407 | ||||||||||||
1721 | x->alloc = alloc; | - | ||||||||||||
1722 | x->capacityReserved = (options & Data::CapacityReserved) ? 1 : 0;
| 11097-1516310 | ||||||||||||
1723 | d = x; | - | ||||||||||||
1724 | } executed 1527407 times by 438 tests: end of block Executed by:
| 1527407 | ||||||||||||
1725 | } | - | ||||||||||||
1726 | - | |||||||||||||
1727 | void QByteArray::expand(int i) | - | ||||||||||||
1728 | { | - | ||||||||||||
1729 | resize(qMax(i + 1, d->size)); | - | ||||||||||||
1730 | } | - | ||||||||||||
1731 | - | |||||||||||||
1732 | /*! | - | ||||||||||||
1733 | \internal | - | ||||||||||||
1734 | Return a QByteArray that is sure to be NUL-terminated. | - | ||||||||||||
1735 | - | |||||||||||||
1736 | By default, all QByteArray have an extra NUL at the end, | - | ||||||||||||
1737 | guaranteeing that assumption. However, if QByteArray::fromRawData | - | ||||||||||||
1738 | is used, then the NUL is there only if the user put it there. We | - | ||||||||||||
1739 | can't be sure. | - | ||||||||||||
1740 | */ | - | ||||||||||||
1741 | QByteArray QByteArray::nulTerminated() const | - | ||||||||||||
1742 | { | - | ||||||||||||
1743 | // is this fromRawData? | - | ||||||||||||
1744 | if (!IS_RAW_DATA(d)) | - | ||||||||||||
1745 | return *this; // no, then we're sure we're zero terminated | - | ||||||||||||
1746 | - | |||||||||||||
1747 | QByteArray copy(*this); | - | ||||||||||||
1748 | copy.detach(); | - | ||||||||||||
1749 | return copy; | - | ||||||||||||
1750 | } | - | ||||||||||||
1751 | - | |||||||||||||
1752 | /*! | - | ||||||||||||
1753 | Prepends the byte array \a ba to this byte array and returns a | - | ||||||||||||
1754 | reference to this byte array. | - | ||||||||||||
1755 | - | |||||||||||||
1756 | Example: | - | ||||||||||||
1757 | \snippet code/src_corelib_tools_qbytearray.cpp 15 | - | ||||||||||||
1758 | - | |||||||||||||
1759 | This is the same as insert(0, \a ba). | - | ||||||||||||
1760 | - | |||||||||||||
1761 | Note: QByteArray is an \l{implicitly shared} class. Consequently, | - | ||||||||||||
1762 | if \e this is an empty QByteArray, then \e this will just share | - | ||||||||||||
1763 | the data held in \a ba. In this case, no copying of data is done, | - | ||||||||||||
1764 | taking \l{constant time}. If a shared instance is modified, it will | - | ||||||||||||
1765 | be copied (copy-on-write), taking \l{linear time}. | - | ||||||||||||
1766 | - | |||||||||||||
1767 | If \e this is not an empty QByteArray, a deep copy of the data is | - | ||||||||||||
1768 | performed, taking \l{linear time}. | - | ||||||||||||
1769 | - | |||||||||||||
1770 | \sa append(), insert() | - | ||||||||||||
1771 | */ | - | ||||||||||||
1772 | - | |||||||||||||
1773 | QByteArray &QByteArray::prepend(const QByteArray &ba) | - | ||||||||||||
1774 | { | - | ||||||||||||
1775 | if (d->size == 0 && d->ref.isStatic() && !IS_RAW_DATA(ba.d)) { | - | ||||||||||||
1776 | *this = ba; | - | ||||||||||||
1777 | } else if (ba.d->size != 0) { | - | ||||||||||||
1778 | QByteArray tmp = *this; | - | ||||||||||||
1779 | *this = ba; | - | ||||||||||||
1780 | append(tmp); | - | ||||||||||||
1781 | } | - | ||||||||||||
1782 | return *this; | - | ||||||||||||
1783 | } | - | ||||||||||||
1784 | - | |||||||||||||
1785 | /*! | - | ||||||||||||
1786 | \overload | - | ||||||||||||
1787 | - | |||||||||||||
1788 | Prepends the string \a str to this byte array. | - | ||||||||||||
1789 | */ | - | ||||||||||||
1790 | - | |||||||||||||
1791 | QByteArray &QByteArray::prepend(const char *str) | - | ||||||||||||
1792 | { | - | ||||||||||||
1793 | return prepend(str, qstrlen(str)); | - | ||||||||||||
1794 | } | - | ||||||||||||
1795 | - | |||||||||||||
1796 | /*! | - | ||||||||||||
1797 | \overload | - | ||||||||||||
1798 | \since 4.6 | - | ||||||||||||
1799 | - | |||||||||||||
1800 | Prepends \a len bytes of the string \a str to this byte array. | - | ||||||||||||
1801 | */ | - | ||||||||||||
1802 | - | |||||||||||||
1803 | QByteArray &QByteArray::prepend(const char *str, int len) | - | ||||||||||||
1804 | { | - | ||||||||||||
1805 | if (str) { | - | ||||||||||||
1806 | if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc) | - | ||||||||||||
1807 | reallocData(uint(d->size + len) + 1u, d->detachFlags() | Data::Grow); | - | ||||||||||||
1808 | memmove(d->data()+len, d->data(), d->size); | - | ||||||||||||
1809 | memcpy(d->data(), str, len); | - | ||||||||||||
1810 | d->size += len; | - | ||||||||||||
1811 | d->data()[d->size] = '\0'; | - | ||||||||||||
1812 | } | - | ||||||||||||
1813 | return *this; | - | ||||||||||||
1814 | } | - | ||||||||||||
1815 | - | |||||||||||||
1816 | /*! \fn QByteArray &QByteArray::prepend(int count, char ch) | - | ||||||||||||
1817 | - | |||||||||||||
1818 | \overload | - | ||||||||||||
1819 | \since 5.7 | - | ||||||||||||
1820 | - | |||||||||||||
1821 | Prepends \a count copies of character \a ch to this byte array. | - | ||||||||||||
1822 | */ | - | ||||||||||||
1823 | - | |||||||||||||
1824 | /*! | - | ||||||||||||
1825 | \overload | - | ||||||||||||
1826 | - | |||||||||||||
1827 | Prepends the character \a ch to this byte array. | - | ||||||||||||
1828 | */ | - | ||||||||||||
1829 | - | |||||||||||||
1830 | QByteArray &QByteArray::prepend(char ch) | - | ||||||||||||
1831 | { | - | ||||||||||||
1832 | if (d->ref.isShared() || uint(d->size) + 2u > d->alloc) | - | ||||||||||||
1833 | reallocData(uint(d->size) + 2u, d->detachFlags() | Data::Grow); | - | ||||||||||||
1834 | memmove(d->data()+1, d->data(), d->size); | - | ||||||||||||
1835 | d->data()[0] = ch; | - | ||||||||||||
1836 | ++d->size; | - | ||||||||||||
1837 | d->data()[d->size] = '\0'; | - | ||||||||||||
1838 | return *this; | - | ||||||||||||
1839 | } | - | ||||||||||||
1840 | - | |||||||||||||
1841 | /*! | - | ||||||||||||
1842 | Appends the byte array \a ba onto the end of this byte array. | - | ||||||||||||
1843 | - | |||||||||||||
1844 | Example: | - | ||||||||||||
1845 | \snippet code/src_corelib_tools_qbytearray.cpp 16 | - | ||||||||||||
1846 | - | |||||||||||||
1847 | This is the same as insert(size(), \a ba). | - | ||||||||||||
1848 | - | |||||||||||||
1849 | Note: QByteArray is an \l{implicitly shared} class. Consequently, | - | ||||||||||||
1850 | if \e this is an empty QByteArray, then \e this will just share | - | ||||||||||||
1851 | the data held in \a ba. In this case, no copying of data is done, | - | ||||||||||||
1852 | taking \l{constant time}. If a shared instance is modified, it will | - | ||||||||||||
1853 | be copied (copy-on-write), taking \l{linear time}. | - | ||||||||||||
1854 | - | |||||||||||||
1855 | If \e this is not an empty QByteArray, a deep copy of the data is | - | ||||||||||||
1856 | performed, taking \l{linear time}. | - | ||||||||||||
1857 | - | |||||||||||||
1858 | This operation typically does not suffer from allocation overhead, | - | ||||||||||||
1859 | because QByteArray preallocates extra space at the end of the data | - | ||||||||||||
1860 | so that it may grow without reallocating for each append operation. | - | ||||||||||||
1861 | - | |||||||||||||
1862 | \sa operator+=(), prepend(), insert() | - | ||||||||||||
1863 | */ | - | ||||||||||||
1864 | - | |||||||||||||
1865 | QByteArray &QByteArray::append(const QByteArray &ba) | - | ||||||||||||
1866 | { | - | ||||||||||||
1867 | if (d->size == 0 && d->ref.isStatic() && !IS_RAW_DATA(ba.d)) { | - | ||||||||||||
1868 | *this = ba; | - | ||||||||||||
1869 | } else if (ba.d->size != 0) { | - | ||||||||||||
1870 | if (d->ref.isShared() || uint(d->size + ba.d->size) + 1u > d->alloc) | - | ||||||||||||
1871 | reallocData(uint(d->size + ba.d->size) + 1u, d->detachFlags() | Data::Grow); | - | ||||||||||||
1872 | memcpy(d->data() + d->size, ba.d->data(), ba.d->size); | - | ||||||||||||
1873 | d->size += ba.d->size; | - | ||||||||||||
1874 | d->data()[d->size] = '\0'; | - | ||||||||||||
1875 | } | - | ||||||||||||
1876 | return *this; | - | ||||||||||||
1877 | } | - | ||||||||||||
1878 | - | |||||||||||||
1879 | /*! \fn QByteArray &QByteArray::append(const QString &str) | - | ||||||||||||
1880 | - | |||||||||||||
1881 | \overload | - | ||||||||||||
1882 | - | |||||||||||||
1883 | Appends the string \a str to this byte array. The Unicode data is | - | ||||||||||||
1884 | converted into 8-bit characters using QString::toUtf8(). | - | ||||||||||||
1885 | - | |||||||||||||
1886 | You can disable this function by defining \c QT_NO_CAST_TO_ASCII when you | - | ||||||||||||
1887 | compile your applications. You then need to call QString::toUtf8() (or | - | ||||||||||||
1888 | QString::toLatin1() or QString::toLocal8Bit()) explicitly if you want to | - | ||||||||||||
1889 | convert the data to \c{const char *}. | - | ||||||||||||
1890 | */ | - | ||||||||||||
1891 | - | |||||||||||||
1892 | /*! | - | ||||||||||||
1893 | \overload | - | ||||||||||||
1894 | - | |||||||||||||
1895 | Appends the string \a str to this byte array. | - | ||||||||||||
1896 | */ | - | ||||||||||||
1897 | - | |||||||||||||
1898 | QByteArray& QByteArray::append(const char *str) | - | ||||||||||||
1899 | { | - | ||||||||||||
1900 | if (str) { | - | ||||||||||||
1901 | const int len = int(strlen(str)); | - | ||||||||||||
1902 | if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc) | - | ||||||||||||
1903 | reallocData(uint(d->size + len) + 1u, d->detachFlags() | Data::Grow); | - | ||||||||||||
1904 | memcpy(d->data() + d->size, str, len + 1); // include null terminator | - | ||||||||||||
1905 | d->size += len; | - | ||||||||||||
1906 | } | - | ||||||||||||
1907 | return *this; | - | ||||||||||||
1908 | } | - | ||||||||||||
1909 | - | |||||||||||||
1910 | /*! | - | ||||||||||||
1911 | \overload append() | - | ||||||||||||
1912 | - | |||||||||||||
1913 | Appends the first \a len characters of the string \a str to this byte | - | ||||||||||||
1914 | array and returns a reference to this byte array. | - | ||||||||||||
1915 | - | |||||||||||||
1916 | If \a len is negative, the length of the string will be determined | - | ||||||||||||
1917 | automatically using qstrlen(). If \a len is zero or \a str is | - | ||||||||||||
1918 | null, nothing is appended to the byte array. Ensure that \a len is | - | ||||||||||||
1919 | \e not longer than \a str. | - | ||||||||||||
1920 | */ | - | ||||||||||||
1921 | - | |||||||||||||
1922 | QByteArray &QByteArray::append(const char *str, int len) | - | ||||||||||||
1923 | { | - | ||||||||||||
1924 | if (len < 0) | - | ||||||||||||
1925 | len = qstrlen(str); | - | ||||||||||||
1926 | if (str && len) { | - | ||||||||||||
1927 | if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc) | - | ||||||||||||
1928 | reallocData(uint(d->size + len) + 1u, d->detachFlags() | Data::Grow); | - | ||||||||||||
1929 | memcpy(d->data() + d->size, str, len); // include null terminator | - | ||||||||||||
1930 | d->size += len; | - | ||||||||||||
1931 | d->data()[d->size] = '\0'; | - | ||||||||||||
1932 | } | - | ||||||||||||
1933 | return *this; | - | ||||||||||||
1934 | } | - | ||||||||||||
1935 | - | |||||||||||||
1936 | /*! \fn QByteArray &QByteArray::append(int count, char ch) | - | ||||||||||||
1937 | - | |||||||||||||
1938 | \overload | - | ||||||||||||
1939 | \since 5.7 | - | ||||||||||||
1940 | - | |||||||||||||
1941 | Appends \a count copies of character \a ch to this byte | - | ||||||||||||
1942 | array and returns a reference to this byte array. | - | ||||||||||||
1943 | - | |||||||||||||
1944 | If \a count is negative or zero nothing is appended to the byte array. | - | ||||||||||||
1945 | */ | - | ||||||||||||
1946 | - | |||||||||||||
1947 | /*! | - | ||||||||||||
1948 | \overload | - | ||||||||||||
1949 | - | |||||||||||||
1950 | Appends the character \a ch to this byte array. | - | ||||||||||||
1951 | */ | - | ||||||||||||
1952 | - | |||||||||||||
1953 | QByteArray& QByteArray::append(char ch) | - | ||||||||||||
1954 | { | - | ||||||||||||
1955 | if (d->ref.isShared() || uint(d->size) + 2u > d->alloc) | - | ||||||||||||
1956 | reallocData(uint(d->size) + 2u, d->detachFlags() | Data::Grow); | - | ||||||||||||
1957 | d->data()[d->size++] = ch; | - | ||||||||||||
1958 | d->data()[d->size] = '\0'; | - | ||||||||||||
1959 | return *this; | - | ||||||||||||
1960 | } | - | ||||||||||||
1961 | - | |||||||||||||
1962 | /*! | - | ||||||||||||
1963 | \internal | - | ||||||||||||
1964 | Inserts \a len bytes from the array \a arr at position \a pos and returns a | - | ||||||||||||
1965 | reference the modified byte array. | - | ||||||||||||
1966 | */ | - | ||||||||||||
1967 | static inline QByteArray &qbytearray_insert(QByteArray *ba, | - | ||||||||||||
1968 | int pos, const char *arr, int len) | - | ||||||||||||
1969 | { | - | ||||||||||||
1970 | Q_ASSERT(pos >= 0); | - | ||||||||||||
1971 | - | |||||||||||||
1972 | if (pos < 0 || len <= 0 || arr == 0) | - | ||||||||||||
1973 | return *ba; | - | ||||||||||||
1974 | - | |||||||||||||
1975 | int oldsize = ba->size(); | - | ||||||||||||
1976 | ba->resize(qMax(pos, oldsize) + len); | - | ||||||||||||
1977 | char *dst = ba->data(); | - | ||||||||||||
1978 | if (pos > oldsize) | - | ||||||||||||
1979 | ::memset(dst + oldsize, 0x20, pos - oldsize); | - | ||||||||||||
1980 | else | - | ||||||||||||
1981 | ::memmove(dst + pos + len, dst + pos, oldsize - pos); | - | ||||||||||||
1982 | memcpy(dst + pos, arr, len); | - | ||||||||||||
1983 | return *ba; | - | ||||||||||||
1984 | } | - | ||||||||||||
1985 | - | |||||||||||||
1986 | /*! | - | ||||||||||||
1987 | Inserts the byte array \a ba at index position \a i and returns a | - | ||||||||||||
1988 | reference to this byte array. | - | ||||||||||||
1989 | - | |||||||||||||
1990 | Example: | - | ||||||||||||
1991 | \snippet code/src_corelib_tools_qbytearray.cpp 17 | - | ||||||||||||
1992 | - | |||||||||||||
1993 | \sa append(), prepend(), replace(), remove() | - | ||||||||||||
1994 | */ | - | ||||||||||||
1995 | - | |||||||||||||
1996 | QByteArray &QByteArray::insert(int i, const QByteArray &ba) | - | ||||||||||||
1997 | { | - | ||||||||||||
1998 | QByteArray copy(ba); | - | ||||||||||||
1999 | return qbytearray_insert(this, i, copy.d->data(), copy.d->size); | - | ||||||||||||
2000 | } | - | ||||||||||||
2001 | - | |||||||||||||
2002 | /*! | - | ||||||||||||
2003 | \fn QByteArray &QByteArray::insert(int i, const QString &str) | - | ||||||||||||
2004 | - | |||||||||||||
2005 | \overload | - | ||||||||||||
2006 | - | |||||||||||||
2007 | Inserts the string \a str at index position \a i in the byte | - | ||||||||||||
2008 | array. The Unicode data is converted into 8-bit characters using | - | ||||||||||||
2009 | QString::toUtf8(). | - | ||||||||||||
2010 | - | |||||||||||||
2011 | If \a i is greater than size(), the array is first extended using | - | ||||||||||||
2012 | resize(). | - | ||||||||||||
2013 | - | |||||||||||||
2014 | You can disable this function by defining \c QT_NO_CAST_TO_ASCII when you | - | ||||||||||||
2015 | compile your applications. You then need to call QString::toUtf8() (or | - | ||||||||||||
2016 | QString::toLatin1() or QString::toLocal8Bit()) explicitly if you want to | - | ||||||||||||
2017 | convert the data to \c{const char *}. | - | ||||||||||||
2018 | */ | - | ||||||||||||
2019 | - | |||||||||||||
2020 | /*! | - | ||||||||||||
2021 | \overload | - | ||||||||||||
2022 | - | |||||||||||||
2023 | Inserts the string \a str at position \a i in the byte array. | - | ||||||||||||
2024 | - | |||||||||||||
2025 | If \a i is greater than size(), the array is first extended using | - | ||||||||||||
2026 | resize(). | - | ||||||||||||
2027 | */ | - | ||||||||||||
2028 | - | |||||||||||||
2029 | QByteArray &QByteArray::insert(int i, const char *str) | - | ||||||||||||
2030 | { | - | ||||||||||||
2031 | return qbytearray_insert(this, i, str, qstrlen(str)); | - | ||||||||||||
2032 | } | - | ||||||||||||
2033 | - | |||||||||||||
2034 | /*! | - | ||||||||||||
2035 | \overload | - | ||||||||||||
2036 | \since 4.6 | - | ||||||||||||
2037 | - | |||||||||||||
2038 | Inserts \a len bytes of the string \a str at position | - | ||||||||||||
2039 | \a i in the byte array. | - | ||||||||||||
2040 | - | |||||||||||||
2041 | If \a i is greater than size(), the array is first extended using | - | ||||||||||||
2042 | resize(). | - | ||||||||||||
2043 | */ | - | ||||||||||||
2044 | - | |||||||||||||
2045 | QByteArray &QByteArray::insert(int i, const char *str, int len) | - | ||||||||||||
2046 | { | - | ||||||||||||
2047 | return qbytearray_insert(this, i, str, len); | - | ||||||||||||
2048 | } | - | ||||||||||||
2049 | - | |||||||||||||
2050 | /*! | - | ||||||||||||
2051 | \overload | - | ||||||||||||
2052 | - | |||||||||||||
2053 | Inserts character \a ch at index position \a i in the byte array. | - | ||||||||||||
2054 | If \a i is greater than size(), the array is first extended using | - | ||||||||||||
2055 | resize(). | - | ||||||||||||
2056 | */ | - | ||||||||||||
2057 | - | |||||||||||||
2058 | QByteArray &QByteArray::insert(int i, char ch) | - | ||||||||||||
2059 | { | - | ||||||||||||
2060 | return qbytearray_insert(this, i, &ch, 1); | - | ||||||||||||
2061 | } | - | ||||||||||||
2062 | - | |||||||||||||
2063 | /*! \fn QByteArray &QByteArray::insert(int i, int count, char ch) | - | ||||||||||||
2064 | - | |||||||||||||
2065 | \overload | - | ||||||||||||
2066 | \since 5.7 | - | ||||||||||||
2067 | - | |||||||||||||
2068 | Inserts \a count copies of character \a ch at index position \a i in the | - | ||||||||||||
2069 | byte array. | - | ||||||||||||
2070 | - | |||||||||||||
2071 | If \a i is greater than size(), the array is first extended using resize(). | - | ||||||||||||
2072 | */ | - | ||||||||||||
2073 | - | |||||||||||||
2074 | QByteArray &QByteArray::insert(int i, int count, char ch) | - | ||||||||||||
2075 | { | - | ||||||||||||
2076 | if (i < 0 || count <= 0)
| 1-46 | ||||||||||||
2077 | return *this; executed 19 times by 1 test: return *this; Executed by:
| 19 | ||||||||||||
2078 | - | |||||||||||||
2079 | int oldsize = size(); | - | ||||||||||||
2080 | resize(qMax(i, oldsize) + count); | - | ||||||||||||
2081 | char *dst = d->data(); | - | ||||||||||||
2082 | if (i > oldsize)
| 1-27 | ||||||||||||
2083 | ::memset(dst + oldsize, 0x20, i - oldsize); executed 1 time by 1 test: ::memset(dst + oldsize, 0x20, i - oldsize); Executed by:
| 1 | ||||||||||||
2084 | else if (i < oldsize)
| 9-18 | ||||||||||||
2085 | ::memmove(dst + i + count, dst + i, oldsize - i); executed 18 times by 1 test: ::memmove(dst + i + count, dst + i, oldsize - i); Executed by:
| 18 | ||||||||||||
2086 | ::memset(dst + i, ch, count); | - | ||||||||||||
2087 | return *this; executed 28 times by 1 test: return *this; Executed by:
| 28 | ||||||||||||
2088 | } | - | ||||||||||||
2089 | - | |||||||||||||
2090 | /*! | - | ||||||||||||
2091 | Removes \a len bytes from the array, starting at index position \a | - | ||||||||||||
2092 | pos, and returns a reference to the array. | - | ||||||||||||
2093 | - | |||||||||||||
2094 | If \a pos is out of range, nothing happens. If \a pos is valid, | - | ||||||||||||
2095 | but \a pos + \a len is larger than the size of the array, the | - | ||||||||||||
2096 | array is truncated at position \a pos. | - | ||||||||||||
2097 | - | |||||||||||||
2098 | Example: | - | ||||||||||||
2099 | \snippet code/src_corelib_tools_qbytearray.cpp 18 | - | ||||||||||||
2100 | - | |||||||||||||
2101 | \sa insert(), replace() | - | ||||||||||||
2102 | */ | - | ||||||||||||
2103 | - | |||||||||||||
2104 | QByteArray &QByteArray::remove(int pos, int len) | - | ||||||||||||
2105 | { | - | ||||||||||||
2106 | if (len <= 0 || uint(pos) >= uint(d->size)) | - | ||||||||||||
2107 | return *this; | - | ||||||||||||
2108 | detach(); | - | ||||||||||||
2109 | if (len >= d->size - pos) { | - | ||||||||||||
2110 | resize(pos); | - | ||||||||||||
2111 | } else { | - | ||||||||||||
2112 | memmove(d->data() + pos, d->data() + pos + len, d->size - pos - len); | - | ||||||||||||
2113 | resize(d->size - len); | - | ||||||||||||
2114 | } | - | ||||||||||||
2115 | return *this; | - | ||||||||||||
2116 | } | - | ||||||||||||
2117 | - | |||||||||||||
2118 | /*! | - | ||||||||||||
2119 | Replaces \a len bytes from index position \a pos with the byte | - | ||||||||||||
2120 | array \a after, and returns a reference to this byte array. | - | ||||||||||||
2121 | - | |||||||||||||
2122 | Example: | - | ||||||||||||
2123 | \snippet code/src_corelib_tools_qbytearray.cpp 19 | - | ||||||||||||
2124 | - | |||||||||||||
2125 | \sa insert(), remove() | - | ||||||||||||
2126 | */ | - | ||||||||||||
2127 | - | |||||||||||||
2128 | QByteArray &QByteArray::replace(int pos, int len, const QByteArray &after) | - | ||||||||||||
2129 | { | - | ||||||||||||
2130 | if (len == after.d->size && (pos + len <= d->size)) { | - | ||||||||||||
2131 | detach(); | - | ||||||||||||
2132 | memmove(d->data() + pos, after.d->data(), len*sizeof(char)); | - | ||||||||||||
2133 | return *this; | - | ||||||||||||
2134 | } else { | - | ||||||||||||
2135 | QByteArray copy(after); | - | ||||||||||||
2136 | // ### optimize me | - | ||||||||||||
2137 | remove(pos, len); | - | ||||||||||||
2138 | return insert(pos, copy); | - | ||||||||||||
2139 | } | - | ||||||||||||
2140 | } | - | ||||||||||||
2141 | - | |||||||||||||
2142 | /*! \fn QByteArray &QByteArray::replace(int pos, int len, const char *after) | - | ||||||||||||
2143 | - | |||||||||||||
2144 | \overload | - | ||||||||||||
2145 | - | |||||||||||||
2146 | Replaces \a len bytes from index position \a pos with the zero terminated | - | ||||||||||||
2147 | string \a after. | - | ||||||||||||
2148 | - | |||||||||||||
2149 | Notice: this can change the length of the byte array. | - | ||||||||||||
2150 | */ | - | ||||||||||||
2151 | QByteArray &QByteArray::replace(int pos, int len, const char *after) | - | ||||||||||||
2152 | { | - | ||||||||||||
2153 | return replace(pos,len,after,qstrlen(after)); | - | ||||||||||||
2154 | } | - | ||||||||||||
2155 | - | |||||||||||||
2156 | /*! \fn QByteArray &QByteArray::replace(int pos, int len, const char *after, int alen) | - | ||||||||||||
2157 | - | |||||||||||||
2158 | \overload | - | ||||||||||||
2159 | - | |||||||||||||
2160 | Replaces \a len bytes from index position \a pos with \a alen bytes | - | ||||||||||||
2161 | from the string \a after. \a after is allowed to have '\\0' characters. | - | ||||||||||||
2162 | - | |||||||||||||
2163 | \since 4.7 | - | ||||||||||||
2164 | */ | - | ||||||||||||
2165 | QByteArray &QByteArray::replace(int pos, int len, const char *after, int alen) | - | ||||||||||||
2166 | { | - | ||||||||||||
2167 | if (len == alen && (pos + len <= d->size)) { | - | ||||||||||||
2168 | detach(); | - | ||||||||||||
2169 | memcpy(d->data() + pos, after, len*sizeof(char)); | - | ||||||||||||
2170 | return *this; | - | ||||||||||||
2171 | } else { | - | ||||||||||||
2172 | remove(pos, len); | - | ||||||||||||
2173 | return qbytearray_insert(this, pos, after, alen); | - | ||||||||||||
2174 | } | - | ||||||||||||
2175 | } | - | ||||||||||||
2176 | - | |||||||||||||
2177 | // ### optimize all other replace method, by offering | - | ||||||||||||
2178 | // QByteArray::replace(const char *before, int blen, const char *after, int alen) | - | ||||||||||||
2179 | - | |||||||||||||
2180 | /*! | - | ||||||||||||
2181 | \overload | - | ||||||||||||
2182 | - | |||||||||||||
2183 | Replaces every occurrence of the byte array \a before with the | - | ||||||||||||
2184 | byte array \a after. | - | ||||||||||||
2185 | - | |||||||||||||
2186 | Example: | - | ||||||||||||
2187 | \snippet code/src_corelib_tools_qbytearray.cpp 20 | - | ||||||||||||
2188 | */ | - | ||||||||||||
2189 | - | |||||||||||||
2190 | QByteArray &QByteArray::replace(const QByteArray &before, const QByteArray &after) | - | ||||||||||||
2191 | { | - | ||||||||||||
2192 | if (isNull() || before.d == after.d) | - | ||||||||||||
2193 | return *this; | - | ||||||||||||
2194 | - | |||||||||||||
2195 | QByteArray aft = after; | - | ||||||||||||
2196 | if (after.d == d) | - | ||||||||||||
2197 | aft.detach(); | - | ||||||||||||
2198 | - | |||||||||||||
2199 | return replace(before.constData(), before.size(), aft.constData(), aft.size()); | - | ||||||||||||
2200 | } | - | ||||||||||||
2201 | - | |||||||||||||
2202 | /*! | - | ||||||||||||
2203 | \fn QByteArray &QByteArray::replace(const char *before, const QByteArray &after) | - | ||||||||||||
2204 | \overload | - | ||||||||||||
2205 | - | |||||||||||||
2206 | Replaces every occurrence of the string \a before with the | - | ||||||||||||
2207 | byte array \a after. | - | ||||||||||||
2208 | */ | - | ||||||||||||
2209 | - | |||||||||||||
2210 | QByteArray &QByteArray::replace(const char *c, const QByteArray &after) | - | ||||||||||||
2211 | { | - | ||||||||||||
2212 | QByteArray aft = after; | - | ||||||||||||
2213 | if (after.d == d) | - | ||||||||||||
2214 | aft.detach(); | - | ||||||||||||
2215 | - | |||||||||||||
2216 | return replace(c, qstrlen(c), aft.constData(), aft.size()); | - | ||||||||||||
2217 | } | - | ||||||||||||
2218 | - | |||||||||||||
2219 | /*! | - | ||||||||||||
2220 | \fn QByteArray &QByteArray::replace(const char *before, int bsize, const char *after, int asize) | - | ||||||||||||
2221 | \overload | - | ||||||||||||
2222 | - | |||||||||||||
2223 | Replaces every occurrence of the string \a before with the string \a after. | - | ||||||||||||
2224 | Since the sizes of the strings are given by \a bsize and \a asize, they | - | ||||||||||||
2225 | may contain zero characters and do not need to be zero-terminated. | - | ||||||||||||
2226 | */ | - | ||||||||||||
2227 | - | |||||||||||||
2228 | QByteArray &QByteArray::replace(const char *before, int bsize, const char *after, int asize) | - | ||||||||||||
2229 | { | - | ||||||||||||
2230 | if (isNull() || (before == after && bsize == asize)) | - | ||||||||||||
2231 | return *this; | - | ||||||||||||
2232 | - | |||||||||||||
2233 | // protect against before or after being part of this | - | ||||||||||||
2234 | const char *a = after; | - | ||||||||||||
2235 | const char *b = before; | - | ||||||||||||
2236 | if (after >= d->data() && after < d->data() + d->size) { | - | ||||||||||||
2237 | char *copy = (char *)malloc(asize); | - | ||||||||||||
2238 | Q_CHECK_PTR(copy); | - | ||||||||||||
2239 | memcpy(copy, after, asize); | - | ||||||||||||
2240 | a = copy; | - | ||||||||||||
2241 | } | - | ||||||||||||
2242 | if (before >= d->data() && before < d->data() + d->size) { | - | ||||||||||||
2243 | char *copy = (char *)malloc(bsize); | - | ||||||||||||
2244 | Q_CHECK_PTR(copy); | - | ||||||||||||
2245 | memcpy(copy, before, bsize); | - | ||||||||||||
2246 | b = copy; | - | ||||||||||||
2247 | } | - | ||||||||||||
2248 | - | |||||||||||||
2249 | QByteArrayMatcher matcher(before, bsize); | - | ||||||||||||
2250 | int index = 0; | - | ||||||||||||
2251 | int len = d->size; | - | ||||||||||||
2252 | char *d = data(); | - | ||||||||||||
2253 | - | |||||||||||||
2254 | if (bsize == asize) { | - | ||||||||||||
2255 | if (bsize) { | - | ||||||||||||
2256 | while ((index = matcher.indexIn(*this, index)) != -1) { | - | ||||||||||||
2257 | memcpy(d + index, after, asize); | - | ||||||||||||
2258 | index += bsize; | - | ||||||||||||
2259 | } | - | ||||||||||||
2260 | } | - | ||||||||||||
2261 | } else if (asize < bsize) { | - | ||||||||||||
2262 | uint to = 0; | - | ||||||||||||
2263 | uint movestart = 0; | - | ||||||||||||
2264 | uint num = 0; | - | ||||||||||||
2265 | while ((index = matcher.indexIn(*this, index)) != -1) { | - | ||||||||||||
2266 | if (num) { | - | ||||||||||||
2267 | int msize = index - movestart; | - | ||||||||||||
2268 | if (msize > 0) { | - | ||||||||||||
2269 | memmove(d + to, d + movestart, msize); | - | ||||||||||||
2270 | to += msize; | - | ||||||||||||
2271 | } | - | ||||||||||||
2272 | } else { | - | ||||||||||||
2273 | to = index; | - | ||||||||||||
2274 | } | - | ||||||||||||
2275 | if (asize) { | - | ||||||||||||
2276 | memcpy(d + to, after, asize); | - | ||||||||||||
2277 | to += asize; | - | ||||||||||||
2278 | } | - | ||||||||||||
2279 | index += bsize; | - | ||||||||||||
2280 | movestart = index; | - | ||||||||||||
2281 | num++; | - | ||||||||||||
2282 | } | - | ||||||||||||
2283 | if (num) { | - | ||||||||||||
2284 | int msize = len - movestart; | - | ||||||||||||
2285 | if (msize > 0) | - | ||||||||||||
2286 | memmove(d + to, d + movestart, msize); | - | ||||||||||||
2287 | resize(len - num*(bsize-asize)); | - | ||||||||||||
2288 | } | - | ||||||||||||
2289 | } else { | - | ||||||||||||
2290 | // the most complex case. We don't want to lose performance by doing repeated | - | ||||||||||||
2291 | // copies and reallocs of the string. | - | ||||||||||||
2292 | while (index != -1) { | - | ||||||||||||
2293 | uint indices[4096]; | - | ||||||||||||
2294 | uint pos = 0; | - | ||||||||||||
2295 | while(pos < 4095) { | - | ||||||||||||
2296 | index = matcher.indexIn(*this, index); | - | ||||||||||||
2297 | if (index == -1) | - | ||||||||||||
2298 | break; | - | ||||||||||||
2299 | indices[pos++] = index; | - | ||||||||||||
2300 | index += bsize; | - | ||||||||||||
2301 | // avoid infinite loop | - | ||||||||||||
2302 | if (!bsize) | - | ||||||||||||
2303 | index++; | - | ||||||||||||
2304 | } | - | ||||||||||||
2305 | if (!pos) | - | ||||||||||||
2306 | break; | - | ||||||||||||
2307 | - | |||||||||||||
2308 | // we have a table of replacement positions, use them for fast replacing | - | ||||||||||||
2309 | int adjust = pos*(asize-bsize); | - | ||||||||||||
2310 | // index has to be adjusted in case we get back into the loop above. | - | ||||||||||||
2311 | if (index != -1) | - | ||||||||||||
2312 | index += adjust; | - | ||||||||||||
2313 | int newlen = len + adjust; | - | ||||||||||||
2314 | int moveend = len; | - | ||||||||||||
2315 | if (newlen > len) { | - | ||||||||||||
2316 | resize(newlen); | - | ||||||||||||
2317 | len = newlen; | - | ||||||||||||
2318 | } | - | ||||||||||||
2319 | d = this->d->data(); | - | ||||||||||||
2320 | - | |||||||||||||
2321 | while(pos) { | - | ||||||||||||
2322 | pos--; | - | ||||||||||||
2323 | int movestart = indices[pos] + bsize; | - | ||||||||||||
2324 | int insertstart = indices[pos] + pos*(asize-bsize); | - | ||||||||||||
2325 | int moveto = insertstart + asize; | - | ||||||||||||
2326 | memmove(d + moveto, d + movestart, (moveend - movestart)); | - | ||||||||||||
2327 | if (asize) | - | ||||||||||||
2328 | memcpy(d + insertstart, after, asize); | - | ||||||||||||
2329 | moveend = movestart - bsize; | - | ||||||||||||
2330 | } | - | ||||||||||||
2331 | } | - | ||||||||||||
2332 | } | - | ||||||||||||
2333 | - | |||||||||||||
2334 | if (a != after) | - | ||||||||||||
2335 | ::free(const_cast<char *>(a)); | - | ||||||||||||
2336 | if (b != before) | - | ||||||||||||
2337 | ::free(const_cast<char *>(b)); | - | ||||||||||||
2338 | - | |||||||||||||
2339 | - | |||||||||||||
2340 | return *this; | - | ||||||||||||
2341 | } | - | ||||||||||||
2342 | - | |||||||||||||
2343 | - | |||||||||||||
2344 | /*! | - | ||||||||||||
2345 | \fn QByteArray &QByteArray::replace(const QByteArray &before, const char *after) | - | ||||||||||||
2346 | \overload | - | ||||||||||||
2347 | - | |||||||||||||
2348 | Replaces every occurrence of the byte array \a before with the | - | ||||||||||||
2349 | string \a after. | - | ||||||||||||
2350 | */ | - | ||||||||||||
2351 | - | |||||||||||||
2352 | /*! \fn QByteArray &QByteArray::replace(const QString &before, const QByteArray &after) | - | ||||||||||||
2353 | - | |||||||||||||
2354 | \overload | - | ||||||||||||
2355 | - | |||||||||||||
2356 | Replaces every occurrence of the string \a before with the byte | - | ||||||||||||
2357 | array \a after. The Unicode data is converted into 8-bit | - | ||||||||||||
2358 | characters using QString::toUtf8(). | - | ||||||||||||
2359 | - | |||||||||||||
2360 | You can disable this function by defining \c QT_NO_CAST_TO_ASCII when you | - | ||||||||||||
2361 | compile your applications. You then need to call QString::toUtf8() (or | - | ||||||||||||
2362 | QString::toLatin1() or QString::toLocal8Bit()) explicitly if you want to | - | ||||||||||||
2363 | convert the data to \c{const char *}. | - | ||||||||||||
2364 | */ | - | ||||||||||||
2365 | - | |||||||||||||
2366 | /*! \fn QByteArray &QByteArray::replace(const QString &before, const char *after) | - | ||||||||||||
2367 | \overload | - | ||||||||||||
2368 | - | |||||||||||||
2369 | Replaces every occurrence of the string \a before with the string | - | ||||||||||||
2370 | \a after. | - | ||||||||||||
2371 | */ | - | ||||||||||||
2372 | - | |||||||||||||
2373 | /*! \fn QByteArray &QByteArray::replace(const char *before, const char *after) | - | ||||||||||||
2374 | - | |||||||||||||
2375 | \overload | - | ||||||||||||
2376 | - | |||||||||||||
2377 | Replaces every occurrence of the string \a before with the string | - | ||||||||||||
2378 | \a after. | - | ||||||||||||
2379 | */ | - | ||||||||||||
2380 | - | |||||||||||||
2381 | /*! | - | ||||||||||||
2382 | \overload | - | ||||||||||||
2383 | - | |||||||||||||
2384 | Replaces every occurrence of the character \a before with the | - | ||||||||||||
2385 | byte array \a after. | - | ||||||||||||
2386 | */ | - | ||||||||||||
2387 | - | |||||||||||||
2388 | QByteArray &QByteArray::replace(char before, const QByteArray &after) | - | ||||||||||||
2389 | { | - | ||||||||||||
2390 | char b[2] = { before, '\0' }; | - | ||||||||||||
2391 | QByteArray cb = fromRawData(b, 1); | - | ||||||||||||
2392 | return replace(cb, after); | - | ||||||||||||
2393 | } | - | ||||||||||||
2394 | - | |||||||||||||
2395 | /*! \fn QByteArray &QByteArray::replace(char before, const QString &after) | - | ||||||||||||
2396 | - | |||||||||||||
2397 | \overload | - | ||||||||||||
2398 | - | |||||||||||||
2399 | Replaces every occurrence of the character \a before with the | - | ||||||||||||
2400 | string \a after. The Unicode data is converted into 8-bit | - | ||||||||||||
2401 | characters using QString::toUtf8(). | - | ||||||||||||
2402 | - | |||||||||||||
2403 | You can disable this function by defining \c QT_NO_CAST_TO_ASCII when you | - | ||||||||||||
2404 | compile your applications. You then need to call QString::toUtf8() (or | - | ||||||||||||
2405 | QString::toLatin1() or QString::toLocal8Bit()) explicitly if you want to | - | ||||||||||||
2406 | convert the data to \c{const char *}. | - | ||||||||||||
2407 | */ | - | ||||||||||||
2408 | - | |||||||||||||
2409 | /*! \fn QByteArray &QByteArray::replace(char before, const char *after) | - | ||||||||||||
2410 | - | |||||||||||||
2411 | \overload | - | ||||||||||||
2412 | - | |||||||||||||
2413 | Replaces every occurrence of the character \a before with the | - | ||||||||||||
2414 | string \a after. | - | ||||||||||||
2415 | */ | - | ||||||||||||
2416 | - | |||||||||||||
2417 | /*! | - | ||||||||||||
2418 | \overload | - | ||||||||||||
2419 | - | |||||||||||||
2420 | Replaces every occurrence of the character \a before with the | - | ||||||||||||
2421 | character \a after. | - | ||||||||||||
2422 | */ | - | ||||||||||||
2423 | - | |||||||||||||
2424 | QByteArray &QByteArray::replace(char before, char after) | - | ||||||||||||
2425 | { | - | ||||||||||||
2426 | if (d->size) { | - | ||||||||||||
2427 | char *i = data(); | - | ||||||||||||
2428 | char *e = i + d->size; | - | ||||||||||||
2429 | for (; i != e; ++i) | - | ||||||||||||
2430 | if (*i == before) | - | ||||||||||||
2431 | * i = after; | - | ||||||||||||
2432 | } | - | ||||||||||||
2433 | return *this; | - | ||||||||||||
2434 | } | - | ||||||||||||
2435 | - | |||||||||||||
2436 | /*! | - | ||||||||||||
2437 | Splits the byte array into subarrays wherever \a sep occurs, and | - | ||||||||||||
2438 | returns the list of those arrays. If \a sep does not match | - | ||||||||||||
2439 | anywhere in the byte array, split() returns a single-element list | - | ||||||||||||
2440 | containing this byte array. | - | ||||||||||||
2441 | */ | - | ||||||||||||
2442 | - | |||||||||||||
2443 | QList<QByteArray> QByteArray::split(char sep) const | - | ||||||||||||
2444 | { | - | ||||||||||||
2445 | QList<QByteArray> list; | - | ||||||||||||
2446 | int start = 0; | - | ||||||||||||
2447 | int end; | - | ||||||||||||
2448 | while ((end = indexOf(sep, start)) != -1) { | - | ||||||||||||
2449 | list.append(mid(start, end - start)); | - | ||||||||||||
2450 | start = end + 1; | - | ||||||||||||
2451 | } | - | ||||||||||||
2452 | list.append(mid(start)); | - | ||||||||||||
2453 | return list; | - | ||||||||||||
2454 | } | - | ||||||||||||
2455 | - | |||||||||||||
2456 | /*! | - | ||||||||||||
2457 | \since 4.5 | - | ||||||||||||
2458 | - | |||||||||||||
2459 | Returns a copy of this byte array repeated the specified number of \a times. | - | ||||||||||||
2460 | - | |||||||||||||
2461 | If \a times is less than 1, an empty byte array is returned. | - | ||||||||||||
2462 | - | |||||||||||||
2463 | Example: | - | ||||||||||||
2464 | - | |||||||||||||
2465 | \code | - | ||||||||||||
2466 | QByteArray ba("ab"); | - | ||||||||||||
2467 | ba.repeated(4); // returns "abababab" | - | ||||||||||||
2468 | \endcode | - | ||||||||||||
2469 | */ | - | ||||||||||||
2470 | QByteArray QByteArray::repeated(int times) const | - | ||||||||||||
2471 | { | - | ||||||||||||
2472 | if (d->size == 0) | - | ||||||||||||
2473 | return *this; | - | ||||||||||||
2474 | - | |||||||||||||
2475 | if (times <= 1) { | - | ||||||||||||
2476 | if (times == 1) | - | ||||||||||||
2477 | return *this; | - | ||||||||||||
2478 | return QByteArray(); | - | ||||||||||||
2479 | } | - | ||||||||||||
2480 | - | |||||||||||||
2481 | const int resultSize = times * d->size; | - | ||||||||||||
2482 | - | |||||||||||||
2483 | QByteArray result; | - | ||||||||||||
2484 | result.reserve(resultSize); | - | ||||||||||||
2485 | if (result.d->alloc != uint(resultSize) + 1u) | - | ||||||||||||
2486 | return QByteArray(); // not enough memory | - | ||||||||||||
2487 | - | |||||||||||||
2488 | memcpy(result.d->data(), d->data(), d->size); | - | ||||||||||||
2489 | - | |||||||||||||
2490 | int sizeSoFar = d->size; | - | ||||||||||||
2491 | char *end = result.d->data() + sizeSoFar; | - | ||||||||||||
2492 | - | |||||||||||||
2493 | const int halfResultSize = resultSize >> 1; | - | ||||||||||||
2494 | while (sizeSoFar <= halfResultSize) { | - | ||||||||||||
2495 | memcpy(end, result.d->data(), sizeSoFar); | - | ||||||||||||
2496 | end += sizeSoFar; | - | ||||||||||||
2497 | sizeSoFar <<= 1; | - | ||||||||||||
2498 | } | - | ||||||||||||
2499 | memcpy(end, result.d->data(), resultSize - sizeSoFar); | - | ||||||||||||
2500 | result.d->data()[resultSize] = '\0'; | - | ||||||||||||
2501 | result.d->size = resultSize; | - | ||||||||||||
2502 | return result; | - | ||||||||||||
2503 | } | - | ||||||||||||
2504 | - | |||||||||||||
2505 | #define REHASH(a) \ | - | ||||||||||||
2506 | if (ol_minus_1 < sizeof(uint) * CHAR_BIT) \ | - | ||||||||||||
2507 | hashHaystack -= (a) << ol_minus_1; \ | - | ||||||||||||
2508 | hashHaystack <<= 1 | - | ||||||||||||
2509 | - | |||||||||||||
2510 | /*! | - | ||||||||||||
2511 | Returns the index position of the first occurrence of the byte | - | ||||||||||||
2512 | array \a ba in this byte array, searching forward from index | - | ||||||||||||
2513 | position \a from. Returns -1 if \a ba could not be found. | - | ||||||||||||
2514 | - | |||||||||||||
2515 | Example: | - | ||||||||||||
2516 | \snippet code/src_corelib_tools_qbytearray.cpp 21 | - | ||||||||||||
2517 | - | |||||||||||||
2518 | \sa lastIndexOf(), contains(), count() | - | ||||||||||||
2519 | */ | - | ||||||||||||
2520 | - | |||||||||||||
2521 | int QByteArray::indexOf(const QByteArray &ba, int from) const | - | ||||||||||||
2522 | { | - | ||||||||||||
2523 | const int ol = ba.d->size; | - | ||||||||||||
2524 | if (ol == 0) | - | ||||||||||||
2525 | return from; | - | ||||||||||||
2526 | if (ol == 1) | - | ||||||||||||
2527 | return indexOf(*ba.d->data(), from); | - | ||||||||||||
2528 | - | |||||||||||||
2529 | const int l = d->size; | - | ||||||||||||
2530 | if (from > d->size || ol + from > l) | - | ||||||||||||
2531 | return -1; | - | ||||||||||||
2532 | - | |||||||||||||
2533 | return qFindByteArray(d->data(), d->size, from, ba.d->data(), ol); | - | ||||||||||||
2534 | } | - | ||||||||||||
2535 | - | |||||||||||||
2536 | /*! \fn int QByteArray::indexOf(const QString &str, int from) const | - | ||||||||||||
2537 | - | |||||||||||||
2538 | \overload | - | ||||||||||||
2539 | - | |||||||||||||
2540 | Returns the index position of the first occurrence of the string | - | ||||||||||||
2541 | \a str in the byte array, searching forward from index position | - | ||||||||||||
2542 | \a from. Returns -1 if \a str could not be found. | - | ||||||||||||
2543 | - | |||||||||||||
2544 | The Unicode data is converted into 8-bit characters using | - | ||||||||||||
2545 | QString::toUtf8(). | - | ||||||||||||
2546 | - | |||||||||||||
2547 | You can disable this function by defining \c QT_NO_CAST_TO_ASCII when you | - | ||||||||||||
2548 | compile your applications. You then need to call QString::toUtf8() (or | - | ||||||||||||
2549 | QString::toLatin1() or QString::toLocal8Bit()) explicitly if you want to | - | ||||||||||||
2550 | convert the data to \c{const char *}. | - | ||||||||||||
2551 | */ | - | ||||||||||||
2552 | - | |||||||||||||
2553 | /*! \fn int QByteArray::indexOf(const char *str, int from) const | - | ||||||||||||
2554 | - | |||||||||||||
2555 | \overload | - | ||||||||||||
2556 | - | |||||||||||||
2557 | Returns the index position of the first occurrence of the string | - | ||||||||||||
2558 | \a str in the byte array, searching forward from index position \a | - | ||||||||||||
2559 | from. Returns -1 if \a str could not be found. | - | ||||||||||||
2560 | */ | - | ||||||||||||
2561 | int QByteArray::indexOf(const char *c, int from) const | - | ||||||||||||
2562 | { | - | ||||||||||||
2563 | const int ol = qstrlen(c); | - | ||||||||||||
2564 | if (ol == 1) | - | ||||||||||||
2565 | return indexOf(*c, from); | - | ||||||||||||
2566 | - | |||||||||||||
2567 | const int l = d->size; | - | ||||||||||||
2568 | if (from > d->size || ol + from > l) | - | ||||||||||||
2569 | return -1; | - | ||||||||||||
2570 | if (ol == 0) | - | ||||||||||||
2571 | return from; | - | ||||||||||||
2572 | - | |||||||||||||
2573 | return qFindByteArray(d->data(), d->size, from, c, ol); | - | ||||||||||||
2574 | } | - | ||||||||||||
2575 | - | |||||||||||||
2576 | /*! | - | ||||||||||||
2577 | \overload | - | ||||||||||||
2578 | - | |||||||||||||
2579 | Returns the index position of the first occurrence of the | - | ||||||||||||
2580 | character \a ch in the byte array, searching forward from index | - | ||||||||||||
2581 | position \a from. Returns -1 if \a ch could not be found. | - | ||||||||||||
2582 | - | |||||||||||||
2583 | Example: | - | ||||||||||||
2584 | \snippet code/src_corelib_tools_qbytearray.cpp 22 | - | ||||||||||||
2585 | - | |||||||||||||
2586 | \sa lastIndexOf(), contains() | - | ||||||||||||
2587 | */ | - | ||||||||||||
2588 | - | |||||||||||||
2589 | int QByteArray::indexOf(char ch, int from) const | - | ||||||||||||
2590 | { | - | ||||||||||||
2591 | if (from < 0) | - | ||||||||||||
2592 | from = qMax(from + d->size, 0); | - | ||||||||||||
2593 | if (from < d->size) { | - | ||||||||||||
2594 | const char *n = d->data() + from - 1; | - | ||||||||||||
2595 | const char *e = d->data() + d->size; | - | ||||||||||||
2596 | while (++n != e) | - | ||||||||||||
2597 | if (*n == ch) | - | ||||||||||||
2598 | return n - d->data(); | - | ||||||||||||
2599 | } | - | ||||||||||||
2600 | return -1; | - | ||||||||||||
2601 | } | - | ||||||||||||
2602 | - | |||||||||||||
2603 | - | |||||||||||||
2604 | static int lastIndexOfHelper(const char *haystack, int l, const char *needle, int ol, int from) | - | ||||||||||||
2605 | { | - | ||||||||||||
2606 | int delta = l - ol; | - | ||||||||||||
2607 | if (from < 0) | - | ||||||||||||
2608 | from = delta; | - | ||||||||||||
2609 | if (from < 0 || from > l) | - | ||||||||||||
2610 | return -1; | - | ||||||||||||
2611 | if (from > delta) | - | ||||||||||||
2612 | from = delta; | - | ||||||||||||
2613 | - | |||||||||||||
2614 | const char *end = haystack; | - | ||||||||||||
2615 | haystack += from; | - | ||||||||||||
2616 | const uint ol_minus_1 = ol - 1; | - | ||||||||||||
2617 | const char *n = needle + ol_minus_1; | - | ||||||||||||
2618 | const char *h = haystack + ol_minus_1; | - | ||||||||||||
2619 | uint hashNeedle = 0, hashHaystack = 0; | - | ||||||||||||
2620 | int idx; | - | ||||||||||||
2621 | for (idx = 0; idx < ol; ++idx) { | - | ||||||||||||
2622 | hashNeedle = ((hashNeedle<<1) + *(n-idx)); | - | ||||||||||||
2623 | hashHaystack = ((hashHaystack<<1) + *(h-idx)); | - | ||||||||||||
2624 | } | - | ||||||||||||
2625 | hashHaystack -= *haystack; | - | ||||||||||||
2626 | while (haystack >= end) { | - | ||||||||||||
2627 | hashHaystack += *haystack; | - | ||||||||||||
2628 | if (hashHaystack == hashNeedle && memcmp(needle, haystack, ol) == 0) | - | ||||||||||||
2629 | return haystack - end; | - | ||||||||||||
2630 | --haystack; | - | ||||||||||||
2631 | REHASH(*(haystack + ol)); | - | ||||||||||||
2632 | } | - | ||||||||||||
2633 | return -1; | - | ||||||||||||
2634 | - | |||||||||||||
2635 | } | - | ||||||||||||
2636 | - | |||||||||||||
2637 | /*! | - | ||||||||||||
2638 | \fn int QByteArray::lastIndexOf(const QByteArray &ba, int from) const | - | ||||||||||||
2639 | - | |||||||||||||
2640 | Returns the index position of the last occurrence of the byte | - | ||||||||||||
2641 | array \a ba in this byte array, searching backward from index | - | ||||||||||||
2642 | position \a from. If \a from is -1 (the default), the search | - | ||||||||||||
2643 | starts at the last byte. Returns -1 if \a ba could not be found. | - | ||||||||||||
2644 | - | |||||||||||||
2645 | Example: | - | ||||||||||||
2646 | \snippet code/src_corelib_tools_qbytearray.cpp 23 | - | ||||||||||||
2647 | - | |||||||||||||
2648 | \sa indexOf(), contains(), count() | - | ||||||||||||
2649 | */ | - | ||||||||||||
2650 | - | |||||||||||||
2651 | int QByteArray::lastIndexOf(const QByteArray &ba, int from) const | - | ||||||||||||
2652 | { | - | ||||||||||||
2653 | const int ol = ba.d->size; | - | ||||||||||||
2654 | if (ol == 1) | - | ||||||||||||
2655 | return lastIndexOf(*ba.d->data(), from); | - | ||||||||||||
2656 | - | |||||||||||||
2657 | return lastIndexOfHelper(d->data(), d->size, ba.d->data(), ol, from); | - | ||||||||||||
2658 | } | - | ||||||||||||
2659 | - | |||||||||||||
2660 | /*! \fn int QByteArray::lastIndexOf(const QString &str, int from) const | - | ||||||||||||
2661 | - | |||||||||||||
2662 | \overload | - | ||||||||||||
2663 | - | |||||||||||||
2664 | Returns the index position of the last occurrence of the string \a | - | ||||||||||||
2665 | str in the byte array, searching backward from index position \a | - | ||||||||||||
2666 | from. If \a from is -1 (the default), the search starts at the | - | ||||||||||||
2667 | last (size() - 1) byte. Returns -1 if \a str could not be found. | - | ||||||||||||
2668 | - | |||||||||||||
2669 | The Unicode data is converted into 8-bit characters using | - | ||||||||||||
2670 | QString::toUtf8(). | - | ||||||||||||
2671 | - | |||||||||||||
2672 | You can disable this function by defining \c QT_NO_CAST_TO_ASCII when you | - | ||||||||||||
2673 | compile your applications. You then need to call QString::toUtf8() (or | - | ||||||||||||
2674 | QString::toLatin1() or QString::toLocal8Bit()) explicitly if you want to | - | ||||||||||||
2675 | convert the data to \c{const char *}. | - | ||||||||||||
2676 | */ | - | ||||||||||||
2677 | - | |||||||||||||
2678 | /*! \fn int QByteArray::lastIndexOf(const char *str, int from) const | - | ||||||||||||
2679 | \overload | - | ||||||||||||
2680 | - | |||||||||||||
2681 | Returns the index position of the last occurrence of the string \a | - | ||||||||||||
2682 | str in the byte array, searching backward from index position \a | - | ||||||||||||
2683 | from. If \a from is -1 (the default), the search starts at the | - | ||||||||||||
2684 | last (size() - 1) byte. Returns -1 if \a str could not be found. | - | ||||||||||||
2685 | */ | - | ||||||||||||
2686 | int QByteArray::lastIndexOf(const char *str, int from) const | - | ||||||||||||
2687 | { | - | ||||||||||||
2688 | const int ol = qstrlen(str); | - | ||||||||||||
2689 | if (ol == 1) | - | ||||||||||||
2690 | return lastIndexOf(*str, from); | - | ||||||||||||
2691 | - | |||||||||||||
2692 | return lastIndexOfHelper(d->data(), d->size, str, ol, from); | - | ||||||||||||
2693 | } | - | ||||||||||||
2694 | - | |||||||||||||
2695 | /*! | - | ||||||||||||
2696 | \overload | - | ||||||||||||
2697 | - | |||||||||||||
2698 | Returns the index position of the last occurrence of character \a | - | ||||||||||||
2699 | ch in the byte array, searching backward from index position \a | - | ||||||||||||
2700 | from. If \a from is -1 (the default), the search starts at the | - | ||||||||||||
2701 | last (size() - 1) byte. Returns -1 if \a ch could not be found. | - | ||||||||||||
2702 | - | |||||||||||||
2703 | Example: | - | ||||||||||||
2704 | \snippet code/src_corelib_tools_qbytearray.cpp 24 | - | ||||||||||||
2705 | - | |||||||||||||
2706 | \sa indexOf(), contains() | - | ||||||||||||
2707 | */ | - | ||||||||||||
2708 | - | |||||||||||||
2709 | int QByteArray::lastIndexOf(char ch, int from) const | - | ||||||||||||
2710 | { | - | ||||||||||||
2711 | if (from < 0) | - | ||||||||||||
2712 | from += d->size; | - | ||||||||||||
2713 | else if (from > d->size) | - | ||||||||||||
2714 | from = d->size-1; | - | ||||||||||||
2715 | if (from >= 0) { | - | ||||||||||||
2716 | const char *b = d->data(); | - | ||||||||||||
2717 | const char *n = d->data() + from + 1; | - | ||||||||||||
2718 | while (n-- != b) | - | ||||||||||||
2719 | if (*n == ch) | - | ||||||||||||
2720 | return n - b; | - | ||||||||||||
2721 | } | - | ||||||||||||
2722 | return -1; | - | ||||||||||||
2723 | } | - | ||||||||||||
2724 | - | |||||||||||||
2725 | /*! | - | ||||||||||||
2726 | Returns the number of (potentially overlapping) occurrences of | - | ||||||||||||
2727 | byte array \a ba in this byte array. | - | ||||||||||||
2728 | - | |||||||||||||
2729 | \sa contains(), indexOf() | - | ||||||||||||
2730 | */ | - | ||||||||||||
2731 | - | |||||||||||||
2732 | int QByteArray::count(const QByteArray &ba) const | - | ||||||||||||
2733 | { | - | ||||||||||||
2734 | int num = 0; | - | ||||||||||||
2735 | int i = -1; | - | ||||||||||||
2736 | if (d->size > 500 && ba.d->size > 5) { | - | ||||||||||||
2737 | QByteArrayMatcher matcher(ba); | - | ||||||||||||
2738 | while ((i = matcher.indexIn(*this, i + 1)) != -1) | - | ||||||||||||
2739 | ++num; | - | ||||||||||||
2740 | } else { | - | ||||||||||||
2741 | while ((i = indexOf(ba, i + 1)) != -1) | - | ||||||||||||
2742 | ++num; | - | ||||||||||||
2743 | } | - | ||||||||||||
2744 | return num; | - | ||||||||||||
2745 | } | - | ||||||||||||
2746 | - | |||||||||||||
2747 | /*! | - | ||||||||||||
2748 | \overload | - | ||||||||||||
2749 | - | |||||||||||||
2750 | Returns the number of (potentially overlapping) occurrences of | - | ||||||||||||
2751 | string \a str in the byte array. | - | ||||||||||||
2752 | */ | - | ||||||||||||
2753 | - | |||||||||||||
2754 | int QByteArray::count(const char *str) const | - | ||||||||||||
2755 | { | - | ||||||||||||
2756 | return count(fromRawData(str, qstrlen(str))); | - | ||||||||||||
2757 | } | - | ||||||||||||
2758 | - | |||||||||||||
2759 | /*! | - | ||||||||||||
2760 | \overload | - | ||||||||||||
2761 | - | |||||||||||||
2762 | Returns the number of occurrences of character \a ch in the byte | - | ||||||||||||
2763 | array. | - | ||||||||||||
2764 | - | |||||||||||||
2765 | \sa contains(), indexOf() | - | ||||||||||||
2766 | */ | - | ||||||||||||
2767 | - | |||||||||||||
2768 | int QByteArray::count(char ch) const | - | ||||||||||||
2769 | { | - | ||||||||||||
2770 | int num = 0; | - | ||||||||||||
2771 | const char *i = d->data() + d->size; | - | ||||||||||||
2772 | const char *b = d->data(); | - | ||||||||||||
2773 | while (i != b) | - | ||||||||||||
2774 | if (*--i == ch) | - | ||||||||||||
2775 | ++num; | - | ||||||||||||
2776 | return num; | - | ||||||||||||
2777 | } | - | ||||||||||||
2778 | - | |||||||||||||
2779 | /*! \fn int QByteArray::count() const | - | ||||||||||||
2780 | - | |||||||||||||
2781 | \overload | - | ||||||||||||
2782 | - | |||||||||||||
2783 | Same as size(). | - | ||||||||||||
2784 | */ | - | ||||||||||||
2785 | - | |||||||||||||
2786 | /*! | - | ||||||||||||
2787 | Returns \c true if this byte array starts with byte array \a ba; | - | ||||||||||||
2788 | otherwise returns \c false. | - | ||||||||||||
2789 | - | |||||||||||||
2790 | Example: | - | ||||||||||||
2791 | \snippet code/src_corelib_tools_qbytearray.cpp 25 | - | ||||||||||||
2792 | - | |||||||||||||
2793 | \sa endsWith(), left() | - | ||||||||||||
2794 | */ | - | ||||||||||||
2795 | bool QByteArray::startsWith(const QByteArray &ba) const | - | ||||||||||||
2796 | { | - | ||||||||||||
2797 | if (d == ba.d || ba.d->size == 0) | - | ||||||||||||
2798 | return true; | - | ||||||||||||
2799 | if (d->size < ba.d->size) | - | ||||||||||||
2800 | return false; | - | ||||||||||||
2801 | return memcmp(d->data(), ba.d->data(), ba.d->size) == 0; | - | ||||||||||||
2802 | } | - | ||||||||||||
2803 | - | |||||||||||||
2804 | /*! \overload | - | ||||||||||||
2805 | - | |||||||||||||
2806 | Returns \c true if this byte array starts with string \a str; | - | ||||||||||||
2807 | otherwise returns \c false. | - | ||||||||||||
2808 | */ | - | ||||||||||||
2809 | bool QByteArray::startsWith(const char *str) const | - | ||||||||||||
2810 | { | - | ||||||||||||
2811 | if (!str || !*str) | - | ||||||||||||
2812 | return true; | - | ||||||||||||
2813 | const int len = int(strlen(str)); | - | ||||||||||||
2814 | if (d->size < len) | - | ||||||||||||
2815 | return false; | - | ||||||||||||
2816 | return qstrncmp(d->data(), str, len) == 0; | - | ||||||||||||
2817 | } | - | ||||||||||||
2818 | - | |||||||||||||
2819 | /*! \overload | - | ||||||||||||
2820 | - | |||||||||||||
2821 | Returns \c true if this byte array starts with character \a ch; | - | ||||||||||||
2822 | otherwise returns \c false. | - | ||||||||||||
2823 | */ | - | ||||||||||||
2824 | bool QByteArray::startsWith(char ch) const | - | ||||||||||||
2825 | { | - | ||||||||||||
2826 | if (d->size == 0) | - | ||||||||||||
2827 | return false; | - | ||||||||||||
2828 | return d->data()[0] == ch; | - | ||||||||||||
2829 | } | - | ||||||||||||
2830 | - | |||||||||||||
2831 | /*! | - | ||||||||||||
2832 | Returns \c true if this byte array ends with byte array \a ba; | - | ||||||||||||
2833 | otherwise returns \c false. | - | ||||||||||||
2834 | - | |||||||||||||
2835 | Example: | - | ||||||||||||
2836 | \snippet code/src_corelib_tools_qbytearray.cpp 26 | - | ||||||||||||
2837 | - | |||||||||||||
2838 | \sa startsWith(), right() | - | ||||||||||||
2839 | */ | - | ||||||||||||
2840 | bool QByteArray::endsWith(const QByteArray &ba) const | - | ||||||||||||
2841 | { | - | ||||||||||||
2842 | if (d == ba.d || ba.d->size == 0) | - | ||||||||||||
2843 | return true; | - | ||||||||||||
2844 | if (d->size < ba.d->size) | - | ||||||||||||
2845 | return false; | - | ||||||||||||
2846 | return memcmp(d->data() + d->size - ba.d->size, ba.d->data(), ba.d->size) == 0; | - | ||||||||||||
2847 | } | - | ||||||||||||
2848 | - | |||||||||||||
2849 | /*! \overload | - | ||||||||||||
2850 | - | |||||||||||||
2851 | Returns \c true if this byte array ends with string \a str; otherwise | - | ||||||||||||
2852 | returns \c false. | - | ||||||||||||
2853 | */ | - | ||||||||||||
2854 | bool QByteArray::endsWith(const char *str) const | - | ||||||||||||
2855 | { | - | ||||||||||||
2856 | if (!str || !*str) | - | ||||||||||||
2857 | return true; | - | ||||||||||||
2858 | const int len = int(strlen(str)); | - | ||||||||||||
2859 | if (d->size < len) | - | ||||||||||||
2860 | return false; | - | ||||||||||||
2861 | return qstrncmp(d->data() + d->size - len, str, len) == 0; | - | ||||||||||||
2862 | } | - | ||||||||||||
2863 | - | |||||||||||||
2864 | /*! \overload | - | ||||||||||||
2865 | - | |||||||||||||
2866 | Returns \c true if this byte array ends with character \a ch; | - | ||||||||||||
2867 | otherwise returns \c false. | - | ||||||||||||
2868 | */ | - | ||||||||||||
2869 | bool QByteArray::endsWith(char ch) const | - | ||||||||||||
2870 | { | - | ||||||||||||
2871 | if (d->size == 0) | - | ||||||||||||
2872 | return false; | - | ||||||||||||
2873 | return d->data()[d->size - 1] == ch; | - | ||||||||||||
2874 | } | - | ||||||||||||
2875 | - | |||||||||||||
2876 | /*! | - | ||||||||||||
2877 | Returns a byte array that contains the leftmost \a len bytes of | - | ||||||||||||
2878 | this byte array. | - | ||||||||||||
2879 | - | |||||||||||||
2880 | The entire byte array is returned if \a len is greater than | - | ||||||||||||
2881 | size(). | - | ||||||||||||
2882 | - | |||||||||||||
2883 | Example: | - | ||||||||||||
2884 | \snippet code/src_corelib_tools_qbytearray.cpp 27 | - | ||||||||||||
2885 | - | |||||||||||||
2886 | \sa right(), mid(), startsWith(), truncate() | - | ||||||||||||
2887 | */ | - | ||||||||||||
2888 | - | |||||||||||||
2889 | QByteArray QByteArray::left(int len) const | - | ||||||||||||
2890 | { | - | ||||||||||||
2891 | if (len >= d->size) | - | ||||||||||||
2892 | return *this; | - | ||||||||||||
2893 | if (len < 0) | - | ||||||||||||
2894 | len = 0; | - | ||||||||||||
2895 | return QByteArray(d->data(), len); | - | ||||||||||||
2896 | } | - | ||||||||||||
2897 | - | |||||||||||||
2898 | /*! | - | ||||||||||||
2899 | Returns a byte array that contains the rightmost \a len bytes of | - | ||||||||||||
2900 | this byte array. | - | ||||||||||||
2901 | - | |||||||||||||
2902 | The entire byte array is returned if \a len is greater than | - | ||||||||||||
2903 | size(). | - | ||||||||||||
2904 | - | |||||||||||||
2905 | Example: | - | ||||||||||||
2906 | \snippet code/src_corelib_tools_qbytearray.cpp 28 | - | ||||||||||||
2907 | - | |||||||||||||
2908 | \sa endsWith(), left(), mid() | - | ||||||||||||
2909 | */ | - | ||||||||||||
2910 | - | |||||||||||||
2911 | QByteArray QByteArray::right(int len) const | - | ||||||||||||
2912 | { | - | ||||||||||||
2913 | if (len >= d->size) | - | ||||||||||||
2914 | return *this; | - | ||||||||||||
2915 | if (len < 0) | - | ||||||||||||
2916 | len = 0; | - | ||||||||||||
2917 | return QByteArray(d->data() + d->size - len, len); | - | ||||||||||||
2918 | } | - | ||||||||||||
2919 | - | |||||||||||||
2920 | /*! | - | ||||||||||||
2921 | Returns a byte array containing \a len bytes from this byte array, | - | ||||||||||||
2922 | starting at position \a pos. | - | ||||||||||||
2923 | - | |||||||||||||
2924 | If \a len is -1 (the default), or \a pos + \a len >= size(), | - | ||||||||||||
2925 | returns a byte array containing all bytes starting at position \a | - | ||||||||||||
2926 | pos until the end of the byte array. | - | ||||||||||||
2927 | - | |||||||||||||
2928 | Example: | - | ||||||||||||
2929 | \snippet code/src_corelib_tools_qbytearray.cpp 29 | - | ||||||||||||
2930 | - | |||||||||||||
2931 | \sa left(), right() | - | ||||||||||||
2932 | */ | - | ||||||||||||
2933 | - | |||||||||||||
2934 | QByteArray QByteArray::mid(int pos, int len) const | - | ||||||||||||
2935 | { | - | ||||||||||||
2936 | using namespace QtPrivate; | - | ||||||||||||
2937 | switch (QContainerImplHelper::mid(size(), &pos, &len)) { | - | ||||||||||||
2938 | case QContainerImplHelper::Null: | - | ||||||||||||
2939 | return QByteArray(); | - | ||||||||||||
2940 | case QContainerImplHelper::Empty: | - | ||||||||||||
2941 | { | - | ||||||||||||
2942 | QByteArrayDataPtr empty = { Data::allocate(0) }; | - | ||||||||||||
2943 | return QByteArray(empty); | - | ||||||||||||
2944 | } | - | ||||||||||||
2945 | case QContainerImplHelper::Full: | - | ||||||||||||
2946 | return *this; | - | ||||||||||||
2947 | case QContainerImplHelper::Subset: | - | ||||||||||||
2948 | return QByteArray(d->data() + pos, len); | - | ||||||||||||
2949 | } | - | ||||||||||||
2950 | Q_UNREACHABLE(); | - | ||||||||||||
2951 | return QByteArray(); | - | ||||||||||||
2952 | } | - | ||||||||||||
2953 | - | |||||||||||||
2954 | /*! | - | ||||||||||||
2955 | \fn QByteArray QByteArray::toLower() const | - | ||||||||||||
2956 | - | |||||||||||||
2957 | Returns a lowercase copy of the byte array. The bytearray is | - | ||||||||||||
2958 | interpreted as a Latin-1 encoded string. | - | ||||||||||||
2959 | - | |||||||||||||
2960 | Example: | - | ||||||||||||
2961 | \snippet code/src_corelib_tools_qbytearray.cpp 30 | - | ||||||||||||
2962 | - | |||||||||||||
2963 | \sa toUpper(), {8-bit Character Comparisons} | - | ||||||||||||
2964 | */ | - | ||||||||||||
2965 | - | |||||||||||||
2966 | // prevent the compiler from inlining the function in each of | - | ||||||||||||
2967 | // toLower and toUpper when the only difference is the table being used | - | ||||||||||||
2968 | // (even with constant propagation, there's no gain in performance). | - | ||||||||||||
2969 | template <typename T> | - | ||||||||||||
2970 | Q_NEVER_INLINE | - | ||||||||||||
2971 | static QByteArray toCase_template(T &input, const uchar * table) | - | ||||||||||||
2972 | { | - | ||||||||||||
2973 | // find the first bad character in input | - | ||||||||||||
2974 | const char *orig_begin = input.constBegin(); | - | ||||||||||||
2975 | const char *firstBad = orig_begin; | - | ||||||||||||
2976 | const char *e = input.constEnd(); | - | ||||||||||||
2977 | for ( ; firstBad != e ; ++firstBad) { | - | ||||||||||||
2978 | uchar ch = uchar(*firstBad); | - | ||||||||||||
2979 | uchar converted = table[ch]; | - | ||||||||||||
2980 | if (ch != converted) | - | ||||||||||||
2981 | break; | - | ||||||||||||
2982 | } | - | ||||||||||||
2983 | - | |||||||||||||
2984 | if (firstBad == e) | - | ||||||||||||
2985 | return qMove(input); | - | ||||||||||||
2986 | - | |||||||||||||
2987 | // transform the rest | - | ||||||||||||
2988 | QByteArray s = qMove(input); // will copy if T is const QByteArray | - | ||||||||||||
2989 | char *b = s.begin(); // will detach if necessary | - | ||||||||||||
2990 | char *p = b + (firstBad - orig_begin); | - | ||||||||||||
2991 | e = b + s.size(); | - | ||||||||||||
2992 | for ( ; p != e; ++p) { | - | ||||||||||||
2993 | *p = char(uchar(table[uchar(*p)])); | - | ||||||||||||
2994 | } | - | ||||||||||||
2995 | return s; | - | ||||||||||||
2996 | } | - | ||||||||||||
2997 | - | |||||||||||||
2998 | QByteArray QByteArray::toLower_helper(const QByteArray &a) | - | ||||||||||||
2999 | { | - | ||||||||||||
3000 | return toCase_template(a, latin1_lowercased); | - | ||||||||||||
3001 | } | - | ||||||||||||
3002 | - | |||||||||||||
3003 | QByteArray QByteArray::toLower_helper(QByteArray &a) | - | ||||||||||||
3004 | { | - | ||||||||||||
3005 | return toCase_template(a, latin1_lowercased); | - | ||||||||||||
3006 | } | - | ||||||||||||
3007 | - | |||||||||||||
3008 | /*! | - | ||||||||||||
3009 | \fn QByteArray QByteArray::toUpper() const | - | ||||||||||||
3010 | - | |||||||||||||
3011 | Returns an uppercase copy of the byte array. The bytearray is | - | ||||||||||||
3012 | interpreted as a Latin-1 encoded string. | - | ||||||||||||
3013 | - | |||||||||||||
3014 | Example: | - | ||||||||||||
3015 | \snippet code/src_corelib_tools_qbytearray.cpp 31 | - | ||||||||||||
3016 | - | |||||||||||||
3017 | \sa toLower(), {8-bit Character Comparisons} | - | ||||||||||||
3018 | */ | - | ||||||||||||
3019 | - | |||||||||||||
3020 | QByteArray QByteArray::toUpper_helper(const QByteArray &a) | - | ||||||||||||
3021 | { | - | ||||||||||||
3022 | return toCase_template(a, latin1_uppercased); | - | ||||||||||||
3023 | } | - | ||||||||||||
3024 | - | |||||||||||||
3025 | QByteArray QByteArray::toUpper_helper(QByteArray &a) | - | ||||||||||||
3026 | { | - | ||||||||||||
3027 | return toCase_template(a, latin1_uppercased); | - | ||||||||||||
3028 | } | - | ||||||||||||
3029 | - | |||||||||||||
3030 | /*! \fn void QByteArray::clear() | - | ||||||||||||
3031 | - | |||||||||||||
3032 | Clears the contents of the byte array and makes it null. | - | ||||||||||||
3033 | - | |||||||||||||
3034 | \sa resize(), isNull() | - | ||||||||||||
3035 | */ | - | ||||||||||||
3036 | - | |||||||||||||
3037 | void QByteArray::clear() | - | ||||||||||||
3038 | { | - | ||||||||||||
3039 | if (!d->ref.deref()) | - | ||||||||||||
3040 | Data::deallocate(d); | - | ||||||||||||
3041 | d = Data::sharedNull(); | - | ||||||||||||
3042 | } | - | ||||||||||||
3043 | - | |||||||||||||
3044 | #if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE)) | - | ||||||||||||
3045 | - | |||||||||||||
3046 | /*! \relates QByteArray | - | ||||||||||||
3047 | - | |||||||||||||
3048 | Writes byte array \a ba to the stream \a out and returns a reference | - | ||||||||||||
3049 | to the stream. | - | ||||||||||||
3050 | - | |||||||||||||
3051 | \sa {Serializing Qt Data Types} | - | ||||||||||||
3052 | */ | - | ||||||||||||
3053 | - | |||||||||||||
3054 | QDataStream &operator<<(QDataStream &out, const QByteArray &ba) | - | ||||||||||||
3055 | { | - | ||||||||||||
3056 | if (ba.isNull() && out.version() >= 6) { | - | ||||||||||||
3057 | out << (quint32)0xffffffff; | - | ||||||||||||
3058 | return out; | - | ||||||||||||
3059 | } | - | ||||||||||||
3060 | return out.writeBytes(ba.constData(), ba.size()); | - | ||||||||||||
3061 | } | - | ||||||||||||
3062 | - | |||||||||||||
3063 | /*! \relates QByteArray | - | ||||||||||||
3064 | - | |||||||||||||
3065 | Reads a byte array into \a ba from the stream \a in and returns a | - | ||||||||||||
3066 | reference to the stream. | - | ||||||||||||
3067 | - | |||||||||||||
3068 | \sa {Serializing Qt Data Types} | - | ||||||||||||
3069 | */ | - | ||||||||||||
3070 | - | |||||||||||||
3071 | QDataStream &operator>>(QDataStream &in, QByteArray &ba) | - | ||||||||||||
3072 | { | - | ||||||||||||
3073 | ba.clear(); | - | ||||||||||||
3074 | quint32 len; | - | ||||||||||||
3075 | in >> len; | - | ||||||||||||
3076 | if (len == 0xffffffff) | - | ||||||||||||
3077 | return in; | - | ||||||||||||
3078 | - | |||||||||||||
3079 | const quint32 Step = 1024 * 1024; | - | ||||||||||||
3080 | quint32 allocated = 0; | - | ||||||||||||
3081 | - | |||||||||||||
3082 | do { | - | ||||||||||||
3083 | int blockSize = qMin(Step, len - allocated); | - | ||||||||||||
3084 | ba.resize(allocated + blockSize); | - | ||||||||||||
3085 | if (in.readRawData(ba.data() + allocated, blockSize) != blockSize) { | - | ||||||||||||
3086 | ba.clear(); | - | ||||||||||||
3087 | in.setStatus(QDataStream::ReadPastEnd); | - | ||||||||||||
3088 | return in; | - | ||||||||||||
3089 | } | - | ||||||||||||
3090 | allocated += blockSize; | - | ||||||||||||
3091 | } while (allocated < len); | - | ||||||||||||
3092 | - | |||||||||||||
3093 | return in; | - | ||||||||||||
3094 | } | - | ||||||||||||
3095 | #endif // QT_NO_DATASTREAM | - | ||||||||||||
3096 | - | |||||||||||||
3097 | /*! \fn bool QByteArray::operator==(const QString &str) const | - | ||||||||||||
3098 | - | |||||||||||||
3099 | Returns \c true if this byte array is equal to string \a str; | - | ||||||||||||
3100 | otherwise returns \c false. | - | ||||||||||||
3101 | - | |||||||||||||
3102 | The Unicode data is converted into 8-bit characters using | - | ||||||||||||
3103 | QString::toUtf8(). | - | ||||||||||||
3104 | - | |||||||||||||
3105 | The comparison is case sensitive. | - | ||||||||||||
3106 | - | |||||||||||||
3107 | You can disable this operator by defining \c | - | ||||||||||||
3108 | QT_NO_CAST_FROM_ASCII when you compile your applications. You | - | ||||||||||||
3109 | then need to call QString::fromUtf8(), QString::fromLatin1(), | - | ||||||||||||
3110 | or QString::fromLocal8Bit() explicitly if you want to convert the byte | - | ||||||||||||
3111 | array to a QString before doing the comparison. | - | ||||||||||||
3112 | */ | - | ||||||||||||
3113 | - | |||||||||||||
3114 | /*! \fn bool QByteArray::operator!=(const QString &str) const | - | ||||||||||||
3115 | - | |||||||||||||
3116 | Returns \c true if this byte array is not equal to string \a str; | - | ||||||||||||
3117 | otherwise returns \c false. | - | ||||||||||||
3118 | - | |||||||||||||
3119 | The Unicode data is converted into 8-bit characters using | - | ||||||||||||
3120 | QString::toUtf8(). | - | ||||||||||||
3121 | - | |||||||||||||
3122 | The comparison is case sensitive. | - | ||||||||||||
3123 | - | |||||||||||||
3124 | You can disable this operator by defining \c | - | ||||||||||||
3125 | QT_NO_CAST_FROM_ASCII when you compile your applications. You | - | ||||||||||||
3126 | then need to call QString::fromUtf8(), QString::fromLatin1(), | - | ||||||||||||
3127 | or QString::fromLocal8Bit() explicitly if you want to convert the byte | - | ||||||||||||
3128 | array to a QString before doing the comparison. | - | ||||||||||||
3129 | */ | - | ||||||||||||
3130 | - | |||||||||||||
3131 | /*! \fn bool QByteArray::operator<(const QString &str) const | - | ||||||||||||
3132 | - | |||||||||||||
3133 | Returns \c true if this byte array is lexically less than string \a | - | ||||||||||||
3134 | str; otherwise returns \c false. | - | ||||||||||||
3135 | - | |||||||||||||
3136 | The Unicode data is converted into 8-bit characters using | - | ||||||||||||
3137 | QString::toUtf8(). | - | ||||||||||||
3138 | - | |||||||||||||
3139 | The comparison is case sensitive. | - | ||||||||||||
3140 | - | |||||||||||||
3141 | You can disable this operator by defining \c | - | ||||||||||||
3142 | QT_NO_CAST_FROM_ASCII when you compile your applications. You | - | ||||||||||||
3143 | then need to call QString::fromUtf8(), QString::fromLatin1(), | - | ||||||||||||
3144 | or QString::fromLocal8Bit() explicitly if you want to convert the byte | - | ||||||||||||
3145 | array to a QString before doing the comparison. | - | ||||||||||||
3146 | */ | - | ||||||||||||
3147 | - | |||||||||||||
3148 | /*! \fn bool QByteArray::operator>(const QString &str) const | - | ||||||||||||
3149 | - | |||||||||||||
3150 | Returns \c true if this byte array is lexically greater than string | - | ||||||||||||
3151 | \a str; otherwise returns \c false. | - | ||||||||||||
3152 | - | |||||||||||||
3153 | The Unicode data is converted into 8-bit characters using | - | ||||||||||||
3154 | QString::toUtf8(). | - | ||||||||||||
3155 | - | |||||||||||||
3156 | The comparison is case sensitive. | - | ||||||||||||
3157 | - | |||||||||||||
3158 | You can disable this operator by defining \c | - | ||||||||||||
3159 | QT_NO_CAST_FROM_ASCII when you compile your applications. You | - | ||||||||||||
3160 | then need to call QString::fromUtf8(), QString::fromLatin1(), | - | ||||||||||||
3161 | or QString::fromLocal8Bit() explicitly if you want to convert the byte | - | ||||||||||||
3162 | array to a QString before doing the comparison. | - | ||||||||||||
3163 | */ | - | ||||||||||||
3164 | - | |||||||||||||
3165 | /*! \fn bool QByteArray::operator<=(const QString &str) const | - | ||||||||||||
3166 | - | |||||||||||||
3167 | Returns \c true if this byte array is lexically less than or equal | - | ||||||||||||
3168 | to string \a str; otherwise returns \c false. | - | ||||||||||||
3169 | - | |||||||||||||
3170 | The Unicode data is converted into 8-bit characters using | - | ||||||||||||
3171 | QString::toUtf8(). | - | ||||||||||||
3172 | - | |||||||||||||
3173 | The comparison is case sensitive. | - | ||||||||||||
3174 | - | |||||||||||||
3175 | You can disable this operator by defining \c | - | ||||||||||||
3176 | QT_NO_CAST_FROM_ASCII when you compile your applications. You | - | ||||||||||||
3177 | then need to call QString::fromUtf8(), QString::fromLatin1(), | - | ||||||||||||
3178 | or QString::fromLocal8Bit() explicitly if you want to convert the byte | - | ||||||||||||
3179 | array to a QString before doing the comparison. | - | ||||||||||||
3180 | */ | - | ||||||||||||
3181 | - | |||||||||||||
3182 | /*! \fn bool QByteArray::operator>=(const QString &str) const | - | ||||||||||||
3183 | - | |||||||||||||
3184 | Returns \c true if this byte array is greater than or equal to string | - | ||||||||||||
3185 | \a str; otherwise returns \c false. | - | ||||||||||||
3186 | - | |||||||||||||
3187 | The Unicode data is converted into 8-bit characters using | - | ||||||||||||
3188 | QString::toUtf8(). | - | ||||||||||||
3189 | - | |||||||||||||
3190 | The comparison is case sensitive. | - | ||||||||||||
3191 | - | |||||||||||||
3192 | You can disable this operator by defining \c | - | ||||||||||||
3193 | QT_NO_CAST_FROM_ASCII when you compile your applications. You | - | ||||||||||||
3194 | then need to call QString::fromUtf8(), QString::fromLatin1(), | - | ||||||||||||
3195 | or QString::fromLocal8Bit() explicitly if you want to convert the byte | - | ||||||||||||
3196 | array to a QString before doing the comparison. | - | ||||||||||||
3197 | */ | - | ||||||||||||
3198 | - | |||||||||||||
3199 | /*! \fn bool operator==(const QByteArray &a1, const QByteArray &a2) | - | ||||||||||||
3200 | \relates QByteArray | - | ||||||||||||
3201 | - | |||||||||||||
3202 | \overload | - | ||||||||||||
3203 | - | |||||||||||||
3204 | Returns \c true if byte array \a a1 is equal to byte array \a a2; | - | ||||||||||||
3205 | otherwise returns \c false. | - | ||||||||||||
3206 | */ | - | ||||||||||||
3207 | - | |||||||||||||
3208 | /*! \fn bool operator==(const QByteArray &a1, const char *a2) | - | ||||||||||||
3209 | \relates QByteArray | - | ||||||||||||
3210 | - | |||||||||||||
3211 | \overload | - | ||||||||||||
3212 | - | |||||||||||||
3213 | Returns \c true if byte array \a a1 is equal to string \a a2; | - | ||||||||||||
3214 | otherwise returns \c false. | - | ||||||||||||
3215 | */ | - | ||||||||||||
3216 | - | |||||||||||||
3217 | /*! \fn bool operator==(const char *a1, const QByteArray &a2) | - | ||||||||||||
3218 | \relates QByteArray | - | ||||||||||||
3219 | - | |||||||||||||
3220 | \overload | - | ||||||||||||
3221 | - | |||||||||||||
3222 | Returns \c true if string \a a1 is equal to byte array \a a2; | - | ||||||||||||
3223 | otherwise returns \c false. | - | ||||||||||||
3224 | */ | - | ||||||||||||
3225 | - | |||||||||||||
3226 | /*! \fn bool operator!=(const QByteArray &a1, const QByteArray &a2) | - | ||||||||||||
3227 | \relates QByteArray | - | ||||||||||||
3228 | - | |||||||||||||
3229 | \overload | - | ||||||||||||
3230 | - | |||||||||||||
3231 | Returns \c true if byte array \a a1 is not equal to byte array \a a2; | - | ||||||||||||
3232 | otherwise returns \c false. | - | ||||||||||||
3233 | */ | - | ||||||||||||
3234 | - | |||||||||||||
3235 | /*! \fn bool operator!=(const QByteArray &a1, const char *a2) | - | ||||||||||||
3236 | \relates QByteArray | - | ||||||||||||
3237 | - | |||||||||||||
3238 | \overload | - | ||||||||||||
3239 | - | |||||||||||||
3240 | Returns \c true if byte array \a a1 is not equal to string \a a2; | - | ||||||||||||
3241 | otherwise returns \c false. | - | ||||||||||||
3242 | */ | - | ||||||||||||
3243 | - | |||||||||||||
3244 | /*! \fn bool operator!=(const char *a1, const QByteArray &a2) | - | ||||||||||||
3245 | \relates QByteArray | - | ||||||||||||
3246 | - | |||||||||||||
3247 | \overload | - | ||||||||||||
3248 | - | |||||||||||||
3249 | Returns \c true if string \a a1 is not equal to byte array \a a2; | - | ||||||||||||
3250 | otherwise returns \c false. | - | ||||||||||||
3251 | */ | - | ||||||||||||
3252 | - | |||||||||||||
3253 | /*! \fn bool operator<(const QByteArray &a1, const QByteArray &a2) | - | ||||||||||||
3254 | \relates QByteArray | - | ||||||||||||
3255 | - | |||||||||||||
3256 | \overload | - | ||||||||||||
3257 | - | |||||||||||||
3258 | Returns \c true if byte array \a a1 is lexically less than byte array | - | ||||||||||||
3259 | \a a2; otherwise returns \c false. | - | ||||||||||||
3260 | */ | - | ||||||||||||
3261 | - | |||||||||||||
3262 | /*! \fn inline bool operator<(const QByteArray &a1, const char *a2) | - | ||||||||||||
3263 | \relates QByteArray | - | ||||||||||||
3264 | - | |||||||||||||
3265 | \overload | - | ||||||||||||
3266 | - | |||||||||||||
3267 | Returns \c true if byte array \a a1 is lexically less than string | - | ||||||||||||
3268 | \a a2; otherwise returns \c false. | - | ||||||||||||
3269 | */ | - | ||||||||||||
3270 | - | |||||||||||||
3271 | /*! \fn bool operator<(const char *a1, const QByteArray &a2) | - | ||||||||||||
3272 | \relates QByteArray | - | ||||||||||||
3273 | - | |||||||||||||
3274 | \overload | - | ||||||||||||
3275 | - | |||||||||||||
3276 | Returns \c true if string \a a1 is lexically less than byte array | - | ||||||||||||
3277 | \a a2; otherwise returns \c false. | - | ||||||||||||
3278 | */ | - | ||||||||||||
3279 | - | |||||||||||||
3280 | /*! \fn bool operator<=(const QByteArray &a1, const QByteArray &a2) | - | ||||||||||||
3281 | \relates QByteArray | - | ||||||||||||
3282 | - | |||||||||||||
3283 | \overload | - | ||||||||||||
3284 | - | |||||||||||||
3285 | Returns \c true if byte array \a a1 is lexically less than or equal | - | ||||||||||||
3286 | to byte array \a a2; otherwise returns \c false. | - | ||||||||||||
3287 | */ | - | ||||||||||||
3288 | - | |||||||||||||
3289 | /*! \fn bool operator<=(const QByteArray &a1, const char *a2) | - | ||||||||||||
3290 | \relates QByteArray | - | ||||||||||||
3291 | - | |||||||||||||
3292 | \overload | - | ||||||||||||
3293 | - | |||||||||||||
3294 | Returns \c true if byte array \a a1 is lexically less than or equal | - | ||||||||||||
3295 | to string \a a2; otherwise returns \c false. | - | ||||||||||||
3296 | */ | - | ||||||||||||
3297 | - | |||||||||||||
3298 | /*! \fn bool operator<=(const char *a1, const QByteArray &a2) | - | ||||||||||||
3299 | \relates QByteArray | - | ||||||||||||
3300 | - | |||||||||||||
3301 | \overload | - | ||||||||||||
3302 | - | |||||||||||||
3303 | Returns \c true if string \a a1 is lexically less than or equal | - | ||||||||||||
3304 | to byte array \a a2; otherwise returns \c false. | - | ||||||||||||
3305 | */ | - | ||||||||||||
3306 | - | |||||||||||||
3307 | /*! \fn bool operator>(const QByteArray &a1, const QByteArray &a2) | - | ||||||||||||
3308 | \relates QByteArray | - | ||||||||||||
3309 | - | |||||||||||||
3310 | \overload | - | ||||||||||||
3311 | - | |||||||||||||
3312 | Returns \c true if byte array \a a1 is lexically greater than byte | - | ||||||||||||
3313 | array \a a2; otherwise returns \c false. | - | ||||||||||||
3314 | */ | - | ||||||||||||
3315 | - | |||||||||||||
3316 | /*! \fn bool operator>(const QByteArray &a1, const char *a2) | - | ||||||||||||
3317 | \relates QByteArray | - | ||||||||||||
3318 | - | |||||||||||||
3319 | \overload | - | ||||||||||||
3320 | - | |||||||||||||
3321 | Returns \c true if byte array \a a1 is lexically greater than string | - | ||||||||||||
3322 | \a a2; otherwise returns \c false. | - | ||||||||||||
3323 | */ | - | ||||||||||||
3324 | - | |||||||||||||
3325 | /*! \fn bool operator>(const char *a1, const QByteArray &a2) | - | ||||||||||||
3326 | \relates QByteArray | - | ||||||||||||
3327 | - | |||||||||||||
3328 | \overload | - | ||||||||||||
3329 | - | |||||||||||||
3330 | Returns \c true if string \a a1 is lexically greater than byte array | - | ||||||||||||
3331 | \a a2; otherwise returns \c false. | - | ||||||||||||
3332 | */ | - | ||||||||||||
3333 | - | |||||||||||||
3334 | /*! \fn bool operator>=(const QByteArray &a1, const QByteArray &a2) | - | ||||||||||||
3335 | \relates QByteArray | - | ||||||||||||
3336 | - | |||||||||||||
3337 | \overload | - | ||||||||||||
3338 | - | |||||||||||||
3339 | Returns \c true if byte array \a a1 is lexically greater than or | - | ||||||||||||
3340 | equal to byte array \a a2; otherwise returns \c false. | - | ||||||||||||
3341 | */ | - | ||||||||||||
3342 | - | |||||||||||||
3343 | /*! \fn bool operator>=(const QByteArray &a1, const char *a2) | - | ||||||||||||
3344 | \relates QByteArray | - | ||||||||||||
3345 | - | |||||||||||||
3346 | \overload | - | ||||||||||||
3347 | - | |||||||||||||
3348 | Returns \c true if byte array \a a1 is lexically greater than or | - | ||||||||||||
3349 | equal to string \a a2; otherwise returns \c false. | - | ||||||||||||
3350 | */ | - | ||||||||||||
3351 | - | |||||||||||||
3352 | /*! \fn bool operator>=(const char *a1, const QByteArray &a2) | - | ||||||||||||
3353 | \relates QByteArray | - | ||||||||||||
3354 | - | |||||||||||||
3355 | \overload | - | ||||||||||||
3356 | - | |||||||||||||
3357 | Returns \c true if string \a a1 is lexically greater than or | - | ||||||||||||
3358 | equal to byte array \a a2; otherwise returns \c false. | - | ||||||||||||
3359 | */ | - | ||||||||||||
3360 | - | |||||||||||||
3361 | /*! \fn const QByteArray operator+(const QByteArray &a1, const QByteArray &a2) | - | ||||||||||||
3362 | \relates QByteArray | - | ||||||||||||
3363 | - | |||||||||||||
3364 | Returns a byte array that is the result of concatenating byte | - | ||||||||||||
3365 | array \a a1 and byte array \a a2. | - | ||||||||||||
3366 | - | |||||||||||||
3367 | \sa QByteArray::operator+=() | - | ||||||||||||
3368 | */ | - | ||||||||||||
3369 | - | |||||||||||||
3370 | /*! \fn const QByteArray operator+(const QByteArray &a1, const char *a2) | - | ||||||||||||
3371 | \relates QByteArray | - | ||||||||||||
3372 | - | |||||||||||||
3373 | \overload | - | ||||||||||||
3374 | - | |||||||||||||
3375 | Returns a byte array that is the result of concatenating byte | - | ||||||||||||
3376 | array \a a1 and string \a a2. | - | ||||||||||||
3377 | */ | - | ||||||||||||
3378 | - | |||||||||||||
3379 | /*! \fn const QByteArray operator+(const QByteArray &a1, char a2) | - | ||||||||||||
3380 | \relates QByteArray | - | ||||||||||||
3381 | - | |||||||||||||
3382 | \overload | - | ||||||||||||
3383 | - | |||||||||||||
3384 | Returns a byte array that is the result of concatenating byte | - | ||||||||||||
3385 | array \a a1 and character \a a2. | - | ||||||||||||
3386 | */ | - | ||||||||||||
3387 | - | |||||||||||||
3388 | /*! \fn const QByteArray operator+(const char *a1, const QByteArray &a2) | - | ||||||||||||
3389 | \relates QByteArray | - | ||||||||||||
3390 | - | |||||||||||||
3391 | \overload | - | ||||||||||||
3392 | - | |||||||||||||
3393 | Returns a byte array that is the result of concatenating string | - | ||||||||||||
3394 | \a a1 and byte array \a a2. | - | ||||||||||||
3395 | */ | - | ||||||||||||
3396 | - | |||||||||||||
3397 | /*! \fn const QByteArray operator+(char a1, const QByteArray &a2) | - | ||||||||||||
3398 | \relates QByteArray | - | ||||||||||||
3399 | - | |||||||||||||
3400 | \overload | - | ||||||||||||
3401 | - | |||||||||||||
3402 | Returns a byte array that is the result of concatenating character | - | ||||||||||||
3403 | \a a1 and byte array \a a2. | - | ||||||||||||
3404 | */ | - | ||||||||||||
3405 | - | |||||||||||||
3406 | /*! | - | ||||||||||||
3407 | \fn QByteArray QByteArray::simplified() const | - | ||||||||||||
3408 | - | |||||||||||||
3409 | Returns a byte array that has whitespace removed from the start | - | ||||||||||||
3410 | and the end, and which has each sequence of internal whitespace | - | ||||||||||||
3411 | replaced with a single space. | - | ||||||||||||
3412 | - | |||||||||||||
3413 | Whitespace means any character for which the standard C++ | - | ||||||||||||
3414 | \c isspace() function returns \c true in the C locale. This includes the ASCII | - | ||||||||||||
3415 | isspace() function returns \c true in the C locale. This includes the ASCII | - | ||||||||||||
3416 | characters '\\t', '\\n', '\\v', '\\f', '\\r', and ' '. | - | ||||||||||||
3417 | - | |||||||||||||
3418 | Example: | - | ||||||||||||
3419 | \snippet code/src_corelib_tools_qbytearray.cpp 32 | - | ||||||||||||
3420 | - | |||||||||||||
3421 | \sa trimmed() | - | ||||||||||||
3422 | */ | - | ||||||||||||
3423 | QByteArray QByteArray::simplified_helper(const QByteArray &a) | - | ||||||||||||
3424 | { | - | ||||||||||||
3425 | return QStringAlgorithms<const QByteArray>::simplified_helper(a); | - | ||||||||||||
3426 | } | - | ||||||||||||
3427 | - | |||||||||||||
3428 | QByteArray QByteArray::simplified_helper(QByteArray &a) | - | ||||||||||||
3429 | { | - | ||||||||||||
3430 | return QStringAlgorithms<QByteArray>::simplified_helper(a); | - | ||||||||||||
3431 | } | - | ||||||||||||
3432 | - | |||||||||||||
3433 | /*! | - | ||||||||||||
3434 | \fn QByteArray QByteArray::trimmed() const | - | ||||||||||||
3435 | - | |||||||||||||
3436 | Returns a byte array that has whitespace removed from the start | - | ||||||||||||
3437 | and the end. | - | ||||||||||||
3438 | - | |||||||||||||
3439 | Whitespace means any character for which the standard C++ | - | ||||||||||||
3440 | \c isspace() function returns \c true in the C locale. This includes the ASCII | - | ||||||||||||
3441 | characters '\\t', '\\n', '\\v', '\\f', '\\r', and ' '. | - | ||||||||||||
3442 | - | |||||||||||||
3443 | Example: | - | ||||||||||||
3444 | \snippet code/src_corelib_tools_qbytearray.cpp 33 | - | ||||||||||||
3445 | - | |||||||||||||
3446 | Unlike simplified(), \l {QByteArray::trimmed()}{trimmed()} leaves internal whitespace alone. | - | ||||||||||||
3447 | - | |||||||||||||
3448 | \sa simplified() | - | ||||||||||||
3449 | */ | - | ||||||||||||
3450 | QByteArray QByteArray::trimmed_helper(const QByteArray &a) | - | ||||||||||||
3451 | { | - | ||||||||||||
3452 | return QStringAlgorithms<const QByteArray>::trimmed_helper(a); | - | ||||||||||||
3453 | } | - | ||||||||||||
3454 | - | |||||||||||||
3455 | QByteArray QByteArray::trimmed_helper(QByteArray &a) | - | ||||||||||||
3456 | { | - | ||||||||||||
3457 | return QStringAlgorithms<QByteArray>::trimmed_helper(a); | - | ||||||||||||
3458 | } | - | ||||||||||||
3459 | - | |||||||||||||
3460 | - | |||||||||||||
3461 | /*! | - | ||||||||||||
3462 | Returns a byte array of size \a width that contains this byte | - | ||||||||||||
3463 | array padded by the \a fill character. | - | ||||||||||||
3464 | - | |||||||||||||
3465 | If \a truncate is false and the size() of the byte array is more | - | ||||||||||||
3466 | than \a width, then the returned byte array is a copy of this byte | - | ||||||||||||
3467 | array. | - | ||||||||||||
3468 | - | |||||||||||||
3469 | If \a truncate is true and the size() of the byte array is more | - | ||||||||||||
3470 | than \a width, then any bytes in a copy of the byte array | - | ||||||||||||
3471 | after position \a width are removed, and the copy is returned. | - | ||||||||||||
3472 | - | |||||||||||||
3473 | Example: | - | ||||||||||||
3474 | \snippet code/src_corelib_tools_qbytearray.cpp 34 | - | ||||||||||||
3475 | - | |||||||||||||
3476 | \sa rightJustified() | - | ||||||||||||
3477 | */ | - | ||||||||||||
3478 | - | |||||||||||||
3479 | QByteArray QByteArray::leftJustified(int width, char fill, bool truncate) const | - | ||||||||||||
3480 | { | - | ||||||||||||
3481 | QByteArray result; | - | ||||||||||||
3482 | int len = d->size; | - | ||||||||||||
3483 | int padlen = width - len; | - | ||||||||||||
3484 | if (padlen > 0) { | - | ||||||||||||
3485 | result.resize(len+padlen); | - | ||||||||||||
3486 | if (len) | - | ||||||||||||
3487 | memcpy(result.d->data(), d->data(), len); | - | ||||||||||||
3488 | memset(result.d->data()+len, fill, padlen); | - | ||||||||||||
3489 | } else { | - | ||||||||||||
3490 | if (truncate) | - | ||||||||||||
3491 | result = left(width); | - | ||||||||||||
3492 | else | - | ||||||||||||
3493 | result = *this; | - | ||||||||||||
3494 | } | - | ||||||||||||
3495 | return result; | - | ||||||||||||
3496 | } | - | ||||||||||||
3497 | - | |||||||||||||
3498 | /*! | - | ||||||||||||
3499 | Returns a byte array of size \a width that contains the \a fill | - | ||||||||||||
3500 | character followed by this byte array. | - | ||||||||||||
3501 | - | |||||||||||||
3502 | If \a truncate is false and the size of the byte array is more | - | ||||||||||||
3503 | than \a width, then the returned byte array is a copy of this byte | - | ||||||||||||
3504 | array. | - | ||||||||||||
3505 | - | |||||||||||||
3506 | If \a truncate is true and the size of the byte array is more | - | ||||||||||||
3507 | than \a width, then the resulting byte array is truncated at | - | ||||||||||||
3508 | position \a width. | - | ||||||||||||
3509 | - | |||||||||||||
3510 | Example: | - | ||||||||||||
3511 | \snippet code/src_corelib_tools_qbytearray.cpp 35 | - | ||||||||||||
3512 | - | |||||||||||||
3513 | \sa leftJustified() | - | ||||||||||||
3514 | */ | - | ||||||||||||
3515 | - | |||||||||||||
3516 | QByteArray QByteArray::rightJustified(int width, char fill, bool truncate) const | - | ||||||||||||
3517 | { | - | ||||||||||||
3518 | QByteArray result; | - | ||||||||||||
3519 | int len = d->size; | - | ||||||||||||
3520 | int padlen = width - len; | - | ||||||||||||
3521 | if (padlen > 0) { | - | ||||||||||||
3522 | result.resize(len+padlen); | - | ||||||||||||
3523 | if (len) | - | ||||||||||||
3524 | memcpy(result.d->data()+padlen, data(), len); | - | ||||||||||||
3525 | memset(result.d->data(), fill, padlen); | - | ||||||||||||
3526 | } else { | - | ||||||||||||
3527 | if (truncate) | - | ||||||||||||
3528 | result = left(width); | - | ||||||||||||
3529 | else | - | ||||||||||||
3530 | result = *this; | - | ||||||||||||
3531 | } | - | ||||||||||||
3532 | return result; | - | ||||||||||||
3533 | } | - | ||||||||||||
3534 | - | |||||||||||||
3535 | bool QByteArray::isNull() const { return d == QArrayData::sharedNull(); } | - | ||||||||||||
3536 | - | |||||||||||||
3537 | static qlonglong toIntegral_helper(const char *data, bool *ok, int base, qlonglong) | - | ||||||||||||
3538 | { | - | ||||||||||||
3539 | return QLocaleData::bytearrayToLongLong(data, base, ok); | - | ||||||||||||
3540 | } | - | ||||||||||||
3541 | - | |||||||||||||
3542 | static qulonglong toIntegral_helper(const char *data, bool *ok, int base, qulonglong) | - | ||||||||||||
3543 | { | - | ||||||||||||
3544 | return QLocaleData::bytearrayToUnsLongLong(data, base, ok); | - | ||||||||||||
3545 | } | - | ||||||||||||
3546 | - | |||||||||||||
3547 | template <typename T> static inline | - | ||||||||||||
3548 | T toIntegral_helper(const char *data, bool *ok, int base) | - | ||||||||||||
3549 | { | - | ||||||||||||
3550 | // ### Qt6: use std::conditional<std::is_unsigned<T>::value, qulonglong, qlonglong>::type | - | ||||||||||||
3551 | const bool isUnsigned = T(0) < T(-1); | - | ||||||||||||
3552 | typedef typename QtPrivate::QConditional<isUnsigned, qulonglong, qlonglong>::Type Int64; | - | ||||||||||||
3553 | - | |||||||||||||
3554 | #if defined(QT_CHECK_RANGE) | - | ||||||||||||
3555 | if (base != 0 && (base < 2 || base > 36)) { | - | ||||||||||||
3556 | qWarning("QByteArray::toIntegral: Invalid base %d", base); | - | ||||||||||||
3557 | base = 10; | - | ||||||||||||
3558 | } | - | ||||||||||||
3559 | #endif | - | ||||||||||||
3560 | - | |||||||||||||
3561 | // we select the right overload by the last, unused parameter | - | ||||||||||||
3562 | Int64 val = toIntegral_helper(data, ok, base, Int64()); | - | ||||||||||||
3563 | if (T(val) != val) { | - | ||||||||||||
3564 | if (ok) | - | ||||||||||||
3565 | *ok = false; | - | ||||||||||||
3566 | val = 0; | - | ||||||||||||
3567 | } | - | ||||||||||||
3568 | return T(val); | - | ||||||||||||
3569 | } | - | ||||||||||||
3570 | - | |||||||||||||
3571 | /*! | - | ||||||||||||
3572 | Returns the byte array converted to a \c {long long} using base \a | - | ||||||||||||
3573 | base, which is 10 by default and must be between 2 and 36, or 0. | - | ||||||||||||
3574 | - | |||||||||||||
3575 | If \a base is 0, the base is determined automatically using the | - | ||||||||||||
3576 | following rules: If the byte array begins with "0x", it is assumed to | - | ||||||||||||
3577 | be hexadecimal; if it begins with "0", it is assumed to be octal; | - | ||||||||||||
3578 | otherwise it is assumed to be decimal. | - | ||||||||||||
3579 | - | |||||||||||||
3580 | Returns 0 if the conversion fails. | - | ||||||||||||
3581 | - | |||||||||||||
3582 | If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to | - | ||||||||||||
3583 | false; otherwise *\a{ok} is set to true. | - | ||||||||||||
3584 | - | |||||||||||||
3585 | \note The conversion of the number is performed in the default C locale, | - | ||||||||||||
3586 | irrespective of the user's locale. | - | ||||||||||||
3587 | - | |||||||||||||
3588 | \sa number() | - | ||||||||||||
3589 | */ | - | ||||||||||||
3590 | - | |||||||||||||
3591 | qlonglong QByteArray::toLongLong(bool *ok, int base) const | - | ||||||||||||
3592 | { | - | ||||||||||||
3593 | return toIntegral_helper<qlonglong>(nulTerminated().constData(), ok, base); | - | ||||||||||||
3594 | } | - | ||||||||||||
3595 | - | |||||||||||||
3596 | /*! | - | ||||||||||||
3597 | Returns the byte array converted to an \c {unsigned long long} | - | ||||||||||||
3598 | using base \a base, which is 10 by default and must be between 2 | - | ||||||||||||
3599 | and 36, or 0. | - | ||||||||||||
3600 | - | |||||||||||||
3601 | If \a base is 0, the base is determined automatically using the | - | ||||||||||||
3602 | following rules: If the byte array begins with "0x", it is assumed to | - | ||||||||||||
3603 | be hexadecimal; if it begins with "0", it is assumed to be octal; | - | ||||||||||||
3604 | otherwise it is assumed to be decimal. | - | ||||||||||||
3605 | - | |||||||||||||
3606 | Returns 0 if the conversion fails. | - | ||||||||||||
3607 | - | |||||||||||||
3608 | If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to | - | ||||||||||||
3609 | false; otherwise *\a{ok} is set to true. | - | ||||||||||||
3610 | - | |||||||||||||
3611 | \note The conversion of the number is performed in the default C locale, | - | ||||||||||||
3612 | irrespective of the user's locale. | - | ||||||||||||
3613 | - | |||||||||||||
3614 | \sa number() | - | ||||||||||||
3615 | */ | - | ||||||||||||
3616 | - | |||||||||||||
3617 | qulonglong QByteArray::toULongLong(bool *ok, int base) const | - | ||||||||||||
3618 | { | - | ||||||||||||
3619 | return toIntegral_helper<qulonglong>(nulTerminated().constData(), ok, base); | - | ||||||||||||
3620 | } | - | ||||||||||||
3621 | - | |||||||||||||
3622 | /*! | - | ||||||||||||
3623 | Returns the byte array converted to an \c int using base \a | - | ||||||||||||
3624 | base, which is 10 by default and must be between 2 and 36, or 0. | - | ||||||||||||
3625 | - | |||||||||||||
3626 | If \a base is 0, the base is determined automatically using the | - | ||||||||||||
3627 | following rules: If the byte array begins with "0x", it is assumed to | - | ||||||||||||
3628 | be hexadecimal; if it begins with "0", it is assumed to be octal; | - | ||||||||||||
3629 | otherwise it is assumed to be decimal. | - | ||||||||||||
3630 | - | |||||||||||||
3631 | Returns 0 if the conversion fails. | - | ||||||||||||
3632 | - | |||||||||||||
3633 | If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to | - | ||||||||||||
3634 | false; otherwise *\a{ok} is set to true. | - | ||||||||||||
3635 | - | |||||||||||||
3636 | \snippet code/src_corelib_tools_qbytearray.cpp 36 | - | ||||||||||||
3637 | - | |||||||||||||
3638 | \note The conversion of the number is performed in the default C locale, | - | ||||||||||||
3639 | irrespective of the user's locale. | - | ||||||||||||
3640 | - | |||||||||||||
3641 | \sa number() | - | ||||||||||||
3642 | */ | - | ||||||||||||
3643 | - | |||||||||||||
3644 | int QByteArray::toInt(bool *ok, int base) const | - | ||||||||||||
3645 | { | - | ||||||||||||
3646 | return toIntegral_helper<int>(nulTerminated().constData(), ok, base); | - | ||||||||||||
3647 | } | - | ||||||||||||
3648 | - | |||||||||||||
3649 | /*! | - | ||||||||||||
3650 | Returns the byte array converted to an \c {unsigned int} using base \a | - | ||||||||||||
3651 | base, which is 10 by default and must be between 2 and 36, or 0. | - | ||||||||||||
3652 | - | |||||||||||||
3653 | If \a base is 0, the base is determined automatically using the | - | ||||||||||||
3654 | following rules: If the byte array begins with "0x", it is assumed to | - | ||||||||||||
3655 | be hexadecimal; if it begins with "0", it is assumed to be octal; | - | ||||||||||||
3656 | otherwise it is assumed to be decimal. | - | ||||||||||||
3657 | - | |||||||||||||
3658 | Returns 0 if the conversion fails. | - | ||||||||||||
3659 | - | |||||||||||||
3660 | If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to | - | ||||||||||||
3661 | false; otherwise *\a{ok} is set to true. | - | ||||||||||||
3662 | - | |||||||||||||
3663 | \note The conversion of the number is performed in the default C locale, | - | ||||||||||||
3664 | irrespective of the user's locale. | - | ||||||||||||
3665 | - | |||||||||||||
3666 | \sa number() | - | ||||||||||||
3667 | */ | - | ||||||||||||
3668 | - | |||||||||||||
3669 | uint QByteArray::toUInt(bool *ok, int base) const | - | ||||||||||||
3670 | { | - | ||||||||||||
3671 | return toIntegral_helper<uint>(nulTerminated().constData(), ok, base); | - | ||||||||||||
3672 | } | - | ||||||||||||
3673 | - | |||||||||||||
3674 | /*! | - | ||||||||||||
3675 | \since 4.1 | - | ||||||||||||
3676 | - | |||||||||||||
3677 | Returns the byte array converted to a \c long int using base \a | - | ||||||||||||
3678 | base, which is 10 by default and must be between 2 and 36, or 0. | - | ||||||||||||
3679 | - | |||||||||||||
3680 | If \a base is 0, the base is determined automatically using the | - | ||||||||||||
3681 | following rules: If the byte array begins with "0x", it is assumed to | - | ||||||||||||
3682 | be hexadecimal; if it begins with "0", it is assumed to be octal; | - | ||||||||||||
3683 | otherwise it is assumed to be decimal. | - | ||||||||||||
3684 | - | |||||||||||||
3685 | Returns 0 if the conversion fails. | - | ||||||||||||
3686 | - | |||||||||||||
3687 | If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to | - | ||||||||||||
3688 | false; otherwise *\a{ok} is set to true. | - | ||||||||||||
3689 | - | |||||||||||||
3690 | \snippet code/src_corelib_tools_qbytearray.cpp 37 | - | ||||||||||||
3691 | - | |||||||||||||
3692 | \note The conversion of the number is performed in the default C locale, | - | ||||||||||||
3693 | irrespective of the user's locale. | - | ||||||||||||
3694 | - | |||||||||||||
3695 | \sa number() | - | ||||||||||||
3696 | */ | - | ||||||||||||
3697 | long QByteArray::toLong(bool *ok, int base) const | - | ||||||||||||
3698 | { | - | ||||||||||||
3699 | return toIntegral_helper<long>(nulTerminated().constData(), ok, base); | - | ||||||||||||
3700 | } | - | ||||||||||||
3701 | - | |||||||||||||
3702 | /*! | - | ||||||||||||
3703 | \since 4.1 | - | ||||||||||||
3704 | - | |||||||||||||
3705 | Returns the byte array converted to an \c {unsigned long int} using base \a | - | ||||||||||||
3706 | base, which is 10 by default and must be between 2 and 36, or 0. | - | ||||||||||||
3707 | - | |||||||||||||
3708 | If \a base is 0, the base is determined automatically using the | - | ||||||||||||
3709 | following rules: If the byte array begins with "0x", it is assumed to | - | ||||||||||||
3710 | be hexadecimal; if it begins with "0", it is assumed to be octal; | - | ||||||||||||
3711 | otherwise it is assumed to be decimal. | - | ||||||||||||
3712 | - | |||||||||||||
3713 | Returns 0 if the conversion fails. | - | ||||||||||||
3714 | - | |||||||||||||
3715 | If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to | - | ||||||||||||
3716 | false; otherwise *\a{ok} is set to true. | - | ||||||||||||
3717 | - | |||||||||||||
3718 | \note The conversion of the number is performed in the default C locale, | - | ||||||||||||
3719 | irrespective of the user's locale. | - | ||||||||||||
3720 | - | |||||||||||||
3721 | \sa number() | - | ||||||||||||
3722 | */ | - | ||||||||||||
3723 | ulong QByteArray::toULong(bool *ok, int base) const | - | ||||||||||||
3724 | { | - | ||||||||||||
3725 | return toIntegral_helper<ulong>(nulTerminated().constData(), ok, base); | - | ||||||||||||
3726 | } | - | ||||||||||||
3727 | - | |||||||||||||
3728 | /*! | - | ||||||||||||
3729 | Returns the byte array converted to a \c short using base \a | - | ||||||||||||
3730 | base, which is 10 by default and must be between 2 and 36, or 0. | - | ||||||||||||
3731 | - | |||||||||||||
3732 | If \a base is 0, the base is determined automatically using the | - | ||||||||||||
3733 | following rules: If the byte array begins with "0x", it is assumed to | - | ||||||||||||
3734 | be hexadecimal; if it begins with "0", it is assumed to be octal; | - | ||||||||||||
3735 | otherwise it is assumed to be decimal. | - | ||||||||||||
3736 | - | |||||||||||||
3737 | Returns 0 if the conversion fails. | - | ||||||||||||
3738 | - | |||||||||||||
3739 | If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to | - | ||||||||||||
3740 | false; otherwise *\a{ok} is set to true. | - | ||||||||||||
3741 | - | |||||||||||||
3742 | \note The conversion of the number is performed in the default C locale, | - | ||||||||||||
3743 | irrespective of the user's locale. | - | ||||||||||||
3744 | - | |||||||||||||
3745 | \sa number() | - | ||||||||||||
3746 | */ | - | ||||||||||||
3747 | - | |||||||||||||
3748 | short QByteArray::toShort(bool *ok, int base) const | - | ||||||||||||
3749 | { | - | ||||||||||||
3750 | return toIntegral_helper<short>(nulTerminated().constData(), ok, base); | - | ||||||||||||
3751 | } | - | ||||||||||||
3752 | - | |||||||||||||
3753 | /*! | - | ||||||||||||
3754 | Returns the byte array converted to an \c {unsigned short} using base \a | - | ||||||||||||
3755 | base, which is 10 by default and must be between 2 and 36, or 0. | - | ||||||||||||
3756 | - | |||||||||||||
3757 | If \a base is 0, the base is determined automatically using the | - | ||||||||||||
3758 | following rules: If the byte array begins with "0x", it is assumed to | - | ||||||||||||
3759 | be hexadecimal; if it begins with "0", it is assumed to be octal; | - | ||||||||||||
3760 | otherwise it is assumed to be decimal. | - | ||||||||||||
3761 | - | |||||||||||||
3762 | Returns 0 if the conversion fails. | - | ||||||||||||
3763 | - | |||||||||||||
3764 | If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to | - | ||||||||||||
3765 | false; otherwise *\a{ok} is set to true. | - | ||||||||||||
3766 | - | |||||||||||||
3767 | \note The conversion of the number is performed in the default C locale, | - | ||||||||||||
3768 | irrespective of the user's locale. | - | ||||||||||||
3769 | - | |||||||||||||
3770 | \sa number() | - | ||||||||||||
3771 | */ | - | ||||||||||||
3772 | - | |||||||||||||
3773 | ushort QByteArray::toUShort(bool *ok, int base) const | - | ||||||||||||
3774 | { | - | ||||||||||||
3775 | return toIntegral_helper<ushort>(nulTerminated().constData(), ok, base); | - | ||||||||||||
3776 | } | - | ||||||||||||
3777 | - | |||||||||||||
3778 | - | |||||||||||||
3779 | /*! | - | ||||||||||||
3780 | Returns the byte array converted to a \c double value. | - | ||||||||||||
3781 | - | |||||||||||||
3782 | Returns 0.0 if the conversion fails. | - | ||||||||||||
3783 | - | |||||||||||||
3784 | If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to | - | ||||||||||||
3785 | false; otherwise *\a{ok} is set to true. | - | ||||||||||||
3786 | - | |||||||||||||
3787 | \snippet code/src_corelib_tools_qbytearray.cpp 38 | - | ||||||||||||
3788 | - | |||||||||||||
3789 | \note The conversion of the number is performed in the default C locale, | - | ||||||||||||
3790 | irrespective of the user's locale. | - | ||||||||||||
3791 | - | |||||||||||||
3792 | \sa number() | - | ||||||||||||
3793 | */ | - | ||||||||||||
3794 | - | |||||||||||||
3795 | double QByteArray::toDouble(bool *ok) const | - | ||||||||||||
3796 | { | - | ||||||||||||
3797 | return QLocaleData::bytearrayToDouble(QByteArray nulled = nulTerminated().(); | - | ||||||||||||
3798 | bool nonNullOk = false; | - | ||||||||||||
3799 | int processed = 0; | - | ||||||||||||
3800 | double d = asciiToDouble(nulled.constData(), oknulled.length(), nonNullOk, processed); | - | ||||||||||||
3801 | if (ok)
| 0-184 | ||||||||||||
3802 | *ok = nonNullOk; executed 184 times by 3 tests: *ok = nonNullOk; Executed by:
| 184 | ||||||||||||
3803 | return d; executed 184 times by 3 tests: return d; Executed by:
| 184 | ||||||||||||
3804 | } | - | ||||||||||||
3805 | - | |||||||||||||
3806 | /*! | - | ||||||||||||
3807 | Returns the byte array converted to a \c float value. | - | ||||||||||||
3808 | - | |||||||||||||
3809 | Returns 0.0 if the conversion fails. | - | ||||||||||||
3810 | - | |||||||||||||
3811 | If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to | - | ||||||||||||
3812 | false; otherwise *\a{ok} is set to true. | - | ||||||||||||
3813 | - | |||||||||||||
3814 | \note The conversion of the number is performed in the default C locale, | - | ||||||||||||
3815 | irrespective of the user's locale. | - | ||||||||||||
3816 | - | |||||||||||||
3817 | \sa number() | - | ||||||||||||
3818 | */ | - | ||||||||||||
3819 | - | |||||||||||||
3820 | float QByteArray::toFloat(bool *ok) const | - | ||||||||||||
3821 | { | - | ||||||||||||
3822 | return QLocaleData::convertDoubleToFloat(toDouble(ok), ok); | - | ||||||||||||
3823 | } | - | ||||||||||||
3824 | - | |||||||||||||
3825 | /*! | - | ||||||||||||
3826 | Returns a copy of the byte array, encoded as Base64. | - | ||||||||||||
3827 | - | |||||||||||||
3828 | \snippet code/src_corelib_tools_qbytearray.cpp 39 | - | ||||||||||||
3829 | - | |||||||||||||
3830 | The algorithm used to encode Base64-encoded data is defined in \l{RFC 4648}. | - | ||||||||||||
3831 | - | |||||||||||||
3832 | \sa fromBase64() | - | ||||||||||||
3833 | */ | - | ||||||||||||
3834 | QByteArray QByteArray::toBase64() const | - | ||||||||||||
3835 | { | - | ||||||||||||
3836 | return toBase64(Base64Encoding); | - | ||||||||||||
3837 | } | - | ||||||||||||
3838 | - | |||||||||||||
3839 | /*! | - | ||||||||||||
3840 | \since 5.2 | - | ||||||||||||
3841 | \overload | - | ||||||||||||
3842 | - | |||||||||||||
3843 | Returns a copy of the byte array, encoded using the options \a options. | - | ||||||||||||
3844 | - | |||||||||||||
3845 | \snippet code/src_corelib_tools_qbytearray.cpp 39bis | - | ||||||||||||
3846 | - | |||||||||||||
3847 | The algorithm used to encode Base64-encoded data is defined in \l{RFC 4648}. | - | ||||||||||||
3848 | - | |||||||||||||
3849 | \sa fromBase64() | - | ||||||||||||
3850 | */ | - | ||||||||||||
3851 | QByteArray QByteArray::toBase64(Base64Options options) const | - | ||||||||||||
3852 | { | - | ||||||||||||
3853 | const char alphabet_base64[] = "ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef" | - | ||||||||||||
3854 | "ghijklmn" "opqrstuv" "wxyz0123" "456789+/"; | - | ||||||||||||
3855 | const char alphabet_base64url[] = "ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef" | - | ||||||||||||
3856 | "ghijklmn" "opqrstuv" "wxyz0123" "456789-_"; | - | ||||||||||||
3857 | const char *const alphabet = options & Base64UrlEncoding ? alphabet_base64url : alphabet_base64; | - | ||||||||||||
3858 | const char padchar = '='; | - | ||||||||||||
3859 | int padlen = 0; | - | ||||||||||||
3860 | - | |||||||||||||
3861 | QByteArray tmp((d->size + 2) / 3 * 4, Qt::Uninitialized); | - | ||||||||||||
3862 | - | |||||||||||||
3863 | int i = 0; | - | ||||||||||||
3864 | char *out = tmp.data(); | - | ||||||||||||
3865 | while (i < d->size) { | - | ||||||||||||
3866 | // encode 3 bytes at a time | - | ||||||||||||
3867 | int chunk = 0; | - | ||||||||||||
3868 | chunk |= int(uchar(d->data()[i++])) << 16; | - | ||||||||||||
3869 | if (i == d->size) { | - | ||||||||||||
3870 | padlen = 2; | - | ||||||||||||
3871 | } else { | - | ||||||||||||
3872 | chunk |= int(uchar(d->data()[i++])) << 8; | - | ||||||||||||
3873 | if (i == d->size) | - | ||||||||||||
3874 | padlen = 1; | - | ||||||||||||
3875 | else | - | ||||||||||||
3876 | chunk |= int(uchar(data()[i++])); | - | ||||||||||||
3877 | } | - | ||||||||||||
3878 | - | |||||||||||||
3879 | int j = (chunk & 0x00fc0000) >> 18; | - | ||||||||||||
3880 | int k = (chunk & 0x0003f000) >> 12; | - | ||||||||||||
3881 | int l = (chunk & 0x00000fc0) >> 6; | - | ||||||||||||
3882 | int m = (chunk & 0x0000003f); | - | ||||||||||||
3883 | *out++ = alphabet[j]; | - | ||||||||||||
3884 | *out++ = alphabet[k]; | - | ||||||||||||
3885 | - | |||||||||||||
3886 | if (padlen > 1) { | - | ||||||||||||
3887 | if ((options & OmitTrailingEquals) == 0) | - | ||||||||||||
3888 | *out++ = padchar; | - | ||||||||||||
3889 | } else { | - | ||||||||||||
3890 | *out++ = alphabet[l]; | - | ||||||||||||
3891 | } | - | ||||||||||||
3892 | if (padlen > 0) { | - | ||||||||||||
3893 | if ((options & OmitTrailingEquals) == 0) | - | ||||||||||||
3894 | *out++ = padchar; | - | ||||||||||||
3895 | } else { | - | ||||||||||||
3896 | *out++ = alphabet[m]; | - | ||||||||||||
3897 | } | - | ||||||||||||
3898 | } | - | ||||||||||||
3899 | Q_ASSERT((options & OmitTrailingEquals) || (out == tmp.size() + tmp.data())); | - | ||||||||||||
3900 | if (options & OmitTrailingEquals) | - | ||||||||||||
3901 | tmp.truncate(out - tmp.data()); | - | ||||||||||||
3902 | return tmp; | - | ||||||||||||
3903 | } | - | ||||||||||||
3904 | - | |||||||||||||
3905 | /*! | - | ||||||||||||
3906 | \fn QByteArray &QByteArray::setNum(int n, int base) | - | ||||||||||||
3907 | - | |||||||||||||
3908 | Sets the byte array to the printed value of \a n in base \a base (10 | - | ||||||||||||
3909 | by default) and returns a reference to the byte array. The \a base can | - | ||||||||||||
3910 | be any value between 2 and 36. For bases other than 10, n is treated | - | ||||||||||||
3911 | as an unsigned integer. | - | ||||||||||||
3912 | - | |||||||||||||
3913 | Example: | - | ||||||||||||
3914 | \snippet code/src_corelib_tools_qbytearray.cpp 40 | - | ||||||||||||
3915 | - | |||||||||||||
3916 | \note The format of the number is not localized; the default C locale | - | ||||||||||||
3917 | is used irrespective of the user's locale. | - | ||||||||||||
3918 | - | |||||||||||||
3919 | \sa number(), toInt() | - | ||||||||||||
3920 | */ | - | ||||||||||||
3921 | - | |||||||||||||
3922 | /*! | - | ||||||||||||
3923 | \fn QByteArray &QByteArray::setNum(uint n, int base) | - | ||||||||||||
3924 | \overload | - | ||||||||||||
3925 | - | |||||||||||||
3926 | \sa toUInt() | - | ||||||||||||
3927 | */ | - | ||||||||||||
3928 | - | |||||||||||||
3929 | /*! | - | ||||||||||||
3930 | \fn QByteArray &QByteArray::setNum(short n, int base) | - | ||||||||||||
3931 | \overload | - | ||||||||||||
3932 | - | |||||||||||||
3933 | \sa toShort() | - | ||||||||||||
3934 | */ | - | ||||||||||||
3935 | - | |||||||||||||
3936 | /*! | - | ||||||||||||
3937 | \fn QByteArray &QByteArray::setNum(ushort n, int base) | - | ||||||||||||
3938 | \overload | - | ||||||||||||
3939 | - | |||||||||||||
3940 | \sa toUShort() | - | ||||||||||||
3941 | */ | - | ||||||||||||
3942 | - | |||||||||||||
3943 | static char *qulltoa2(char *p, qulonglong n, int base) | - | ||||||||||||
3944 | { | - | ||||||||||||
3945 | #if defined(QT_CHECK_RANGE) | - | ||||||||||||
3946 | if (base < 2 || base > 36) { | - | ||||||||||||
3947 | qWarning("QByteArray::setNum: Invalid base %d", base); | - | ||||||||||||
3948 | base = 10; | - | ||||||||||||
3949 | } | - | ||||||||||||
3950 | #endif | - | ||||||||||||
3951 | const char b = 'a' - 10; | - | ||||||||||||
3952 | do { | - | ||||||||||||
3953 | const int c = n % base; | - | ||||||||||||
3954 | n /= base; | - | ||||||||||||
3955 | *--p = c + (c < 10 ? '0' : b); | - | ||||||||||||
3956 | } while (n); | - | ||||||||||||
3957 | - | |||||||||||||
3958 | return p; | - | ||||||||||||
3959 | } | - | ||||||||||||
3960 | - | |||||||||||||
3961 | /*! | - | ||||||||||||
3962 | \overload | - | ||||||||||||
3963 | - | |||||||||||||
3964 | \sa toLongLong() | - | ||||||||||||
3965 | */ | - | ||||||||||||
3966 | QByteArray &QByteArray::setNum(qlonglong n, int base) | - | ||||||||||||
3967 | { | - | ||||||||||||
3968 | const int buffsize = 66; // big enough for MAX_ULLONG in base 2 | - | ||||||||||||
3969 | char buff[buffsize]; | - | ||||||||||||
3970 | char *p; | - | ||||||||||||
3971 | - | |||||||||||||
3972 | if (n < 0 && base == 10) { | - | ||||||||||||
3973 | p = qulltoa2(buff + buffsize, qulonglong(-(1 + n)) + 1, base); | - | ||||||||||||
3974 | *--p = '-'; | - | ||||||||||||
3975 | } else { | - | ||||||||||||
3976 | p = qulltoa2(buff + buffsize, qulonglong(n), base); | - | ||||||||||||
3977 | } | - | ||||||||||||
3978 | - | |||||||||||||
3979 | clear(); | - | ||||||||||||
3980 | append(p, buffsize - (p - buff)); | - | ||||||||||||
3981 | return *this; | - | ||||||||||||
3982 | } | - | ||||||||||||
3983 | - | |||||||||||||
3984 | /*! | - | ||||||||||||
3985 | \overload | - | ||||||||||||
3986 | - | |||||||||||||
3987 | \sa toULongLong() | - | ||||||||||||
3988 | */ | - | ||||||||||||
3989 | - | |||||||||||||
3990 | QByteArray &QByteArray::setNum(qulonglong n, int base) | - | ||||||||||||
3991 | { | - | ||||||||||||
3992 | const int buffsize = 66; // big enough for MAX_ULLONG in base 2 | - | ||||||||||||
3993 | char buff[buffsize]; | - | ||||||||||||
3994 | char *p = qulltoa2(buff + buffsize, n, base); | - | ||||||||||||
3995 | - | |||||||||||||
3996 | clear(); | - | ||||||||||||
3997 | append(p, buffsize - (p - buff)); | - | ||||||||||||
3998 | return *this; | - | ||||||||||||
3999 | } | - | ||||||||||||
4000 | - | |||||||||||||
4001 | /*! | - | ||||||||||||
4002 | \overload | - | ||||||||||||
4003 | - | |||||||||||||
4004 | Sets the byte array to the printed value of \a n, formatted in format | - | ||||||||||||
4005 | \a f with precision \a prec, and returns a reference to the | - | ||||||||||||
4006 | byte array. | - | ||||||||||||
4007 | - | |||||||||||||
4008 | The format \a f can be any of the following: | - | ||||||||||||
4009 | - | |||||||||||||
4010 | \table | - | ||||||||||||
4011 | \header \li Format \li Meaning | - | ||||||||||||
4012 | \row \li \c e \li format as [-]9.9e[+|-]999 | - | ||||||||||||
4013 | \row \li \c E \li format as [-]9.9E[+|-]999 | - | ||||||||||||
4014 | \row \li \c f \li format as [-]9.9 | - | ||||||||||||
4015 | \row \li \c g \li use \c e or \c f format, whichever is the most concise | - | ||||||||||||
4016 | \row \li \c G \li use \c E or \c f format, whichever is the most concise | - | ||||||||||||
4017 | \endtable | - | ||||||||||||
4018 | - | |||||||||||||
4019 | With 'e', 'E', and 'f', \a prec is the number of digits after the | - | ||||||||||||
4020 | decimal point. With 'g' and 'G', \a prec is the maximum number of | - | ||||||||||||
4021 | significant digits (trailing zeroes are omitted). | - | ||||||||||||
4022 | - | |||||||||||||
4023 | \note The format of the number is not localized; the default C locale | - | ||||||||||||
4024 | is used irrespective of the user's locale. | - | ||||||||||||
4025 | - | |||||||||||||
4026 | \sa toDouble() | - | ||||||||||||
4027 | */ | - | ||||||||||||
4028 | - | |||||||||||||
4029 | QByteArray &QByteArray::setNum(double n, char f, int prec) | - | ||||||||||||
4030 | { | - | ||||||||||||
4031 | QLocaleData::DoubleForm form = QLocaleData::DFDecimal; | - | ||||||||||||
4032 | uint flags = 0QLocaleData::ZeroPadExponent; | - | ||||||||||||
4033 | - | |||||||||||||
4034 | if (qIsUpper(f))
| 0-265 | ||||||||||||
4035 | flags |= QLocaleData::CapitalEorX; never executed: flags |= QLocaleData::CapitalEorX; | 0 | ||||||||||||
4036 | f = qToLower(f); | - | ||||||||||||
4037 | - | |||||||||||||
4038 | switch (f) { | - | ||||||||||||
4039 | case 'f': never executed: case 'f': | 0 | ||||||||||||
4040 | form = QLocaleData::DFDecimal; | - | ||||||||||||
4041 | break; never executed: break; | 0 | ||||||||||||
4042 | case 'e': never executed: case 'e': | 0 | ||||||||||||
4043 | form = QLocaleData::DFExponent; | - | ||||||||||||
4044 | break; never executed: break; | 0 | ||||||||||||
4045 | case 'g': executed 265 times by 4 tests: case 'g': Executed by:
| 265 | ||||||||||||
4046 | form = QLocaleData::DFSignificantDigits; | - | ||||||||||||
4047 | break; executed 265 times by 4 tests: break; Executed by:
| 265 | ||||||||||||
4048 | default: never executed: default: | 0 | ||||||||||||
4049 | #if defined(QT_CHECK_RANGE) | - | ||||||||||||
4050 | qWarning("QByteArray::setNum: Invalid format char '%c'", f); | - | ||||||||||||
4051 | #endif | - | ||||||||||||
4052 | break; never executed: break; | 0 | ||||||||||||
4053 | } | - | ||||||||||||
4054 | - | |||||||||||||
4055 | *this = QLocaleData::c()->doubleToString(n, prec, form, -1, flags).toLatin1(); | - | ||||||||||||
4056 | return *this; executed 265 times by 4 tests: return *this; Executed by:
| 265 | ||||||||||||
4057 | } | - | ||||||||||||
4058 | - | |||||||||||||
4059 | /*! | - | ||||||||||||
4060 | \fn QByteArray &QByteArray::setNum(float n, char f, int prec) | - | ||||||||||||
4061 | \overload | - | ||||||||||||
4062 | - | |||||||||||||
4063 | Sets the byte array to the printed value of \a n, formatted in format | - | ||||||||||||
4064 | \a f with precision \a prec, and returns a reference to the | - | ||||||||||||
4065 | byte array. | - | ||||||||||||
4066 | - | |||||||||||||
4067 | \note The format of the number is not localized; the default C locale | - | ||||||||||||
4068 | is used irrespective of the user's locale. | - | ||||||||||||
4069 | - | |||||||||||||
4070 | \sa toFloat() | - | ||||||||||||
4071 | */ | - | ||||||||||||
4072 | - | |||||||||||||
4073 | /*! | - | ||||||||||||
4074 | Returns a byte array containing the string equivalent of the | - | ||||||||||||
4075 | number \a n to base \a base (10 by default). The \a base can be | - | ||||||||||||
4076 | any value between 2 and 36. | - | ||||||||||||
4077 | - | |||||||||||||
4078 | Example: | - | ||||||||||||
4079 | \snippet code/src_corelib_tools_qbytearray.cpp 41 | - | ||||||||||||
4080 | - | |||||||||||||
4081 | \note The format of the number is not localized; the default C locale | - | ||||||||||||
4082 | is used irrespective of the user's locale. | - | ||||||||||||
4083 | - | |||||||||||||
4084 | \sa setNum(), toInt() | - | ||||||||||||
4085 | */ | - | ||||||||||||
4086 | QByteArray QByteArray::number(int n, int base) | - | ||||||||||||
4087 | { | - | ||||||||||||
4088 | QByteArray s; | - | ||||||||||||
4089 | s.setNum(n, base); | - | ||||||||||||
4090 | return s; | - | ||||||||||||
4091 | } | - | ||||||||||||
4092 | - | |||||||||||||
4093 | /*! | - | ||||||||||||
4094 | \overload | - | ||||||||||||
4095 | - | |||||||||||||
4096 | \sa toUInt() | - | ||||||||||||
4097 | */ | - | ||||||||||||
4098 | QByteArray QByteArray::number(uint n, int base) | - | ||||||||||||
4099 | { | - | ||||||||||||
4100 | QByteArray s; | - | ||||||||||||
4101 | s.setNum(n, base); | - | ||||||||||||
4102 | return s; | - | ||||||||||||
4103 | } | - | ||||||||||||
4104 | - | |||||||||||||
4105 | /*! | - | ||||||||||||
4106 | \overload | - | ||||||||||||
4107 | - | |||||||||||||
4108 | \sa toLongLong() | - | ||||||||||||
4109 | */ | - | ||||||||||||
4110 | QByteArray QByteArray::number(qlonglong n, int base) | - | ||||||||||||
4111 | { | - | ||||||||||||
4112 | QByteArray s; | - | ||||||||||||
4113 | s.setNum(n, base); | - | ||||||||||||
4114 | return s; | - | ||||||||||||
4115 | } | - | ||||||||||||
4116 | - | |||||||||||||
4117 | /*! | - | ||||||||||||
4118 | \overload | - | ||||||||||||
4119 | - | |||||||||||||
4120 | \sa toULongLong() | - | ||||||||||||
4121 | */ | - | ||||||||||||
4122 | QByteArray QByteArray::number(qulonglong n, int base) | - | ||||||||||||
4123 | { | - | ||||||||||||
4124 | QByteArray s; | - | ||||||||||||
4125 | s.setNum(n, base); | - | ||||||||||||
4126 | return s; | - | ||||||||||||
4127 | } | - | ||||||||||||
4128 | - | |||||||||||||
4129 | /*! | - | ||||||||||||
4130 | \overload | - | ||||||||||||
4131 | - | |||||||||||||
4132 | Returns a byte array that contains the printed value of \a n, | - | ||||||||||||
4133 | formatted in format \a f with precision \a prec. | - | ||||||||||||
4134 | - | |||||||||||||
4135 | Argument \a n is formatted according to the \a f format specified, | - | ||||||||||||
4136 | which is \c g by default, and can be any of the following: | - | ||||||||||||
4137 | - | |||||||||||||
4138 | \table | - | ||||||||||||
4139 | \header \li Format \li Meaning | - | ||||||||||||
4140 | \row \li \c e \li format as [-]9.9e[+|-]999 | - | ||||||||||||
4141 | \row \li \c E \li format as [-]9.9E[+|-]999 | - | ||||||||||||
4142 | \row \li \c f \li format as [-]9.9 | - | ||||||||||||
4143 | \row \li \c g \li use \c e or \c f format, whichever is the most concise | - | ||||||||||||
4144 | \row \li \c G \li use \c E or \c f format, whichever is the most concise | - | ||||||||||||
4145 | \endtable | - | ||||||||||||
4146 | - | |||||||||||||
4147 | With 'e', 'E', and 'f', \a prec is the number of digits after the | - | ||||||||||||
4148 | decimal point. With 'g' and 'G', \a prec is the maximum number of | - | ||||||||||||
4149 | significant digits (trailing zeroes are omitted). | - | ||||||||||||
4150 | - | |||||||||||||
4151 | \snippet code/src_corelib_tools_qbytearray.cpp 42 | - | ||||||||||||
4152 | - | |||||||||||||
4153 | \note The format of the number is not localized; the default C locale | - | ||||||||||||
4154 | is used irrespective of the user's locale. | - | ||||||||||||
4155 | - | |||||||||||||
4156 | \sa toDouble() | - | ||||||||||||
4157 | */ | - | ||||||||||||
4158 | QByteArray QByteArray::number(double n, char f, int prec) | - | ||||||||||||
4159 | { | - | ||||||||||||
4160 | QByteArray s; | - | ||||||||||||
4161 | s.setNum(n, f, prec); | - | ||||||||||||
4162 | return s; | - | ||||||||||||
4163 | } | - | ||||||||||||
4164 | - | |||||||||||||
4165 | /*! | - | ||||||||||||
4166 | Constructs a QByteArray that uses the first \a size bytes of the | - | ||||||||||||
4167 | \a data array. The bytes are \e not copied. The QByteArray will | - | ||||||||||||
4168 | contain the \a data pointer. The caller guarantees that \a data | - | ||||||||||||
4169 | will not be deleted or modified as long as this QByteArray and any | - | ||||||||||||
4170 | copies of it exist that have not been modified. In other words, | - | ||||||||||||
4171 | because QByteArray is an \l{implicitly shared} class and the | - | ||||||||||||
4172 | instance returned by this function contains the \a data pointer, | - | ||||||||||||
4173 | the caller must not delete \a data or modify it directly as long | - | ||||||||||||
4174 | as the returned QByteArray and any copies exist. However, | - | ||||||||||||
4175 | QByteArray does not take ownership of \a data, so the QByteArray | - | ||||||||||||
4176 | destructor will never delete the raw \a data, even when the | - | ||||||||||||
4177 | last QByteArray referring to \a data is destroyed. | - | ||||||||||||
4178 | - | |||||||||||||
4179 | A subsequent attempt to modify the contents of the returned | - | ||||||||||||
4180 | QByteArray or any copy made from it will cause it to create a deep | - | ||||||||||||
4181 | copy of the \a data array before doing the modification. This | - | ||||||||||||
4182 | ensures that the raw \a data array itself will never be modified | - | ||||||||||||
4183 | by QByteArray. | - | ||||||||||||
4184 | - | |||||||||||||
4185 | Here is an example of how to read data using a QDataStream on raw | - | ||||||||||||
4186 | data in memory without copying the raw data into a QByteArray: | - | ||||||||||||
4187 | - | |||||||||||||
4188 | \snippet code/src_corelib_tools_qbytearray.cpp 43 | - | ||||||||||||
4189 | - | |||||||||||||
4190 | \warning A byte array created with fromRawData() is \e not | - | ||||||||||||
4191 | null-terminated, unless the raw data contains a 0 character at | - | ||||||||||||
4192 | position \a size. While that does not matter for QDataStream or | - | ||||||||||||
4193 | functions like indexOf(), passing the byte array to a function | - | ||||||||||||
4194 | accepting a \c{const char *} expected to be '\\0'-terminated will | - | ||||||||||||
4195 | fail. | - | ||||||||||||
4196 | - | |||||||||||||
4197 | \sa setRawData(), data(), constData() | - | ||||||||||||
4198 | */ | - | ||||||||||||
4199 | - | |||||||||||||
4200 | QByteArray QByteArray::fromRawData(const char *data, int size) | - | ||||||||||||
4201 | { | - | ||||||||||||
4202 | Data *x; | - | ||||||||||||
4203 | if (!data) { | - | ||||||||||||
4204 | x = Data::sharedNull(); | - | ||||||||||||
4205 | } else if (!size) { | - | ||||||||||||
4206 | x = Data::allocate(0); | - | ||||||||||||
4207 | } else { | - | ||||||||||||
4208 | x = Data::fromRawData(data, size); | - | ||||||||||||
4209 | Q_CHECK_PTR(x); | - | ||||||||||||
4210 | } | - | ||||||||||||
4211 | QByteArrayDataPtr dataPtr = { x }; | - | ||||||||||||
4212 | return QByteArray(dataPtr); | - | ||||||||||||
4213 | } | - | ||||||||||||
4214 | - | |||||||||||||
4215 | /*! | - | ||||||||||||
4216 | \since 4.7 | - | ||||||||||||
4217 | - | |||||||||||||
4218 | Resets the QByteArray to use the first \a size bytes of the | - | ||||||||||||
4219 | \a data array. The bytes are \e not copied. The QByteArray will | - | ||||||||||||
4220 | contain the \a data pointer. The caller guarantees that \a data | - | ||||||||||||
4221 | will not be deleted or modified as long as this QByteArray and any | - | ||||||||||||
4222 | copies of it exist that have not been modified. | - | ||||||||||||
4223 | - | |||||||||||||
4224 | This function can be used instead of fromRawData() to re-use | - | ||||||||||||
4225 | existing QByteArray objects to save memory re-allocations. | - | ||||||||||||
4226 | - | |||||||||||||
4227 | \sa fromRawData(), data(), constData() | - | ||||||||||||
4228 | */ | - | ||||||||||||
4229 | QByteArray &QByteArray::setRawData(const char *data, uint size) | - | ||||||||||||
4230 | { | - | ||||||||||||
4231 | if (d->ref.isShared() || d->alloc) { | - | ||||||||||||
4232 | *this = fromRawData(data, size); | - | ||||||||||||
4233 | } else { | - | ||||||||||||
4234 | if (data) { | - | ||||||||||||
4235 | d->size = size; | - | ||||||||||||
4236 | d->offset = data - reinterpret_cast<char *>(d); | - | ||||||||||||
4237 | } else { | - | ||||||||||||
4238 | d->offset = sizeof(QByteArrayData); | - | ||||||||||||
4239 | d->size = 0; | - | ||||||||||||
4240 | *d->data() = 0; | - | ||||||||||||
4241 | } | - | ||||||||||||
4242 | } | - | ||||||||||||
4243 | return *this; | - | ||||||||||||
4244 | } | - | ||||||||||||
4245 | - | |||||||||||||
4246 | /*! | - | ||||||||||||
4247 | Returns a decoded copy of the Base64 array \a base64. Input is not checked | - | ||||||||||||
4248 | for validity; invalid characters in the input are skipped, enabling the | - | ||||||||||||
4249 | decoding process to continue with subsequent characters. | - | ||||||||||||
4250 | - | |||||||||||||
4251 | For example: | - | ||||||||||||
4252 | - | |||||||||||||
4253 | \snippet code/src_corelib_tools_qbytearray.cpp 44 | - | ||||||||||||
4254 | - | |||||||||||||
4255 | The algorithm used to decode Base64-encoded data is defined in \l{RFC 4648}. | - | ||||||||||||
4256 | - | |||||||||||||
4257 | \sa toBase64() | - | ||||||||||||
4258 | */ | - | ||||||||||||
4259 | QByteArray QByteArray::fromBase64(const QByteArray &base64) | - | ||||||||||||
4260 | { | - | ||||||||||||
4261 | return fromBase64(base64, Base64Encoding); | - | ||||||||||||
4262 | } | - | ||||||||||||
4263 | - | |||||||||||||
4264 | /*! | - | ||||||||||||
4265 | \since 5.2 | - | ||||||||||||
4266 | \overload | - | ||||||||||||
4267 | - | |||||||||||||
4268 | Returns a decoded copy of the Base64 array \a base64, using the alphabet | - | ||||||||||||
4269 | defined by \a options. Input is not checked for validity; invalid | - | ||||||||||||
4270 | characters in the input are skipped, enabling the decoding process to | - | ||||||||||||
4271 | continue with subsequent characters. | - | ||||||||||||
4272 | - | |||||||||||||
4273 | For example: | - | ||||||||||||
4274 | - | |||||||||||||
4275 | \snippet code/src_corelib_tools_qbytearray.cpp 44bis | - | ||||||||||||
4276 | - | |||||||||||||
4277 | The algorithm used to decode Base64-encoded data is defined in \l{RFC 4648}. | - | ||||||||||||
4278 | - | |||||||||||||
4279 | \sa toBase64() | - | ||||||||||||
4280 | */ | - | ||||||||||||
4281 | QByteArray QByteArray::fromBase64(const QByteArray &base64, Base64Options options) | - | ||||||||||||
4282 | { | - | ||||||||||||
4283 | unsigned int buf = 0; | - | ||||||||||||
4284 | int nbits = 0; | - | ||||||||||||
4285 | QByteArray tmp((base64.size() * 3) / 4, Qt::Uninitialized); | - | ||||||||||||
4286 | - | |||||||||||||
4287 | int offset = 0; | - | ||||||||||||
4288 | for (int i = 0; i < base64.size(); ++i) { | - | ||||||||||||
4289 | int ch = base64.at(i); | - | ||||||||||||
4290 | int d; | - | ||||||||||||
4291 | - | |||||||||||||
4292 | if (ch >= 'A' && ch <= 'Z') | - | ||||||||||||
4293 | d = ch - 'A'; | - | ||||||||||||
4294 | else if (ch >= 'a' && ch <= 'z') | - | ||||||||||||
4295 | d = ch - 'a' + 26; | - | ||||||||||||
4296 | else if (ch >= '0' && ch <= '9') | - | ||||||||||||
4297 | d = ch - '0' + 52; | - | ||||||||||||
4298 | else if (ch == '+' && (options & Base64UrlEncoding) == 0) | - | ||||||||||||
4299 | d = 62; | - | ||||||||||||
4300 | else if (ch == '-' && (options & Base64UrlEncoding) != 0) | - | ||||||||||||
4301 | d = 62; | - | ||||||||||||
4302 | else if (ch == '/' && (options & Base64UrlEncoding) == 0) | - | ||||||||||||
4303 | d = 63; | - | ||||||||||||
4304 | else if (ch == '_' && (options & Base64UrlEncoding) != 0) | - | ||||||||||||
4305 | d = 63; | - | ||||||||||||
4306 | else | - | ||||||||||||
4307 | d = -1; | - | ||||||||||||
4308 | - | |||||||||||||
4309 | if (d != -1) { | - | ||||||||||||
4310 | buf = (buf << 6) | d; | - | ||||||||||||
4311 | nbits += 6; | - | ||||||||||||
4312 | if (nbits >= 8) { | - | ||||||||||||
4313 | nbits -= 8; | - | ||||||||||||
4314 | tmp[offset++] = buf >> nbits; | - | ||||||||||||
4315 | buf &= (1 << nbits) - 1; | - | ||||||||||||
4316 | } | - | ||||||||||||
4317 | } | - | ||||||||||||
4318 | } | - | ||||||||||||
4319 | - | |||||||||||||
4320 | tmp.truncate(offset); | - | ||||||||||||
4321 | return tmp; | - | ||||||||||||
4322 | } | - | ||||||||||||
4323 | - | |||||||||||||
4324 | /*! | - | ||||||||||||
4325 | Returns a decoded copy of the hex encoded array \a hexEncoded. Input is not checked | - | ||||||||||||
4326 | for validity; invalid characters in the input are skipped, enabling the | - | ||||||||||||
4327 | decoding process to continue with subsequent characters. | - | ||||||||||||
4328 | - | |||||||||||||
4329 | For example: | - | ||||||||||||
4330 | - | |||||||||||||
4331 | \snippet code/src_corelib_tools_qbytearray.cpp 45 | - | ||||||||||||
4332 | - | |||||||||||||
4333 | \sa toHex() | - | ||||||||||||
4334 | */ | - | ||||||||||||
4335 | QByteArray QByteArray::fromHex(const QByteArray &hexEncoded) | - | ||||||||||||
4336 | { | - | ||||||||||||
4337 | QByteArray res((hexEncoded.size() + 1)/ 2, Qt::Uninitialized); | - | ||||||||||||
4338 | uchar *result = (uchar *)res.data() + res.size(); | - | ||||||||||||
4339 | - | |||||||||||||
4340 | bool odd_digit = true; | - | ||||||||||||
4341 | for (int i = hexEncoded.size() - 1; i >= 0; --i) { | - | ||||||||||||
4342 | uchar ch = uchar(hexEncoded.at(i)); | - | ||||||||||||
4343 | int tmp = QtMiscUtils::fromHex(ch); | - | ||||||||||||
4344 | if (tmp == -1) | - | ||||||||||||
4345 | continue; | - | ||||||||||||
4346 | if (odd_digit) { | - | ||||||||||||
4347 | --result; | - | ||||||||||||
4348 | *result = tmp; | - | ||||||||||||
4349 | odd_digit = false; | - | ||||||||||||
4350 | } else { | - | ||||||||||||
4351 | *result |= tmp << 4; | - | ||||||||||||
4352 | odd_digit = true; | - | ||||||||||||
4353 | } | - | ||||||||||||
4354 | } | - | ||||||||||||
4355 | - | |||||||||||||
4356 | res.remove(0, result - (const uchar *)res.constData()); | - | ||||||||||||
4357 | return res; | - | ||||||||||||
4358 | } | - | ||||||||||||
4359 | - | |||||||||||||
4360 | /*! | - | ||||||||||||
4361 | Returns a hex encoded copy of the byte array. The hex encoding uses the numbers 0-9 and | - | ||||||||||||
4362 | the letters a-f. | - | ||||||||||||
4363 | - | |||||||||||||
4364 | \sa fromHex() | - | ||||||||||||
4365 | */ | - | ||||||||||||
4366 | QByteArray QByteArray::toHex() const | - | ||||||||||||
4367 | { | - | ||||||||||||
4368 | QByteArray hex(d->size * 2, Qt::Uninitialized); | - | ||||||||||||
4369 | char *hexData = hex.data(); | - | ||||||||||||
4370 | const uchar *data = (const uchar *)d->data(); | - | ||||||||||||
4371 | for (int i = 0; i < d->size; ++i) { | - | ||||||||||||
4372 | hexData[i*2] = QtMiscUtils::toHexLower(data[i] >> 4); | - | ||||||||||||
4373 | hexData[i*2+1] = QtMiscUtils::toHexLower(data[i] & 0xf); | - | ||||||||||||
4374 | } | - | ||||||||||||
4375 | return hex; | - | ||||||||||||
4376 | } | - | ||||||||||||
4377 | - | |||||||||||||
4378 | static void q_fromPercentEncoding(QByteArray *ba, char percent) | - | ||||||||||||
4379 | { | - | ||||||||||||
4380 | if (ba->isEmpty()) | - | ||||||||||||
4381 | return; | - | ||||||||||||
4382 | - | |||||||||||||
4383 | char *data = ba->data(); | - | ||||||||||||
4384 | const char *inputPtr = data; | - | ||||||||||||
4385 | - | |||||||||||||
4386 | int i = 0; | - | ||||||||||||
4387 | int len = ba->count(); | - | ||||||||||||
4388 | int outlen = 0; | - | ||||||||||||
4389 | int a, b; | - | ||||||||||||
4390 | char c; | - | ||||||||||||
4391 | while (i < len) { | - | ||||||||||||
4392 | c = inputPtr[i]; | - | ||||||||||||
4393 | if (c == percent && i + 2 < len) { | - | ||||||||||||
4394 | a = inputPtr[++i]; | - | ||||||||||||
4395 | b = inputPtr[++i]; | - | ||||||||||||
4396 | - | |||||||||||||
4397 | if (a >= '0' && a <= '9') a -= '0'; | - | ||||||||||||
4398 | else if (a >= 'a' && a <= 'f') a = a - 'a' + 10; | - | ||||||||||||
4399 | else if (a >= 'A' && a <= 'F') a = a - 'A' + 10; | - | ||||||||||||
4400 | - | |||||||||||||
4401 | if (b >= '0' && b <= '9') b -= '0'; | - | ||||||||||||
4402 | else if (b >= 'a' && b <= 'f') b = b - 'a' + 10; | - | ||||||||||||
4403 | else if (b >= 'A' && b <= 'F') b = b - 'A' + 10; | - | ||||||||||||
4404 | - | |||||||||||||
4405 | *data++ = (char)((a << 4) | b); | - | ||||||||||||
4406 | } else { | - | ||||||||||||
4407 | *data++ = c; | - | ||||||||||||
4408 | } | - | ||||||||||||
4409 | - | |||||||||||||
4410 | ++i; | - | ||||||||||||
4411 | ++outlen; | - | ||||||||||||
4412 | } | - | ||||||||||||
4413 | - | |||||||||||||
4414 | if (outlen != len) | - | ||||||||||||
4415 | ba->truncate(outlen); | - | ||||||||||||
4416 | } | - | ||||||||||||
4417 | - | |||||||||||||
4418 | void q_fromPercentEncoding(QByteArray *ba) | - | ||||||||||||
4419 | { | - | ||||||||||||
4420 | q_fromPercentEncoding(ba, '%'); | - | ||||||||||||
4421 | } | - | ||||||||||||
4422 | - | |||||||||||||
4423 | /*! | - | ||||||||||||
4424 | \since 4.4 | - | ||||||||||||
4425 | - | |||||||||||||
4426 | Returns a decoded copy of the URI/URL-style percent-encoded \a input. | - | ||||||||||||
4427 | The \a percent parameter allows you to replace the '%' character for | - | ||||||||||||
4428 | another (for instance, '_' or '='). | - | ||||||||||||
4429 | - | |||||||||||||
4430 | For example: | - | ||||||||||||
4431 | \code | - | ||||||||||||
4432 | QByteArray text = QByteArray::fromPercentEncoding("Qt%20is%20great%33"); | - | ||||||||||||
4433 | text.data(); // returns "Qt is great!" | - | ||||||||||||
4434 | \endcode | - | ||||||||||||
4435 | - | |||||||||||||
4436 | \sa toPercentEncoding(), QUrl::fromPercentEncoding() | - | ||||||||||||
4437 | */ | - | ||||||||||||
4438 | QByteArray QByteArray::fromPercentEncoding(const QByteArray &input, char percent) | - | ||||||||||||
4439 | { | - | ||||||||||||
4440 | if (input.isNull()) | - | ||||||||||||
4441 | return QByteArray(); // preserve null | - | ||||||||||||
4442 | if (input.isEmpty()) | - | ||||||||||||
4443 | return QByteArray(input.data(), 0); | - | ||||||||||||
4444 | - | |||||||||||||
4445 | QByteArray tmp = input; | - | ||||||||||||
4446 | q_fromPercentEncoding(&tmp, percent); | - | ||||||||||||
4447 | return tmp; | - | ||||||||||||
4448 | } | - | ||||||||||||
4449 | - | |||||||||||||
4450 | /*! \fn QByteArray QByteArray::fromStdString(const std::string &str) | - | ||||||||||||
4451 | \since 5.4 | - | ||||||||||||
4452 | - | |||||||||||||
4453 | Returns a copy of the \a str string as a QByteArray. | - | ||||||||||||
4454 | - | |||||||||||||
4455 | \sa toStdString(), QString::fromStdString() | - | ||||||||||||
4456 | */ | - | ||||||||||||
4457 | - | |||||||||||||
4458 | /*! | - | ||||||||||||
4459 | \fn std::string QByteArray::toStdString() const | - | ||||||||||||
4460 | \since 5.4 | - | ||||||||||||
4461 | - | |||||||||||||
4462 | Returns a std::string object with the data contained in this | - | ||||||||||||
4463 | QByteArray. | - | ||||||||||||
4464 | - | |||||||||||||
4465 | This operator is mostly useful to pass a QByteArray to a function | - | ||||||||||||
4466 | that accepts a std::string object. | - | ||||||||||||
4467 | - | |||||||||||||
4468 | \sa fromStdString(), QString::toStdString() | - | ||||||||||||
4469 | */ | - | ||||||||||||
4470 | - | |||||||||||||
4471 | /*! \fn QByteArray QByteArray::fromCFData(CFDataRef data) | - | ||||||||||||
4472 | \since 5.3 | - | ||||||||||||
4473 | - | |||||||||||||
4474 | Constructs a new QByteArray containing a copy of the CFData \a data. | - | ||||||||||||
4475 | - | |||||||||||||
4476 | \sa fromRawCFData(), fromRawData(), toRawCFData(), toCFData() | - | ||||||||||||
4477 | */ | - | ||||||||||||
4478 | - | |||||||||||||
4479 | /*! \fn QByteArray QByteArray::fromRawCFData(CFDataRef data) | - | ||||||||||||
4480 | \since 5.3 | - | ||||||||||||
4481 | - | |||||||||||||
4482 | Constructs a QByteArray that uses the bytes of the CFData \a data. | - | ||||||||||||
4483 | - | |||||||||||||
4484 | The \a data's bytes are not copied. | - | ||||||||||||
4485 | - | |||||||||||||
4486 | The caller guarantees that the CFData will not be deleted | - | ||||||||||||
4487 | or modified as long as this QByteArray object exists. | - | ||||||||||||
4488 | - | |||||||||||||
4489 | \sa fromCFData(), fromRawData(), toRawCFData(), toCFData() | - | ||||||||||||
4490 | */ | - | ||||||||||||
4491 | - | |||||||||||||
4492 | /*! \fn CFDataRef QByteArray::toCFData() const | - | ||||||||||||
4493 | \since 5.3 | - | ||||||||||||
4494 | - | |||||||||||||
4495 | Creates a CFData from a QByteArray. The caller owns the CFData object | - | ||||||||||||
4496 | and is responsible for releasing it. | - | ||||||||||||
4497 | - | |||||||||||||
4498 | \sa toRawCFData(), fromCFData(), fromRawCFData(), fromRawData() | - | ||||||||||||
4499 | */ | - | ||||||||||||
4500 | - | |||||||||||||
4501 | /*! \fn CFDataRef QByteArray::toRawCFData() const | - | ||||||||||||
4502 | \since 5.3 | - | ||||||||||||
4503 | - | |||||||||||||
4504 | Constructs a CFData that uses the bytes of the QByteArray. | - | ||||||||||||
4505 | - | |||||||||||||
4506 | The QByteArray's bytes are not copied. | - | ||||||||||||
4507 | - | |||||||||||||
4508 | The caller guarantees that the QByteArray will not be deleted | - | ||||||||||||
4509 | or modified as long as this CFData object exists. | - | ||||||||||||
4510 | - | |||||||||||||
4511 | \sa toCFData(), fromRawCFData(), fromCFData(), fromRawData() | - | ||||||||||||
4512 | */ | - | ||||||||||||
4513 | - | |||||||||||||
4514 | /*! \fn QByteArray QByteArray::fromNSData(const NSData *data) | - | ||||||||||||
4515 | \since 5.3 | - | ||||||||||||
4516 | - | |||||||||||||
4517 | Constructs a new QByteArray containing a copy of the NSData \a data. | - | ||||||||||||
4518 | - | |||||||||||||
4519 | \sa fromRawNSData(), fromRawData(), toNSData(), toRawNSData() | - | ||||||||||||
4520 | */ | - | ||||||||||||
4521 | - | |||||||||||||
4522 | /*! \fn QByteArray QByteArray::fromRawNSData(const NSData *data) | - | ||||||||||||
4523 | \since 5.3 | - | ||||||||||||
4524 | - | |||||||||||||
4525 | Constructs a QByteArray that uses the bytes of the NSData \a data. | - | ||||||||||||
4526 | - | |||||||||||||
4527 | The \a data's bytes are not copied. | - | ||||||||||||
4528 | - | |||||||||||||
4529 | The caller guarantees that the NSData will not be deleted | - | ||||||||||||
4530 | or modified as long as this QByteArray object exists. | - | ||||||||||||
4531 | - | |||||||||||||
4532 | \sa fromNSData(), fromRawData(), toRawNSData(), toNSData() | - | ||||||||||||
4533 | */ | - | ||||||||||||
4534 | - | |||||||||||||
4535 | /*! \fn NSData QByteArray::toNSData() const | - | ||||||||||||
4536 | \since 5.3 | - | ||||||||||||
4537 | - | |||||||||||||
4538 | Creates a NSData from a QByteArray. The NSData object is autoreleased. | - | ||||||||||||
4539 | - | |||||||||||||
4540 | \sa fromNSData(), fromRawNSData(), fromRawData(), toRawNSData() | - | ||||||||||||
4541 | */ | - | ||||||||||||
4542 | - | |||||||||||||
4543 | /*! \fn NSData QByteArray::toRawNSData() const | - | ||||||||||||
4544 | \since 5.3 | - | ||||||||||||
4545 | - | |||||||||||||
4546 | Constructs a NSData that uses the bytes of the QByteArray. | - | ||||||||||||
4547 | - | |||||||||||||
4548 | The QByteArray's bytes are not copied. | - | ||||||||||||
4549 | - | |||||||||||||
4550 | The caller guarantees that the QByteArray will not be deleted | - | ||||||||||||
4551 | or modified as long as this NSData object exists. | - | ||||||||||||
4552 | - | |||||||||||||
4553 | \sa fromRawNSData(), fromNSData(), fromRawData(), toNSData() | - | ||||||||||||
4554 | */ | - | ||||||||||||
4555 | - | |||||||||||||
4556 | static inline bool q_strchr(const char str[], char chr) | - | ||||||||||||
4557 | { | - | ||||||||||||
4558 | if (!str) return false; | - | ||||||||||||
4559 | - | |||||||||||||
4560 | const char *ptr = str; | - | ||||||||||||
4561 | char c; | - | ||||||||||||
4562 | while ((c = *ptr++)) | - | ||||||||||||
4563 | if (c == chr) | - | ||||||||||||
4564 | return true; | - | ||||||||||||
4565 | return false; | - | ||||||||||||
4566 | } | - | ||||||||||||
4567 | - | |||||||||||||
4568 | static void q_toPercentEncoding(QByteArray *ba, const char *dontEncode, const char *alsoEncode, char percent) | - | ||||||||||||
4569 | { | - | ||||||||||||
4570 | if (ba->isEmpty()) | - | ||||||||||||
4571 | return; | - | ||||||||||||
4572 | - | |||||||||||||
4573 | QByteArray input = *ba; | - | ||||||||||||
4574 | int len = input.count(); | - | ||||||||||||
4575 | const char *inputData = input.constData(); | - | ||||||||||||
4576 | char *output = 0; | - | ||||||||||||
4577 | int length = 0; | - | ||||||||||||
4578 | - | |||||||||||||
4579 | for (int i = 0; i < len; ++i) { | - | ||||||||||||
4580 | unsigned char c = *inputData++; | - | ||||||||||||
4581 | if (((c >= 0x61 && c <= 0x7A) // ALPHA | - | ||||||||||||
4582 | || (c >= 0x41 && c <= 0x5A) // ALPHA | - | ||||||||||||
4583 | || (c >= 0x30 && c <= 0x39) // DIGIT | - | ||||||||||||
4584 | || c == 0x2D // - | - | ||||||||||||
4585 | || c == 0x2E // . | - | ||||||||||||
4586 | || c == 0x5F // _ | - | ||||||||||||
4587 | || c == 0x7E // ~ | - | ||||||||||||
4588 | || q_strchr(dontEncode, c)) | - | ||||||||||||
4589 | && !q_strchr(alsoEncode, c)) { | - | ||||||||||||
4590 | if (output) | - | ||||||||||||
4591 | output[length] = c; | - | ||||||||||||
4592 | ++length; | - | ||||||||||||
4593 | } else { | - | ||||||||||||
4594 | if (!output) { | - | ||||||||||||
4595 | // detach now | - | ||||||||||||
4596 | ba->resize(len*3); // worst case | - | ||||||||||||
4597 | output = ba->data(); | - | ||||||||||||
4598 | } | - | ||||||||||||
4599 | output[length++] = percent; | - | ||||||||||||
4600 | output[length++] = QtMiscUtils::toHexUpper((c & 0xf0) >> 4); | - | ||||||||||||
4601 | output[length++] = QtMiscUtils::toHexUpper(c & 0xf); | - | ||||||||||||
4602 | } | - | ||||||||||||
4603 | } | - | ||||||||||||
4604 | if (output) | - | ||||||||||||
4605 | ba->truncate(length); | - | ||||||||||||
4606 | } | - | ||||||||||||
4607 | - | |||||||||||||
4608 | void q_toPercentEncoding(QByteArray *ba, const char *exclude, const char *include) | - | ||||||||||||
4609 | { | - | ||||||||||||
4610 | q_toPercentEncoding(ba, exclude, include, '%'); | - | ||||||||||||
4611 | } | - | ||||||||||||
4612 | - | |||||||||||||
4613 | void q_normalizePercentEncoding(QByteArray *ba, const char *exclude) | - | ||||||||||||
4614 | { | - | ||||||||||||
4615 | q_fromPercentEncoding(ba, '%'); | - | ||||||||||||
4616 | q_toPercentEncoding(ba, exclude, 0, '%'); | - | ||||||||||||
4617 | } | - | ||||||||||||
4618 | - | |||||||||||||
4619 | /*! | - | ||||||||||||
4620 | \since 4.4 | - | ||||||||||||
4621 | - | |||||||||||||
4622 | Returns a URI/URL-style percent-encoded copy of this byte array. The | - | ||||||||||||
4623 | \a percent parameter allows you to override the default '%' | - | ||||||||||||
4624 | character for another. | - | ||||||||||||
4625 | - | |||||||||||||
4626 | By default, this function will encode all characters that are not | - | ||||||||||||
4627 | one of the following: | - | ||||||||||||
4628 | - | |||||||||||||
4629 | ALPHA ("a" to "z" and "A" to "Z") / DIGIT (0 to 9) / "-" / "." / "_" / "~" | - | ||||||||||||
4630 | - | |||||||||||||
4631 | To prevent characters from being encoded pass them to \a | - | ||||||||||||
4632 | exclude. To force characters to be encoded pass them to \a | - | ||||||||||||
4633 | include. The \a percent character is always encoded. | - | ||||||||||||
4634 | - | |||||||||||||
4635 | Example: | - | ||||||||||||
4636 | - | |||||||||||||
4637 | \code | - | ||||||||||||
4638 | QByteArray text = "{a fishy string?}"; | - | ||||||||||||
4639 | QByteArray ba = text.toPercentEncoding("{}", "s"); | - | ||||||||||||
4640 | qDebug(ba.constData()); | - | ||||||||||||
4641 | // prints "{a fi%73hy %73tring%3F}" | - | ||||||||||||
4642 | \endcode | - | ||||||||||||
4643 | - | |||||||||||||
4644 | The hex encoding uses the numbers 0-9 and the uppercase letters A-F. | - | ||||||||||||
4645 | - | |||||||||||||
4646 | \sa fromPercentEncoding(), QUrl::toPercentEncoding() | - | ||||||||||||
4647 | */ | - | ||||||||||||
4648 | QByteArray QByteArray::toPercentEncoding(const QByteArray &exclude, const QByteArray &include, | - | ||||||||||||
4649 | char percent) const | - | ||||||||||||
4650 | { | - | ||||||||||||
4651 | if (isNull()) | - | ||||||||||||
4652 | return QByteArray(); // preserve null | - | ||||||||||||
4653 | if (isEmpty()) | - | ||||||||||||
4654 | return QByteArray(data(), 0); | - | ||||||||||||
4655 | - | |||||||||||||
4656 | QByteArray include2 = include; | - | ||||||||||||
4657 | if (percent != '%') // the default | - | ||||||||||||
4658 | if ((percent >= 0x61 && percent <= 0x7A) // ALPHA | - | ||||||||||||
4659 | || (percent >= 0x41 && percent <= 0x5A) // ALPHA | - | ||||||||||||
4660 | || (percent >= 0x30 && percent <= 0x39) // DIGIT | - | ||||||||||||
4661 | || percent == 0x2D // - | - | ||||||||||||
4662 | || percent == 0x2E // . | - | ||||||||||||
4663 | || percent == 0x5F // _ | - | ||||||||||||
4664 | || percent == 0x7E) // ~ | - | ||||||||||||
4665 | include2 += percent; | - | ||||||||||||
4666 | - | |||||||||||||
4667 | QByteArray result = *this; | - | ||||||||||||
4668 | q_toPercentEncoding(&result, exclude.nulTerminated().constData(), include2.nulTerminated().constData(), percent); | - | ||||||||||||
4669 | - | |||||||||||||
4670 | return result; | - | ||||||||||||
4671 | } | - | ||||||||||||
4672 | - | |||||||||||||
4673 | /*! \typedef QByteArray::ConstIterator | - | ||||||||||||
4674 | \internal | - | ||||||||||||
4675 | */ | - | ||||||||||||
4676 | - | |||||||||||||
4677 | /*! \typedef QByteArray::Iterator | - | ||||||||||||
4678 | \internal | - | ||||||||||||
4679 | */ | - | ||||||||||||
4680 | - | |||||||||||||
4681 | /*! \typedef QByteArray::const_iterator | - | ||||||||||||
4682 | - | |||||||||||||
4683 | This typedef provides an STL-style const iterator for QByteArray. | - | ||||||||||||
4684 | - | |||||||||||||
4685 | \sa QByteArray::const_reverse_iterator, QByteArray::iterator | - | ||||||||||||
4686 | */ | - | ||||||||||||
4687 | - | |||||||||||||
4688 | /*! \typedef QByteArray::iterator | - | ||||||||||||
4689 | - | |||||||||||||
4690 | This typedef provides an STL-style non-const iterator for QByteArray. | - | ||||||||||||
4691 | - | |||||||||||||
4692 | \sa QByteArray::reverse_iterator, QByteArray::const_iterator | - | ||||||||||||
4693 | */ | - | ||||||||||||
4694 | - | |||||||||||||
4695 | /*! \typedef QByteArray::const_reverse_iterator | - | ||||||||||||
4696 | \since 5.6 | - | ||||||||||||
4697 | - | |||||||||||||
4698 | This typedef provides an STL-style const reverse iterator for QByteArray. | - | ||||||||||||
4699 | - | |||||||||||||
4700 | \sa QByteArray::reverse_iterator, QByteArray::const_iterator | - | ||||||||||||
4701 | */ | - | ||||||||||||
4702 | - | |||||||||||||
4703 | /*! \typedef QByteArray::reverse_iterator | - | ||||||||||||
4704 | \since 5.6 | - | ||||||||||||
4705 | - | |||||||||||||
4706 | This typedef provides an STL-style non-const reverse iterator for QByteArray. | - | ||||||||||||
4707 | - | |||||||||||||
4708 | \sa QByteArray::const_reverse_iterator, QByteArray::iterator | - | ||||||||||||
4709 | */ | - | ||||||||||||
4710 | - | |||||||||||||
4711 | /*! \typedef QByteArray::size_type | - | ||||||||||||
4712 | \internal | - | ||||||||||||
4713 | */ | - | ||||||||||||
4714 | - | |||||||||||||
4715 | /*! \typedef QByteArray::difference_type | - | ||||||||||||
4716 | \internal | - | ||||||||||||
4717 | */ | - | ||||||||||||
4718 | - | |||||||||||||
4719 | /*! \typedef QByteArray::const_reference | - | ||||||||||||
4720 | \internal | - | ||||||||||||
4721 | */ | - | ||||||||||||
4722 | - | |||||||||||||
4723 | /*! \typedef QByteArray::reference | - | ||||||||||||
4724 | \internal | - | ||||||||||||
4725 | */ | - | ||||||||||||
4726 | - | |||||||||||||
4727 | /*! \typedef QByteArray::const_pointer | - | ||||||||||||
4728 | \internal | - | ||||||||||||
4729 | */ | - | ||||||||||||
4730 | - | |||||||||||||
4731 | /*! \typedef QByteArray::pointer | - | ||||||||||||
4732 | \internal | - | ||||||||||||
4733 | */ | - | ||||||||||||
4734 | - | |||||||||||||
4735 | /*! \typedef QByteArray::value_type | - | ||||||||||||
4736 | \internal | - | ||||||||||||
4737 | */ | - | ||||||||||||
4738 | - | |||||||||||||
4739 | /*! | - | ||||||||||||
4740 | \fn DataPtr &QByteArray::data_ptr() | - | ||||||||||||
4741 | \internal | - | ||||||||||||
4742 | */ | - | ||||||||||||
4743 | - | |||||||||||||
4744 | /*! | - | ||||||||||||
4745 | \typedef QByteArray::DataPtr | - | ||||||||||||
4746 | \internal | - | ||||||||||||
4747 | */ | - | ||||||||||||
4748 | - | |||||||||||||
4749 | QT_END_NAMESPACE | - | ||||||||||||
Source code | Switch to Preprocessed file |