diff --git a/src/corelib/text/qstringlist.cpp b/src/corelib/text/qstringlist.cpp index eddd392029..61923e0b3f 100644 --- a/src/corelib/text/qstringlist.cpp +++ b/src/corelib/text/qstringlist.cpp @@ -592,10 +592,12 @@ QString QtPrivate::QStringList_join(const QStringList *that, QStringView sep) \include qstringlist.cpp comparison-case-sensitivity +//! [overloading-base-class-methods] \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. +//! [overloading-base-class-methods] \sa lastIndexOf() */ @@ -629,6 +631,52 @@ qsizetype QtPrivate::QStringList_indexOf(const QStringList &that, QLatin1StringV return indexOf_helper(that, needle, from, cs); } +/*! + \fn qsizetype QStringList::lastIndexOf(const QString &str, qsizetype from, Qt::CaseSensitivity cs) const + \fn qsizetype QStringList::lastIndexOf(QStringView str, qsizetype from, Qt::CaseSensitivity cs) const + \fn qsizetype QStringList::lastIndexOf(QLatin1StringView str, qsizetype from, Qt::CaseSensitivity cs) const + + Returns the index position of the last match 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. + + \include qstringlist.cpp comparison-case-sensitivity + + \include qstringlist.cpp overloading-base-class-methods + + \sa indexOf() +*/ + +template +qsizetype lastIndexof_helper(const QStringList &that, String needle, qsizetype from, + Qt::CaseSensitivity cs) +{ + if (from < 0) + from += that.size(); + else if (from >= that.size()) + from = that.size() - 1; + + for (qsizetype i = from; i >= 0; --i) { + if (needle.compare(that.at(i), cs) == 0) + return i; + } + + return -1; +} + +qsizetype QtPrivate::QStringList_lastIndexOf(const QStringList &that, QLatin1StringView needle, + qsizetype from, Qt::CaseSensitivity cs) +{ + return lastIndexof_helper(that, needle, from, cs); +} + +qsizetype QtPrivate::QStringList_lastIndexOf(const QStringList &that, QStringView needle, + qsizetype from, Qt::CaseSensitivity cs) +{ + return lastIndexof_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 13a93f505a..fc5c49bfe1 100644 --- a/src/corelib/text/qstringlist.h +++ b/src/corelib/text/qstringlist.h @@ -45,6 +45,11 @@ namespace QtPrivate { qsizetype Q_CORE_EXPORT QStringList_indexOf(const QStringList &that, QLatin1StringView str, qsizetype from, Qt::CaseSensitivity cs); + Q_CORE_EXPORT qsizetype QStringList_lastIndexOf(const QStringList &that, QStringView str, + qsizetype from, Qt::CaseSensitivity cs); + Q_CORE_EXPORT qsizetype QStringList_lastIndexOf(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); @@ -141,8 +146,15 @@ public: 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); } + qsizetype lastIndexOf(const QString &str, qsizetype from = -1, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return lastIndexOf(QStringView(str), from, cs); } + qsizetype lastIndexOf(QStringView str, qsizetype from = -1, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return QtPrivate::QStringList_lastIndexOf(*self(), str, from, cs); } + qsizetype lastIndexOf(QLatin1StringView needle, qsizetype from = -1, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return QtPrivate::QStringList_lastIndexOf(*self(), needle, from, cs); } #if QT_CONFIG(regularexpression) inline QStringList filter(const QRegularExpression &re) const diff --git a/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp b/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp index e511d63be1..712d306dca 100644 --- a/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp +++ b/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp @@ -159,6 +159,15 @@ void tst_QStringList::lastIndexOf() QCOMPARE(list.lastIndexOf(QStringView(search), from), expectedResult); QCOMPARE(list.lastIndexOf(QLatin1String(search.toLatin1()), from), expectedResult); QCOMPARE(list.lastIndexOf(QRegularExpression(QRegularExpression::escape(search)), from), expectedResult); + + const QString searchUpper = search.toUpper(); + QCOMPARE(list.lastIndexOf(searchUpper, from, Qt::CaseInsensitive), expectedResult); + QCOMPARE(list.lastIndexOf(QStringView(searchUpper), from, Qt::CaseInsensitive), expectedResult); + QCOMPARE(list.lastIndexOf(QLatin1String(searchUpper.toLatin1()), from, Qt::CaseInsensitive), + expectedResult); + const QRegularExpression re(QRegularExpression::escape(searchUpper), + QRegularExpression::CaseInsensitiveOption); + QCOMPARE(list.lastIndexOf(re, from), expectedResult); } void tst_QStringList::filter()