summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2020-05-20 13:09:21 +0200
committerLars Knoll <lars.knoll@qt.io>2020-05-21 17:59:31 +0200
commit2f05aa82e792089a249a0a77f1ae7e055cc90208 (patch)
treefd5a020d13d521d9205c5ac02d852e9a57476cfb /src
parent42024666a37ad0e8b5c557a2fd901779f63d1fcb (diff)
Adjust resize() behavior of QString and QByteArray to match Qt 5
resize() to a smaller size does not reallocate in Qt 5 if the container is not shared. Match this here. As a drive-by also fix resize calls on raw data strings to ensure they are null terminated after the resize. Change-Id: Ic4d8830e86ed3f247020d7ece3217cebd344ae96 Reviewed-by: Marc Mutz <marc.mutz@kdab.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/text/qbytearray.cpp22
-rw-r--r--src/corelib/text/qstring.cpp10
2 files changed, 7 insertions, 25 deletions
diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp
index f8c2759507..b85a46b146 100644
--- a/src/corelib/text/qbytearray.cpp
+++ b/src/corelib/text/qbytearray.cpp
@@ -1714,23 +1714,11 @@ void QByteArray::resize(int size)
if (size < 0)
size = 0;
- if (!d->isShared() && !d->isMutable() && size < int(d.size)) {
- d.size = size;
- return;
- }
-
- if (size == 0 && !(d->flags() & Data::CapacityReserved)) {
- d = DataPointer(Data::allocate(0), 0);
- } else {
- if (d->needsDetach() || size > capacity()
- || (!(d->flags() & Data::CapacityReserved) && size < int(d.size)
- && size < (capacity() >> 1)))
- reallocData(uint(size) + 1u, d->detachFlags() | Data::GrowsForward);
- d.size = size;
- if (d->isMutable()) {
- d.data()[size] = '\0';
- }
- }
+ if (d->needsDetach() || size > capacity())
+ reallocData(uint(size) + 1u, d->detachFlags() | Data::GrowsForward);
+ d.size = size;
+ if (d->allocatedCapacity())
+ d.data()[size] = 0;
}
/*!
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp
index 57be0eabcf..12b04b8ba7 100644
--- a/src/corelib/text/qstring.cpp
+++ b/src/corelib/text/qstring.cpp
@@ -2242,17 +2242,11 @@ void QString::resize(int size)
if (size < 0)
size = 0;
- if (!d->isShared() && !d->isMutable() && size < int(d.size)) {
- d.size = size;
- return;
- }
-
if (d->needsDetach() || size > capacity())
reallocData(uint(size) + 1u, true);
d.size = size;
- if (d->isMutable()) {
- d.data()[size] = '\0';
- }
+ if (d->allocatedCapacity())
+ d.data()[size] = 0;
}
/*!