diff options
Diffstat (limited to 'src/corelib/text/qbytearray.cpp')
-rw-r--r-- | src/corelib/text/qbytearray.cpp | 116 |
1 files changed, 32 insertions, 84 deletions
diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index e6387e4bed..d9f0ee405a 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -56,15 +56,35 @@ static constexpr inline uchar asciiLower(uchar c) return c >= 'A' && c <= 'Z' ? c | 0x20 : c; } -qsizetype qFindByteArray( - const char *haystack0, qsizetype haystackLen, qsizetype from, - const char *needle0, qsizetype needleLen); - /***************************************************************************** Safe and portable C string functions; extensions to standard string.h *****************************************************************************/ /*! \relates QByteArray + \internal + + Wrapper around memrchr() for systems that don't have it. It's provided in + every system because, as a GNU extension, memrchr() may not be declared in + string.h depending on how strict the compiler was asked to be. + + Used in QByteArrayView::lastIndexOf() overload for a single char. +*/ +const void *qmemrchr(const void *s, int needle, size_t size) noexcept +{ +#if QT_CONFIG(memrchr) + return memrchr(s, needle, size); +#endif + auto b = static_cast<const char *>(s); + const char *n = b + size; + while (n-- != b) { + if (*n == needle) + return n; + } + return nullptr; +} + + +/*! \relates QByteArray Returns a duplicate string. @@ -2680,31 +2700,6 @@ QByteArray QByteArray::repeated(qsizetype times) const return result; } -#define REHASH(a) \ - if (ol_minus_1 < sizeof(std::size_t) * CHAR_BIT) \ - hashHaystack -= std::size_t(a) << ol_minus_1; \ - hashHaystack <<= 1 - -qsizetype QtPrivate::findByteArray(QByteArrayView haystack, qsizetype from, QByteArrayView needle) noexcept -{ - const auto ol = needle.size(); - const auto l = haystack.size(); - if (ol == 0) { - if (from < 0) - return qMax(from + l, 0); - else - return from > l ? -1 : from; - } - - if (ol == 1) - return findByteArray(haystack, from, needle.front()); - - if (from > l || ol + from > l) - return -1; - - return qFindByteArray(haystack.data(), haystack.size(), from, needle.data(), ol); -} - /*! \fn qsizetype QByteArray::indexOf(QByteArrayView bv, qsizetype from) const \since 6.0 @@ -2745,10 +2740,10 @@ static qsizetype lastIndexOfHelper(const char *haystack, qsizetype l, const char const char *end = haystack; haystack += from; - const auto ol_minus_1 = std::size_t(ol - 1); + const qregisteruint ol_minus_1 = ol - 1; const char *n = needle + ol_minus_1; const char *h = haystack + ol_minus_1; - std::size_t hashNeedle = 0, hashHaystack = 0; + qregisteruint hashNeedle = 0, hashHaystack = 0; qsizetype idx; for (idx = 0; idx < ol; ++idx) { hashNeedle = ((hashNeedle<<1) + *(n-idx)); @@ -2760,34 +2755,11 @@ static qsizetype lastIndexOfHelper(const char *haystack, qsizetype l, const char if (hashHaystack == hashNeedle && memcmp(needle, haystack, ol) == 0) return haystack - end; --haystack; - REHASH(*(haystack + ol)); + if (ol_minus_1 < sizeof(ol_minus_1) * CHAR_BIT) + hashHaystack -= qregisteruint(*(haystack + ol)) << ol_minus_1; + hashHaystack <<= 1; } return -1; - -} - -static inline qsizetype lastIndexOfCharHelper(QByteArrayView haystack, qsizetype from, char needle) noexcept -{ - if (haystack.size() == 0) - return -1; - if (from < 0) - from += haystack.size(); - else if (from > haystack.size()) - from = haystack.size() - 1; - if (from >= 0) { - const char *b = haystack.data(); - const char *n = b + from + 1; - while (n-- != b) { - if (*n == needle) - return n - b; - } - } - return -1; -} - -qsizetype QtPrivate::lastIndexOf(QByteArrayView haystack, qsizetype from, char needle) noexcept -{ - return lastIndexOfCharHelper(haystack, from, needle); } qsizetype QtPrivate::lastIndexOf(QByteArrayView haystack, qsizetype from, QByteArrayView needle) noexcept @@ -2799,7 +2771,7 @@ qsizetype QtPrivate::lastIndexOf(QByteArrayView haystack, qsizetype from, QByteA } const auto ol = needle.size(); if (ol == 1) - return lastIndexOfCharHelper(haystack, from, needle.front()); + return QtPrivate::lastIndexOf(haystack, from, needle.front()); return lastIndexOfHelper(haystack.data(), haystack.size(), needle.data(), ol, from); } @@ -4218,24 +4190,6 @@ QByteArray QByteArray::toBase64(Base64Options options) const \sa toUShort() */ -static char *qulltoa2(char *p, qulonglong n, int base) -{ -#if defined(QT_CHECK_RANGE) - if (base < 2 || base > 36) { - qWarning("QByteArray::setNum: Invalid base %d", base); - base = 10; - } -#endif - constexpr char b = 'a' - 10; - do { - const int c = n % base; - n /= base; - *--p = c + (c < 10 ? '0' : b); - } while (n); - - return p; -} - /*! \overload @@ -4255,9 +4209,7 @@ QByteArray &QByteArray::setNum(qlonglong n, int base) p = qulltoa2(buff + buffsize, qulonglong(n), base); } - clear(); - append(p, buffsize - (p - buff)); - return *this; + return assign(QByteArrayView{p, buff + buffsize}); } /*! @@ -4272,9 +4224,7 @@ QByteArray &QByteArray::setNum(qulonglong n, int base) char buff[buffsize]; char *p = qulltoa2(buff + buffsize, n, base); - clear(); - append(p, buffsize - (p - buff)); - return *this; + return assign(QByteArrayView{p, buff + buffsize}); } /*! @@ -5206,5 +5156,3 @@ size_t qHash(const QByteArray::FromBase64Result &key, size_t seed) noexcept */ QT_END_NAMESPACE - -#undef REHASH |