QRegularExpression: support for QStringList overloads
Change-Id: Ia9017348742e41187684185d04b56d27edd383b5 Reviewed-by: Lars Knoll <lars.knoll@nokia.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
054b69c963
commit
8659495202
@ -144,6 +144,21 @@ Widget::Widget(QWidget *parent)
|
|||||||
list.replaceInStrings(QRegExp("^(.*), (.*)$"), "\\2 \\1");
|
list.replaceInStrings(QRegExp("^(.*), (.*)$"), "\\2 \\1");
|
||||||
// list == ["Bill Clinton", "Bill Murray"]
|
// list == ["Bill Clinton", "Bill Murray"]
|
||||||
//! [15]
|
//! [15]
|
||||||
|
|
||||||
|
list.clear();
|
||||||
|
//! [16]
|
||||||
|
list << "alpha" << "beta" << "gamma" << "epsilon";
|
||||||
|
list.replaceInStrings(QRegularExpression("^a"), "o");
|
||||||
|
// list == ["olpha", "beta", "gamma", "epsilon"]
|
||||||
|
//! [16]
|
||||||
|
|
||||||
|
list.clear();
|
||||||
|
//! [17]
|
||||||
|
list << "Bill Clinton" << "Murray, Bill";
|
||||||
|
list.replaceInStrings(QRegularExpression("^(.*), (.*)$"), "\\2 \\1");
|
||||||
|
// list == ["Bill Clinton", "Bill Murray"]
|
||||||
|
//! [17]
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
|
@ -41,6 +41,7 @@
|
|||||||
|
|
||||||
#include <qstringlist.h>
|
#include <qstringlist.h>
|
||||||
#include <qset.h>
|
#include <qset.h>
|
||||||
|
#include <qregularexpression.h>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
@ -304,6 +305,28 @@ QStringList QtPrivate::QStringList_filter(const QStringList *that, const QRegExp
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef QT_BOOTSTRAPPED
|
||||||
|
#ifndef QT_NO_REGEXP
|
||||||
|
/*!
|
||||||
|
\fn QStringList QStringList::filter(const QRegularExpression &re) const
|
||||||
|
\overload
|
||||||
|
\since 5.0
|
||||||
|
|
||||||
|
Returns a list of all the strings that match the regular
|
||||||
|
expression \a re.
|
||||||
|
*/
|
||||||
|
QStringList QtPrivate::QStringList_filter(const QStringList *that, const QRegularExpression &re)
|
||||||
|
{
|
||||||
|
QStringList res;
|
||||||
|
for (int i = 0; i < that->size(); ++i) {
|
||||||
|
if (that->at(i).contains(re))
|
||||||
|
res << that->at(i);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
#endif // QT_NO_REGEXP
|
||||||
|
#endif // QT_BOOTSTRAPPED
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\fn QStringList &QStringList::replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs)
|
\fn QStringList &QStringList::replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs)
|
||||||
|
|
||||||
@ -357,6 +380,39 @@ void QtPrivate::QStringList_replaceInStrings(QStringList *that, const QRegExp &r
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef QT_BOOTSTRAPPED
|
||||||
|
#ifndef QT_NO_REGEXP
|
||||||
|
/*!
|
||||||
|
\fn QStringList &QStringList::replaceInStrings(const QRegularExpression &re, const QString &after)
|
||||||
|
\overload
|
||||||
|
\since 5.0
|
||||||
|
|
||||||
|
Replaces every occurrence of the regular expression \a re, in each of the
|
||||||
|
string lists's strings, with \a after. Returns a reference to the string
|
||||||
|
list.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
\snippet doc/src/snippets/qstringlist/main.cpp 5
|
||||||
|
\snippet doc/src/snippets/qstringlist/main.cpp 16
|
||||||
|
|
||||||
|
For regular expressions that contain capturing groups,
|
||||||
|
occurrences of \b{\\1}, \b{\\2}, ..., in \a after are
|
||||||
|
replaced with the string captured by the corresponding capturing group.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
\snippet doc/src/snippets/qstringlist/main.cpp 5
|
||||||
|
\snippet doc/src/snippets/qstringlist/main.cpp 17
|
||||||
|
*/
|
||||||
|
void QtPrivate::QStringList_replaceInStrings(QStringList *that, const QRegularExpression &re, const QString &after)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < that->size(); ++i)
|
||||||
|
(*that)[i].replace(re, after);
|
||||||
|
}
|
||||||
|
#endif // QT_NO_REGEXP
|
||||||
|
#endif // QT_BOOTSTRAPPED
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\fn QString QStringList::join(const QString &separator) const
|
\fn QString QStringList::join(const QString &separator) const
|
||||||
|
|
||||||
@ -542,6 +598,67 @@ int QtPrivate::QStringList_lastIndexOf(const QStringList *that, QRegExp &rx, int
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef QT_BOOTSTRAPPED
|
||||||
|
#ifndef QT_NO_REGEXP
|
||||||
|
/*!
|
||||||
|
\fn int QStringList::indexOf(const QRegularExpression &re, int from) const
|
||||||
|
\overload
|
||||||
|
\since 5.0
|
||||||
|
|
||||||
|
Returns the index position of the first match of \a re in
|
||||||
|
the list, searching forward from index position \a from. Returns
|
||||||
|
-1 if no item matched.
|
||||||
|
|
||||||
|
\sa lastIndexOf()
|
||||||
|
*/
|
||||||
|
int QtPrivate::QStringList_indexOf(const QStringList *that, const QRegularExpression &re, int from)
|
||||||
|
{
|
||||||
|
if (from < 0)
|
||||||
|
from = qMax(from + that->size(), 0);
|
||||||
|
|
||||||
|
QString exactPattern = QLatin1String("\\A(?:") + re.pattern() + QLatin1String(")\\z");
|
||||||
|
QRegularExpression exactRe(exactPattern, re.patternOptions());
|
||||||
|
|
||||||
|
for (int i = from; i < that->size(); ++i) {
|
||||||
|
QRegularExpressionMatch m = exactRe.match(that->at(i));
|
||||||
|
if (m.hasMatch())
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\fn int QStringList::lastIndexOf(const QRegularExpression &re, int from) const
|
||||||
|
\overload
|
||||||
|
\since 5.0
|
||||||
|
|
||||||
|
Returns the index position of the last exact match of \a re 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.
|
||||||
|
|
||||||
|
\sa indexOf()
|
||||||
|
*/
|
||||||
|
int QtPrivate::QStringList_lastIndexOf(const QStringList *that, const QRegularExpression &re, int from)
|
||||||
|
{
|
||||||
|
if (from < 0)
|
||||||
|
from += that->size();
|
||||||
|
else if (from >= that->size())
|
||||||
|
from = that->size() - 1;
|
||||||
|
|
||||||
|
QString exactPattern = QLatin1String("\\A(?:") + re.pattern() + QLatin1String(")\\z");
|
||||||
|
QRegularExpression exactRe(exactPattern, re.patternOptions());
|
||||||
|
|
||||||
|
for (int i = from; i >= 0; --i) {
|
||||||
|
QRegularExpressionMatch m = exactRe.match(that->at(i));
|
||||||
|
if (m.hasMatch())
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
#endif // QT_NO_REGEXP
|
||||||
|
#endif // QT_BOOTSTRAPPED
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\fn int QStringList::indexOf(const QString &value, int from = 0) const
|
\fn int QStringList::indexOf(const QString &value, int from = 0) const
|
||||||
|
|
||||||
|
@ -55,6 +55,7 @@ QT_BEGIN_NAMESPACE
|
|||||||
|
|
||||||
|
|
||||||
class QRegExp;
|
class QRegExp;
|
||||||
|
class QRegularExpression;
|
||||||
|
|
||||||
typedef QListIterator<QString> QStringListIterator;
|
typedef QListIterator<QString> QStringListIterator;
|
||||||
typedef QMutableListIterator<QString> QMutableStringListIterator;
|
typedef QMutableListIterator<QString> QMutableStringListIterator;
|
||||||
@ -95,6 +96,16 @@ public:
|
|||||||
inline int indexOf(QRegExp &rx, int from = 0) const;
|
inline int indexOf(QRegExp &rx, int from = 0) const;
|
||||||
inline int lastIndexOf(QRegExp &rx, int from = -1) const;
|
inline int lastIndexOf(QRegExp &rx, int from = -1) const;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef QT_BOOTSTRAPPED
|
||||||
|
#ifndef QT_NO_REGEXP
|
||||||
|
inline QStringList filter(const QRegularExpression &re) const;
|
||||||
|
inline QStringList &replaceInStrings(const QRegularExpression &re, const QString &after);
|
||||||
|
inline int indexOf(const QRegularExpression &re, int from = 0) const;
|
||||||
|
inline int lastIndexOf(const QRegularExpression &re, int from = -1) const;
|
||||||
|
#endif // QT_NO_REGEXP
|
||||||
|
#endif // QT_BOOTSTRAPPED
|
||||||
|
|
||||||
#if !defined(Q_NO_USING_KEYWORD)
|
#if !defined(Q_NO_USING_KEYWORD)
|
||||||
using QList<QString>::indexOf;
|
using QList<QString>::indexOf;
|
||||||
using QList<QString>::lastIndexOf;
|
using QList<QString>::lastIndexOf;
|
||||||
@ -127,6 +138,15 @@ namespace QtPrivate {
|
|||||||
int Q_CORE_EXPORT QStringList_indexOf(const QStringList *that, QRegExp &rx, int from);
|
int Q_CORE_EXPORT QStringList_indexOf(const QStringList *that, QRegExp &rx, int from);
|
||||||
int Q_CORE_EXPORT QStringList_lastIndexOf(const QStringList *that, QRegExp &rx, int from);
|
int Q_CORE_EXPORT QStringList_lastIndexOf(const QStringList *that, QRegExp &rx, int from);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef QT_BOOTSTRAPPED
|
||||||
|
#ifndef QT_NO_REGEXP
|
||||||
|
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);
|
||||||
|
int Q_CORE_EXPORT QStringList_indexOf(const QStringList *that, const QRegularExpression &re, int from);
|
||||||
|
int Q_CORE_EXPORT QStringList_lastIndexOf(const QStringList *that, const QRegularExpression &re, int from);
|
||||||
|
#endif // QT_NO_REGEXP
|
||||||
|
#endif // QT_BOOTSTRAPPED
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void QStringList::sort()
|
inline void QStringList::sort()
|
||||||
@ -193,6 +213,30 @@ inline int QStringList::lastIndexOf(QRegExp &rx, int from) const
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef QT_BOOTSTRAPPED
|
||||||
|
#ifndef QT_NO_REGEXP
|
||||||
|
inline QStringList &QStringList::replaceInStrings(const QRegularExpression &rx, const QString &after)
|
||||||
|
{
|
||||||
|
QtPrivate::QStringList_replaceInStrings(this, rx, after);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline QStringList QStringList::filter(const QRegularExpression &rx) const
|
||||||
|
{
|
||||||
|
return QtPrivate::QStringList_filter(this, rx);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int QStringList::indexOf(const QRegularExpression &rx, int from) const
|
||||||
|
{
|
||||||
|
return QtPrivate::QStringList_indexOf(this, rx, from);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int QStringList::lastIndexOf(const QRegularExpression &rx, int from) const
|
||||||
|
{
|
||||||
|
return QtPrivate::QStringList_lastIndexOf(this, rx, from);
|
||||||
|
}
|
||||||
|
#endif // QT_NO_REGEXP
|
||||||
|
#endif // QT_BOOTSTRAPPED
|
||||||
|
|
||||||
#ifndef QT_NO_DATASTREAM
|
#ifndef QT_NO_DATASTREAM
|
||||||
inline QDataStream &operator>>(QDataStream &in, QStringList &list)
|
inline QDataStream &operator>>(QDataStream &in, QStringList &list)
|
||||||
|
@ -41,6 +41,7 @@
|
|||||||
|
|
||||||
#include <QtTest/QtTest>
|
#include <QtTest/QtTest>
|
||||||
#include <qregexp.h>
|
#include <qregexp.h>
|
||||||
|
#include <qregularexpression.h>
|
||||||
#include <qstringlist.h>
|
#include <qstringlist.h>
|
||||||
|
|
||||||
class tst_QStringList : public QObject
|
class tst_QStringList : public QObject
|
||||||
@ -72,18 +73,37 @@ void tst_QStringList::indexOf_regExp()
|
|||||||
{
|
{
|
||||||
QStringList list;
|
QStringList list;
|
||||||
list << "harald" << "trond" << "vohi" << "harald";
|
list << "harald" << "trond" << "vohi" << "harald";
|
||||||
|
{
|
||||||
|
QRegExp re(".*o.*");
|
||||||
|
|
||||||
QRegExp re(".*o.*");
|
QCOMPARE(list.indexOf(re), 1);
|
||||||
|
QCOMPARE(list.indexOf(re, 2), 2);
|
||||||
|
QCOMPARE(list.indexOf(re, 3), -1);
|
||||||
|
|
||||||
QCOMPARE(list.indexOf(re), 1);
|
QCOMPARE(list.indexOf(QRegExp(".*x.*")), -1);
|
||||||
QCOMPARE(list.indexOf(re, 2), 2);
|
QCOMPARE(list.indexOf(re, -1), -1);
|
||||||
QCOMPARE(list.indexOf(re, 3), -1);
|
QCOMPARE(list.indexOf(re, -3), 1);
|
||||||
|
QCOMPARE(list.indexOf(re, -9999), 1);
|
||||||
|
QCOMPARE(list.indexOf(re, 9999), -1);
|
||||||
|
|
||||||
QCOMPARE(list.indexOf(QRegExp(".*x.*")), -1);
|
QCOMPARE(list.indexOf(QRegExp("[aeiou]")), -1);
|
||||||
QCOMPARE(list.indexOf(re, -1), -1);
|
}
|
||||||
QCOMPARE(list.indexOf(re, -3), 1);
|
|
||||||
QCOMPARE(list.indexOf(re, -9999), 1);
|
{
|
||||||
QCOMPARE(list.indexOf(re, 9999), -1);
|
QRegularExpression re(".*o.*");
|
||||||
|
|
||||||
|
QCOMPARE(list.indexOf(re), 1);
|
||||||
|
QCOMPARE(list.indexOf(re, 2), 2);
|
||||||
|
QCOMPARE(list.indexOf(re, 3), -1);
|
||||||
|
|
||||||
|
QCOMPARE(list.indexOf(QRegularExpression(".*x.*")), -1);
|
||||||
|
QCOMPARE(list.indexOf(re, -1), -1);
|
||||||
|
QCOMPARE(list.indexOf(re, -3), 1);
|
||||||
|
QCOMPARE(list.indexOf(re, -9999), 1);
|
||||||
|
QCOMPARE(list.indexOf(re, 9999), -1);
|
||||||
|
|
||||||
|
QCOMPARE(list.indexOf(QRegularExpression("[aeiou]")), -1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_QStringList::lastIndexOf_regExp()
|
void tst_QStringList::lastIndexOf_regExp()
|
||||||
@ -91,17 +111,39 @@ void tst_QStringList::lastIndexOf_regExp()
|
|||||||
QStringList list;
|
QStringList list;
|
||||||
list << "harald" << "trond" << "vohi" << "harald";
|
list << "harald" << "trond" << "vohi" << "harald";
|
||||||
|
|
||||||
QRegExp re(".*o.*");
|
{
|
||||||
|
QRegExp re(".*o.*");
|
||||||
|
|
||||||
|
QCOMPARE(list.lastIndexOf(re), 2);
|
||||||
|
QCOMPARE(list.lastIndexOf(re, 2), 2);
|
||||||
|
QCOMPARE(list.lastIndexOf(re, 1), 1);
|
||||||
|
|
||||||
|
QCOMPARE(list.lastIndexOf(QRegExp(".*x.*")), -1);
|
||||||
|
QCOMPARE(list.lastIndexOf(re, -1), 2);
|
||||||
|
QCOMPARE(list.lastIndexOf(re, -3), 1);
|
||||||
|
QCOMPARE(list.lastIndexOf(re, -9999), -1);
|
||||||
|
QCOMPARE(list.lastIndexOf(re, 9999), 2);
|
||||||
|
|
||||||
|
QCOMPARE(list.lastIndexOf(QRegExp("[aeiou]")), -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
QRegularExpression re(".*o.*");
|
||||||
|
|
||||||
|
QCOMPARE(list.lastIndexOf(re), 2);
|
||||||
|
QCOMPARE(list.lastIndexOf(re, 2), 2);
|
||||||
|
QCOMPARE(list.lastIndexOf(re, 1), 1);
|
||||||
|
|
||||||
|
QCOMPARE(list.lastIndexOf(QRegularExpression(".*x.*")), -1);
|
||||||
|
QCOMPARE(list.lastIndexOf(re, -1), 2);
|
||||||
|
QCOMPARE(list.lastIndexOf(re, -3), 1);
|
||||||
|
QCOMPARE(list.lastIndexOf(re, -9999), -1);
|
||||||
|
QCOMPARE(list.lastIndexOf(re, 9999), 2);
|
||||||
|
|
||||||
|
QCOMPARE(list.lastIndexOf(QRegularExpression("[aeiou]")), -1);
|
||||||
|
}
|
||||||
|
|
||||||
QCOMPARE(list.lastIndexOf(re), 2);
|
|
||||||
QCOMPARE(list.lastIndexOf(re, 2), 2);
|
|
||||||
QCOMPARE(list.lastIndexOf(re, 1), 1);
|
|
||||||
|
|
||||||
QCOMPARE(list.lastIndexOf(QRegExp(".*x.*")), -1);
|
|
||||||
QCOMPARE(list.lastIndexOf(re, -1), 2);
|
|
||||||
QCOMPARE(list.lastIndexOf(re, -3), 1);
|
|
||||||
QCOMPARE(list.lastIndexOf(re, -9999), -1);
|
|
||||||
QCOMPARE(list.lastIndexOf(re, 9999), 2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_QStringList::indexOf()
|
void tst_QStringList::indexOf()
|
||||||
@ -149,6 +191,12 @@ void tst_QStringList::filter()
|
|||||||
list3 = list3.filter( QRegExp("[i]ll") );
|
list3 = list3.filter( QRegExp("[i]ll") );
|
||||||
list4 << "Bill Gates" << "Bill Clinton";
|
list4 << "Bill Gates" << "Bill Clinton";
|
||||||
QCOMPARE( list3, list4 );
|
QCOMPARE( list3, list4 );
|
||||||
|
|
||||||
|
QStringList list5, list6;
|
||||||
|
list5 << "Bill Gates" << "Joe Blow" << "Bill Clinton";
|
||||||
|
list5 = list5.filter( QRegularExpression("[i]ll") );
|
||||||
|
list6 << "Bill Gates" << "Bill Clinton";
|
||||||
|
QCOMPARE( list5, list6 );
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_QStringList::replaceInStrings()
|
void tst_QStringList::replaceInStrings()
|
||||||
@ -170,6 +218,18 @@ void tst_QStringList::replaceInStrings()
|
|||||||
list6 << "Bill Clinton" << "Bill Gates";
|
list6 << "Bill Clinton" << "Bill Gates";
|
||||||
list5.replaceInStrings( QRegExp("^(.*), (.*)$"), "\\2 \\1" );
|
list5.replaceInStrings( QRegExp("^(.*), (.*)$"), "\\2 \\1" );
|
||||||
QCOMPARE( list5, list6 );
|
QCOMPARE( list5, list6 );
|
||||||
|
|
||||||
|
QStringList list7, list8;
|
||||||
|
list7 << "alpha" << "beta" << "gamma" << "epsilon";
|
||||||
|
list7.replaceInStrings( QRegularExpression("^a"), "o" );
|
||||||
|
list8 << "olpha" << "beta" << "gamma" << "epsilon";
|
||||||
|
QCOMPARE( list7, list8 );
|
||||||
|
|
||||||
|
QStringList list9, list10;
|
||||||
|
list9 << "Bill Clinton" << "Gates, Bill";
|
||||||
|
list10 << "Bill Clinton" << "Bill Gates";
|
||||||
|
list9.replaceInStrings( QRegularExpression("^(.*), (.*)$"), "\\2 \\1" );
|
||||||
|
QCOMPARE( list9, list10 );
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_QStringList::contains()
|
void tst_QStringList::contains()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user