From fb50ab700600cbcdf21e5d06321f357eae4303bb Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Thu, 15 Feb 2024 12:36:39 +0100 Subject: [PATCH] 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 --- src/corelib/text/qbytearrayview.h | 35 ++++++++++++------- src/corelib/text/qbytearrayview.qdoc | 12 +++---- .../tst_qstringapisymmetry.cpp | 4 --- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/corelib/text/qbytearrayview.h b/src/corelib/text/qbytearrayview.h index 29624537542..a80ef6797dc 100644 --- a/src/corelib/text/qbytearrayview.h +++ b/src/corelib/text/qbytearrayview.h @@ -4,6 +4,7 @@ #define QBYTEARRAYVIEW_H #include +#include #include #include @@ -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; }; diff --git a/src/corelib/text/qbytearrayview.qdoc b/src/corelib/text/qbytearrayview.qdoc index 2dc63e469c8..fc8325cc607 100644 --- a/src/corelib/text/qbytearrayview.qdoc +++ b/src/corelib/text/qbytearrayview.qdoc @@ -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. */ diff --git a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp index 635b9cfa52f..e1a2232b7b6 100644 --- a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -320,10 +320,8 @@ private Q_SLOTS: void compare_QByteArrayView_QByteArray() { compare_impl(); } void compare_QByteArrayView_QByteArrayView_data() { compare_data(); } void compare_QByteArrayView_QByteArrayView() { compare_impl(); } -#ifdef AMBIGUOUS_CALL void compare_QByteArrayView_const_char_star_data() { compare_data(); } void compare_QByteArrayView_const_char_star() { compare_impl(); } -#endif void compare_const_char_star_QChar_data() { compare_data(false); } void compare_const_char_star_QChar() { compare_impl(); } @@ -337,10 +335,8 @@ private Q_SLOTS: void compare_const_char_star_QLatin1String() { compare_impl(); } void compare_const_char_star_QByteArray_data() { compare_data(); } void compare_const_char_star_QByteArray() { compare_impl(); } -#ifdef AMBIGUOUS_CALL void compare_const_char_star_QByteArrayView_data() { compare_data(); } void compare_const_char_star_QByteArrayView() { compare_impl(); } -#endif //void compare_const_char_star_const_char_star_data() { compare_data(); } //void compare_const_char_star_const_char_star() { compare_impl(); }