diff options
-rw-r--r-- | src/corelib/text/qlatin1stringview.h | 19 | ||||
-rw-r--r-- | tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp | 5 |
2 files changed, 24 insertions, 0 deletions
diff --git a/src/corelib/text/qlatin1stringview.h b/src/corelib/text/qlatin1stringview.h index fd4f046971..91392d9540 100644 --- a/src/corelib/text/qlatin1stringview.h +++ b/src/corelib/text/qlatin1stringview.h @@ -283,6 +283,16 @@ public: } Q_DECLARE_STRONGLY_ORDERED(QLatin1StringView, QStringView) + // Reversed helper methods for QStringView <> QLatin1StringView comparison. + // If we do not provide them explicitly, QStringView <> QByteArrayView + // overloads will be selected, which will provide wrong results, because + // they will convert from utf-8 + friend bool comparesEqual(const QStringView &lhs, const QLatin1StringView &rhs) noexcept + { return comparesEqual(rhs, lhs); } + friend Qt::strong_ordering + compareThreeWay(const QStringView &lhs, const QLatin1StringView &rhs) noexcept + { return QtOrderingPrivate::reversed(compareThreeWay(rhs, lhs)); } + private: friend bool comparesEqual(const QLatin1StringView &lhs, const QByteArrayView &rhs) noexcept { return equal_helper(lhs, rhs.data(), rhs.size()); } @@ -293,6 +303,15 @@ private: return Qt::compareThreeWay(res, 0); } + // Reversed helper methods for QByteArrayView <> QLatin1StringView comparison. + // If we do not provide them explicitly, QByteArrayView <> QByteArrayView + // overloads will be selected, which will provide wrong results + friend bool comparesEqual(const QByteArrayView &lhs, const QLatin1StringView &rhs) noexcept + { return comparesEqual(rhs, lhs); } + friend Qt::strong_ordering + compareThreeWay(const QByteArrayView &lhs, const QLatin1StringView &rhs) noexcept + { return QtOrderingPrivate::reversed(compareThreeWay(rhs, lhs)); } + public: #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) Q_DECLARE_STRONGLY_ORDERED(QLatin1StringView, QByteArrayView, QT_ASCII_CAST_WARN) diff --git a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp index e1a262b61a..35a734cf02 100644 --- a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -1360,6 +1360,11 @@ void tst_QStringApiSymmetry::compare_impl() const CHECK(<=); CHECK(>=); #undef CHECK + // Test that all string-like types implemente compareThreeWay() as a friend + // function. + const Qt::strong_ordering expectedOrdering = + Qt::compareThreeWay(caseSensitiveCompareResult, 0); + QCOMPARE_EQ(qCompareThreeWay(lhs, rhs), expectedOrdering); } template <typename LHS, typename RHS> |