diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2017-05-04 19:41:48 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2017-05-16 09:34:59 +0000 |
commit | cafefd1d33349209617d391b3b49c9663590b3c9 (patch) | |
tree | a5e8043ea2bc07afdf5e79fd313702ba3dc16d1c | |
parent | 51e1374bae4ae449b04ddeb30c14bc67935705ca (diff) |
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 <antkudr@mail.ru>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
-rw-r--r-- | src/corelib/tools/qstring.cpp | 48 | ||||
-rw-r--r-- | src/corelib/tools/qstring.h | 2 | ||||
-rw-r--r-- | src/corelib/tools/qstringalgorithms.h | 2 | ||||
-rw-r--r-- | src/corelib/tools/qstringview.cpp | 12 | ||||
-rw-r--r-- | src/corelib/tools/qstringview.h | 2 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp | 6 |
6 files changed, 71 insertions, 1 deletions
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<QString>::simplified_helper(str); } +namespace { + template <typename StringView> + StringView qt_trimmed(StringView s) Q_DECL_NOTHROW + { + auto begin = s.begin(); + auto end = s.end(); + QStringAlgorithms<const StringView>::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 @@ -665,6 +665,18 @@ QT_BEGIN_NAMESPACE */ /*! + \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 \fn bool QStringView::startsWith(QChar ch) 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<QString>(); } void trim_trimmed_QStringRef_data() { trimmed_data(); } void trim_trimmed_QStringRef() { trimmed_impl<QStringRef>(); } + void trim_trimmed_QStringView_data() { trimmed_data(); } + void trim_trimmed_QStringView() { trimmed_impl<QStringView>(); } + void trim_trimmed_QLatin1String_data() { trimmed_data(); } + void trim_trimmed_QLatin1String() { trimmed_impl<QLatin1String>(); } void trim_trimmed_QByteArray_data() { trimmed_data(); } void trim_trimmed_QByteArray() { trimmed_impl<QByteArray>(); } @@ -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<String>(ref, l1, utf8); |