From 3d0eaf863edeed9095b35026974a54cc7d418962 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Tue, 20 Feb 2024 15:06:16 +0100 Subject: [PATCH] Add missing QUtf8StringView relational operators Add QU8SV vs QSV, QU8SV vs QChar, and QU8SV vs char16_t relational operators. This allows to get rid of the dummy relational operators in tst_qstringapisymmetry. Task-number: QTBUG-117661 Change-Id: If95d7418efd13c505ed0e3bef748b86ff55e623a Reviewed-by: Thiago Macieira --- src/corelib/text/qstring.h | 17 ++++++++++++ src/corelib/text/qutf8stringview.h | 26 +++++++++++++++++++ src/corelib/text/qutf8stringview.qdoc | 8 ++++++ .../tst_qstringapisymmetry.cpp | 6 ----- 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index 6616bf5fe8..e2a6605861 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -1141,12 +1141,29 @@ ushort QStringView::toUShort(bool *ok, int base) const // QUtf8StringView inline members that require QStringView: // +template +int QBasicUtf8StringView::compare(QChar other, Qt::CaseSensitivity cs) const noexcept +{ + return QtPrivate::compareStrings(*this, QStringView(&other, 1), cs); +} + template int QBasicUtf8StringView::compare(QStringView other, Qt::CaseSensitivity cs) const noexcept { return QtPrivate::compareStrings(*this, other, cs); } +template +[[nodiscard]] bool QBasicUtf8StringView::equal(QChar other) const noexcept +{ + return QtPrivate::equalStrings(*this, QStringView(&other, 1)); +} + +template +[[nodiscard]] bool QBasicUtf8StringView::equal(QStringView other) const noexcept +{ + return QtPrivate::equalStrings(*this, other); +} // // QUtf8StringView inline members that require QString, QL1SV or QBA: diff --git a/src/corelib/text/qutf8stringview.h b/src/corelib/text/qutf8stringview.h index 339c89e43f..cfbfb097c5 100644 --- a/src/corelib/text/qutf8stringview.h +++ b/src/corelib/text/qutf8stringview.h @@ -293,6 +293,8 @@ public: return QtPrivate::compareStrings(*this, other, cs); } + [[nodiscard]] int compare(QChar other, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; [[nodiscard]] int compare(QStringView other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; [[nodiscard]] int compare(QLatin1StringView other, @@ -300,6 +302,8 @@ public: [[nodiscard]] int compare(const QByteArray &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; + [[nodiscard]] bool equal(QChar other) const noexcept; + [[nodiscard]] bool equal(QStringView other) const noexcept; [[nodiscard]] bool equal(QLatin1StringView other) const noexcept; [[nodiscard]] bool equal(const QByteArray &other) const noexcept; @@ -346,6 +350,28 @@ private: } Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, QLatin1StringView) + friend bool + comparesEqual(const QBasicUtf8StringView &lhs, const QStringView &rhs) noexcept + { return lhs.equal(rhs); } + friend Qt::strong_ordering + compareThreeWay(const QBasicUtf8StringView &lhs, const QStringView &rhs) noexcept + { + const int res = lhs.compare(rhs); + return Qt::compareThreeWay(res, 0); + } + Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, QStringView) + + friend bool comparesEqual(const QBasicUtf8StringView &lhs, const QChar &rhs) noexcept + { return lhs.equal(rhs); } + friend Qt::strong_ordering + compareThreeWay(const QBasicUtf8StringView &lhs, const QChar &rhs) noexcept + { + const int res = lhs.compare(rhs); + return Qt::compareThreeWay(res, 0); + } + Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, QChar) + Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, char16_t) + #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) friend bool comparesEqual(const QBasicUtf8StringView &lhs, const QByteArrayView &rhs) noexcept diff --git a/src/corelib/text/qutf8stringview.qdoc b/src/corelib/text/qutf8stringview.qdoc index dbc524bdb5..b433e5b995 100644 --- a/src/corelib/text/qutf8stringview.qdoc +++ b/src/corelib/text/qutf8stringview.qdoc @@ -11,6 +11,14 @@ \ingroup tools \ingroup string-processing + \compares strong + \compareswith strong char16_t QChar {const char16_t *} QString QStringView \ + QLatin1StringView + \endcompareswith + \compareswith strong {const char *} QByteArray QByteArrayView + The contents of byte arrays is interpreted as utf-8. + \endcompareswith + A QUtf8StringView references a contiguous portion of a UTF-8 string it does not own. It acts as an interface type to all kinds of UTF-8 string, without the need to construct a QString or diff --git a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp index d4c89d3c30..a007d47fd9 100644 --- a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -68,10 +68,6 @@ MAKE_ALL(const char*, QChar) MAKE_ALL(QChar, QByteArray) MAKE_ALL(QChar, const char*) -MAKE_ALL(QChar, QUtf8StringView) - -MAKE_ALL(QUtf8StringView, QChar) -MAKE_ALL(QUtf8StringView, char16_t) #undef MAKE_ALL #undef MAKE_RELOP @@ -224,10 +220,8 @@ private Q_SLOTS: void compare_QStringView_QString() { compare_impl(); } void compare_QStringView_QStringView_data() { compare_data(); } void compare_QStringView_QStringView() { compare_impl(); } -#ifdef NOT_YET_IMPLEMENTED void compare_QStringView_QUtf8StringView_data() { compare_data(); } void compare_QStringView_QUtf8StringView() { compare_impl(); } -#endif void compare_QStringView_QLatin1String_data() { compare_data(); } void compare_QStringView_QLatin1String() { compare_impl(); } void compare_QStringView_QByteArray_data() { compare_data(); }