summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qstring.cpp
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-12-02 12:31:41 +0100
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-12-10 02:15:18 +0100
commitce0b76731042412a0fa5a5f633fc48a52f79ef81 (patch)
treeb0e697beba8c24158a6312341417b82b6a561d2c /src/corelib/text/qstring.cpp
parentbe83ff65c424cff1036e7da19d6175826d9f7ed9 (diff)
QStringView: add some QRegularExpression-related overloads
[ChangeLog][QtCore][QStringView] Added the indexOf(), contains(), lastIndexOf() and count() methods taking a QRegularExpression. Fixes: QTBUG-89050 Change-Id: Ic726754f67e06b3764302d2fad252e0378a77afc Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/text/qstring.cpp')
-rw-r--r--src/corelib/text/qstring.cpp78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp
index 92eed93251..07b8c8512b 100644
--- a/src/corelib/text/qstring.cpp
+++ b/src/corelib/text/qstring.cpp
@@ -10259,6 +10259,84 @@ qsizetype QtPrivate::lastIndexOf(QLatin1String haystack, qsizetype from, QLatin1
return qLastIndexOf(haystack, from, needle, cs);
}
+#if QT_CONFIG(regularexpression)
+qsizetype QtPrivate::indexOf(QStringView haystack, const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch)
+{
+ if (!re.isValid()) {
+ qWarning("QStringView::indexOf: invalid QRegularExpression object");
+ return -1;
+ }
+
+ QRegularExpressionMatch match = re.match(haystack, from);
+ if (match.hasMatch()) {
+ const qsizetype ret = match.capturedStart();
+ if (rmatch)
+ *rmatch = std::move(match);
+ return ret;
+ }
+
+ return -1;
+}
+
+qsizetype QtPrivate::lastIndexOf(QStringView haystack, const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch)
+{
+ if (!re.isValid()) {
+ qWarning("QStringView::lastIndexOf: invalid QRegularExpression object");
+ return -1;
+ }
+
+ qsizetype endpos = (from < 0) ? (haystack.size() + from + 1) : (from);
+ QRegularExpressionMatchIterator iterator = re.globalMatch(haystack);
+ 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;
+}
+
+bool QtPrivate::contains(QStringView haystack, const QRegularExpression &re, QRegularExpressionMatch *rmatch)
+{
+ if (!re.isValid()) {
+ qWarning("QStringView::contains: invalid QRegularExpression object");
+ return false;
+ }
+ QRegularExpressionMatch m = re.match(haystack);
+ bool hasMatch = m.hasMatch();
+ if (hasMatch && rmatch)
+ *rmatch = std::move(m);
+ return hasMatch;
+}
+
+qsizetype QtPrivate::count(QStringView haystack, const QRegularExpression &re)
+{
+ if (!re.isValid()) {
+ qWarning("QStringView::count: invalid QRegularExpression object");
+ return 0;
+ }
+ qsizetype count = 0;
+ qsizetype index = -1;
+ qsizetype len = haystack.length();
+ while (index <= len - 1) {
+ QRegularExpressionMatch match = re.match(haystack, index + 1);
+ if (!match.hasMatch())
+ break;
+ index = match.capturedStart();
+ count++;
+ }
+ return count;
+}
+
+#endif // QT_CONFIG(regularexpression)
+
/*!
\since 5.0