From c205f051282e78e9d2608b5863d14c6d0725b77c Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Fri, 15 Sep 2023 19:10:56 +0300 Subject: [PATCH] QStringList: add filter(QL1SV) overload [ChangeLog][QtCore][QStringList] Added filter(QLatin1StringView) overload, which is more optimized when searching for a Latin-1 string literal as no conversion to QString is necessary. Task-number: QTBUG-116918 Change-Id: Ieb92f4cfd545b070258dbc5c701ddfb2e6f3fc64 Reviewed-by: Thiago Macieira --- src/corelib/text/qstringlist.cpp | 12 ++++++++++++ src/corelib/text/qstringlist.h | 4 ++++ .../corelib/text/qstringlist/tst_qstringlist.cpp | 2 ++ 3 files changed, 18 insertions(+) diff --git a/src/corelib/text/qstringlist.cpp b/src/corelib/text/qstringlist.cpp index 4ff6d56603..eddd392029 100644 --- a/src/corelib/text/qstringlist.cpp +++ b/src/corelib/text/qstringlist.cpp @@ -286,6 +286,18 @@ QStringList QtPrivate::QStringList_filter(const QStringList &that, const QString return res; } +/*! + \fn QStringList QStringList::filter(QLatin1StringView str, Qt::CaseSensitivity cs) const + \since 6.7 + \overload +*/ + +QStringList QtPrivate::QStringList_filter(const QStringList &that, QLatin1StringView needle, + Qt::CaseSensitivity cs) +{ + return filter_helper(that, needle, cs); +} + template static bool stringList_contains(const QStringList &stringList, const T &str, Qt::CaseSensitivity cs) { diff --git a/src/corelib/text/qstringlist.h b/src/corelib/text/qstringlist.h index 752fc58865..13a93f505a 100644 --- a/src/corelib/text/qstringlist.h +++ b/src/corelib/text/qstringlist.h @@ -30,6 +30,8 @@ namespace QtPrivate { Q_CORE_EXPORT QString QStringList_join(const QStringList &list, QLatin1StringView sep); QStringList Q_CORE_EXPORT QStringList_filter(const QStringList *that, QStringView str, Qt::CaseSensitivity cs); + Q_CORE_EXPORT QStringList QStringList_filter(const QStringList &that, QLatin1StringView needle, + Qt::CaseSensitivity cs); Q_CORE_EXPORT QStringList QStringList_filter(const QStringList &that, const QStringMatcher &matcher); @@ -88,6 +90,8 @@ public: QStringList filter(const QStringMatcher &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 { return QtPrivate::QStringList_filter(self(), str, cs); } inline QStringList &replaceInStrings(QStringView before, QStringView after, Qt::CaseSensitivity cs = Qt::CaseSensitive) diff --git a/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp b/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp index 233f642dbf..e511d63be1 100644 --- a/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp +++ b/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp @@ -169,6 +169,7 @@ void tst_QStringList::filter() const QStringList expected{u"Bill Gates"_s, u"Bill Clinton"_s}; QCOMPARE(list.filter(u"Bill"_s), expected); QCOMPARE(list.filter(u"Bill"), expected); + QCOMPARE(list.filter("Bill"_L1), expected); QCOMPARE(list.filter(QRegularExpression(u"[i]ll"_s)), expected); QCOMPARE(list.filter(QStringMatcher(u"Bill")), expected); } @@ -177,6 +178,7 @@ void tst_QStringList::filter() const QStringList expected = {u"Bill Gates"_s, u"Bill Clinton"_s, u"bIll"_s}; QCOMPARE(list.filter(u"bill"_s, Qt::CaseInsensitive), expected); QCOMPARE(list.filter(u"bill", Qt::CaseInsensitive), expected); + QCOMPARE(list.filter("bill"_L1, Qt::CaseInsensitive), expected); QCOMPARE(list.filter(QRegularExpression(u"[i]ll"_s, QRegularExpression::CaseInsensitiveOption)), expected); QCOMPARE(list.filter(QStringMatcher(u"Bill", Qt::CaseInsensitive)), expected);