1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 03:07:43 +00:00

AK: Add String::replace() functionality

This adds a replace functionality that replaces a string that contains
occurences of a "needle" by a "replacement" value. With "all_occurences"
enabled, all occurences are being replaced, otherwise only the first
occurence is being replaced.
This commit is contained in:
Emanuel Sprung 2020-04-01 21:27:39 +02:00 committed by Andreas Kling
parent 9d5d0261e1
commit 2577712a1c
3 changed files with 62 additions and 0 deletions

View file

@ -320,6 +320,41 @@ bool String::equals_ignoring_case(const StringView& other) const
return StringUtils::equals_ignoring_case(view(), other);
}
int String::replace(const String& needle, const String& replacement, bool all_occurences)
{
if (is_empty())
return 0;
Vector<size_t> positions;
size_t start = 0, pos;
for (;;) {
const char* ptr = strstr(characters() + start, needle.characters());
if (!ptr)
break;
pos = ptr - characters();
positions.append(pos);
if (!all_occurences)
break;
start = pos + 1;
}
if (!positions.size())
return 0;
StringBuilder b;
size_t lastpos = 0;
for (auto& pos : positions) {
b.append(substring_view(lastpos, pos - lastpos));
b.append(replacement);
lastpos = pos + needle.length();
}
b.append(substring_view(lastpos, length() - lastpos));
m_impl = StringImpl::create(b.build().characters());
return positions.size();
}
String escape_html_entities(const StringView& html)
{
StringBuilder builder;