1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 15:28:11 +00:00

AK: Make String::count not use strstr and take a StringView

This was needlessly copying StringView arguments, and was also using
strstr internally, which meant it was doing a bunch of unnecessary
strlen calls on it. This also moves the implementation to StringUtils
to allow API consistency between String and StringView.
This commit is contained in:
Idan Horowitz 2021-09-11 01:02:24 +03:00
parent 4e40eaf34c
commit 6d2b003b6e
5 changed files with 19 additions and 17 deletions

View file

@ -382,22 +382,6 @@ int String::replace(const String& needle, const String& replacement, bool all_oc
return positions.size();
}
size_t String::count(const String& needle) const
{
size_t count = 0;
size_t start = 0, pos;
for (;;) {
const char* ptr = strstr(characters() + start, needle.characters());
if (!ptr)
break;
pos = ptr - characters();
count++;
start = pos + 1;
}
return count;
}
String String::reverse() const
{
StringBuilder reversed_string(length());