File indexing completed on 2025-01-18 10:07:17
0001
0002
0003
0004
0005 #ifndef QBYTEARRAY_H
0006 #define QBYTEARRAY_H
0007
0008 #include <QtCore/qrefcount.h>
0009 #include <QtCore/qnamespace.h>
0010 #include <QtCore/qarraydata.h>
0011 #include <QtCore/qarraydatapointer.h>
0012 #include <QtCore/qcontainerfwd.h>
0013 #include <QtCore/qbytearrayalgorithms.h>
0014 #include <QtCore/qbytearrayview.h>
0015
0016 #include <stdlib.h>
0017 #include <string.h>
0018
0019 #include <string>
0020 #include <iterator>
0021
0022 #ifndef QT5_NULL_STRINGS
0023
0024 #define QT5_NULL_STRINGS 1
0025 #endif
0026
0027 #ifdef truncate
0028 #error qbytearray.h must be included before any header file that defines truncate
0029 #endif
0030
0031 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0032 Q_FORWARD_DECLARE_CF_TYPE(CFData);
0033 Q_FORWARD_DECLARE_OBJC_CLASS(NSData);
0034 #endif
0035
0036 #if defined(Q_OS_WASM) || defined(Q_QDOC)
0037 namespace emscripten {
0038 class val;
0039 }
0040 #endif
0041
0042 class tst_QByteArray;
0043
0044 QT_BEGIN_NAMESPACE
0045
0046 class QString;
0047 class QDataStream;
0048
0049 using QByteArrayData = QArrayDataPointer<char>;
0050
0051 # define QByteArrayLiteral(str) \
0052 (QByteArray(QByteArrayData(nullptr, const_cast<char *>(str), sizeof(str) - 1))) \
0053
0054
0055 class Q_CORE_EXPORT QByteArray
0056 {
0057 public:
0058 using DataPointer = QByteArrayData;
0059 private:
0060 typedef QTypedArrayData<char> Data;
0061
0062 DataPointer d;
0063 static const char _empty;
0064
0065 friend class ::tst_QByteArray;
0066
0067 template <typename InputIterator>
0068 using if_input_iterator = QtPrivate::IfIsInputIterator<InputIterator>;
0069 public:
0070
0071 enum Base64Option {
0072 Base64Encoding = 0,
0073 Base64UrlEncoding = 1,
0074
0075 KeepTrailingEquals = 0,
0076 OmitTrailingEquals = 2,
0077
0078 IgnoreBase64DecodingErrors = 0,
0079 AbortOnBase64DecodingErrors = 4,
0080 };
0081 Q_DECLARE_FLAGS(Base64Options, Base64Option)
0082
0083 enum class Base64DecodingStatus {
0084 Ok,
0085 IllegalInputLength,
0086 IllegalCharacter,
0087 IllegalPadding,
0088 };
0089
0090 inline constexpr QByteArray() noexcept;
0091 QByteArray(const char *, qsizetype size = -1);
0092 QByteArray(qsizetype size, char c);
0093 QByteArray(qsizetype size, Qt::Initialization);
0094 inline QByteArray(const QByteArray &) noexcept;
0095 inline ~QByteArray();
0096
0097 QByteArray &operator=(const QByteArray &) noexcept;
0098 QByteArray &operator=(const char *str);
0099 inline QByteArray(QByteArray && other) noexcept
0100 = default;
0101 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QByteArray)
0102 inline void swap(QByteArray &other) noexcept
0103 { d.swap(other.d); }
0104
0105 bool isEmpty() const noexcept { return size() == 0; }
0106 void resize(qsizetype size);
0107 void resize(qsizetype size, char c);
0108
0109 QByteArray &fill(char c, qsizetype size = -1);
0110
0111 inline qsizetype capacity() const;
0112 inline void reserve(qsizetype size);
0113 inline void squeeze();
0114
0115 #ifndef QT_NO_CAST_FROM_BYTEARRAY
0116 inline operator const char *() const;
0117 inline operator const void *() const;
0118 #endif
0119 inline char *data();
0120 inline const char *data() const noexcept;
0121 const char *constData() const noexcept { return data(); }
0122 inline void detach();
0123 inline bool isDetached() const;
0124 inline bool isSharedWith(const QByteArray &other) const noexcept
0125 { return data() == other.data() && size() == other.size(); }
0126 void clear();
0127
0128 inline char at(qsizetype i) const;
0129 inline char operator[](qsizetype i) const;
0130 [[nodiscard]] inline char &operator[](qsizetype i);
0131 [[nodiscard]] char front() const { return at(0); }
0132 [[nodiscard]] inline char &front();
0133 [[nodiscard]] char back() const { return at(size() - 1); }
0134 [[nodiscard]] inline char &back();
0135
0136 qsizetype indexOf(char c, qsizetype from = 0) const;
0137 qsizetype indexOf(QByteArrayView bv, qsizetype from = 0) const
0138 { return QtPrivate::findByteArray(qToByteArrayViewIgnoringNull(*this), from, bv); }
0139
0140 qsizetype lastIndexOf(char c, qsizetype from = -1) const;
0141 qsizetype lastIndexOf(QByteArrayView bv) const
0142 { return lastIndexOf(bv, size()); }
0143 qsizetype lastIndexOf(QByteArrayView bv, qsizetype from) const
0144 { return QtPrivate::lastIndexOf(qToByteArrayViewIgnoringNull(*this), from, bv); }
0145
0146 inline bool contains(char c) const;
0147 inline bool contains(QByteArrayView bv) const;
0148 qsizetype count(char c) const;
0149 qsizetype count(QByteArrayView bv) const
0150 { return QtPrivate::count(qToByteArrayViewIgnoringNull(*this), bv); }
0151
0152 inline int compare(QByteArrayView a, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
0153
0154 #if QT_CORE_REMOVED_SINCE(6, 7)
0155 QByteArray left(qsizetype len) const;
0156 QByteArray right(qsizetype len) const;
0157 QByteArray mid(qsizetype index, qsizetype len = -1) const;
0158 QByteArray first(qsizetype n) const;
0159 QByteArray last(qsizetype n) const;
0160 QByteArray sliced(qsizetype pos) const;
0161 QByteArray sliced(qsizetype pos, qsizetype n) const;
0162 QByteArray chopped(qsizetype len) const;
0163 #else
0164 [[nodiscard]] QByteArray left(qsizetype n) const &
0165 {
0166 if (n >= size())
0167 return *this;
0168 return first(qMax(n, 0));
0169 }
0170 [[nodiscard]] QByteArray left(qsizetype n) &&
0171 {
0172 if (n >= size())
0173 return std::move(*this);
0174 return std::move(*this).first(qMax(n, 0));
0175 }
0176 [[nodiscard]] QByteArray right(qsizetype n) const &
0177 {
0178 if (n >= size())
0179 return *this;
0180 return last(qMax(n, 0));
0181 }
0182 [[nodiscard]] QByteArray right(qsizetype n) &&
0183 {
0184 if (n >= size())
0185 return std::move(*this);
0186 return std::move(*this).last(qMax(n, 0));
0187 }
0188 [[nodiscard]] QByteArray mid(qsizetype index, qsizetype len = -1) const &;
0189 [[nodiscard]] QByteArray mid(qsizetype index, qsizetype len = -1) &&;
0190
0191 [[nodiscard]] QByteArray first(qsizetype n) const &
0192 { verify(0, n); return sliced(0, n); }
0193 [[nodiscard]] QByteArray last(qsizetype n) const &
0194 { verify(0, n); return sliced(size() - n, n); }
0195 [[nodiscard]] QByteArray sliced(qsizetype pos) const &
0196 { verify(pos, 0); return sliced(pos, size() - pos); }
0197 [[nodiscard]] QByteArray sliced(qsizetype pos, qsizetype n) const &
0198 { verify(pos, n); return QByteArray(d.data() + pos, n); }
0199 [[nodiscard]] QByteArray chopped(qsizetype len) const &
0200 { verify(0, len); return sliced(0, size() - len); }
0201
0202 [[nodiscard]] QByteArray first(qsizetype n) &&
0203 {
0204 verify(0, n);
0205 resize(n);
0206 return std::move(*this);
0207 }
0208 [[nodiscard]] QByteArray last(qsizetype n) &&
0209 { verify(0, n); return sliced_helper(*this, size() - n, n); }
0210 [[nodiscard]] QByteArray sliced(qsizetype pos) &&
0211 { verify(pos, 0); return sliced_helper(*this, pos, size() - pos); }
0212 [[nodiscard]] QByteArray sliced(qsizetype pos, qsizetype n) &&
0213 { verify(pos, n); return sliced_helper(*this, pos, n); }
0214 [[nodiscard]] QByteArray chopped(qsizetype len) &&
0215 { verify(0, len); return std::move(*this).first(size() - len); }
0216 #endif
0217
0218 bool startsWith(QByteArrayView bv) const
0219 { return QtPrivate::startsWith(qToByteArrayViewIgnoringNull(*this), bv); }
0220 bool startsWith(char c) const { return size() > 0 && front() == c; }
0221
0222 bool endsWith(char c) const { return size() > 0 && back() == c; }
0223 bool endsWith(QByteArrayView bv) const
0224 { return QtPrivate::endsWith(qToByteArrayViewIgnoringNull(*this), bv); }
0225
0226 bool isUpper() const;
0227 bool isLower() const;
0228
0229 [[nodiscard]] bool isValidUtf8() const noexcept
0230 {
0231 return QtPrivate::isValidUtf8(qToByteArrayViewIgnoringNull(*this));
0232 }
0233
0234 void truncate(qsizetype pos);
0235 void chop(qsizetype n);
0236
0237 #if !defined(Q_QDOC)
0238 [[nodiscard]] QByteArray toLower() const &
0239 { return toLower_helper(*this); }
0240 [[nodiscard]] QByteArray toLower() &&
0241 { return toLower_helper(*this); }
0242 [[nodiscard]] QByteArray toUpper() const &
0243 { return toUpper_helper(*this); }
0244 [[nodiscard]] QByteArray toUpper() &&
0245 { return toUpper_helper(*this); }
0246 [[nodiscard]] QByteArray trimmed() const &
0247 { return trimmed_helper(*this); }
0248 [[nodiscard]] QByteArray trimmed() &&
0249 { return trimmed_helper(*this); }
0250 [[nodiscard]] QByteArray simplified() const &
0251 { return simplified_helper(*this); }
0252 [[nodiscard]] QByteArray simplified() &&
0253 { return simplified_helper(*this); }
0254 #else
0255 [[nodiscard]] QByteArray toLower() const;
0256 [[nodiscard]] QByteArray toUpper() const;
0257 [[nodiscard]] QByteArray trimmed() const;
0258 [[nodiscard]] QByteArray simplified() const;
0259 #endif
0260
0261 [[nodiscard]] QByteArray leftJustified(qsizetype width, char fill = ' ', bool truncate = false) const;
0262 [[nodiscard]] QByteArray rightJustified(qsizetype width, char fill = ' ', bool truncate = false) const;
0263
0264 QByteArray &prepend(char c)
0265 { return insert(0, QByteArrayView(&c, 1)); }
0266 inline QByteArray &prepend(qsizetype count, char c);
0267 QByteArray &prepend(const char *s)
0268 { return insert(0, QByteArrayView(s, qsizetype(qstrlen(s)))); }
0269 QByteArray &prepend(const char *s, qsizetype len)
0270 { return insert(0, QByteArrayView(s, len)); }
0271 QByteArray &prepend(const QByteArray &a);
0272 QByteArray &prepend(QByteArrayView a)
0273 { return insert(0, a); }
0274
0275 QByteArray &append(char c);
0276 inline QByteArray &append(qsizetype count, char c);
0277 QByteArray &append(const char *s)
0278 { return append(s, -1); }
0279 QByteArray &append(const char *s, qsizetype len)
0280 { return append(QByteArrayView(s, len < 0 ? qsizetype(qstrlen(s)) : len)); }
0281 QByteArray &append(const QByteArray &a);
0282 QByteArray &append(QByteArrayView a)
0283 { return insert(size(), a); }
0284
0285 QByteArray &assign(QByteArrayView v);
0286 QByteArray &assign(qsizetype n, char c)
0287 {
0288 Q_ASSERT(n >= 0);
0289 return fill(c, n);
0290 }
0291 template <typename InputIterator, if_input_iterator<InputIterator> = true>
0292 QByteArray &assign(InputIterator first, InputIterator last)
0293 {
0294 d.assign(first, last);
0295 d.data()[d.size] = '\0';
0296 return *this;
0297 }
0298
0299 QByteArray &insert(qsizetype i, QByteArrayView data);
0300 inline QByteArray &insert(qsizetype i, const char *s)
0301 { return insert(i, QByteArrayView(s)); }
0302 inline QByteArray &insert(qsizetype i, const QByteArray &data)
0303 { return insert(i, QByteArrayView(data)); }
0304 QByteArray &insert(qsizetype i, qsizetype count, char c);
0305 QByteArray &insert(qsizetype i, char c)
0306 { return insert(i, QByteArrayView(&c, 1)); }
0307 QByteArray &insert(qsizetype i, const char *s, qsizetype len)
0308 { return insert(i, QByteArrayView(s, len)); }
0309
0310 QByteArray &remove(qsizetype index, qsizetype len);
0311 QByteArray &removeAt(qsizetype pos)
0312 { return size_t(pos) < size_t(size()) ? remove(pos, 1) : *this; }
0313 QByteArray &removeFirst() { return !isEmpty() ? remove(0, 1) : *this; }
0314 QByteArray &removeLast() { return !isEmpty() ? remove(size() - 1, 1) : *this; }
0315
0316 template <typename Predicate>
0317 QByteArray &removeIf(Predicate pred)
0318 {
0319 removeIf_helper(pred);
0320 return *this;
0321 }
0322
0323 QByteArray &replace(qsizetype index, qsizetype len, const char *s, qsizetype alen)
0324 { return replace(index, len, QByteArrayView(s, alen)); }
0325 QByteArray &replace(qsizetype index, qsizetype len, QByteArrayView s);
0326 QByteArray &replace(char before, QByteArrayView after)
0327 { return replace(QByteArrayView(&before, 1), after); }
0328 QByteArray &replace(const char *before, qsizetype bsize, const char *after, qsizetype asize)
0329 { return replace(QByteArrayView(before, bsize), QByteArrayView(after, asize)); }
0330 QByteArray &replace(QByteArrayView before, QByteArrayView after);
0331 QByteArray &replace(char before, char after);
0332
0333 QByteArray &operator+=(char c)
0334 { return append(c); }
0335 QByteArray &operator+=(const char *s)
0336 { return append(s); }
0337 QByteArray &operator+=(const QByteArray &a)
0338 { return append(a); }
0339 QByteArray &operator+=(QByteArrayView a)
0340 { return append(a); }
0341
0342 QList<QByteArray> split(char sep) const;
0343
0344 [[nodiscard]] QByteArray repeated(qsizetype times) const;
0345
0346 #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
0347 QT_ASCII_CAST_WARN inline bool operator==(const QString &s2) const;
0348 QT_ASCII_CAST_WARN inline bool operator!=(const QString &s2) const;
0349 QT_ASCII_CAST_WARN inline bool operator<(const QString &s2) const;
0350 QT_ASCII_CAST_WARN inline bool operator>(const QString &s2) const;
0351 QT_ASCII_CAST_WARN inline bool operator<=(const QString &s2) const;
0352 QT_ASCII_CAST_WARN inline bool operator>=(const QString &s2) const;
0353 #endif
0354 friend inline bool operator==(const QByteArray &a1, const QByteArray &a2) noexcept
0355 { return QByteArrayView(a1) == QByteArrayView(a2); }
0356 friend inline bool operator==(const QByteArray &a1, const char *a2) noexcept
0357 { return QByteArrayView(a1) == QByteArrayView(a2); }
0358 friend inline bool operator==(const char *a1, const QByteArray &a2) noexcept
0359 { return QByteArrayView(a1) == QByteArrayView(a2); }
0360 friend inline bool operator!=(const QByteArray &a1, const QByteArray &a2) noexcept
0361 { return !(a1==a2); }
0362 friend inline bool operator!=(const QByteArray &a1, const char *a2) noexcept
0363 { return QByteArrayView(a1) != QByteArrayView(a2); }
0364 friend inline bool operator!=(const char *a1, const QByteArray &a2) noexcept
0365 { return QByteArrayView(a1) != QByteArrayView(a2); }
0366 friend inline bool operator<(const QByteArray &a1, const QByteArray &a2) noexcept
0367 { return QtPrivate::compareMemory(QByteArrayView(a1), QByteArrayView(a2)) < 0; }
0368 friend inline bool operator<(const QByteArray &a1, const char *a2) noexcept
0369 { return QtPrivate::compareMemory(a1, a2) < 0; }
0370 friend inline bool operator<(const char *a1, const QByteArray &a2) noexcept
0371 { return QtPrivate::compareMemory(a1, a2) < 0; }
0372 friend inline bool operator<=(const QByteArray &a1, const QByteArray &a2) noexcept
0373 { return QtPrivate::compareMemory(QByteArrayView(a1), QByteArrayView(a2)) <= 0; }
0374 friend inline bool operator<=(const QByteArray &a1, const char *a2) noexcept
0375 { return QtPrivate::compareMemory(a1, a2) <= 0; }
0376 friend inline bool operator<=(const char *a1, const QByteArray &a2) noexcept
0377 { return QtPrivate::compareMemory(a1, a2) <= 0; }
0378 friend inline bool operator>(const QByteArray &a1, const QByteArray &a2) noexcept
0379 { return QtPrivate::compareMemory(QByteArrayView(a1), QByteArrayView(a2)) > 0; }
0380 friend inline bool operator>(const QByteArray &a1, const char *a2) noexcept
0381 { return QtPrivate::compareMemory(a1, a2) > 0; }
0382 friend inline bool operator>(const char *a1, const QByteArray &a2) noexcept
0383 { return QtPrivate::compareMemory(a1, a2) > 0; }
0384 friend inline bool operator>=(const QByteArray &a1, const QByteArray &a2) noexcept
0385 { return QtPrivate::compareMemory(QByteArrayView(a1), QByteArrayView(a2)) >= 0; }
0386 friend inline bool operator>=(const QByteArray &a1, const char *a2) noexcept
0387 { return QtPrivate::compareMemory(a1, a2) >= 0; }
0388 friend inline bool operator>=(const char *a1, const QByteArray &a2) noexcept
0389 { return QtPrivate::compareMemory(a1, a2) >= 0; }
0390
0391
0392 friend inline bool operator==(const QByteArray &a1, std::nullptr_t) noexcept { return a1.isEmpty(); }
0393 friend inline bool operator!=(const QByteArray &a1, std::nullptr_t) noexcept { return !a1.isEmpty(); }
0394 friend inline bool operator< (const QByteArray & , std::nullptr_t) noexcept { return false; }
0395 friend inline bool operator> (const QByteArray &a1, std::nullptr_t) noexcept { return !a1.isEmpty(); }
0396 friend inline bool operator<=(const QByteArray &a1, std::nullptr_t) noexcept { return a1.isEmpty(); }
0397 friend inline bool operator>=(const QByteArray & , std::nullptr_t) noexcept { return true; }
0398
0399 friend inline bool operator==(std::nullptr_t, const QByteArray &a2) noexcept { return a2 == nullptr; }
0400 friend inline bool operator!=(std::nullptr_t, const QByteArray &a2) noexcept { return a2 != nullptr; }
0401 friend inline bool operator< (std::nullptr_t, const QByteArray &a2) noexcept { return a2 > nullptr; }
0402 friend inline bool operator> (std::nullptr_t, const QByteArray &a2) noexcept { return a2 < nullptr; }
0403 friend inline bool operator<=(std::nullptr_t, const QByteArray &a2) noexcept { return a2 >= nullptr; }
0404 friend inline bool operator>=(std::nullptr_t, const QByteArray &a2) noexcept { return a2 <= nullptr; }
0405
0406 short toShort(bool *ok = nullptr, int base = 10) const;
0407 ushort toUShort(bool *ok = nullptr, int base = 10) const;
0408 int toInt(bool *ok = nullptr, int base = 10) const;
0409 uint toUInt(bool *ok = nullptr, int base = 10) const;
0410 long toLong(bool *ok = nullptr, int base = 10) const;
0411 ulong toULong(bool *ok = nullptr, int base = 10) const;
0412 qlonglong toLongLong(bool *ok = nullptr, int base = 10) const;
0413 qulonglong toULongLong(bool *ok = nullptr, int base = 10) const;
0414 float toFloat(bool *ok = nullptr) const;
0415 double toDouble(bool *ok = nullptr) const;
0416 QByteArray toBase64(Base64Options options = Base64Encoding) const;
0417 QByteArray toHex(char separator = '\0') const;
0418 QByteArray toPercentEncoding(const QByteArray &exclude = QByteArray(),
0419 const QByteArray &include = QByteArray(),
0420 char percent = '%') const;
0421 [[nodiscard]] QByteArray percentDecoded(char percent = '%') const;
0422
0423 inline QByteArray &setNum(short, int base = 10);
0424 inline QByteArray &setNum(ushort, int base = 10);
0425 inline QByteArray &setNum(int, int base = 10);
0426 inline QByteArray &setNum(uint, int base = 10);
0427 inline QByteArray &setNum(long, int base = 10);
0428 inline QByteArray &setNum(ulong, int base = 10);
0429 QByteArray &setNum(qlonglong, int base = 10);
0430 QByteArray &setNum(qulonglong, int base = 10);
0431 inline QByteArray &setNum(float, char format = 'g', int precision = 6);
0432 QByteArray &setNum(double, char format = 'g', int precision = 6);
0433 QByteArray &setRawData(const char *a, qsizetype n);
0434
0435 [[nodiscard]] static QByteArray number(int, int base = 10);
0436 [[nodiscard]] static QByteArray number(uint, int base = 10);
0437 [[nodiscard]] static QByteArray number(long, int base = 10);
0438 [[nodiscard]] static QByteArray number(ulong, int base = 10);
0439 [[nodiscard]] static QByteArray number(qlonglong, int base = 10);
0440 [[nodiscard]] static QByteArray number(qulonglong, int base = 10);
0441 [[nodiscard]] static QByteArray number(double, char format = 'g', int precision = 6);
0442 [[nodiscard]] static QByteArray fromRawData(const char *data, qsizetype size)
0443 {
0444 return QByteArray(DataPointer(nullptr, const_cast<char *>(data), size));
0445 }
0446
0447 class FromBase64Result;
0448 [[nodiscard]] static FromBase64Result fromBase64Encoding(QByteArray &&base64, Base64Options options = Base64Encoding);
0449 [[nodiscard]] static FromBase64Result fromBase64Encoding(const QByteArray &base64, Base64Options options = Base64Encoding);
0450 [[nodiscard]] static QByteArray fromBase64(const QByteArray &base64, Base64Options options = Base64Encoding);
0451 [[nodiscard]] static QByteArray fromHex(const QByteArray &hexEncoded);
0452 [[nodiscard]] static QByteArray fromPercentEncoding(const QByteArray &pctEncoded, char percent = '%');
0453
0454 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0455 static QByteArray fromCFData(CFDataRef data);
0456 static QByteArray fromRawCFData(CFDataRef data);
0457 CFDataRef toCFData() const Q_DECL_CF_RETURNS_RETAINED;
0458 CFDataRef toRawCFData() const Q_DECL_CF_RETURNS_RETAINED;
0459 static QByteArray fromNSData(const NSData *data);
0460 static QByteArray fromRawNSData(const NSData *data);
0461 NSData *toNSData() const Q_DECL_NS_RETURNS_AUTORELEASED;
0462 NSData *toRawNSData() const Q_DECL_NS_RETURNS_AUTORELEASED;
0463 #endif
0464
0465 #if defined(Q_OS_WASM) || defined(Q_QDOC)
0466 static QByteArray fromEcmaUint8Array(emscripten::val uint8array);
0467 emscripten::val toEcmaUint8Array();
0468 #endif
0469
0470 typedef char *iterator;
0471 typedef const char *const_iterator;
0472 typedef iterator Iterator;
0473 typedef const_iterator ConstIterator;
0474 typedef std::reverse_iterator<iterator> reverse_iterator;
0475 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
0476 iterator begin() { return data(); }
0477 const_iterator begin() const noexcept { return data(); }
0478 const_iterator cbegin() const noexcept { return begin(); }
0479 const_iterator constBegin() const noexcept { return begin(); }
0480 iterator end() { return begin() + size(); }
0481 const_iterator end() const noexcept { return begin() + size(); }
0482 const_iterator cend() const noexcept { return end(); }
0483 const_iterator constEnd() const noexcept { return end(); }
0484 reverse_iterator rbegin() { return reverse_iterator(end()); }
0485 reverse_iterator rend() { return reverse_iterator(begin()); }
0486 const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
0487 const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
0488 const_reverse_iterator crbegin() const noexcept { return rbegin(); }
0489 const_reverse_iterator crend() const noexcept { return rend(); }
0490
0491
0492 typedef qsizetype size_type;
0493 typedef qptrdiff difference_type;
0494 typedef const char & const_reference;
0495 typedef char & reference;
0496 typedef char *pointer;
0497 typedef const char *const_pointer;
0498 typedef char value_type;
0499 void push_back(char c)
0500 { append(c); }
0501 void push_back(const char *s)
0502 { append(s); }
0503 void push_back(const QByteArray &a)
0504 { append(a); }
0505 void push_back(QByteArrayView a)
0506 { append(a); }
0507 void push_front(char c)
0508 { prepend(c); }
0509 void push_front(const char *c)
0510 { prepend(c); }
0511 void push_front(const QByteArray &a)
0512 { prepend(a); }
0513 void push_front(QByteArrayView a)
0514 { prepend(a); }
0515 void shrink_to_fit() { squeeze(); }
0516 iterator erase(const_iterator first, const_iterator last);
0517 inline iterator erase(const_iterator it) { return erase(it, it + 1); }
0518
0519 static QByteArray fromStdString(const std::string &s);
0520 std::string toStdString() const;
0521
0522 inline qsizetype size() const noexcept { return d->size; }
0523 #if QT_DEPRECATED_SINCE(6, 4)
0524 QT_DEPRECATED_VERSION_X_6_4("Use size() or length() instead.")
0525 inline qsizetype count() const noexcept { return size(); }
0526 #endif
0527 inline qsizetype length() const noexcept { return size(); }
0528 QT_CORE_INLINE_SINCE(6, 4)
0529 bool isNull() const noexcept;
0530
0531 inline const DataPointer &data_ptr() const { return d; }
0532 inline DataPointer &data_ptr() { return d; }
0533 #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
0534 explicit inline QByteArray(const DataPointer &dd) : d(dd) {}
0535 #endif
0536 explicit inline QByteArray(DataPointer &&dd) : d(std::move(dd)) {}
0537
0538 private:
0539 void reallocData(qsizetype alloc, QArrayData::AllocationOption option);
0540 void reallocGrowData(qsizetype n);
0541 void expand(qsizetype i);
0542
0543 Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
0544 [[maybe_unused]] qsizetype n = 1) const
0545 {
0546 Q_ASSERT(pos >= 0);
0547 Q_ASSERT(pos <= d.size);
0548 Q_ASSERT(n >= 0);
0549 Q_ASSERT(n <= d.size - pos);
0550 }
0551
0552 static QByteArray sliced_helper(QByteArray &a, qsizetype pos, qsizetype n);
0553 static QByteArray toLower_helper(const QByteArray &a);
0554 static QByteArray toLower_helper(QByteArray &a);
0555 static QByteArray toUpper_helper(const QByteArray &a);
0556 static QByteArray toUpper_helper(QByteArray &a);
0557 static QByteArray trimmed_helper(const QByteArray &a);
0558 static QByteArray trimmed_helper(QByteArray &a);
0559 static QByteArray simplified_helper(const QByteArray &a);
0560 static QByteArray simplified_helper(QByteArray &a);
0561 template <typename Predicate>
0562 qsizetype removeIf_helper(Predicate pred)
0563 {
0564 const qsizetype result = d->eraseIf(pred);
0565 if (result > 0)
0566 d.data()[d.size] = '\0';
0567 return result;
0568 }
0569
0570 friend class QString;
0571 friend Q_CORE_EXPORT QByteArray qUncompress(const uchar *data, qsizetype nbytes);
0572
0573 template <typename T> friend qsizetype erase(QByteArray &ba, const T &t);
0574 template <typename Predicate> friend qsizetype erase_if(QByteArray &ba, Predicate pred);
0575 };
0576
0577 Q_DECLARE_OPERATORS_FOR_FLAGS(QByteArray::Base64Options)
0578
0579 inline constexpr QByteArray::QByteArray() noexcept {}
0580 inline QByteArray::~QByteArray() {}
0581
0582 inline char QByteArray::at(qsizetype i) const
0583 { verify(i, 1); return d.data()[i]; }
0584 inline char QByteArray::operator[](qsizetype i) const
0585 { verify(i, 1); return d.data()[i]; }
0586
0587 #ifndef QT_NO_CAST_FROM_BYTEARRAY
0588 inline QByteArray::operator const char *() const
0589 { return data(); }
0590 inline QByteArray::operator const void *() const
0591 { return data(); }
0592 #endif
0593 inline char *QByteArray::data()
0594 {
0595 detach();
0596 Q_ASSERT(d.data());
0597 return d.data();
0598 }
0599 inline const char *QByteArray::data() const noexcept
0600 {
0601 #if QT5_NULL_STRINGS == 1
0602 return d.data() ? d.data() : &_empty;
0603 #else
0604 return d.data();
0605 #endif
0606 }
0607 inline void QByteArray::detach()
0608 { if (d->needsDetach()) reallocData(size(), QArrayData::KeepSize); }
0609 inline bool QByteArray::isDetached() const
0610 { return !d->isShared(); }
0611 inline QByteArray::QByteArray(const QByteArray &a) noexcept : d(a.d)
0612 {}
0613
0614 inline qsizetype QByteArray::capacity() const { return qsizetype(d->constAllocatedCapacity()); }
0615
0616 inline void QByteArray::reserve(qsizetype asize)
0617 {
0618 if (d->needsDetach() || asize > capacity() - d->freeSpaceAtBegin())
0619 reallocData(qMax(size(), asize), QArrayData::KeepSize);
0620 if (d->constAllocatedCapacity())
0621 d->setFlag(Data::CapacityReserved);
0622 }
0623
0624 inline void QByteArray::squeeze()
0625 {
0626 if (!d.isMutable())
0627 return;
0628 if (d->needsDetach() || size() < capacity())
0629 reallocData(size(), QArrayData::KeepSize);
0630 if (d->constAllocatedCapacity())
0631 d->clearFlag(Data::CapacityReserved);
0632 }
0633
0634 inline char &QByteArray::operator[](qsizetype i)
0635 { verify(i, 1); return data()[i]; }
0636 inline char &QByteArray::front() { return operator[](0); }
0637 inline char &QByteArray::back() { return operator[](size() - 1); }
0638 inline QByteArray &QByteArray::append(qsizetype n, char ch)
0639 { return insert(size(), n, ch); }
0640 inline QByteArray &QByteArray::prepend(qsizetype n, char ch)
0641 { return insert(0, n, ch); }
0642 inline bool QByteArray::contains(char c) const
0643 { return indexOf(c) != -1; }
0644 inline bool QByteArray::contains(QByteArrayView bv) const
0645 { return indexOf(bv) != -1; }
0646 inline int QByteArray::compare(QByteArrayView a, Qt::CaseSensitivity cs) const noexcept
0647 {
0648 return cs == Qt::CaseSensitive ? QtPrivate::compareMemory(*this, a) :
0649 qstrnicmp(data(), size(), a.data(), a.size());
0650 }
0651 #if !defined(QT_USE_QSTRINGBUILDER)
0652 inline QByteArray operator+(const QByteArray &a1, const QByteArray &a2)
0653 { return QByteArray(a1) += a2; }
0654 inline QByteArray operator+(QByteArray &&lhs, const QByteArray &rhs)
0655 { return std::move(lhs += rhs); }
0656 inline QByteArray operator+(const QByteArray &a1, const char *a2)
0657 { return QByteArray(a1) += a2; }
0658 inline QByteArray operator+(QByteArray &&lhs, const char *rhs)
0659 { return std::move(lhs += rhs); }
0660 inline QByteArray operator+(const QByteArray &a1, char a2)
0661 { return QByteArray(a1) += a2; }
0662 inline QByteArray operator+(QByteArray &&lhs, char rhs)
0663 { return std::move(lhs += rhs); }
0664 inline QByteArray operator+(const char *a1, const QByteArray &a2)
0665 { return QByteArray(a1) += a2; }
0666 inline QByteArray operator+(char a1, const QByteArray &a2)
0667 { return QByteArray(&a1, 1) += a2; }
0668 #endif
0669
0670 inline QByteArray &QByteArray::setNum(short n, int base)
0671 { return setNum(qlonglong(n), base); }
0672 inline QByteArray &QByteArray::setNum(ushort n, int base)
0673 { return setNum(qulonglong(n), base); }
0674 inline QByteArray &QByteArray::setNum(int n, int base)
0675 { return setNum(qlonglong(n), base); }
0676 inline QByteArray &QByteArray::setNum(uint n, int base)
0677 { return setNum(qulonglong(n), base); }
0678 inline QByteArray &QByteArray::setNum(long n, int base)
0679 { return setNum(qlonglong(n), base); }
0680 inline QByteArray &QByteArray::setNum(ulong n, int base)
0681 { return setNum(qulonglong(n), base); }
0682 inline QByteArray &QByteArray::setNum(float n, char format, int precision)
0683 { return setNum(double(n), format, precision); }
0684
0685 #if QT_CORE_INLINE_IMPL_SINCE(6, 4)
0686 bool QByteArray::isNull() const noexcept
0687 {
0688 return d->isNull();
0689 }
0690 #endif
0691
0692 #if !defined(QT_NO_DATASTREAM) || defined(QT_BOOTSTRAPPED)
0693 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QByteArray &);
0694 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QByteArray &);
0695 #endif
0696
0697 #ifndef QT_NO_COMPRESS
0698 Q_CORE_EXPORT QByteArray qCompress(const uchar* data, qsizetype nbytes, int compressionLevel = -1);
0699 Q_CORE_EXPORT QByteArray qUncompress(const uchar* data, qsizetype nbytes);
0700 inline QByteArray qCompress(const QByteArray& data, int compressionLevel = -1)
0701 { return qCompress(reinterpret_cast<const uchar *>(data.constData()), data.size(), compressionLevel); }
0702 inline QByteArray qUncompress(const QByteArray& data)
0703 { return qUncompress(reinterpret_cast<const uchar*>(data.constData()), data.size()); }
0704 #endif
0705
0706 Q_DECLARE_SHARED(QByteArray)
0707
0708 class QByteArray::FromBase64Result
0709 {
0710 public:
0711 QByteArray decoded;
0712 QByteArray::Base64DecodingStatus decodingStatus;
0713
0714 void swap(QByteArray::FromBase64Result &other) noexcept
0715 {
0716 decoded.swap(other.decoded);
0717 std::swap(decodingStatus, other.decodingStatus);
0718 }
0719
0720 explicit operator bool() const noexcept { return decodingStatus == QByteArray::Base64DecodingStatus::Ok; }
0721
0722 #if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(Q_QDOC)
0723 QByteArray &operator*() & noexcept { return decoded; }
0724 const QByteArray &operator*() const & noexcept { return decoded; }
0725 QByteArray &&operator*() && noexcept { return std::move(decoded); }
0726 #else
0727 QByteArray &operator*() noexcept { return decoded; }
0728 const QByteArray &operator*() const noexcept { return decoded; }
0729 #endif
0730
0731 friend inline bool operator==(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
0732 {
0733 if (lhs.decodingStatus != rhs.decodingStatus)
0734 return false;
0735
0736 if (lhs.decodingStatus == QByteArray::Base64DecodingStatus::Ok && lhs.decoded != rhs.decoded)
0737 return false;
0738
0739 return true;
0740 }
0741
0742 friend inline bool operator!=(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
0743 {
0744 return !(lhs == rhs);
0745 }
0746 };
0747
0748 Q_DECLARE_SHARED(QByteArray::FromBase64Result)
0749
0750
0751 Q_CORE_EXPORT Q_DECL_PURE_FUNCTION size_t qHash(const QByteArray::FromBase64Result &key, size_t seed = 0) noexcept;
0752
0753 template <typename T>
0754 qsizetype erase(QByteArray &ba, const T &t)
0755 {
0756 return ba.removeIf_helper([&t](const auto &e) { return t == e; });
0757 }
0758
0759 template <typename Predicate>
0760 qsizetype erase_if(QByteArray &ba, Predicate pred)
0761 {
0762 return ba.removeIf_helper(pred);
0763 }
0764
0765
0766
0767
0768 QByteArray QByteArrayView::toByteArray() const
0769 {
0770 return QByteArray(data(), size());
0771 }
0772
0773 namespace Qt {
0774 inline namespace Literals {
0775 inline namespace StringLiterals {
0776
0777 inline QByteArray operator""_ba(const char *str, size_t size) noexcept
0778 {
0779 return QByteArray(QByteArrayData(nullptr, const_cast<char *>(str), qsizetype(size)));
0780 }
0781
0782 }
0783 }
0784 }
0785
0786 inline namespace QtLiterals {
0787 #if QT_DEPRECATED_SINCE(6, 8)
0788
0789 QT_DEPRECATED_VERSION_X_6_8("Use _ba from Qt::StringLiterals namespace instead.")
0790 inline QByteArray operator""_qba(const char *str, size_t size) noexcept
0791 {
0792 return Qt::StringLiterals::operator""_ba(str, size);
0793 }
0794
0795 #endif
0796 }
0797
0798 QT_END_NAMESPACE
0799
0800 #endif