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>
This commit is contained in:
Ivan Solovev 2024-02-15 13:19:29 +01:00
parent fb50ab7006
commit 4832426d1b
3 changed files with 42 additions and 6 deletions

View File

@ -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

View File

@ -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);

View File

@ -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); }