AK: Add a keep_empty argument to String[View]::substring{_view}

This commit is contained in:
Sergey Bugaev 2019-09-21 00:43:37 +03:00 committed by Andreas Kling
parent 07ca753124
commit 127d168def
Notes: sideshowbarker 2024-07-19 11:58:03 +09:00
4 changed files with 10 additions and 10 deletions

View file

@ -110,7 +110,7 @@ Vector<String> String::split_limit(const char separator, int limit) const
return v;
}
Vector<StringView> String::split_view(const char separator) const
Vector<StringView> String::split_view(const char separator, bool keep_empty) const
{
if (is_empty())
return {};
@ -121,15 +121,15 @@ Vector<StringView> String::split_view(const char separator) const
char ch = characters()[i];
if (ch == separator) {
int sublen = i - substart;
if (sublen != 0)
if (sublen != 0 || keep_empty)
v.append(substring_view(substart, sublen));
substart = i + 1;
}
}
int taillen = length() - substart;
if (taillen != 0)
if (taillen != 0 || keep_empty)
v.append(substring_view(substart, taillen));
if (characters()[length() - 1] == separator)
if (characters()[length() - 1] == separator && keep_empty)
v.append(empty());
return v;
}

View file

@ -114,7 +114,7 @@ public:
Vector<String> split(char separator) const;
String substring(int start, int length) const;
Vector<StringView> split_view(char separator) const;
Vector<StringView> split_view(char separator, bool keep_empty = false) const;
StringView substring_view(int start, int length) const;
bool is_null() const { return !m_impl; }

View file

@ -16,7 +16,7 @@ StringView::StringView(const ByteBuffer& buffer)
{
}
Vector<StringView> StringView::split_view(const char separator) const
Vector<StringView> StringView::split_view(const char separator, bool keep_empty) const
{
if (is_empty())
return {};
@ -27,15 +27,15 @@ Vector<StringView> StringView::split_view(const char separator) const
char ch = characters_without_null_termination()[i];
if (ch == separator) {
ssize_t sublen = i - substart;
if (sublen != 0)
if (sublen != 0 || keep_empty)
v.append(substring_view(substart, sublen));
substart = i + 1;
}
}
ssize_t taillen = length() - substart;
if (taillen != 0)
if (taillen != 0 || keep_empty)
v.append(substring_view(substart, taillen));
if (characters_without_null_termination()[length() - 1] == separator)
if (characters_without_null_termination()[length() - 1] == separator && keep_empty)
v.append(String::empty());
return v;
}

View file

@ -44,7 +44,7 @@ public:
bool starts_with(const StringView&) const;
StringView substring_view(int start, int length) const;
Vector<StringView> split_view(char) const;
Vector<StringView> split_view(char, bool keep_empty = false) const;
// FIXME: These should be shared between String and StringView somehow!
unsigned to_uint(bool& ok) const;