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:
parent
cd42f64bc7
commit
142e099001
2 changed files with 32 additions and 19 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue