QString: port replace_helpers to QSpan

to simplify code

Change-Id: I597432b16d36c6f1d9948d83c9e7270bc7ac160e
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Anton Kudryavtsev 2023-10-18 19:07:41 +03:00
parent 32500a76a0
commit b419b5b65a

View File

@ -3661,7 +3661,7 @@ QString &QString::remove(QChar ch, Qt::CaseSensitivity cs)
and there isn't enough capacity, create a new string, copy characters to it and there isn't enough capacity, create a new string, copy characters to it
as needed, then swap it with "str". as needed, then swap it with "str".
*/ */
static void replace_with_copy(QString &str, size_t *indices, qsizetype nIndices, qsizetype blen, static void replace_with_copy(QString &str, QSpan<size_t> indices, qsizetype blen,
QStringView after) QStringView after)
{ {
const qsizetype alen = after.size(); const qsizetype alen = after.size();
@ -3669,12 +3669,12 @@ static void replace_with_copy(QString &str, size_t *indices, qsizetype nIndices,
const QString::DataPointer &str_d = str.data_ptr(); const QString::DataPointer &str_d = str.data_ptr();
auto src_start = str_d.begin(); auto src_start = str_d.begin();
const qsizetype newSize = str_d.size + nIndices * (alen - blen); const qsizetype newSize = str_d.size + indices.size() * (alen - blen);
QString copy{ newSize, Qt::Uninitialized }; QString copy{ newSize, Qt::Uninitialized };
QString::DataPointer &copy_d = copy.data_ptr(); QString::DataPointer &copy_d = copy.data_ptr();
auto dst = copy_d.begin(); auto dst = copy_d.begin();
for (qsizetype i = 0; i < nIndices; ++i) { for (size_t index : indices) {
auto hit = str_d.begin() + indices[i]; auto hit = str_d.begin() + index;
dst = std::copy(src_start, hit, dst); dst = std::copy(src_start, hit, dst);
dst = std::copy_n(after_b, alen, dst); dst = std::copy_n(after_b, alen, dst);
src_start = hit + blen; src_start = hit + blen;
@ -3684,7 +3684,7 @@ static void replace_with_copy(QString &str, size_t *indices, qsizetype nIndices,
} }
// No detaching or reallocation is needed // No detaching or reallocation is needed
static void replace_in_place(QString &str, size_t *indices, qsizetype nIndices, static void replace_in_place(QString &str, QSpan<size_t> indices,
qsizetype blen, QStringView after) qsizetype blen, QStringView after)
{ {
const qsizetype alen = after.size(); const qsizetype alen = after.size();
@ -3692,16 +3692,16 @@ static void replace_in_place(QString &str, size_t *indices, qsizetype nIndices,
const char16_t *after_e = after.utf16() + after.size(); const char16_t *after_e = after.utf16() + after.size();
if (blen == alen) { // Replace in place if (blen == alen) { // Replace in place
for (qsizetype i = 0; i < nIndices; ++i) for (size_t index : indices)
std::copy_n(after_b, alen, str.data_ptr().begin() + indices[i]); std::copy_n(after_b, alen, str.data_ptr().begin() + index);
} else if (blen > alen) { // Replace from front } else if (blen > alen) { // Replace from front
char16_t *begin = str.data_ptr().begin(); char16_t *begin = str.data_ptr().begin();
char16_t *hit = begin + indices[0]; char16_t *hit = begin + indices.front();
char16_t *to = hit; char16_t *to = hit;
to = std::copy_n(after_b, alen, to); to = std::copy_n(after_b, alen, to);
char16_t *movestart = hit + blen; char16_t *movestart = hit + blen;
for (qsizetype i = 1; i < nIndices; ++i) { for (size_t index : indices.sliced(1)) {
hit = begin + indices[i]; hit = begin + index;
to = std::move(movestart, hit, to); to = std::move(movestart, hit, to);
to = std::copy_n(after_b, alen, to); to = std::copy_n(after_b, alen, to);
movestart = hit + blen; movestart = hit + blen;
@ -3710,7 +3710,7 @@ static void replace_in_place(QString &str, size_t *indices, qsizetype nIndices,
str.resize(std::distance(begin, to)); str.resize(std::distance(begin, to));
} else { // blen < alen, Replace from back } else { // blen < alen, Replace from back
const qsizetype oldSize = str.data_ptr().size; const qsizetype oldSize = str.data_ptr().size;
const qsizetype adjust = nIndices * (alen - blen); const qsizetype adjust = indices.size() * (alen - blen);
const qsizetype newSize = oldSize + adjust; const qsizetype newSize = oldSize + adjust;
str.resize(newSize); str.resize(newSize);
@ -3718,9 +3718,8 @@ static void replace_in_place(QString &str, size_t *indices, qsizetype nIndices,
char16_t *moveend = begin + oldSize; char16_t *moveend = begin + oldSize;
char16_t *to = str.data_ptr().end(); char16_t *to = str.data_ptr().end();
while (nIndices) { for (auto it = indices.rbegin(), end = indices.rend(); it != end; ++it) {
--nIndices; char16_t *hit = begin + *it;
char16_t *hit = begin + indices[nIndices];
char16_t *movestart = hit + blen; char16_t *movestart = hit + blen;
to = std::move_backward(movestart, moveend, to); to = std::move_backward(movestart, moveend, to);
to = std::copy_backward(after_b, after_e, to); to = std::copy_backward(after_b, after_e, to);
@ -3729,22 +3728,22 @@ static void replace_in_place(QString &str, size_t *indices, qsizetype nIndices,
} }
} }
static void replace_helper(QString &str, size_t *indices, qsizetype nIndices, qsizetype blen, QStringView after) static void replace_helper(QString &str, QSpan<size_t> indices, qsizetype blen, QStringView after)
{ {
const qsizetype oldSize = str.data_ptr().size; const qsizetype oldSize = str.data_ptr().size;
const qsizetype adjust = nIndices * (after.size() - blen); const qsizetype adjust = indices.size() * (after.size() - blen);
const qsizetype newSize = oldSize + adjust; const qsizetype newSize = oldSize + adjust;
if (str.data_ptr().needsDetach() || needsReallocate(str, newSize)) { if (str.data_ptr().needsDetach() || needsReallocate(str, newSize)) {
replace_with_copy(str, indices, nIndices, blen, after); replace_with_copy(str, indices, blen, after);
return; return;
} }
if (QtPrivate::q_points_into_range(after.begin(), str)) if (QtPrivate::q_points_into_range(after.begin(), str))
// Copy after if it lies inside our own d.b area (which we could // Copy after if it lies inside our own d.b area (which we could
// possibly invalidate via a realloc or modify by replacement) // possibly invalidate via a realloc or modify by replacement)
replace_in_place(str, indices, nIndices, blen, QVarLengthArray(after.begin(), after.end())); replace_in_place(str, indices, blen, QVarLengthArray(after.begin(), after.end()));
else else
replace_in_place(str, indices, nIndices, blen, after); replace_in_place(str, indices, blen, after);
} }
/*! /*!
@ -3783,7 +3782,7 @@ QString &QString::replace(qsizetype pos, qsizetype len, const QChar *after, qsiz
len = this->size() - pos; len = this->size() - pos;
size_t index = pos; size_t index = pos;
replace_helper(*this, &index, 1, len, QStringView{after, alen}); replace_helper(*this, QSpan(&index, 1), len, QStringView{after, alen});
return *this; return *this;
} }
@ -3862,7 +3861,7 @@ QString &QString::replace(const QChar *before, qsizetype blen,
if (indices.isEmpty()) if (indices.isEmpty())
return *this; return *this;
replace_helper(*this, indices.data(), indices.size(), blen, QStringView{after, alen}); replace_helper(*this, indices, blen, QStringView{after, alen});
return *this; return *this;
} }
@ -3904,7 +3903,7 @@ QString& QString::replace(QChar ch, const QString &after, Qt::CaseSensitivity cs
if (indices.isEmpty()) if (indices.isEmpty())
return *this; return *this;
replace_helper(*this, indices.data(), indices.size(), 1, after); replace_helper(*this, indices, 1, after);
return *this; return *this;
} }