QStringList: add QStringView overloads of join, filter, replaceInStrings

[ChangeLog][QtCore][QStringList] Added QStringView overloads of join(),
filter(), and replaceInStrings().

Change-Id: I9636e21e2e43ed46cce0aa7fa23ab0710aa641ba
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Mat Sutcliffe 2019-06-23 23:24:32 +01:00 committed by Marc Mutz
parent 57055ffafd
commit 2a99f60cfb
3 changed files with 133 additions and 0 deletions

View File

@ -283,6 +283,7 @@ void QtPrivate::QStringList_sort(QStringList *that, Qt::CaseSensitivity cs)
}
#if QT_STRINGVIEW_LEVEL < 2
/*!
\fn QStringList QStringList::filter(const QString &str, Qt::CaseSensitivity cs) const
@ -302,6 +303,26 @@ void QtPrivate::QStringList_sort(QStringList *that, Qt::CaseSensitivity cs)
\sa contains()
*/
#endif
/*!
\fn QStringList QStringList::filter(QStringView str, Qt::CaseSensitivity cs) const
\overload
\since 5.14
*/
QStringList QtPrivate::QStringList_filter(const QStringList *that, QStringView str,
Qt::CaseSensitivity cs)
{
QStringMatcher matcher(str.data(), str.length(), cs);
QStringList res;
for (int i = 0; i < that->size(); ++i)
if (matcher.indexIn(that->at(i)) != -1)
res << that->at(i);
return res;
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
/// Not really needed anymore, but kept for binary compatibility
QStringList QtPrivate::QStringList_filter(const QStringList *that, const QString &str,
Qt::CaseSensitivity cs)
{
@ -312,6 +333,7 @@ QStringList QtPrivate::QStringList_filter(const QStringList *that, const QString
res << that->at(i);
return res;
}
#endif
template<typename T>
static bool stringList_contains(const QStringList &stringList, const T &str, Qt::CaseSensitivity cs)
@ -466,6 +488,7 @@ QStringList QtPrivate::QStringList_filter(const QStringList *that, const QRegula
}
#endif // QT_CONFIG(regularexpression)
#if QT_STRINGVIEW_LEVEL < 2
/*!
\fn QStringList &QStringList::replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs)
@ -481,12 +504,41 @@ QStringList QtPrivate::QStringList_filter(const QStringList *that, const QRegula
\sa QString::replace()
*/
/*!
\fn QStringList &QStringList::replaceInStrings(QStringView before, const QString &after, Qt::CaseSensitivity cs)
\overload
\since 5.14
*/
/*!
\fn QStringList &QStringList::replaceInStrings(const QString &before, QStringView after, Qt::CaseSensitivity cs)
\overload
\since 5.14
*/
#endif
/*!
\fn QStringList &QStringList::replaceInStrings(QStringView before, QStringView after, Qt::CaseSensitivity cs)
\overload
\since 5.14
*/
void QtPrivate::QStringList_replaceInStrings(QStringList *that, QStringView before,
QStringView after, Qt::CaseSensitivity cs)
{
for (int i = 0; i < that->size(); ++i)
(*that)[i].replace(before.data(), before.length(), after.data(), after.length(), cs);
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
/// Not really needed anymore, but kept for binary compatibility
void QtPrivate::QStringList_replaceInStrings(QStringList *that, const QString &before,
const QString &after, Qt::CaseSensitivity cs)
{
for (int i = 0; i < that->size(); ++i)
(*that)[i].replace(before, after, cs);
}
#endif
#ifndef QT_NO_REGEXP
@ -561,6 +613,7 @@ static int accumulatedSize(const QStringList &list, int seplen)
return result;
}
#if QT_STRINGVIEW_LEVEL < 2
/*!
\fn QString QStringList::join(const QString &separator) const
@ -570,6 +623,7 @@ static int accumulatedSize(const QStringList &list, int seplen)
\sa QString::split()
*/
#endif
/*!
\fn QString QStringList::join(QChar separator) const
@ -614,6 +668,16 @@ QString QtPrivate::QStringList_join(const QStringList &list, QLatin1String sep)
return result;
}
/*!
\fn QString QStringList::join(QStringView separator) const
\overload
\since 5.14
*/
QString QtPrivate::QStringList_join(const QStringList *that, QStringView sep)
{
return QStringList_join(that, sep.data(), sep.length());
}
/*!
\fn QStringList QStringList::operator+(const QStringList &other) const

View File

@ -73,12 +73,21 @@ public:
inline void sort(Qt::CaseSensitivity cs = Qt::CaseSensitive);
inline int removeDuplicates();
#if QT_STRINGVIEW_LEVEL < 2
inline QString join(const QString &sep) const;
#endif
inline QString join(QStringView sep) const;
inline QString join(QLatin1String sep) const;
inline QString join(QChar sep) const;
inline QStringList filter(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
inline QStringList &replaceInStrings(QStringView before, QStringView after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
#if QT_STRINGVIEW_LEVEL < 2
inline QStringList filter(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
inline QStringList &replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
inline QStringList &replaceInStrings(const QString &before, QStringView after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
inline QStringList &replaceInStrings(QStringView before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
#endif
#ifndef QT_NO_REGEXP
inline QStringList filter(const QRegExp &rx) const;
@ -163,18 +172,27 @@ inline const QStringList *QListSpecialMethods<QString>::self() const
namespace QtPrivate {
void Q_CORE_EXPORT QStringList_sort(QStringList *that, Qt::CaseSensitivity cs);
int Q_CORE_EXPORT QStringList_removeDuplicates(QStringList *that);
QString Q_CORE_EXPORT QStringList_join(const QStringList *that, QStringView sep);
QString Q_CORE_EXPORT QStringList_join(const QStringList *that, const QChar *sep, int seplen);
Q_CORE_EXPORT QString QStringList_join(const QStringList &list, QLatin1String sep);
QStringList Q_CORE_EXPORT QStringList_filter(const QStringList *that, QStringView str,
Qt::CaseSensitivity cs);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QStringList Q_CORE_EXPORT QStringList_filter(const QStringList *that, const QString &str,
Qt::CaseSensitivity cs);
#endif
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
bool Q_CORE_EXPORT QStringList_contains(const QStringList *that, const QString &str, Qt::CaseSensitivity cs);
#endif
bool Q_CORE_EXPORT QStringList_contains(const QStringList *that, QStringView str, Qt::CaseSensitivity cs);
bool Q_CORE_EXPORT QStringList_contains(const QStringList *that, QLatin1String str, Qt::CaseSensitivity cs);
void Q_CORE_EXPORT QStringList_replaceInStrings(QStringList *that, QStringView before, QStringView after,
Qt::CaseSensitivity cs);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void Q_CORE_EXPORT QStringList_replaceInStrings(QStringList *that, const QString &before, const QString &after,
Qt::CaseSensitivity cs);
#endif
#ifndef QT_NO_REGEXP
void Q_CORE_EXPORT QStringList_replaceInStrings(QStringList *that, const QRegExp &rx, const QString &after);
@ -203,10 +221,17 @@ inline int QListSpecialMethods<QString>::removeDuplicates()
return QtPrivate::QStringList_removeDuplicates(self());
}
#if QT_STRINGVIEW_LEVEL < 2
inline QString QListSpecialMethods<QString>::join(const QString &sep) const
{
return QtPrivate::QStringList_join(self(), sep.constData(), sep.length());
}
#endif
inline QString QListSpecialMethods<QString>::join(QStringView sep) const
{
return QtPrivate::QStringList_join(self(), sep);
}
QString QListSpecialMethods<QString>::join(QLatin1String sep) const
{
@ -218,10 +243,17 @@ inline QString QListSpecialMethods<QString>::join(QChar sep) const
return QtPrivate::QStringList_join(self(), &sep, 1);
}
inline QStringList QListSpecialMethods<QString>::filter(QStringView str, Qt::CaseSensitivity cs) const
{
return QtPrivate::QStringList_filter(self(), str, cs);
}
#if QT_STRINGVIEW_LEVEL < 2
inline QStringList QListSpecialMethods<QString>::filter(const QString &str, Qt::CaseSensitivity cs) const
{
return QtPrivate::QStringList_filter(self(), str, cs);
}
#endif
#if QT_STRINGVIEW_LEVEL < 2
inline bool QStringList::contains(const QString &str, Qt::CaseSensitivity cs) const
@ -240,12 +272,32 @@ inline bool QStringList::contains(QStringView str, Qt::CaseSensitivity cs) const
return QtPrivate::QStringList_contains(this, str, cs);
}
inline QStringList &QListSpecialMethods<QString>::replaceInStrings(QStringView before, QStringView after, Qt::CaseSensitivity cs)
{
QtPrivate::QStringList_replaceInStrings(self(), before, after, cs);
return *self();
}
#if QT_STRINGVIEW_LEVEL < 2
inline QStringList &QListSpecialMethods<QString>::replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs)
{
QtPrivate::QStringList_replaceInStrings(self(), before, after, cs);
return *self();
}
inline QStringList &QListSpecialMethods<QString>::replaceInStrings(QStringView before, const QString &after, Qt::CaseSensitivity cs)
{
QtPrivate::QStringList_replaceInStrings(self(), before, qToStringViewIgnoringNull(after), cs);
return *self();
}
inline QStringList &QListSpecialMethods<QString>::replaceInStrings(const QString &before, QStringView after, Qt::CaseSensitivity cs)
{
QtPrivate::QStringList_replaceInStrings(self(), QStringView(before), after, cs);
return *self();
}
#endif
inline QStringList operator+(const QList<QString> &one, const QStringList &other)
{
QStringList n = one;

View File

@ -259,6 +259,12 @@ void tst_QStringList::filter()
list5 = list5.filter( QRegularExpression("[i]ll") );
list6 << "Bill Gates" << "Bill Clinton";
QCOMPARE( list5, list6 );
QStringList list7, list8;
list7 << "Bill Gates" << "Joe Blow" << "Bill Clinton";
list7 = list7.filter( QStringView(QString("Bill")) );
list8 << "Bill Gates" << "Bill Clinton";
QCOMPARE( list7, list8 );
}
void tst_QStringList::sort()
@ -316,6 +322,16 @@ void tst_QStringList::replaceInStrings()
list10 << "Bill Clinton" << "Bill Gates";
list9.replaceInStrings( QRegularExpression("^(.*), (.*)$"), "\\2 \\1" );
QCOMPARE( list9, list10 );
QStringList list11, list12, list13, list14;
list11 << "alpha" << "beta" << "gamma" << "epsilon";
list12 << "alpha" << "beta" << "gamma" << "epsilon";
list13 << "alpha" << "beta" << "gamma" << "epsilon";
list11.replaceInStrings( QStringView(QString("a")), QStringView(QString("o")) );
list12.replaceInStrings( QStringView(QString("a")), QString("o") );
list13.replaceInStrings( QString("a"), QStringView(QString("o")) );
list14 << "olpho" << "beto" << "gommo" << "epsilon";
QCOMPARE( list11, list12 );
}
void tst_QStringList::contains()
@ -427,6 +443,7 @@ void tst_QStringList::join() const
QCOMPARE(input.join(separator), expectedResult);
QCOMPARE(input.join(QLatin1String(separator.toLatin1())), expectedResult);
QCOMPARE(input.join(QStringView(separator)), expectedResult);
}
void tst_QStringList::join_data() const