From 159e17bc5b83f46d3c0a78aacd6b91c2da12ab30 Mon Sep 17 00:00:00 2001 From: Keith Gardner Date: Sat, 19 Jan 2013 21:35:16 -0600 Subject: QStringRef: Added a trimmed() function. Change-Id: I67c5d10f29f420e0aea95cf32b5d3c17c141899c Reviewed-by: Oswald Buddenhagen Reviewed-by: Friedemann Kleint Reviewed-by: Olivier Goffart --- src/corelib/tools/qstring.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'src/corelib/tools/qstring.cpp') 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 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 -- cgit v1.2.3