diff options
author | Jędrzej Nowacki <jedrzej.nowacki@digia.com> | 2014-05-14 16:56:10 +0200 |
---|---|---|
committer | Jędrzej Nowacki <jedrzej.nowacki@digia.com> | 2014-08-06 14:04:40 +0200 |
commit | 1fceabe8c7b5e99509a67aec8303241b6ba4099a (patch) | |
tree | 246aec96493ff9995d756ee474c00027e2a89ef5 /src/corelib | |
parent | fc16dcce9df51d4d65065f8f9a8b9525856cb5ae (diff) |
Implement QStringRef::split
[ChangeLog][QtCore] Added the QStringRef::split() function
Change-Id: I28709c9761785dea7be4e7d621ecf4e1ae007a72
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/tools/qstring.cpp | 35 | ||||
-rw-r--r-- | src/corelib/tools/qstring.h | 5 |
2 files changed, 38 insertions, 2 deletions
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index d3cb62429f..8b23f33735 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -6625,8 +6625,8 @@ QString QString::number(double n, char f, int prec) } namespace { -template<class ResultList, typename MidMethod, typename Separator> -static ResultList splitString(const QString &source, MidMethod mid, const Separator &sep, +template<class ResultList, class StringSource, typename MidMethod, typename Separtor> +static ResultList splitString(const StringSource &source, MidMethod mid, const Separtor &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs, const int separatorSize) { ResultList list; @@ -6707,6 +6707,37 @@ QVector<QStringRef> QString::splitRef(QChar sep, SplitBehavior behavior, Qt::Cas return splitString<QVector<QStringRef> >(*this, &QString::midRef, sep, behavior, cs, 1); } +/*! + Splits the string into substrings references wherever \a sep occurs, and + returns the list of those strings. If \a sep does not match + anywhere in the string, split() returns a single-element vector + containing this string reference. + + \a cs specifies whether \a sep should be matched case + sensitively or case insensitively. + + If \a behavior is QString::SkipEmptyParts, empty entries don't + appear in the result. By default, empty entries are kept. + + \note All references are valid as long this string is alive. Destroying this + string will cause all references be dangling pointers. + + \since 5.4 +*/ +QVector<QStringRef> QStringRef::split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const +{ + return splitString<QVector<QStringRef> >(*this, &QStringRef::mid, sep, behavior, cs, sep.size()); +} + +/*! + \overload + \since 5.4 +*/ +QVector<QStringRef> QStringRef::split(QChar sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const +{ + return splitString<QVector<QStringRef> >(*this, &QStringRef::mid, sep, behavior, cs, 1); +} + #ifndef QT_NO_REGEXP namespace { template<class ResultList, typename MidMethod> diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 21b64f5006..349588911b 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -1305,6 +1305,11 @@ public: int count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; int count(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + QVector<QStringRef> split(const QString &sep, QString::SplitBehavior behavior = QString::KeepEmptyParts, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_REQUIRED_RESULT; + QVector<QStringRef> split(QChar sep, QString::SplitBehavior behavior = QString::KeepEmptyParts, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_REQUIRED_RESULT; + QStringRef left(int n) const Q_REQUIRED_RESULT; QStringRef right(int n) const Q_REQUIRED_RESULT; QStringRef mid(int pos, int n = -1) const Q_REQUIRED_RESULT; |