QString: optimize remove(QChar, Qt::CaseSensitivity)
remove(int, int) with O(N) was used in a loop. We had a quadratic complexity. Use erase-remove idiom to fix it. Change-Id: I643a2a75619ec5ea2bf99e48a25f64a7f69ba156 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
parent
b5222307af
commit
be94fc445a
@ -2300,21 +2300,20 @@ QString &QString::remove(const QString &str, Qt::CaseSensitivity cs)
|
|||||||
*/
|
*/
|
||||||
QString &QString::remove(QChar ch, Qt::CaseSensitivity cs)
|
QString &QString::remove(QChar ch, Qt::CaseSensitivity cs)
|
||||||
{
|
{
|
||||||
int i = 0;
|
const int idx = indexOf(ch, 0, cs);
|
||||||
ushort c = ch.unicode();
|
if (idx != -1) {
|
||||||
|
const auto first = begin(); // implicit detach()
|
||||||
|
auto last = end();
|
||||||
if (cs == Qt::CaseSensitive) {
|
if (cs == Qt::CaseSensitive) {
|
||||||
while (i < d->size)
|
last = std::remove(first + idx, last, ch);
|
||||||
if (d->data()[i] == ch)
|
|
||||||
remove(i, 1);
|
|
||||||
else
|
|
||||||
i++;
|
|
||||||
} else {
|
} else {
|
||||||
c = foldCase(c);
|
const QChar c = ch.toCaseFolded();
|
||||||
while (i < d->size)
|
auto caseInsensEqual = [c](QChar x) {
|
||||||
if (foldCase(d->data()[i]) == c)
|
return c == x.toCaseFolded();
|
||||||
remove(i, 1);
|
};
|
||||||
else
|
last = std::remove_if(first + idx, last, caseInsensEqual);
|
||||||
i++;
|
}
|
||||||
|
resize(last - first);
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user