diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index 158cfcc357..84f95d28a7 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -1125,7 +1125,7 @@ int QBasicUtf8StringView::compare(QStringView other, Qt::CaseSensitiv // -// QUtf8StringView inline members that require QString: +// QUtf8StringView inline members that require QString, QL1SV or QBA: // template @@ -1141,12 +1141,29 @@ template return QtPrivate::compareStrings(*this, other, cs); } +template +[[nodiscard]] int QBasicUtf8StringView::compare(const QByteArray &other, + Qt::CaseSensitivity cs) const noexcept +{ + return QtPrivate::compareStrings(*this, + QBasicUtf8StringView(other.data(), other.size()), + cs); +} + template [[nodiscard]] bool QBasicUtf8StringView::equal(QLatin1StringView other) const noexcept { return QtPrivate::equalStrings(*this, other); } +template +[[nodiscard]] bool QBasicUtf8StringView::equal(const QByteArray &other) const noexcept +{ + return size() == other.size() + && QtPrivate::equalStrings(*this, QBasicUtf8StringView(other.data(), + other.size())); +} + // // QAnyStringView inline members that require QString: // diff --git a/src/corelib/text/qutf8stringview.h b/src/corelib/text/qutf8stringview.h index 377fee8469..339c89e43f 100644 --- a/src/corelib/text/qutf8stringview.h +++ b/src/corelib/text/qutf8stringview.h @@ -297,8 +297,11 @@ public: Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; [[nodiscard]] int compare(QLatin1StringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; + [[nodiscard]] int compare(const QByteArray &other, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; [[nodiscard]] bool equal(QLatin1StringView other) const noexcept; + [[nodiscard]] bool equal(const QByteArray &other) const noexcept; private: [[nodiscard]] static inline int compare(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept @@ -343,6 +346,44 @@ private: } Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, QLatin1StringView) +#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) + friend bool + comparesEqual(const QBasicUtf8StringView &lhs, const QByteArrayView &rhs) noexcept + { + return lhs.size() == rhs.size() + && QtPrivate::equalStrings(QBasicUtf8StringView(lhs.data(), lhs.size()), + QBasicUtf8StringView(rhs.data(), rhs.size())); + } + friend Qt::strong_ordering + compareThreeWay(const QBasicUtf8StringView &lhs, const QByteArrayView &rhs) noexcept + { + const int res = QtPrivate::compareStrings(QBasicUtf8StringView(lhs.data(), lhs.size()), + QBasicUtf8StringView(rhs.data(), rhs.size())); + return Qt::compareThreeWay(res, 0); + } + Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, QByteArrayView, QT_ASCII_CAST_WARN) + + friend bool + comparesEqual(const QBasicUtf8StringView &lhs, const QByteArray &rhs) noexcept + { + return lhs.equal(rhs); + } + friend Qt::strong_ordering + compareThreeWay(const QBasicUtf8StringView &lhs, const QByteArray &rhs) noexcept + { + const int res = lhs.compare(rhs); + return Qt::compareThreeWay(res, 0); + } + Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, QByteArray, QT_ASCII_CAST_WARN) + + friend bool comparesEqual(const QBasicUtf8StringView &lhs, const char *rhs) noexcept + { return comparesEqual(lhs, QByteArrayView(rhs)); } + friend Qt::strong_ordering + compareThreeWay(const QBasicUtf8StringView &lhs, const char *rhs) noexcept + { return compareThreeWay(lhs, QByteArrayView(rhs)); } + Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, const char *, QT_ASCII_CAST_WARN) +#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) + Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0, [[maybe_unused]] qsizetype n = 1) const { diff --git a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp index 0b4c2f87fa..8cf04c40ea 100644 --- a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -72,8 +72,6 @@ MAKE_ALL(QChar, const char*) MAKE_ALL(QChar, QUtf8StringView) MAKE_ALL(QString, QUtf8StringView) -MAKE_ALL(QByteArray, QUtf8StringView) -MAKE_ALL(const char*, QUtf8StringView) MAKE_ALL(QUtf8StringView, QChar) MAKE_ALL(QUtf8StringView, char16_t) @@ -257,14 +255,12 @@ private Q_SLOTS: void compare_QUtf8StringView_QUtf8StringView() { compare_impl(); } void compare_QUtf8StringView_QLatin1String_data() { compare_data(); } void compare_QUtf8StringView_QLatin1String() { compare_impl(); } -#ifdef NOT_YET_IMPLMENTED void compare_QUtf8StringView_QByteArray_data() { compare_data(); } void compare_QUtf8StringView_QByteArray() { compare_impl(); } void compare_QUtf8StringView_QByteArrayView_data() { compare_data(); } void compare_QUtf8StringView_QByteArrayView() { compare_impl(); } void compare_QUtf8StringView_const_char_star_data() { compare_data(); } void compare_QUtf8StringView_const_char_star() { compare_impl(); } -#endif void compare_QLatin1String_QChar_data() { compare_data(false); } void compare_QLatin1String_QChar() { compare_impl(); } @@ -318,10 +314,8 @@ private Q_SLOTS: void compare_QByteArrayView_QStringView_data() { compare_data(); } void compare_QByteArrayView_QStringView() { compare_impl(); } #endif -#ifdef AMBIGUOUS_CALL void compare_QByteArrayView_QUtf8StringView_data() { compare_data(); } void compare_QByteArrayView_QUtf8StringView() { compare_impl(); } -#endif void compare_QByteArrayView_QLatin1String_data() { compare_data(); } void compare_QByteArrayView_QLatin1String() { compare_impl(); } #ifdef AMBIGUOUS_CALL