Simplify QLatin1StringView vs byte array relational operators

Now when QLatin1StringView implements relational operators with
QByteArrayView in terms of new comparison helper macros and helper
methods taking QByteArrayView, we can easily re-use these helper
methods to provide comparison with QByteArray and const char *.

QLatin1StringView already provided almost all of these operations,
partly as hidden friend functions, partly as inline methods.
Since the class is not exported, and the methods were inline, we
can just remove all of them and replace them with the comparison
helper macros.

This should speed up the relational operators, because they do not
construct string objects using QString::fromUtf8() anymore, but use
QUtf8StringView instead.

This also adds the previously missing QByteArray vs QLatin1StringView
relational operators.

Task-number: QTBUG-117661
Change-Id: I17a9185127ae130dab9409c6340a58f5d39f5a10
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ivan Solovev 2024-02-15 15:10:56 +01:00
parent 4832426d1b
commit 77bec4f7c8
4 changed files with 54 additions and 115 deletions

View File

@ -312,28 +312,9 @@ private:
public:
#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
QT_ASCII_CAST_WARN inline bool operator==(const char *s) const;
QT_ASCII_CAST_WARN inline bool operator!=(const char *s) const;
QT_ASCII_CAST_WARN inline bool operator<(const char *s) const;
QT_ASCII_CAST_WARN inline bool operator>(const char *s) const;
QT_ASCII_CAST_WARN inline bool operator<=(const char *s) const;
QT_ASCII_CAST_WARN inline bool operator>=(const char *s) const;
QT_ASCII_CAST_WARN inline bool operator==(const QByteArray &s) const;
QT_ASCII_CAST_WARN inline bool operator!=(const QByteArray &s) const;
QT_ASCII_CAST_WARN inline bool operator<(const QByteArray &s) const;
QT_ASCII_CAST_WARN inline bool operator>(const QByteArray &s) const;
QT_ASCII_CAST_WARN inline bool operator<=(const QByteArray &s) const;
QT_ASCII_CAST_WARN inline bool operator>=(const QByteArray &s) const;
Q_DECLARE_STRONGLY_ORDERED(QLatin1StringView, QByteArrayView, QT_ASCII_CAST_WARN)
QT_ASCII_CAST_WARN friend bool operator==(const char *s1, QLatin1StringView s2) { return compare_helper(s2, s1) == 0; }
QT_ASCII_CAST_WARN friend bool operator!=(const char *s1, QLatin1StringView s2) { return compare_helper(s2, s1) != 0; }
QT_ASCII_CAST_WARN friend bool operator< (const char *s1, QLatin1StringView s2) { return compare_helper(s2, s1) > 0; }
QT_ASCII_CAST_WARN friend bool operator> (const char *s1, QLatin1StringView s2) { return compare_helper(s2, s1) < 0; }
QT_ASCII_CAST_WARN friend bool operator<=(const char *s1, QLatin1StringView s2) { return compare_helper(s2, s1) >= 0; }
QT_ASCII_CAST_WARN friend bool operator>=(const char *s1, QLatin1StringView s2) { return compare_helper(s2, s1) <= 0; }
Q_DECLARE_STRONGLY_ORDERED(QLatin1StringView, QByteArray, QT_ASCII_CAST_WARN)
Q_DECLARE_STRONGLY_ORDERED(QLatin1StringView, const char *, QT_ASCII_CAST_WARN)
#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
private:

View File

