QString: refactor remove(QChar, Qt::CaseSensitivity)

Change-Id: I0e38e9fd00d81aea6e779012beabdfb29695bd43
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ahmad Samir 2022-10-25 09:35:29 +02:00
parent f41089ba3d
commit ed7e57973a

View File

@ -3407,20 +3407,22 @@ QString &QString::remove(QLatin1StringView str, Qt::CaseSensitivity cs)
QString &QString::remove(QChar ch, Qt::CaseSensitivity cs) QString &QString::remove(QChar ch, Qt::CaseSensitivity cs)
{ {
const qsizetype idx = indexOf(ch, 0, cs); const qsizetype idx = indexOf(ch, 0, cs);
if (idx != -1) { if (idx == -1)
const auto first = begin(); // implicit detach() return *this;
auto last = end();
if (cs == Qt::CaseSensitive) { const bool isCase = cs == Qt::CaseSensitive;
last = std::remove(first + idx, last, ch); ch = isCase ? ch : ch.toCaseFolded();
} else { auto match = [ch, isCase](QChar x) {
const QChar c = ch.toCaseFolded(); return ch == (isCase ? x : x.toCaseFolded());
auto caseInsensEqual = [c](QChar x) { };
return c == x.toCaseFolded();
}; detach();
last = std::remove_if(first + idx, last, caseInsensEqual); auto begin = d.begin();
} auto first_match = begin + idx;
resize(last - first); auto end = d.end();
} auto it = std::remove_if(first_match, end, match);
d->erase(it, std::distance(it, end));
d.data()[d.size] = u'\0';
return *this; return *this;
} }