From 0c0ee82bff31ff2733c5229cf39f678f80b2e7e6 Mon Sep 17 00:00:00 2001 From: Anton Kudryavtsev Date: Wed, 31 Jan 2018 20:26:38 +0300 Subject: QString:: add remove() overload taking QLatin1String [ChangeLog][QtCore][QString] Added remove() overload taking QLatin1String Change-Id: I11ddb8b8603144effe44f89d0d02e131a255122c Reviewed-by: Thiago Macieira --- src/corelib/tools/qstring.cpp | 46 +++++++++++++++++++++++++++++++++---------- src/corelib/tools/qstring.h | 1 + 2 files changed, 37 insertions(+), 10 deletions(-) (limited to 'src/corelib/tools') diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 4e7baa18b6..4040d59b0a 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -2586,6 +2586,21 @@ QString &QString::remove(int pos, int len) return *this; } +template +static void removeStringImpl(QString &s, const T &needle, Qt::CaseSensitivity cs) +{ + const int needleSize = needle.size(); + if (needleSize) { + if (needleSize == 1) { + s.remove(needle.front(), cs); + } else { + int i = 0; + while ((i = s.indexOf(needle, i, cs)) != -1) + s.remove(i, needleSize); + } + } +} + /*! Removes every occurrence of the given \a str string in this string, and returns a reference to this string. @@ -2599,16 +2614,27 @@ QString &QString::remove(int pos, int len) */ QString &QString::remove(const QString &str, Qt::CaseSensitivity cs) { - 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); - } - } + removeStringImpl(*this, str, cs); + return *this; +} + +/*! + \since 5.11 + \overload + + Removes every occurrence of the given \a str string in this + string, and returns a reference to this string. + + If \a cs is Qt::CaseSensitive (default), the search is + case sensitive; otherwise the search is case insensitive. + + This is the same as \c replace(str, "", cs). + + \sa replace() +*/ +QString &QString::remove(QLatin1String str, Qt::CaseSensitivity cs) +{ + removeStringImpl(*this, str, cs); return *this; } diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index b40a622c7c..0138ae4098 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -483,6 +483,7 @@ public: QString &remove(int i, int len); QString &remove(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive); + QString &remove(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive); QString &remove(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive); QString &replace(int i, int len, QChar after); QString &replace(int i, int len, const QChar *s, int slen); -- cgit v1.2.3