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 <thiago.macieira@intel.com>bb10
parent
a6ad755734
commit
3dffd5aa0b
|
|
@ -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 <typename String>
|
||||
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
|
||||
|
|
|
|||
|
|
@ -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); }
|
||||
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Reference in New Issue