diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2021-06-08 17:48:16 +0200 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2021-08-19 08:49:07 +0200 |
commit | 11d1dcc6e263c5059f34b44d531c9ccdf7c0b1d6 (patch) | |
tree | cd32196502def427e8ddfdef417574d17b10490e /src/corelib | |
parent | f0d1f50e0294e5a55a0e450993e0810bd4dbf63d (diff) |
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 <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/text/qstring.cpp | 71 |
1 files changed, 8 insertions, 63 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index f929d20c9e..0ca5c7a2cb 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; |