From 11d1dcc6e263c5059f34b44d531c9ccdf7c0b1d6 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Tue, 8 Jun 2021 17:48:16 +0200 Subject: [PATCH] QString: use the QRegularExpression operations on QStringView There's no need of duplicating code all over the place; QString can reuse the implementation of the indexOf/contains/count/lastIndexOf family of functions already existing for QStringView. For simplicity, the warning messages (that our autotests actually check) have been made more generic, rather than introducing some other parameter (as in, "which class is using this functionality so to emit a more precise warning"), which would have just complicated things as the implementation of these functions is exported and used by inline QStringView member functions. Change-Id: I85cd94a31c82b00d61341b3058b954749a2d6c6b Reviewed-by: Thiago Macieira --- src/corelib/text/qstring.cpp | 71 +++---------------- .../auto/corelib/text/qstring/tst_qstring.cpp | 20 +++--- 2 files changed, 18 insertions(+), 73 deletions(-) diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index f929d20c9ef..0ca5c7a2cba 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -4408,20 +4408,7 @@ qsizetype QString::count(QStringView str, Qt::CaseSensitivity cs) const */ qsizetype QString::indexOf(const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch) const { - if (!re.isValid()) { - qWarning("QString::indexOf: invalid QRegularExpression object"); - return -1; - } - - QRegularExpressionMatch match = re.match(*this, from); - if (match.hasMatch()) { - const qsizetype ret = match.capturedStart(); - if (rmatch) - *rmatch = std::move(match); - return ret; - } - - return -1; + return QtPrivate::indexOf(QStringView(*this), re, from, rmatch); } /*! @@ -4455,27 +4442,7 @@ qsizetype QString::indexOf(const QRegularExpression &re, qsizetype from, QRegula */ qsizetype QString::lastIndexOf(const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch) const { - if (!re.isValid()) { - qWarning("QString::lastIndexOf: invalid QRegularExpression object"); - return -1; - } - - qsizetype endpos = (from < 0) ? (size() + from + 1) : (from + 1); - QRegularExpressionMatchIterator iterator = re.globalMatch(*this); - qsizetype lastIndex = -1; - while (iterator.hasNext()) { - QRegularExpressionMatch match = iterator.next(); - qsizetype start = match.capturedStart(); - if (start < endpos) { - lastIndex = start; - if (rmatch) - *rmatch = std::move(match); - } else { - break; - } - } - - return lastIndex; + return QtPrivate::lastIndexOf(QStringView(*this), re, from, rmatch); } /*! @@ -4514,15 +4481,7 @@ qsizetype QString::lastIndexOf(const QRegularExpression &re, qsizetype from, QRe bool QString::contains(const QRegularExpression &re, QRegularExpressionMatch *rmatch) const { - if (!re.isValid()) { - qWarning("QString::contains: invalid QRegularExpression object"); - return false; - } - QRegularExpressionMatch m = re.match(*this); - bool hasMatch = m.hasMatch(); - if (hasMatch && rmatch) - *rmatch = std::move(m); - return hasMatch; + return QtPrivate::contains(QStringView(*this), re, rmatch); } /*! @@ -4545,21 +4504,7 @@ bool QString::contains(const QRegularExpression &re, QRegularExpressionMatch *rm */ qsizetype QString::count(const QRegularExpression &re) const { - if (!re.isValid()) { - qWarning("QString::count: invalid QRegularExpression object"); - return 0; - } - qsizetype count = 0; - qsizetype index = -1; - qsizetype len = length(); - while (index <= len - 1) { - QRegularExpressionMatch match = re.match(*this, index + 1); - if (!match.hasMatch()) - break; - index = match.capturedStart(); - count++; - } - return count; + return QtPrivate::count(QStringView(*this), re); } #endif // QT_CONFIG(regularexpression) @@ -10601,7 +10546,7 @@ qsizetype QtPrivate::lastIndexOf(QLatin1String haystack, qsizetype from, QLatin1 qsizetype QtPrivate::indexOf(QStringView haystack, const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch) { if (!re.isValid()) { - qWarning("QStringView::indexOf: invalid QRegularExpression object"); + qWarning("QString(View)::indexOf: invalid QRegularExpression object"); return -1; } @@ -10619,7 +10564,7 @@ qsizetype QtPrivate::indexOf(QStringView haystack, const QRegularExpression &re, qsizetype QtPrivate::lastIndexOf(QStringView haystack, const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch) { if (!re.isValid()) { - qWarning("QStringView::lastIndexOf: invalid QRegularExpression object"); + qWarning("QString(View)::lastIndexOf: invalid QRegularExpression object"); return -1; } @@ -10644,7 +10589,7 @@ qsizetype QtPrivate::lastIndexOf(QStringView haystack, const QRegularExpression bool QtPrivate::contains(QStringView haystack, const QRegularExpression &re, QRegularExpressionMatch *rmatch) { if (!re.isValid()) { - qWarning("QStringView::contains: invalid QRegularExpression object"); + qWarning("QString(View)::contains: invalid QRegularExpression object"); return false; } QRegularExpressionMatch m = re.match(haystack); @@ -10657,7 +10602,7 @@ bool QtPrivate::contains(QStringView haystack, const QRegularExpression &re, QRe qsizetype QtPrivate::count(QStringView haystack, const QRegularExpression &re) { if (!re.isValid()) { - qWarning("QStringView::count: invalid QRegularExpression object"); + qWarning("QString(View)::count: invalid QRegularExpression object"); return 0; } qsizetype count = 0; diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp index 813e2470134..7ad218be96b 100644 --- a/tests/auto/corelib/text/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp @@ -1687,14 +1687,14 @@ void tst_QString::indexOf2() #if QT_CONFIG(regularexpression) void tst_QString::indexOfInvalidRegex() { - QTest::ignoreMessage(QtWarningMsg, "QString::indexOf: invalid QRegularExpression object"); + QTest::ignoreMessage(QtWarningMsg, "QString(View)::indexOf: invalid QRegularExpression object"); QCOMPARE(QString("invalid regex\\").indexOf(QRegularExpression("invalid regex\\")), -1); - QTest::ignoreMessage(QtWarningMsg, "QString::indexOf: invalid QRegularExpression object"); + QTest::ignoreMessage(QtWarningMsg, "QString(View)::indexOf: invalid QRegularExpression object"); QCOMPARE(QString("invalid regex\\").indexOf(QRegularExpression("invalid regex\\"), -1, nullptr), -1); QRegularExpressionMatch match; QVERIFY(!match.hasMatch()); - QTest::ignoreMessage(QtWarningMsg, "QString::indexOf: invalid QRegularExpression object"); + QTest::ignoreMessage(QtWarningMsg, "QString(View)::indexOf: invalid QRegularExpression object"); QCOMPARE(QString("invalid regex\\").indexOf(QRegularExpression("invalid regex\\"), -1, &match), -1); QVERIFY(!match.hasMatch()); } @@ -1813,14 +1813,14 @@ void tst_QString::lastIndexOf() #if QT_CONFIG(regularexpression) void tst_QString::lastIndexOfInvalidRegex() { - QTest::ignoreMessage(QtWarningMsg, "QString::lastIndexOf: invalid QRegularExpression object"); + QTest::ignoreMessage(QtWarningMsg, "QString(View)::lastIndexOf: invalid QRegularExpression object"); QCOMPARE(QString("invalid regex\\").lastIndexOf(QRegularExpression("invalid regex\\"), 0), -1); - QTest::ignoreMessage(QtWarningMsg, "QString::lastIndexOf: invalid QRegularExpression object"); + QTest::ignoreMessage(QtWarningMsg, "QString(View)::lastIndexOf: invalid QRegularExpression object"); QCOMPARE(QString("invalid regex\\").lastIndexOf(QRegularExpression("invalid regex\\"), -1, nullptr), -1); QRegularExpressionMatch match; QVERIFY(!match.hasMatch()); - QTest::ignoreMessage(QtWarningMsg, "QString::lastIndexOf: invalid QRegularExpression object"); + QTest::ignoreMessage(QtWarningMsg, "QString(View)::lastIndexOf: invalid QRegularExpression object"); QCOMPARE(QString("invalid regex\\").lastIndexOf(QRegularExpression("invalid regex\\"), -1, &match), -1); QVERIFY(!match.hasMatch()); } @@ -1843,7 +1843,7 @@ void tst_QString::count() QCOMPARE(a.count(QRegularExpression("")), 16); QCOMPARE(a.count(QRegularExpression("[FG][HI]")), 1); QCOMPARE(a.count(QRegularExpression("[G][HE]")), 2); - QTest::ignoreMessage(QtWarningMsg, "QString::count: invalid QRegularExpression object"); + QTest::ignoreMessage(QtWarningMsg, "QString(View)::count: invalid QRegularExpression object"); QCOMPARE(a.count(QRegularExpression("invalid regex\\")), 0); #endif @@ -1862,7 +1862,7 @@ void tst_QString::count() #if QT_CONFIG(regularexpression) QCOMPARE(nullStr.count(QRegularExpression("")), 1); QCOMPARE(nullStr.count(QRegularExpression("[FG][HI]")), 0); - QTest::ignoreMessage(QtWarningMsg, "QString::count: invalid QRegularExpression object"); + QTest::ignoreMessage(QtWarningMsg, "QString(View)::count: invalid QRegularExpression object"); QCOMPARE(nullStr.count(QRegularExpression("invalid regex\\")), 0); #endif @@ -1876,7 +1876,7 @@ void tst_QString::count() #if QT_CONFIG(regularexpression) QCOMPARE(emptyStr.count(QRegularExpression("")), 1); QCOMPARE(emptyStr.count(QRegularExpression("[FG][HI]")), 0); - QTest::ignoreMessage(QtWarningMsg, "QString::count: invalid QRegularExpression object"); + QTest::ignoreMessage(QtWarningMsg, "QString(View)::count: invalid QRegularExpression object"); QCOMPARE(emptyStr.count(QRegularExpression("invalid regex\\")), 0); #endif } @@ -1949,7 +1949,7 @@ void tst_QString::contains() QVERIFY(!a.contains(QRegularExpression("ZZZ"), 0)); } - QTest::ignoreMessage(QtWarningMsg, "QString::contains: invalid QRegularExpression object"); + QTest::ignoreMessage(QtWarningMsg, "QString(View)::contains: invalid QRegularExpression object"); QVERIFY(!a.contains(QRegularExpression("invalid regex\\"))); #endif