Fix QByteArray::count implementation for longer data

The issue has been introduced when refactoring QByteArray::count
implementation (see 631127126cc14e7c01cc611532b3256b58785670).

Because the last argument of QByteArrayMatcher::indexIn() method
has a defult value, it has been compiling without issues, but was being
called with incorrect size parameter. Fixed it to pass the length of the
input data correctly.

Change-Id: Ic9c2f33733131ec17276aa889f2d7ea40ec79b01
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
This commit is contained in:
Sona Kurazyan 2020-07-09 18:16:13 +02:00
parent c023b025ee
commit 577558daf5
2 changed files with 9 additions and 2 deletions

View File

@ -2523,8 +2523,8 @@ qsizetype QtPrivate::count(QByteArrayView haystack, QByteArrayView needle) noexc
qsizetype num = 0;
qsizetype i = -1;
if (haystack.size() > 500 && needle.size() > 5) {
QByteArrayMatcher matcher(needle.data());
while ((i = matcher.indexIn(haystack.data(), i + 1)) != -1)
QByteArrayMatcher matcher(needle.data(), needle.size());
while ((i = matcher.indexIn(haystack.data(), haystack.size(), i + 1)) != -1)
++num;
} else {
while ((i = haystack.indexOf(needle, i + 1)) != -1)

View File

@ -490,6 +490,13 @@ void tst_QByteArrayApiSymmetry::count_data()
QTest::addRow("aaa") << QByteArray("aaa") << QByteArray("a") << 3;
QTest::addRow("xyzaaaxyz") << QByteArray("xyzaaxyaxyz") << QByteArray("xyz") << 2;
const int len = 500;
QByteArray longData(len, 'a');
const QByteArray needle("abcdef");
longData.insert(0, needle);
longData.insert(len / 2, needle);
QTest::addRow("longInput") << longData << needle << 2;
}
template <typename Haystack, typename Needle>