QStringList: add lastIndexOf() overloads
[ChangeLog][QtCore][QStringList] Added lastIndexOf() overloads that take a QString/QStringView/QLatin1StringView and a Qt::CaseSenitivity parameters. Prior to this calling lastIndexOf() would call the methods inherited from the base class. This change is source compatible and existing code should continue to work. Task-number: QTBUG-116918 Change-Id: Ia50c884c00021bf581c23c12e0e0c22700dae446 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
c205f05128
commit
f2e19d37de
@ -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 <typename String>
|
||||
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
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user