From b9290cb6e58b47542306c32872e5f53b1c61beee Mon Sep 17 00:00:00 2001 From: Oswald Buddenhagen Date: Fri, 11 Sep 2020 11:11:45 +0200 Subject: [PATCH] make {QString,QByteArray}::squeeze() work without prior reserve() string-shortening operations never throw away capacity (unless detaching), so it may very much make sense to squeeze a string whose capacity was not explicitly reserved. this does in fact restore the behavior prior to commit a3aa2fcf, which changed it presumably only due to not considering the case above. Change-Id: I0d7919a1724dd3ecc6cd4cbd7236eb52067f0a1c Reviewed-by: Andrei Golubev Reviewed-by: Oswald Buddenhagen --- src/corelib/text/qbytearray.h | 2 +- src/corelib/text/qstring.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h index f98313a1f0..7162c6cdcb 100644 --- a/src/corelib/text/qbytearray.h +++ b/src/corelib/text/qbytearray.h @@ -532,7 +532,7 @@ inline void QByteArray::reserve(qsizetype asize) inline void QByteArray::squeeze() { - if ((d->flags() & Data::CapacityReserved) == 0) + if (!d.isMutable()) return; if (d->needsDetach() || size() < capacity()) { reallocData(size(), d->detachFlags() & ~Data::CapacityReserved); diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index f4e58e38b2..fc3a0431ff 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -1166,9 +1166,9 @@ inline void QString::reserve(qsizetype asize) inline void QString::squeeze() { - if ((d->flags() & Data::CapacityReserved) == 0) + if (!d.isMutable()) return; - if (d->needsDetach() || d.size < capacity()) { + if (d->needsDetach() || size() < capacity()) { reallocData(d.size, d->detachFlags() & ~Data::CapacityReserved); } else { d->clearFlag(Data::CapacityReserved);