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>
This commit is contained in:
parent
507382c778
commit
159e17bc5b
@ -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
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user