BAN: Make StringView::split const and fix bug with empties

This commit is contained in:
Bananymous 2024-01-03 00:14:01 +02:00
parent 1bd33e76e5
commit d2bc399770
2 changed files with 6 additions and 6 deletions

View File

@ -61,7 +61,7 @@ namespace BAN
return result; return result;
} }
ErrorOr<Vector<StringView>> StringView::split(char delim, bool allow_empties) ErrorOr<Vector<StringView>> StringView::split(char delim, bool allow_empties) const
{ {
size_type count = 0; size_type count = 0;
{ {
@ -92,12 +92,12 @@ namespace BAN
start = i + 1; start = i + 1;
} }
} }
if (start != m_size) if (start < m_size || (start == m_size && allow_empties))
TRY(result.push_back(this->substring(start))); TRY(result.push_back(this->substring(start)));
return result; return result;
} }
ErrorOr<Vector<StringView>> StringView::split(bool(*comp)(char), bool allow_empties) ErrorOr<Vector<StringView>> StringView::split(bool(*comp)(char), bool allow_empties) const
{ {
size_type count = 0; size_type count = 0;
{ {
@ -128,7 +128,7 @@ namespace BAN
start = i + 1; start = i + 1;
} }
} }
if (start != m_size) if (start < m_size || (start == m_size && allow_empties))
TRY(result.push_back(this->substring(start))); TRY(result.push_back(this->substring(start)));
return result; return result;
} }

View File

@ -30,8 +30,8 @@ namespace BAN
StringView substring(size_type, size_type = -1) const; StringView substring(size_type, size_type = -1) const;
ErrorOr<Vector<StringView>> split(char, bool = false); ErrorOr<Vector<StringView>> split(char, bool = false) const;
ErrorOr<Vector<StringView>> split(bool(*comp)(char), bool = false); ErrorOr<Vector<StringView>> split(bool(*comp)(char), bool = false) const;
char back() const; char back() const;
char front() const; char front() const;