Add QUtf8StringView vs byte array relational operators

... by using the new comparison helper macros.

First the relational operators between QU8SV and QBAV where added,
and this resulted in the ambiguities when comparing QU8SV vs QBA.
So, the relational operators between QU8SV and QBA are also added
in the same commit. This, in turn, resulted in ambiguities when
comparing QU8SV and const char *, so add these relational operators
as well.

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: If7919496fdf4519fd2a9398397a39210aadba077
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ivan Solovev 2024-02-12 17:56:31 +01:00
parent 05e5b87ba0
commit 9b945b381a
3 changed files with 59 additions and 7 deletions

View File

@ -1125,7 +1125,7 @@ int QBasicUtf8StringView<UseChar8T>::compare(QStringView other, Qt::CaseSensitiv
//
// QUtf8StringView inline members that require QString:
// QUtf8StringView inline members that require QString, QL1SV or QBA:
//
template <bool UseChar8T>
@ -1141,12 +1141,29 @@ template<bool UseChar8T>
return QtPrivate::compareStrings(*this, other, cs);
}
template<bool UseChar8T>
[[nodiscard]] int QBasicUtf8StringView<UseChar8T>::compare(const QByteArray &other,
Qt::CaseSensitivity cs) const noexcept
{
return QtPrivate::compareStrings(*this,
QBasicUtf8StringView<UseChar8T>(other.data(), other.size()),
cs);
}
template <bool UseChar8T>
[[nodiscard]] bool QBasicUtf8StringView<UseChar8T>::equal(QLatin1StringView other) const noexcept
{
return QtPrivate::equalStrings(*this, other);
}
template <bool UseChar8T>
[[nodiscard]] bool QBasicUtf8StringView<UseChar8T>::equal(const QByteArray &other) const noexcept
{
return size() == other.size()
&& QtPrivate::equalStrings(*this, QBasicUtf8StringView<UseChar8T>(other.data(),
other.size()));
}
//
// QAnyStringView inline members that require QString:
//

View File

@ -297,8 +297,11 @@ public:
Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
[[nodiscard]] int compare(QLatin1StringView other,
Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
[[nodiscard]] int compare(const QByteArray &other,
Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
[[nodiscard]] bool equal(QLatin1StringView other) const noexcept;
[[nodiscard]] bool equal(const QByteArray &other) const noexcept;
private:
[[nodiscard]] static inline int compare(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept
@ -343,6 +346,44 @@ private:
}
Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, QLatin1StringView)
#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
friend bool
comparesEqual(const QBasicUtf8StringView &lhs, const QByteArrayView &rhs) noexcept
{
return lhs.size() == rhs.size()
&& QtPrivate::equalStrings(QBasicUtf8StringView<false>(lhs.data(), lhs.size()),
QBasicUtf8StringView<false>(rhs.data(), rhs.size()));
}
friend Qt::strong_ordering
compareThreeWay(const QBasicUtf8StringView &lhs, const QByteArrayView &rhs) noexcept
{
const int res = QtPrivate::compareStrings(QBasicUtf8StringView<false>(lhs.data(), lhs.size()),
QBasicUtf8StringView<false>(rhs.data(), rhs.size()));
return Qt::compareThreeWay(res, 0);
}
Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, QByteArrayView, QT_ASCII_CAST_WARN)
friend bool
comparesEqual(const QBasicUtf8StringView &lhs, const QByteArray &rhs) noexcept
{
return lhs.equal(rhs);
}
friend Qt::strong_ordering
compareThreeWay(const QBasicUtf8StringView &lhs, const QByteArray &rhs) noexcept
{
const int res = lhs.compare(rhs);
return Qt::compareThreeWay(res, 0);
}
Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, QByteArray, QT_ASCII_CAST_WARN)
friend bool comparesEqual(const QBasicUtf8StringView &lhs, const char *rhs) noexcept
{ return comparesEqual(lhs, QByteArrayView(rhs)); }
friend Qt::strong_ordering
compareThreeWay(const QBasicUtf8StringView &lhs, const char *rhs) noexcept
{ return compareThreeWay(lhs, QByteArrayView(rhs)); }
Q_DECLARE_STRONGLY_ORDERED(QBasicUtf8StringView, const char *, QT_ASCII_CAST_WARN)
#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
[[maybe_unused]] qsizetype n = 1) const
{

View File

@ -72,8 +72,6 @@ MAKE_ALL(QChar, const char*)
MAKE_ALL(QChar, QUtf8StringView)
MAKE_ALL(QString, QUtf8StringView)
MAKE_ALL(QByteArray, QUtf8StringView)
MAKE_ALL(const char*, QUtf8StringView)
MAKE_ALL(QUtf8StringView, QChar)
MAKE_ALL(QUtf8StringView, char16_t)
@ -257,14 +255,12 @@ private Q_SLOTS:
void compare_QUtf8StringView_QUtf8StringView() { compare_impl<QUtf8StringView, QUtf8StringView>(); }
void compare_QUtf8StringView_QLatin1String_data() { compare_data(); }
void compare_QUtf8StringView_QLatin1String() { compare_impl<QUtf8StringView, QLatin1String>(); }
#ifdef NOT_YET_IMPLMENTED
void compare_QUtf8StringView_QByteArray_data() { compare_data(); }
void compare_QUtf8StringView_QByteArray() { compare_impl<QUtf8StringView, QByteArray>(); }
void compare_QUtf8StringView_QByteArrayView_data() { compare_data(); }
void compare_QUtf8StringView_QByteArrayView() { compare_impl<QUtf8StringView, QByteArrayView>(); }
void compare_QUtf8StringView_const_char_star_data() { compare_data(); }
void compare_QUtf8StringView_const_char_star() { compare_impl<QUtf8StringView, const char *>(); }
#endif
void compare_QLatin1String_QChar_data() { compare_data(false); }
void compare_QLatin1String_QChar() { compare_impl<QLatin1String, QChar>(); }
@ -318,10 +314,8 @@ private Q_SLOTS:
void compare_QByteArrayView_QStringView_data() { compare_data(); }
void compare_QByteArrayView_QStringView() { compare_impl<QByteArrayView, QStringView>(); }
#endif
#ifdef AMBIGUOUS_CALL
void compare_QByteArrayView_QUtf8StringView_data() { compare_data(); }
void compare_QByteArrayView_QUtf8StringView() { compare_impl<QByteArrayView, QUtf8StringView>(); }
#endif
void compare_QByteArrayView_QLatin1String_data() { compare_data(); }
void compare_QByteArrayView_QLatin1String() { compare_impl<QByteArrayView, QLatin1String>(); }
#ifdef AMBIGUOUS_CALL