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 <ivan.solovev@qt.io>
This commit is contained in:
Tatiana Borisova 2024-04-19 15:20:05 +02:00
parent a074dd5616
commit 7b738ffc58
5 changed files with 49 additions and 49 deletions

View File

@ -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

View File

@ -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==()

View File

@ -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;

View File

@ -14,4 +14,6 @@ endif()
qt_internal_add_test(tst_qregularexpression
SOURCES
tst_qregularexpression.cpp
LIBRARIES
Qt::TestPrivate
)

View File

@ -3,6 +3,7 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
#include <QtTest/private/qcomparisontesthelper_p.h>
#include <qstring.h>
#include <qlist.h>
#include <qstringlist.h>
@ -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<QRegularExpression>();
}
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()