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