QStringRef: add truncate()

Missing part of QString API.

[ChangeLog][QtCore][QStringRef] Added truncate(int).

Change-Id: I49e218daf8f47fcd3dad131155e0abc8e2a133e5
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2015-05-17 16:29:19 +02:00
parent 302cc32dee
commit 24ef5d2263
3 changed files with 42 additions and 1 deletions

View File

@ -4880,7 +4880,7 @@ modifiable reference.
If \a position is negative, it is equivalent to passing zero.
\sa chop(), resize(), left()
\sa chop(), resize(), left(), QStringRef::truncate()
*/
void QString::truncate(int pos)
@ -9375,6 +9375,20 @@ QStringRef QString::midRef(int position, int n) const
return QStringRef();
}
/*!
\fn void QStringRef::truncate(int position)
\since 5.6
Truncates the string at the given \a position index.
If the specified \a position index is beyond the end of the
string, nothing happens.
If \a position is negative, it is equivalent to passing zero.
\sa QString::truncate()
*/
/*!
\since 4.8

View File

@ -1399,6 +1399,8 @@ public:
QStringRef right(int n) const Q_REQUIRED_RESULT;
QStringRef mid(int pos, int n = -1) const Q_REQUIRED_RESULT;
void truncate(int pos) Q_DECL_NOTHROW { m_size = qBound(0, pos, m_size); }
bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
bool startsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;

View File

@ -82,6 +82,7 @@ private slots:
void integer_conversion_data();
void integer_conversion();
void trimmed();
void truncate();
void left();
void right();
void mid();
@ -1839,6 +1840,30 @@ void tst_QStringRef::trimmed()
QCOMPARE(b.trimmed().compare(QStringLiteral("a")), 0);
}
void tst_QStringRef::truncate()
{
const QString str = "OriginalString~";
const QStringRef cref = str.midRef(0);
{
QStringRef ref = cref;
ref.truncate(1000);
QCOMPARE(ref, cref);
for (int i = str.size(); i >= 0; --i) {
ref.truncate(i);
QCOMPARE(ref.size(), i);
QCOMPARE(ref, cref.left(i));
}
QVERIFY(ref.isEmpty());
}
{
QStringRef ref = cref;
QVERIFY(!ref.isEmpty());
ref.truncate(-1);
QVERIFY(ref.isEmpty());
}
}
void tst_QStringRef::left()
{
QString originalString = "OrginalString~";