Line data Source code
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 : #ifndef QBYTEARRAY_H
42 : #define QBYTEARRAY_H
43 :
44 : #include <QtCore/qrefcount.h>
45 : #include <QtCore/qnamespace.h>
46 : #include <QtCore/qarraydata.h>
47 :
48 : #include <stdlib.h>
49 : #include <string.h>
50 : #include <stdarg.h>
51 :
52 : #include <string>
53 : #include <iterator>
54 :
55 : #ifdef truncate
56 : #error qbytearray.h must be included before any header file that defines truncate
57 : #endif
58 :
59 : #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
60 : Q_FORWARD_DECLARE_CF_TYPE(CFData);
61 : Q_FORWARD_DECLARE_OBJC_CLASS(NSData);
62 : #endif
63 :
64 : QT_BEGIN_NAMESPACE
65 :
66 :
67 : /*****************************************************************************
68 : Safe and portable C string functions; extensions to standard string.h
69 : *****************************************************************************/
70 :
71 : Q_CORE_EXPORT char *qstrdup(const char *);
72 :
73 : inline uint qstrlen(const char *str)
74 : { return str ? uint(strlen(str)) : 0; }
75 :
76 53 : inline uint qstrnlen(const char *str, uint maxlen)
77 : {
78 53 : uint length = 0;
79 53 : if (str) {
80 37141 : while (length < maxlen && *str++)
81 18544 : length++;
82 : }
83 53 : return length;
84 : }
85 :
86 : Q_CORE_EXPORT char *qstrcpy(char *dst, const char *src);
87 : Q_CORE_EXPORT char *qstrncpy(char *dst, const char *src, uint len);
88 :
89 : Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2);
90 : Q_CORE_EXPORT int qstrcmp(const QByteArray &str1, const QByteArray &str2);
91 : Q_CORE_EXPORT int qstrcmp(const QByteArray &str1, const char *str2);
92 : static inline int qstrcmp(const char *str1, const QByteArray &str2)
93 : { return -qstrcmp(str2, str1); }
94 :
95 : inline int qstrncmp(const char *str1, const char *str2, uint len)
96 : {
97 : return (str1 && str2) ? strncmp(str1, str2, len)
98 : : (str1 ? 1 : (str2 ? -1 : 0));
99 : }
100 : Q_CORE_EXPORT int qstricmp(const char *, const char *);
101 : Q_CORE_EXPORT int qstrnicmp(const char *, const char *, uint len);
102 :
103 : // implemented in qvsnprintf.cpp
104 : Q_CORE_EXPORT int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap);
105 : Q_CORE_EXPORT int qsnprintf(char *str, size_t n, const char *fmt, ...);
106 :
107 : // qChecksum: Internet checksum
108 : Q_CORE_EXPORT quint16 qChecksum(const char *s, uint len); // ### Qt 6: Remove
109 : Q_CORE_EXPORT quint16 qChecksum(const char *s, uint len, Qt::ChecksumType standard); // ### Qt 6: Use Qt::ChecksumType standard = Qt::ChecksumIso3309
110 :
111 : class QByteRef;
112 : class QString;
113 : class QDataStream;
114 : template <typename T> class QList;
115 :
116 : typedef QArrayData QByteArrayData;
117 :
118 : template<int N> struct QStaticByteArrayData
119 : {
120 : QByteArrayData ba;
121 : char data[N + 1];
122 :
123 : QByteArrayData *data_ptr() const
124 : {
125 : Q_ASSERT(ba.ref.isStatic());
126 : return const_cast<QByteArrayData *>(&ba);
127 : }
128 : };
129 :
130 : struct QByteArrayDataPtr
131 : {
132 : QByteArrayData *ptr;
133 : };
134 :
135 : #define Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, offset) \
136 : Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, offset)
137 : /**/
138 :
139 : #define Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER(size) \
140 : Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, sizeof(QByteArrayData)) \
141 : /**/
142 :
143 : # define QByteArrayLiteral(str) \
144 : ([]() -> QByteArray { \
145 : enum { Size = sizeof(str) - 1 }; \
146 : static const QStaticByteArrayData<Size> qbytearray_literal = { \
147 : Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER(Size), \
148 : str }; \
149 : QByteArrayDataPtr holder = { qbytearray_literal.data_ptr() }; \
150 : const QByteArray ba(holder); \
151 : return ba; \
152 : }()) \
153 : /**/
154 :
155 : class Q_CORE_EXPORT QByteArray
156 : {
157 : private:
158 : typedef QTypedArrayData<char> Data;
159 :
160 : public:
161 : enum Base64Option {
162 : Base64Encoding = 0,
163 : Base64UrlEncoding = 1,
164 :
165 : KeepTrailingEquals = 0,
166 : OmitTrailingEquals = 2
167 : };
168 : Q_DECLARE_FLAGS(Base64Options, Base64Option)
169 :
170 : inline QByteArray() Q_DECL_NOTHROW;
171 : QByteArray(const char *, int size = -1);
172 : QByteArray(int size, char c);
173 : QByteArray(int size, Qt::Initialization);
174 : inline QByteArray(const QByteArray &) Q_DECL_NOTHROW;
175 : inline ~QByteArray();
176 :
177 : QByteArray &operator=(const QByteArray &) Q_DECL_NOTHROW;
178 : QByteArray &operator=(const char *str);
179 : #ifdef Q_COMPILER_RVALUE_REFS
180 3 : inline QByteArray(QByteArray && other) Q_DECL_NOTHROW : d(other.d) { other.d = Data::sharedNull(); }
181 62 : inline QByteArray &operator=(QByteArray &&other) Q_DECL_NOTHROW
182 62 : { qSwap(d, other.d); return *this; }
183 : #endif
184 :
185 : inline void swap(QByteArray &other) Q_DECL_NOTHROW
186 : { qSwap(d, other.d); }
187 :
188 : inline int size() const;
189 : inline bool isEmpty() const;
190 : void resize(int size);
191 :
192 : QByteArray &fill(char c, int size = -1);
193 :
194 : inline int capacity() const;
195 : inline void reserve(int size);
196 : inline void squeeze();
197 :
198 : #ifndef QT_NO_CAST_FROM_BYTEARRAY
199 : inline operator const char *() const;
200 : inline operator const void *() const;
201 : #endif
202 : inline char *data();
203 : inline const char *data() const;
204 : inline const char *constData() const;
205 : inline void detach();
206 : inline bool isDetached() const;
207 : inline bool isSharedWith(const QByteArray &other) const { return d == other.d; }
208 : void clear();
209 :
210 : inline char at(int i) const;
211 : inline char operator[](int i) const;
212 : inline char operator[](uint i) const;
213 : inline QByteRef operator[](int i);
214 : inline QByteRef operator[](uint i);
215 : Q_REQUIRED_RESULT char front() const { return at(0); }
216 : Q_REQUIRED_RESULT inline QByteRef front();
217 : Q_REQUIRED_RESULT char back() const { return at(size() - 1); }
218 : Q_REQUIRED_RESULT inline QByteRef back();
219 :
220 : int indexOf(char c, int from = 0) const;
221 : int indexOf(const char *c, int from = 0) const;
222 : int indexOf(const QByteArray &a, int from = 0) const;
223 : int lastIndexOf(char c, int from = -1) const;
224 : int lastIndexOf(const char *c, int from = -1) const;
225 : int lastIndexOf(const QByteArray &a, int from = -1) const;
226 :
227 : inline bool contains(char c) const;
228 : inline bool contains(const char *a) const;
229 : inline bool contains(const QByteArray &a) const;
230 : int count(char c) const;
231 : int count(const char *a) const;
232 : int count(const QByteArray &a) const;
233 :
234 : Q_REQUIRED_RESULT QByteArray left(int len) const;
235 : Q_REQUIRED_RESULT QByteArray right(int len) const;
236 : Q_REQUIRED_RESULT QByteArray mid(int index, int len = -1) const;
237 : Q_REQUIRED_RESULT QByteArray chopped(int len) const
238 : { Q_ASSERT(len >= 0); Q_ASSERT(len <= size()); return left(size() - len); }
239 :
240 : bool startsWith(const QByteArray &a) const;
241 : bool startsWith(char c) const;
242 : bool startsWith(const char *c) const;
243 :
244 : bool endsWith(const QByteArray &a) const;
245 : bool endsWith(char c) const;
246 : bool endsWith(const char *c) const;
247 :
248 : void truncate(int pos);
249 : void chop(int n);
250 :
251 : #if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QSTRING_COMPAT_CPP) && !defined(Q_CLANG_QDOC)
252 : # if defined(Q_CC_GNU) && !defined(Q_CC_CLANG) && !defined(Q_CC_INTEL) && !QT_HAS_CPP_ATTRIBUTE(nodiscard)
253 : // required due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61941
254 : # pragma push_macro("Q_REQUIRED_RESULT")
255 : # undef Q_REQUIRED_RESULT
256 : # define Q_REQUIRED_RESULT
257 : # define Q_REQUIRED_RESULT_pushed
258 : # endif
259 : Q_REQUIRED_RESULT QByteArray toLower() const &
260 : { return toLower_helper(*this); }
261 : Q_REQUIRED_RESULT QByteArray toLower() &&
262 : { return toLower_helper(*this); }
263 : Q_REQUIRED_RESULT QByteArray toUpper() const &
264 : { return toUpper_helper(*this); }
265 : Q_REQUIRED_RESULT QByteArray toUpper() &&
266 : { return toUpper_helper(*this); }
267 : Q_REQUIRED_RESULT QByteArray trimmed() const &
268 : { return trimmed_helper(*this); }
269 0 : Q_REQUIRED_RESULT QByteArray trimmed() &&
270 0 : { return trimmed_helper(*this); }
271 : Q_REQUIRED_RESULT QByteArray simplified() const &
272 : { return simplified_helper(*this); }
273 : Q_REQUIRED_RESULT QByteArray simplified() &&
274 : { return simplified_helper(*this); }
275 : # ifdef Q_REQUIRED_RESULT_pushed
276 : # pragma pop_macro("Q_REQUIRED_RESULT")
277 : # endif
278 : #else
279 : Q_REQUIRED_RESULT QByteArray toLower() const;
280 : Q_REQUIRED_RESULT QByteArray toUpper() const;
281 : Q_REQUIRED_RESULT QByteArray trimmed() const;
282 : Q_REQUIRED_RESULT QByteArray simplified() const;
283 : #endif
284 :
285 : Q_REQUIRED_RESULT QByteArray leftJustified(int width, char fill = ' ', bool truncate = false) const;
286 : Q_REQUIRED_RESULT QByteArray rightJustified(int width, char fill = ' ', bool truncate = false) const;
287 :
288 : QByteArray &prepend(char c);
289 : inline QByteArray &prepend(int count, char c);
290 : QByteArray &prepend(const char *s);
291 : QByteArray &prepend(const char *s, int len);
292 : QByteArray &prepend(const QByteArray &a);
293 : QByteArray &append(char c);
294 : inline QByteArray &append(int count, char c);
295 : QByteArray &append(const char *s);
296 : QByteArray &append(const char *s, int len);
297 : QByteArray &append(const QByteArray &a);
298 : QByteArray &insert(int i, char c);
299 : QByteArray &insert(int i, int count, char c);
300 : QByteArray &insert(int i, const char *s);
301 : QByteArray &insert(int i, const char *s, int len);
302 : QByteArray &insert(int i, const QByteArray &a);
303 : QByteArray &remove(int index, int len);
304 : QByteArray &replace(int index, int len, const char *s);
305 : QByteArray &replace(int index, int len, const char *s, int alen);
306 : QByteArray &replace(int index, int len, const QByteArray &s);
307 : inline QByteArray &replace(char before, const char *after);
308 : QByteArray &replace(char before, const QByteArray &after);
309 : inline QByteArray &replace(const char *before, const char *after);
310 : QByteArray &replace(const char *before, int bsize, const char *after, int asize);
311 : QByteArray &replace(const QByteArray &before, const QByteArray &after);
312 : inline QByteArray &replace(const QByteArray &before, const char *after);
313 : QByteArray &replace(const char *before, const QByteArray &after);
314 : QByteArray &replace(char before, char after);
315 : inline QByteArray &operator+=(char c);
316 : inline QByteArray &operator+=(const char *s);
317 : inline QByteArray &operator+=(const QByteArray &a);
318 :
319 : QList<QByteArray> split(char sep) const;
320 :
321 : Q_REQUIRED_RESULT QByteArray repeated(int times) const;
322 :
323 : #ifndef QT_NO_CAST_TO_ASCII
324 : QT_ASCII_CAST_WARN QByteArray &append(const QString &s);
325 : QT_ASCII_CAST_WARN QByteArray &insert(int i, const QString &s);
326 : QT_ASCII_CAST_WARN QByteArray &replace(const QString &before, const char *after);
327 : QT_ASCII_CAST_WARN QByteArray &replace(char c, const QString &after);
328 : QT_ASCII_CAST_WARN QByteArray &replace(const QString &before, const QByteArray &after);
329 :
330 : QT_ASCII_CAST_WARN QByteArray &operator+=(const QString &s);
331 : QT_ASCII_CAST_WARN int indexOf(const QString &s, int from = 0) const;
332 : QT_ASCII_CAST_WARN int lastIndexOf(const QString &s, int from = -1) const;
333 : #endif
334 : #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
335 : inline QT_ASCII_CAST_WARN bool operator==(const QString &s2) const;
336 : inline QT_ASCII_CAST_WARN bool operator!=(const QString &s2) const;
337 : inline QT_ASCII_CAST_WARN bool operator<(const QString &s2) const;
338 : inline QT_ASCII_CAST_WARN bool operator>(const QString &s2) const;
339 : inline QT_ASCII_CAST_WARN bool operator<=(const QString &s2) const;
340 : inline QT_ASCII_CAST_WARN bool operator>=(const QString &s2) const;
341 : #endif
342 :
343 : short toShort(bool *ok = Q_NULLPTR, int base = 10) const;
344 : ushort toUShort(bool *ok = Q_NULLPTR, int base = 10) const;
345 : int toInt(bool *ok = Q_NULLPTR, int base = 10) const;
346 : uint toUInt(bool *ok = Q_NULLPTR, int base = 10) const;
347 : long toLong(bool *ok = Q_NULLPTR, int base = 10) const;
348 : ulong toULong(bool *ok = Q_NULLPTR, int base = 10) const;
349 : qlonglong toLongLong(bool *ok = Q_NULLPTR, int base = 10) const;
350 : qulonglong toULongLong(bool *ok = Q_NULLPTR, int base = 10) const;
351 : float toFloat(bool *ok = Q_NULLPTR) const;
352 : double toDouble(bool *ok = Q_NULLPTR) const;
353 : QByteArray toBase64(Base64Options options) const;
354 : QByteArray toBase64() const; // ### Qt6 merge with previous
355 : QByteArray toHex() const;
356 : QByteArray toHex(char separator) const; // ### Qt6 merge with previous
357 : QByteArray toPercentEncoding(const QByteArray &exclude = QByteArray(),
358 : const QByteArray &include = QByteArray(),
359 : char percent = '%') const;
360 :
361 : inline QByteArray &setNum(short, int base = 10);
362 : inline QByteArray &setNum(ushort, int base = 10);
363 : inline QByteArray &setNum(int, int base = 10);
364 : inline QByteArray &setNum(uint, int base = 10);
365 : QByteArray &setNum(qlonglong, int base = 10);
366 : QByteArray &setNum(qulonglong, int base = 10);
367 : inline QByteArray &setNum(float, char f = 'g', int prec = 6);
368 : QByteArray &setNum(double, char f = 'g', int prec = 6);
369 : QByteArray &setRawData(const char *a, uint n); // ### Qt 6: use an int
370 :
371 : Q_REQUIRED_RESULT static QByteArray number(int, int base = 10);
372 : Q_REQUIRED_RESULT static QByteArray number(uint, int base = 10);
373 : Q_REQUIRED_RESULT static QByteArray number(qlonglong, int base = 10);
374 : Q_REQUIRED_RESULT static QByteArray number(qulonglong, int base = 10);
375 : Q_REQUIRED_RESULT static QByteArray number(double, char f = 'g', int prec = 6);
376 : Q_REQUIRED_RESULT static QByteArray fromRawData(const char *, int size);
377 : Q_REQUIRED_RESULT static QByteArray fromBase64(const QByteArray &base64, Base64Options options);
378 : Q_REQUIRED_RESULT static QByteArray fromBase64(const QByteArray &base64); // ### Qt6 merge with previous
379 : Q_REQUIRED_RESULT static QByteArray fromHex(const QByteArray &hexEncoded);
380 : Q_REQUIRED_RESULT static QByteArray fromPercentEncoding(const QByteArray &pctEncoded, char percent = '%');
381 :
382 : #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
383 : static QByteArray fromCFData(CFDataRef data);
384 : static QByteArray fromRawCFData(CFDataRef data);
385 : CFDataRef toCFData() const Q_DECL_CF_RETURNS_RETAINED;
386 : CFDataRef toRawCFData() const Q_DECL_CF_RETURNS_RETAINED;
387 : static QByteArray fromNSData(const NSData *data);
388 : static QByteArray fromRawNSData(const NSData *data);
389 : NSData *toNSData() const Q_DECL_NS_RETURNS_AUTORELEASED;
390 : NSData *toRawNSData() const Q_DECL_NS_RETURNS_AUTORELEASED;
391 : #endif
392 :
393 : typedef char *iterator;
394 : typedef const char *const_iterator;
395 : typedef iterator Iterator;
396 : typedef const_iterator ConstIterator;
397 : typedef std::reverse_iterator<iterator> reverse_iterator;
398 : typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
399 : inline iterator begin();
400 : inline const_iterator begin() const;
401 : inline const_iterator cbegin() const;
402 : inline const_iterator constBegin() const;
403 : inline iterator end();
404 : inline const_iterator end() const;
405 : inline const_iterator cend() const;
406 : inline const_iterator constEnd() const;
407 : reverse_iterator rbegin() { return reverse_iterator(end()); }
408 : reverse_iterator rend() { return reverse_iterator(begin()); }
409 : const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
410 : const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
411 : const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); }
412 : const_reverse_iterator crend() const { return const_reverse_iterator(begin()); }
413 :
414 : // stl compatibility
415 : typedef int size_type;
416 : typedef qptrdiff difference_type;
417 : typedef const char & const_reference;
418 : typedef char & reference;
419 : typedef char *pointer;
420 : typedef const char *const_pointer;
421 : typedef char value_type;
422 : inline void push_back(char c);
423 : inline void push_back(const char *c);
424 : inline void push_back(const QByteArray &a);
425 : inline void push_front(char c);
426 : inline void push_front(const char *c);
427 : inline void push_front(const QByteArray &a);
428 : void shrink_to_fit() { squeeze(); }
429 :
430 : static inline QByteArray fromStdString(const std::string &s);
431 : inline std::string toStdString() const;
432 :
433 : inline int count() const { return d->size; }
434 0 : int length() const { return d->size; }
435 : bool isNull() const;
436 :
437 : inline QByteArray(QByteArrayDataPtr dd)
438 : : d(static_cast<Data *>(dd.ptr))
439 : {
440 : }
441 :
442 : private:
443 : operator QNoImplicitBoolCast() const;
444 : Data *d;
445 : void reallocData(uint alloc, Data::AllocationOptions options);
446 : void expand(int i);
447 : QByteArray nulTerminated() const;
448 :
449 : static QByteArray toLower_helper(const QByteArray &a);
450 : static QByteArray toLower_helper(QByteArray &a);
451 : static QByteArray toUpper_helper(const QByteArray &a);
452 : static QByteArray toUpper_helper(QByteArray &a);
453 : static QByteArray trimmed_helper(const QByteArray &a);
454 : static QByteArray trimmed_helper(QByteArray &a);
455 : static QByteArray simplified_helper(const QByteArray &a);
456 : static QByteArray simplified_helper(QByteArray &a);
457 :
458 : friend class QByteRef;
459 : friend class QString;
460 : friend Q_CORE_EXPORT QByteArray qUncompress(const uchar *data, int nbytes);
461 : public:
462 : typedef Data * DataPtr;
463 : inline DataPtr &data_ptr() { return d; }
464 : };
465 :
466 : Q_DECLARE_OPERATORS_FOR_FLAGS(QByteArray::Base64Options)
467 :
468 176 : inline QByteArray::QByteArray() Q_DECL_NOTHROW : d(Data::sharedNull()) { }
469 340 : inline QByteArray::~QByteArray() { if (!d->ref.deref()) Data::deallocate(d); }
470 444 : inline int QByteArray::size() const
471 444 : { return d->size; }
472 :
473 : inline char QByteArray::at(int i) const
474 : { Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; }
475 : inline char QByteArray::operator[](int i) const
476 : { Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; }
477 : inline char QByteArray::operator[](uint i) const
478 : { Q_ASSERT(i < uint(size())); return d->data()[i]; }
479 :
480 0 : inline bool QByteArray::isEmpty() const
481 0 : { return d->size == 0; }
482 : #ifndef QT_NO_CAST_FROM_BYTEARRAY
483 : inline QByteArray::operator const char *() const
484 : { return d->data(); }
485 0 : inline QByteArray::operator const void *() const
486 0 : { return d->data(); }
487 : #endif
488 125 : inline char *QByteArray::data()
489 125 : { detach(); return d->data(); }
490 33 : inline const char *QByteArray::data() const
491 33 : { return d->data(); }
492 120 : inline const char *QByteArray::constData() const
493 120 : { return d->data(); }
494 125 : inline void QByteArray::detach()
495 125 : { if (d->ref.isShared() || (d->offset != sizeof(QByteArrayData))) reallocData(uint(d->size) + 1u, d->detachFlags()); }
496 : inline bool QByteArray::isDetached() const
497 : { return !d->ref.isShared(); }
498 81 : inline QByteArray::QByteArray(const QByteArray &a) Q_DECL_NOTHROW : d(a.d)
499 81 : { d->ref.ref(); }
500 :
501 : inline int QByteArray::capacity() const
502 : { return d->alloc ? d->alloc - 1 : 0; }
503 :
504 : inline void QByteArray::reserve(int asize)
505 : {
506 : if (d->ref.isShared() || uint(asize) + 1u > d->alloc) {
507 : reallocData(qMax(uint(size()), uint(asize)) + 1u, d->detachFlags() | Data::CapacityReserved);
508 : } else {
509 : // cannot set unconditionally, since d could be the shared_null or
510 : // otherwise static
511 : d->capacityReserved = true;
512 : }
513 : }
514 :
515 : inline void QByteArray::squeeze()
516 : {
517 : if (d->ref.isShared() || uint(d->size) + 1u < d->alloc) {
518 : reallocData(uint(d->size) + 1u, d->detachFlags() & ~Data::CapacityReserved);
519 : } else {
520 : // cannot set unconditionally, since d could be shared_null or
521 : // otherwise static.
522 : d->capacityReserved = false;
523 : }
524 : }
525 :
526 : class Q_CORE_EXPORT QByteRef {
527 : QByteArray &a;
528 : int i;
529 : inline QByteRef(QByteArray &array, int idx)
530 : : a(array),i(idx) {}
531 : friend class QByteArray;
532 : public:
533 : inline operator char() const
534 : { return i < a.d->size ? a.d->data()[i] : char(0); }
535 : inline QByteRef &operator=(char c)
536 : { if (i >= a.d->size) a.expand(i); else a.detach();
537 : a.d->data()[i] = c; return *this; }
538 : inline QByteRef &operator=(const QByteRef &c)
539 : { if (i >= a.d->size) a.expand(i); else a.detach();
540 : a.d->data()[i] = c.a.d->data()[c.i]; return *this; }
541 : inline bool operator==(char c) const
542 : { return a.d->data()[i] == c; }
543 : inline bool operator!=(char c) const
544 : { return a.d->data()[i] != c; }
545 : inline bool operator>(char c) const
546 : { return a.d->data()[i] > c; }
547 : inline bool operator>=(char c) const
548 : { return a.d->data()[i] >= c; }
549 : inline bool operator<(char c) const
550 : { return a.d->data()[i] < c; }
551 : inline bool operator<=(char c) const
552 : { return a.d->data()[i] <= c; }
553 : };
554 :
555 : inline QByteRef QByteArray::operator[](int i)
556 : { Q_ASSERT(i >= 0); return QByteRef(*this, i); }
557 : inline QByteRef QByteArray::operator[](uint i)
558 : { return QByteRef(*this, i); }
559 : inline QByteRef QByteArray::front() { return operator[](0); }
560 : inline QByteRef QByteArray::back() { return operator[](size() - 1); }
561 : inline QByteArray::iterator QByteArray::begin()
562 : { detach(); return d->data(); }
563 : inline QByteArray::const_iterator QByteArray::begin() const
564 : { return d->data(); }
565 : inline QByteArray::const_iterator QByteArray::cbegin() const
566 : { return d->data(); }
567 : inline QByteArray::const_iterator QByteArray::constBegin() const
568 : { return d->data(); }
569 : inline QByteArray::iterator QByteArray::end()
570 : { detach(); return d->data() + d->size; }
571 : inline QByteArray::const_iterator QByteArray::end() const
572 : { return d->data() + d->size; }
573 : inline QByteArray::const_iterator QByteArray::cend() const
574 : { return d->data() + d->size; }
575 : inline QByteArray::const_iterator QByteArray::constEnd() const
576 : { return d->data() + d->size; }
577 : inline QByteArray &QByteArray::append(int n, char ch)
578 : { return insert(d->size, n, ch); }
579 : inline QByteArray &QByteArray::prepend(int n, char ch)
580 : { return insert(0, n, ch); }
581 : inline QByteArray &QByteArray::operator+=(char c)
582 : { return append(c); }
583 : inline QByteArray &QByteArray::operator+=(const char *s)
584 : { return append(s); }
585 : inline QByteArray &QByteArray::operator+=(const QByteArray &a)
586 : { return append(a); }
587 : inline void QByteArray::push_back(char c)
588 : { append(c); }
589 : inline void QByteArray::push_back(const char *c)
590 : { append(c); }
591 : inline void QByteArray::push_back(const QByteArray &a)
592 : { append(a); }
593 : inline void QByteArray::push_front(char c)
594 : { prepend(c); }
595 : inline void QByteArray::push_front(const char *c)
596 : { prepend(c); }
597 : inline void QByteArray::push_front(const QByteArray &a)
598 : { prepend(a); }
599 : inline bool QByteArray::contains(const QByteArray &a) const
600 : { return indexOf(a) != -1; }
601 : inline bool QByteArray::contains(char c) const
602 : { return indexOf(c) != -1; }
603 6 : inline bool operator==(const QByteArray &a1, const QByteArray &a2) Q_DECL_NOTHROW
604 6 : { return (a1.size() == a2.size()) && (memcmp(a1.constData(), a2.constData(), a1.size())==0); }
605 : inline bool operator==(const QByteArray &a1, const char *a2) Q_DECL_NOTHROW
606 : { return a2 ? qstrcmp(a1,a2) == 0 : a1.isEmpty(); }
607 : inline bool operator==(const char *a1, const QByteArray &a2) Q_DECL_NOTHROW
608 : { return a1 ? qstrcmp(a1,a2) == 0 : a2.isEmpty(); }
609 : inline bool operator!=(const QByteArray &a1, const QByteArray &a2) Q_DECL_NOTHROW
610 : { return !(a1==a2); }
611 : inline bool operator!=(const QByteArray &a1, const char *a2) Q_DECL_NOTHROW
612 : { return a2 ? qstrcmp(a1,a2) != 0 : !a1.isEmpty(); }
613 : inline bool operator!=(const char *a1, const QByteArray &a2) Q_DECL_NOTHROW
614 : { return a1 ? qstrcmp(a1,a2) != 0 : !a2.isEmpty(); }
615 : inline bool operator<(const QByteArray &a1, const QByteArray &a2) Q_DECL_NOTHROW
616 : { return qstrcmp(a1, a2) < 0; }
617 : inline bool operator<(const QByteArray &a1, const char *a2) Q_DECL_NOTHROW
618 : { return qstrcmp(a1, a2) < 0; }
619 : inline bool operator<(const char *a1, const QByteArray &a2) Q_DECL_NOTHROW
620 : { return qstrcmp(a1, a2) < 0; }
621 : inline bool operator<=(const QByteArray &a1, const QByteArray &a2) Q_DECL_NOTHROW
622 : { return qstrcmp(a1, a2) <= 0; }
623 : inline bool operator<=(const QByteArray &a1, const char *a2) Q_DECL_NOTHROW
624 : { return qstrcmp(a1, a2) <= 0; }
625 : inline bool operator<=(const char *a1, const QByteArray &a2) Q_DECL_NOTHROW
626 : { return qstrcmp(a1, a2) <= 0; }
627 : inline bool operator>(const QByteArray &a1, const QByteArray &a2) Q_DECL_NOTHROW
628 : { return qstrcmp(a1, a2) > 0; }
629 : inline bool operator>(const QByteArray &a1, const char *a2) Q_DECL_NOTHROW
630 : { return qstrcmp(a1, a2) > 0; }
631 : inline bool operator>(const char *a1, const QByteArray &a2) Q_DECL_NOTHROW
632 : { return qstrcmp(a1, a2) > 0; }
633 : inline bool operator>=(const QByteArray &a1, const QByteArray &a2) Q_DECL_NOTHROW
634 : { return qstrcmp(a1, a2) >= 0; }
635 : inline bool operator>=(const QByteArray &a1, const char *a2) Q_DECL_NOTHROW
636 : { return qstrcmp(a1, a2) >= 0; }
637 : inline bool operator>=(const char *a1, const QByteArray &a2) Q_DECL_NOTHROW
638 : { return qstrcmp(a1, a2) >= 0; }
639 : #if !defined(QT_USE_QSTRINGBUILDER)
640 : inline const QByteArray operator+(const QByteArray &a1, const QByteArray &a2)
641 : { return QByteArray(a1) += a2; }
642 : inline const QByteArray operator+(const QByteArray &a1, const char *a2)
643 : { return QByteArray(a1) += a2; }
644 : inline const QByteArray operator+(const QByteArray &a1, char a2)
645 : { return QByteArray(a1) += a2; }
646 : inline const QByteArray operator+(const char *a1, const QByteArray &a2)
647 : { return QByteArray(a1) += a2; }
648 : inline const QByteArray operator+(char a1, const QByteArray &a2)
649 : { return QByteArray(&a1, 1) += a2; }
650 : #endif // QT_USE_QSTRINGBUILDER
651 : inline bool QByteArray::contains(const char *c) const
652 : { return indexOf(c) != -1; }
653 : inline QByteArray &QByteArray::replace(char before, const char *c)
654 : { return replace(&before, 1, c, qstrlen(c)); }
655 : inline QByteArray &QByteArray::replace(const QByteArray &before, const char *c)
656 : { return replace(before.constData(), before.size(), c, qstrlen(c)); }
657 : inline QByteArray &QByteArray::replace(const char *before, const char *after)
658 : { return replace(before, qstrlen(before), after, qstrlen(after)); }
659 :
660 : inline QByteArray &QByteArray::setNum(short n, int base)
661 : { return base == 10 ? setNum(qlonglong(n), base) : setNum(qulonglong(ushort(n)), base); }
662 : inline QByteArray &QByteArray::setNum(ushort n, int base)
663 : { return setNum(qulonglong(n), base); }
664 : inline QByteArray &QByteArray::setNum(int n, int base)
665 : { return base == 10 ? setNum(qlonglong(n), base) : setNum(qulonglong(uint(n)), base); }
666 : inline QByteArray &QByteArray::setNum(uint n, int base)
667 : { return setNum(qulonglong(n), base); }
668 : inline QByteArray &QByteArray::setNum(float n, char f, int prec)
669 : { return setNum(double(n),f,prec); }
670 :
671 0 : inline std::string QByteArray::toStdString() const
672 0 : { return std::string(constData(), length()); }
673 :
674 : inline QByteArray QByteArray::fromStdString(const std::string &s)
675 : { return QByteArray(s.data(), int(s.size())); }
676 :
677 : #if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE))
678 : Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QByteArray &);
679 : Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QByteArray &);
680 : #endif
681 :
682 : #ifndef QT_NO_COMPRESS
683 : Q_CORE_EXPORT QByteArray qCompress(const uchar* data, int nbytes, int compressionLevel = -1);
684 : Q_CORE_EXPORT QByteArray qUncompress(const uchar* data, int nbytes);
685 : inline QByteArray qCompress(const QByteArray& data, int compressionLevel = -1)
686 : { return qCompress(reinterpret_cast<const uchar *>(data.constData()), data.size(), compressionLevel); }
687 : inline QByteArray qUncompress(const QByteArray& data)
688 : { return qUncompress(reinterpret_cast<const uchar*>(data.constData()), data.size()); }
689 : #endif
690 :
691 : Q_DECLARE_SHARED(QByteArray)
692 :
693 : QT_END_NAMESPACE
694 :
695 : #endif // QBYTEARRAY_H
|