diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2020-12-07 19:03:10 +0100 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2020-12-09 20:41:18 +0100 |
commit | 7c3208c97d2ad2c046908e144325c4ddb1e54876 (patch) | |
tree | af619067ae0f1bb7fc755e41c27359408d7c45ed /src | |
parent | 92c066b1e5c1986f6d779613b22e53f58f5236ac (diff) |
QString: fix count(QRegularExpression)
There is an off by one in the implementation of count(): a match
must be attempted even at the very end of the string, because
a 0-length match can happen there. While at it, improve
the documentation on the counter-intuitive behavior of count(),
which doesn't merely count how many times a regexp matches
into a string using ordinary global matching.
[ChangeLog][QtCore][QString] Fixed a corner case when using
QString::count(QRegularExpression), causing an empty in the
last position not to be accounted for in the returned result.
Change-Id: I064497839a96979abfbac2d0a96546ce160bbc46
Pick-to: 5.15 6.0
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/text/qstring.cpp | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 56d9e0a011..02501e00f4 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -4324,10 +4324,16 @@ bool QString::contains(const QRegularExpression &re, QRegularExpressionMatch *rm Returns the number of times the regular expression \a re matches in the string. - This function counts overlapping matches, so in the example - below, there are four instances of "ana" or "ama": + For historical reasons, this function counts overlapping matches, + so in the example below, there are four instances of "ana" or + "ama": \snippet qstring/main.cpp 95 + + This behavior is different from simply iterating over the matches + in the string using QRegularExpressionMatchIterator. + + \sa QRegularExpression::globalMatch() */ qsizetype QString::count(const QRegularExpression &re) const { @@ -4338,7 +4344,7 @@ qsizetype QString::count(const QRegularExpression &re) const qsizetype count = 0; qsizetype index = -1; qsizetype len = length(); - while (index < len - 1) { + while (index <= len - 1) { QRegularExpressionMatch match = re.match(*this, index + 1); if (!match.hasMatch()) break; |