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 | |
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>
-rw-r--r-- | src/corelib/tools/qstring.cpp | 31 | ||||
-rw-r--r-- | src/corelib/tools/qstring.h | 2 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qstringref/tst_qstringref.cpp | 22 |
3 files changed, 55 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); diff --git a/tests/auto/corelib/tools/qstringref/tst_qstringref.cpp b/tests/auto/corelib/tools/qstringref/tst_qstringref.cpp index 66f40e05cb..3494f48603 100644 --- a/tests/auto/corelib/tools/qstringref/tst_qstringref.cpp +++ b/tests/auto/corelib/tools/qstringref/tst_qstringref.cpp @@ -69,6 +69,7 @@ private slots: void compare_data(); void compare(); void operator_eqeq_nullstring(); + void trimmed(); }; static QStringRef emptyRef() @@ -840,6 +841,27 @@ void tst_QStringRef::compare() } } +void tst_QStringRef::trimmed() +{ + QString a; + QStringRef b; + a = "Text"; + b = a.leftRef(-1); + QCOMPARE(b.compare(QStringLiteral("Text")), 0); + QCOMPARE(b.trimmed().compare(QStringLiteral("Text")), 0); + a = " "; + b = a.leftRef(-1); + QCOMPARE(b.compare(QStringLiteral(" ")), 0); + QCOMPARE(b.trimmed().compare(QStringLiteral("")), 0); + a = " a "; + b = a.leftRef(-1); + QCOMPARE(b.trimmed().compare(QStringLiteral("a")), 0); + a = "Text a "; + b = a.midRef(4); + QCOMPARE(b.compare(QStringLiteral(" a ")), 0); + QCOMPARE(b.trimmed().compare(QStringLiteral("a")), 0); +} + QTEST_APPLESS_MAIN(tst_QStringRef) #include "tst_qstringref.moc" |