From 3dffd5aa0bba785cd0da80d46efda9375eb39d14 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Sun, 17 Sep 2023 01:29:13 +0300 Subject: [PATCH] QStringList: add indexOf() QString/QStringView/QL1SV overloads [ChangeLog][QtCore][QStringList] Added indexOf() overloads that take QString/QStringView/QLatin1StringView, and a Qt::CaseSensitivity parameter. Prior to this using QStringList::indexOf() called the methods inherited from the base class. Task-number: QTBUG-116918 Change-Id: Ibc42130b6509f6ecfe7de0d6be378f226ae61982 Reviewed-by: Thiago Macieira --- src/corelib/text/qstringlist.cpp | 48 +++++++++++++++++++ src/corelib/text/qstringlist.h | 18 ++++++- .../text/qstringlist/tst_qstringlist.cpp | 9 ++++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/src/corelib/text/qstringlist.cpp b/src/corelib/text/qstringlist.cpp index 7f2572838f..4ff6d56603 100644 --- a/src/corelib/text/qstringlist.cpp +++ b/src/corelib/text/qstringlist.cpp @@ -569,6 +569,54 @@ QString QtPrivate::QStringList_join(const QStringList *that, QStringView sep) the latter string list. */ +/*! + \fn qsizetype QStringList::indexOf(const QString &str, qsizetype from, Qt::CaseSensitivity cs) const + \fn qsizetype QStringList::indexOf(QStringView str, qsizetype from, Qt::CaseSensitivity cs) const + \fn qsizetype QStringList::indexOf(QLatin1StringView str, qsizetype from, Qt::CaseSensitivity cs) const + + Returns the index position of the first match of \a str in the list, + searching forward from index position \a from. Returns -1 if no item + matched. + + \include qstringlist.cpp comparison-case-sensitivity + + \note The \a cs parameter was added in Qt 6.7, i.e. these methods now overload + the methods inherited from the base class. Prior to that these methods only + had two parameters. This change is source compatible and existing code should + continue to work. + + \sa lastIndexOf() +*/ + +template +qsizetype indexOf_helper(const QStringList &that, String needle, qsizetype from, + Qt::CaseSensitivity cs) +{ + if (from < 0) // Historical behavior + from = qMax(from + that.size(), 0); + + if (from >= that.size()) + return -1; + + for (qsizetype i = from; i < that.size(); ++i) { + if (needle.compare(that.at(i), cs) == 0) + return i; + } + return -1; +} + +qsizetype QtPrivate::QStringList_indexOf(const QStringList &that, QStringView needle, + qsizetype from, Qt::CaseSensitivity cs) +{ + return indexOf_helper(that, needle, from, cs); +} + +qsizetype QtPrivate::QStringList_indexOf(const QStringList &that, QLatin1StringView needle, + qsizetype from, Qt::CaseSensitivity cs) +{ + return indexOf_helper(that, needle, from, cs); +} + #if QT_CONFIG(regularexpression) /*! \fn qsizetype QStringList::indexOf(const QRegularExpression &re, qsizetype from) const diff --git a/src/corelib/text/qstringlist.h b/src/corelib/text/qstringlist.h index 99f51667ce..752fc58865 100644 --- a/src/corelib/text/qstringlist.h +++ b/src/corelib/text/qstringlist.h @@ -38,6 +38,11 @@ namespace QtPrivate { void Q_CORE_EXPORT QStringList_replaceInStrings(QStringList *that, QStringView before, QStringView after, Qt::CaseSensitivity cs); + qsizetype Q_CORE_EXPORT QStringList_indexOf(const QStringList &that, QStringView str, + qsizetype from, Qt::CaseSensitivity cs); + qsizetype Q_CORE_EXPORT QStringList_indexOf(const QStringList &that, QLatin1StringView str, + qsizetype from, Qt::CaseSensitivity cs); + #if QT_CONFIG(regularexpression) void Q_CORE_EXPORT QStringList_replaceInStrings(QStringList *that, const QRegularExpression &rx, const QString &after); QStringList Q_CORE_EXPORT QStringList_filter(const QStringList *that, const QRegularExpression &re); @@ -121,8 +126,17 @@ public: inline bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::QStringList_contains(self(), str, cs); } - qsizetype indexOf(const QString &str, qsizetype from = 0) const noexcept - { return indexOf(QStringView(str), from); } + + qsizetype indexOf(const QString &str, qsizetype from = 0, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return indexOf(QStringView(str), from, cs); } + qsizetype indexOf(QStringView needle, qsizetype from = 0, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return QtPrivate::QStringList_indexOf(*self(), needle, from, cs); } + qsizetype indexOf(QLatin1StringView needle, qsizetype from = 0, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return QtPrivate::QStringList_indexOf(*self(), needle, from, cs); } + qsizetype lastIndexOf(const QString &str, qsizetype from = -1) const noexcept { return lastIndexOf(QStringView(str), from); } diff --git a/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp b/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp index a20e4be145..233f642dbf 100644 --- a/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp +++ b/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp @@ -113,6 +113,15 @@ void tst_QStringList::indexOf() QCOMPARE(list.indexOf(QStringView(search), from), expectedResult); QCOMPARE(list.indexOf(QLatin1String(search.toLatin1()), from), expectedResult); QCOMPARE(list.indexOf(QRegularExpression(QRegularExpression::escape(search)), from), expectedResult); + + QString searchUpper = search.toUpper(); + QCOMPARE(list.indexOf(searchUpper, from, Qt::CaseInsensitive), expectedResult); + QCOMPARE(list.indexOf(QStringView(searchUpper), from, Qt::CaseInsensitive), expectedResult); + QCOMPARE(list.indexOf(QLatin1StringView(searchUpper.toLatin1()), from, Qt::CaseInsensitive), + expectedResult); + const QRegularExpression re(QRegularExpression::escape(searchUpper), + QRegularExpression::CaseInsensitiveOption); + QCOMPARE(list.indexOf(re, from), expectedResult); } void tst_QStringList::lastIndexOf_data()