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>bb10
parent
42024666a3
commit
2f05aa82e7
|
|
@ -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;
|
||||
}
|
||||
|
||||
/*!
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
/*!
|
||||
|
|
|
|||
|
|
@ -1606,7 +1606,9 @@ void tst_QByteArray::resizeAfterFromRawData()
|
|||
QByteArray array = QByteArray::fromRawData(buffer.constData(), buffer.size());
|
||||
QVERIFY(array.constData() == buffer.constData());
|
||||
array.resize(5);
|
||||
QVERIFY(array.constData() == buffer.constData());
|
||||
QVERIFY(array.constData() != buffer.constData());
|
||||
// check null termination
|
||||
QVERIFY(array.constData()[5] == 0);
|
||||
}
|
||||
|
||||
void tst_QByteArray::appendAfterFromRawData()
|
||||
|
|
|
|||
|
|
@ -6238,7 +6238,9 @@ void tst_QString::resizeAfterFromRawData()
|
|||
QString array = QString::fromRawData(buffer.constData(), buffer.size());
|
||||
QVERIFY(array.constData() == buffer.constData());
|
||||
array.resize(5);
|
||||
QVERIFY(array.constData() == buffer.constData());
|
||||
QVERIFY(array.constData() != buffer.constData());
|
||||
// check null termination
|
||||
QVERIFY(array.constData()[5] == 0);
|
||||
}
|
||||
|
||||
void tst_QString::resizeAfterReserve()
|
||||
|
|
|
|||
Loading…
Reference in New Issue