QLatin1StringView: consistently use comparison helper macros

Replace all friend relational operators with comparison helper macros.
This allows to enable operator<=>() in C++20 builds.

Use new \compares and \compareswith qdoc commands in the documentation.

Task-number: QTBUG-117661
Change-Id: I0445d7af3c2d692c810e15e83041de2a19f946a9
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Ivan Solovev 2024-02-20 18:42:42 +01:00
parent 3d0eaf863e
commit 34372af708
2 changed files with 103 additions and 112 deletions

View File

@ -250,55 +250,38 @@ public:
-> decltype(qTokenize(*this, std::forward<Needle>(needle), flags...))
{ return qTokenize(*this, std::forward<Needle>(needle), flags...); }
friend bool operator==(QLatin1StringView s1, QLatin1StringView s2) noexcept
{ return QByteArrayView(s1) == QByteArrayView(s2); }
friend bool operator!=(QLatin1StringView s1, QLatin1StringView s2) noexcept
{ return !(s1 == s2); }
friend bool operator<(QLatin1StringView s1, QLatin1StringView s2) noexcept
friend bool comparesEqual(const QLatin1StringView &s1, const QLatin1StringView &s2) noexcept
{ return s1.size() == s2.size() && QtPrivate::equalStrings(s1, s2); }
friend Qt::strong_ordering
compareThreeWay(const QLatin1StringView &s1, const QLatin1StringView &s2) noexcept
{
const qsizetype len = qMin(s1.size(), s2.size());
const int r = len ? memcmp(s1.latin1(), s2.latin1(), len) : 0;
return r < 0 || (r == 0 && s1.size() < s2.size());
const int res = QtPrivate::compareStrings(s1, s2);
return Qt::compareThreeWay(res, 0);
}
friend bool operator>(QLatin1StringView s1, QLatin1StringView s2) noexcept
{ return s2 < s1; }
friend bool operator<=(QLatin1StringView s1, QLatin1StringView s2) noexcept
{ return !(s1 > s2); }
friend bool operator>=(QLatin1StringView s1, QLatin1StringView s2) noexcept
{ return !(s1 < s2); }
Q_DECLARE_STRONGLY_ORDERED(QLatin1StringView)
// QChar <> QLatin1StringView
friend bool operator==(QChar lhs, QLatin1StringView rhs) noexcept { return rhs.size() == 1 && lhs == rhs.front(); }
friend bool operator< (QChar lhs, QLatin1StringView rhs) noexcept { return compare_helper(&lhs, 1, rhs) < 0; }
friend bool operator> (QChar lhs, QLatin1StringView rhs) noexcept { return compare_helper(&lhs, 1, rhs) > 0; }
friend bool operator!=(QChar lhs, QLatin1StringView rhs) noexcept { return !(lhs == rhs); }
friend bool operator<=(QChar lhs, QLatin1StringView rhs) noexcept { return !(lhs > rhs); }
friend bool operator>=(QChar lhs, QLatin1StringView rhs) noexcept { return !(lhs < rhs); }
friend bool operator==(QLatin1StringView lhs, QChar rhs) noexcept { return rhs == lhs; }
friend bool operator!=(QLatin1StringView lhs, QChar rhs) noexcept { return !(rhs == lhs); }
friend bool operator< (QLatin1StringView lhs, QChar rhs) noexcept { return rhs > lhs; }
friend bool operator> (QLatin1StringView lhs, QChar rhs) noexcept { return rhs < lhs; }
friend bool operator<=(QLatin1StringView lhs, QChar rhs) noexcept { return !(rhs < lhs); }
friend bool operator>=(QLatin1StringView lhs, QChar rhs) noexcept { return !(rhs > lhs); }
friend bool comparesEqual(const QLatin1StringView &lhs, QChar rhs) noexcept
{ return lhs.size() == 1 && rhs == lhs.front(); }
friend Qt::strong_ordering
compareThreeWay(const QLatin1StringView &lhs, QChar rhs) noexcept
{
// negate, as the helper function expects QChar as lhs
const int res = -compare_helper(&rhs, 1, lhs);
return Qt::compareThreeWay(res, 0);
}
Q_DECLARE_STRONGLY_ORDERED(QLatin1StringView, QChar)
// QStringView <> QLatin1StringView
friend bool operator==(QStringView lhs, QLatin1StringView rhs) noexcept
friend bool comparesEqual(const QLatin1StringView &lhs, const QStringView &rhs) noexcept
{ return lhs.size() == rhs.size() && QtPrivate::equalStrings(lhs, rhs); }
friend bool operator!=(QStringView lhs, QLatin1StringView rhs) noexcept { return !(lhs == rhs); }
friend bool operator< (QStringView lhs, QLatin1StringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) < 0; }
friend bool operator<=(QStringView lhs, QLatin1StringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) <= 0; }
friend bool operator> (QStringView lhs, QLatin1StringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) > 0; }
friend bool operator>=(QStringView lhs, QLatin1StringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) >= 0; }
friend bool operator==(QLatin1StringView lhs, QStringView rhs) noexcept
{ return lhs.size() == rhs.size() && QtPrivate::equalStrings(lhs, rhs); }
friend bool operator!=(QLatin1StringView lhs, QStringView rhs) noexcept { return !(lhs == rhs); }
friend bool operator< (QLatin1StringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) < 0; }
friend bool operator<=(QLatin1StringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) <= 0; }
friend bool operator> (QLatin1StringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) > 0; }
friend bool operator>=(QLatin1StringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) >= 0; }
friend Qt::strong_ordering
compareThreeWay(const QLatin1StringView &lhs, const QStringView &rhs) noexcept
{
const int res = QtPrivate::compareStrings(lhs, rhs);
return Qt::compareThreeWay(res, 0);
}
Q_DECLARE_STRONGLY_ORDERED(QLatin1StringView, QStringView)
private:
friend bool comparesEqual(const QLatin1StringView &lhs, const QByteArrayView &rhs) noexcept

