mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Add a keep_empty argument to String[View]::substring{_view}
This commit is contained in:
parent
07ca753124
commit
127d168def
Notes:
sideshowbarker
2024-07-19 11:58:03 +09:00
Author: https://github.com/bugaevc Commit: https://github.com/SerenityOS/serenity/commit/127d168def2 Pull-request: https://github.com/SerenityOS/serenity/pull/609 Reviewed-by: https://github.com/awesomekling ✅
4 changed files with 10 additions and 10 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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; }
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue