|
@@ -75,6 +75,32 @@ Vector<StringView> StringView::split_view(const char separator, bool keep_empty)
|
|
|
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
|
|
|
{
|
|
|
if (is_empty())
|
|
@@ -244,7 +270,7 @@ Optional<size_t> StringView::find_first_of(const StringView& view) const
|
|
|
|
|
|
Optional<size_t> StringView::find_last_of(char c) const
|
|
|
{
|
|
|
- for (size_t pos = m_length; --pos >0;) {
|
|
|
+ for (size_t pos = m_length; --pos > 0;) {
|
|
|
if (m_characters[pos] == c)
|
|
|
return pos;
|
|
|
}
|