diff options
author | Ivan Solovev <[email protected]> | 2021-01-20 11:42:14 +0100 |
---|---|---|
committer | Ivan Solovev <[email protected]> | 2021-01-26 17:20:01 +0100 |
commit | fe7cbf8fe671cfe7a39ddaf8a963f9e7e3cd79ef (patch) | |
tree | a584e0f9c30c3facb300dddc0785a7cf2e40913d | |
parent | 114d5c045da872be8cd304949c86b9e9655af2bf (diff) |
QRegularExpression: add move constructor
- Add move constructors to QRegularExpression, QRegularExpressionMatch
and QRegularExpressionMatchIterator.
- Update the documentation to explicitly state that only destructor
and assignment operators can be called for a moved-from object
Task-number: QTBUG-86634
Change-Id: I06b4f54e300541033a9a18339c97338717a06da0
Reviewed-by: Edward Welbourne <[email protected]>
Reviewed-by: Alex Blasche <[email protected]>
-rw-r--r-- | src/corelib/text/qregularexpression.cpp | 73 | ||||
-rw-r--r-- | src/corelib/text/qregularexpression.h | 8 | ||||
-rw-r--r-- | tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp | 115 |
3 files changed, 190 insertions, 6 deletions
diff --git a/src/corelib/text/qregularexpression.cpp b/src/corelib/text/qregularexpression.cpp index 1127b778310..72079c19964 100644 --- a/src/corelib/text/qregularexpression.cpp +++ b/src/corelib/text/qregularexpression.cpp @@ -2,7 +2,7 @@ ** ** Copyright (C) 2020 Giuseppe D'Angelo <[email protected]>. ** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected], author Giuseppe D'Angelo <[email protected]> -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -1371,12 +1371,28 @@ QRegularExpression::QRegularExpression(const QRegularExpression &re) } /*! + \fn QRegularExpression::QRegularExpression(QRegularExpression &&re) + + \since 6.1 + + Constructs a QRegularExpression object by moving from \a re. + + Note that a moved-from QRegularExpression can only be destroyed or + assigned to. The effect of calling other functions than the destructor + or one of the assignment operators is undefined. + + \sa operator=() +*/ + +/*! Destroys the QRegularExpression object. */ QRegularExpression::~QRegularExpression() { } +QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QRegularExpressionPrivate) + /*! Assigns the regular expression \a re to this object, and returns a reference to the copy. Both the pattern and the pattern options are copied. @@ -1723,8 +1739,12 @@ bool QRegularExpression::operator==(const QRegularExpression &re) const /*! \fn QRegularExpression & QRegularExpression::operator=(QRegularExpression && re) - Move-assigns the regular expression \a re to this object, and returns a reference - to the copy. Both the pattern and the pattern options are copied. + Move-assigns the regular expression \a re to this object, and returns a + reference to the result. Both the pattern and the pattern options are copied. + + Note that a moved-from QRegularExpression can only be destroyed or + assigned to. The effect of calling other functions than the destructor + or one of the assignment operators is undefined. */ /*! @@ -2036,6 +2056,8 @@ QRegularExpressionMatch::~QRegularExpressionMatch() { } +QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QRegularExpressionMatchPrivate) + /*! Constructs a match result by copying the result of the given \a match. @@ -2047,6 +2069,20 @@ QRegularExpressionMatch::QRegularExpressionMatch(const QRegularExpressionMatch & } /*! + \fn QRegularExpressionMatch::QRegularExpressionMatch(QRegularExpressionMatch &&match) + + \since 6.1 + + Constructs a match result by moving the result from the given \a match. + + Note that a moved-from QRegularExpressionMatch can only be destroyed or + assigned to. The effect of calling other functions than the destructor + or one of the assignment operators is undefined. + + \sa operator=() +*/ + +/*! Assigns the match result \a match to this object, and returns a reference to the copy. */ @@ -2059,8 +2095,12 @@ QRegularExpressionMatch &QRegularExpressionMatch::operator=(const QRegularExpres /*! \fn QRegularExpressionMatch &QRegularExpressionMatch::operator=(QRegularExpressionMatch &&match) - Move-assigns the match result \a match to this object, and returns a reference - to the copy. + Move-assigns the match result \a match to this object, and returns a + reference to the result. + + Note that a moved-from QRegularExpressionMatch can only be destroyed or + assigned to. The effect of calling other functions than the destructor + or one of the assignment operators is undefined. */ /*! @@ -2468,6 +2508,8 @@ QRegularExpressionMatchIterator::~QRegularExpressionMatchIterator() { } +QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QRegularExpressionMatchIteratorPrivate) + /*! Constructs a QRegularExpressionMatchIterator object as a copy of \a iterator. @@ -2480,6 +2522,20 @@ QRegularExpressionMatchIterator::QRegularExpressionMatchIterator(const QRegularE } /*! + \fn QRegularExpressionMatchIterator::QRegularExpressionMatchIterator(QRegularExpressionMatchIterator &&iterator) + + \since 6.1 + + Constructs a QRegularExpressionMatchIterator object by moving from \a iterator. + + Note that a moved-from QRegularExpressionMatchIterator can only be destroyed + or assigned to. The effect of calling other functions than the destructor + or one of the assignment operators is undefined. + + \sa operator=() +*/ + +/*! Assigns the iterator \a iterator to this object, and returns a reference to the copy. */ @@ -2492,7 +2548,12 @@ QRegularExpressionMatchIterator &QRegularExpressionMatchIterator::operator=(cons /*! \fn QRegularExpressionMatchIterator &QRegularExpressionMatchIterator::operator=(QRegularExpressionMatchIterator &&iterator) - Move-assigns the \a iterator to this object. + Move-assigns the \a iterator to this object, and returns a reference to the + result. + + Note that a moved-from QRegularExpressionMatchIterator can only be destroyed + or assigned to. The effect of calling other functions than the destructor + or one of the assignment operators is undefined. */ /*! diff --git a/src/corelib/text/qregularexpression.h b/src/corelib/text/qregularexpression.h index 142ec7f8389..570679800c6 100644 --- a/src/corelib/text/qregularexpression.h +++ b/src/corelib/text/qregularexpression.h @@ -2,6 +2,7 @@ ** ** Copyright (C) 2020 Giuseppe D'Angelo <[email protected]>. ** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected], author Giuseppe D'Angelo <[email protected]> +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -60,6 +61,8 @@ class QRegularExpressionMatchIterator; struct QRegularExpressionPrivate; class QRegularExpression; +QT_DECLARE_QESDP_SPECIALIZATION_DTOR_WITH_EXPORT(QRegularExpressionPrivate, Q_CORE_EXPORT) + Q_CORE_EXPORT size_t qHash(const QRegularExpression &key, size_t seed = 0) noexcept; class Q_CORE_EXPORT QRegularExpression @@ -86,6 +89,7 @@ public: QRegularExpression(); explicit QRegularExpression(const QString &pattern, PatternOptions options = NoPatternOption); QRegularExpression(const QRegularExpression &re); + QRegularExpression(QRegularExpression &&re) = default; ~QRegularExpression(); QRegularExpression &operator=(const QRegularExpression &re); QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QRegularExpression) @@ -204,6 +208,7 @@ Q_CORE_EXPORT QDebug operator<<(QDebug debug, QRegularExpression::PatternOptions #endif struct QRegularExpressionMatchPrivate; +QT_DECLARE_QESDP_SPECIALIZATION_DTOR_WITH_EXPORT(QRegularExpressionMatchPrivate, Q_CORE_EXPORT) class Q_CORE_EXPORT QRegularExpressionMatch { @@ -211,6 +216,7 @@ public: QRegularExpressionMatch(); ~QRegularExpressionMatch(); QRegularExpressionMatch(const QRegularExpressionMatch &match); + QRegularExpressionMatch(QRegularExpressionMatch &&match) = default; QRegularExpressionMatch &operator=(const QRegularExpressionMatch &match); QRegularExpressionMatch &operator=(QRegularExpressionMatch &&match) noexcept { d.swap(match.d); return *this; } @@ -278,6 +284,7 @@ class QRegularExpressionMatchIteratorRangeBasedForIteratorSentinel {}; } struct QRegularExpressionMatchIteratorPrivate; +QT_DECLARE_QESDP_SPECIALIZATION_DTOR_WITH_EXPORT(QRegularExpressionMatchIteratorPrivate, Q_CORE_EXPORT) class Q_CORE_EXPORT QRegularExpressionMatchIterator { @@ -285,6 +292,7 @@ public: QRegularExpressionMatchIterator(); ~QRegularExpressionMatchIterator(); QRegularExpressionMatchIterator(const QRegularExpressionMatchIterator &iterator); + QRegularExpressionMatchIterator(QRegularExpressionMatchIterator &&iterator) = default; QRegularExpressionMatchIterator &operator=(const QRegularExpressionMatchIterator &iterator); QRegularExpressionMatchIterator &operator=(QRegularExpressionMatchIterator &&iterator) noexcept { d.swap(iterator.d); return *this; } diff --git a/tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp b/tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp index 7570feb4de6..f2fe382521f 100644 --- a/tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp +++ b/tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp @@ -47,6 +47,9 @@ class tst_QRegularExpression : public QObject private slots: void defaultConstructors(); + void moveSemantics(); + void moveSemanticsMatch(); + void moveSemanticsMatchIterator(); void gettersSetters_data(); void gettersSetters(); void escape_data(); @@ -274,7 +277,11 @@ void consistencyCheck(const QRegularExpressionMatchIterator &iterator) QRegularExpressionMatch peeked = i.peekNext(); QRegularExpressionMatch match = i.next(); consistencyCheck(peeked); + if (QTest::currentTestFailed()) + return; consistencyCheck(match); + if (QTest::currentTestFailed()) + return; QVERIFY(match.isValid()); QVERIFY(match.hasMatch() || match.hasPartialMatch()); QCOMPARE(i.regularExpression(), match.regularExpression()); @@ -434,6 +441,10 @@ void tst_QRegularExpression::defaultConstructors() QRegularExpression re; QCOMPARE(re.pattern(), QString()); QCOMPARE(re.patternOptions(), QRegularExpression::NoPatternOption); + QCOMPARE(re.isValid(), true); + QCOMPARE(re.patternErrorOffset(), -1); + QCOMPARE(re.captureCount(), 0); + QCOMPARE(re.namedCaptureGroups(), QStringList { QString() }); QRegularExpressionMatch match; QCOMPARE(match.regularExpression(), QRegularExpression()); @@ -444,6 +455,15 @@ void tst_QRegularExpression::defaultConstructors() QCOMPARE(match.hasPartialMatch(), false); QCOMPARE(match.isValid(), true); QCOMPARE(match.lastCapturedIndex(), -1); + QCOMPARE(match.captured(), QString()); + QCOMPARE(match.captured("test"), QString()); + QCOMPARE(match.capturedTexts(), QStringList()); + QCOMPARE(match.capturedStart(), -1); + QCOMPARE(match.capturedEnd(), -1); + QCOMPARE(match.capturedLength(), 0); + QCOMPARE(match.capturedStart("test"), -1); + QCOMPARE(match.capturedEnd("test"), -1); + QCOMPARE(match.capturedLength("test"), 0); QRegularExpressionMatchIterator iterator; QCOMPARE(iterator.regularExpression(), QRegularExpression()); @@ -454,6 +474,101 @@ void tst_QRegularExpression::defaultConstructors() QCOMPARE(iterator.hasNext(), false); } +void tst_QRegularExpression::moveSemantics() +{ + const QString pattern = "pattern"; + const QRegularExpression::PatternOptions options = QRegularExpression::CaseInsensitiveOption; + QRegularExpression expr1(pattern, options); + QCOMPARE(expr1.pattern(), pattern); + QCOMPARE(expr1.patternOptions(), options); + + QRegularExpression expr2(std::move(expr1)); + QCOMPARE(expr2.pattern(), pattern); + QCOMPARE(expr2.patternOptions(), options); + + const QString pattern2 = "pattern2"; + QRegularExpression expr3(pattern2); + QCOMPARE(expr3.pattern(), pattern2); + QCOMPARE(expr3.patternOptions(), QRegularExpression::NoPatternOption); + + // check that (move)assigning to the moved-from object is ok + expr1 = std::move(expr3); + QCOMPARE(expr1.pattern(), pattern2); + QCOMPARE(expr1.patternOptions(), QRegularExpression::NoPatternOption); + + // here expr3 is in the moved-from state, so destructor call for moved-from + // object is also checked +} + +void tst_QRegularExpression::moveSemanticsMatch() +{ + QRegularExpression re("test"); + QRegularExpressionMatch match1 = re.match("abctestdef"); + QCOMPARE(match1.hasMatch(), true); + QCOMPARE(match1.capturedStart(), 3); + QCOMPARE(match1.capturedEnd(), 7); + + QRegularExpressionMatch match2(std::move(match1)); + QCOMPARE(match2.hasMatch(), true); + QCOMPARE(match2.capturedStart(), 3); + QCOMPARE(match2.capturedEnd(), 7); + consistencyCheck(match2); + if (QTest::currentTestFailed()) + return; + + QRegularExpressionMatch match3 = re.match("test1"); + QCOMPARE(match3.hasMatch(), true); + QCOMPARE(match3.capturedStart(), 0); + QCOMPARE(match3.capturedEnd(), 4); + + // check that (move)assigning to the moved-from object is ok + match1 = std::move(match3); + QCOMPARE(match1.hasMatch(), true); + QCOMPARE(match1.capturedStart(), 0); + QCOMPARE(match1.capturedEnd(), 4); + consistencyCheck(match1); + if (QTest::currentTestFailed()) + return; + + // here match3 is in the moved-from state, so destructor call for moved-from + // object is also checked +} + +void tst_QRegularExpression::moveSemanticsMatchIterator() +{ + QRegularExpression re("(\\w+)"); + QRegularExpressionMatchIterator it1 = re.globalMatch("some test"); + QVERIFY(it1.isValid()); + QVERIFY(it1.hasNext()); + QCOMPARE(it1.regularExpression(), re); + + QRegularExpressionMatchIterator it2(std::move(it1)); + QVERIFY(it2.isValid()); + QVERIFY(it2.hasNext()); + QCOMPARE(it2.regularExpression(), re); + consistencyCheck(it2); + if (QTest::currentTestFailed()) + return; + + QRegularExpression re2("test"); + QRegularExpressionMatchIterator it3 = re2.globalMatch("123test456"); + QVERIFY(it3.isValid()); + QVERIFY(it3.hasNext()); + QCOMPARE(it3.regularExpression(), re2); + + // 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); + consistencyCheck(it1); + if (QTest::currentTestFailed()) + return; + + // here it3 is in the moved-from state, so destructor call for moved-from + // object is also checked +} + void tst_QRegularExpression::gettersSetters_data() { provideRegularExpressions(); |