forked from Bananymous/banan-os
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:
parent
7ae00ac76e
commit
6732cabf20
|
@ -63,13 +63,20 @@ namespace BAN
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<Vector<StringView>> StringView::Split(char delim, bool allow_empties)
|
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 count = 0;
|
||||||
{
|
{
|
||||||
size_type start = 0;
|
size_type start = 0;
|
||||||
for (size_type i = 0; i < m_size; i++)
|
for (size_type i = 0; i < m_size; i++)
|
||||||
{
|
{
|
||||||
if (m_data[i] == delim)
|
if (comp(m_data[i]))
|
||||||
{
|
{
|
||||||
if (allow_empties || start != i)
|
if (allow_empties || start != i)
|
||||||
count++;
|
count++;
|
||||||
|
@ -86,7 +93,7 @@ namespace BAN
|
||||||
size_type start = 0;
|
size_type start = 0;
|
||||||
for (size_type i = 0; i < m_size; i++)
|
for (size_type i = 0; i < m_size; i++)
|
||||||
{
|
{
|
||||||
if (m_data[i] == delim)
|
if (comp(m_data[i]))
|
||||||
{
|
{
|
||||||
if (allow_empties || start != i)
|
if (allow_empties || start != i)
|
||||||
result.PushBack(this->Substring(start, i - start));
|
result.PushBack(this->Substring(start, i - start));
|
||||||
|
|
|
@ -16,7 +16,7 @@ LIBDIR?=$(EXEC_PREFIX)/lib
|
||||||
CFLAGS:=$(CFLAGS) -Iinclude -ffreestanding -Wall -Wextra
|
CFLAGS:=$(CFLAGS) -Iinclude -ffreestanding -Wall -Wextra
|
||||||
CPPFLAGS:=$(CPPFLAGS)
|
CPPFLAGS:=$(CPPFLAGS)
|
||||||
LIBBANK_CFLAGS:=$(CFLAGS) -D__is_kernel -Iinclude -ffreestanding -Wall -Wextra
|
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)
|
ARCHDIR=arch/$(HOSTARCH)
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,7 @@ 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);
|
||||||
|
ErrorOr<Vector<StringView>> Split(bool(*comp)(char), bool = false);
|
||||||
|
|
||||||
size_type Count(char) const;
|
size_type Count(char) const;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue