summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2015-05-17 16:29:19 +0200
committerMarc Mutz <marc.mutz@kdab.com>2015-06-19 05:41:41 +0000
commit24ef5d2263a11428cd9db83da1d8d9cb3534bc9f (patch)
tree516311b5c10080af49b83a3eede924dc16f00d00
parent302cc32deed6f230fa3987f94e962b0f0d43439a (diff)
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>
-rw-r--r--src/corelib/tools/qstring.cpp16
-rw-r--r--src/corelib/tools/qstring.h2
-rw-r--r--tests/auto/corelib/tools/qstringref/tst_qstringref.cpp25
3 files changed, 42 insertions, 1 deletions
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp
index 2585686156..04d94b4a2b 100644
--- a/src/corelib/tools/qstring.cpp
+++ b/src/corelib/tools/qstring.cpp
@@ -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)
@@ -9376,6 +9376,20 @@ QStringRef QString::midRef(int position, int n) const
}
/*!
+ \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
Returns the index position of the first occurrence of the string \a
diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h
index 670d94279d..af5d22a53b 100644
--- a/src/corelib/tools/qstring.h
+++ b/src/corelib/tools/qstring.h
@@ -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;
diff --git a/tests/auto/corelib/tools/qstringref/tst_qstringref.cpp b/tests/auto/corelib/tools/qstringref/tst_qstringref.cpp
index 7fc855a359..82d103c460 100644
--- a/tests/auto/corelib/tools/qstringref/tst_qstringref.cpp
+++ b/tests/auto/corelib/tools/qstringref/tst_qstringref.cpp
@@ -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~";