summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2017-05-04 19:41:48 +0200
committerMarc Mutz <marc.mutz@kdab.com>2017-05-16 09:34:59 +0000
commitcafefd1d33349209617d391b3b49c9663590b3c9 (patch)
treea5e8043ea2bc07afdf5e79fd313702ba3dc16d1c /src/corelib/tools
parent51e1374bae4ae449b04ddeb30c14bc67935705ca (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>
Diffstat (limited to 'src/corelib/tools')
-rw-r--r--src/corelib/tools/qstring.cpp48
-rw-r--r--src/corelib/tools/qstring.h2
-rw-r--r--src/corelib/tools/qstringalgorithms.h2
-rw-r--r--src/corelib/tools/qstringview.cpp12
-rw-r--r--src/corelib/tools/qstringview.h2
5 files changed, 66 insertions, 0 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;