From fe34ea9c88b48fc1874b8da679b6f999352e1f1d Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Fri, 15 Sep 2023 19:15:59 +0300 Subject: [PATCH] QStringList: add filter(QLatin1StringMatcher) overload Drive-by change: remove a redundant #include [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 --- src/corelib/doc/snippets/qstringlist/main.cpp | 9 ++++++ src/corelib/text/qstringlist.cpp | 30 +++++++++++++++++-- src/corelib/text/qstringlist.h | 6 ++++ .../text/qstringlist/tst_qstringlist.cpp | 3 ++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/corelib/doc/snippets/qstringlist/main.cpp b/src/corelib/doc/snippets/qstringlist/main.cpp index 1b7453cf6a0..1791faf5116 100644 --- a/src/corelib/doc/snippets/qstringlist/main.cpp +++ b/src/corelib/doc/snippets/qstringlist/main.cpp @@ -4,6 +4,7 @@ #include #include 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[]) diff --git a/src/corelib/text/qstringlist.cpp b/src/corelib/text/qstringlist.cpp index 2cc5d0c7f72..281ef611547 100644 --- a/src/corelib/text/qstringlist.cpp +++ b/src/corelib/text/qstringlist.cpp @@ -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 -#include #if QT_CONFIG(regularexpression) # include #endif #include +#include #include 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 diff --git a/src/corelib/text/qstringlist.h b/src/corelib/text/qstringlist.h index fc5c49bfe18..a8135f2f1ba 100644 --- a/src/corelib/text/qstringlist.h +++ b/src/corelib/text/qstringlist.h @@ -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 diff --git a/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp b/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp index 6ac8236d57b..2616cd3b717 100644 --- a/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp +++ b/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include +#include #include #include #include @@ -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); } }