From 1fceabe8c7b5e99509a67aec8303241b6ba4099a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Nowacki?= Date: Wed, 14 May 2014 16:56:10 +0200 Subject: Implement QStringRef::split [ChangeLog][QtCore] Added the QStringRef::split() function Change-Id: I28709c9761785dea7be4e7d621ecf4e1ae007a72 Reviewed-by: Thiago Macieira --- src/corelib/tools/qstring.cpp | 35 +++++++++++++++++++++++++++++++++-- src/corelib/tools/qstring.h | 5 +++++ 2 files changed, 38 insertions(+), 2 deletions(-) (limited to 'src/corelib') 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 -static ResultList splitString(const QString &source, MidMethod mid, const Separator &sep, +template +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 QString::splitRef(QChar sep, SplitBehavior behavior, Qt::Cas return splitString >(*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::split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const +{ + return splitString >(*this, &QStringRef::mid, sep, behavior, cs, sep.size()); +} + +/*! + \overload + \since 5.4 +*/ +QVector QStringRef::split(QChar sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const +{ + return splitString >(*this, &QStringRef::mid, sep, behavior, cs, 1); +} + #ifndef QT_NO_REGEXP namespace { template 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 split(const QString &sep, QString::SplitBehavior behavior = QString::KeepEmptyParts, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_REQUIRED_RESULT; + QVector 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; -- cgit v1.2.3