From f2b8412f91198ef1d00c06a05183de1768b85757 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Wed, 23 Sep 2020 09:55:54 +0200 Subject: Add QStringView::split() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The implementation has to live in qstringlist.h and qregularexpression.h, as those classes are only forward declared in qstring.h. Task-number: QTBUG-86516 Change-Id: Ia9b3ff48999d1c2e7df905191ee192764b309d08 Reviewed-by: MÃ¥rten Nordheim --- src/corelib/text/qregularexpression.h | 14 ++++++++++++ src/corelib/text/qstringlist.h | 25 ++++++++++++++++++++++ src/corelib/text/qstringview.cpp | 17 +++++++++++++++ src/corelib/text/qstringview.h | 14 ++++++++++++ .../qstringapisymmetry/tst_qstringapisymmetry.cpp | 5 +++++ 5 files changed, 75 insertions(+) diff --git a/src/corelib/text/qregularexpression.h b/src/corelib/text/qregularexpression.h index 4fa258b080..e00979527d 100644 --- a/src/corelib/text/qregularexpression.h +++ b/src/corelib/text/qregularexpression.h @@ -278,6 +278,20 @@ private: Q_DECLARE_SHARED(QRegularExpressionMatchIterator) +// implementation here, so we have all required classes +inline +QList QStringView::split(const QRegularExpression &sep, Qt::SplitBehavior behavior) const +{ + Q_ASSERT(int(m_size) == m_size); + QString s = QString::fromRawData(data(), int(m_size)); + const auto split = s.splitRef(sep, behavior); + QList result; + result.reserve(split.size()); + for (const QStringRef &r : split) + result.append(r); + return result; +} + QT_END_NAMESPACE #endif // QREGULAREXPRESSION_H diff --git a/src/corelib/text/qstringlist.h b/src/corelib/text/qstringlist.h index 9ea379bb92..02b129a3cb 100644 --- a/src/corelib/text/qstringlist.h +++ b/src/corelib/text/qstringlist.h @@ -384,6 +384,31 @@ inline int QStringList::lastIndexOf(const QRegularExpression &rx, int from) cons #endif // QT_CONFIG(regularexpression) #endif // Q_QDOC +// those methods need to be here, so they can be implemented inline +inline +QList QStringView::split(QStringView sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const +{ + Q_ASSERT(int(m_size) == m_size); + QString s = QString::fromRawData(data(), int(m_size)); + const auto split = s.splitRef(sep.toString(), behavior, cs); + QList result; + for (const QStringRef &r : split) + result.append(QStringView(m_data + r.position(), r.size())); + return result; +} + +inline +QList QStringView::split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const +{ + Q_ASSERT(int(m_size) == m_size); + QString s = QString::fromRawData(data(), int(m_size)); + const auto split = s.splitRef(sep, behavior, cs); + QList result; + for (const QStringRef &r : split) + result.append(QStringView(m_data + r.position(), r.size())); + return result; +} + QT_END_NAMESPACE #endif // QSTRINGLIST_H diff --git a/src/corelib/text/qstringview.cpp b/src/corelib/text/qstringview.cpp index 1451dfed44..f3712a412a 100644 --- a/src/corelib/text/qstringview.cpp +++ b/src/corelib/text/qstringview.cpp @@ -898,6 +898,23 @@ QT_BEGIN_NAMESPACE \sa QString::isValidUtf16() */ +/*! + \fn QList QStringView::split(QStringView sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const; + \fn QList QStringView::split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const; + \fn QList QStringView::split(const QRegularExpression &sep, Qt::SplitBehavior behavior) const; + + Splits the string into substrings 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 This method has been added in 5.15.2 to simplify writing code that is portable + between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5. + + \since 5.15.2 +*/ + /*! \fn QStringView::toWCharArray(wchar_t *array) const \since 5.14 diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index e599e5a8c2..5e561d88a8 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -55,6 +55,7 @@ QT_BEGIN_NAMESPACE class QString; class QStringRef; +class QRegularExpression; namespace QtPrivate { template @@ -323,6 +324,19 @@ public: Q_REQUIRED_RESULT inline int toWCharArray(wchar_t *array) const; // defined in qstring.h + Q_REQUIRED_RESULT inline + QList split(QStringView sep, + Qt::SplitBehavior behavior = Qt::KeepEmptyParts, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + Q_REQUIRED_RESULT inline + QList split(QChar sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + +#if QT_CONFIG(regularexpression) + Q_REQUIRED_RESULT inline + QList split(const QRegularExpression &sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const; +#endif + // // STL compatibility API: // diff --git a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp index 35546c2968..73ab353978 100644 --- a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -507,6 +507,11 @@ private Q_SLOTS: void split_QStringRef_QChar_data() { split_data(false); } void split_QStringRef_QChar() { split_impl(); } + void split_QStringView_QString_data() { split_data(); } + void split_QStringView_QString() { split_impl(); } + void split_QStringView_QChar_data() { split_data(false); } + void split_QStringView_QChar() { split_impl(); } + private: void mid_data(); template void mid_impl(); -- cgit v1.2.1