1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtCore module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #ifndef QBYTEARRAY_H
|
---|
43 | #define QBYTEARRAY_H
|
---|
44 |
|
---|
45 | #include <QtCore/qglobal.h>
|
---|
46 | #include <QtCore/qatomic.h>
|
---|
47 |
|
---|
48 | #include <string.h>
|
---|
49 | #include <stdarg.h>
|
---|
50 |
|
---|
51 | #ifdef truncate
|
---|
52 | #error qbytearray.h must be included before any header file that defines truncate
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | QT_BEGIN_HEADER
|
---|
56 |
|
---|
57 | QT_BEGIN_NAMESPACE
|
---|
58 |
|
---|
59 | QT_MODULE(Core)
|
---|
60 |
|
---|
61 | /*****************************************************************************
|
---|
62 | Safe and portable C string functions; extensions to standard string.h
|
---|
63 | *****************************************************************************/
|
---|
64 |
|
---|
65 | Q_CORE_EXPORT char *qstrdup(const char *);
|
---|
66 |
|
---|
67 | inline uint qstrlen(const char *str)
|
---|
68 | { return str ? uint(strlen(str)) : 0; }
|
---|
69 |
|
---|
70 | inline uint qstrnlen(const char *str, uint maxlen)
|
---|
71 | {
|
---|
72 | uint length = 0;
|
---|
73 | if (str) {
|
---|
74 | while (length < maxlen && *str++)
|
---|
75 | length++;
|
---|
76 | }
|
---|
77 | return length;
|
---|
78 | }
|
---|
79 |
|
---|
80 | Q_CORE_EXPORT char *qstrcpy(char *dst, const char *src);
|
---|
81 | Q_CORE_EXPORT char *qstrncpy(char *dst, const char *src, uint len);
|
---|
82 |
|
---|
83 | Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2);
|
---|
84 | Q_CORE_EXPORT int qstrcmp(const QByteArray &str1, const QByteArray &str2);
|
---|
85 | Q_CORE_EXPORT int qstrcmp(const QByteArray &str1, const char *str2);
|
---|
86 | static inline int qstrcmp(const char *str1, const QByteArray &str2)
|
---|
87 | { return -qstrcmp(str2, str1); }
|
---|
88 |
|
---|
89 | inline int qstrncmp(const char *str1, const char *str2, uint len)
|
---|
90 | {
|
---|
91 | return (str1 && str2) ? strncmp(str1, str2, len)
|
---|
92 | : (str1 ? 1 : (str2 ? -1 : 0));
|
---|
93 | }
|
---|
94 | Q_CORE_EXPORT int qstricmp(const char *, const char *);
|
---|
95 | Q_CORE_EXPORT int qstrnicmp(const char *, const char *, uint len);
|
---|
96 |
|
---|
97 | // implemented in qvsnprintf.cpp
|
---|
98 | Q_CORE_EXPORT int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap);
|
---|
99 | Q_CORE_EXPORT int qsnprintf(char *str, size_t n, const char *fmt, ...);
|
---|
100 |
|
---|
101 | #ifdef QT3_SUPPORT
|
---|
102 | inline QT3_SUPPORT void *qmemmove(void *dst, const void *src, uint len)
|
---|
103 | { return memmove(dst, src, len); }
|
---|
104 | inline QT3_SUPPORT uint cstrlen(const char *str)
|
---|
105 | { return uint(strlen(str)); }
|
---|
106 | inline QT3_SUPPORT char *cstrcpy(char *dst, const char *src)
|
---|
107 | { return qstrcpy(dst,src); }
|
---|
108 | inline QT3_SUPPORT int cstrcmp(const char *str1, const char *str2)
|
---|
109 | { return strcmp(str1,str2); }
|
---|
110 | inline QT3_SUPPORT int cstrncmp(const char *str1, const char *str2, uint len)
|
---|
111 | { return strncmp(str1,str2,len); }
|
---|
112 | #endif
|
---|
113 |
|
---|
114 | // qChecksum: Internet checksum
|
---|
115 |
|
---|
116 | Q_CORE_EXPORT quint16 qChecksum(const char *s, uint len);
|
---|
117 |
|
---|
118 | class QByteRef;
|
---|
119 | class QString;
|
---|
120 | class QDataStream;
|
---|
121 | template <typename T> class QList;
|
---|
122 |
|
---|
123 | class Q_CORE_EXPORT QByteArray
|
---|
124 | {
|
---|
125 | public:
|
---|
126 | inline QByteArray();
|
---|
127 | QByteArray(const char *);
|
---|
128 | QByteArray(const char *, int size);
|
---|
129 | QByteArray(int size, char c);
|
---|
130 | inline QByteArray(const QByteArray &);
|
---|
131 | inline ~QByteArray();
|
---|
132 |
|
---|
133 | QByteArray &operator=(const QByteArray &);
|
---|
134 | QByteArray &operator=(const char *str);
|
---|
135 |
|
---|
136 | inline int size() const;
|
---|
137 | bool isEmpty() const;
|
---|
138 | void resize(int size);
|
---|
139 |
|
---|
140 | QByteArray &fill(char c, int size = -1);
|
---|
141 |
|
---|
142 | int capacity() const;
|
---|
143 | void reserve(int size);
|
---|
144 | void squeeze();
|
---|
145 |
|
---|
146 | #ifndef QT_NO_CAST_FROM_BYTEARRAY
|
---|
147 | operator const char *() const;
|
---|
148 | operator const void *() const;
|
---|
149 | #endif
|
---|
150 | char *data();
|
---|
151 | const char *data() const;
|
---|
152 | inline const char *constData() const;
|
---|
153 | inline void detach();
|
---|
154 | bool isDetached() const;
|
---|
155 | void clear();
|
---|
156 |
|
---|
157 | #ifdef Q_COMPILER_MANGLES_RETURN_TYPE
|
---|
158 | const char at(int i) const;
|
---|
159 | const char operator[](int i) const;
|
---|
160 | const char operator[](uint i) const;
|
---|
161 | #else
|
---|
162 | char at(int i) const;
|
---|
163 | char operator[](int i) const;
|
---|
164 | char operator[](uint i) const;
|
---|
165 | #endif
|
---|
166 | QByteRef operator[](int i);
|
---|
167 | QByteRef operator[](uint i);
|
---|
168 |
|
---|
169 | int indexOf(char c, int from = 0) const;
|
---|
170 | int indexOf(const char *c, int from = 0) const;
|
---|
171 | int indexOf(const QByteArray &a, int from = 0) const;
|
---|
172 | int lastIndexOf(char c, int from = -1) const;
|
---|
173 | int lastIndexOf(const char *c, int from = -1) const;
|
---|
174 | int lastIndexOf(const QByteArray &a, int from = -1) const;
|
---|
175 |
|
---|
176 | QBool contains(char c) const;
|
---|
177 | QBool contains(const char *a) const;
|
---|
178 | QBool contains(const QByteArray &a) const;
|
---|
179 | int count(char c) const;
|
---|
180 | int count(const char *a) const;
|
---|
181 | int count(const QByteArray &a) const;
|
---|
182 |
|
---|
183 | QByteArray left(int len) const;
|
---|
184 | QByteArray right(int len) const;
|
---|
185 | QByteArray mid(int index, int len = -1) const;
|
---|
186 |
|
---|
187 | bool startsWith(const QByteArray &a) const;
|
---|
188 | bool startsWith(char c) const;
|
---|
189 | bool startsWith(const char *c) const;
|
---|
190 |
|
---|
191 | bool endsWith(const QByteArray &a) const;
|
---|
192 | bool endsWith(char c) const;
|
---|
193 | bool endsWith(const char *c) const;
|
---|
194 |
|
---|
195 | void truncate(int pos);
|
---|
196 | void chop(int n);
|
---|
197 |
|
---|
198 | QByteArray toLower() const;
|
---|
199 | QByteArray toUpper() const;
|
---|
200 |
|
---|
201 | QByteArray trimmed() const;
|
---|
202 | QByteArray simplified() const;
|
---|
203 | QByteArray leftJustified(int width, char fill = ' ', bool truncate = false) const;
|
---|
204 | QByteArray rightJustified(int width, char fill = ' ', bool truncate = false) const;
|
---|
205 |
|
---|
206 | #ifdef QT3_SUPPORT
|
---|
207 | inline QT3_SUPPORT QByteArray leftJustify(uint width, char aFill = ' ', bool aTruncate = false) const
|
---|
208 | { return leftJustified(int(width), aFill, aTruncate); }
|
---|
209 | inline QT3_SUPPORT QByteArray rightJustify(uint width, char aFill = ' ', bool aTruncate = false) const
|
---|
210 | { return rightJustified(int(width), aFill, aTruncate); }
|
---|
211 | #endif
|
---|
212 |
|
---|
213 | QByteArray &prepend(char c);
|
---|
214 | QByteArray &prepend(const char *s);
|
---|
215 | QByteArray &prepend(const QByteArray &a);
|
---|
216 | QByteArray &append(char c);
|
---|
217 | QByteArray &append(const char *s);
|
---|
218 | QByteArray &append(const char *s, int len);
|
---|
219 | QByteArray &append(const QByteArray &a);
|
---|
220 | QByteArray &insert(int i, char c);
|
---|
221 | QByteArray &insert(int i, const char *s);
|
---|
222 | QByteArray &insert(int i, const QByteArray &a);
|
---|
223 | QByteArray &remove(int index, int len);
|
---|
224 | QByteArray &replace(int index, int len, const char *s);
|
---|
225 | QByteArray &replace(int index, int len, const QByteArray &s);
|
---|
226 | QByteArray &replace(char before, const char *after);
|
---|
227 | QByteArray &replace(char before, const QByteArray &after);
|
---|
228 | QByteArray &replace(const char *before, const char *after);
|
---|
229 | QByteArray &replace(const char *before, int bsize, const char *after, int asize);
|
---|
230 | QByteArray &replace(const QByteArray &before, const QByteArray &after);
|
---|
231 | QByteArray &replace(const QByteArray &before, const char *after);
|
---|
232 | QByteArray &replace(const char *before, const QByteArray &after);
|
---|
233 | QByteArray &replace(char before, char after);
|
---|
234 | QByteArray &operator+=(char c);
|
---|
235 | QByteArray &operator+=(const char *s);
|
---|
236 | QByteArray &operator+=(const QByteArray &a);
|
---|
237 |
|
---|
238 | QList<QByteArray> split(char sep) const;
|
---|
239 |
|
---|
240 | QByteArray repeated(int times) const;
|
---|
241 |
|
---|
242 | #ifndef QT_NO_CAST_TO_ASCII
|
---|
243 | QT_ASCII_CAST_WARN QByteArray &append(const QString &s);
|
---|
244 | QT_ASCII_CAST_WARN QByteArray &insert(int i, const QString &s);
|
---|
245 | QT_ASCII_CAST_WARN QByteArray &replace(const QString &before, const char *after);
|
---|
246 | QT_ASCII_CAST_WARN QByteArray &replace(char c, const QString &after);
|
---|
247 | QT_ASCII_CAST_WARN QByteArray &replace(const QString &before, const QByteArray &after);
|
---|
248 |
|
---|
249 | QT_ASCII_CAST_WARN QByteArray &operator+=(const QString &s);
|
---|
250 | QT_ASCII_CAST_WARN int indexOf(const QString &s, int from = 0) const;
|
---|
251 | QT_ASCII_CAST_WARN int lastIndexOf(const QString &s, int from = -1) const;
|
---|
252 | #endif
|
---|
253 | #ifndef QT_NO_CAST_FROM_ASCII
|
---|
254 | inline QT_ASCII_CAST_WARN bool operator==(const QString &s2) const;
|
---|
255 | inline QT_ASCII_CAST_WARN bool operator!=(const QString &s2) const;
|
---|
256 | inline QT_ASCII_CAST_WARN bool operator<(const QString &s2) const;
|
---|
257 | inline QT_ASCII_CAST_WARN bool operator>(const QString &s2) const;
|
---|
258 | inline QT_ASCII_CAST_WARN bool operator<=(const QString &s2) const;
|
---|
259 | inline QT_ASCII_CAST_WARN bool operator>=(const QString &s2) const;
|
---|
260 | #endif
|
---|
261 |
|
---|
262 | short toShort(bool *ok = 0, int base = 10) const;
|
---|
263 | ushort toUShort(bool *ok = 0, int base = 10) const;
|
---|
264 | int toInt(bool *ok = 0, int base = 10) const;
|
---|
265 | uint toUInt(bool *ok = 0, int base = 10) const;
|
---|
266 | long toLong(bool *ok = 0, int base = 10) const;
|
---|
267 | ulong toULong(bool *ok = 0, int base = 10) const;
|
---|
268 | qlonglong toLongLong(bool *ok = 0, int base = 10) const;
|
---|
269 | qulonglong toULongLong(bool *ok = 0, int base = 10) const;
|
---|
270 | float toFloat(bool *ok = 0) const;
|
---|
271 | double toDouble(bool *ok = 0) const;
|
---|
272 | QByteArray toBase64() const;
|
---|
273 | QByteArray toHex() const;
|
---|
274 | QByteArray toPercentEncoding(const QByteArray &exclude = QByteArray(),
|
---|
275 | const QByteArray &include = QByteArray(),
|
---|
276 | char percent = '%') const;
|
---|
277 |
|
---|
278 | QByteArray &setNum(short, int base = 10);
|
---|
279 | QByteArray &setNum(ushort, int base = 10);
|
---|
280 | QByteArray &setNum(int, int base = 10);
|
---|
281 | QByteArray &setNum(uint, int base = 10);
|
---|
282 | QByteArray &setNum(qlonglong, int base = 10);
|
---|
283 | QByteArray &setNum(qulonglong, int base = 10);
|
---|
284 | QByteArray &setNum(float, char f = 'g', int prec = 6);
|
---|
285 | QByteArray &setNum(double, char f = 'g', int prec = 6);
|
---|
286 |
|
---|
287 | static QByteArray number(int, int base = 10);
|
---|
288 | static QByteArray number(uint, int base = 10);
|
---|
289 | static QByteArray number(qlonglong, int base = 10);
|
---|
290 | static QByteArray number(qulonglong, int base = 10);
|
---|
291 | static QByteArray number(double, char f = 'g', int prec = 6);
|
---|
292 | static QByteArray fromRawData(const char *, int size);
|
---|
293 | static QByteArray fromBase64(const QByteArray &base64);
|
---|
294 | static QByteArray fromHex(const QByteArray &hexEncoded);
|
---|
295 | static QByteArray fromPercentEncoding(const QByteArray &pctEncoded, char percent = '%');
|
---|
296 |
|
---|
297 |
|
---|
298 | typedef char *iterator;
|
---|
299 | typedef const char *const_iterator;
|
---|
300 | typedef iterator Iterator;
|
---|
301 | typedef const_iterator ConstIterator;
|
---|
302 | iterator begin();
|
---|
303 | const_iterator begin() const;
|
---|
304 | const_iterator constBegin() const;
|
---|
305 | iterator end();
|
---|
306 | const_iterator end() const;
|
---|
307 | const_iterator constEnd() const;
|
---|
308 |
|
---|
309 | // stl compatibility
|
---|
310 | typedef const char & const_reference;
|
---|
311 | typedef char & reference;
|
---|
312 | void push_back(char c);
|
---|
313 | void push_back(const char *c);
|
---|
314 | void push_back(const QByteArray &a);
|
---|
315 | void push_front(char c);
|
---|
316 | void push_front(const char *c);
|
---|
317 | void push_front(const QByteArray &a);
|
---|
318 |
|
---|
319 | inline int count() const { return d->size; }
|
---|
320 | int length() const { return d->size; }
|
---|
321 | bool isNull() const;
|
---|
322 |
|
---|
323 | // compatibility
|
---|
324 | #ifdef QT3_SUPPORT
|
---|
325 | QT3_SUPPORT_CONSTRUCTOR QByteArray(int size);
|
---|
326 | inline QT3_SUPPORT QByteArray& duplicate(const QByteArray& a) { *this = a; return *this; }
|
---|
327 | inline QT3_SUPPORT QByteArray& duplicate(const char *a, uint n)
|
---|
328 | { *this = QByteArray(a, n); return *this; }
|
---|
329 | inline QT3_SUPPORT QByteArray& setRawData(const char *a, uint n)
|
---|
330 | { *this = fromRawData(a, n); return *this; }
|
---|
331 | inline QT3_SUPPORT void resetRawData(const char *, uint) { clear(); }
|
---|
332 | inline QT3_SUPPORT QByteArray lower() const { return toLower(); }
|
---|
333 | inline QT3_SUPPORT QByteArray upper() const { return toUpper(); }
|
---|
334 | inline QT3_SUPPORT QByteArray stripWhiteSpace() const { return trimmed(); }
|
---|
335 | inline QT3_SUPPORT QByteArray simplifyWhiteSpace() const { return simplified(); }
|
---|
336 | inline QT3_SUPPORT int find(char c, int from = 0) const { return indexOf(c, from); }
|
---|
337 | inline QT3_SUPPORT int find(const char *c, int from = 0) const { return indexOf(c, from); }
|
---|
338 | inline QT3_SUPPORT int find(const QByteArray &ba, int from = 0) const { return indexOf(ba, from); }
|
---|
339 | inline QT3_SUPPORT int findRev(char c, int from = -1) const { return lastIndexOf(c, from); }
|
---|
340 | inline QT3_SUPPORT int findRev(const char *c, int from = -1) const { return lastIndexOf(c, from); }
|
---|
341 | inline QT3_SUPPORT int findRev(const QByteArray &ba, int from = -1) const { return lastIndexOf(ba, from); }
|
---|
342 | #ifndef QT_NO_CAST_TO_ASCII
|
---|
343 | QT3_SUPPORT int find(const QString &s, int from = 0) const;
|
---|
344 | QT3_SUPPORT int findRev(const QString &s, int from = -1) const;
|
---|
345 | #endif
|
---|
346 | #endif
|
---|
347 |
|
---|
348 | private:
|
---|
349 | operator QNoImplicitBoolCast() const;
|
---|
350 | struct Data {
|
---|
351 | QBasicAtomicInt ref;
|
---|
352 | int alloc, size;
|
---|
353 | // ### Qt 5.0: We need to add the missing capacity bit
|
---|
354 | // (like other tool classes have), to maintain the
|
---|
355 | // reserved memory on resize.
|
---|
356 | char *data;
|
---|
357 | char array[1];
|
---|
358 | };
|
---|
359 | static Data shared_null;
|
---|
360 | static Data shared_empty;
|
---|
361 | Data *d;
|
---|
362 | QByteArray(Data *dd, int /*dummy*/, int /*dummy*/) : d(dd) {}
|
---|
363 | void realloc(int alloc);
|
---|
364 | void expand(int i);
|
---|
365 | QByteArray nulTerminated() const;
|
---|
366 |
|
---|
367 | friend class QByteRef;
|
---|
368 | friend class QString;
|
---|
369 | public:
|
---|
370 | typedef Data * DataPtr;
|
---|
371 | inline DataPtr &data_ptr() { return d; }
|
---|
372 | };
|
---|
373 |
|
---|
374 | inline QByteArray::QByteArray(): d(&shared_null) { d->ref.ref(); }
|
---|
375 | inline QByteArray::~QByteArray() { if (!d->ref.deref()) qFree(d); }
|
---|
376 | inline int QByteArray::size() const
|
---|
377 | { return d->size; }
|
---|
378 |
|
---|
379 | #ifdef Q_COMPILER_MANGLES_RETURN_TYPE
|
---|
380 | inline const char QByteArray::at(int i) const
|
---|
381 | { Q_ASSERT(i >= 0 && i < size()); return d->data[i]; }
|
---|
382 | inline const char QByteArray::operator[](int i) const
|
---|
383 | { Q_ASSERT(i >= 0 && i < size()); return d->data[i]; }
|
---|
384 | inline const char QByteArray::operator[](uint i) const
|
---|
385 | { Q_ASSERT(i < uint(size())); return d->data[i]; }
|
---|
386 | #else
|
---|
387 | inline char QByteArray::at(int i) const
|
---|
388 | { Q_ASSERT(i >= 0 && i < size()); return d->data[i]; }
|
---|
389 | inline char QByteArray::operator[](int i) const
|
---|
390 | { Q_ASSERT(i >= 0 && i < size()); return d->data[i]; }
|
---|
391 | inline char QByteArray::operator[](uint i) const
|
---|
392 | { Q_ASSERT(i < uint(size())); return d->data[i]; }
|
---|
393 | #endif
|
---|
394 |
|
---|
395 | inline bool QByteArray::isEmpty() const
|
---|
396 | { return d->size == 0; }
|
---|
397 | #ifndef QT_NO_CAST_FROM_BYTEARRAY
|
---|
398 | inline QByteArray::operator const char *() const
|
---|
399 | { return d->data; }
|
---|
400 | inline QByteArray::operator const void *() const
|
---|
401 | { return d->data; }
|
---|
402 | #endif
|
---|
403 | inline char *QByteArray::data()
|
---|
404 | { detach(); return d->data; }
|
---|
405 | inline const char *QByteArray::data() const
|
---|
406 | { return d->data; }
|
---|
407 | inline const char *QByteArray::constData() const
|
---|
408 | { return d->data; }
|
---|
409 | inline void QByteArray::detach()
|
---|
410 | { if (d->ref != 1 || d->data != d->array) realloc(d->size); }
|
---|
411 | inline bool QByteArray::isDetached() const
|
---|
412 | { return d->ref == 1; }
|
---|
413 | inline QByteArray::QByteArray(const QByteArray &a) : d(a.d)
|
---|
414 | { d->ref.ref(); }
|
---|
415 | #ifdef QT3_SUPPORT
|
---|
416 | inline QByteArray::QByteArray(int aSize) : d(&shared_null)
|
---|
417 | { d->ref.ref(); if (aSize > 0) fill('\0', aSize); }
|
---|
418 | #endif
|
---|
419 |
|
---|
420 | inline int QByteArray::capacity() const
|
---|
421 | { return d->alloc; }
|
---|
422 |
|
---|
423 | inline void QByteArray::reserve(int asize)
|
---|
424 | { if (d->ref != 1 || asize > d->alloc) realloc(asize); }
|
---|
425 |
|
---|
426 | inline void QByteArray::squeeze()
|
---|
427 | { if (d->size < d->alloc) realloc(d->size); }
|
---|
428 |
|
---|
429 | class Q_CORE_EXPORT QByteRef {
|
---|
430 | QByteArray &a;
|
---|
431 | int i;
|
---|
432 | inline QByteRef(QByteArray &array, int idx)
|
---|
433 | : a(array),i(idx) {}
|
---|
434 | friend class QByteArray;
|
---|
435 | public:
|
---|
436 | #ifdef Q_COMPILER_MANGLES_RETURN_TYPE
|
---|
437 | inline operator const char() const
|
---|
438 | { return i < a.d->size ? a.d->data[i] : 0; }
|
---|
439 | #else
|
---|
440 | inline operator char() const
|
---|
441 | { return i < a.d->size ? a.d->data[i] : 0; }
|
---|
442 | #endif
|
---|
443 | inline QByteRef &operator=(char c)
|
---|
444 | { if (i >= a.d->size) a.expand(i); else a.detach();
|
---|
445 | a.d->data[i] = c; return *this; }
|
---|
446 | inline QByteRef &operator=(const QByteRef &c)
|
---|
447 | { if (i >= a.d->size) a.expand(i); else a.detach();
|
---|
448 | a.d->data[i] = c.a.d->data[c.i]; return *this; }
|
---|
449 | inline bool operator==(char c) const
|
---|
450 | { return a.d->data[i] == c; }
|
---|
451 | inline bool operator!=(char c) const
|
---|
452 | { return a.d->data[i] != c; }
|
---|
453 | inline bool operator>(char c) const
|
---|
454 | { return a.d->data[i] > c; }
|
---|
455 | inline bool operator>=(char c) const
|
---|
456 | { return a.d->data[i] >= c; }
|
---|
457 | inline bool operator<(char c) const
|
---|
458 | { return a.d->data[i] < c; }
|
---|
459 | inline bool operator<=(char c) const
|
---|
460 | { return a.d->data[i] <= c; }
|
---|
461 | };
|
---|
462 |
|
---|
463 | inline QByteRef QByteArray::operator[](int i)
|
---|
464 | { Q_ASSERT(i >= 0); return QByteRef(*this, i); }
|
---|
465 | inline QByteRef QByteArray::operator[](uint i)
|
---|
466 | { return QByteRef(*this, i); }
|
---|
467 | inline QByteArray::iterator QByteArray::begin()
|
---|
468 | { detach(); return d->data; }
|
---|
469 | inline QByteArray::const_iterator QByteArray::begin() const
|
---|
470 | { return d->data; }
|
---|
471 | inline QByteArray::const_iterator QByteArray::constBegin() const
|
---|
472 | { return d->data; }
|
---|
473 | inline QByteArray::iterator QByteArray::end()
|
---|
474 | { detach(); return d->data + d->size; }
|
---|
475 | inline QByteArray::const_iterator QByteArray::end() const
|
---|
476 | { return d->data + d->size; }
|
---|
477 | inline QByteArray::const_iterator QByteArray::constEnd() const
|
---|
478 | { return d->data + d->size; }
|
---|
479 | inline QByteArray &QByteArray::operator+=(char c)
|
---|
480 | { return append(c); }
|
---|
481 | inline QByteArray &QByteArray::operator+=(const char *s)
|
---|
482 | { return append(s); }
|
---|
483 | inline QByteArray &QByteArray::operator+=(const QByteArray &a)
|
---|
484 | { return append(a); }
|
---|
485 | inline void QByteArray::push_back(char c)
|
---|
486 | { append(c); }
|
---|
487 | inline void QByteArray::push_back(const char *c)
|
---|
488 | { append(c); }
|
---|
489 | inline void QByteArray::push_back(const QByteArray &a)
|
---|
490 | { append(a); }
|
---|
491 | inline void QByteArray::push_front(char c)
|
---|
492 | { prepend(c); }
|
---|
493 | inline void QByteArray::push_front(const char *c)
|
---|
494 | { prepend(c); }
|
---|
495 | inline void QByteArray::push_front(const QByteArray &a)
|
---|
496 | { prepend(a); }
|
---|
497 | inline QBool QByteArray::contains(const QByteArray &a) const
|
---|
498 | { return QBool(indexOf(a) != -1); }
|
---|
499 | inline QBool QByteArray::contains(char c) const
|
---|
500 | { return QBool(indexOf(c) != -1); }
|
---|
501 | inline bool operator==(const QByteArray &a1, const QByteArray &a2)
|
---|
502 | { return (a1.size() == a2.size()) && (memcmp(a1.constData(), a2.constData(), a1.size())==0); }
|
---|
503 | inline bool operator==(const QByteArray &a1, const char *a2)
|
---|
504 | { return a2 ? qstrcmp(a1,a2) == 0 : a1.isEmpty(); }
|
---|
505 | inline bool operator==(const char *a1, const QByteArray &a2)
|
---|
506 | { return a1 ? qstrcmp(a1,a2) == 0 : a2.isEmpty(); }
|
---|
507 | inline bool operator!=(const QByteArray &a1, const QByteArray &a2)
|
---|
508 | { return !(a1==a2); }
|
---|
509 | inline bool operator!=(const QByteArray &a1, const char *a2)
|
---|
510 | { return a2 ? qstrcmp(a1,a2) != 0 : !a1.isEmpty(); }
|
---|
511 | inline bool operator!=(const char *a1, const QByteArray &a2)
|
---|
512 | { return a1 ? qstrcmp(a1,a2) != 0 : !a2.isEmpty(); }
|
---|
513 | inline bool operator<(const QByteArray &a1, const QByteArray &a2)
|
---|
514 | { return qstrcmp(a1, a2) < 0; }
|
---|
515 | inline bool operator<(const QByteArray &a1, const char *a2)
|
---|
516 | { return qstrcmp(a1, a2) < 0; }
|
---|
517 | inline bool operator<(const char *a1, const QByteArray &a2)
|
---|
518 | { return qstrcmp(a1, a2) < 0; }
|
---|
519 | inline bool operator<=(const QByteArray &a1, const QByteArray &a2)
|
---|
520 | { return qstrcmp(a1, a2) <= 0; }
|
---|
521 | inline bool operator<=(const QByteArray &a1, const char *a2)
|
---|
522 | { return qstrcmp(a1, a2) <= 0; }
|
---|
523 | inline bool operator<=(const char *a1, const QByteArray &a2)
|
---|
524 | { return qstrcmp(a1, a2) <= 0; }
|
---|
525 | inline bool operator>(const QByteArray &a1, const QByteArray &a2)
|
---|
526 | { return qstrcmp(a1, a2) > 0; }
|
---|
527 | inline bool operator>(const QByteArray &a1, const char *a2)
|
---|
528 | { return qstrcmp(a1, a2) > 0; }
|
---|
529 | inline bool operator>(const char *a1, const QByteArray &a2)
|
---|
530 | { return qstrcmp(a1, a2) > 0; }
|
---|
531 | inline bool operator>=(const QByteArray &a1, const QByteArray &a2)
|
---|
532 | { return qstrcmp(a1, a2) >= 0; }
|
---|
533 | inline bool operator>=(const QByteArray &a1, const char *a2)
|
---|
534 | { return qstrcmp(a1, a2) >= 0; }
|
---|
535 | inline bool operator>=(const char *a1, const QByteArray &a2)
|
---|
536 | { return qstrcmp(a1, a2) >= 0; }
|
---|
537 | inline const QByteArray operator+(const QByteArray &a1, const QByteArray &a2)
|
---|
538 | { return QByteArray(a1) += a2; }
|
---|
539 | inline const QByteArray operator+(const QByteArray &a1, const char *a2)
|
---|
540 | { return QByteArray(a1) += a2; }
|
---|
541 | inline const QByteArray operator+(const QByteArray &a1, char a2)
|
---|
542 | { return QByteArray(a1) += a2; }
|
---|
543 | inline const QByteArray operator+(const char *a1, const QByteArray &a2)
|
---|
544 | { return QByteArray(a1) += a2; }
|
---|
545 | inline const QByteArray operator+(char a1, const QByteArray &a2)
|
---|
546 | { return QByteArray(&a1, 1) += a2; }
|
---|
547 | inline QBool QByteArray::contains(const char *c) const
|
---|
548 | { return QBool(indexOf(c) != -1); }
|
---|
549 | inline QByteArray &QByteArray::replace(char before, const char *c)
|
---|
550 | { return replace(&before, 1, c, qstrlen(c)); }
|
---|
551 | inline QByteArray &QByteArray::replace(const QByteArray &before, const char *c)
|
---|
552 | { return replace(before.constData(), before.size(), c, qstrlen(c)); }
|
---|
553 | inline QByteArray &QByteArray::replace(const char *before, const char *after)
|
---|
554 | { return replace(before, qstrlen(before), after, qstrlen(after)); }
|
---|
555 |
|
---|
556 | inline QByteArray &QByteArray::setNum(short n, int base)
|
---|
557 | { return setNum(qlonglong(n), base); }
|
---|
558 | inline QByteArray &QByteArray::setNum(ushort n, int base)
|
---|
559 | { return setNum(qulonglong(n), base); }
|
---|
560 | inline QByteArray &QByteArray::setNum(int n, int base)
|
---|
561 | { return setNum(qlonglong(n), base); }
|
---|
562 | inline QByteArray &QByteArray::setNum(uint n, int base)
|
---|
563 | { return setNum(qulonglong(n), base); }
|
---|
564 | inline QByteArray &QByteArray::setNum(float n, char f, int prec)
|
---|
565 | { return setNum(double(n),f,prec); }
|
---|
566 |
|
---|
567 |
|
---|
568 | #ifndef QT_NO_DATASTREAM
|
---|
569 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QByteArray &);
|
---|
570 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QByteArray &);
|
---|
571 | #endif
|
---|
572 |
|
---|
573 | #ifndef QT_NO_COMPRESS
|
---|
574 | Q_CORE_EXPORT QByteArray qCompress(const uchar* data, int nbytes, int compressionLevel = -1);
|
---|
575 | Q_CORE_EXPORT QByteArray qUncompress(const uchar* data, int nbytes);
|
---|
576 | inline QByteArray qCompress(const QByteArray& data, int compressionLevel = -1)
|
---|
577 | { return qCompress(reinterpret_cast<const uchar *>(data.constData()), data.size(), compressionLevel); }
|
---|
578 | inline QByteArray qUncompress(const QByteArray& data)
|
---|
579 | { return qUncompress(reinterpret_cast<const uchar*>(data.constData()), data.size()); }
|
---|
580 | #endif
|
---|
581 |
|
---|
582 | Q_DECLARE_TYPEINFO(QByteArray, Q_MOVABLE_TYPE);
|
---|
583 | Q_DECLARE_SHARED(QByteArray)
|
---|
584 |
|
---|
585 | QT_END_NAMESPACE
|
---|
586 |
|
---|
587 | QT_END_HEADER
|
---|
588 |
|
---|
589 | #endif // QBYTEARRAY_H
|
---|