diff options
Diffstat (limited to 'src/corelib/text/qstringview.h')
-rw-r--r-- | src/corelib/text/qstringview.h | 266 |
1 files changed, 141 insertions, 125 deletions
diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index e34418e956..ab97d834d3 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -1,65 +1,18 @@ -/**************************************************************************** -** -** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com> -** Copyright (C) 2019 Mail.ru Group. -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com> +// Copyright (C) 2019 Mail.ru Group. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #ifndef QSTRINGVIEW_H #define QSTRINGVIEW_H -/* - This macro enables three "levels" of QStringView support: - - 1. offer QStringView, overload some functions taking QString with - QStringView - - 2. Obsolete: QStringRef and its overloads have been removed. - - 3. like 2, but replace functions taking QString, too. -*/ -#ifndef QT_STRINGVIEW_LEVEL -# define QT_STRINGVIEW_LEVEL 1 -#endif - #include <QtCore/qchar.h> +#include <QtCore/qcompare.h> #include <QtCore/qbytearray.h> #include <QtCore/qstringliteral.h> #include <QtCore/qstringalgorithms.h> #include <string> +#include <string_view> +#include <QtCore/q20type_traits.h> #if defined(Q_OS_DARWIN) || defined(Q_QDOC) Q_FORWARD_DECLARE_CF_TYPE(CFString); @@ -72,6 +25,9 @@ class QString; class QStringView; class QRegularExpression; class QRegularExpressionMatch; +#ifdef Q_QDOC +class QUtf8StringView; +#endif namespace QtPrivate { template <typename Char> @@ -83,7 +39,7 @@ struct IsCompatibleCharTypeHelper (std::is_same<Char, wchar_t>::value && sizeof(wchar_t) == sizeof(QChar))> {}; template <typename Char> struct IsCompatibleCharType - : IsCompatibleCharTypeHelper<typename std::remove_cv<typename std::remove_reference<Char>::type>::type> {}; + : IsCompatibleCharTypeHelper<q20::remove_cvref_t<Char>> {}; template <typename Pointer> struct IsCompatiblePointerHelper : std::false_type {}; @@ -92,7 +48,7 @@ struct IsCompatiblePointerHelper<Char*> : IsCompatibleCharType<Char> {}; template <typename Pointer> struct IsCompatiblePointer - : IsCompatiblePointerHelper<typename std::remove_cv<typename std::remove_reference<Pointer>::type>::type> {}; + : IsCompatiblePointerHelper<q20::remove_cvref_t<Pointer>> {}; template <typename T, typename Enable = void> struct IsContainerCompatibleWithQStringView : std::false_type {}; @@ -149,15 +105,10 @@ private: using if_compatible_container = typename std::enable_if<QtPrivate::IsContainerCompatibleWithQStringView<T>::value, bool>::type; template <typename Char> - static qsizetype lengthHelperPointer(const Char *str) noexcept + static constexpr qsizetype lengthHelperPointer(const Char *str) noexcept { -#if defined(__cpp_lib_is_constant_evaluated) - if (std::is_constant_evaluated()) - return std::char_traits<Char>::length(str); -#elif defined(Q_CC_GNU) && !defined(Q_CC_CLANG) && !defined(Q_CC_INTEL) - if (__builtin_constant_p(*str)) + if (q20::is_constant_evaluated()) return std::char_traits<Char>::length(str); -#endif return QtPrivate::qustrlen(reinterpret_cast<const char16_t *>(str)); } static qsizetype lengthHelperPointer(const QChar *str) noexcept @@ -165,20 +116,6 @@ private: return QtPrivate::qustrlen(reinterpret_cast<const char16_t *>(str)); } - template <typename Container> - static constexpr qsizetype lengthHelperContainer(const Container &c) noexcept - { - return qsizetype(std::size(c)); - } - - template <typename Char, size_t N> - static constexpr qsizetype lengthHelperContainer(const Char (&str)[N]) noexcept - { - const auto it = std::char_traits<Char>::find(str, N, Char(0)); - const auto end = it ? it : std::end(str); - return qsizetype(std::distance(str, end)); - } - template <typename Char> static const storage_type *castHelper(const Char *str) noexcept { return reinterpret_cast<const storage_type*>(str); } @@ -186,21 +123,26 @@ private: { return str; } public: - constexpr QStringView() noexcept - : m_size(0), m_data(nullptr) {} + constexpr QStringView() noexcept {} constexpr QStringView(std::nullptr_t) noexcept : QStringView() {} template <typename Char, if_compatible_char<Char> = true> constexpr QStringView(const Char *str, qsizetype len) +#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED) + : m_data(castHelper(str)), + m_size((Q_ASSERT(len >= 0), Q_ASSERT(str || !len), len)) +#else : m_size((Q_ASSERT(len >= 0), Q_ASSERT(str || !len), len)), - m_data(castHelper(str)) {} + m_data(castHelper(str)) +#endif + {} template <typename Char, if_compatible_char<Char> = true> constexpr QStringView(const Char *f, const Char *l) : QStringView(f, l - f) {} -#ifdef Q_CLANG_QDOC +#ifdef Q_QDOC template <typename Char, size_t N> constexpr QStringView(const Char (&array)[N]) noexcept; @@ -213,7 +155,7 @@ public: : QStringView(str, str ? lengthHelperPointer(str) : 0) {} #endif -#ifdef Q_CLANG_QDOC +#ifdef Q_QDOC QStringView(const QString &str) noexcept; #else template <typename String, if_compatible_qstring_like<String> = true> @@ -222,8 +164,8 @@ public: #endif template <typename Container, if_compatible_container<Container> = true> - constexpr QStringView(const Container &c) noexcept - : QStringView(std::data(c), lengthHelperContainer(c)) {} + constexpr Q_ALWAYS_INLINE QStringView(const Container &c) noexcept + : QStringView(std::data(c), QtPrivate::lengthHelperContainer(c)) {} template <typename Char, size_t Size, if_compatible_char<Char> = true> [[nodiscard]] constexpr static QStringView fromArray(const Char (&string)[Size]) noexcept @@ -242,7 +184,7 @@ public: [[nodiscard]] constexpr const storage_type *utf16() const noexcept { return m_data; } [[nodiscard]] constexpr QChar operator[](qsizetype n) const - { return Q_ASSERT(n >= 0), Q_ASSERT(n < size()), QChar(m_data[n]); } + { verify(n, 1); return QChar(m_data[n]); } // // QString API @@ -278,20 +220,20 @@ public: } [[nodiscard]] constexpr QStringView first(qsizetype n) const noexcept - { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QStringView(m_data, n); } + { verify(0, n); return sliced(0, n); } [[nodiscard]] constexpr QStringView last(qsizetype n) const noexcept - { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return QStringView(m_data + size() - n, n); } + { verify(0, n); return sliced(size() - n, n); } [[nodiscard]] constexpr QStringView sliced(qsizetype pos) const noexcept - { Q_ASSERT(pos >= 0); Q_ASSERT(pos <= size()); return QStringView(m_data + pos, size() - pos); } + { verify(pos, 0); return QStringView(m_data + pos, size() - pos); } [[nodiscard]] constexpr QStringView sliced(qsizetype pos, qsizetype n) const noexcept - { Q_ASSERT(pos >= 0); Q_ASSERT(n >= 0); Q_ASSERT(size_t(pos) + size_t(n) <= size_t(size())); return QStringView(m_data + pos, n); } + { verify(pos, n); return QStringView(m_data + pos, n); } [[nodiscard]] constexpr QStringView chopped(qsizetype n) const noexcept - { return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QStringView(m_data, m_size - n); } + { verify(0, n); return sliced(0, m_size - n); } constexpr void truncate(qsizetype n) noexcept - { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size = n; } + { verify(0, n); ; m_size = n; } constexpr void chop(qsizetype n) noexcept - { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size -= n; } + { verify(0, n); m_size -= n; } [[nodiscard]] QStringView trimmed() const noexcept { return QtPrivate::trimmed(*this); } @@ -303,15 +245,18 @@ public: [[nodiscard]] int compare(QStringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::compareStrings(*this, other, cs); } - [[nodiscard]] inline int compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; + [[nodiscard]] inline int compare(QLatin1StringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; + [[nodiscard]] inline int compare(QUtf8StringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; [[nodiscard]] constexpr int compare(QChar c) const noexcept { return size() >= 1 ? compare_single_char_helper(*utf16() - c.unicode()) : -1; } [[nodiscard]] int compare(QChar c, Qt::CaseSensitivity cs) const noexcept { return QtPrivate::compareStrings(*this, QStringView(&c, 1), cs); } + [[nodiscard]] inline int localeAwareCompare(QStringView other) const; + [[nodiscard]] bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::startsWith(*this, s, cs); } - [[nodiscard]] inline bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; + [[nodiscard]] inline bool startsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; [[nodiscard]] bool startsWith(QChar c) const noexcept { return !empty() && front() == c; } [[nodiscard]] bool startsWith(QChar c, Qt::CaseSensitivity cs) const noexcept @@ -319,41 +264,57 @@ public: [[nodiscard]] bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::endsWith(*this, s, cs); } - [[nodiscard]] inline bool endsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; + [[nodiscard]] inline bool endsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; [[nodiscard]] bool endsWith(QChar c) const noexcept { return !empty() && back() == c; } [[nodiscard]] bool endsWith(QChar c, Qt::CaseSensitivity cs) const noexcept { return QtPrivate::endsWith(*this, QStringView(&c, 1), cs); } [[nodiscard]] qsizetype indexOf(QChar c, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return QtPrivate::findString(*this, from, QStringView(&c, 1), cs); } + { return QtPrivate::findString(*this, from, c.unicode(), cs); } [[nodiscard]] qsizetype indexOf(QStringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::findString(*this, from, s, cs); } - [[nodiscard]] inline qsizetype indexOf(QLatin1String s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; + [[nodiscard]] inline qsizetype indexOf(QLatin1StringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; [[nodiscard]] bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return indexOf(QStringView(&c, 1), 0, cs) != qsizetype(-1); } [[nodiscard]] bool contains(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return indexOf(s, 0, cs) != qsizetype(-1); } - [[nodiscard]] inline bool contains(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; + [[nodiscard]] inline bool contains(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; [[nodiscard]] qsizetype count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::count(*this, c, cs); } [[nodiscard]] qsizetype count(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::count(*this, s, cs); } - - [[nodiscard]] qsizetype lastIndexOf(QChar c, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept - { return QtPrivate::lastIndexOf(*this, from, QStringView(&c, 1), cs); } - [[nodiscard]] qsizetype lastIndexOf(QStringView s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + [[nodiscard]] inline qsizetype count(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + + [[nodiscard]] qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return lastIndexOf(c, -1, cs); } + [[nodiscard]] qsizetype lastIndexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return QtPrivate::lastIndexOf(*this, from, c.unicode(), cs); } + [[nodiscard]] qsizetype lastIndexOf(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return lastIndexOf(s, size(), cs); } + [[nodiscard]] qsizetype lastIndexOf(QStringView s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::lastIndexOf(*this, from, s, cs); } - [[nodiscard]] inline qsizetype lastIndexOf(QLatin1String s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; + [[nodiscard]] inline qsizetype lastIndexOf(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; + [[nodiscard]] inline qsizetype lastIndexOf(QLatin1StringView s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; #if QT_CONFIG(regularexpression) [[nodiscard]] qsizetype indexOf(const QRegularExpression &re, qsizetype from = 0, QRegularExpressionMatch *rmatch = nullptr) const { return QtPrivate::indexOf(*this, re, from, rmatch); } - [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, qsizetype from = -1, QRegularExpressionMatch *rmatch = nullptr) const +#ifdef Q_QDOC + [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const; +#else + // prevent an ambiguity when called like this: lastIndexOf(re, 0) + template <typename T = QRegularExpressionMatch, std::enable_if_t<std::is_same_v<T, QRegularExpressionMatch>, bool> = false> + [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, T *rmatch = nullptr) const + { + return QtPrivate::lastIndexOf(*this, re, size(), rmatch); + } +#endif + [[nodiscard]] qsizetype lastIndexOf(const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch = nullptr) const { return QtPrivate::lastIndexOf(*this, re, from, rmatch); } @@ -372,6 +333,11 @@ public: [[nodiscard]] bool isValidUtf16() const noexcept { return QtPrivate::isValidUtf16(*this); } + [[nodiscard]] bool isUpper() const noexcept + { return QtPrivate::isUpper(*this); } + [[nodiscard]] bool isLower() const noexcept + { return QtPrivate::isLower(*this); } + [[nodiscard]] inline short toShort(bool *ok = nullptr, int base = 10) const; [[nodiscard]] inline ushort toUShort(bool *ok = nullptr, int base = 10) const; [[nodiscard]] inline int toInt(bool *ok = nullptr, int base = 10) const; @@ -401,27 +367,22 @@ public: #endif // QStringView <> QStringView - friend bool operator==(QStringView lhs, QStringView rhs) noexcept { return lhs.size() == rhs.size() && QtPrivate::equalStrings(lhs, rhs); } - friend bool operator!=(QStringView lhs, QStringView rhs) noexcept { return !(lhs == rhs); } - friend bool operator< (QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) < 0; } - friend bool operator<=(QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) <= 0; } - friend bool operator> (QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) > 0; } - friend bool operator>=(QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) >= 0; } + friend bool comparesEqual(const QStringView &lhs, const QStringView &rhs) noexcept + { return lhs.size() == rhs.size() && QtPrivate::equalStrings(lhs, rhs); } + friend Qt::strong_ordering + compareThreeWay(const QStringView &lhs, const QStringView &rhs) noexcept + { + const int res = QtPrivate::compareStrings(lhs, rhs); + return Qt::compareThreeWay(res, 0); + } + Q_DECLARE_STRONGLY_ORDERED(QStringView) // QStringView <> QChar - friend bool operator==(QStringView lhs, QChar rhs) noexcept { return lhs == QStringView(&rhs, 1); } - friend bool operator!=(QStringView lhs, QChar rhs) noexcept { return lhs != QStringView(&rhs, 1); } - friend bool operator< (QStringView lhs, QChar rhs) noexcept { return lhs < QStringView(&rhs, 1); } - friend bool operator<=(QStringView lhs, QChar rhs) noexcept { return lhs <= QStringView(&rhs, 1); } - friend bool operator> (QStringView lhs, QChar rhs) noexcept { return lhs > QStringView(&rhs, 1); } - friend bool operator>=(QStringView lhs, QChar rhs) noexcept { return lhs >= QStringView(&rhs, 1); } - - friend bool operator==(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) == rhs; } - friend bool operator!=(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) != rhs; } - friend bool operator< (QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) < rhs; } - friend bool operator<=(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) <= rhs; } - friend bool operator> (QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) > rhs; } - friend bool operator>=(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) >= rhs; } + friend bool comparesEqual(const QStringView &lhs, QChar rhs) noexcept + { return lhs.size() == 1 && lhs[0] == rhs; } + friend Qt::strong_ordering compareThreeWay(const QStringView &lhs, QChar rhs) noexcept + { return compareThreeWay(lhs, QStringView(&rhs, 1)); } + Q_DECLARE_STRONGLY_ORDERED(QStringView, QChar) // // STL compatibility API: @@ -439,6 +400,9 @@ public: [[nodiscard]] constexpr QChar front() const { return Q_ASSERT(!empty()), QChar(m_data[0]); } [[nodiscard]] constexpr QChar back() const { return Q_ASSERT(!empty()), QChar(m_data[m_size - 1]); } + [[nodiscard]] Q_IMPLICIT operator std::u16string_view() const noexcept + { return std::u16string_view(m_data, size_t(m_size)); } + // // Qt compatibility API: // @@ -451,11 +415,42 @@ public: [[nodiscard]] constexpr QChar first() const { return front(); } [[nodiscard]] constexpr QChar last() const { return back(); } private: - qsizetype m_size; - const storage_type *m_data; +#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED) + const storage_type *m_data = nullptr; + qsizetype m_size = 0; +#else + qsizetype m_size = 0; + const storage_type *m_data = nullptr; +#endif + + Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0, + [[maybe_unused]] qsizetype n = 1) const + { + Q_ASSERT(pos >= 0); + Q_ASSERT(pos <= size()); + Q_ASSERT(n >= 0); + Q_ASSERT(n <= size() - pos); + } constexpr int compare_single_char_helper(int diff) const noexcept { return diff ? diff : size() > 1 ? 1 : 0; } + + Q_CORE_EXPORT static bool equal_helper(QStringView sv, const char *data, qsizetype len); + Q_CORE_EXPORT static int compare_helper(QStringView sv, const char *data, qsizetype len); + +#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) + friend bool comparesEqual(const QStringView &lhs, const QByteArrayView &rhs) noexcept + { return equal_helper(lhs, rhs.data(), rhs.size()); } + friend Qt::strong_ordering + compareThreeWay(const QStringView &lhs, const QByteArrayView &rhs) noexcept + { + const int res = compare_helper(lhs, rhs.data(), rhs.size()); + return Qt::compareThreeWay(res, 0); + } + Q_DECLARE_STRONGLY_ORDERED(QStringView, QByteArrayView, QT_ASCII_CAST_WARN) + Q_DECLARE_STRONGLY_ORDERED(QStringView, QByteArray, QT_ASCII_CAST_WARN) + Q_DECLARE_STRONGLY_ORDERED(QStringView, const char *, QT_ASCII_CAST_WARN) +#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) }; Q_DECLARE_TYPEINFO(QStringView, Q_PRIMITIVE_TYPE); @@ -463,7 +458,7 @@ template <typename QStringLike, typename std::enable_if< std::is_same<QStringLike, QString>::value, bool>::type = true> inline QStringView qToStringViewIgnoringNull(const QStringLike &s) noexcept -{ return QStringView(s.data(), s.size()); } +{ return QStringView(s.begin(), s.size()); } // QChar inline functions: @@ -481,6 +476,27 @@ inline QStringView qToStringViewIgnoringNull(const QStringLike &s) noexcept R{{char16_t(c), u'\0'}} ; } +qsizetype QtPrivate::findString(QStringView str, qsizetype from, QChar ch, Qt::CaseSensitivity cs) noexcept +{ + if (from < -str.size()) // from < 0 && abs(from) > str.size(), avoiding overflow + return -1; + if (from < 0) + from = qMax(from + str.size(), qsizetype(0)); + if (from < str.size()) { + const char16_t *s = str.utf16(); + char16_t c = ch.unicode(); + const char16_t *n = s + from; + const char16_t *e = s + str.size(); + if (cs == Qt::CaseSensitive) + n = qustrchr(QStringView(n, e), c); + else + n = qustrcasechr(QStringView(n, e), c); + if (n != e) + return n - s; + } + return -1; +} + QT_END_NAMESPACE #endif /* QSTRINGVIEW_H */ |