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

AK: Implement StringView::for_each_split_view

StringView::for_each_split_view allows you to process the splits in a
StringView without needing to allocate a Vector<StringView> to store
each of the parts.

Since we migrated the implementation from the normal split_view path, we
can also re-implement split_view in terms of for_each_split_view.
This commit is contained in:
Brian Gianforcaro 2022-01-09 02:26:45 -08:00 committed by Andreas Kling
parent cd42f64bc7
commit 142e099001
2 changed files with 32 additions and 19 deletions

View file

@ -42,27 +42,10 @@ Vector<StringView> StringView::split_view(const char separator, bool keep_empty)
Vector<StringView> StringView::split_view(StringView separator, bool keep_empty) const
{
VERIFY(!separator.is_empty());
if (is_empty())
return {};
StringView view { *this };
Vector<StringView> parts;
auto maybe_separator_index = find(separator);
while (maybe_separator_index.has_value()) {
auto separator_index = maybe_separator_index.value();
auto part_with_separator = view.substring_view(0, separator_index + separator.length());
if (keep_empty || separator_index > 0)
parts.append(part_with_separator.substring_view(0, separator_index));
view = view.substring_view_starting_after_substring(part_with_separator);
maybe_separator_index = view.find(separator);
}
if (keep_empty || !view.is_empty())
for_each_split_view(separator, keep_empty, [&](StringView view) {
parts.append(view);
});
return parts;
}