AK: Also add a keep_empty argument to String::split[_limit]()

Just like String[View]::split_view() has already.
This commit is contained in:
Sergey Bugaev 2020-01-22 17:17:27 +03:00 committed by Andreas Kling
parent 9cbce68b1d
commit 6a64077ed7
Notes: sideshowbarker 2024-07-19 09:53:37 +09:00
2 changed files with 7 additions and 9 deletions

View file

@ -131,12 +131,12 @@ StringView String::substring_view(size_t start, size_t length) const
return { characters() + start, length };
}
Vector<String> String::split(const char separator) const
Vector<String> String::split(char separator, bool keep_empty) const
{
return split_limit(separator, 0);
return split_limit(separator, 0, keep_empty);
}
Vector<String> String::split_limit(const char separator, size_t limit) const
Vector<String> String::split_limit(char separator, size_t limit, bool keep_empty) const
{
if (is_empty())
return {};
@ -147,16 +147,14 @@ Vector<String> String::split_limit(const char separator, size_t limit) const
char ch = characters()[i];
if (ch == separator) {
size_t sublen = i - substart;
if (sublen != 0)
if (sublen != 0 || keep_empty)
v.append(substring(substart, sublen));
substart = i + 1;
}
}
size_t taillen = length() - substart;
if (taillen != 0)
if (taillen != 0 || keep_empty)
v.append(substring(substart, taillen));
if (characters()[length() - 1] == separator)
v.append(empty());
return v;
}

View file

@ -140,8 +140,8 @@ public:
bool contains(const String&) const;
Vector<String> split_limit(char separator, size_t limit) const;
Vector<String> split(char separator) const;
Vector<String> split_limit(char separator, size_t limit, bool keep_empty = false) const;
Vector<String> split(char separator, bool keep_empty = false) const;
String substring(size_t start, size_t length) const;
Vector<StringView> split_view(char separator, bool keep_empty = false) const;