From 1dc4c3817b79394e913170aca1f65194cbd46918 Mon Sep 17 00:00:00 2001 From: Anton Kudryavtsev Date: Sun, 19 Mar 2017 14:01:49 +0300 Subject: [PATCH] QStringList: add contains(QLatin1String) overload ... to avoid the expensive conversion from QString to QL1S. [ChangeLog][QtCore][QStringList] Added contains(QLatin1String) overload. Change-Id: Ie75839ce9e46e03fe5155a02c7dcf00277b95c8d Reviewed-by: Thiago Macieira --- src/corelib/tools/qstringlist.cpp | 34 +++++++++++++++---- src/corelib/tools/qstringlist.h | 7 ++++ .../tools/qstringlist/tst_qstringlist.cpp | 9 +++++ 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/corelib/tools/qstringlist.cpp b/src/corelib/tools/qstringlist.cpp index 75ff934f15..d65563f76d 100644 --- a/src/corelib/tools/qstringlist.cpp +++ b/src/corelib/tools/qstringlist.cpp @@ -299,6 +299,16 @@ QStringList QtPrivate::QStringList_filter(const QStringList *that, const QString return res; } +template +static bool stringList_contains(const QStringList &stringList, const T &str, Qt::CaseSensitivity cs) +{ + for (const auto &string : stringList) { + if (string.size() == str.size() && string.compare(str, cs) == 0) + return true; + } + return false; +} + /*! \fn bool QStringList::contains(const QString &str, Qt::CaseSensitivity cs) const @@ -312,12 +322,24 @@ QStringList QtPrivate::QStringList_filter(const QStringList *that, const QString bool QtPrivate::QStringList_contains(const QStringList *that, const QString &str, Qt::CaseSensitivity cs) { - for (int i = 0; i < that->size(); ++i) { - const QString & string = that->at(i); - if (string.length() == str.length() && str.compare(string, cs) == 0) - return true; - } - return false; + return stringList_contains(*that, str, cs); +} + +/*! + \fn bool QStringList::contains(QLatin1String str, Qt::CaseSensitivity cs) const + \overload + \since 5.10 + + Returns \c true if the list contains the string \a str; otherwise + returns \c false. The search is case insensitive if \a cs is + Qt::CaseInsensitive; the search is case sensitive by default. + + \sa indexOf(), lastIndexOf(), QString::contains() + */ +bool QtPrivate::QStringList_contains(const QStringList *that, QLatin1String str, + Qt::CaseSensitivity cs) +{ + return stringList_contains(*that, str, cs); } #ifndef QT_NO_REGEXP diff --git a/src/corelib/tools/qstringlist.h b/src/corelib/tools/qstringlist.h index 720d7d7419..b11856d9be 100644 --- a/src/corelib/tools/qstringlist.h +++ b/src/corelib/tools/qstringlist.h @@ -120,6 +120,7 @@ public: #endif inline bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + inline bool contains(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; inline QStringList operator+(const QStringList &other) const { QStringList n = *this; n += other; return n; } @@ -165,6 +166,7 @@ namespace QtPrivate { Qt::CaseSensitivity cs); bool Q_CORE_EXPORT QStringList_contains(const QStringList *that, const QString &str, Qt::CaseSensitivity cs); + bool Q_CORE_EXPORT QStringList_contains(const QStringList *that, QLatin1String str, Qt::CaseSensitivity cs); void Q_CORE_EXPORT QStringList_replaceInStrings(QStringList *that, const QString &before, const QString &after, Qt::CaseSensitivity cs); @@ -222,6 +224,11 @@ inline bool QStringList::contains(const QString &str, Qt::CaseSensitivity cs) co return QtPrivate::QStringList_contains(this, str, cs); } +inline bool QStringList::contains(QLatin1String str, Qt::CaseSensitivity cs) const +{ + return QtPrivate::QStringList_contains(this, str, cs); +} + inline QStringList &QListSpecialMethods::replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs) { QtPrivate::QStringList_replaceInStrings(self(), before, after, cs); diff --git a/tests/auto/corelib/tools/qstringlist/tst_qstringlist.cpp b/tests/auto/corelib/tools/qstringlist/tst_qstringlist.cpp index 2385aa992c..9f054190e5 100644 --- a/tests/auto/corelib/tools/qstringlist/tst_qstringlist.cpp +++ b/tests/auto/corelib/tools/qstringlist/tst_qstringlist.cpp @@ -264,6 +264,15 @@ void tst_QStringList::contains() QVERIFY(list.contains("ARTHUR", Qt::CaseInsensitive)); QVERIFY(list.contains("dent", Qt::CaseInsensitive)); QVERIFY(!list.contains("hans", Qt::CaseInsensitive)); + + QVERIFY(list.contains(QLatin1String("arthur"))); + QVERIFY(!list.contains(QLatin1String("ArthuR"))); + QVERIFY(!list.contains(QLatin1String("Hans"))); + QVERIFY(list.contains(QLatin1String("arthur"), Qt::CaseInsensitive)); + QVERIFY(list.contains(QLatin1String("ArthuR"), Qt::CaseInsensitive)); + QVERIFY(list.contains(QLatin1String("ARTHUR"), Qt::CaseInsensitive)); + QVERIFY(list.contains(QLatin1String("dent"), Qt::CaseInsensitive)); + QVERIFY(!list.contains(QLatin1String("hans"), Qt::CaseInsensitive)); } void tst_QStringList::removeDuplicates_data()