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);
102
103// implemented in qvsnprintf.cpp
104Q_CORE_EXPORT int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap);
105Q_CORE_EXPORT int qsnprintf(char *str, size_t n, const char *fmt, ...);
106
107// qChecksum: Internet checksum
108Q_CORE_EXPORT quint16 qChecksum(const char *s, uint len); // ### Qt 6: Remove
109Q_CORE_EXPORT quint16 qChecksum(const char *s, uint len, Qt::ChecksumType standard); // ### Qt 6: Use Qt::ChecksumType standard = Qt::ChecksumIso3309
110
111class QByteRef;
112class QString;
113class QDataStream;
114template <typename T> class QList;
115
116typedef QArrayData QByteArrayData;
117
118template<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
130struct 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
155class Q_CORE_EXPORT QByteArray
156{
157private:
158 typedef QTypedArrayData<char> Data;
159
160public:
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 inline QByteArray(QByteArray && other) Q_DECL_NOTHROW : d(other.d) { other.d = Data::sharedNull(); }
181 inline QByteArray &operator=(QByteArray &&other) Q_DECL_NOTHROW
182 { 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 bool isUpper() const;
249 bool isLower() const;
250
251 void truncate(int pos);
252 void chop(int n);
253
254#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QSTRING_COMPAT_CPP) && !defined(Q_CLANG_QDOC)
255# if defined(Q_CC_GNU) && !defined(Q_CC_CLANG) && !defined(Q_CC_INTEL) && !QT_HAS_CPP_ATTRIBUTE(nodiscard)
256 // required due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61941
257# pragma push_macro("Q_REQUIRED_RESULT")
258# undef Q_REQUIRED_RESULT
259# define Q_REQUIRED_RESULT
260# define Q_REQUIRED_RESULT_pushed
261# endif
262 Q_REQUIRED_RESULT QByteArray toLower() const &
263 { return toLower_helper(*this); }
264 Q_REQUIRED_RESULT QByteArray toLower() &&
265 { return toLower_helper(*this); }
266 Q_REQUIRED_RESULT QByteArray toUpper() const &
267 { return toUpper_helper(*this); }
268 Q_REQUIRED_RESULT QByteArray toUpper() &&
269 { return toUpper_helper(*this); }
270 Q_REQUIRED_RESULT QByteArray trimmed() const &
271 { return trimmed_helper(*this); }
272 Q_REQUIRED_RESULT QByteArray trimmed() &&
273 { return trimmed_helper(*this); }
274 Q_REQUIRED_RESULT QByteArray simplified() const &
275 { return simplified_helper(*this); }
276 Q_REQUIRED_RESULT QByteArray simplified() &&
277 { return simplified_helper(*this); }
278# ifdef Q_REQUIRED_RESULT_pushed
279# pragma pop_macro("Q_REQUIRED_RESULT")
280# endif
281#else
282 Q_REQUIRED_RESULT QByteArray toLower() const;
283 Q_REQUIRED_RESULT QByteArray toUpper() const;
284 Q_REQUIRED_RESULT QByteArray trimmed() const;
285 Q_REQUIRED_RESULT QByteArray simplified() const;
286#endif
287
288 Q_REQUIRED_RESULT QByteArray leftJustified(int width, char fill = ' ', bool truncate = false) const;
289 Q_REQUIRED_RESULT QByteArray rightJustified(int width, char fill = ' ', bool truncate = false) const;
290
291 QByteArray &prepend(char c);
292 inline QByteArray &prepend(int count, char c);
293 QByteArray &prepend(const char *s);
294 QByteArray &prepend(const char *s, int len);
295 QByteArray &prepend(const QByteArray &a);
296 QByteArray &append(char c);
297 inline QByteArray &append(int count, char c);
298 QByteArray &append(const char *s);
299 QByteArray &append(const char *s, int len);
300 QByteArray &append(const QByteArray &a);
301 QByteArray &insert(int i, char c);
302 QByteArray &insert(int i, int count, char c);
303 QByteArray &insert(int i, const char *s);
304 QByteArray &insert(int i, const char *s, int len);
305 QByteArray &insert(int i, const QByteArray &a);
306 QByteArray &remove(int index, int len);
307 QByteArray &replace(int index, int len, const char *s);
308 QByteArray &replace(int index, int len, const char *s, int alen);
309 QByteArray &replace(int index, int len, const QByteArray &s);
310 inline QByteArray &replace(char before, const char *after);
311 QByteArray &replace(char before, const QByteArray &after);
312 inline QByteArray &replace(const char *before, const char *after);
313 QByteArray &replace(const char *before, int bsize, const char *after, int asize);
314 QByteArray &replace(const QByteArray &before, const QByteArray &after);
315 inline QByteArray &replace(const QByteArray &before, const char *after);
316 QByteArray &replace(const char *before, const QByteArray &after);
317 QByteArray &replace(char before, char after);
318 inline QByteArray &operator+=(char c);
319 inline QByteArray &operator+=(const char *s);
320 inline QByteArray &operator+=(const QByteArray &a);
321
322 QList<QByteArray> split(char sep) const;
323
324 Q_REQUIRED_RESULT QByteArray repeated(int times) const;
325
326#ifndef QT_NO_CAST_TO_ASCII
327 QT_ASCII_CAST_WARN QByteArray &append(const QString &s);
328 QT_ASCII_CAST_WARN QByteArray &insert(int i, const QString &s);
329 QT_ASCII_CAST_WARN QByteArray &replace(const QString &before, const char *after);
330 QT_ASCII_CAST_WARN QByteArray &replace(char c, const QString &after);
331 QT_ASCII_CAST_WARN QByteArray &replace(const QString &before, const QByteArray &after);
332
333 QT_ASCII_CAST_WARN QByteArray &operator+=(const QString &s);
334 QT_ASCII_CAST_WARN int indexOf(const QString &s, int from = 0) const;
335 QT_ASCII_CAST_WARN int lastIndexOf(const QString &s, int from = -1) const;
336#endif
337#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
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 inline QT_ASCII_CAST_WARN bool operator>(const QString &s2) const;
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#endif
345
346 short toShort(bool *ok = nullptr, int base = 10) const;
347 ushort toUShort(bool *ok = nullptr, int base = 10) const;
348 int toInt(bool *ok = nullptr, int base = 10) const;
349 uint toUInt(bool *ok = nullptr, int base = 10) const;
350 long toLong(bool *ok = nullptr, int base = 10) const;
351 ulong toULong(bool *ok = nullptr, int base = 10) const;
352 qlonglong toLongLong(bool *ok = nullptr, int base = 10) const;
353 qulonglong toULongLong(bool *ok = nullptr, int base = 10) const;
354 float toFloat(bool *ok = nullptr) const;
355 double toDouble(bool *ok = nullptr) const;
356 QByteArray toBase64(Base64Options options) const;
357 QByteArray toBase64() const; // ### Qt6 merge with previous
358 QByteArray toHex() const;
359 QByteArray toHex(char separator) const; // ### Qt6 merge with previous
360 QByteArray toPercentEncoding(const QByteArray &exclude = QByteArray(),
361 const QByteArray &include = QByteArray(),
362 char percent = '%') const;
363
364 inline QByteArray &setNum(short, int base = 10);
365 inline QByteArray &setNum(ushort, int base = 10);
366 inline QByteArray &setNum(int, int base = 10);
367 inline QByteArray &setNum(uint, int base = 10);
368 QByteArray &setNum(qlonglong, int base = 10);
369 QByteArray &setNum(qulonglong, int base = 10);
370 inline QByteArray &setNum(float, char f = 'g', int prec = 6);
371 QByteArray &setNum(double, char f = 'g', int prec = 6);
372 QByteArray &setRawData(const char *a, uint n); // ### Qt 6: use an int
373
374 Q_REQUIRED_RESULT static QByteArray number(int, int base = 10);
375 Q_REQUIRED_RESULT static QByteArray number(uint, int base = 10);
376 Q_REQUIRED_RESULT static QByteArray number(qlonglong, int base = 10);
377 Q_REQUIRED_RESULT static QByteArray number(qulonglong, int base = 10);
378 Q_REQUIRED_RESULT static QByteArray number(double, char f = 'g', int prec = 6);
379 Q_REQUIRED_RESULT static QByteArray fromRawData(const char *, int size);
380 Q_REQUIRED_RESULT static QByteArray fromBase64(const QByteArray &base64, Base64Options options);
381 Q_REQUIRED_RESULT static QByteArray fromBase64(const QByteArray &base64); // ### Qt6 merge with previous
382 Q_REQUIRED_RESULT static QByteArray fromHex(const QByteArray &hexEncoded);
383 Q_REQUIRED_RESULT static QByteArray fromPercentEncoding(const QByteArray &pctEncoded, char percent = '%');
384
385#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
386 static QByteArray fromCFData(CFDataRef data);
387 static QByteArray fromRawCFData(CFDataRef data);
388 CFDataRef toCFData() const Q_DECL_CF_RETURNS_RETAINED;
389 CFDataRef toRawCFData() const Q_DECL_CF_RETURNS_RETAINED;
390 static QByteArray fromNSData(const NSData *data);
391 static QByteArray fromRawNSData(const NSData *data);
392 NSData *toNSData() const Q_DECL_NS_RETURNS_AUTORELEASED;
393 NSData *toRawNSData() const Q_DECL_NS_RETURNS_AUTORELEASED;
394#endif
395
396 typedef char *iterator;
397 typedef const char *const_iterator;
398 typedef iterator Iterator;
399 typedef const_iterator ConstIterator;
400 typedef std::reverse_iterator<iterator> reverse_iterator;
401 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
402 inline iterator begin();
403 inline const_iterator begin() const;
404 inline const_iterator cbegin() const;
405 inline const_iterator constBegin() const;
406 inline iterator end();
407 inline const_iterator end() const;
408 inline const_iterator cend() const;
409 inline const_iterator constEnd() const;
410 reverse_iterator rbegin() { return reverse_iterator(end()); }
411 reverse_iterator rend() { return reverse_iterator(begin()); }
412 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
413 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
414 const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); }
415 const_reverse_iterator crend() const { return const_reverse_iterator(begin()); }
416
417 // stl compatibility
418 typedef int size_type;
419 typedef qptrdiff difference_type;
420 typedef const char & const_reference;
421 typedef char & reference;
422 typedef char *pointer;
423 typedef const char *const_pointer;
424 typedef char value_type;
425 inline void push_back(char c);
426 inline void push_back(const char *c);
427 inline void push_back(const QByteArray &a);
428 inline void push_front(char c);
429 inline void push_front(const char *c);
430 inline void push_front(const QByteArray &a);
431 void shrink_to_fit() { squeeze(); }
432
433 static inline QByteArray fromStdString(const std::string &s);
434 inline std::string toStdString() const;
435
436 inline int count() const { return d->size; }
437 int length() const { return d->size; }
438 bool isNull() const;
439
440 inline QByteArray(QByteArrayDataPtr dd)
441 : d(static_cast<Data *>(dd.ptr))
442 {
443 }
444
445private:
446 operator QNoImplicitBoolCast() const;
447 Data *d;
448 void reallocData(uint alloc, Data::AllocationOptions options);
449 void expand(int i);
450 QByteArray nulTerminated() const;
451
452 static QByteArray toLower_helper(const QByteArray &a);
453 static QByteArray toLower_helper(QByteArray &a);
454 static QByteArray toUpper_helper(const QByteArray &a);
455 static QByteArray toUpper_helper(QByteArray &a);
456 static QByteArray trimmed_helper(const QByteArray &a);
457 static QByteArray trimmed_helper(QByteArray &a);
458 static QByteArray simplified_helper(const QByteArray &a);
459 static QByteArray simplified_helper(QByteArray &a);
460
461 friend class QByteRef;
462 friend class QString;
463 friend Q_CORE_EXPORT QByteArray qUncompress(const uchar *data, int nbytes);
464public:
465 typedef Data * DataPtr;
466 inline DataPtr &data_ptr() { return d; }
467};
468
469Q_DECLARE_OPERATORS_FOR_FLAGS(QByteArray::Base64Options)
470
471inline QByteArray::QByteArray() Q_DECL_NOTHROW : d(Data::sharedNull()) { }
472inline QByteArray::~QByteArray() { if (!d->ref.deref()) Data::deallocate(d); }
473inline int QByteArray::size() const
474{ return d->size; }
475
476inline char QByteArray::at(int i) const
477{ Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; }
478inline char QByteArray::operator[](int i) const
479{ Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; }
480inline char QByteArray::operator[](uint i) const
481{ Q_ASSERT(i < uint(size())); return d->data()[i]; }
482
483inline bool QByteArray::isEmpty() const
484{ return d->size == 0; }
485#ifndef QT_NO_CAST_FROM_BYTEARRAY
486inline QByteArray::operator const char *() const
487{ return d->data(); }
488inline QByteArray::operator const void *() const
489{ return d->data(); }
490#endif
491inline char *QByteArray::data()
492{ detach(); return d->data(); }
493inline const char *QByteArray::data() const
494{ return d->data(); }
495inline const char *QByteArray::constData() const
496{ return d->data(); }
497inline void QByteArray::detach()
498{ if (d->ref.isShared() || (d->offset != sizeof(QByteArrayData))) reallocData(uint(d->size) + 1u, d->detachFlags()); }
499inline bool QByteArray::isDetached() const
500{ return !d->ref.isShared(); }
501inline QByteArray::QByteArray(const QByteArray &a) Q_DECL_NOTHROW : d(a.d)
502{ d->ref.ref(); }
503
504inline int QByteArray::capacity() const
505{ return d->alloc ? d->alloc - 1 : 0; }
506
507inline void QByteArray::reserve(int asize)
508{
509 if (d->ref.isShared() || uint(asize) + 1u > d->alloc) {
510 reallocData(qMax(uint(size()), uint(asize)) + 1u, d->detachFlags() | Data::CapacityReserved);
511 } else {
512 // cannot set unconditionally, since d could be the shared_null or
513 // otherwise static
514 d->capacityReserved = true;
515 }
516}
517
518inline void QByteArray::squeeze()
519{
520 if (d->ref.isShared() || uint(d->size) + 1u < d->alloc) {
521 reallocData(uint(d->size) + 1u, d->detachFlags() & ~Data::CapacityReserved);
522 } else {
523 // cannot set unconditionally, since d could be shared_null or
524 // otherwise static.
525 d->capacityReserved = false;
526 }
527}
528
529class Q_CORE_EXPORT QByteRef {
530 QByteArray &a;
531 int i;
532 inline QByteRef(QByteArray &array, int idx)
533 : a(array),i(idx) {}
534 friend class QByteArray;
535public:
536 inline operator char() const
537 { return i < a.d->size ? a.d->data()[i] : char(0); }
538 inline QByteRef &operator=(char c)
539 { if (i >= a.d->size) a.expand(i); else a.detach();
540 a.d->data()[i] = c; return *this; }
541 inline QByteRef &operator=(const QByteRef &c)
542 { if (i >= a.d->size) a.expand(i); else a.detach();
543 a.d->data()[i] = c.a.d->data()[c.i]; return *this; }
544 inline bool operator==(char c) const
545 { return a.d->data()[i] == c; }
546 inline bool operator!=(char c) const
547 { return a.d->data()[i] != c; }
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};
557
558inline QByteRef QByteArray::operator[](int i)
559{ Q_ASSERT(i >= 0); return QByteRef(*this, i); }
560inline QByteRef QByteArray::operator[](uint i)
561{ return QByteRef(*this, i); }
562inline QByteRef QByteArray::front() { return operator[](0); }
563inline QByteRef QByteArray::back() { return operator[](size() - 1); }
564inline QByteArray::iterator QByteArray::begin()
565{ detach(); return d->data(); }
566inline QByteArray::const_iterator QByteArray::begin() const
567{ return d->data(); }
568inline QByteArray::const_iterator QByteArray::cbegin() const
569{ return d->data(); }
570inline QByteArray::const_iterator QByteArray::constBegin() const
571{ return d->data(); }
572inline QByteArray::iterator QByteArray::end()
573{ detach(); return d->data() + d->size; }
574inline QByteArray::const_iterator QByteArray::end() const
575{ return d->data() + d->size; }
576inline QByteArray::const_iterator QByteArray::cend() const
577{ return d->data() + d->size; }
578inline QByteArray::const_iterator QByteArray::constEnd() const
579{ return d->data() + d->size; }
580inline QByteArray &QByteArray::append(int n, char ch)
581{ return insert(d->size, n, ch); }
582inline QByteArray &QByteArray::prepend(int n, char ch)
583{ return insert(0, n, ch); }
584inline QByteArray &QByteArray::operator+=(char c)
585{ return append(c); }
586inline QByteArray &QByteArray::operator+=(const char *s)
587{ return append(s); }
588inline QByteArray &QByteArray::operator+=(const QByteArray &a)
589{ return append(a); }
590inline void QByteArray::push_back(char c)
591{ append(c); }
592inline void QByteArray::push_back(const char *c)
593{ append(c); }
594inline void QByteArray::push_back(const QByteArray &a)
595{ append(a); }
596inline void QByteArray::push_front(char c)
597{ prepend(c); }
598inline void QByteArray::push_front(const char *c)
599{ prepend(c); }
600inline void QByteArray::push_front(const QByteArray &a)
601{ prepend(a); }
602inline bool QByteArray::contains(const QByteArray &a) const
603{ return indexOf(a) != -1; }
604inline bool QByteArray::contains(char c) const
605{ return indexOf(c) != -1; }
606inline bool operator==(const QByteArray &a1, const QByteArray &a2) Q_DECL_NOTHROW
607{ return (a1.size() == a2.size()) && (memcmp(a1.constData(), a2.constData(), a1.size())==0); }
608inline bool operator==(const QByteArray &a1, const char *a2) Q_DECL_NOTHROW
609{ return a2 ? qstrcmp(a1,a2) == 0 : a1.isEmpty(); }
610inline bool operator==(const char *a1, const QByteArray &a2) Q_DECL_NOTHROW
611{ return a1 ? qstrcmp(a1,a2) == 0 : a2.isEmpty(); }
612inline bool operator!=(const QByteArray &a1, const QByteArray &a2) Q_DECL_NOTHROW
613{ return !(a1==a2); }
614inline bool operator!=(const QByteArray &a1, const char *a2) Q_DECL_NOTHROW
615{ return a2 ? qstrcmp(a1,a2) != 0 : !a1.isEmpty(); }
616inline bool operator!=(const char *a1, const QByteArray &a2) Q_DECL_NOTHROW
617{ return a1 ? qstrcmp(a1,a2) != 0 : !a2.isEmpty(); }
618inline bool operator<(const QByteArray &a1, const QByteArray &a2) Q_DECL_NOTHROW
619{ return qstrcmp(a1, a2) < 0; }
620 inline bool operator<(const QByteArray &a1, const char *a2) Q_DECL_NOTHROW
621{ return qstrcmp(a1, a2) < 0; }
622inline bool operator<(const char *a1, const QByteArray &a2) Q_DECL_NOTHROW
623{ return qstrcmp(a1, a2) < 0; }
624inline bool operator<=(const QByteArray &a1, const QByteArray &a2) Q_DECL_NOTHROW
625{ return qstrcmp(a1, a2) <= 0; }
626inline bool operator<=(const QByteArray &a1, const char *a2) Q_DECL_NOTHROW
627{ return qstrcmp(a1, a2) <= 0; }
628inline bool operator<=(const char *a1, const QByteArray &a2) Q_DECL_NOTHROW
629{ return qstrcmp(a1, a2) <= 0; }
630inline bool operator>(const QByteArray &a1, const QByteArray &a2) Q_DECL_NOTHROW
631{ return qstrcmp(a1, a2) > 0; }
632inline bool operator>(const QByteArray &a1, const char *a2) Q_DECL_NOTHROW
633{ return qstrcmp(a1, a2) > 0; }
634inline bool operator>(const char *a1, const QByteArray &a2) Q_DECL_NOTHROW
635{ return qstrcmp(a1, a2) > 0; }
636inline bool operator>=(const QByteArray &a1, const QByteArray &a2) Q_DECL_NOTHROW
637{ return qstrcmp(a1, a2) >= 0; }
638inline bool operator>=(const QByteArray &a1, const char *a2) Q_DECL_NOTHROW
639{ return qstrcmp(a1, a2) >= 0; }
640inline bool operator>=(const char *a1, const QByteArray &a2) Q_DECL_NOTHROW
641{ return qstrcmp(a1, a2) >= 0; }
642#if !defined(QT_USE_QSTRINGBUILDER)
643inline const QByteArray operator+(const QByteArray &a1, const QByteArray &a2)
644{ return QByteArray(a1) += a2; }
645inline const QByteArray operator+(const QByteArray &a1, const char *a2)
646{ return QByteArray(a1) += a2; }
647inline const QByteArray operator+(const QByteArray &a1, char a2)
648{ return QByteArray(a1) += a2; }
649inline const QByteArray operator+(const char *a1, const QByteArray &a2)
650{ return QByteArray(a1) += a2; }
651inline const QByteArray operator+(char a1, const QByteArray &a2)
652{ return QByteArray(&a1, 1) += a2; }
653#endif // QT_USE_QSTRINGBUILDER
654inline bool QByteArray::contains(const char *c) const
655{ return indexOf(c) != -1; }
656inline QByteArray &QByteArray::replace(char before, const char *c)
657{ return replace(&before, 1, c, qstrlen(c)); }
658inline QByteArray &QByteArray::replace(const QByteArray &before, const char *c)
659{ return replace(before.constData(), before.size(), c, qstrlen(c)); }
660inline QByteArray &QByteArray::replace(const char *before, const char *after)
661{ return replace(before, qstrlen(before), after, qstrlen(after)); }
662
663inline QByteArray &QByteArray::setNum(short n, int base)
664{ return base == 10 ? setNum(qlonglong(n), base) : setNum(qulonglong(ushort(n)), base); }
665inline QByteArray &QByteArray::setNum(ushort n, int base)
666{ return setNum(qulonglong(n), base); }
667inline QByteArray &QByteArray::setNum(int n, int base)
668{ return base == 10 ? setNum(qlonglong(n), base) : setNum(qulonglong(uint(n)), base); }
669inline QByteArray &QByteArray::setNum(uint n, int base)
670{ return setNum(qulonglong(n), base); }
671inline QByteArray &QByteArray::setNum(float n, char f, int prec)
672{ return setNum(double(n),f,prec); }
673
674inline std::string QByteArray::toStdString() const
675{ return std::string(constData(), length()); }
676
677inline QByteArray QByteArray::fromStdString(const std::string &s)
678{ return QByteArray(s.data(), int(s.size())); }
679
680#if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE))
681Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QByteArray &);
682Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QByteArray &);
683#endif
684
685#ifndef QT_NO_COMPRESS
686Q_CORE_EXPORT QByteArray qCompress(const uchar* data, int nbytes, int compressionLevel = -1);
687Q_CORE_EXPORT QByteArray qUncompress(const uchar* data, int nbytes);
688inline QByteArray qCompress(const QByteArray& data, int compressionLevel = -1)
689{ return qCompress(reinterpret_cast<const uchar *>(data.constData()), data.size(), compressionLevel); }
690inline QByteArray qUncompress(const QByteArray& data)
691{ return qUncompress(reinterpret_cast<const uchar*>(data.constData()), data.size()); }
692#endif
693
694Q_DECLARE_SHARED(QByteArray)
695
696QT_END_NAMESPACE
697
698#endif // QBYTEARRAY_H
699