Add QStringList::indexOf/lastIndexOf for QStringView and QLatin1String
Change-Id: I42eac69c1f7ab88441d464b9d325139defe32b03 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
503ff495aa
commit
e0567d137d
@ -118,6 +118,11 @@ struct Q_CORE_EXPORT QListData {
|
|||||||
inline void **end() const Q_DECL_NOTHROW { return d->array + d->end; }
|
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>
|
template <typename T>
|
||||||
class QList
|
class QList
|
||||||
#ifndef Q_QDOC
|
#ifndef Q_QDOC
|
||||||
@ -136,6 +141,8 @@ public:
|
|||||||
QListData::InlineWithPaddingLayout
|
QListData::InlineWithPaddingLayout
|
||||||
>::type>::type {};
|
>::type>::type {};
|
||||||
private:
|
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;
|
struct Node { void *v;
|
||||||
#if defined(Q_CC_BOR)
|
#if defined(Q_CC_BOR)
|
||||||
Q_INLINE_TEMPLATE T &t();
|
Q_INLINE_TEMPLATE T &t();
|
||||||
@ -975,35 +982,57 @@ inline void QList<T>::append(const QList<T> &t)
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
Q_OUTOFLINE_TEMPLATE int QList<T>::indexOf(const T &t, int from) const
|
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)
|
if (from < 0)
|
||||||
from = qMax(from + p.size(), 0);
|
from = qMax(from + list.p.size(), 0);
|
||||||
if (from < p.size()) {
|
if (from < list.p.size()) {
|
||||||
Node *n = reinterpret_cast<Node *>(p.at(from -1));
|
Node *n = reinterpret_cast<Node *>(list.p.at(from -1));
|
||||||
Node *e = reinterpret_cast<Node *>(p.end());
|
Node *e = reinterpret_cast<Node *>(list.p.end());
|
||||||
while (++n != e)
|
while (++n != e)
|
||||||
if (n->t() == t)
|
if (n->t() == u)
|
||||||
return int(n - reinterpret_cast<Node *>(p.begin()));
|
return int(n - reinterpret_cast<Node *>(list.p.begin()));
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
Q_OUTOFLINE_TEMPLATE int QList<T>::lastIndexOf(const T &t, int from) const
|
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)
|
if (from < 0)
|
||||||
from += p.size();
|
from += list.p.size();
|
||||||
else if (from >= p.size())
|
else if (from >= list.p.size())
|
||||||
from = p.size()-1;
|
from = list.p.size()-1;
|
||||||
if (from >= 0) {
|
if (from >= 0) {
|
||||||
Node *b = reinterpret_cast<Node *>(p.begin());
|
Node *b = reinterpret_cast<Node *>(list.p.begin());
|
||||||
Node *n = reinterpret_cast<Node *>(p.at(from + 1));
|
Node *n = reinterpret_cast<Node *>(list.p.at(from + 1));
|
||||||
while (n-- != b) {
|
while (n-- != b) {
|
||||||
if (n->t() == t)
|
if (n->t() == u)
|
||||||
return n - b;
|
return n - b;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
Q_OUTOFLINE_TEMPLATE bool QList<T>::contains(const T &t) const
|
Q_OUTOFLINE_TEMPLATE bool QList<T>::contains(const T &t) const
|
||||||
|
@ -376,6 +376,56 @@ bool QtPrivate::QStringList_contains(const QStringList *that, QLatin1String str,
|
|||||||
return stringList_contains(*that, str, cs);
|
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
|
#ifndef QT_NO_REGEXP
|
||||||
/*!
|
/*!
|
||||||
\fn QStringList QStringList::filter(const QRegExp &rx) const
|
\fn QStringList QStringList::filter(const QRegExp &rx) const
|
||||||
|
@ -132,6 +132,12 @@ public:
|
|||||||
inline QStringList &operator<<(const QList<QString> &l)
|
inline QStringList &operator<<(const QList<QString> &l)
|
||||||
{ *this += l; return *this; }
|
{ *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
|
#ifndef QT_NO_REGEXP
|
||||||
inline int indexOf(const QRegExp &rx, int from = 0) const;
|
inline int indexOf(const QRegExp &rx, int from = 0) const;
|
||||||
inline int lastIndexOf(const QRegExp &rx, int from = -1) 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;
|
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
|
#ifndef QT_NO_REGEXP
|
||||||
inline QStringList &QListSpecialMethods<QString>::replaceInStrings(const QRegExp &rx, const QString &after)
|
inline QStringList &QListSpecialMethods<QString>::replaceInStrings(const QRegExp &rx, const QString &after)
|
||||||
{
|
{
|
||||||
|
@ -43,7 +43,9 @@ private slots:
|
|||||||
void removeDuplicates();
|
void removeDuplicates();
|
||||||
void removeDuplicates_data();
|
void removeDuplicates_data();
|
||||||
void contains();
|
void contains();
|
||||||
|
void indexOf_data();
|
||||||
void indexOf();
|
void indexOf();
|
||||||
|
void lastIndexOf_data();
|
||||||
void lastIndexOf();
|
void lastIndexOf();
|
||||||
|
|
||||||
void indexOf_regExp();
|
void indexOf_regExp();
|
||||||
@ -141,20 +143,52 @@ void tst_QStringList::lastIndexOf_regExp()
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void tst_QStringList::indexOf_data()
|
||||||
|
{
|
||||||
|
QTest::addColumn<QString>("search");
|
||||||
|
QTest::addColumn<int>("from");
|
||||||
|
QTest::addColumn<int>("expectedResult");
|
||||||
|
|
||||||
|
QTest::newRow("harald") << "harald" << 0 << 0;
|
||||||
|
QTest::newRow("trond") << "trond" << 0 << 1;
|
||||||
|
QTest::newRow("vohi") << "vohi" << 0 << 2;
|
||||||
|
QTest::newRow("harald-1") << "harald" << 1 << 3;
|
||||||
|
|
||||||
|
QTest::newRow("hans") << "hans" << 0 << -1;
|
||||||
|
QTest::newRow("trond-1") << "trond" << 2 << -1;
|
||||||
|
QTest::newRow("harald-2") << "harald" << -1 << 3;
|
||||||
|
QTest::newRow("vohi-1") << "vohi" << -3 << 2;
|
||||||
|
}
|
||||||
|
|
||||||
void tst_QStringList::indexOf()
|
void tst_QStringList::indexOf()
|
||||||
{
|
{
|
||||||
QStringList list;
|
QStringList list;
|
||||||
list << "harald" << "trond" << "vohi" << "harald";
|
list << "harald" << "trond" << "vohi" << "harald";
|
||||||
|
|
||||||
QCOMPARE(list.indexOf("harald"), 0);
|
QFETCH(QString, search);
|
||||||
QCOMPARE(list.indexOf("trond"), 1);
|
QFETCH(int, from);
|
||||||
QCOMPARE(list.indexOf("vohi"), 2);
|
QFETCH(int, expectedResult);
|
||||||
QCOMPARE(list.indexOf("harald", 1), 3);
|
|
||||||
|
|
||||||
QCOMPARE(list.indexOf("hans"), -1);
|
QCOMPARE(list.indexOf(search, from), expectedResult);
|
||||||
QCOMPARE(list.indexOf("trond", 2), -1);
|
QCOMPARE(list.indexOf(QStringView(search), from), expectedResult);
|
||||||
QCOMPARE(list.indexOf("harald", -1), 3);
|
QCOMPARE(list.indexOf(QLatin1String(search.toLatin1()), from), expectedResult);
|
||||||
QCOMPARE(list.indexOf("vohi", -3), 2);
|
}
|
||||||
|
|
||||||
|
void tst_QStringList::lastIndexOf_data()
|
||||||
|
{
|
||||||
|
QTest::addColumn<QString>("search");
|
||||||
|
QTest::addColumn<int>("from");
|
||||||
|
QTest::addColumn<int>("expectedResult");
|
||||||
|
|
||||||
|
QTest::newRow("harald") << "harald" << -1 << 3;
|
||||||
|
QTest::newRow("trond") << "trond" << -1 << 1;
|
||||||
|
QTest::newRow("vohi") << "vohi" << -1 << 2;
|
||||||
|
QTest::newRow("harald-1") << "harald" << 2 << 0;
|
||||||
|
|
||||||
|
QTest::newRow("hans") << "hans" << -1 << -1;
|
||||||
|
QTest::newRow("vohi-1") << "vohi" << 1 << -1;
|
||||||
|
QTest::newRow("vohi-2") << "vohi" << -1 << 2;
|
||||||
|
QTest::newRow("vohi-3") << "vohi" << -3 << -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_QStringList::lastIndexOf()
|
void tst_QStringList::lastIndexOf()
|
||||||
@ -162,15 +196,13 @@ void tst_QStringList::lastIndexOf()
|
|||||||
QStringList list;
|
QStringList list;
|
||||||
list << "harald" << "trond" << "vohi" << "harald";
|
list << "harald" << "trond" << "vohi" << "harald";
|
||||||
|
|
||||||
QCOMPARE(list.lastIndexOf("harald"), 3);
|
QFETCH(QString, search);
|
||||||
QCOMPARE(list.lastIndexOf("trond"), 1);
|
QFETCH(int, from);
|
||||||
QCOMPARE(list.lastIndexOf("vohi"), 2);
|
QFETCH(int, expectedResult);
|
||||||
QCOMPARE(list.lastIndexOf("harald", 2), 0);
|
|
||||||
|
|
||||||
QCOMPARE(list.lastIndexOf("hans"), -1);
|
QCOMPARE(list.lastIndexOf(search, from), expectedResult);
|
||||||
QCOMPARE(list.lastIndexOf("vohi", 1), -1);
|
QCOMPARE(list.lastIndexOf(QStringView(search), from), expectedResult);
|
||||||
QCOMPARE(list.lastIndexOf("vohi", -1), 2);
|
QCOMPARE(list.lastIndexOf(QLatin1String(search.toLatin1()), from), expectedResult);
|
||||||
QCOMPARE(list.lastIndexOf("vohi", -3), -1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_QStringList::filter()
|
void tst_QStringList::filter()
|
||||||
|
Loading…
Reference in New Issue
Block a user