Skip to content

Commit

Permalink
make {QString,QByteArray}::squeeze() work without prior reserve()
Browse files Browse the repository at this point in the history
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 a3aa2fc, which
changed it presumably only due to not considering the case above.

Change-Id: I0d7919a1724dd3ecc6cd4cbd7236eb52067f0a1c
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
  • Loading branch information
ossilator authored and Morten242 committed Sep 28, 2020
1 parent 7ee9bfc commit b9290cb
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/corelib/text/qbytearray.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/corelib/text/qstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b9290cb

Please sign in to comment.