@ -840,14 +840,13 @@
*/
/*!
\fn bool QLatin1StringView::operator==(const char *other) const
\fn bool QLatin1StringView::operator==(const QLatin1StringView &lhs, const char * const &rhs)
\since 4.3
Returns \c true if the string is equal to const char pointer \a other;
Returns \c true if the string \a lhs is equal to const char pointer \a rhs;
otherwise returns \c false.
The \a other const char pointer is converted to a QString using
the QString::fromUtf8() function.
The \a rhs const char pointer is converted to a QUtf8StringView.
You can disable this operator by defining
\l QT_NO_CAST_FROM_ASCII when you compile your applications. This
@ -858,12 +857,11 @@
*/
/*!
\fn bool QLatin1StringView::operator==(const QByteArray &other) const
\fn bool QLatin1StringView::operator==(const QLatin1StringView &lhs, const QByteArray &rhs)
\since 5.0
\overload
The \a other byte array is converted to a QString using
the QString::fromUtf8() function.
The \a rhs byte array is converted to a QUtf8StringView.
You can disable this operator by defining
\l QT_NO_CAST_FROM_ASCII when you compile your applications. This
@ -872,14 +870,13 @@
*/
/*!
\fn bool QLatin1StringView::operator!=(const char *other) const
\fn bool QLatin1StringView::operator!=(const QLatin1StringView &lhs, const char * const &rhs)
\since 4.3
Returns \c true if this string is not equal to const char pointer \a other;
Returns \c true if the string \a lhs is not equal to const char pointer \a rhs;
otherwise returns \c false.
The \a other const char pointer is converted to a QString using
the QString::fromUtf8() function.
The \a rhs const char pointer is converted to a QUtf8StringView.
You can disable this operator by defining
\l QT_NO_CAST_FROM_ASCII when you compile your applications. This
@ -890,12 +887,11 @@
*/
/*!
\fn bool QLatin1StringView::operator!=(const QByteArray &other) const
\fn bool QLatin1StringView::operator!=(const QLatin1StringView &lhs, const QByteArray &rhs)
\since 5.0
\overload operator!=()
The \a other byte array is converted to a QString using
the QString::fromUtf8() function.
The \a rhs byte array is converted to a QUtf8StringView.
You can disable this operator by defining
\l QT_NO_CAST_FROM_ASCII when you compile your applications. This
@ -904,14 +900,13 @@
*/
/*!
\fn bool QLatin1StringView::operator>(const char *other) const
\fn bool QLatin1StringView::operator>(const QLatin1StringView &lhs, const char * const &rhs)
\since 4.3
Returns \c true if this string is lexically greater than const char pointer
\a other; otherwise returns \c false.
Returns \c true if the string \a lhs is lexically greater than const char pointer
\a rhs; otherwise returns \c false.
The \a other const char pointer is converted to a QString using
the QString::fromUtf8() function.
The \a rhs const char pointer is converted to a QUtf8StringView.
You can disable this operator by defining \l QT_NO_CAST_FROM_ASCII
when you compile your applications. This can be useful if you want
@ -922,12 +917,11 @@
*/
/*!
\fn bool QLatin1StringView::operator>(const QByteArray &other) const
\fn bool QLatin1StringView::operator>(const QLatin1StringView &lhs, const QByteArray &rhs)
\since 5.0
\overload
The \a other byte array is converted to a QString using
the QString::fromUtf8() function.
The \a rhs byte array is converted to a QUtf8StringView.
You can disable this operator by defining \l QT_NO_CAST_FROM_ASCII
when you compile your applications. This can be useful if you want
@ -936,14 +930,13 @@
*/
/*!
\fn bool QLatin1StringView::operator<(const char *other) const
\fn bool QLatin1StringView::operator<(const QLatin1StringView &lhs, const char * const &rhs)
\since 4.3
Returns \c true if this string is lexically less than const char pointer
\a other; otherwise returns \c false.
Returns \c true if the string \a lhs is lexically less than const char pointer
\a rhs; otherwise returns \c false.
The \a other const char pointer is converted to a QString using
the QString::fromUtf8() function.
The \a rhs const char pointer is converted to a QUtf8StringView.
You can disable this operator by defining
\l QT_NO_CAST_FROM_ASCII when you compile your applications. This
@ -954,12 +947,11 @@
*/
/*!
\fn bool QLatin1StringView::operator<(const QByteArray &other) const
\fn bool QLatin1StringView::operator<(const QLatin1StringView &lhs, const QByteArray &rhs)
\since 5.0
\overload
The \a other byte array is converted to a QString using
the QString::fromUtf8() function.
The \a rhs byte array is converted to a QUtf8StringView.
You can disable this operator by defining
\l QT_NO_CAST_FROM_ASCII when you compile your applications. This
@ -968,14 +960,13 @@
*/
/*!
\fn bool QLatin1StringView::operator>=(const char *other) const
\fn bool QLatin1StringView::operator>=(const QLatin1StringView &lhs, const char * const &rhs)
\since 4.3
Returns \c true if this string is lexically greater than or equal to
const char pointer \a other; otherwise returns \c false.
Returns \c true if the string \a lhs is lexically greater than or equal to
const char pointer \a rhs; otherwise returns \c false.
The \a other const char pointer is converted to a QString using
the QString::fromUtf8() function.
The \a rhs const char pointer is converted to a QUtf8StringView.
You can disable this operator by defining
\l QT_NO_CAST_FROM_ASCII when you compile your applications. This
@ -986,12 +977,11 @@
*/
/*!
\fn bool QLatin1StringView::operator>=(const QByteArray &other) const
\fn bool QLatin1StringView::operator>=(const QLatin1StringView &lhs, const QByteArray &rhs)
\since 5.0
\overload
The \a other byte array is converted to a QString using
the QString::fromUtf8() function.
The \a rhs byte array is converted to a QUtf8StringView.
You can disable this operator by defining
\l QT_NO_CAST_FROM_ASCII when you compile your applications. This
@ -1000,14 +990,13 @@
*/
/*!
\fn bool QLatin1StringView::operator<=(const char *other) const
\fn bool QLatin1StringView::operator<=(const QLatin1StringView &lhs, const char * const &rhs)
\since 4.3
Returns \c true if this string is lexically less than or equal to
const char pointer \a other; otherwise returns \c false.
Returns \c true if the string \a lhs is lexically less than or equal to
const char pointer \a rhs; otherwise returns \c false.
The \a other const char pointer is converted to a QString using
the QString::fromUtf8() function.
The \a rhs const char pointer is converted to a QUtf8StringView.
You can disable this operator by defining
\l QT_NO_CAST_FROM_ASCII when you compile your applications. This
@ -1018,12 +1007,11 @@
*/
/*!
\fn bool QLatin1StringView::operator<=(const QByteArray &other) const
\fn bool QLatin1StringView::operator<=(const QLatin1StringView &lhs, const QByteArray &rhs)
\since 5.0
\overload
The \a other byte array is converted to a QString using
the QString::fromUtf8() function.
The \a rhs byte array is converted to a QUtf8StringView.
You can disable this operator by defining
\l QT_NO_CAST_FROM_ASCII when you compile your applications. This
@ -1185,35 +1173,35 @@
string view \a s2; otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator==(const char *s1, QLatin1StringView s2)
/*! \fn bool QLatin1StringView::operator==(const char * const &lhs, const QLatin1StringView &rhs)
Returns \c true if const char pointer \a s1 is lexically equal to
string \a s2; otherwise returns \c false.
Returns \c true if const char pointer \a lhs is lexically equal to
string \a rhs; otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator<(const char *s1, QLatin1StringView s2)
/*! \fn bool QLatin1StringView::operator<(const char * const &lhs, const QLatin1StringView &rhs)
Returns \c true if const char pointer \a s1 is lexically less than
string \a s2; otherwise returns \c false.
Returns \c true if const char pointer \a lhs is lexically less than
string \a rhs; otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator>(const char *s1, QLatin1StringView s2)
/*! \fn bool QLatin1StringView::operator>(const char * const &lhs, const QLatin1StringView &rhs)
Returns \c true if const char pointer \a s1 is lexically greater than
string \a s2; otherwise returns \c false.
Returns \c true if const char pointer \a lhs is lexically greater than
string \a rhs; otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator!=(const char *s1, QLatin1StringView s2)
/*! \fn bool QLatin1StringView::operator!=(const char * const &lhs, const QLatin1StringView &rhs)
Returns \c true if const char pointer \a s1 is lexically not equal to
string \a s2; otherwise returns \c false.
Returns \c true if const char pointer \a lhs is lexically not equal to
string \a rhs; otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator<=(const char *s1, QLatin1StringView s2)
/*! \fn bool QLatin1StringView::operator<=(const char * const &lhs, const QLatin1StringView &rhs)
Returns \c true if const char pointer \a s1 is lexically less than or
equal to string \a s2; otherwise returns \c false.
Returns \c true if const char pointer \a lhs is lexically less than or
equal to string \a rhs; otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator>=(const char *s1, QLatin1StringView s2)
/*! \fn bool QLatin1StringView::operator>=(const char * const &lhs, const QLatin1StringView &rhs)
Returns \c true if const char pointer \a s1 is lexically greater than or
equal to string \a s2; otherwise returns \c false.
Returns \c true if const char pointer \a lhs is lexically greater than or
equal to string \a rhs; otherwise returns \c false.
*/
/*!

View File

@ -1336,35 +1336,6 @@ bool QString::operator<=(const char *s) const
bool QString::operator>=(const char *s) const
{ return QString::compare_helper(constData(), size(), s, -1) >= 0; }
//
// QLatin1StringView inline members that require QString:
//
QT_ASCII_CAST_WARN bool QLatin1StringView::operator==(const char *s) const
{ return QString::fromUtf8(s) == *this; }
QT_ASCII_CAST_WARN bool QLatin1StringView::operator!=(const char *s) const
{ return QString::fromUtf8(s) != *this; }
QT_ASCII_CAST_WARN bool QLatin1StringView::operator<(const char *s) const
{ return QString::fromUtf8(s) > *this; }
QT_ASCII_CAST_WARN bool QLatin1StringView::operator>(const char *s) const
{ return QString::fromUtf8(s) < *this; }
QT_ASCII_CAST_WARN bool QLatin1StringView::operator<=(const char *s) const
{ return QString::fromUtf8(s) >= *this; }
QT_ASCII_CAST_WARN bool QLatin1StringView::operator>=(const char *s) const
{ return QString::fromUtf8(s) <= *this; }
QT_ASCII_CAST_WARN bool QLatin1StringView::operator==(const QByteArray &s) const
{ return QString::fromUtf8(s) == *this; }
QT_ASCII_CAST_WARN bool QLatin1StringView::operator!=(const QByteArray &s) const
{ return QString::fromUtf8(s) != *this; }
QT_ASCII_CAST_WARN bool QLatin1StringView::operator<(const QByteArray &s) const
{ return QString::fromUtf8(s) > *this; }
QT_ASCII_CAST_WARN bool QLatin1StringView::operator>(const QByteArray &s) const
{ return QString::fromUtf8(s) < *this; }
QT_ASCII_CAST_WARN bool QLatin1StringView::operator<=(const QByteArray &s) const
{ return QString::fromUtf8(s) >= *this; }
QT_ASCII_CAST_WARN bool QLatin1StringView::operator>=(const QByteArray &s) const
{ return QString::fromUtf8(s) <= *this; }
QT_ASCII_CAST_WARN bool QString::operator==(const QByteArray &s) const
{ return QString::compare_helper(constData(), size(), s.constData(), s.size()) == 0; }
QT_ASCII_CAST_WARN bool QString::operator!=(const QByteArray &s) const

View File

@ -61,7 +61,6 @@ constexpr bool is_fake_comparator_v = false;
/*end*/
MAKE_ALL(QByteArray, QChar)
MAKE_ALL(QByteArray, QLatin1String)
MAKE_ALL(QByteArray, char16_t)
MAKE_ALL(char16_t, QByteArray)