diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2017-04-05 08:53:15 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2017-04-18 09:43:18 +0000 |
commit | d176808eef61d27a6536f268125dab4088a248d3 (patch) | |
tree | 91b8155c55e092bbf9a8976fa77d5be2adbcb6b3 /tests/auto/corelib/tools/qstringapisymmetry | |
parent | 6d3c4833c26cced876ed57e9f158fbade5b2679c (diff) |
QByteArray, QString, QStringRef: Add chopped()
We have two functions to get a substring without doing some
calculations involving size():
- mid(p): mid(p, size() - p)
- right(n) : mid(size() - n, n)
(left does not involve size(), so isn't in that set). What was missing
was a name for
- f(n): mid(0, size() - n)
As an action, it's called chop(), so call the transformation version
chopped().
I made chopped(n), n < 0 or n > size(), undefined, because QString(Ref)
::left() is broken[1], while the QByteArray implementation is not. This
is the only way to get consistent behavior among the three classes.
I's also the correct thing to do.
[1] instead of returning the empty string for negative indexes, it
returns the whole string.
[ChangeLog][QtCore][QString/QStringRef/QByteArray] Added chopped(n), a
const version of chop(n).
Change-Id: I6c2c5b16e0060fa924ced5860f21f2d0f23bd023
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Anton Kudryavtsev <antkudr@mail.ru>
Diffstat (limited to 'tests/auto/corelib/tools/qstringapisymmetry')
-rw-r--r-- | tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp index 1a7d25e400..14e84d80b6 100644 --- a/tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -609,6 +609,14 @@ void tst_QStringApiSymmetry::chop_impl() const auto s = make<String>(unicode, latin1, utf8); { + const auto chopped = s.chopped(n); + + QVERIFY(chopped == result); + QCOMPARE(chopped.isNull(), result.isNull()); + QCOMPARE(chopped.isEmpty(), result.isEmpty()); + } + + { auto chopped = s; chopped.chop(n); |