diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2024-02-15 13:19:29 +0100 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2024-02-20 01:04:33 +0100 |
commit | 4832426d1b22099cbdacaede17a4fd50336b9ae3 (patch) | |
tree | c0af70c22ce540ef7775b4c1a4c1f839b9a67fb9 | |
parent | fb50ab700600cbcdf21e5d06321f357eae4303bb (diff) |
Add QStringView vs byte array relational operators
... by using the new comparison helper macors.
Note that by providing helper functions for QByteArrayView,
we can support all three types: QByteArray, QByteArrayView, and
const char *.
Use the regular QT_NO_CAST_FROM_ASCII and
QT_RESTRICTED_CAST_FROM_ASCII guards to disable the operators
if the cast from ASCII is forbidden.
Also use QT_ASCII_CAST_WARN on each operator.
This allows to enable related tests in tst_qstringapisymmetry.
Task-number: QTBUG-117661
Task-number: QTBUG-108805
Change-Id: I0d77c30245d8b5ac4b8cfd98d650c1885aca2005
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r-- | src/corelib/text/qstring.cpp | 22 | ||||
-rw-r--r-- | src/corelib/text/qstringview.h | 18 | ||||
-rw-r--r-- | tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp | 8 |
3 files changed, 42 insertions, 6 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index ea74cb9d72..d3bb1d92d1 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -6588,6 +6588,28 @@ int QString::compare_helper(const QChar *data1, qsizetype length1, const char *d \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 +*/ bool QLatin1StringView::equal_helper(QLatin1StringView s1, const char *s2, qsizetype len) noexcept { // because qlatin1stringview.h can't include qutf8stringview.h diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index 57ae1fe869..2e099a9ba5 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -5,6 +5,7 @@ #define QSTRINGVIEW_H #include <QtCore/qchar.h> +#include <QtCore/qcompare.h> #include <QtCore/qbytearray.h> #include <QtCore/qstringliteral.h> #include <QtCore/qstringalgorithms.h> @@ -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 e1a2232b7b..7d74253407 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<QStringView, QLatin1String>(); } -#ifdef NOT_YET_IMPLMENTED void compare_QStringView_QByteArray_data() { compare_data(); } void compare_QStringView_QByteArray() { compare_impl<QStringView, QByteArray>(); } void compare_QStringView_QByteArrayView_data() { compare_data(); } void compare_QStringView_QByteArrayView() { compare_impl<QStringView, QByteArrayView>(); } void compare_QStringView_const_char_star_data() { compare_data(); } void compare_QStringView_const_char_star() { compare_impl<QStringView, const char *>(); } -#endif void compare_QUtf8StringView_QChar_data() { compare_data(false); } void compare_QUtf8StringView_QChar() { compare_impl<QUtf8StringView, QChar>(); } @@ -287,10 +285,8 @@ private Q_SLOTS: void compare_QByteArray_char16_t() { compare_impl<QByteArray, char16_t>(); } void compare_QByteArray_QString_data() { compare_data(); } void compare_QByteArray_QString() { compare_impl<QByteArray, QString>(); } -#ifdef NOT_YET_IMPLEMENTED void compare_QByteArray_QStringView_data() { compare_data(); } void compare_QByteArray_QStringView() { compare_impl<QByteArray, QStringView>(); } -#endif void compare_QByteArray_QUtf8StringView_data() { compare_data(); } void compare_QByteArray_QUtf8StringView() { compare_impl<QByteArray, QUtf8StringView>(); } void compare_QByteArray_QLatin1String_data() { compare_data(); } @@ -308,10 +304,8 @@ private Q_SLOTS: void compare_QByteArrayView_char16_t() { compare_impl<QByteArrayView, char16_t>(); } void compare_QByteArrayView_QString_data() { compare_data(); } void compare_QByteArrayView_QString() { compare_impl<QByteArrayView, QString>(); } -#ifdef NOT_YET_IMPLEMENTED void compare_QByteArrayView_QStringView_data() { compare_data(); } void compare_QByteArrayView_QStringView() { compare_impl<QByteArrayView, QStringView>(); } -#endif void compare_QByteArrayView_QUtf8StringView_data() { compare_data(); } void compare_QByteArrayView_QUtf8StringView() { compare_impl<QByteArrayView, QUtf8StringView>(); } void compare_QByteArrayView_QLatin1String_data() { compare_data(); } @@ -329,6 +323,8 @@ private Q_SLOTS: //void compare_const_char_star_char16_t() { compare_impl<const char *, char16_t>(); } void compare_const_char_star_QString_data() { compare_data(); } void compare_const_char_star_QString() { compare_impl<const char *, QString>(); } + void compare_const_char_star_QStringView_data() { compare_data(); } + void compare_const_char_star_QStringView() { compare_impl<const char *, QStringView>(); } void compare_const_char_star_QUtf8StringView_data() { compare_data(); } void compare_const_char_star_QUtf8StringView() { compare_impl<const char *, QUtf8StringView>(); } void compare_const_char_star_QLatin1String_data() { compare_data(false); } |