| 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) |
| 60 | Q_FORWARD_DECLARE_CF_TYPE(CFData); |
| 61 | Q_FORWARD_DECLARE_OBJC_CLASS(NSData); |
| 62 | #endif |
| 63 | |
| 64 | QT_BEGIN_NAMESPACE |
| 65 | |
| 66 | |
| 67 | /***************************************************************************** |
| 68 | Safe and portable C string functions; extensions to standard string.h |
| 69 | *****************************************************************************/ |
| 70 | |
| 71 | Q_CORE_EXPORT char *qstrdup(const char *); |
| 72 | |
| 73 | inline uint qstrlen(const char *str) |
| 74 | { return str ? uint(strlen(s: str)) : 0; } |
| 75 | |
| 76 | inline 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 | |
| 86 | Q_CORE_EXPORT char *qstrcpy(char *dst, const char *src); |
| 87 | Q_CORE_EXPORT char *qstrncpy(char *dst, const char *src, uint len); |
| 88 | |
| 89 | Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2); |
| 90 | Q_CORE_EXPORT int qstrcmp(const QByteArray &str1, const QByteArray &str2); |
| 91 | Q_CORE_EXPORT int qstrcmp(const QByteArray &str1, const char *str2); |
| 92 | static inline int qstrcmp(const char *str1, const QByteArray &str2) |
| 93 | { return -qstrcmp(str1: str2, str2: str1); } |
| 94 | |
| 95 | inline int qstrncmp(const char *str1, const char *str2, uint len) |
| 96 | { |
| 97 | return (str1 && str2) ? strncmp(s1: str1, s2: str2, n: len) |
| 98 | : (str1 ? 1 : (str2 ? -1 : 0)); |
| 99 | } |
| 100 | Q_CORE_EXPORT int qstricmp(const char *, const char *); |
| 101 | Q_CORE_EXPORT int qstrnicmp(const char *, const char *, uint len); |
| 102 | Q_CORE_EXPORT int qstrnicmp(const char *, qsizetype, const char *, qsizetype = -1); |
| 103 | |
| 104 | // implemented in qvsnprintf.cpp |
| 105 | Q_CORE_EXPORT int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap); |
| 106 | Q_CORE_EXPORT int qsnprintf(char *str, size_t n, const char *fmt, ...); |
| 107 | |
| 108 | // qChecksum: Internet checksum |
| 109 | Q_CORE_EXPORT quint16 qChecksum(const char *s, uint len); // ### Qt 6: Remove |
| 110 | Q_CORE_EXPORT quint16 qChecksum(const char *s, uint len, Qt::ChecksumType standard); // ### Qt 6: Use Qt::ChecksumType standard = Qt::ChecksumIso3309 |
| 111 | |
| 112 | class QByteRef; |
| 113 | class QString; |
| 114 | class QDataStream; |
| 115 | template <typename T> class QList; |
| 116 | |
| 117 | typedef QArrayData QByteArrayData; |
| 118 | |
| 119 | template<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 | |
| 131 | struct QByteArrayDataPtr |
| 132 | { |
| 133 | QByteArrayData *ptr; |
| 134 | }; |
| 135 | |
| 136 | #define (size, offset) \ |
| 137 | Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, offset) |
| 138 | /**/ |
| 139 | |
| 140 | #define (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 | |
| 155 | class Q_CORE_EXPORT QByteArray |
| 156 | { |
| 157 | private: |
| 158 | typedef QTypedArrayData<char> Data; |
| 159 | |
| 160 | public: |
| 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(value1&: d, value2&: other.d); return *this; } |
| 192 | |
| 193 | inline void swap(QByteArray &other) noexcept |
| 194 | { qSwap(value1&: d, value2&: 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(i: 0); } |
| 224 | Q_REQUIRED_RESULT inline QByteRef front(); |
| 225 | Q_REQUIRED_RESULT char back() const { return at(i: 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(len: 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(a: *this); } |
| 275 | Q_REQUIRED_RESULT QByteArray toLower() && |
| 276 | { return toLower_helper(a&: *this); } |
| 277 | Q_REQUIRED_RESULT QByteArray toUpper() const & |
| 278 | { return toUpper_helper(a: *this); } |
| 279 | Q_REQUIRED_RESULT QByteArray toUpper() && |
| 280 | { return toUpper_helper(a&: *this); } |
| 281 | Q_REQUIRED_RESULT QByteArray trimmed() const & |
| 282 | { return trimmed_helper(a: *this); } |
| 283 | Q_REQUIRED_RESULT QByteArray trimmed() && |
| 284 | { return trimmed_helper(a&: *this); } |
| 285 | Q_REQUIRED_RESULT QByteArray simplified() const & |
| 286 | { return simplified_helper(a: *this); } |
| 287 | Q_REQUIRED_RESULT QByteArray simplified() && |
| 288 | { return simplified_helper(a&: *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 | |
| 468 | private: |
| 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); |
| 487 | public: |
| 488 | typedef Data * DataPtr; |
| 489 | inline DataPtr &data_ptr() { return d; } |
| 490 | }; |
| 491 | |
| 492 | Q_DECLARE_OPERATORS_FOR_FLAGS(QByteArray::Base64Options) |
| 493 | |
| 494 | inline QByteArray::QByteArray() noexcept : d(Data::sharedNull()) { } |
| 495 | inline QByteArray::~QByteArray() { if (!d->ref.deref()) Data::deallocate(data: d); } |
| 496 | inline int QByteArray::size() const |
| 497 | { return d->size; } |
| 498 | |
| 499 | inline char QByteArray::at(int i) const |
| 500 | { Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; } |
| 501 | inline char QByteArray::operator[](int i) const |
| 502 | { Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; } |
| 503 | inline char QByteArray::operator[](uint i) const |
| 504 | { Q_ASSERT(i < uint(size())); return d->data()[i]; } |
| 505 | |
| 506 | inline bool QByteArray::isEmpty() const |
| 507 | { return d->size == 0; } |
| 508 | #ifndef QT_NO_CAST_FROM_BYTEARRAY |
| 509 | inline QByteArray::operator const char *() const |
| 510 | { return d->data(); } |
| 511 | inline QByteArray::operator const void *() const |
| 512 | { return d->data(); } |
| 513 | #endif |
| 514 | inline char *QByteArray::data() |
| 515 | { detach(); return d->data(); } |
| 516 | inline const char *QByteArray::data() const |
| 517 | { return d->data(); } |
| 518 | inline const char *QByteArray::constData() const |
| 519 | { return d->data(); } |
| 520 | inline void QByteArray::detach() |
| 521 | { if (d->ref.isShared() || (d->offset != sizeof(QByteArrayData))) reallocData(alloc: uint(d->size) + 1u, options: d->detachFlags()); } |
| 522 | inline bool QByteArray::isDetached() const |
| 523 | { return !d->ref.isShared(); } |
| 524 | inline QByteArray::QByteArray(const QByteArray &a) noexcept : d(a.d) |
| 525 | { d->ref.ref(); } |
| 526 | |
| 527 | inline int QByteArray::capacity() const |
| 528 | { return d->alloc ? d->alloc - 1 : 0; } |
| 529 | |
| 530 | inline void QByteArray::reserve(int asize) |
| 531 | { |
| 532 | if (d->ref.isShared() || uint(asize) + 1u > d->alloc) { |
| 533 | reallocData(alloc: qMax(a: uint(size()), b: uint(asize)) + 1u, options: 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 | |
| 541 | inline void QByteArray::squeeze() |
| 542 | { |
| 543 | if (d->ref.isShared() || uint(d->size) + 1u < d->alloc) { |
| 544 | reallocData(alloc: uint(d->size) + 1u, options: 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 | |
| 552 | namespace QtPrivate { |
| 553 | namespace 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 | |
| 568 | class |
| 569 | #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 570 | Q_CORE_EXPORT |
| 571 | #endif |
| 572 | QByteRef { // ### 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; |
| 578 | public: |
| 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(w: WarningType::OutOfRange, c: 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(w: WarningType::OutOfRange, c: EmittingClass::QByteRef); |
| 596 | #endif |
| 597 | a.expand(i); |
| 598 | } else { |
| 599 | #ifdef QT_DEBUG |
| 600 | if (Q_UNLIKELY(!a.isDetached())) |
| 601 | warn(w: WarningType::DelayedDetach, c: 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=(c: 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 | |
| 626 | inline QByteRef QByteArray::operator[](int i) |
| 627 | { Q_ASSERT(i >= 0); detach(); return QByteRef(*this, i); } |
| 628 | inline QByteRef QByteArray::operator[](uint i) |
| 629 | { detach(); return QByteRef(*this, i); } |
| 630 | inline QByteRef QByteArray::front() { return operator[](i: 0); } |
| 631 | inline QByteRef QByteArray::back() { return operator[](i: size() - 1); } |
| 632 | inline QByteArray::iterator QByteArray::begin() |
| 633 | { detach(); return d->data(); } |
| 634 | inline QByteArray::const_iterator QByteArray::begin() const |
| 635 | { return d->data(); } |
| 636 | inline QByteArray::const_iterator QByteArray::cbegin() const |
| 637 | { return d->data(); } |
| 638 | inline QByteArray::const_iterator QByteArray::constBegin() const |
| 639 | { return d->data(); } |
| 640 | inline QByteArray::iterator QByteArray::end() |
| 641 | { detach(); return d->data() + d->size; } |
| 642 | inline QByteArray::const_iterator QByteArray::end() const |
| 643 | { return d->data() + d->size; } |
| 644 | inline QByteArray::const_iterator QByteArray::cend() const |
| 645 | { return d->data() + d->size; } |
| 646 | inline QByteArray::const_iterator QByteArray::constEnd() const |
| 647 | { return d->data() + d->size; } |
| 648 | inline QByteArray &QByteArray::append(int n, char ch) |
| 649 | { return insert(i: d->size, count: n, c: ch); } |
| 650 | inline QByteArray &QByteArray::prepend(int n, char ch) |
| 651 | { return insert(i: 0, count: n, c: ch); } |
| 652 | inline QByteArray &QByteArray::operator+=(char c) |
| 653 | { return append(c); } |
| 654 | inline QByteArray &QByteArray::operator+=(const char *s) |
| 655 | { return append(s); } |
| 656 | inline QByteArray &QByteArray::operator+=(const QByteArray &a) |
| 657 | { return append(a); } |
| 658 | inline void QByteArray::push_back(char c) |
| 659 | { append(c); } |
| 660 | inline void QByteArray::push_back(const char *c) |
| 661 | { append(s: c); } |
| 662 | inline void QByteArray::push_back(const QByteArray &a) |
| 663 | { append(a); } |
| 664 | inline void QByteArray::push_front(char c) |
| 665 | { prepend(c); } |
| 666 | inline void QByteArray::push_front(const char *c) |
| 667 | { prepend(s: c); } |
| 668 | inline void QByteArray::push_front(const QByteArray &a) |
| 669 | { prepend(a); } |
| 670 | inline bool QByteArray::contains(const QByteArray &a) const |
| 671 | { return indexOf(a) != -1; } |
| 672 | inline bool QByteArray::contains(char c) const |
| 673 | { return indexOf(c) != -1; } |
| 674 | inline int QByteArray::compare(const char *c, Qt::CaseSensitivity cs) const noexcept |
| 675 | { |
| 676 | return cs == Qt::CaseSensitive ? qstrcmp(str1: *this, str2: c) : |
| 677 | qstrnicmp(data(), size(), c, -1); |
| 678 | } |
| 679 | inline int QByteArray::compare(const QByteArray &a, Qt::CaseSensitivity cs) const noexcept |
| 680 | { |
| 681 | return cs == Qt::CaseSensitive ? qstrcmp(str1: *this, str2: a) : |
| 682 | qstrnicmp(data(), size(), a.data(), a.size()); |
| 683 | } |
| 684 | inline bool operator==(const QByteArray &a1, const QByteArray &a2) noexcept |
| 685 | { return (a1.size() == a2.size()) && (memcmp(s1: a1.constData(), s2: a2.constData(), n: a1.size())==0); } |
| 686 | inline bool operator==(const QByteArray &a1, const char *a2) noexcept |
| 687 | { return a2 ? qstrcmp(str1: a1,str2: a2) == 0 : a1.isEmpty(); } |
| 688 | inline bool operator==(const char *a1, const QByteArray &a2) noexcept |
| 689 | { return a1 ? qstrcmp(str1: a1,str2: a2) == 0 : a2.isEmpty(); } |
| 690 | inline bool operator!=(const QByteArray &a1, const QByteArray &a2) noexcept |
| 691 | { return !(a1==a2); } |
| 692 | inline bool operator!=(const QByteArray &a1, const char *a2) noexcept |
| 693 | { return a2 ? qstrcmp(str1: a1,str2: a2) != 0 : !a1.isEmpty(); } |
| 694 | inline bool operator!=(const char *a1, const QByteArray &a2) noexcept |
| 695 | { return a1 ? qstrcmp(str1: a1,str2: a2) != 0 : !a2.isEmpty(); } |
| 696 | inline bool operator<(const QByteArray &a1, const QByteArray &a2) noexcept |
| 697 | { return qstrcmp(str1: a1, str2: a2) < 0; } |
| 698 | inline bool operator<(const QByteArray &a1, const char *a2) noexcept |
| 699 | { return qstrcmp(str1: a1, str2: a2) < 0; } |
| 700 | inline bool operator<(const char *a1, const QByteArray &a2) noexcept |
| 701 | { return qstrcmp(str1: a1, str2: a2) < 0; } |
| 702 | inline bool operator<=(const QByteArray &a1, const QByteArray &a2) noexcept |
| 703 | { return qstrcmp(str1: a1, str2: a2) <= 0; } |
| 704 | inline bool operator<=(const QByteArray &a1, const char *a2) noexcept |
| 705 | { return qstrcmp(str1: a1, str2: a2) <= 0; } |
| 706 | inline bool operator<=(const char *a1, const QByteArray &a2) noexcept |
| 707 | { return qstrcmp(str1: a1, str2: a2) <= 0; } |
| 708 | inline bool operator>(const QByteArray &a1, const QByteArray &a2) noexcept |
| 709 | { return qstrcmp(str1: a1, str2: a2) > 0; } |
| 710 | inline bool operator>(const QByteArray &a1, const char *a2) noexcept |
| 711 | { return qstrcmp(str1: a1, str2: a2) > 0; } |
| 712 | inline bool operator>(const char *a1, const QByteArray &a2) noexcept |
| 713 | { return qstrcmp(str1: a1, str2: a2) > 0; } |
| 714 | inline bool operator>=(const QByteArray &a1, const QByteArray &a2) noexcept |
| 715 | { return qstrcmp(str1: a1, str2: a2) >= 0; } |
| 716 | inline bool operator>=(const QByteArray &a1, const char *a2) noexcept |
| 717 | { return qstrcmp(str1: a1, str2: a2) >= 0; } |
| 718 | inline bool operator>=(const char *a1, const QByteArray &a2) noexcept |
| 719 | { return qstrcmp(str1: a1, str2: a2) >= 0; } |
| 720 | #if !defined(QT_USE_QSTRINGBUILDER) |
| 721 | inline const QByteArray operator+(const QByteArray &a1, const QByteArray &a2) |
| 722 | { return QByteArray(a1) += a2; } |
| 723 | inline const QByteArray operator+(const QByteArray &a1, const char *a2) |
| 724 | { return QByteArray(a1) += a2; } |
| 725 | inline const QByteArray operator+(const QByteArray &a1, char a2) |
| 726 | { return QByteArray(a1) += a2; } |
| 727 | inline const QByteArray operator+(const char *a1, const QByteArray &a2) |
| 728 | { return QByteArray(a1) += a2; } |
| 729 | inline const QByteArray operator+(char a1, const QByteArray &a2) |
| 730 | { return QByteArray(&a1, 1) += a2; } |
| 731 | #endif // QT_USE_QSTRINGBUILDER |
| 732 | inline bool QByteArray::contains(const char *c) const |
| 733 | { return indexOf(c) != -1; } |
| 734 | inline QByteArray &QByteArray::replace(char before, const char *c) |
| 735 | { return replace(before: &before, bsize: 1, after: c, asize: qstrlen(str: c)); } |
| 736 | inline QByteArray &QByteArray::replace(const QByteArray &before, const char *c) |
| 737 | { return replace(before: before.constData(), bsize: before.size(), after: c, asize: qstrlen(str: c)); } |
| 738 | inline QByteArray &QByteArray::replace(const char *before, const char *after) |
| 739 | { return replace(before, bsize: qstrlen(str: before), after, asize: qstrlen(str: after)); } |
| 740 | |
| 741 | inline QByteArray &QByteArray::setNum(short n, int base) |
| 742 | { return base == 10 ? setNum(qlonglong(n), base) : setNum(qulonglong(ushort(n)), base); } |
| 743 | inline QByteArray &QByteArray::setNum(ushort n, int base) |
| 744 | { return setNum(qulonglong(n), base); } |
| 745 | inline QByteArray &QByteArray::setNum(int n, int base) |
| 746 | { return base == 10 ? setNum(qlonglong(n), base) : setNum(qulonglong(uint(n)), base); } |
| 747 | inline QByteArray &QByteArray::setNum(uint n, int base) |
| 748 | { return setNum(qulonglong(n), base); } |
| 749 | inline QByteArray &QByteArray::setNum(float n, char f, int prec) |
| 750 | { return setNum(double(n),f,prec); } |
| 751 | |
| 752 | inline std::string QByteArray::toStdString() const |
| 753 | { return std::string(constData(), length()); } |
| 754 | |
| 755 | inline 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)) |
| 759 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QByteArray &); |
| 760 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QByteArray &); |
| 761 | #endif |
| 762 | |
| 763 | #ifndef QT_NO_COMPRESS |
| 764 | Q_CORE_EXPORT QByteArray qCompress(const uchar* data, int nbytes, int compressionLevel = -1); |
| 765 | Q_CORE_EXPORT QByteArray qUncompress(const uchar* data, int nbytes); |
| 766 | inline QByteArray qCompress(const QByteArray& data, int compressionLevel = -1) |
| 767 | { return qCompress(data: reinterpret_cast<const uchar *>(data.constData()), nbytes: data.size(), compressionLevel); } |
| 768 | inline QByteArray qUncompress(const QByteArray& data) |
| 769 | { return qUncompress(data: reinterpret_cast<const uchar*>(data.constData()), nbytes: data.size()); } |
| 770 | #endif |
| 771 | |
| 772 | Q_DECLARE_SHARED(QByteArray) |
| 773 | |
| 774 | class QByteArray::FromBase64Result |
| 775 | { |
| 776 | public: |
| 777 | QByteArray decoded; |
| 778 | QByteArray::Base64DecodingStatus decodingStatus; |
| 779 | |
| 780 | void swap(QByteArray::FromBase64Result &other) noexcept |
| 781 | { |
| 782 | qSwap(value1&: decoded, value2&: other.decoded); |
| 783 | qSwap(value1&: decodingStatus, value2&: 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 | |
| 798 | Q_DECLARE_SHARED(QByteArray::FromBase64Result) |
| 799 | |
| 800 | inline 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 | |
| 811 | inline bool operator!=(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept |
| 812 | { |
| 813 | return !operator==(lhs, rhs); |
| 814 | } |
| 815 | |
| 816 | Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QByteArray::FromBase64Result &key, uint seed = 0) noexcept; |
| 817 | |
| 818 | QT_END_NAMESPACE |
| 819 | |
| 820 | #endif // QBYTEARRAY_H |
| 821 | |