View File

@ -11,6 +11,14 @@
\ingroup string-processing
\reentrant
\compares strong
\compareswith strong char16_t QChar QStringView QUtf8StringView QString \
{const char16_t *}
\endcompareswith
\compareswith strong {const char *} QByteArray QByteArrayView
The byte array data is interpreted as utf-8.
\endcompareswith
Many of QString's member functions are overloaded to accept
\c{const char *} instead of QString. This includes the copy
constructor, the assignment operator, the comparison operators,
@ -1019,158 +1027,158 @@
go through QObject::tr(), for example.
*/
/*! \fn bool QLatin1StringView::operator==(QLatin1StringView s1, QLatin1StringView s2)
/*! \fn bool QLatin1StringView::operator==(const QLatin1StringView &lhs, const QLatin1StringView &rhs)
Returns \c true if string \a s1 is lexically equal to string \a s2;
Returns \c true if string \a lhs is lexically equal to string \a rhs;
otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator!=(QLatin1StringView s1, QLatin1StringView s2)
/*! \fn bool QLatin1StringView::operator!=(const QLatin1StringView &lhs, const QLatin1StringView &rhs)
Returns \c true if string \a s1 is lexically not equal to string \a s2;
Returns \c true if string \a lhs is lexically not equal to string \a rhs;
otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator<(QLatin1StringView s1, QLatin1StringView s2)
/*! \fn bool QLatin1StringView::operator<(const QLatin1StringView &lhs, const QLatin1StringView &rhs)
Returns \c true if string \a s1 is lexically less than string \a s2;
Returns \c true if string \a lhs is lexically less than string \a rhs;
otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator<=(QLatin1StringView s1, QLatin1StringView s2)
/*! \fn bool QLatin1StringView::operator<=(const QLatin1StringView &lhs, const QLatin1StringView &rhs)
Returns \c true if string \a s1 is lexically less than or equal to
string \a s2; otherwise returns \c false.
Returns \c true if string \a lhs is lexically less than or equal to
string \a rhs; otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator>(QLatin1StringView s1, QLatin1StringView s2)
/*! \fn bool QLatin1StringView::operator>(const QLatin1StringView &lhs, const QLatin1StringView &rhs)
Returns \c true if string \a s1 is lexically greater than string \a s2;
Returns \c true if string \a lhs is lexically greater than string \a rhs;
otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator>=(QLatin1StringView s1, QLatin1StringView s2)
/*! \fn bool QLatin1StringView::operator>=(const QLatin1StringView &lhs, const QLatin1StringView &rhs)
Returns \c true if string \a s1 is lexically greater than or equal
to string \a s2; otherwise returns \c false.
Returns \c true if string \a lhs is lexically greater than or equal
to string \a rhs; otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator==(QChar ch, QLatin1StringView s)
/*! \fn bool QLatin1StringView::operator==(const QChar &lhs, const QLatin1StringView &rhs)
Returns \c true if char \a ch is lexically equal to string \a s;
Returns \c true if char \a lhs is lexically equal to string \a rhs;
otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator<(QChar ch, QLatin1StringView s)
/*! \fn bool QLatin1StringView::operator<(const QChar &lhs, const QLatin1StringView &rhs)
Returns \c true if char \a ch is lexically less than string \a s;
Returns \c true if char \a lhs is lexically less than string \a rhs;
otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator>(QChar ch, QLatin1StringView s)
Returns \c true if char \a ch is lexically greater than string \a s;
/*! \fn bool QLatin1StringView::operator>(const QChar &lhs, const QLatin1StringView &rhs)
Returns \c true if char \a lhs is lexically greater than string \a rhs;
otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator!=(QChar ch, QLatin1StringView s)
/*! \fn bool QLatin1StringView::operator!=(const QChar &lhs, const QLatin1StringView &rhs)
Returns \c true if char \a ch is lexically not equal to string \a s;
Returns \c true if char \a lhs is lexically not equal to string \a rhs;
otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator<=(QChar ch, QLatin1StringView s)
/*! \fn bool QLatin1StringView::operator<=(const QChar &lhs, const QLatin1StringView &rhs)
Returns \c true if char \a ch is lexically less than or equal to
string \a s; otherwise returns \c false.
Returns \c true if char \a lhs is lexically less than or equal to
string \a rhs; otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator>=(QChar ch, QLatin1StringView s)
/*! \fn bool QLatin1StringView::operator>=(const QChar &lhs, const QLatin1StringView &rhs)
Returns \c true if char \a ch is lexically greater than or equal to
string \a s; otherwise returns \c false.
Returns \c true if char \a lhs is lexically greater than or equal to
string \a rhs; otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator==(QLatin1StringView s, QChar ch)
/*! \fn bool QLatin1StringView::operator==(const QLatin1StringView &lhs, const QChar &rhs)
Returns \c true if string \a s is lexically equal to char \a ch;
Returns \c true if string \a lhs is lexically equal to char \a rhs;
otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator<(QLatin1StringView s, QChar ch)
/*! \fn bool QLatin1StringView::operator<(const QLatin1StringView &lhs, const QChar &rhs)
Returns \c true if string \a s is lexically less than char \a ch;
Returns \c true if string \a lhs is lexically less than char \a rhs;
otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator>(QLatin1StringView s, QChar ch)
/*! \fn bool QLatin1StringView::operator>(const QLatin1StringView &lhs, const QChar &rhs)
Returns \c true if string \a s is lexically greater than char \a ch;
Returns \c true if string \a lhs is lexically greater than char \a rhs;
otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator!=(QLatin1StringView s, QChar ch)
/*! \fn bool QLatin1StringView::operator!=(const QLatin1StringView &lhs, const QChar &rhs)
Returns \c true if string \a s is lexically not equal to char \a ch;
Returns \c true if string \a lhs is lexically not equal to char \a rhs;
otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator<=(QLatin1StringView s, QChar ch)
/*! \fn bool QLatin1StringView::operator<=(const QLatin1StringView &lhs, const QChar &rhs)
Returns \c true if string \a s is lexically less than or equal to
char \a ch; otherwise returns \c false.
Returns \c true if string \a lhs is lexically less than or equal to
char \a rhs; otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator>=(QLatin1StringView s, QChar ch)
/*! \fn bool QLatin1StringView::operator>=(const QLatin1StringView &lhs, const QChar &rhs)
Returns \c true if string \a s is lexically greater than or equal to
char \a ch; otherwise returns \c false.
Returns \c true if string \a lhs is lexically greater than or equal to
char \a rhs; otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator==(QStringView s1, QLatin1StringView s2)
/*! \fn bool QLatin1StringView::operator==(const QStringView &lhs, const QLatin1StringView &rhs)
Returns \c true if string view \a s1 is lexically equal to string \a s2;
Returns \c true if string view \a lhs is lexically equal to string \a rhs;
otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator<(QStringView s1, QLatin1StringView s2)
/*! \fn bool QLatin1StringView::operator<(const QStringView &lhs, const QLatin1StringView &rhs)
Returns \c true if string view \a s1 is lexically less than string \a s2;
Returns \c true if string view \a lhs is lexically less than string \a rhs;
otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator>(QStringView s1, QLatin1StringView s2)
/*! \fn bool QLatin1StringView::operator>(const QStringView &lhs, const QLatin1StringView &rhs)
Returns \c true if string view \a s1 is lexically greater than string \a s2;
Returns \c true if string view \a lhs is lexically greater than string \a rhs;
otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator!=(QStringView s1, QLatin1StringView s2)
/*! \fn bool QLatin1StringView::operator!=(const QStringView &lhs, const QLatin1StringView &rhs)
Returns \c true if string view \a s1 is lexically not equal to string \a s2;
Returns \c true if string view \a lhs is lexically not equal to string \a rhs;
otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator<=(QStringView s1, QLatin1StringView s2)
/*! \fn bool QLatin1StringView::operator<=(const QStringView &lhs, const QLatin1StringView &rhs)
Returns \c true if string view \a s1 is lexically less than or equal to
string \a s2; otherwise returns \c false.
Returns \c true if string view \a lhs is lexically less than or equal to
string \a rhs; otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator>=(QStringView s1, QLatin1StringView s2)
/*! \fn bool QLatin1StringView::operator>=(const QStringView &lhs, const QLatin1StringView &rhs)
Returns \c true if string view \a s1 is lexically greater than or equal to
string \a s2; otherwise returns \c false.
Returns \c true if string view \a lhs is lexically greater than or equal to
string \a rhs; otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator==(QLatin1StringView s1, QStringView s2)
/*! \fn bool QLatin1StringView::operator==(const QLatin1StringView &lhs, const QStringView &rhs)
Returns \c true if string \a s1 is lexically equal to string view \a s2;
Returns \c true if string \a lhs is lexically equal to string view \a rhs;
otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator<(QLatin1StringView s1, QStringView s2)
/*! \fn bool QLatin1StringView::operator<(const QLatin1StringView &lhs, const QStringView &rhs)
Returns \c true if string \a s1 is lexically less than string view \a s2;
Returns \c true if string \a lhs is lexically less than string view \a rhs;
otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator>(QLatin1StringView s1, QStringView s2)
/*! \fn bool QLatin1StringView::operator>(const QLatin1StringView &lhs, const QStringView &rhs)
Returns \c true if string \a s1 is lexically greater than string view \a s2;
Returns \c true if string \a lhs is lexically greater than string view \a rhs;
otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator!=(QLatin1StringView s1, QStringView s2)
/*! \fn bool QLatin1StringView::operator!=(const QLatin1StringView &lhs, const QStringView &rhs)
Returns \c true if string \a s1 is lexically not equal to string view \a s2;
Returns \c true if string \a lhs is lexically not equal to string view \a rhs;
otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator<=(QLatin1StringView s1, QStringView s2)
/*! \fn bool QLatin1StringView::operator<=(const QLatin1StringView &lhs, const QStringView &rhs)
Returns \c true if string \a s1 is lexically less than or equal to
string view \a s2; otherwise returns \c false.
Returns \c true if string \a lhs is lexically less than or equal to
string view \a rhs; otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator>=(QLatin1StringView s1, QStringView s2)
/*! \fn bool QLatin1StringView::operator>=(const QLatin1StringView &lhs, const QStringView &rhs)
Returns \c true if string \a s1 is lexically greater than or equal to
string view \a s2; otherwise returns \c false.
Returns \c true if string \a lhs is lexically greater than or equal to
string view \a rhs; otherwise returns \c false.
*/
/*! \fn bool QLatin1StringView::operator==(const char * const &lhs, const QLatin1StringView &rhs)