QString:: add remove() overload taking QLatin1String

[ChangeLog][QtCore][QString] Added remove() overload taking QLatin1String

Change-Id: I11ddb8b8603144effe44f89d0d02e131a255122c
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Anton Kudryavtsev 2018-01-31 20:26:38 +03:00 committed by Anton Kudryavtsev
parent 5c60e4b8f9
commit 0c0ee82bff
3 changed files with 43 additions and 10 deletions

View File

@ -2586,6 +2586,21 @@ QString &QString::remove(int pos, int len)
return *this;
}
template<typename T>
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;
}

View File

@ -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);

View File

@ -2994,6 +2994,12 @@ void tst_QString::remove_string()
QString s5 = string;
s5.replace( QRegExp(before, cs, QRegExp::FixedString), after );
QTEST( s5, "result" );
if (QtPrivate::isLatin1(before)) {
QString s6 = string;
s6.remove( QLatin1String(before.toLatin1()), cs );
QTEST( s6, "result" );
}
} else {
QCOMPARE( 0, 0 ); // shut Qt Test
}