1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 08:04:57 +00:00

AK: Implement StringView::find_all()

This implements the StringView::find_all() method by re-implemeting the
current method existing for String in StringUtils, and using that
implementation for both String and StringView.

The rewrite uses memmem() instead of strstr(), so the String::find_all()
argument type has been changed from String to StringView, as the null
byte is no longer required.
This commit is contained in:
Max Wipfli 2021-07-01 17:00:34 +02:00 committed by Andreas Kling
parent 3bdaed501e
commit d7a104c27c
4 changed files with 19 additions and 18 deletions

View file

@ -362,6 +362,22 @@ Optional<size_t> find_last(StringView const& haystack, char needle)
return {};
}
Vector<size_t> find_all(StringView const& haystack, StringView const& needle)
{
Vector<size_t> positions;
size_t current_position = 0;
while (current_position <= haystack.length()) {
auto maybe_position = AK::memmem_optional(
haystack.characters_without_null_termination() + current_position, haystack.length() - current_position,
needle.characters_without_null_termination(), needle.length());
if (!maybe_position.has_value())
break;
positions.append(current_position + *maybe_position);
current_position += *maybe_position + 1;
}
return positions;
}
String to_snakecase(const StringView& str)
{
auto should_insert_underscore = [&](auto i, auto current_char) {