mirror of
https://github.com/RGBCube/serenity
synced 2025-07-23 12:47:41 +00:00
AK: Add StringView::split_view() taking a StringView
Since the task of splitting a string via another is pretty common, we might as well have this overload of split_view() as well.
This commit is contained in:
parent
d83d46fd7a
commit
f9bf2c7e78
2 changed files with 28 additions and 1 deletions
|
@ -75,6 +75,32 @@ Vector<StringView> StringView::split_view(const char separator, bool keep_empty)
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vector<StringView> StringView::split_view(const StringView& separator, bool keep_empty) const
|
||||||
|
{
|
||||||
|
ASSERT(!separator.is_empty());
|
||||||
|
|
||||||
|
if (is_empty())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
StringView view { *this };
|
||||||
|
|
||||||
|
Vector<StringView> parts;
|
||||||
|
|
||||||
|
auto maybe_separator_index = find_first_of(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_first_of(separator);
|
||||||
|
}
|
||||||
|
if (keep_empty || !view.is_empty())
|
||||||
|
parts.append(view);
|
||||||
|
|
||||||
|
return parts;
|
||||||
|
}
|
||||||
|
|
||||||
Vector<StringView> StringView::lines(bool consider_cr) const
|
Vector<StringView> StringView::lines(bool consider_cr) const
|
||||||
{
|
{
|
||||||
if (is_empty())
|
if (is_empty())
|
||||||
|
|
|
@ -88,6 +88,7 @@ public:
|
||||||
|
|
||||||
StringView substring_view(size_t start, size_t length) const;
|
StringView substring_view(size_t start, size_t length) const;
|
||||||
Vector<StringView> split_view(char, bool keep_empty = false) const;
|
Vector<StringView> split_view(char, bool keep_empty = false) const;
|
||||||
|
Vector<StringView> split_view(const StringView&, bool keep_empty = false) const;
|
||||||
|
|
||||||
// Create a Vector of StringViews split by line endings. As of CommonMark
|
// Create a Vector of StringViews split by line endings. As of CommonMark
|
||||||
// 0.29, the spec defines a line ending as "a newline (U+000A), a carriage
|
// 0.29, the spec defines a line ending as "a newline (U+000A), a carriage
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue