diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-05-22 16:47:19 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-06-01 20:43:21 +0200 |
commit | aac39167b7903eea886d8638ab84296d4e8952f1 (patch) | |
tree | 5a44a583ad44169ea5e3fa5d6b8209d1e73759f7 /src/corelib | |
parent | 4f1ebf666e36020c501e6d3b20d70320b45ab2ec (diff) |
Add QStringView::split() methods
Since QString::split() is not going away in Qt 6, we should aim
to provide API symmetry here, and ease porting existing code from
QString(Ref) to use QStringView.
This is easier than having to port everything to use tokenize() at
the same time. tokenize() will however lead to better performance
and thus should be preferred.
Change-Id: I1eb43300a90167c6e9389ab56f416f2bf7edf506
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/text/qstring.cpp | 59 | ||||
-rw-r--r-- | src/corelib/text/qstringview.h | 16 |
2 files changed, 68 insertions, 7 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 01b4337c2a..e026829a3d 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -7197,7 +7197,7 @@ static ResultList splitString(const StringSource &source, QStringView sep, extra = (sep.size() == 0 ? 1 : 0); } if (start != source.size() || behavior == Qt::KeepEmptyParts) - list.append(source.mid(start, -1)); + list.append(source.mid(start)); return list; } @@ -7381,10 +7381,37 @@ QVector<QStringRef> QStringRef::split(QChar sep, QString::SplitBehavior behavior } #endif + +/*! + \fn QList<QStringView> QStringView::split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const + \fn QList<QStringView> QStringView::split(QStringView sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const + + + Splits the string into substring references wherever \a sep occurs, and + returns the list of those strings. + + See QString::split() for how \a sep, \a behavior and \a cs interact to form + the result. + + \note All references are valid as long this string is alive. Destroying this + string will cause all references to be dangling pointers. + + \since 6.0 +*/ +QList<QStringView> QStringView::split(QStringView sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const +{ + return splitString<QVector<QStringView>>(QStringView(*this), sep, behavior, cs); +} + +QList<QStringView> QStringView::split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const +{ + return split(QStringView(&sep, 1), behavior, cs); +} + #if QT_CONFIG(regularexpression) namespace { -template<class ResultList, typename MidMethod> -static ResultList splitString(const QString &source, MidMethod mid, const QRegularExpression &re, +template<class ResultList, typename String> +static ResultList splitString(const String &source, const QRegularExpression &re, Qt::SplitBehavior behavior) { ResultList list; @@ -7400,12 +7427,12 @@ static ResultList splitString(const QString &source, MidMethod mid, const QRegul QRegularExpressionMatch match = iterator.next(); end = match.capturedStart(); if (start != end || behavior == Qt::KeepEmptyParts) - list.append((source.*mid)(start, end - start)); + list.append(source.mid(start, end - start)); start = match.capturedEnd(); } if (start != source.size() || behavior == Qt::KeepEmptyParts) - list.append((source.*mid)(start, -1)); + list.append(source.mid(start)); return list; } @@ -7440,7 +7467,7 @@ static ResultList splitString(const QString &source, MidMethod mid, const QRegul */ QStringList QString::split(const QRegularExpression &re, Qt::SplitBehavior behavior) const { - return splitString<QStringList>(*this, &QString::mid, re, behavior); + return splitString<QStringList>(*this, re, behavior); } # if QT_DEPRECATED_SINCE(5, 15) @@ -7471,7 +7498,25 @@ QStringList QString::split(const QRegularExpression &re, SplitBehavior behavior) */ QVector<QStringRef> QString::splitRef(const QRegularExpression &re, Qt::SplitBehavior behavior) const { - return splitString<QVector<QStringRef> >(*this, &QString::midRef, re, behavior); + return splitString<QVector<QStringRef> >(QStringRef(this), re, behavior); +} + +/*! + \since 6.0 + + Splits the string into substring views wherever the regular expression + \a re matches, and returns the list of those strings. If \a re + does not match anywhere in the string, splitRef() returns a + single-element vector containing this string reference. + + \note All references are valid as long this string is alive. Destroying this + string will cause all references to be dangling pointers. + + \sa split() QStringRef +*/ +QList<QStringView> QStringView::split(const QRegularExpression &re, Qt::SplitBehavior behavior) const +{ + return splitString<QList<QStringView>>(*this, re, behavior); } # if QT_DEPRECATED_SINCE(5, 15) diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index 5890515652..4e780628cc 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -73,6 +73,7 @@ QT_BEGIN_NAMESPACE class QString; class QStringRef; class QStringView; +class QRegularExpression; namespace QtPrivate { template <typename Char> @@ -339,6 +340,21 @@ public: Q_REQUIRED_RESULT inline int toWCharArray(wchar_t *array) const; // defined in qstring.h + + Q_REQUIRED_RESULT Q_CORE_EXPORT + QList<QStringView> split(QStringView sep, + Qt::SplitBehavior behavior = Qt::KeepEmptyParts, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + Q_REQUIRED_RESULT Q_CORE_EXPORT + QList<QStringView> split(QChar sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + +#if QT_CONFIG(regularexpression) + Q_REQUIRED_RESULT Q_CORE_EXPORT + QList<QStringView> split(const QRegularExpression &sep, + Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const; +#endif + // // STL compatibility API: // |