{
return replace(before.constData(), before.size(), after.constData(), after.size(), cs);
}
const QChar *after,
int alen,
Qt::CaseSensitivity cs)
{
if (d->size == 0) {
if (blen)
return *this;
} else {
if (cs == Qt::CaseSensitive && before == after && blen == alen)
return *this;
}
if (alen == 0 && blen == 0)
return *this;
int index = 0;
while (1) {
uint indices[1024];
uint pos = 0;
while (pos < 1023) {
index = matcher.indexIn(*this, index);
if (index == -1)
break;
indices[pos++] = index;
index += blen;
// avoid infinite loop
if (!blen)
index++;
}
if (!pos)
break;
replace_helper(indices, pos, blen, after, alen);
if (index == -1)
break;
// index has to be adjusted in case we get back into the loop above.
index += pos*(alen-blen);
}
return *this;
}
QString &QString::replace(const QString &before, const QString &after, Qt::CaseSensitivity cs)
{
return replace(before.constData(), before.size(), after.constData(), after.size(), cs);
}
QString &QString::replace(const QChar *before, int blen,
const QChar *after, int alen,
Qt::CaseSensitivity cs)
{
if (d->size == 0) {
if (blen)
return *this;
} else {
if (cs == Qt::CaseSensitive && before == after && blen == alen)
return *this;
}
if (alen == 0 && blen == 0)
return *this;
QStringMatcher matcher(before, blen, cs);
int index = 0;
while (1) {
uint indices[1024];
uint pos = 0;
while (pos < 1023) {
index = matcher.indexIn(*this, index);
if (index == -1)
break;
indices[pos++] = index;
index += blen;
// avoid infinite loop
if (!blen)
index++;
}
if (!pos)
break;
replace_helper(indices, pos, blen, after, alen);
if (index == -1)
break;
// index has to be adjusted in case we get back into the loop above.
index += pos*(alen-blen);
}
return *this;
}
To copy to clipboard, switch view to plain text mode
The only thing is that every time a QStringMatcher is created. But that's normally not such a big thing. (if you don't work on an app werer every 0,0001 ms counts.)
Bookmarks