diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2024-02-15 12:36:39 +0100 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2024-02-20 01:04:33 +0100 |
commit | fb50ab700600cbcdf21e5d06321f357eae4303bb (patch) | |
tree | c2d5a9a25513c554d63c5f1c1bfcd4704136d921 /src/corelib/text/qbytearrayview.h | |
parent | 1f92ff3ee08259d56209191cff24983b5969d50c (diff) |
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>
Diffstat (limited to 'src/corelib/text/qbytearrayview.h')
-rw-r--r-- | src/corelib/text/qbytearrayview.h | 35 |
1 files changed, 22 insertions, 13 deletions
diff --git a/src/corelib/text/qbytearrayview.h b/src/corelib/text/qbytearrayview.h index 2962453754..a80ef6797d 100644 --- a/src/corelib/text/qbytearrayview.h +++ b/src/corelib/text/qbytearrayview.h @@ -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; }; |