QString/QByteArray: avoid data() handling _empty in sliced()

.data() in both classes has a null pointer check so it will return non-
null even if the object is storing a null pointer, for compatibility
with Qt 5 (controlled by QT5_NULL_STRINGS). We don't need this in
first()/last()/sliced()/chopped(), so we can skip the test and pass
whatever pointer it is directly to the class constructor. Both of them
handle null pointers creating an isNull() object.

This is a minor performance optimization and interestingly makes these
functions now retain isNull() with the result. I'm not adding test for
that as I don't want to hardcode that they will do so.

Change-Id: Ifeb6206a9fa04424964bfffd17888d14ec8244ec
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
bb10
Thiago Macieira 2023-09-26 13:33:00 -07:00
parent 4cffb3b5fb
commit 2b7c2c3a71
2 changed files with 2 additions and 2 deletions

View File

@ -163,7 +163,7 @@ public:
[[nodiscard]] QByteArray sliced(qsizetype pos) const
{ verify(pos, 0); return QByteArray(data() + pos, size() - pos); }
[[nodiscard]] QByteArray sliced(qsizetype pos, qsizetype n) const
{ verify(pos, n); return QByteArray(data() + pos, n); }
{ verify(pos, n); return QByteArray(d.data() + pos, n); }
[[nodiscard]] QByteArray chopped(qsizetype len) const
{ verify(0, len); return sliced(0, size() - len); }

View File

@ -342,7 +342,7 @@ public:
[[nodiscard]] QString sliced(qsizetype pos) const
{ verify(pos, 0); return QString(data() + pos, size() - pos); }
[[nodiscard]] QString sliced(qsizetype pos, qsizetype n) const
{ verify(pos, n); return QString(data() + pos, n); }
{ verify(pos, n); return QString(begin() + pos, n); }
[[nodiscard]] QString chopped(qsizetype n) const
{ verify(0, n); return sliced(0, size() - n); }