📄 qbytearray.cpp
字号:
This operator is mostly useful to pass a byte array to a function that accepts a void *. \sa constData()*//*! \fn char *QByteArray::data() Returns a pointer to the data stored in the byte array. The pointer can be used to access and modify the bytes that compose the array. The data is '\\0'-terminated. Example: \code QByteArray ba("Hello world"); char *data = ba.data(); while (*data) { cout << "[" << *data << "]" << endl; ++data; } \endcode The pointer remains valid as long as the byte array isn't reallocated. For read-only access, constData() is faster because it never causes a \l{deep copy} to occur. This function is mostly useful to pass a byte array to a function that accepts a \c{const char *}. Note: A QByteArray can store any byte values including '\\0's, but most functions that take \c{char *} arguments assume that the data ends at the first '\\0' they encounter. \sa constData(), operator[]()*//*! \fn const char *QByteArray::data() const \overload*//*! \fn const char *QByteArray::constData() const Returns a pointer to the data stored in the byte array. The pointer can be used to access the bytes that compose the array. The data is '\\0'-terminated. The pointer remains valid as long as the byte array isn't reallocated. This function is mostly useful to pass a byte array to a function that accepts a \c{const char *}. Note: A QByteArray can store any byte values including '\\0's, but most functions that take \c{char *} arguments assume that the data ends at the first '\\0' they encounter. \sa data(), operator[]()*//*! \fn void QByteArray::detach() \internal*//*! \fn bool QByteArray::isDetached() const \internal*//*! \fn char QByteArray::at(int i) const Returns the character at index position \a i in the byte array. \a i must be a valid index position in the byte array (i.e., 0 <= \a i < size()). \sa operator[]()*//*! \fn QByteRef QByteArray::operator[](int i) Returns the byte at index position \a i as a modifiable reference. If an assignment is made beyond the end of the byte array, the array is extended with resize() before the assignment takes place. Example: \code QByteArray ba; for (int i = 0; i < 10; ++i) ba[i] = 'A' + i; // ba == "ABCDEFGHIJ" \endcode The return value is of type QByteRef, a helper class for QByteArray. When you get an object of type QByteRef, you can use it as if it were a char &. If you assign to it, the assignment will apply to the character in the QByteArray from which you got the reference. \sa at()*//*! \fn char QByteArray::operator[](int i) const \overload Same as at(\a i).*//*! \fn QByteRef QByteArray::operator[](uint i) \overload*//*! \fn char QByteArray::operator[](uint i) const \overload*//*! \fn QBool QByteArray::contains(const QByteArray &ba) const Returns true if the byte array contains an occurrence of the byte array \a ba; otherwise returns false. \sa indexOf(), count()*//*! \fn QBool QByteArray::contains(const char *str) const \overload Returns true if the byte array contains the string \a str; otherwise returns false.*//*! \fn QBool QByteArray::contains(char ch) const \overload Returns true if the byte array contains the character \a ch; otherwise returns false.*//*! Truncates the byte array at index position \a pos. If \a pos is beyond the end of the array, nothing happens. Example: \code QByteArray ba("Stockholm"); ba.truncate(5); // ba == "Stock" \endcode \sa chop(), resize(), left()*/void QByteArray::truncate(int pos){ if (pos < d->size) resize(pos);}/*! Removes \a n bytes from the end of the byte array. If \a n is greater than size(), the result is an empty byte array. Example: \code QByteArray ba("STARTTLS\r\n"); ba.chop(2); // ba == "STARTTLS" \endcode \sa truncate(), resize(), left()*/void QByteArray::chop(int n){ if (n > 0) resize(d->size - n);}/*! \fn QByteArray &QByteArray::operator+=(const QByteArray &ba) Appends the byte array \a ba onto the end of this byte array and returns a reference to this byte array. Example: \code QByteArray x("free"); QByteArray y("dom"); x += y; // x == "freedom" \endcode This operation is typically very fast (\l{constant time}), because QByteArray preallocates extra space at the end of the character data so it can grow without reallocating the entire data each time. \sa append(), prepend()*//*! \fn QByteArray &QByteArray::operator+=(const QString &str) \overload Appends the string \a str onto the end of this byte array and returns a reference to this byte array. The Unicode data is converted into 8-bit characters using QString::toAscii(). If the QString contains non-ASCII Unicode characters, using this operator can lead to loss of information. You can disable this operator by defining \c QT_NO_CAST_TO_ASCII when you compile your applications. You then need to call QString::toAscii() (or QString::toLatin1() or QString::toUtf8() or QString::toLocal8Bit()) explicitly if you want to convert the data to \c{const char *}.*//*! \fn QByteArray &QByteArray::operator+=(const char *str) \overload Appends the string \a str onto the end of this byte array and returns a reference to this byte array.*//*! \fn QByteArray &QByteArray::operator+=(char ch) \overload Appends the character \a ch onto the end of this byte array and returns a reference to this byte array.*//*! \fn int QByteArray::length() const Same as size().*//*! \fn bool QByteArray::isNull() const Returns true if this byte array is null; otherwise returns false. Example: \code QByteArray().isNull(); // returns true QByteArray("").isNull(); // returns false QByteArray("abc").isNull(); // returns false \endcode Qt makes a distinction between null byte arrays and empty byte arrays for historical reasons. For most applications, what matters is whether or not a byte array contains any data, and this can be determined using isEmpty(). \sa isEmpty()*//*! \fn QByteArray::QByteArray() Constructs an empty byte array. \sa isEmpty()*//*! \fn QByteArray::QByteArray(const char *str) Constructs a byte array initialized with the string \a str. QByteArray makes a deep copy of the string data.*/QByteArray::QByteArray(const char *str){ if (!str) { d = &shared_null; } else if (!*str) { d = &shared_empty; } else { int len = qstrlen(str); d = static_cast<Data *>(qMalloc(sizeof(Data)+len)); if (!d) { d = &shared_null; } else { d->ref.init(0); d->alloc = d->size = len; d->data = d->array; memcpy(d->array, str, len+1); // include null terminator } } d->ref.ref();}/*! Constructs a byte array containing the first \a size bytes of array \a data. If \a data is 0, a null byte array is constructed. QByteArray makes a deep copy of the string data. \sa fromRawData()*/QByteArray::QByteArray(const char *data, int size){ if (!data) { d = &shared_null; } else if (size <= 0) { d = &shared_empty; } else { d = static_cast<Data *>(qMalloc(sizeof(Data) + size)); if (!d) { d = &shared_null; } else { d->ref.init(0); d->alloc = d->size = size; d->data = d->array; memcpy(d->array, data, size); d->array[size] = '\0'; } } d->ref.ref();}/*! Constructs a byte array of size \a size with every byte set to character \a ch. \sa fill()*/QByteArray::QByteArray(int size, char ch){ if (size <= 0) { d = &shared_null; } else { d = static_cast<Data *>(qMalloc(sizeof(Data)+size)); if (!d) { d = &shared_null; } else { d->ref.init(0); d->alloc = d->size = size; d->data = d->array; d->array[size] = '\0'; memset(d->array, ch, size); } } d->ref.ref();}/*! Sets the size of the byte array to \a size bytes. If \a size is greater than the current size, the byte array is extended to make it \a size bytes with the extra bytes added to the end. The new bytes are uninitialized. If \a size is less than the current size, bytes are removed from the end. \sa size()*/void QByteArray::resize(int size){ if (size <= 0) { Data *x = &shared_empty; x->ref.ref(); x = qAtomicSetPtr(&d, x); if (!x->ref.deref()) qFree(x); } else if ( d == &shared_null ) { // // Optimize the idiom: // QByteArray a; // a.resize(sz); // ... // which is used in place of the Qt 3 idiom: // QByteArray a(sz); // Data *x = static_cast<Data *>(qMalloc(sizeof(Data)+size)); if (!x) return; x->ref.init(1); x->alloc = x->size = size; x->data = x->array; x->array[size] = '\0'; x = qAtomicSetPtr(&d, x); (void) x->ref.deref(); // cannot be 0, x points to shared_null } else { if (d->ref != 1 || size > d->alloc || (size < d->size && size < d->alloc >> 1)) realloc(qAllocMore(size, sizeof(Data))); if (d->alloc >= size) { d->size = size; if (d->data == d->array) { d->array[size] = '\0'; } } }}/*! Sets every byte in the byte array to character \a ch. If \a size is different from -1 (the default), the byte array is resized to size \a size beforehand. Example: \code QByteArray ba("Istambul"); ba.fill('o'); // ba == "oooooooo" ba.fill('X', 2); // ba == "XX" \endcode \sa resize()*/QByteArray &QByteArray::fill(char ch, int size){ resize(size < 0 ? d->size : size); if (d->size) memset(d->data, ch, d->size); return *this;}void QByteArray::realloc(int alloc){ if (d->ref != 1 || d->data != d->array) { Data *x = static_cast<Data *>(qMalloc(sizeof(Data) + alloc)); if (!x) return; x->size = qMin(alloc, d->size); ::memcpy(x->array, d->data, x->size); x->array[x->size] = '\0'; x->ref.init(1); x->alloc = alloc; x->data = x->array; x = qAtomicSetPtr(&d, x); if (!x->ref.deref()) qFree(x); } else { Data *x = static_cast<Data *>(qRealloc(d, sizeof(Data) + alloc)); if (!x) return; x->alloc = alloc; x->data = x->array; d = x; }}void QByteArray::expand(int i){ resize(qMax(i + 1, d->size));}/*! Prepends the byte array \a ba to this byte array and returns a reference to this byte array. Example: \code
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -