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/qstring.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/qstring.h')
-rw-r--r-- | src/corelib/tools/qstring.h | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index f65331f7e4..ce2d323a91 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -421,6 +421,10 @@ public: QString left(int n) const Q_REQUIRED_RESULT; QString right(int n) const Q_REQUIRED_RESULT; QString mid(int position, int n = -1) const Q_REQUIRED_RESULT; + QString chopped(int n) const Q_REQUIRED_RESULT + { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return left(size() - n); } + + QStringRef leftRef(int n) const Q_REQUIRED_RESULT; QStringRef rightRef(int n) const Q_REQUIRED_RESULT; QStringRef midRef(int position, int n = -1) const Q_REQUIRED_RESULT; @@ -1492,6 +1496,8 @@ public: QStringRef left(int n) const Q_REQUIRED_RESULT; QStringRef right(int n) const Q_REQUIRED_RESULT; QStringRef mid(int pos, int n = -1) const Q_REQUIRED_RESULT; + QStringRef chopped(int n) const Q_REQUIRED_RESULT + { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); return left(size() - n); } void truncate(int pos) Q_DECL_NOTHROW { m_size = qBound(0, pos, m_size); } void chop(int n) Q_DECL_NOTHROW |