BAN: StringView::Split now accepts a function

Original overload with char is not threadsafe anymore, but I don't
want to duplicate code :)
This commit is contained in:
Bananymous 2022-12-20 11:50:32 +02:00
parent 7ae00ac76e
commit 6732cabf20
3 changed files with 11 additions and 3 deletions

View File

@ -63,13 +63,20 @@ namespace BAN
}
ErrorOr<Vector<StringView>> StringView::Split(char delim, bool allow_empties)
{
// FIXME: Won't work while multithreading
static char s_delim = delim;
return Split([](char c){ return c == s_delim; }, allow_empties);
}
ErrorOr<Vector<StringView>> StringView::Split(bool(*comp)(char), bool allow_empties)
{
size_type count = 0;
{
size_type start = 0;
for (size_type i = 0; i < m_size; i++)
{
if (m_data[i] == delim)
if (comp(m_data[i]))
{
if (allow_empties || start != i)
count++;
@ -86,7 +93,7 @@ namespace BAN
size_type start = 0;
for (size_type i = 0; i < m_size; i++)
{
if (m_data[i] == delim)
if (comp(m_data[i]))
{
if (allow_empties || start != i)
result.PushBack(this->Substring(start, i - start));

View File

@ -16,7 +16,7 @@ LIBDIR?=$(EXEC_PREFIX)/lib
CFLAGS:=$(CFLAGS) -Iinclude -ffreestanding -Wall -Wextra
CPPFLAGS:=$(CPPFLAGS)
LIBBANK_CFLAGS:=$(CFLAGS) -D__is_kernel -Iinclude -ffreestanding -Wall -Wextra
LIBBANK_CPPFLAGS:=$(CPPFLAGS) -fno-rtti -fno-exceptions
LIBBANK_CPPFLAGS:=$(CPPFLAGS) -fno-rtti -fno-exceptions -fno-threadsafe-statics
ARCHDIR=arch/$(HOSTARCH)

View File

@ -25,6 +25,7 @@ namespace BAN
StringView Substring(size_type, size_type = -1) const;
ErrorOr<Vector<StringView>> Split(char, bool = false);
ErrorOr<Vector<StringView>> Split(bool(*comp)(char), bool = false);
size_type Count(char) const;