From 05e5b87ba0b0b26ae7d330018c027d22a52d7644 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Mon, 12 Feb 2024 17:01:21 +0100 Subject: [PATCH] Add QLatin1StringView vs QByteArrayView relational operators ... by using the new comparison helper macros. The operators are marked as QT_ASCII_CAST_WARN, like the pre-existing relational operators with QByteArray and const char *. This allows to enable related tests in tst_qstringapisymmetry. Task-number: QTBUG-117661 Task-number: QTBUG-108805 Change-Id: Ic9bcddffc25585edb7375c3e651d49d040a60454 Reviewed-by: Thiago Macieira --- src/corelib/text/qlatin1stringview.h | 15 +++++++++++++++ src/corelib/text/qstring.cpp | 12 ++++++++++++ .../qstringapisymmetry/tst_qstringapisymmetry.cpp | 4 ++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/corelib/text/qlatin1stringview.h b/src/corelib/text/qlatin1stringview.h index 28b7647f1c0..4a1cd9774bf 100644 --- a/src/corelib/text/qlatin1stringview.h +++ b/src/corelib/text/qlatin1stringview.h @@ -8,6 +8,7 @@ #define QLATIN1STRINGVIEW_H #include +#include #include #include #include @@ -299,6 +300,17 @@ public: friend bool operator>=(QLatin1StringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) >= 0; } +private: + friend bool comparesEqual(const QLatin1StringView &lhs, const QByteArrayView &rhs) noexcept + { return equal_helper(lhs, rhs.data(), rhs.size()); } + friend Qt::strong_ordering + compareThreeWay(const QLatin1StringView &lhs, const QByteArrayView &rhs) noexcept + { + const int res = compare_helper(lhs, rhs.data(), rhs.size()); + return Qt::compareThreeWay(res, 0); + } + +public: #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) QT_ASCII_CAST_WARN inline bool operator==(const char *s) const; QT_ASCII_CAST_WARN inline bool operator!=(const char *s) const; @@ -314,6 +326,8 @@ public: QT_ASCII_CAST_WARN inline bool operator<=(const QByteArray &s) const; QT_ASCII_CAST_WARN inline bool operator>=(const QByteArray &s) const; + Q_DECLARE_STRONGLY_ORDERED(QLatin1StringView, QByteArrayView, QT_ASCII_CAST_WARN) + QT_ASCII_CAST_WARN friend bool operator==(const char *s1, QLatin1StringView s2) { return compare_helper(s2, s1) == 0; } QT_ASCII_CAST_WARN friend bool operator!=(const char *s1, QLatin1StringView s2) { return compare_helper(s2, s1) != 0; } QT_ASCII_CAST_WARN friend bool operator< (const char *s1, QLatin1StringView s2) { return compare_helper(s2, s1) > 0; } @@ -333,6 +347,7 @@ private: } static int compare_helper(const QLatin1StringView &s1, const char *s2) noexcept { return compare_helper(s1, s2, qstrlen(s2)); } + Q_CORE_EXPORT static bool equal_helper(QLatin1StringView s1, const char *s2, qsizetype len) noexcept; Q_CORE_EXPORT static int compare_helper(const QLatin1StringView &s1, const char *s2, qsizetype len) noexcept; Q_CORE_EXPORT static int compare_helper(const QChar *data1, qsizetype length1, QLatin1StringView s2, diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index b95c7d22b77..ea74cb9d72f 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -6584,6 +6584,18 @@ int QString::compare_helper(const QChar *data1, qsizetype length1, const char *d \overload compare() */ +/*! + \internal + \since 6.8 +*/ +bool QLatin1StringView::equal_helper(QLatin1StringView s1, const char *s2, qsizetype len) noexcept +{ + // because qlatin1stringview.h can't include qutf8stringview.h + Q_ASSERT(len >= 0); + Q_ASSERT(s2 || len == 0); + return QtPrivate::equalStrings(s1, QUtf8StringView(s2, len)); +} + /*! \internal \since 6.6 diff --git a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp index ef862c9e6f4..0b4c2f87fa0 100644 --- a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -280,10 +280,8 @@ private Q_SLOTS: void compare_QLatin1String_QLatin1String() { compare_impl(); } void compare_QLatin1String_QByteArray_data() { compare_data(); } void compare_QLatin1String_QByteArray() { compare_impl(); } -#ifdef AMBIGUOUS_CALL void compare_QLatin1String_QByteArrayView_data() { compare_data(); } void compare_QLatin1String_QByteArrayView() { compare_impl(); } -#endif void compare_QLatin1String_const_char_star_data() { compare_data(); } void compare_QLatin1String_const_char_star() { compare_impl(); } @@ -323,8 +321,10 @@ private Q_SLOTS: #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 void compare_QByteArrayView_QByteArray_data() { compare_data(); } void compare_QByteArrayView_QByteArray() { compare_impl(); } #endif