summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/corelib/tools/qstring.cpp31
-rw-r--r--src/corelib/tools/qstring.h2
2 files changed, 33 insertions, 0 deletions
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp
index 8583bcf70f..ed8cb734f5 100644
--- a/src/corelib/tools/qstring.cpp
+++ b/src/corelib/tools/qstring.cpp
@@ -9193,6 +9193,37 @@ QVector<uint> QStringRef::toUcs4() const
return v;
}
+/*!
+ Returns a string that has whitespace removed from the start and
+ the end.
+
+ Whitespace means any character for which QChar::isSpace() returns
+ true. This includes the ASCII characters '\\t', '\\n', '\\v',
+ '\\f', '\\r', and ' '.
+
+ Unlike QString::simplified(), trimmed() leaves internal whitespace alone.
+
+ \since 5.1
+
+ \sa QString::trimmed()
+*/
+QStringRef QStringRef::trimmed() const
+{
+ if (m_size == 0 || m_string == 0)
+ return *this;
+ const QChar *s = m_string->constData() + m_position;
+ int start = 0;
+ int end = m_size - 1;
+ while (start <= end && s[start].isSpace()) // skip white space from start
+ start++;
+ if (start <= end) { // only white space
+ while (end && s[end].isSpace()) // skip white space from end
+ end--;
+ }
+ int l = end - start + 1;
+ Q_ASSERT(l >= 0);
+ return QStringRef(m_string, m_position + start, l);
+}
/*!
\obsolete
diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h
index 35f8a3dc8e..1e45da060c 100644
--- a/src/corelib/tools/qstring.h
+++ b/src/corelib/tools/qstring.h
@@ -1273,6 +1273,8 @@ public:
int localeAwareCompare(const QStringRef &s) const;
static int localeAwareCompare(const QStringRef &s1, const QString &s2);
static int localeAwareCompare(const QStringRef &s1, const QStringRef &s2);
+
+ QStringRef trimmed() const Q_REQUIRED_RESULT;
};
Q_DECLARE_TYPEINFO(QStringRef, Q_PRIMITIVE_TYPE);