QStringList: add filter(QLatin1StringMatcher) overload

Drive-by change: remove a redundant #include <qset.h>

[ChangeLog][QtCore][QStringList] Added filter(QLatin1StringMatcher)
overload, which may be faster when searching in large lists or lists
with longer strings.

Change-Id: I1551e98360b5c1f536d935efc46151ef9992152c
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ahmad Samir 2023-09-15 19:15:59 +03:00
parent a00337f852
commit fe34ea9c88
4 changed files with 46 additions and 2 deletions

View File

@ -4,6 +4,7 @@
#include <QtGui>
#include <iostream>
using namespace std;
using namespace Qt::StringLiterals;
class Widget : public QWidget
{
@ -100,6 +101,14 @@ Widget::Widget(QWidget *parent)
QStringList filtered = veryLongList.filter(matcher);
//! [18]
}
{
//! [19]
QStringList veryLargeList;
QLatin1StringMatcher matcher("Street"_L1, Qt::CaseInsensitive);
QStringList filtered = veryLargeList.filter(matcher);
//! [19]
}
}
int main(int argc, char *argv[])

View File

@ -2,11 +2,11 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include <qstringlist.h>
#include <qset.h>
#if QT_CONFIG(regularexpression)
# include <qregularexpression.h>
#endif
#include <private/qduplicatetracker_p.h>
#include <QtCore/qlatin1stringmatcher.h>
#include <algorithm>
QT_BEGIN_NAMESPACE
@ -273,9 +273,25 @@ QStringList QtPrivate::QStringList_filter(const QStringList *that, QStringView s
For example:
\snippet qstringlist/main.cpp 18
\sa contains()
\sa contains(), filter(const QLatin1StringMatcher &)
*/
/*!
\fn QStringList QStringList::filter(const QLatin1StringMatcher &matcher) const
\since 6.9
Returns a list of all the strings matched by \a matcher (i.e. for which
\c matcher.indexIn() returns an index >= 0).
Using QLatin1StringMatcher may be faster when searching in large
lists and/or in lists with long strings (the best way to find out is
benchmarking).
For example:
\snippet qstringlist/main.cpp 19
\sa contains(), filter(const QStringMatcher &)
*/
QStringList QtPrivate::QStringList_filter(const QStringList &that, const QStringMatcher &matcher)
{
QStringList res;
@ -286,6 +302,16 @@ QStringList QtPrivate::QStringList_filter(const QStringList &that, const QString
return res;
}
QStringList QtPrivate::QStringList_filter(const QStringList &that, const QLatin1StringMatcher &matcher)
{
QStringList res;
for (const auto &s : that) {
if (matcher.indexIn(s) != -1)
res.append(s);
}
return res;
}
/*!
\fn QStringList QStringList::filter(QLatin1StringView str, Qt::CaseSensitivity cs) const
\since 6.7

View File

@ -14,6 +14,7 @@
QT_BEGIN_NAMESPACE
class QLatin1StringMatcher;
class QRegularExpression;
#if !defined(QT_NO_JAVA_STYLE_ITERATORS)
@ -34,6 +35,8 @@ namespace QtPrivate {
Qt::CaseSensitivity cs);
Q_CORE_EXPORT QStringList QStringList_filter(const QStringList &that,
const QStringMatcher &matcher);
Q_CORE_EXPORT QStringList QStringList_filter(const QStringList &that,
const QLatin1StringMatcher &matcher);
bool Q_CORE_EXPORT QStringList_contains(const QStringList *that, QStringView str, Qt::CaseSensitivity cs);
bool Q_CORE_EXPORT QStringList_contains(const QStringList *that, QLatin1StringView str, Qt::CaseSensitivity cs);
@ -95,6 +98,9 @@ public:
QStringList filter(const QStringMatcher &matcher) const
{ return QtPrivate::QStringList_filter(*self(), matcher); }
QStringList filter(const QLatin1StringMatcher &matcher) const
{ return QtPrivate::QStringList_filter(*self(), matcher); }
QStringList filter(QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
{ return QtPrivate::QStringList_filter(*self(), needle, cs); }
inline QStringList filter(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

View File

@ -2,6 +2,7 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
#include <qlatin1stringmatcher.h>
#include <qlist.h>
#include <qregularexpression.h>
#include <qstringlist.h>
@ -181,6 +182,7 @@ void tst_QStringList::filter()
QCOMPARE(list.filter("Bill"_L1), expected);
QCOMPARE(list.filter(QRegularExpression(u"[i]ll"_s)), expected);
QCOMPARE(list.filter(QStringMatcher(u"Bill")), expected);
QCOMPARE(list.filter(QLatin1StringMatcher("Bill"_L1)), expected);
}
{ // CaseInsensitive
@ -191,6 +193,7 @@ void tst_QStringList::filter()
QCOMPARE(list.filter(QRegularExpression(u"[i]ll"_s, QRegularExpression::CaseInsensitiveOption)),
expected);
QCOMPARE(list.filter(QStringMatcher(u"Bill", Qt::CaseInsensitive)), expected);
QCOMPARE(list.filter(QLatin1StringMatcher("bill"_L1, Qt::CaseInsensitive)), expected);
}
}