summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qstring.cpp
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2022-07-05 19:01:08 +0200
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2022-07-29 23:04:27 +0200
commite540d4a8647c41a3e710a555f5dcc44edb7dfcb4 (patch)
tree25c6441166d1647b95e8983ac48a2204e3390fe4 /src/corelib/text/qstring.cpp
parent8d6b274fa4c65e87443c0fbf8425229230385405 (diff)
QRegularExpression: introduce (global)matchView
QRegularExpression::match (and globalMatch) is currently overloaded for QString and QStringView. This creates a subtle API asymmetry: QRegularExpression re; auto m1 = re.match(getQString()); // OK auto m2 = re.match(getStdU16String()); // Dangling This goes against our decision that every time that there's a possible lifetime issue at play, it should be "evident". Solving the lifetime issue here is possible, but tricky -- since QRegularExpression is out-of-line, one needs a type-erased container for the input string (basically, std::any) to keep it alive and so on. Instead I went for the simpler solution: deprecate match(QStringView) and introduce matchView(QStringView) (same for globalMatch). This makes it clear that the call is matching over a view and therefore users are supposed to keep the source object alive. Drive-by, remove the documentation that says that the QString overloads might not keep the string alive: they do and forever will. [ChangeLog][QtCore][QRegularExpression] Added the matchView() and globalMatchView() functions that operate on string views. The match(QStringView) and globalMatch(QStringView) overloads have been deprecated. Change-Id: I054b8605c2fdea59b556dcfea8920ef4eee78ee9 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/text/qstring.cpp')
-rw-r--r--src/corelib/text/qstring.cpp25
1 files changed, 17 insertions, 8 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp
index 5fbc3c6a48..f52f7317b9 100644
--- a/src/corelib/text/qstring.cpp
+++ b/src/corelib/text/qstring.cpp
@@ -7690,8 +7690,9 @@ QList<QStringView> QStringView::split(QChar sep, Qt::SplitBehavior behavior, Qt:
#if QT_CONFIG(regularexpression)
namespace {
-template<class ResultList, typename String>
+template<class ResultList, typename String, typename MatchingFunction>
static ResultList splitString(const String &source, const QRegularExpression &re,
+ MatchingFunction matchingFunction,
Qt::SplitBehavior behavior)
{
ResultList list;
@@ -7702,7 +7703,7 @@ static ResultList splitString(const String &source, const QRegularExpression &re
qsizetype start = 0;
qsizetype end = 0;
- QRegularExpressionMatchIterator iterator = re.globalMatch(source);
+ QRegularExpressionMatchIterator iterator = (re.*matchingFunction)(source, 0, QRegularExpression::NormalMatch, QRegularExpression::NoMatchOption);
while (iterator.hasNext()) {
QRegularExpressionMatch match = iterator.next();
end = match.capturedStart();
@@ -7747,7 +7748,15 @@ static ResultList splitString(const String &source, const QRegularExpression &re
*/
QStringList QString::split(const QRegularExpression &re, Qt::SplitBehavior behavior) const
{
- return splitString<QStringList>(*this, re, behavior);
+#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
+ const auto matchingFunction = qOverload<const QString &, qsizetype, QRegularExpression::MatchType, QRegularExpression::MatchOptions>(&QRegularExpression::globalMatch);
+#else
+ const auto matchingFunction = &QRegularExpression::globalMatch;
+#endif
+ return splitString<QStringList>(*this,
+ re,
+ matchingFunction,
+ behavior);
}
/*!
@@ -7764,7 +7773,7 @@ QStringList QString::split(const QRegularExpression &re, Qt::SplitBehavior behav
*/
QList<QStringView> QStringView::split(const QRegularExpression &re, Qt::SplitBehavior behavior) const
{
- return splitString<QList<QStringView>>(*this, re, behavior);
+ return splitString<QList<QStringView>>(*this, re, &QRegularExpression::globalMatchView, behavior);
}
#endif // QT_CONFIG(regularexpression)
@@ -10793,7 +10802,7 @@ qsizetype QtPrivate::indexOf(QStringView viewHaystack, const QString *stringHays
QRegularExpressionMatch match = stringHaystack
? re.match(*stringHaystack, from)
- : re.match(viewHaystack, from);
+ : re.matchView(viewHaystack, from);
if (match.hasMatch()) {
const qsizetype ret = match.capturedStart();
if (rmatch)
@@ -10819,7 +10828,7 @@ qsizetype QtPrivate::lastIndexOf(QStringView viewHaystack, const QString *string
qsizetype endpos = (from < 0) ? (viewHaystack.size() + from + 1) : (from + 1);
QRegularExpressionMatchIterator iterator = stringHaystack
? re.globalMatch(*stringHaystack)
- : re.globalMatch(viewHaystack);
+ : re.globalMatchView(viewHaystack);
qsizetype lastIndex = -1;
while (iterator.hasNext()) {
QRegularExpressionMatch match = iterator.next();
@@ -10849,7 +10858,7 @@ bool QtPrivate::contains(QStringView viewHaystack, const QString *stringHaystack
}
QRegularExpressionMatch m = stringHaystack
? re.match(*stringHaystack)
- : re.match(viewHaystack);
+ : re.matchView(viewHaystack);
bool hasMatch = m.hasMatch();
if (hasMatch && rmatch)
*rmatch = std::move(m);
@@ -10871,7 +10880,7 @@ qsizetype QtPrivate::count(QStringView haystack, const QRegularExpression &re)
qsizetype index = -1;
qsizetype len = haystack.length();
while (index <= len - 1) {
- QRegularExpressionMatch match = re.match(haystack, index + 1);
+ QRegularExpressionMatch match = re.matchView(haystack, index + 1);
if (!match.hasMatch())
break;
index = match.capturedStart();