?? qbytearray.cpp
字號:
return *this;}/*! Splits the byte array into subarrays wherever \a sep occurs, and returns the list of those arrays. If \a sep does not match anywhere in the byte array, split() returns a single-element list containing this byte array.*/QList<QByteArray> QByteArray::split(char sep) const{ QList<QByteArray> list; int start = 0; int end; while ((end = indexOf(sep, start)) != -1) { list.append(mid(start, end - start)); start = end + 1; } list.append(mid(start)); return list;}#define REHASH(a) \ if (ol_minus_1 < sizeof(uint) * CHAR_BIT) \ hashHaystack -= (a) << ol_minus_1; \ hashHaystack <<= 1/*! Returns the index position of the first occurrence of the byte array \a ba in this byte array, searching forward from index position \a from. Returns -1 if \a ba could not be found. Example: \code QByteArray x("sticky question"); QByteArray y("sti"); x.indexOf(y); // returns 0 x.indexOf(y, 1); // returns 10 x.indexOf(y, 10); // returns 10 x.indexOf(y, 11); // returns -1 \endcode \sa lastIndexOf(), contains(), count()*/int QByteArray::indexOf(const QByteArray &ba, int from) const{ const int l = d->size; const int ol = ba.d->size; if (from > d->size || ol + from > l) return -1; if (ol == 0) return from; if (ol == 1) return indexOf(*ba.d->data, from); if (l > 500 && ol > 5) return QByteArrayMatcher(ba).indexIn(*this, from); const char *needle = ba.d->data; const char *haystack = d->data + from; const char *end = d->data + (l - ol); const uint ol_minus_1 = ol - 1; uint hashNeedle = 0, hashHaystack = 0; int idx; for (idx = 0; idx < ol; ++idx) { hashNeedle = ((hashNeedle<<1) + needle[idx]); hashHaystack = ((hashHaystack<<1) + haystack[idx]); } hashHaystack -= *(haystack + ol_minus_1); while (haystack <= end) { hashHaystack += *(haystack + ol_minus_1); if (hashHaystack == hashNeedle && *needle == *haystack && strncmp(needle, haystack, ol) == 0) return haystack - d->data; REHASH(*haystack); ++haystack; return -1;}/*! \fn int QByteArray::indexOf(const QString &str, int from) const \overload Returns the index position of the first occurrence of the string \a str in the byte array, searching forward from index position \a from. Returns -1 if \a str could not be found. 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 int QByteArray::indexOf(const char *str, int from) const \overload Returns the index position of the first occurrence of the string \a str in the byte array, searching forward from index position \a from. Returns -1 if \a str could not be found.*//*! \overload Returns the index position of the first occurrence of the character \a ch in the byte array, searching forward from index position \a from. Returns -1 if \a ch could not be found. Example: \code QByteArray ba("ABCBA"); ba.indexOf("B"); // returns 1 ba.indexOf("B", 1); // returns 1 ba.indexOf("B", 2); // returns 3 ba.indexOf("X"); // returns -1 \endcode \sa lastIndexOf(), contains()*/int QByteArray::indexOf(char ch, int from) const{ if (from < 0) from = qMax(from + d->size, 0); if (from < d->size) { const char *n = d->data + from - 1; const char *e = d->data + d->size; while (++n != e) if (*n == ch) return n - d->data; } return -1;}/*! Returns the index position of the last occurrence of the byte array \a ba in this byte array, searching backward from index position \a from. If \a from is -1 (the default), the search starts at the last byte. Returns -1 if \a ba could not be found. Example: \code QByteArray x("crazy azimuths"); QByteArray y("azy"); x.lastIndexOf(y); // returns 6 x.lastIndexOf(y, 6); // returns 6 x.lastIndexOf(y, 5); // returns 2 x.lastIndexOf(y, 1); // returns -1 \endcode \sa indexOf(), contains(), count()*/int QByteArray::lastIndexOf(const QByteArray &ba, int from) const{ const int ol = ba.d->size; const int l = d->size; int delta = l - ol; if (from < 0) from = delta; if (from < 0 || from > l) return -1; if (from > delta) from = delta; if (ol == 1) return lastIndexOf(*ba.d->data, from); const char *needle = ba.d->data; const char *haystack = d->data + from; const char *end = d->data; const uint ol_minus_1 = ol - 1; const char *n = needle + ol_minus_1; const char *h = haystack + ol_minus_1; uint hashNeedle = 0, hashHaystack = 0; int idx; for (idx = 0; idx < ol; ++idx) { hashNeedle = ((hashNeedle<<1) + *(n-idx)); hashHaystack = ((hashHaystack<<1) + *(h-idx)); hashHaystack -= *haystack; while (haystack >= end) { hashHaystack += *haystack; if (hashHaystack == hashNeedle && strncmp(needle, haystack, ol) == 0) return haystack-d->data; --haystack; REHASH(*(haystack + ol)); } return -1;}/*! \fn int QByteArray::lastIndexOf(const QString &str, int from) const \overload Returns the index position of the last occurrence of the string \a str in the byte array, searching backward from index position \a from. If \a from is -1 (the default), the search starts at the last (size() - 1) byte. Returns -1 if \a str could not be found. 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 int QByteArray::lastIndexOf(const char *str, int from) const \overload Returns the index position of the last occurrence of the string \a str in the byte array, searching backward from index position \a from. If \a from is -1 (the default), the search starts at the last (size() - 1) byte. Returns -1 if \a str could not be found.*//*! \overload Returns the index position of the last occurrence of character \a ch in the byte array, searching backward from index position \a from. If \a from is -1 (the default), the search starts at the last (size() - 1) byte. Returns -1 if \a ch could not be found. Example: \code QByteArray ba("ABCBA"); ba.lastIndexOf("B"); // returns 3 ba.lastIndexOf("B", 3); // returns 3 ba.lastIndexOf("B", 2); // returns 1 ba.lastIndexOf("X"); // returns -1 \endcode \sa indexOf(), contains()*/int QByteArray::lastIndexOf(char ch, int from) const{ if (from < 0) from += d->size; else if (from > d->size) from = d->size-1; if (from >= 0) { const char *b = d->data; const char *n = d->data + from + 1; while (n-- != b) if (*n == ch) return n - b; } return -1;}/*! Returns the number of (potentially overlapping) occurrences of byte array \a ba in this byte array. \sa contains(), indexOf()*/int QByteArray::count(const QByteArray &ba) const{ int num = 0; int i = -1; if (d->size > 500 && ba.d->size > 5) { QByteArrayMatcher matcher(ba); while ((i = matcher.indexIn(*this, i + 1)) != -1) ++num; } else { while ((i = indexOf(ba, i + 1)) != -1) ++num; } return num;}/*! \overload Returns the number of (potentially overlapping) occurrences of string \a str in the byte array.*/int QByteArray::count(const char *str) const{ int num = 0; int i = -1; while ((i = indexOf(str, i + 1)) != -1) ++num; return num;}/*! \overload Returns the number of occurrences of character \a ch in the byte array. \sa contains(), indexOf()*/int QByteArray::count(char ch) const{ int num = 0; const char *i = d->data + d->size; const char *b = d->data; while (i != b) if (*--i == ch) ++num; return num;}/*! \fn int QByteArray::count() const \overload Same as size().*//*! Returns true if this byte array starts with byte array \a ba; otherwise returns false. Example: \code QByteArray url("ftp://ftp.trolltech.com/"); if (url.startsWith("ftp:")) ... \endcode \sa endsWith(), left()*/bool QByteArray::startsWith(const QByteArray &ba) const{ if (d == ba.d || ba.d->size == 0) return true; if (d->size < ba.d->size) return false; return memcmp(d->data, ba.d->data, ba.d->size) == 0;}/*! \overload Returns true if this byte array starts with string \a str; otherwise returns false.*/bool QByteArray::startsWith(const char *str) const{ if (!str || !*str) return true; int len = qstrlen(str); if (d->size < len) return false; return qstrncmp(d->data, str, len) == 0;}/*! \overload Returns true if this byte array starts with character \a ch; otherwise returns false.*/bool QByteArray::startsWith(char ch) const{ if (d->size == 0) return false; return d->data[0] == ch;}/*! Returns true if this byte array ends with byte array \a ba; otherwise returns false. Example: \code QByteArray url("http://www.trolltech.com/index.html"); if (url.endsWith(".html")) ... \endcode \sa startsWith(), right()*/bool QByteArray::endsWith(const QByteArray &ba) const{ if (d == ba.d || ba.d->size == 0) return true; if (d->size < ba.d->size) return false; return memcmp(d->data + d->size - ba.d->size, ba.d->data, ba.d->size) == 0;}/*! \overload Returns true if this byte array ends with string \a str; otherwise returns false.*/bool QByteArray::endsWith(const char *str) const{ if (!str || !*str) return true; int len = qstrlen(str); if (d->size < len) return false; return qstrncmp(d->data + d->size - len, str, len) == 0;}/*! \overload Returns true if this byte array ends with character \a ch; otherwise returns false.*/bool QByteArray::endsWith(char ch) const{ if (d->size == 0) return false; return d->data[d->size - 1] == ch;}/*! Returns a byte array that contains the leftmost \a len bytes of this byte array. The entire byte array is returned if \a len is greater than size(). Example: \code QByteArray x("Pineapple"); QByteArray y = x.left(4); // y == "Pine" \endcode \sa right(), mid(), startsWith(), truncate()*/QByteArray QByteArray::left(int len) const{ if (len >= d->size) return *this; if (len < 0) len = 0; return QByteArray(d->data, len);}/*! Returns a byte array that contains the rightmost \a len bytes of this byte array. The entire byte array is returned if \a len is greater than size(). Example: \code QByteArray x("Pineapple"); QByteArray y = x.right(5); // y == "apple" \endcode \sa endsWith(), left(), mid()*/QByteArray QByteArray::right(int len) con
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -