?? qbytearray.cpp
字號:
int len = qstrlen(str); if (d->ref != 1 || d->size + len > d->alloc) realloc(qAllocMore(d->size + len, sizeof(Data))); memmove(d->data+len, d->data, d->size); memcpy(d->data, str, len); d->size += len; d->data[d->size] = '\0'; } return *this;}/*! \overload Prepends the character \a ch to this byte array.*/QByteArray &QByteArray::prepend(char ch){ if (d->ref != 1 || d->size + 1 > d->alloc) realloc(qAllocMore(d->size + 1, sizeof(Data))); memmove(d->data+1, d->data, d->size); d->data[0] = ch; ++d->size; d->data[d->size] = '\0'; return *this;}/*! Appends the byte array \a ba onto the end of this byte array. Example: \code QByteArray x("free"); QByteArray y("dom"); x.append(y); // x == "freedom" \endcode This is the same as insert(size(), \a ba). 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 operator+=(), prepend(), insert()*/QByteArray &QByteArray::append(const QByteArray &ba){ if (d == &shared_null || d == &shared_empty) { *this = ba; } else if (ba.d != &shared_null) { if (d->ref != 1 || d->size + ba.d->size > d->alloc) realloc(qAllocMore(d->size + ba.d->size, sizeof(Data))); memcpy(d->data + d->size, ba.d->data, ba.d->size); d->size += ba.d->size; d->data[d->size] = '\0'; } return *this;}/*! \fn QByteArray &QByteArray::append(const QString &str) \overload Appends the string \a str 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 function can lead to loss of information. You can disable this function 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 *}.*//*! \overload Appends the string \a str to this byte array.*/QByteArray& QByteArray::append(const char *str){ if (str) { int len = qstrlen(str); if (d->ref != 1 || d->size + len > d->alloc) realloc(qAllocMore(d->size + len, sizeof(Data))); memcpy(d->data + d->size, str, len + 1); // include null terminator d->size += len; } return *this;}/*! \overload Appends the character \a ch to this byte array.*/QByteArray& QByteArray::append(char ch){ if (d->ref != 1 || d->size + 1 > d->alloc) realloc(qAllocMore(d->size + 1, sizeof(Data))); d->data[d->size++] = ch; d->data[d->size] = '\0'; return *this;}/*! \internal Inserts \a len bytes from the array \a arr at position \a pos and returns a reference the modified byte array.*/static inline QByteArray &qbytearray_insert(QByteArray *ba, int pos, const char *arr, int len){ Q_ASSERT(pos >= 0); if (pos < 0 || len <= 0 || arr == 0) return *ba; int oldsize = ba->size(); ba->resize(qMax(pos, oldsize) + len); char *dst = ba->data(); if (pos > oldsize) ::memset(dst + oldsize, 0x20, pos - oldsize); else ::memmove(dst + pos + len, dst + pos, oldsize - pos); memcpy(dst + pos, arr, len); return *ba;}/*! Inserts the byte array \a ba at index position \a i and returns a reference to this byte array. Example: \code QByteArray ba("Meal"); ba.insert(1, QByteArray("ontr")); // ba == "Montreal" \endcode \sa append(), prepend(), replace(), remove()*/QByteArray &QByteArray::insert(int i, const QByteArray &ba){ QByteArray copy(ba); return qbytearray_insert(this, i, copy.d->data, copy.d->size);}/*! \fn QByteArray &QByteArray::insert(int i, const QString &str) \overload Inserts the string \a str at index position \a i in the byte array. The Unicode data is converted into 8-bit characters using QString::toAscii(). If \a i is greater than size(), the array is first extended using resize(). If the QString contains non-ASCII Unicode characters, using this function can lead to loss of information. You can disable this function 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 *}.*//*! \overload Inserts the string \a str at position \a i in the byte array. If \a i is greater than size(), the array is first extended using resize().*/QByteArray &QByteArray::insert(int i, const char *str){ return qbytearray_insert(this, i, str, qstrlen(str));}/*! \overload Inserts character \a ch at index position \a i in the byte array. If \a i is greater than size(), the array is first extended using resize().*/QByteArray &QByteArray::insert(int i, char ch){ return qbytearray_insert(this, i, &ch, 1);}/*! Removes \a len bytes from the array, starting at index position \a pos, and returns a reference to the array. If \a pos is out of range, nothing happens. If \a pos is valid, but \a pos + \a len is larger than the size of the array, the array is truncated at position \a pos. Example: \code QByteArray ba("Montreal"); ba.remove(1, 4); // ba == "Meal" \endcode \sa insert(), replace()*/QByteArray &QByteArray::remove(int pos, int len){ if (len <= 0 || pos >= d->size || pos < 0) return *this; detach(); if (pos + len >= d->size) { resize(pos); } else { memmove(d->data + pos, d->data + pos + len, d->size - pos - len); resize(d->size - len); } return *this;}/*! Replaces \a len bytes from index position \a pos with the byte array \a after, and returns a reference to this byte array. Example: \code QByteArray x("Say yes!"); QByteArray y("no"); x.replace(4, 3, y); // x == "Say no!" \endcode \sa insert(), remove()*/QByteArray &QByteArray::replace(int pos, int len, const QByteArray &after){ QByteArray copy(after); remove(pos, len); return insert(pos, copy);}/*! \fn QByteArray &QByteArray::replace(int pos, int len, const char *after) \overload*//*! \overload Replaces every occurrence of the byte array \a before with the byte array \a after. Example: \code QByteArray ba("colour behaviour flavour neighbour"); ba.replace(QByteArray("ou"), QByteArray("o")); // ba == "color behavior flavor neighbor" \endcode*/QByteArray &QByteArray::replace(const QByteArray &before, const QByteArray &after){ if (isNull() || before == after) return *this; QByteArray aft = after; if (after.d == d) aft.detach(); QByteArrayMatcher matcher(before); int index = 0; const int bl = before.d->size; const int al = aft.d->size; int len = d->size; char *d = data(); if (bl == al) { if (bl) { while ((index = matcher.indexIn(*this, index)) != -1) { memcpy(d + index, aft.constData(), al); index += bl; } } } else if (al < bl) { uint to = 0; uint movestart = 0; uint num = 0; while ((index = matcher.indexIn(*this, index)) != -1) { if (num) { int msize = index - movestart; if (msize > 0) { memmove(d + to, d + movestart, msize); to += msize; } } else { to = index; } if (al) { memcpy(d + to, aft.constData(), al); to += al; } index += bl; movestart = index; num++; } if (num) { int msize = len - movestart; if (msize > 0) memmove(d + to, d + movestart, msize); resize(len - num*(bl-al)); } } else { // the most complex case. We don't want to lose performance by doing repeated // copies and reallocs of the string. while (index != -1) { uint indices[4096]; uint pos = 0; while(pos < 4095) { index = matcher.indexIn(*this, index); if (index == -1) break; indices[pos++] = index; index += bl; // avoid infinite loop if (!bl) index++; } if (!pos) break; // we have a table of replacement positions, use them for fast replacing int adjust = pos*(al-bl); // index has to be adjusted in case we get back into the loop above. if (index != -1) index += adjust; int newlen = len + adjust; int moveend = len; if (newlen > len) { resize(newlen); len = newlen; } d = this->d->data; while(pos) { pos--; int movestart = indices[pos] + bl; int insertstart = indices[pos] + pos*(al-bl); int moveto = insertstart + al; memmove(d + moveto, d + movestart, (moveend - movestart)); if (aft.size()) memcpy(d + insertstart, aft.constData(), al); moveend = movestart - bl; } } } return *this;}/*! \fn QByteArray &QByteArray::replace(const char *before, const QByteArray &after) \overload Replaces every occurrence of the string \a before with the byte array \a after.*//*! \fn QByteArray &QByteArray::replace(const QByteArray &before, const char *after) \overload Replaces every occurrence of the byte array \a before with the string \a after.*//*! \fn QByteArray &QByteArray::replace(const QString &before, const QByteArray &after) \overload Replaces every occurrence of the string \a before with the byte array \a after. The Unicode data is converted into 8-bit characters using QString::toAscii(). If the QString contains non-ASCII Unicode characters, using this function can lead to loss of information. You can disable this function 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::replace(const QString &before, const char *after) \overload Replaces every occurrence of the string \a before with the string \a after.*//*! \fn QByteArray &QByteArray::replace(const char *before, const char *after) \overload Replaces every occurrence of the string \a before with the string \a after.*//*! \overload Replaces every occurrence of the character \a before with the byte array \a after.*/QByteArray &QByteArray::replace(char before, const QByteArray &after){ char b[2] = { before, '\0' }; QByteArray cb = fromRawData(b, 1); return replace(cb, after);}/*! \fn QByteArray &QByteArray::replace(char before, const QString &after) \overload Replaces every occurrence of the character \a before with the string \a after. The Unicode data is converted into 8-bit characters using QString::toAscii(). If the QString contains non-ASCII Unicode characters, using this function can lead to loss of information. You can disable this function 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::replace(char before, const char *after) \overload Replaces every occurrence of the character \a before with the string \a after.*//*! \overload Replaces every occurrence of the character \a before with the character \a after.*/QByteArray &QByteArray::replace(char before, char after){ if (d->size) { char *i = data(); char *e = i + d->size; for (; i != e; ++i) if (*i == before) * i = after; }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -