From b419b5b65a5e231ee2af3e91c09423fd8ed6b49b Mon Sep 17 00:00:00 2001 From: Anton Kudryavtsev Date: Wed, 18 Oct 2023 19:07:41 +0300 Subject: [PATCH] QString: port replace_helpers to QSpan to simplify code Change-Id: I597432b16d36c6f1d9948d83c9e7270bc7ac160e Reviewed-by: Thiago Macieira --- src/corelib/text/qstring.cpp | 43 ++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 1222a6807a..49641be2dc 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -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 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 indices, qsizetype blen, QStringView after) { 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(); 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::DataPointer ©_d = copy.data_ptr(); auto dst = copy_d.begin(); - for (qsizetype i = 0; i < nIndices; ++i) { - auto hit = str_d.begin() + indices[i]; + for (size_t index : indices) { + auto hit = str_d.begin() + index; dst = std::copy(src_start, hit, dst); dst = std::copy_n(after_b, alen, dst); 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 -static void replace_in_place(QString &str, size_t *indices, qsizetype nIndices, +static void replace_in_place(QString &str, QSpan indices, qsizetype blen, QStringView after) { 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(); if (blen == alen) { // Replace in place - for (qsizetype i = 0; i < nIndices; ++i) - std::copy_n(after_b, alen, str.data_ptr().begin() + indices[i]); + for (size_t index : indices) + std::copy_n(after_b, alen, str.data_ptr().begin() + index); } else if (blen > alen) { // Replace from front char16_t *begin = str.data_ptr().begin(); - char16_t *hit = begin + indices[0]; + char16_t *hit = begin + indices.front(); char16_t *to = hit; to = std::copy_n(after_b, alen, to); char16_t *movestart = hit + blen; - for (qsizetype i = 1; i < nIndices; ++i) { - hit = begin + indices[i]; + for (size_t index : indices.sliced(1)) { + hit = begin + index; to = std::move(movestart, hit, to); to = std::copy_n(after_b, alen, to); 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)); } else { // blen < alen, Replace from back 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; 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 *to = str.data_ptr().end(); - while (nIndices) { - --nIndices; - char16_t *hit = begin + indices[nIndices]; + for (auto it = indices.rbegin(), end = indices.rend(); it != end; ++it) { + char16_t *hit = begin + *it; char16_t *movestart = hit + blen; to = std::move_backward(movestart, moveend, 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 indices, qsizetype blen, QStringView after) { 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; if (str.data_ptr().needsDetach() || needsReallocate(str, newSize)) { - replace_with_copy(str, indices, nIndices, blen, after); + replace_with_copy(str, indices, blen, after); return; } if (QtPrivate::q_points_into_range(after.begin(), str)) // Copy after if it lies inside our own d.b area (which we could // 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 - 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; 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; } @@ -3862,7 +3861,7 @@ QString &QString::replace(const QChar *before, qsizetype blen, if (indices.isEmpty()) return *this; - replace_helper(*this, indices.data(), indices.size(), blen, QStringView{after, alen}); + replace_helper(*this, indices, blen, QStringView{after, alen}); return *this; } @@ -3904,7 +3903,7 @@ QString& QString::replace(QChar ch, const QString &after, Qt::CaseSensitivity cs if (indices.isEmpty()) return *this; - replace_helper(*this, indices.data(), indices.size(), 1, after); + replace_helper(*this, indices, 1, after); return *this; }