From cafefd1d33349209617d391b3b49c9663590b3c9 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 4 May 2017 19:41:48 +0200 Subject: QStringView/QLatin1String: add trimmed() ... using the same qt_trimmed(), qTrimmed(), Q..::trimmed() split we've been using for all other out-of-line string-view member functions to avoid forcing string-view objects onto the stack for the passing of 'this'. In the test, had to fix nullness not being propagated from a QByteArray to the QLatin1String constructed from it. Probably worth fixing in QLatin1String(QByteArray), too. [ChangeLog][QtCore] Added qTrimmed() free functions. [ChangeLog][QtCore][QLatin1String] Added trimmed() function. Change-Id: I73c18ef87e203f30f7552c10dd5c84223bcfae0e Reviewed-by: Anton Kudryavtsev Reviewed-by: Lars Knoll Reviewed-by: Edward Welbourne --- src/corelib/tools/qstring.cpp | 48 ++++++++++++++++++++++ src/corelib/tools/qstring.h | 2 + src/corelib/tools/qstringalgorithms.h | 2 + src/corelib/tools/qstringview.cpp | 12 ++++++ src/corelib/tools/qstringview.h | 2 + .../qstringapisymmetry/tst_qstringapisymmetry.cpp | 6 ++- 6 files changed, 71 insertions(+), 1 deletion(-) diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index a6295da45e..907de3cb4b 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -5208,6 +5208,41 @@ QString QString::simplified_helper(QString &str) return QStringAlgorithms::simplified_helper(str); } +namespace { + template + StringView qt_trimmed(StringView s) Q_DECL_NOTHROW + { + auto begin = s.begin(); + auto end = s.end(); + QStringAlgorithms::trimmed_helper_positions(begin, end); + return StringView{begin, end}; + } +} + +/*! + \fn QStringView qTrimmed(QStringView s) + \fn QLatin1String qTrimmed(QLatin1String s) + \relates QStringView + \since 5.10 + + Returns \a s with whitespace removed from the start and the end. + + Whitespace means any character for which QChar::isSpace() returns + \c true. This includes the ASCII characters '\\t', '\\n', '\\v', + '\\f', '\\r', and ' '. + + \sa QString::trimmed(), QStringView::trimmed(), QLatin1String::trimmed() +*/ +QStringView qTrimmed(QStringView s) Q_DECL_NOTHROW +{ + return qt_trimmed(s); +} + +QLatin1String qTrimmed(QLatin1String s) Q_DECL_NOTHROW +{ + return qt_trimmed(s); +} + /*! \fn QString QString::trimmed() const @@ -9127,6 +9162,19 @@ QString &QString::setRawData(const QChar *unicode, int size) \sa mid(), left(), right(), chopped(), truncate() */ +/*! + \fn QLatin1String QLatin1String::trimmed() const + \since 5.10 + + Strips leading and trailing whitespace and returns the result. + + Whitespace means any character for which QChar::isSpace() returns + \c true. This includes the ASCII characters '\\t', '\\n', '\\v', + '\\f', '\\r', and ' '. + + \sa qTrimmed() +*/ + /*! \fn bool QLatin1String::operator==(const QString &other) const Returns \c true if this string is equal to string \a other; diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 33ecba6cf8..d5301a396f 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -167,6 +167,8 @@ public: Q_DECL_RELAXED_CONSTEXPR void truncate(int n) { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size = n; } + Q_REQUIRED_RESULT QLatin1String trimmed() const Q_DECL_NOTHROW { return qTrimmed(*this); } + inline bool operator==(const QString &s) const Q_DECL_NOTHROW; inline bool operator!=(const QString &s) const Q_DECL_NOTHROW; inline bool operator>(const QString &s) const Q_DECL_NOTHROW; diff --git a/src/corelib/tools/qstringalgorithms.h b/src/corelib/tools/qstringalgorithms.h index 50c48bfb91..e1b8b90428 100644 --- a/src/corelib/tools/qstringalgorithms.h +++ b/src/corelib/tools/qstringalgorithms.h @@ -69,6 +69,8 @@ Q_REQUIRED_RESULT Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool qEndsWith(QStringView Q_REQUIRED_RESULT Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool qEndsWith(QLatin1String haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW; Q_REQUIRED_RESULT Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool qEndsWith(QLatin1String haystack, QLatin1String needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW; +Q_REQUIRED_RESULT Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QStringView qTrimmed(QStringView s) Q_DECL_NOTHROW; +Q_REQUIRED_RESULT Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QLatin1String qTrimmed(QLatin1String s) Q_DECL_NOTHROW; Q_REQUIRED_RESULT Q_CORE_EXPORT QByteArray qConvertToLatin1(QStringView str); Q_REQUIRED_RESULT Q_CORE_EXPORT QByteArray qConvertToUtf8(QStringView str); diff --git a/src/corelib/tools/qstringview.cpp b/src/corelib/tools/qstringview.cpp index 8d2fc996e9..8eefc6d814 100644 --- a/src/corelib/tools/qstringview.cpp +++ b/src/corelib/tools/qstringview.cpp @@ -664,6 +664,18 @@ QT_BEGIN_NAMESPACE \sa mid(), left(), right(), chopped(), truncate() */ +/*! + \fn QStringView QStringView::trimmed() const + + Strips leading and trailing whitespace and returns the result. + + Whitespace means any character for which QChar::isSpace() returns + \c true. This includes the ASCII characters '\\t', '\\n', '\\v', + '\\f', '\\r', and ' '. + + \sa qTrimmed() +*/ + /*! \fn bool QStringView::startsWith(QStringView str, Qt::CaseSensitivity cs) const \fn bool QStringView::startsWith(QLatin1String l1, Qt::CaseSensitivity cs) const diff --git a/src/corelib/tools/qstringview.h b/src/corelib/tools/qstringview.h index fdbf644518..24be441b00 100644 --- a/src/corelib/tools/qstringview.h +++ b/src/corelib/tools/qstringview.h @@ -245,6 +245,8 @@ public: Q_DECL_RELAXED_CONSTEXPR void chop(qssize_t n) { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size -= n; } + Q_REQUIRED_RESULT QStringView trimmed() const Q_DECL_NOTHROW { return qTrimmed(*this); } + Q_REQUIRED_RESULT bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW { return qStartsWith(*this, s, cs); } Q_REQUIRED_RESULT inline bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW; diff --git a/tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp index 0c495d2c8f..e482b409a5 100644 --- a/tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -349,6 +349,10 @@ private Q_SLOTS: void trim_trimmed_QString() { trimmed_impl(); } void trim_trimmed_QStringRef_data() { trimmed_data(); } void trim_trimmed_QStringRef() { trimmed_impl(); } + void trim_trimmed_QStringView_data() { trimmed_data(); } + void trim_trimmed_QStringView() { trimmed_impl(); } + void trim_trimmed_QLatin1String_data() { trimmed_data(); } + void trim_trimmed_QLatin1String() { trimmed_impl(); } void trim_trimmed_QByteArray_data() { trimmed_data(); } void trim_trimmed_QByteArray() { trimmed_impl(); } @@ -1050,7 +1054,7 @@ void tst_QStringApiSymmetry::trimmed_impl() const auto utf8 = unicode.toUtf8(); const auto l1s = unicode.toLatin1(); - const auto l1 = QLatin1String(l1s); + const auto l1 = l1s.isNull() ? QLatin1String() : QLatin1String(l1s); const auto ref = unicode.isNull() ? QStringRef() : QStringRef(&unicode); const auto s = make(ref, l1, utf8); -- cgit v1.2.3