From 7b738ffc583a19138d9772d7ed84c8d771c85a77 Mon Sep 17 00:00:00 2001 From: Tatiana Borisova Date: Fri, 19 Apr 2024 15:20:05 +0200 Subject: [PATCH] QRegularExpression: use modernize comparisons Replace class operators operator==(), operator!=() of QRegularExpression to friend method comparesEqual() and Q_DECLARE_EQUALITY_COMPARABLE macro. Use QT_CORE_REMOVED_SINCE and removed_api.cpp to get rid of current comparison methods and replace them with a friend. Task-number: QTBUG-120304 Change-Id: Ib6fc83d29ad9bc710c2fdf32a3d60131fbf298b6 Reviewed-by: Ivan Solovev --- src/corelib/compat/removed_api.cpp | 5 +++ src/corelib/text/qregularexpression.cpp | 17 ++++---- src/corelib/text/qregularexpression.h | 34 ++++++++-------- .../text/qregularexpression/CMakeLists.txt | 2 + .../tst_qregularexpression.cpp | 40 ++++++++----------- 5 files changed, 49 insertions(+), 49 deletions(-) diff --git a/src/corelib/compat/removed_api.cpp b/src/corelib/compat/removed_api.cpp index 23679bf4ccd..ed285dc8f12 100644 --- a/src/corelib/compat/removed_api.cpp +++ b/src/corelib/compat/removed_api.cpp @@ -1069,6 +1069,11 @@ qsizetype QRegularExpressionMatch::capturedEnd(QStringView name) const { return capturedEnd(QAnyStringView(name)); } + +bool QRegularExpression::operator==(const QRegularExpression &other) const +{ + return comparesEqual(*this, other); +} #endif // QT_CONFIG(regularexpression) #include "qstring.h" // inlined API diff --git a/src/corelib/text/qregularexpression.cpp b/src/corelib/text/qregularexpression.cpp index 67c422568ba..78261e14cb3 100644 --- a/src/corelib/text/qregularexpression.cpp +++ b/src/corelib/text/qregularexpression.cpp @@ -42,6 +42,7 @@ using namespace Qt::StringLiterals; \keyword regular expression + \compares equality Regular expressions, or \e{regexps}, are a very powerful tool to handle strings and texts. This is useful in many contexts, e.g., @@ -1734,18 +1735,20 @@ void QRegularExpression::optimize() const } /*! - Returns \c true if the regular expression is equal to \a re, or false + \fn bool QRegularExpression::operator==(const QRegularExpression &lhs, const QRegularExpression &rhs) noexcept + + Returns \c true if the \a lhs regular expression is equal to the \a rhs, or false otherwise. Two QRegularExpression objects are equal if they have the same pattern string and the same pattern options. \sa operator!=() */ -bool QRegularExpression::operator==(const QRegularExpression &re) const +bool comparesEqual(const QRegularExpression &lhs, + const QRegularExpression &rhs) noexcept { - return (d == re.d) || - (d->pattern == re.d->pattern && d->patternOptions == re.d->patternOptions); + return (lhs.d == rhs.d) || + (lhs.d->pattern == rhs.d->pattern && lhs.d->patternOptions == rhs.d->patternOptions); } - /*! \fn QRegularExpression & QRegularExpression::operator=(QRegularExpression && re) @@ -1758,9 +1761,9 @@ bool QRegularExpression::operator==(const QRegularExpression &re) const */ /*! - \fn bool QRegularExpression::operator!=(const QRegularExpression &re) const + \fn bool QRegularExpression::operator!=(const QRegularExpression &lhs, const QRegularExpression &rhs) noexcept - Returns \c true if the regular expression is different from \a re, or + Returns \c true if the \a lhs regular expression is different from the \a rhs, or false otherwise. \sa operator==() diff --git a/src/corelib/text/qregularexpression.h b/src/corelib/text/qregularexpression.h index 2b51b94b153..ab147b87d45 100644 --- a/src/corelib/text/qregularexpression.h +++ b/src/corelib/text/qregularexpression.h @@ -157,11 +157,15 @@ public: static QRegularExpression fromWildcard(QStringView pattern, Qt::CaseSensitivity cs = Qt::CaseInsensitive, WildcardConversionOptions options = DefaultWildcardConversion); - +#if QT_CORE_REMOVED_SINCE(6, 8) bool operator==(const QRegularExpression &re) const; inline bool operator!=(const QRegularExpression &re) const { return !operator==(re); } - +#endif private: + friend Q_CORE_EXPORT bool comparesEqual(const QRegularExpression &lhs, + const QRegularExpression &rhs) noexcept; + Q_DECLARE_EQUALITY_COMPARABLE(QRegularExpression) + friend struct QRegularExpressionPrivate; friend class QRegularExpressionMatch; friend struct QRegularExpressionMatchPrivate; @@ -365,30 +369,24 @@ private: // [input.iterators] imposes operator== on us. Unfortunately, it's not // trivial to implement, so just do the bare minimum to satifisfy // Cpp17EqualityComparable. - friend bool operator==(const QRegularExpressionMatchIteratorRangeBasedForIterator &lhs, - const QRegularExpressionMatchIteratorRangeBasedForIterator &rhs) noexcept + friend bool comparesEqual(const QRegularExpressionMatchIteratorRangeBasedForIterator &lhs, + const QRegularExpressionMatchIteratorRangeBasedForIterator &rhs) + noexcept { return (&lhs == &rhs); } - - friend bool operator!=(const QRegularExpressionMatchIteratorRangeBasedForIterator &lhs, - const QRegularExpressionMatchIteratorRangeBasedForIterator &rhs) noexcept - { - return !(lhs == rhs); - } + Q_DECLARE_EQUALITY_COMPARABLE(QRegularExpressionMatchIteratorRangeBasedForIterator) // This is what we really use in a range-based for. - friend bool operator==(const QRegularExpressionMatchIteratorRangeBasedForIterator &lhs, - QRegularExpressionMatchIteratorRangeBasedForIteratorSentinel) noexcept + friend bool comparesEqual(const QRegularExpressionMatchIteratorRangeBasedForIterator &lhs, + const QRegularExpressionMatchIteratorRangeBasedForIteratorSentinel &rhs) + noexcept { + Q_UNUSED(rhs); return lhs.m_atEnd; } - - friend bool operator!=(const QRegularExpressionMatchIteratorRangeBasedForIterator &lhs, - QRegularExpressionMatchIteratorRangeBasedForIteratorSentinel) noexcept - { - return !lhs.m_atEnd; - } + Q_DECLARE_EQUALITY_COMPARABLE(QRegularExpressionMatchIteratorRangeBasedForIterator, + QRegularExpressionMatchIteratorRangeBasedForIteratorSentinel) QRegularExpressionMatchIterator m_matchIterator; QRegularExpressionMatch m_currentMatch; diff --git a/tests/auto/corelib/text/qregularexpression/CMakeLists.txt b/tests/auto/corelib/text/qregularexpression/CMakeLists.txt index b1d3ed0a8de..a7a7fe298f2 100644 --- a/tests/auto/corelib/text/qregularexpression/CMakeLists.txt +++ b/tests/auto/corelib/text/qregularexpression/CMakeLists.txt @@ -14,4 +14,6 @@ endif() qt_internal_add_test(tst_qregularexpression SOURCES tst_qregularexpression.cpp + LIBRARIES + Qt::TestPrivate ) diff --git a/tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp b/tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp index 72c49d2a9cd..d0ce4140953 100644 --- a/tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp +++ b/tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp @@ -3,6 +3,7 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include +#include #include #include #include @@ -27,6 +28,7 @@ public: static void initMain(); private slots: + void compareCompiles(); void defaultConstructors(); void moveSemantics(); void moveSemanticsMatch(); @@ -460,6 +462,11 @@ void tst_QRegularExpression::initMain() } } +void tst_QRegularExpression::compareCompiles() +{ + QTestPrivate::testEqualityOperatorsCompile(); +} + void tst_QRegularExpression::defaultConstructors() { QRegularExpression re; @@ -564,12 +571,12 @@ void tst_QRegularExpression::moveSemanticsMatchIterator() QRegularExpressionMatchIterator it1 = re.globalMatch("some test"); QVERIFY(it1.isValid()); QVERIFY(it1.hasNext()); - QCOMPARE(it1.regularExpression(), re); + QT_TEST_EQUALITY_OPS(it1.regularExpression(), re, true); QRegularExpressionMatchIterator it2(std::move(it1)); QVERIFY(it2.isValid()); QVERIFY(it2.hasNext()); - QCOMPARE(it2.regularExpression(), re); + QT_TEST_EQUALITY_OPS(it2.regularExpression(), re, true); consistencyCheck(it2); if (QTest::currentTestFailed()) return; @@ -578,13 +585,13 @@ void tst_QRegularExpression::moveSemanticsMatchIterator() QRegularExpressionMatchIterator it3 = re2.globalMatch("123test456"); QVERIFY(it3.isValid()); QVERIFY(it3.hasNext()); - QCOMPARE(it3.regularExpression(), re2); + QT_TEST_EQUALITY_OPS(it3.regularExpression(), re2, true); // check that (move)assigning to the moved-from object is ok it1 = std::move(it3); QVERIFY(it1.isValid()); QVERIFY(it1.hasNext()); - QCOMPARE(it1.regularExpression(), re2); + QT_TEST_EQUALITY_OPS(it1.regularExpression(), re2, true); consistencyCheck(it1); if (QTest::currentTestFailed()) return; @@ -1680,38 +1687,23 @@ void tst_QRegularExpression::serialize() static void verifyEquality(const QRegularExpression &re1, const QRegularExpression &re2) { - QVERIFY(re1 == re2); - QVERIFY(re2 == re1); + QT_TEST_EQUALITY_OPS(re1, re2, true); QCOMPARE(qHash(re1), qHash(re2)); - QVERIFY(!(re1 != re2)); - QVERIFY(!(re2 != re1)); QRegularExpression re3(re1); - QVERIFY(re1 == re3); - QVERIFY(re3 == re1); QCOMPARE(qHash(re1), qHash(re3)); - QVERIFY(!(re1 != re3)); - QVERIFY(!(re3 != re1)); + QT_TEST_EQUALITY_OPS(re1, re3, true); - QVERIFY(re2 == re3); - QVERIFY(re3 == re2); QCOMPARE(qHash(re2), qHash(re3)); - QVERIFY(!(re2 != re3)); - QVERIFY(!(re3 != re2)); + QT_TEST_EQUALITY_OPS(re2, re3, true); re3 = re2; - QVERIFY(re1 == re3); - QVERIFY(re3 == re1); QCOMPARE(qHash(re1), qHash(re3)); - QVERIFY(!(re1 != re3)); - QVERIFY(!(re3 != re1)); + QT_TEST_EQUALITY_OPS(re1, re3, true); - QVERIFY(re2 == re3); - QVERIFY(re3 == re2); QCOMPARE(qHash(re2), qHash(re3)); - QVERIFY(!(re2 != re3)); - QVERIFY(!(re3 != re2)); + QT_TEST_EQUALITY_OPS(re2, re3, true); } void tst_QRegularExpression::operatoreq_data()