QByteArray: don't detach in remove()
- If this bytearray isn't shared call d->erase() as needed - if it's shared, instead of detaching, create a new bytearray, and copy all characters except for the ones that would be removed See task for details. Adjust unittest to test both code paths. Task-number: QTBUG-106182 Change-Id: I806e4d1707004345a2472e056905fbf675f765ab Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
71c3aab7ba
commit
358b7a9e74
@ -2253,11 +2253,20 @@ QByteArray &QByteArray::remove(qsizetype pos, qsizetype len)
|
|||||||
{
|
{
|
||||||
if (len <= 0 || pos < 0 || size_t(pos) >= size_t(size()))
|
if (len <= 0 || pos < 0 || size_t(pos) >= size_t(size()))
|
||||||
return *this;
|
return *this;
|
||||||
detach();
|
|
||||||
if (pos + len > d->size)
|
if (pos + len > d->size)
|
||||||
len = d->size - pos;
|
len = d->size - pos;
|
||||||
d->erase(d.begin() + pos, len);
|
|
||||||
|
auto begin = d.begin();
|
||||||
|
if (!d->isShared()) {
|
||||||
|
d->erase(begin + pos, len);
|
||||||
d.data()[d.size] = '\0';
|
d.data()[d.size] = '\0';
|
||||||
|
} else {
|
||||||
|
QByteArray copy{size() - len, Qt::Uninitialized};
|
||||||
|
const auto toRemove_start = d.begin() + pos;
|
||||||
|
copy.d->copyRanges({{d.begin(), toRemove_start},
|
||||||
|
{toRemove_start + len, d.end()}});
|
||||||
|
swap(copy);
|
||||||
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1258,7 +1258,14 @@ void tst_QByteArray::remove()
|
|||||||
QFETCH(int, position);
|
QFETCH(int, position);
|
||||||
QFETCH(int, length);
|
QFETCH(int, length);
|
||||||
QFETCH(QByteArray, expected);
|
QFETCH(QByteArray, expected);
|
||||||
QCOMPARE(src.remove(position, length), expected);
|
// Test when it's shared
|
||||||
|
QByteArray ba1 = src;
|
||||||
|
QCOMPARE(ba1.remove(position, length), expected);
|
||||||
|
|
||||||
|
// Test when it's not shared
|
||||||
|
QByteArray ba2 = src;
|
||||||
|
ba2.detach();
|
||||||
|
QCOMPARE(ba2.remove(position, length), expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_QByteArray::removeIf()
|
void tst_QByteArray::removeIf()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user