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 return QByteArray(holder); \
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 IgnoreBase64DecodingErrors = 0,
169 AbortOnBase64DecodingErrors = 4,
170 };
171 Q_DECLARE_FLAGS(Base64Options, Base64Option)
172
173 enum class Base64DecodingStatus {
174 Ok,
175 IllegalInputLength,
176 IllegalCharacter,
177 IllegalPadding,
178 };
179
180 inline QByteArray() noexcept;
181 QByteArray(const char *, int size = -1);
182 QByteArray(int size, char c);
183 QByteArray(int size, Qt::Initialization);
184 inline QByteArray(const QByteArray &) noexcept;
185 inline ~QByteArray();
186
187 QByteArray &operator=(const QByteArray &) noexcept;
188 QByteArray &operator=(const char *str);
189 inline QByteArray(QByteArray && other) noexcept : d(other.d) { other.d = Data::sharedNull(); }
190 inline QByteArray &operator=(QByteArray &&other) noexcept
191 { qSwap(d, other.d); return *this; }
192
193 inline void swap(QByteArray &other) noexcept
194 { qSwap(d, other.d); }
195
196 inline int size() const;
197 inline bool isEmpty() const;
198 void resize(int size);
199
200 QByteArray &fill(char c, int size = -1);
201
202 inline int capacity() const;
203 inline void reserve(int size);
204 inline void squeeze();
205
206#ifndef QT_NO_CAST_FROM_BYTEARRAY
207 inline operator const char *() const;
208 inline operator const void *() const;
209#endif
210 inline char *data();
211 inline const char *data() const;
212 inline const char *constData() const;
213 inline void detach();
214 inline bool isDetached() const;
215 inline bool isSharedWith(const QByteArray &other) const { return d == other.d; }
216 void clear();
217
218 inline char at(int i) const;
219 inline char operator[](int i) const;
220 inline char operator[](uint i) const;
221 Q_REQUIRED_RESULT inline QByteRef operator[](int i);
222 Q_REQUIRED_RESULT inline QByteRef operator[](uint i);
223 Q_REQUIRED_RESULT char front() const { return at(0); }
224 Q_REQUIRED_RESULT inline QByteRef front();
225 Q_REQUIRED_RESULT char back() const { return at(size() - 1); }
226 Q_REQUIRED_RESULT inline QByteRef back();
227
228 int indexOf(char c, int from = 0) const;
229 int indexOf(const char *c, int from = 0) const;
230 int indexOf(const QByteArray &a, int from = 0) const;
231 int lastIndexOf(char c, int from = -1) const;
232 int lastIndexOf(const char *c, int from = -1) const;
233 int lastIndexOf(const QByteArray &a, int from = -1) const;
234
235 inline bool contains(char c) const;
236 inline bool contains(const char *a) const;
237 inline bool contains(const QByteArray &a) const;
238 int count(char c) const;
239 int count(const char *a) const;
240 int count(const QByteArray &a) const;
241
242 inline int compare(const char *c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
243 inline int compare(const QByteArray &a, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
244
245 Q_REQUIRED_RESULT QByteArray left(int len) const;
246 Q_REQUIRED_RESULT QByteArray right(int len) const;
247 Q_REQUIRED_RESULT QByteArray mid(int index, int len = -1) const;
248 Q_REQUIRED_RESULT QByteArray chopped(int len) const
249 { Q_ASSERT(len >= 0); Q_ASSERT(len <= size()); return left(size() - len); }
250
251 bool startsWith(const QByteArray &a) const;
252 bool startsWith(char c) const;
253 bool startsWith(const char *c) const;
254
255 bool endsWith(const QByteArray &a) const;
256 bool endsWith(char c) const;
257 bool endsWith(const char *c) const;
258
259 bool isUpper() const;
260 bool isLower() const;
261
262 void truncate(int pos);
263 void chop(int n);
264
265#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QSTRING_COMPAT_CPP) && !defined(Q_CLANG_QDOC)
266# if defined(Q_CC_GNU) && !defined(Q_CC_CLANG) && !defined(Q_CC_INTEL) && !__has_cpp_attribute(nodiscard)
267 // required due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61941
268# pragma push_macro("Q_REQUIRED_RESULT")
269# undef Q_REQUIRED_RESULT
270# define Q_REQUIRED_RESULT
271# define Q_REQUIRED_RESULT_pushed
272# endif
273 Q_REQUIRED_RESULT QByteArray toLower() const &
274 { return toLower_helper(*this); }
275 Q_REQUIRED_RESULT QByteArray toLower() &&
276 { return toLower_helper(*this); }
277 Q_REQUIRED_RESULT QByteArray toUpper() const &
278 { return toUpper_helper(*this); }
279 Q_REQUIRED_RESULT QByteArray toUpper() &&
280 { return toUpper_helper(*this); }
281 Q_REQUIRED_RESULT QByteArray trimmed() const &
282 { return trimmed_helper(*this); }
283 Q_REQUIRED_RESULT QByteArray trimmed() &&
284 { return trimmed_helper(*this); }
285 Q_REQUIRED_RESULT QByteArray simplified() const &
286 { return simplified_helper(*this); }
287 Q_REQUIRED_RESULT QByteArray simplified() &&
288 { return simplified_helper(*this); }
289# ifdef Q_REQUIRED_RESULT_pushed
290# pragma pop_macro("Q_REQUIRED_RESULT")
291# endif
292#else
293 Q_REQUIRED_RESULT QByteArray toLower() const;
294 Q_REQUIRED_RESULT QByteArray toUpper() const;
295 Q_REQUIRED_RESULT QByteArray trimmed() const;
296 Q_REQUIRED_RESULT QByteArray simplified() const;
297#endif
298
299 Q_REQUIRED_RESULT QByteArray leftJustified(int width, char fill = ' ', bool truncate = false) const;
300 Q_REQUIRED_RESULT QByteArray rightJustified(int width, char fill = ' ', bool truncate = false) const;
301
302 QByteArray &prepend(char c);
303 inline QByteArray &prepend(int count, char c);
304 QByteArray &prepend(const char *s);
305 QByteArray &prepend(const char *s, int len);
306 QByteArray &prepend(const QByteArray &a);
307 QByteArray &append(char c);
308 inline QByteArray &append(int count, char c);
309 QByteArray &append(const char *s);
310 QByteArray &append(const char *s, int len);
311 QByteArray &append(const QByteArray &a);
312 QByteArray &insert(int i, char c);
313 QByteArray &insert(int i, int count, char c);
314 QByteArray &insert(int i, const char *s);
315 QByteArray &insert(int i, const char *s, int len);
316 QByteArray &insert(int i, const QByteArray &a);
317 QByteArray &remove(int index, int len);
318 QByteArray &replace(int index, int len, const char *s);
319 QByteArray &replace(int index, int len, const char *s, int alen);
320 QByteArray &replace(int index, int len, const QByteArray &s);
321 inline QByteArray &replace(char before, const char *after);
322 QByteArray &replace(char before, const QByteArray &after);
323 inline QByteArray &replace(const char *before, const char *after);
324 QByteArray &replace(const char *before, int bsize, const char *after, int asize);
325 QByteArray &replace(const QByteArray &before, const QByteArray &after);
326 inline QByteArray &replace(const QByteArray &before, const char *after);
327 QByteArray &replace(const char *before, const QByteArray &after);
328 QByteArray &replace(char before, char after);
329 inline QByteArray &operator+=(char c);
330 inline QByteArray &operator+=(const char *s);
331 inline QByteArray &operator+=(const QByteArray &a);
332
333 QList<QByteArray> split(char sep) const;
334
335 Q_REQUIRED_RESULT QByteArray repeated(int times) const;
336
337#if !defined(QT_NO_CAST_TO_ASCII) && QT_DEPRECATED_SINCE(5, 15)
338 QT_DEPRECATED_VERSION_X_5_15("Use QString's toUtf8(), toLatin1() or toLocal8Bit()")
339 QByteArray &append(const QString &s);
340 QT_DEPRECATED_VERSION_X_5_15("Use QString's toUtf8(), toLatin1() or toLocal8Bit()")
341 QByteArray &insert(int i, const QString &s);
342 QT_DEPRECATED_VERSION_X_5_15("Use QString's toUtf8(), toLatin1() or toLocal8Bit()")
343 QByteArray &replace(const QString &before, const char *after);
344 QT_DEPRECATED_VERSION_X_5_15("Use QString's toUtf8(), toLatin1() or toLocal8Bit()")
345 QByteArray &replace(char c, const QString &after);
346 QT_DEPRECATED_VERSION_X_5_15("Use QString's toUtf8(), toLatin1() or toLocal8Bit()")
347 QByteArray &replace(const QString &before, const QByteArray &after);
348
349 QT_DEPRECATED_VERSION_X_5_15("Use QString's toUtf8(), toLatin1() or toLocal8Bit()")
350 QByteArray &operator+=(const QString &s);
351 QT_DEPRECATED_VERSION_X_5_15("Use QString's toUtf8(), toLatin1() or toLocal8Bit()")
352 int indexOf(const QString &s, int from = 0) const;
353 QT_DEPRECATED_VERSION_X_5_15("Use QString's toUtf8(), toLatin1() or toLocal8Bit()")
354 int lastIndexOf(const QString &s, int from = -1) const;
355#endif
356#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
357 inline bool operator==(const QString &s2) const;
358 inline bool operator!=(const QString &s2) const;
359 inline bool operator<(const QString &s2) const;
360 inline bool operator>(const QString &s2) const;
361 inline bool operator<=(const QString &s2) const;
362 inline bool operator>=(const QString &s2) const;
363#endif
364
365 short toShort(bool *ok = nullptr, int base = 10) const;
366 ushort toUShort(bool *ok = nullptr, int base = 10) const;
367 int toInt(bool *ok = nullptr, int base = 10) const;
368 uint toUInt(bool *ok = nullptr, int base = 10) const;
369 long toLong(bool *ok = nullptr, int base = 10) const;
370 ulong toULong(bool *ok = nullptr, int base = 10) const;
371 qlonglong toLongLong(bool *ok = nullptr, int base = 10) const;
372 qulonglong toULongLong(bool *ok = nullptr, int base = 10) const;
373 float toFloat(bool *ok = nullptr) const;
374 double toDouble(bool *ok = nullptr) const;
375 QByteArray toBase64(Base64Options options) const;
376 QByteArray toBase64() const; // ### Qt6 merge with previous
377 QByteArray toHex() const;
378 QByteArray toHex(char separator) const; // ### Qt6 merge with previous
379 QByteArray toPercentEncoding(const QByteArray &exclude = QByteArray(),
380 const QByteArray &include = QByteArray(),
381 char percent = '%') const;
382
383 inline QByteArray &setNum(short, int base = 10);
384 inline QByteArray &setNum(ushort, int base = 10);
385 inline QByteArray &setNum(int, int base = 10);
386 inline QByteArray &setNum(uint, int base = 10);
387 QByteArray &setNum(qlonglong, int base = 10);
388 QByteArray &setNum(qulonglong, int base = 10);
389 inline QByteArray &setNum(float, char f = 'g', int prec = 6);
390 QByteArray &setNum(double, char f = 'g', int prec = 6);
391 QByteArray &setRawData(const char *a, uint n); // ### Qt 6: use an int
392
393 Q_REQUIRED_RESULT static QByteArray number(int, int base = 10);
394 Q_REQUIRED_RESULT static QByteArray number(uint, int base = 10);
395 Q_REQUIRED_RESULT static QByteArray number(qlonglong, int base = 10);
396 Q_REQUIRED_RESULT static QByteArray number(qulonglong, int base = 10);
397 Q_REQUIRED_RESULT static QByteArray number(double, char f = 'g', int prec = 6);
398 Q_REQUIRED_RESULT static QByteArray fromRawData(const char *, int size);
399
400 class FromBase64Result;
401 Q_REQUIRED_RESULT static FromBase64Result fromBase64Encoding(QByteArray &&base64, Base64Options options = Base64Encoding);
402 Q_REQUIRED_RESULT static FromBase64Result fromBase64Encoding(const QByteArray &base64, Base64Options options = Base64Encoding);
403 Q_REQUIRED_RESULT static QByteArray fromBase64(const QByteArray &base64, Base64Options options);
404 Q_REQUIRED_RESULT static QByteArray fromBase64(const QByteArray &base64); // ### Qt6 merge with previous
405 Q_REQUIRED_RESULT static QByteArray fromHex(const QByteArray &hexEncoded);
406 Q_REQUIRED_RESULT static QByteArray fromPercentEncoding(const QByteArray &pctEncoded, char percent = '%');
407
408#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
409 static QByteArray fromCFData(CFDataRef data);
410 static QByteArray fromRawCFData(CFDataRef data);
411 CFDataRef toCFData() const Q_DECL_CF_RETURNS_RETAINED;
412 CFDataRef toRawCFData() const Q_DECL_CF_RETURNS_RETAINED;
413 static QByteArray fromNSData(const NSData *data);
414 static QByteArray fromRawNSData(const NSData *data);
415 NSData *toNSData() const Q_DECL_NS_RETURNS_AUTORELEASED;
416 NSData *toRawNSData() const Q_DECL_NS_RETURNS_AUTORELEASED;
417#endif
418
419 typedef char *iterator;
420 typedef const char *const_iterator;
421 typedef iterator Iterator;
422 typedef const_iterator ConstIterator;
423 typedef std::reverse_iterator<iterator> reverse_iterator;
424 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
425 inline iterator begin();
426 inline const_iterator begin() const;
427 inline const_iterator cbegin() const;
428 inline const_iterator constBegin() const;
429 inline iterator end();
430 inline const_iterator end() const;
431 inline const_iterator cend() const;
432 inline const_iterator constEnd() const;
433 reverse_iterator rbegin() { return reverse_iterator(end()); }
434 reverse_iterator rend() { return reverse_iterator(begin()); }
435 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
436 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
437 const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); }
438 const_reverse_iterator crend() const { return const_reverse_iterator(begin()); }
439
440 // stl compatibility
441 typedef int size_type;
442 typedef qptrdiff difference_type;
443 typedef const char & const_reference;
444 typedef char & reference;
445 typedef char *pointer;
446 typedef const char *const_pointer;
447 typedef char value_type;
448 inline void push_back(char c);
449 inline void push_back(const char *c);
450 inline void push_back(const QByteArray &a);
451 inline void push_front(char c);
452 inline void push_front(const char *c);
453 inline void push_front(const QByteArray &a);
454 void shrink_to_fit() { squeeze(); }
455
456 static inline QByteArray fromStdString(const std::string &s);
457 inline std::string toStdString() const;
458
459 inline int count() const { return d->size; }
460 int length() const { return d->size; }
461 bool isNull() const;
462
463 inline QByteArray(QByteArrayDataPtr dd)
464 : d(static_cast<Data *>(dd.ptr))
465 {
466 }
467
468private:
469 operator QNoImplicitBoolCast() const;
470 Data *d;
471 void reallocData(uint alloc, Data::AllocationOptions options);
472 void expand(int i);
473 QByteArray nulTerminated() const;
474
475 static QByteArray toLower_helper(const QByteArray &a);
476 static QByteArray toLower_helper(QByteArray &a);
477 static QByteArray toUpper_helper(const QByteArray &a);
478 static QByteArray toUpper_helper(QByteArray &a);
479 static QByteArray trimmed_helper(const QByteArray &a);
480 static QByteArray trimmed_helper(QByteArray &a);
481 static QByteArray simplified_helper(const QByteArray &a);
482 static QByteArray simplified_helper(QByteArray &a);
483
484 friend class QByteRef;
485 friend class QString;
486 friend Q_CORE_EXPORT QByteArray qUncompress(const uchar *data, int nbytes);
487public:
488 typedef Data * DataPtr;
489 inline DataPtr &data_ptr() { return d; }
490};
491
492Q_DECLARE_OPERATORS_FOR_FLAGS(QByteArray::Base64Options)
493
494inline QByteArray::QByteArray() noexcept : d(Data::sharedNull()) { }
495inline QByteArray::~QByteArray() { if (!d->ref.deref()) Data::deallocate(d); }
496inline int QByteArray::size() const
497{ return d->size; }
498
499inline char QByteArray::at(int i) const
500{ Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; }
501inline char QByteArray::operator[](int i) const
502{ Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; }
503inline char QByteArray::operator[](uint i) const
504{ Q_ASSERT(i < uint(size())); return d->data()[i]; }
505
506inline bool QByteArray::isEmpty() const
507{ return d->size == 0; }
508#ifndef QT_NO_CAST_FROM_BYTEARRAY
509inline QByteArray::operator const char *() const
510{ return d->data(); }
511inline QByteArray::operator const void *() const
512{ return d->data(); }
513#endif
514inline char *QByteArray::data()
515{ detach(); return d->data(); }
516inline const char *QByteArray::data() const
517{ return d->data(); }
518inline const char *QByteArray::constData() const
519{ return d->data(); }
520inline void QByteArray::detach()
521{ if (d->ref.isShared() || (d->offset != sizeof(QByteArrayData))) reallocData(uint(d->size) + 1u, d->detachFlags()); }
522inline bool QByteArray::isDetached() const
523{ return !d->ref.isShared(); }
524inline QByteArray::QByteArray(const QByteArray &a) noexcept : d(a.d)
525{ d->ref.ref(); }
526
527inline int QByteArray::capacity() const
528{ return d->alloc ? d->alloc - 1 : 0; }
529
530inline void QByteArray::reserve(int asize)
531{
532 if (d->ref.isShared() || uint(asize) + 1u > d->alloc) {
533 reallocData(qMax(uint(size()), uint(asize)) + 1u, d->detachFlags() | Data::CapacityReserved);
534 } else {
535 // cannot set unconditionally, since d could be the shared_null or
536 // otherwise static
537 d->capacityReserved = true;
538 }
539}
540
541inline void QByteArray::squeeze()
542{
543 if (d->ref.isShared() || uint(d->size) + 1u < d->alloc) {
544 reallocData(uint(d->size) + 1u, d->detachFlags() & ~Data::CapacityReserved);
545 } else {
546 // cannot set unconditionally, since d could be shared_null or
547 // otherwise static.
548 d->capacityReserved = false;
549 }
550}
551
552namespace QtPrivate {
553namespace DeprecatedRefClassBehavior {
554 enum class EmittingClass {
555 QByteRef,
556 QCharRef,
557 };
558
559 enum class WarningType {
560 OutOfRange,
561 DelayedDetach,
562 };
563
564 Q_CORE_EXPORT Q_DECL_COLD_FUNCTION void warn(WarningType w, EmittingClass c);
565} // namespace DeprecatedAssignmentOperatorBehavior
566} // namespace QtPrivate
567
568class
569#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
570Q_CORE_EXPORT
571#endif
572QByteRef { // ### Qt 7: remove
573 QByteArray &a;
574 int i;
575 inline QByteRef(QByteArray &array, int idx)
576 : a(array),i(idx) {}
577 friend class QByteArray;
578public:
579 QByteRef(const QByteRef &) = default;
580 inline operator char() const
581 {
582 using namespace QtPrivate::DeprecatedRefClassBehavior;
583 if (Q_LIKELY(i < a.d->size))
584 return a.d->data()[i];
585#ifdef QT_DEBUG
586 warn(WarningType::OutOfRange, EmittingClass::QByteRef);
587#endif
588 return char(0);
589 }
590 inline QByteRef &operator=(char c)
591 {
592 using namespace QtPrivate::DeprecatedRefClassBehavior;
593 if (Q_UNLIKELY(i >= a.d->size)) {
594#ifdef QT_DEBUG
595 warn(WarningType::OutOfRange, EmittingClass::QByteRef);
596#endif
597 a.expand(i);
598 } else {
599#ifdef QT_DEBUG
600 if (Q_UNLIKELY(!a.isDetached()))
601 warn(WarningType::DelayedDetach, EmittingClass::QByteRef);
602#endif
603 a.detach();
604 }
605 a.d->data()[i] = c;
606 return *this;
607 }
608 inline QByteRef &operator=(const QByteRef &c)
609 {
610 return operator=(char(c));
611 }
612 inline bool operator==(char c) const
613 { return a.d->data()[i] == c; }
614 inline bool operator!=(char c) const
615 { return a.d->data()[i] != c; }
616 inline bool operator>(char c) const
617 { return a.d->data()[i] > c; }
618 inline bool operator>=(char c) const
619 { return a.d->data()[i] >= c; }
620 inline bool operator<(char c) const
621 { return a.d->data()[i] < c; }
622 inline bool operator<=(char c) const
623 { return a.d->data()[i] <= c; }
624};
625
626inline QByteRef QByteArray::operator[](int i)
627{ Q_ASSERT(i >= 0); detach(); return QByteRef(*this, i); }
628inline QByteRef QByteArray::operator[](uint i)
629{ detach(); return QByteRef(*this, i); }
630inline QByteRef QByteArray::front() { return operator[](0); }
631inline QByteRef QByteArray::back() { return operator[](size() - 1); }
632inline QByteArray::iterator QByteArray::begin()
633{ detach(); return d->data(); }
634inline QByteArray::const_iterator QByteArray::begin() const
635{ return d->data(); }
636inline QByteArray::const_iterator QByteArray::cbegin() const
637{ return d->data(); }
638inline QByteArray::const_iterator QByteArray::constBegin() const
639{ return d->data(); }
640inline QByteArray::iterator QByteArray::end()
641{ detach(); return d->data() + d->size; }
642inline QByteArray::const_iterator QByteArray::end() const
643{ return d->data() + d->size; }
644inline QByteArray::const_iterator QByteArray::cend() const
645{ return d->data() + d->size; }
646inline QByteArray::const_iterator QByteArray::constEnd() const
647{ return d->data() + d->size; }
648inline QByteArray &QByteArray::append(int n, char ch)
649{ return insert(d->size, n, ch); }
650inline QByteArray &QByteArray::prepend(int n, char ch)
651{ return insert(0, n, ch); }
652inline QByteArray &QByteArray::operator+=(char c)
653{ return append(c); }
654inline QByteArray &QByteArray::operator+=(const char *s)
655{ return append(s); }
656inline QByteArray &QByteArray::operator+=(const QByteArray &a)
657{ return append(a); }
658inline void QByteArray::push_back(char c)
659{ append(c); }
660inline void QByteArray::push_back(const char *c)
661{ append(c); }
662inline void QByteArray::push_back(const QByteArray &a)
663{ append(a); }
664inline void QByteArray::push_front(char c)
665{ prepend(c); }
666inline void QByteArray::push_front(const char *c)
667{ prepend(c); }
668inline void QByteArray::push_front(const QByteArray &a)
669{ prepend(a); }
670inline bool QByteArray::contains(const QByteArray &a) const
671{ return indexOf(a) != -1; }
672inline bool QByteArray::contains(char c) const
673{ return indexOf(c) != -1; }
674inline int QByteArray::compare(const char *c, Qt::CaseSensitivity cs) const noexcept
675{
676 return cs == Qt::CaseSensitive ? qstrcmp(*this, c) :
677 qstrnicmp(data(), size(), c, -1);
678}
679inline int QByteArray::compare(const QByteArray &a, Qt::CaseSensitivity cs) const noexcept
680{
681 return cs == Qt::CaseSensitive ? qstrcmp(*this, a) :
682 qstrnicmp(data(), size(), a.data(), a.size());
683}
684inline bool operator==(const QByteArray &a1, const QByteArray &a2) noexcept
685{ return (a1.size() == a2.size()) && (memcmp(a1.constData(), a2.constData(), a1.size())==0); }
686inline bool operator==(const QByteArray &a1, const char *a2) noexcept
687{ return a2 ? qstrcmp(a1,a2) == 0 : a1.isEmpty(); }
688inline bool operator==(const char *a1, const QByteArray &a2) noexcept
689{ return a1 ? qstrcmp(a1,a2) == 0 : a2.isEmpty(); }
690inline bool operator!=(const QByteArray &a1, const QByteArray &a2) noexcept
691{ return !(a1==a2); }
692inline bool operator!=(const QByteArray &a1, const char *a2) noexcept
693{ return a2 ? qstrcmp(a1,a2) != 0 : !a1.isEmpty(); }
694inline bool operator!=(const char *a1, const QByteArray &a2) noexcept
695{ return a1 ? qstrcmp(a1,a2) != 0 : !a2.isEmpty(); }
696inline bool operator<(const QByteArray &a1, const QByteArray &a2) noexcept
697{ return qstrcmp(a1, a2) < 0; }
698 inline bool operator<(const QByteArray &a1, const char *a2) noexcept
699{ return qstrcmp(a1, a2) < 0; }
700inline bool operator<(const char *a1, const QByteArray &a2) noexcept
701{ return qstrcmp(a1, a2) < 0; }
702inline bool operator<=(const QByteArray &a1, const QByteArray &a2) noexcept
703{ return qstrcmp(a1, a2) <= 0; }
704inline bool operator<=(const QByteArray &a1, const char *a2) noexcept
705{ return qstrcmp(a1, a2) <= 0; }
706inline bool operator<=(const char *a1, const QByteArray &a2) noexcept
707{ return qstrcmp(a1, a2) <= 0; }
708inline bool operator>(const QByteArray &a1, const QByteArray &a2) noexcept
709{ return qstrcmp(a1, a2) > 0; }
710inline bool operator>(const QByteArray &a1, const char *a2) noexcept
711{ return qstrcmp(a1, a2) > 0; }
712inline bool operator>(const char *a1, const QByteArray &a2) noexcept
713{ return qstrcmp(a1, a2) > 0; }
714inline bool operator>=(const QByteArray &a1, const QByteArray &a2) noexcept
715{ return qstrcmp(a1, a2) >= 0; }
716inline bool operator>=(const QByteArray &a1, const char *a2) noexcept
717{ return qstrcmp(a1, a2) >= 0; }
718inline bool operator>=(const char *a1, const QByteArray &a2) noexcept
719{ return qstrcmp(a1, a2) >= 0; }
720#if !defined(QT_USE_QSTRINGBUILDER)
721inline const QByteArray operator+(const QByteArray &a1, const QByteArray &a2)
722{ return QByteArray(a1) += a2; }
723inline const QByteArray operator+(const QByteArray &a1, const char *a2)
724{ return QByteArray(a1) += a2; }
725inline const QByteArray operator+(const QByteArray &a1, char a2)
726{ return QByteArray(a1) += a2; }
727inline const QByteArray operator+(const char *a1, const QByteArray &a2)
728{ return QByteArray(a1) += a2; }
729inline const QByteArray operator+(char a1, const QByteArray &a2)
730{ return QByteArray(&a1, 1) += a2; }
731#endif // QT_USE_QSTRINGBUILDER
732inline bool QByteArray::contains(const char *c) const
733{ return indexOf(c) != -1; }
734inline QByteArray &QByteArray::replace(char before, const char *c)
735{ return replace(&before, 1, c, qstrlen(c)); }
736inline QByteArray &QByteArray::replace(const QByteArray &before, const char *c)
737{ return replace(before.constData(), before.size(), c, qstrlen(c)); }
738inline QByteArray &QByteArray::replace(const char *before, const char *after)
739{ return replace(before, qstrlen(before), after, qstrlen(after)); }
740
741inline QByteArray &QByteArray::setNum(short n, int base)
742{ return base == 10 ? setNum(qlonglong(n), base) : setNum(qulonglong(ushort(n)), base); }
743inline QByteArray &QByteArray::setNum(ushort n, int base)
744{ return setNum(qulonglong(n), base); }
745inline QByteArray &QByteArray::setNum(int n, int base)
746{ return base == 10 ? setNum(qlonglong(n), base) : setNum(qulonglong(uint(n)), base); }
747inline QByteArray &QByteArray::setNum(uint n, int base)
748{ return setNum(qulonglong(n), base); }
749inline QByteArray &QByteArray::setNum(float n, char f, int prec)
750{ return setNum(double(n),f,prec); }
751
752inline std::string QByteArray::toStdString() const
753{ return std::string(constData(), length()); }
754
755inline QByteArray QByteArray::fromStdString(const std::string &s)
756{ return QByteArray(s.data(), int(s.size())); }
757
758#if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE))
759Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QByteArray &);
760Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QByteArray &);
761#endif
762
763#ifndef QT_NO_COMPRESS
764Q_CORE_EXPORT QByteArray qCompress(const uchar* data, int nbytes, int compressionLevel = -1);
765Q_CORE_EXPORT QByteArray qUncompress(const uchar* data, int nbytes);
766inline QByteArray qCompress(const QByteArray& data, int compressionLevel = -1)
767{ return qCompress(reinterpret_cast<const uchar *>(data.constData()), data.size(), compressionLevel); }
768inline QByteArray qUncompress(const QByteArray& data)
769{ return qUncompress(reinterpret_cast<const uchar*>(data.constData()), data.size()); }
770#endif
771
772Q_DECLARE_SHARED(QByteArray)
773
774class QByteArray::FromBase64Result
775{
776public:
777 QByteArray decoded;
778 QByteArray::Base64DecodingStatus decodingStatus;
779
780 void swap(QByteArray::FromBase64Result &other) noexcept
781 {
782 qSwap(decoded, other.decoded);
783 qSwap(decodingStatus, other.decodingStatus);
784 }
785
786 explicit operator bool() const noexcept { return decodingStatus == QByteArray::Base64DecodingStatus::Ok; }
787
788#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(Q_QDOC)
789 QByteArray &operator*() & noexcept { return decoded; }
790 const QByteArray &operator*() const & noexcept { return decoded; }
791 QByteArray &&operator*() && noexcept { return std::move(decoded); }
792#else
793 QByteArray &operator*() noexcept { return decoded; }
794 const QByteArray &operator*() const noexcept { return decoded; }
795#endif
796};
797
798Q_DECLARE_SHARED(QByteArray::FromBase64Result)
799
800inline bool operator==(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
801{
802 if (lhs.decodingStatus != rhs.decodingStatus)
803 return false;
804
805 if (lhs.decodingStatus == QByteArray::Base64DecodingStatus::Ok && lhs.decoded != rhs.decoded)
806 return false;
807
808 return true;
809}
810
811inline bool operator!=(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
812{
813 return !operator==(lhs, rhs);
814}
815
816Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QByteArray::FromBase64Result &key, uint seed = 0) noexcept;
817
818QT_END_NAMESPACE
819
820#endif // QBYTEARRAY_H
821