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