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 /src/corelib/tools/qbytearray.h | |
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 'src/corelib/tools/qbytearray.h')
-rw-r--r-- | src/corelib/tools/qbytearray.h | 2 |
1 files changed, 2 insertions, 0 deletions
diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h index 3fc6718abb..4763afafe1 100644 --- a/src/corelib/tools/qbytearray.h +++ b/src/corelib/tools/qbytearray.h @@ -234,6 +234,8 @@ public: QByteArray left(int len) const Q_REQUIRED_RESULT; QByteArray right(int len) const Q_REQUIRED_RESULT; QByteArray mid(int index, int len = -1) const Q_REQUIRED_RESULT; + QByteArray chopped(int len) const Q_REQUIRED_RESULT + { Q_ASSERT(len >= 0); Q_ASSERT(len <= size()); return left(size() - len); } bool startsWith(const QByteArray &a) const; bool startsWith(char c) const; |