diff options
Diffstat (limited to 'src/corelib/tools/qbytearray.cpp')
-rw-r--r-- | src/corelib/tools/qbytearray.cpp | 125 |
1 files changed, 63 insertions, 62 deletions
diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index 31cf65b78d..b67d655a9d 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -131,7 +131,7 @@ char *qstrcpy(char *dst, const char *src) if (!src) return 0; #if defined(_MSC_VER) && _MSC_VER >= 1400 - int len = strlen(src); + const int len = int(strlen(src)); // This is actually not secure!!! It will be fixed // properly in a later release! if (len >= 0 && strcpy_s(dst, len+1, src) == 0) @@ -648,7 +648,7 @@ static inline char qToLower(char c) char *} to its constructor. For example, the following code creates a byte array of size 5 containing the data "Hello": - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 0 + \snippet code/src_corelib_tools_qbytearray.cpp 0 Although the size() is 5, the byte array also maintains an extra '\\0' character at the end so that if a function is used that @@ -668,11 +668,11 @@ static inline char qToLower(char c) arrays, operator[]() returns a reference to a byte that can be used on the left side of an assignment. For example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 1 + \snippet code/src_corelib_tools_qbytearray.cpp 1 For read-only access, an alternative syntax is to use at(): - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 2 + \snippet code/src_corelib_tools_qbytearray.cpp 2 at() can be faster than operator[](), because it never causes a \l{deep copy} to occur. @@ -700,7 +700,7 @@ static inline char qToLower(char c) the byte data: append(), prepend(), insert(), replace(), and remove(). For example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 3 + \snippet code/src_corelib_tools_qbytearray.cpp 3 The replace() and remove() functions' first two arguments are the position from which to start erasing and the number of bytes that @@ -728,7 +728,7 @@ static inline char qToLower(char c) For example, here's a typical loop that finds all occurrences of a particular substring: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 4 + \snippet code/src_corelib_tools_qbytearray.cpp 4 If you simply want to check whether a QByteArray contains a particular character or substring, use contains(). If you want to @@ -752,7 +752,7 @@ static inline char qToLower(char c) array is always empty, but an empty byte array isn't necessarily null: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 5 + \snippet code/src_corelib_tools_qbytearray.cpp 5 All functions except isNull() treat null byte arrays the same as empty byte arrays. For example, data() returns a pointer to a @@ -914,12 +914,13 @@ QByteArray &QByteArray::operator=(const char *str) } else if (!*str) { x = Data::allocate(0); } else { - int len = strlen(str); - if (d->ref.isShared() || uint(len) + 1u > d->alloc - || (len < d->size && uint(len) + 1u < uint(d->alloc >> 1))) - reallocData(uint(len) + 1u, d->detachFlags()); + const int len = int(strlen(str)); + const uint fullLen = len + 1; + if (d->ref.isShared() || fullLen > d->alloc + || (len < d->size && fullLen < uint(d->alloc >> 1))) + reallocData(fullLen, d->detachFlags()); x = d; - memcpy(x->data(), str, uint(len) + 1u); // include null terminator + memcpy(x->data(), str, fullLen); // include null terminator x->size = len; } x->ref.ref(); @@ -949,7 +950,7 @@ QByteArray &QByteArray::operator=(const char *str) automaticall unless the \l{deep copy} is created. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 6 + \snippet code/src_corelib_tools_qbytearray.cpp 6 \sa isEmpty(), resize() */ @@ -959,7 +960,7 @@ QByteArray &QByteArray::operator=(const char *str) Returns true if the byte array has size 0; otherwise returns false. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 7 + \snippet code/src_corelib_tools_qbytearray.cpp 7 \sa size() */ @@ -1046,7 +1047,7 @@ QByteArray &QByteArray::operator=(const char *str) '\\0' terminator. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 8 + \snippet code/src_corelib_tools_qbytearray.cpp 8 The pointer remains valid as long as the byte array isn't reallocated or destroyed. For read-only access, constData() is @@ -1059,11 +1060,11 @@ QByteArray &QByteArray::operator=(const char *str) data(), but it will corrupt the heap and cause a crash because it does not allocate a byte for the '\\0' at the end: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 46 + \snippet code/src_corelib_tools_qbytearray.cpp 46 This one allocates the correct amount of space: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 47 + \snippet code/src_corelib_tools_qbytearray.cpp 47 Note: A QByteArray can store any byte values including '\\0's, but most functions that take \c{char *} arguments assume that the @@ -1129,7 +1130,7 @@ QByteArray &QByteArray::operator=(const char *str) place. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 9 + \snippet code/src_corelib_tools_qbytearray.cpp 9 The return value is of type QByteRef, a helper class for QByteArray. When you get an object of type QByteRef, you can use @@ -1188,7 +1189,7 @@ QByteArray &QByteArray::operator=(const char *str) If \a pos is beyond the end of the array, nothing happens. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 10 + \snippet code/src_corelib_tools_qbytearray.cpp 10 \sa chop(), resize(), left() */ @@ -1206,7 +1207,7 @@ void QByteArray::truncate(int pos) array. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 11 + \snippet code/src_corelib_tools_qbytearray.cpp 11 \sa truncate(), resize(), left() */ @@ -1224,7 +1225,7 @@ void QByteArray::chop(int n) returns a reference to this byte array. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 12 + \snippet code/src_corelib_tools_qbytearray.cpp 12 Note: QByteArray is an \l{implicitly shared} class. Consequently, if \e this is an empty QByteArray, then \e this will just share @@ -1284,7 +1285,7 @@ void QByteArray::chop(int n) Returns true if this byte array is null; otherwise returns false. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 13 + \snippet code/src_corelib_tools_qbytearray.cpp 13 Qt makes a distinction between null byte arrays and empty byte arrays for historical reasons. For most applications, what @@ -1322,7 +1323,7 @@ QByteArray::QByteArray(const char *data, int size) d = Data::sharedNull(); } else { if (size < 0) - size = strlen(data); + size = int(strlen(data)); if (!size) { d = Data::allocate(0); } else { @@ -1428,7 +1429,7 @@ void QByteArray::resize(int size) size \a size beforehand. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 14 + \snippet code/src_corelib_tools_qbytearray.cpp 14 \sa resize() */ @@ -1493,7 +1494,7 @@ QByteArray QByteArray::nulTerminated() const reference to this byte array. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 15 + \snippet code/src_corelib_tools_qbytearray.cpp 15 This is the same as insert(0, \a ba). @@ -1573,7 +1574,7 @@ QByteArray &QByteArray::prepend(char ch) Appends the byte array \a ba onto the end of this byte array. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 16 + \snippet code/src_corelib_tools_qbytearray.cpp 16 This is the same as insert(size(), \a ba). @@ -1631,7 +1632,7 @@ QByteArray &QByteArray::append(const QByteArray &ba) QByteArray& QByteArray::append(const char *str) { if (str) { - int len = strlen(str); + const int len = int(strlen(str)); if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc) reallocData(uint(d->size + len) + 1u, d->detachFlags() | Data::Grow); memcpy(d->data() + d->size, str, len + 1); // include null terminator @@ -1710,7 +1711,7 @@ static inline QByteArray &qbytearray_insert(QByteArray *ba, reference to this byte array. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 17 + \snippet code/src_corelib_tools_qbytearray.cpp 17 \sa append(), prepend(), replace(), remove() */ @@ -1793,7 +1794,7 @@ QByteArray &QByteArray::insert(int i, char ch) array is truncated at position \a pos. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 18 + \snippet code/src_corelib_tools_qbytearray.cpp 18 \sa insert(), replace() */ @@ -1817,7 +1818,7 @@ QByteArray &QByteArray::remove(int pos, int len) array \a after, and returns a reference to this byte array. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 19 + \snippet code/src_corelib_tools_qbytearray.cpp 19 \sa insert(), remove() */ @@ -1855,7 +1856,7 @@ QByteArray &QByteArray::replace(int pos, int len, const char *after) \overload Replaces \a len bytes from index position \a pos with \a alen bytes - from the string \a after. \a after is allowed to have '\0' characters. + from the string \a after. \a after is allowed to have '\\0' characters. \since 4.7 */ @@ -1881,7 +1882,7 @@ QByteArray &QByteArray::replace(int pos, int len, const char *after, int alen) byte array \a after. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 20 + \snippet code/src_corelib_tools_qbytearray.cpp 20 */ QByteArray &QByteArray::replace(const QByteArray &before, const QByteArray &after) @@ -2214,7 +2215,7 @@ QByteArray QByteArray::repeated(int times) const position \a from. Returns -1 if \a ba could not be found. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 21 + \snippet code/src_corelib_tools_qbytearray.cpp 21 \sa lastIndexOf(), contains(), count() */ @@ -2284,7 +2285,7 @@ int QByteArray::indexOf(const char *c, int from) const position \a from. Returns -1 if \a ch could not be found. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 22 + \snippet code/src_corelib_tools_qbytearray.cpp 22 \sa lastIndexOf(), contains() */ @@ -2346,7 +2347,7 @@ static int lastIndexOfHelper(const char *haystack, int l, const char *needle, in starts at the last byte. Returns -1 if \a ba could not be found. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 23 + \snippet code/src_corelib_tools_qbytearray.cpp 23 \sa indexOf(), contains(), count() */ @@ -2406,7 +2407,7 @@ int QByteArray::lastIndexOf(const char *str, int from) const last (size() - 1) byte. Returns -1 if \a ch could not be found. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 24 + \snippet code/src_corelib_tools_qbytearray.cpp 24 \sa indexOf(), contains() */ @@ -2493,7 +2494,7 @@ int QByteArray::count(char ch) const otherwise returns false. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 25 + \snippet code/src_corelib_tools_qbytearray.cpp 25 \sa endsWith(), left() */ @@ -2515,7 +2516,7 @@ bool QByteArray::startsWith(const char *str) const { if (!str || !*str) return true; - int len = strlen(str); + const int len = int(strlen(str)); if (d->size < len) return false; return qstrncmp(d->data(), str, len) == 0; @@ -2538,7 +2539,7 @@ bool QByteArray::startsWith(char ch) const otherwise returns false. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 26 + \snippet code/src_corelib_tools_qbytearray.cpp 26 \sa startsWith(), right() */ @@ -2560,7 +2561,7 @@ bool QByteArray::endsWith(const char *str) const { if (!str || !*str) return true; - int len = strlen(str); + const int len = int(strlen(str)); if (d->size < len) return false; return qstrncmp(d->data() + d->size - len, str, len) == 0; @@ -2586,7 +2587,7 @@ bool QByteArray::endsWith(char ch) const size(). Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 27 + \snippet code/src_corelib_tools_qbytearray.cpp 27 \sa right(), mid(), startsWith(), truncate() */ @@ -2608,7 +2609,7 @@ QByteArray QByteArray::left(int len) const size(). Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 28 + \snippet code/src_corelib_tools_qbytearray.cpp 28 \sa endsWith(), left(), mid() */ @@ -2631,7 +2632,7 @@ QByteArray QByteArray::right(int len) const pos until the end of the byte array. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 29 + \snippet code/src_corelib_tools_qbytearray.cpp 29 \sa left(), right() */ @@ -2658,7 +2659,7 @@ QByteArray QByteArray::mid(int pos, int len) const interpreted as a Latin-1 encoded string. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 30 + \snippet code/src_corelib_tools_qbytearray.cpp 30 \sa toUpper(), {8-bit Character Comparisons} */ @@ -2680,7 +2681,7 @@ QByteArray QByteArray::toLower() const interpreted as a Latin-1 encoded string. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 31 + \snippet code/src_corelib_tools_qbytearray.cpp 31 \sa toLower(), {8-bit Character Comparisons} */ @@ -3090,7 +3091,7 @@ QDataStream &operator>>(QDataStream &in, QByteArray &ba) characters '\\t', '\\n', '\\v', '\\f', '\\r', and ' '. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 32 + \snippet code/src_corelib_tools_qbytearray.cpp 32 \sa trimmed() */ @@ -3128,7 +3129,7 @@ QByteArray QByteArray::simplified() const characters '\\t', '\\n', '\\v', '\\f', '\\r', and ' '. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 33 + \snippet code/src_corelib_tools_qbytearray.cpp 33 Unlike simplified(), trimmed() leaves internal whitespace alone. @@ -3170,7 +3171,7 @@ QByteArray QByteArray::trimmed() const after position \a width are removed, and the copy is returned. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 34 + \snippet code/src_corelib_tools_qbytearray.cpp 34 \sa rightJustified() */ @@ -3207,7 +3208,7 @@ QByteArray QByteArray::leftJustified(int width, char fill, bool truncate) const position \a width. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 35 + \snippet code/src_corelib_tools_qbytearray.cpp 35 \sa leftJustified() */ @@ -3314,7 +3315,7 @@ qulonglong QByteArray::toULongLong(bool *ok, int base) const If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to false; otherwise *\a{ok} is set to true. - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 36 + \snippet code/src_corelib_tools_qbytearray.cpp 36 \note The conversion of the number is performed in the default C locale, irrespective of the user's locale. @@ -3380,7 +3381,7 @@ uint QByteArray::toUInt(bool *ok, int base) const If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to false; otherwise *\a{ok} is set to true. - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 37 + \snippet code/src_corelib_tools_qbytearray.cpp 37 \note The conversion of the number is performed in the default C locale, irrespective of the user's locale. @@ -3501,7 +3502,7 @@ ushort QByteArray::toUShort(bool *ok, int base) const If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to false; otherwise *\a{ok} is set to true. - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 38 + \snippet code/src_corelib_tools_qbytearray.cpp 38 \note The conversion of the number is performed in the default C locale, irrespective of the user's locale. @@ -3536,7 +3537,7 @@ float QByteArray::toFloat(bool *ok) const /*! Returns a copy of the byte array, encoded as Base64. - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 39 + \snippet code/src_corelib_tools_qbytearray.cpp 39 The algorithm used to encode Base64-encoded data is defined in \l{RFC 2045}. @@ -3588,7 +3589,7 @@ QByteArray QByteArray::toBase64() const be any value between 2 and 36. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 40 + \snippet code/src_corelib_tools_qbytearray.cpp 40 \note The format of the number is not localized; the default C locale is used irrespective of the user's locale. @@ -3632,7 +3633,7 @@ QByteArray &QByteArray::setNum(qlonglong n, int base) } #endif QLocale locale(QLocale::C); - *this = locale.d()->longLongToString(n, -1, base).toLatin1(); + *this = locale.d->longLongToString(n, -1, base).toLatin1(); return *this; } @@ -3651,7 +3652,7 @@ QByteArray &QByteArray::setNum(qulonglong n, int base) } #endif QLocale locale(QLocale::C); - *this = locale.d()->unsLongLongToString(n, -1, base).toLatin1(); + *this = locale.d->unsLongLongToString(n, -1, base).toLatin1(); return *this; } @@ -3710,7 +3711,7 @@ QByteArray &QByteArray::setNum(double n, char f, int prec) } QLocale locale(QLocale::C); - *this = locale.d()->doubleToString(n, prec, form, -1, flags).toLatin1(); + *this = locale.d->doubleToString(n, prec, form, -1, flags).toLatin1(); return *this; } @@ -3734,7 +3735,7 @@ QByteArray &QByteArray::setNum(double n, char f, int prec) any value between 2 and 36. Example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 41 + \snippet code/src_corelib_tools_qbytearray.cpp 41 \note The format of the number is not localized; the default C locale is used irrespective of the user's locale. @@ -3806,7 +3807,7 @@ QByteArray QByteArray::number(qulonglong n, int base) decimal point. With 'g' and 'G', \a prec is the maximum number of significant digits (trailing zeroes are omitted). - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 42 + \snippet code/src_corelib_tools_qbytearray.cpp 42 \note The format of the number is not localized; the default C locale is used irrespective of the user's locale. @@ -3843,7 +3844,7 @@ QByteArray QByteArray::number(double n, char f, int prec) Here is an example of how to read data using a QDataStream on raw data in memory without copying the raw data into a QByteArray: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 43 + \snippet code/src_corelib_tools_qbytearray.cpp 43 \warning A byte array created with fromRawData() is \e not null-terminated, unless the raw data contains a 0 character at @@ -3908,7 +3909,7 @@ QByteArray &QByteArray::setRawData(const char *data, uint size) For example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 44 + \snippet code/src_corelib_tools_qbytearray.cpp 44 The algorithm used to decode Base64-encoded data is defined in \l{RFC 2045}. @@ -3960,7 +3961,7 @@ QByteArray QByteArray::fromBase64(const QByteArray &base64) For example: - \snippet doc/src/snippets/code/src_corelib_tools_qbytearray.cpp 45 + \snippet code/src_corelib_tools_qbytearray.cpp 45 \sa toHex() */ |