From cdebc1a138eb4e4272b0b96e6bfce4fd66851ce1 Mon Sep 17 00:00:00 2001 From: Anton Kudryavtsev Date: Tue, 30 Jan 2018 19:57:28 +0300 Subject: [PATCH] QString: optimize remove() In remove(const QString &str, Qt::CaseSensitivity cs) call remove(QChar c, Qt::CaseSensitivity cs) if str.size() is equal 1. It prevents quadratic behavior for that case. Change-Id: I9a7ab3019c580343533c8c6c6a04b6b0c8c1fb55 Reviewed-by: Thiago Macieira --- src/corelib/tools/qstring.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 69751eb6dc..4e7baa18b6 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -2599,10 +2599,15 @@ QString &QString::remove(int pos, int len) */ QString &QString::remove(const QString &str, Qt::CaseSensitivity cs) { - if (str.d->size) { - int i = 0; - while ((i = indexOf(str, i, cs)) != -1) - remove(i, str.d->size); + const int strSize = str.size(); + if (strSize) { + if (strSize == 1) { + remove(str.front(), cs); + } else { + int i = 0; + while ((i = indexOf(str, i, cs)) != -1) + remove(i, strSize); + } } return *this; }