diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index ea74cb9d72f..d3bb1d92d12 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -6584,6 +6584,28 @@ int QString::compare_helper(const QChar *data1, qsizetype length1, const char *d \overload compare() */ +/*! + \internal + \since 6.8 +*/ +bool QStringView::equal_helper(QStringView sv, const char *data, qsizetype len) +{ + Q_ASSERT(len >= 0); + Q_ASSERT(data || len == 0); + return QtPrivate::equalStrings(sv, QUtf8StringView(data, len)); +} + +/*! + \internal + \since 6.8 +*/ +int QStringView::compare_helper(QStringView sv, const char *data, qsizetype len) +{ + Q_ASSERT(len >= 0); + Q_ASSERT(data || len == 0); + return QtPrivate::compareStrings(sv, QUtf8StringView(data, len)); +} + /*! \internal \since 6.8 diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index 57ae1fe8692..2e099a9ba52 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -5,6 +5,7 @@ #define QSTRINGVIEW_H #include +#include #include #include #include @@ -431,6 +432,23 @@ private: 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); diff --git a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp index e1a2232b7b6..7d742534075 100644 --- a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -234,14 +234,12 @@ private Q_SLOTS: #endif void compare_QStringView_QLatin1String_data() { compare_data(); } void compare_QStringView_QLatin1String() { compare_impl(); } -#ifdef NOT_YET_IMPLMENTED void compare_QStringView_QByteArray_data() { compare_data(); } void compare_QStringView_QByteArray() { compare_impl(); } void compare_QStringView_QByteArrayView_data() { compare_data(); } void compare_QStringView_QByteArrayView() { compare_impl(); } void compare_QStringView_const_char_star_data() { compare_data(); } void compare_QStringView_const_char_star() { compare_impl(); } -#endif void compare_QUtf8StringView_QChar_data() { compare_data(false); } void compare_QUtf8StringView_QChar() { compare_impl(); } @@ -287,10 +285,8 @@ private Q_SLOTS: void compare_QByteArray_char16_t() { compare_impl(); } void compare_QByteArray_QString_data() { compare_data(); } void compare_QByteArray_QString() { compare_impl(); } -#ifdef NOT_YET_IMPLEMENTED void compare_QByteArray_QStringView_data() { compare_data(); } void compare_QByteArray_QStringView() { compare_impl(); } -#endif void compare_QByteArray_QUtf8StringView_data() { compare_data(); } void compare_QByteArray_QUtf8StringView() { compare_impl(); } void compare_QByteArray_QLatin1String_data() { compare_data(); } @@ -308,10 +304,8 @@ private Q_SLOTS: void compare_QByteArrayView_char16_t() { compare_impl(); } void compare_QByteArrayView_QString_data() { compare_data(); } void compare_QByteArrayView_QString() { compare_impl(); } -#ifdef NOT_YET_IMPLEMENTED void compare_QByteArrayView_QStringView_data() { compare_data(); } void compare_QByteArrayView_QStringView() { compare_impl(); } -#endif void compare_QByteArrayView_QUtf8StringView_data() { compare_data(); } void compare_QByteArrayView_QUtf8StringView() { compare_impl(); } void compare_QByteArrayView_QLatin1String_data() { compare_data(); } @@ -329,6 +323,8 @@ private Q_SLOTS: //void compare_const_char_star_char16_t() { compare_impl(); } void compare_const_char_star_QString_data() { compare_data(); } void compare_const_char_star_QString() { compare_impl(); } + void compare_const_char_star_QStringView_data() { compare_data(); } + void compare_const_char_star_QStringView() { compare_impl(); } void compare_const_char_star_QUtf8StringView_data() { compare_data(); } void compare_const_char_star_QUtf8StringView() { compare_impl(); } void compare_const_char_star_QLatin1String_data() { compare_data(false); }