diff options
author | Albert Astals Cid <albert.astals.cid@kdab.com> | 2018-07-27 17:46:04 +0200 |
---|---|---|
committer | Albert Astals Cid <albert.astals.cid@kdab.com> | 2019-01-22 15:07:29 +0000 |
commit | e0567d137df4ff3978f767fa723ae05a7b0ab546 (patch) | |
tree | d85ba53515c7427a71fefd58f0d40b02bcdb9a21 /src | |
parent | 503ff495aac77e957f711f19275ab1754228cfeb (diff) |
Add QStringList::indexOf/lastIndexOf for QStringView and QLatin1String
Change-Id: I42eac69c1f7ab88441d464b9d325139defe32b03
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/tools/qlist.h | 53 | ||||
-rw-r--r-- | src/corelib/tools/qstringlist.cpp | 50 | ||||
-rw-r--r-- | src/corelib/tools/qstringlist.h | 26 |
3 files changed, 117 insertions, 12 deletions
diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index fe8a1407e2..6643288bd5 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -118,6 +118,11 @@ struct Q_CORE_EXPORT QListData { inline void **end() const Q_DECL_NOTHROW { return d->array + d->end; } }; +namespace QtPrivate { + template <typename V, typename U> int indexOf(const QList<V> &list, const U &u, int from); + template <typename V, typename U> int lastIndexOf(const QList<V> &list, const U &u, int from); +} + template <typename T> class QList #ifndef Q_QDOC @@ -136,6 +141,8 @@ public: QListData::InlineWithPaddingLayout >::type>::type {}; private: + template <typename V, typename U> friend int QtPrivate::indexOf(const QList<V> &list, const U &u, int from); + template <typename V, typename U> friend int QtPrivate::lastIndexOf(const QList<V> &list, const U &u, int from); struct Node { void *v; #if defined(Q_CC_BOR) Q_INLINE_TEMPLATE T &t(); @@ -975,35 +982,57 @@ inline void QList<T>::append(const QList<T> &t) template <typename T> Q_OUTOFLINE_TEMPLATE int QList<T>::indexOf(const T &t, int from) const { + return QtPrivate::indexOf<T, T>(*this, t, from); +} + +namespace QtPrivate +{ +template <typename T, typename U> +int indexOf(const QList<T> &list, const U &u, int from) +{ + typedef typename QList<T>::Node Node; + if (from < 0) - from = qMax(from + p.size(), 0); - if (from < p.size()) { - Node *n = reinterpret_cast<Node *>(p.at(from -1)); - Node *e = reinterpret_cast<Node *>(p.end()); + from = qMax(from + list.p.size(), 0); + if (from < list.p.size()) { + Node *n = reinterpret_cast<Node *>(list.p.at(from -1)); + Node *e = reinterpret_cast<Node *>(list.p.end()); while (++n != e) - if (n->t() == t) - return int(n - reinterpret_cast<Node *>(p.begin())); + if (n->t() == u) + return int(n - reinterpret_cast<Node *>(list.p.begin())); } return -1; } +} template <typename T> Q_OUTOFLINE_TEMPLATE int QList<T>::lastIndexOf(const T &t, int from) const { + return QtPrivate::lastIndexOf<T, T>(*this, t, from); +} + +namespace QtPrivate +{ +template <typename T, typename U> +int lastIndexOf(const QList<T> &list, const U &u, int from) +{ + typedef typename QList<T>::Node Node; + if (from < 0) - from += p.size(); - else if (from >= p.size()) - from = p.size()-1; + from += list.p.size(); + else if (from >= list.p.size()) + from = list.p.size()-1; if (from >= 0) { - Node *b = reinterpret_cast<Node *>(p.begin()); - Node *n = reinterpret_cast<Node *>(p.at(from + 1)); + Node *b = reinterpret_cast<Node *>(list.p.begin()); + Node *n = reinterpret_cast<Node *>(list.p.at(from + 1)); while (n-- != b) { - if (n->t() == t) + if (n->t() == u) return n - b; } } return -1; } +} template <typename T> Q_OUTOFLINE_TEMPLATE bool QList<T>::contains(const T &t) const diff --git a/src/corelib/tools/qstringlist.cpp b/src/corelib/tools/qstringlist.cpp index ec6de08805..cc6eaf8ad2 100644 --- a/src/corelib/tools/qstringlist.cpp +++ b/src/corelib/tools/qstringlist.cpp @@ -376,6 +376,56 @@ bool QtPrivate::QStringList_contains(const QStringList *that, QLatin1String str, return stringList_contains(*that, str, cs); } +/*! + \fn bool QStringList::indexOf(QStringView str, int from) const + \overload + \since 5.13 + + Returns the index position of the first occurrence of \a str in + the list, searching forward from index position \a from. Returns + -1 if no item matched. + + \sa lastIndexOf(), contains() + */ + +/*! + \fn bool QStringList::indexOf(QLatin1String str, int from) const + \overload + \since 5.13 + + Returns the index position of the first occurrence of \a str in + the list, searching forward from index position \a from. Returns + -1 if no item matched. + + \sa lastIndexOf(), contains() + */ + +/*! + \fn bool QStringList::lastIndexOf(QStringView str, int from) const + \overload + \since 5.13 + + Returns the index position of the last occurrence of \a str in + the list, searching backward from index position \a from. If \a + from is -1 (the default), the search starts at the last item. + Returns -1 if no item matched. + + \sa indexOf(), contains() + */ + +/*! + \fn bool QStringList::lastIndexOf(QLatin1String str, int from) const + \overload + \since 5.13 + + Returns the index position of the last occurrence of \a str in + the list, searching backward from index position \a from. If \a + from is -1 (the default), the search starts at the last item. + Returns -1 if no item matched. + + \sa indexOf(), contains() + */ + #ifndef QT_NO_REGEXP /*! \fn QStringList QStringList::filter(const QRegExp &rx) const diff --git a/src/corelib/tools/qstringlist.h b/src/corelib/tools/qstringlist.h index 10cbad04d6..b6c48488df 100644 --- a/src/corelib/tools/qstringlist.h +++ b/src/corelib/tools/qstringlist.h @@ -132,6 +132,12 @@ public: inline QStringList &operator<<(const QList<QString> &l) { *this += l; return *this; } + inline int indexOf(QStringView str, int from = 0) const; + inline int indexOf(QLatin1String str, int from = 0) const; + + inline int lastIndexOf(QStringView str, int from = -1) const; + inline int lastIndexOf(QLatin1String str, int from = -1) const; + #ifndef QT_NO_REGEXP inline int indexOf(const QRegExp &rx, int from = 0) const; inline int lastIndexOf(const QRegExp &rx, int from = -1) const; @@ -249,6 +255,26 @@ inline QStringList operator+(const QList<QString> &one, const QStringList &other return n; } +inline int QStringList::indexOf(QStringView string, int from) const +{ + return QtPrivate::indexOf<QString, QStringView>(*this, string, from); +} + +inline int QStringList::indexOf(QLatin1String string, int from) const +{ + return QtPrivate::indexOf<QString, QLatin1String>(*this, string, from); +} + +inline int QStringList::lastIndexOf(QStringView string, int from) const +{ + return QtPrivate::lastIndexOf<QString, QStringView>(*this, string, from); +} + +inline int QStringList::lastIndexOf(QLatin1String string, int from) const +{ + return QtPrivate::lastIndexOf<QString, QLatin1String>(*this, string, from); +} + #ifndef QT_NO_REGEXP inline QStringList &QListSpecialMethods<QString>::replaceInStrings(const QRegExp &rx, const QString &after) { |