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 <thiago.macieira@intel.com>
This commit is contained in:
Anton Kudryavtsev 2017-03-19 14:01:49 +03:00
parent 8066ae4943
commit 1dc4c3817b
3 changed files with 44 additions and 6 deletions

View File

@ -299,6 +299,16 @@ QStringList QtPrivate::QStringList_filter(const QStringList *that, const QString
return res; return res;
} }
template<typename T>
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 \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, bool QtPrivate::QStringList_contains(const QStringList *that, const QString &str,
Qt::CaseSensitivity cs) Qt::CaseSensitivity cs)
{ {
for (int i = 0; i < that->size(); ++i) { return stringList_contains(*that, str, cs);
const QString & string = that->at(i); }
if (string.length() == str.length() && str.compare(string, cs) == 0)
return true; /*!
} \fn bool QStringList::contains(QLatin1String str, Qt::CaseSensitivity cs) const
return false; \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 #ifndef QT_NO_REGEXP

View File

@ -120,6 +120,7 @@ public:
#endif #endif
inline bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; 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 inline QStringList operator+(const QStringList &other) const
{ QStringList n = *this; n += other; return n; } { QStringList n = *this; n += other; return n; }
@ -165,6 +166,7 @@ namespace QtPrivate {
Qt::CaseSensitivity cs); 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, 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, void Q_CORE_EXPORT QStringList_replaceInStrings(QStringList *that, const QString &before, const QString &after,
Qt::CaseSensitivity cs); 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); 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<QString>::replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs) inline QStringList &QListSpecialMethods<QString>::replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs)
{ {
QtPrivate::QStringList_replaceInStrings(self(), before, after, cs); QtPrivate::QStringList_replaceInStrings(self(), before, after, cs);

View File

@ -264,6 +264,15 @@ void tst_QStringList::contains()
QVERIFY(list.contains("ARTHUR", Qt::CaseInsensitive)); QVERIFY(list.contains("ARTHUR", Qt::CaseInsensitive));
QVERIFY(list.contains("dent", Qt::CaseInsensitive)); QVERIFY(list.contains("dent", Qt::CaseInsensitive));
QVERIFY(!list.contains("hans", 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() void tst_QStringList::removeDuplicates_data()