Add QByteArrayView vs const char * relational operators

... by using the new comparison helper macros.

Also, convert the existing QByteArrayView relational operators to use
these macros.

An attempt to define the helper functions comparesEqual() and
compareThreeWay() as hidden friends leads to compilation errors, because
the QByteArrayView(QLatin1StringView) constructor gets somehow disabled.
Apparently, it cannot satisfy the
QtPrivate::IsContainerCompatibleWithQByteArrayView trait anymore.
I could not find a reason for that, so I just defined the helper
functions as static inline private members of QByteArrayView. This fixes
the issue.

This allows to enable related tests in tst_qstringapisymmetry.

Task-number: QTBUG-108805
Change-Id: I35a69e99db8c61531ec726dab5b242b857f69e85
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ivan Solovev 2024-02-15 12:36:39 +01:00
parent 1f92ff3ee0
commit fb50ab7006
3 changed files with 28 additions and 23 deletions

View File

@ -4,6 +4,7 @@
#define QBYTEARRAYVIEW_H
#include <QtCore/qbytearrayalgorithms.h>
#include <QtCore/qcompare.h>
#include <QtCore/qstringfwd.h>
#include <QtCore/qarraydata.h>
@ -315,19 +316,6 @@ public:
[[nodiscard]] constexpr char first() const { return front(); }
[[nodiscard]] constexpr char last() const { return back(); }
friend inline bool operator==(QByteArrayView lhs, QByteArrayView rhs) noexcept
{ return lhs.size() == rhs.size() && (!lhs.size() || memcmp(lhs.data(), rhs.data(), lhs.size()) == 0); }
friend inline bool operator!=(QByteArrayView lhs, QByteArrayView rhs) noexcept
{ return !(lhs == rhs); }
friend inline bool operator< (QByteArrayView lhs, QByteArrayView rhs) noexcept
{ return QtPrivate::compareMemory(lhs, rhs) < 0; }
friend inline bool operator<=(QByteArrayView lhs, QByteArrayView rhs) noexcept
{ return QtPrivate::compareMemory(lhs, rhs) <= 0; }
friend inline bool operator> (QByteArrayView lhs, QByteArrayView rhs) noexcept
{ return !(lhs <= rhs); }
friend inline bool operator>=(QByteArrayView lhs, QByteArrayView rhs) noexcept
{ return !(lhs < rhs); }
private:
Q_ALWAYS_INLINE constexpr void verify([[maybe_unused]] qsizetype pos = 0,
[[maybe_unused]] qsizetype n = 1) const
@ -338,6 +326,27 @@ private:
Q_ASSERT(n <= size() - pos);
}
static inline bool
comparesEqual(const QByteArrayView &lhs, const QByteArrayView &rhs) noexcept
{
return lhs.size() == rhs.size()
&& (!lhs.size() || memcmp(lhs.data(), rhs.data(), lhs.size()) == 0);
}
static inline Qt::strong_ordering
compareThreeWay(const QByteArrayView &lhs, const QByteArrayView &rhs) noexcept
{
const int res = QtPrivate::compareMemory(lhs, rhs);
return Qt::compareThreeWay(res, 0);
}
Q_DECLARE_STRONGLY_ORDERED(QByteArrayView)
static inline bool comparesEqual(const QByteArrayView &lhs, const char *rhs) noexcept
{ return comparesEqual(lhs, QByteArrayView(rhs)); }
static inline Qt::strong_ordering
compareThreeWay(const QByteArrayView &lhs, const char *rhs) noexcept
{ return compareThreeWay(lhs, QByteArrayView(rhs)); }
Q_DECLARE_STRONGLY_ORDERED(QByteArrayView, const char *)
qsizetype m_size;
const storage_type *m_data;
};

View File

@ -332,12 +332,12 @@
*/
/*! //! friend
\fn int QByteArrayView::operator==(QByteArrayView lhs, QByteArrayView rhs)
\fn int QByteArrayView::operator!=(QByteArrayView lhs, QByteArrayView rhs)
\fn int QByteArrayView::operator< (QByteArrayView lhs, QByteArrayView rhs)
\fn int QByteArrayView::operator<=(QByteArrayView lhs, QByteArrayView rhs)
\fn int QByteArrayView::operator> (QByteArrayView lhs, QByteArrayView rhs)
\fn int QByteArrayView::operator>=(QByteArrayView lhs, QByteArrayView rhs)
\fn int QByteArrayView::operator==(const QByteArrayView &lhs, const QByteArrayView &rhs)
\fn int QByteArrayView::operator!=(const QByteArrayView &lhs, const QByteArrayView &rhs)
\fn int QByteArrayView::operator< (const QByteArrayView &lhs, const QByteArrayView &rhs)
\fn int QByteArrayView::operator<=(const QByteArrayView &lhs, const QByteArrayView &rhs)
\fn int QByteArrayView::operator> (const QByteArrayView &lhs, const QByteArrayView &rhs)
\fn int QByteArrayView::operator>=(const QByteArrayView &lhs, const QByteArrayView &rhs)
Comparison operators for QByteArrayView.
*/

View File

@ -320,10 +320,8 @@ private Q_SLOTS:
void compare_QByteArrayView_QByteArray() { compare_impl<QByteArrayView, QByteArray>(); }
void compare_QByteArrayView_QByteArrayView_data() { compare_data(); }
void compare_QByteArrayView_QByteArrayView() { compare_impl<QByteArrayView, QByteArrayView>(); }
#ifdef AMBIGUOUS_CALL
void compare_QByteArrayView_const_char_star_data() { compare_data(); }
void compare_QByteArrayView_const_char_star() { compare_impl<QByteArrayView, const char *>(); }
#endif
void compare_const_char_star_QChar_data() { compare_data(false); }
void compare_const_char_star_QChar() { compare_impl<const char *, QChar>(); }
@ -337,10 +335,8 @@ private Q_SLOTS:
void compare_const_char_star_QLatin1String() { compare_impl<const char *, QLatin1String>(); }
void compare_const_char_star_QByteArray_data() { compare_data(); }
void compare_const_char_star_QByteArray() { compare_impl<const char *, QByteArray>(); }
#ifdef AMBIGUOUS_CALL
void compare_const_char_star_QByteArrayView_data() { compare_data(); }
void compare_const_char_star_QByteArrayView() { compare_impl<const char *, QByteArrayView>(); }
#endif
//void compare_const_char_star_const_char_star_data() { compare_data(); }
//void compare_const_char_star_const_char_star() { compare_impl<const char *, const char *>(); }