QByteArray: Disregard space at front during ::reserve(...)

Traditionally when calling reserve it's because you expect to append
up to X amount of bytes. We should keep that behavior the same.

With another patch still in the works current behavior caused an issue
with QStringBuilder in QNAM, as mirrored in the testcase attached.

Change-Id: I9792a8f158fc9235e3de48ac8b06ac2c10e7f3dc
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Mårten Nordheim 2020-09-01 09:07:09 +02:00
parent 39e07ebf64
commit b98b7de0da
2 changed files with 9 additions and 1 deletions

View File

@ -489,7 +489,7 @@ inline qsizetype QByteArray::capacity() const
inline void QByteArray::reserve(qsizetype asize)
{
if (d->needsDetach() || asize > capacity()) {
if (d->needsDetach() || asize > capacity() - d->freeSpaceAtBegin()) {
reallocData(qMax(size_t(size()), size_t(asize)) + 1u, d->detachFlags() | Data::CapacityReserved);
} else {
d->setFlag(Data::CapacityReserved);

View File

@ -344,6 +344,14 @@ void runScenario()
QByteArray ba2 = withZero;
ba2 += ba2 P withZero;
QCOMPARE(ba2, QByteArray(withZero + withZero + withZero));
// With space allocated in front, mirroring what happens with QHttpMultiPart in QNAM
QByteArray byteArray;
byteArray.reserve(70);
byteArray.insert(0, "multipart/");
byteArray.insert(byteArray.size(), "mixed");
byteArray += "; boundary=\"" P QByteArray(30, 'o') P '"';
QCOMPARE(byteArray, "multipart/mixed; boundary=\"oooooooooooooooooooooooooooooo\"");
}
}