diff options
author | Keith Gardner <kreios4004@gmail.com> | 2013-01-19 21:35:16 -0600 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-01-29 22:28:17 +0100 |
commit | 159e17bc5b83f46d3c0a78aacd6b91c2da12ab30 (patch) | |
tree | 9d281d42b0c20c72996407656e5f5a9912652b27 /src/corelib/tools/qstring.cpp | |
parent | 507382c778959e02de25edfe31ad04ecdd231c79 (diff) |
QStringRef: Added a trimmed() function.
Change-Id: I67c5d10f29f420e0aea95cf32b5d3c17c141899c
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'src/corelib/tools/qstring.cpp')
-rw-r--r-- | src/corelib/tools/qstring.cpp | 31 |
1 files changed, 31 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 |