mirror of
https://github.com/fergalmoran/ladybird.git
synced 2026-01-06 08:36:15 +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:
@@ -427,6 +427,20 @@ String to_titlecase(StringView const& str)
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
// TODO: Benchmark against KMP (AK/MemMem.h) and switch over if it's faster for short strings too
|
||||
size_t count(StringView const& str, StringView const& needle)
|
||||
{
|
||||
if (needle.is_empty())
|
||||
return str.length();
|
||||
|
||||
size_t count = 0;
|
||||
for (size_t i = 0; i < str.length() - needle.length() + 1; ++i) {
|
||||
if (str.substring_view(i).starts_with(needle))
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user