From 2b7c2c3a71dda00e9fa821fa520a2abc8c6a018b Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 26 Sep 2023 13:33:00 -0700 Subject: [PATCH] QString/QByteArray: avoid data() handling _empty in sliced() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit .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 Reviewed-by: MÃ¥rten Nordheim --- src/corelib/text/qbytearray.h | 2 +- src/corelib/text/qstring.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h index c9e90ce62b..2ee7dadcce 100644 --- a/src/corelib/text/qbytearray.h +++ b/src/corelib/text/qbytearray.h @@ -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); } diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index 31d19f7fe6..63432e5c5e 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -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); }