BAN: Implement more basic functionality

String has more features
StringView was implemented
Basic move semantics are now working
Added file for forward declarations
This commit is contained in:
Bananymous
2022-12-13 20:41:32 +02:00
parent 174daa3e02
commit cb6dee9d91
7 changed files with 284 additions and 12 deletions

View File

@@ -1,5 +1,10 @@
#include <BAN/Errors.h>
#include <BAN/Memory.h>
#include <BAN/Move.h>
#include <BAN/String.h>
#include <BAN/StringView.h>
#include <kernel/Serial.h>
#include <assert.h>
#include <string.h>
@@ -10,18 +15,29 @@ namespace BAN
String::String()
{
MUST(EnsureCapasity(1));
m_data[0] = '\0';
m_size = 0;
MUST(copy_impl("", 0));
}
String::String(const char* string)
String::String(const String& other)
{
size_type len = strlen(string);
MUST(EnsureCapasity(len + 1));
memcpy(m_data, string, len);
m_data[len] = '\0';
m_size = len;
MUST(copy_impl(other.Data(), other.Size()));
}
String::String(String&& other)
{
move_impl(Move(other));
}
String::String(const StringView& other)
{
MUST(copy_impl(other.Data(), other.Size()));
}
String::String(const char* data, size_type len)
{
if (len == size_type(-1))
len = strlen(data);
MUST(copy_impl(data, len));
}
String::~String()
@@ -29,6 +45,19 @@ namespace BAN
BAN::deallocator(m_data);
}
String& String::operator=(const String& other)
{
copy_impl(other.Data(), other.Size());
return *this;
}
String& String::operator=(String&& other)
{
BAN::deallocator(m_data);
move_impl(Move(other));
return *this;
}
ErrorOr<void> String::PushBack(char ch)
{
TRY(EnsureCapasity(m_size + 2));
@@ -80,6 +109,12 @@ namespace BAN
m_size--;
}
void String::Clear()
{
m_data[0] = '\0';
m_size = 0;
}
char String::operator[](size_type index) const
{
assert(index < m_size);
@@ -92,6 +127,13 @@ namespace BAN
return m_data[index];
}
bool String::operator==(const String& other) const
{
if (m_size != other.m_size)
return false;
return memcmp(m_data, other.m_data, m_size) == 0;
}
ErrorOr<void> String::Resize(size_type size, char ch)
{
if (size < m_size)
@@ -113,10 +155,15 @@ namespace BAN
ErrorOr<void> String::Reserve(size_type size)
{
TRY(EnsureCapasity(size + 1));
TRY(EnsureCapasity(size));
return {};
}
StringView String::SV() const
{
return StringView(*this);
}
bool String::Empty() const
{
return m_size == 0;
@@ -152,4 +199,24 @@ namespace BAN
return {};
}
ErrorOr<void> String::copy_impl(const char* data, size_type len)
{
TRY(EnsureCapasity(len + 1));
memcpy(m_data, data, len);
m_data[len] = '\0';
m_size = len;
return {};
}
void String::move_impl(String&& other)
{
m_data = other.m_data;
m_size = other.m_size;
m_capasity = other.m_capasity;
other.m_data = nullptr;
other.m_size = 0;
other.m_capasity = 0;
}
}

93
BAN/BAN/StringView.cpp Normal file
View File

@@ -0,0 +1,93 @@
#include <BAN/String.h>
#include <BAN/StringView.h>
#include <BAN/Vector.h>
#include <assert.h>
#include <string.h>
namespace BAN
{
StringView::StringView()
{ }
StringView::StringView(const String& other)
: StringView(other.Data(), other.Size())
{ }
StringView::StringView(const char* string, size_type len)
{
if (len == size_type(-1))
len = strlen(string);
m_data = string;
m_size = len;
}
char StringView::operator[](size_type index) const
{
assert(index < m_size);
return m_data[index];
}
bool StringView::operator==(const StringView& other) const
{
if (m_size != other.m_size)
return false;
return memcmp(m_data, other.m_data, m_size) == 0;
}
StringView StringView::Substring(size_type index, size_type len) const
{
assert(index <= m_size);
if (len == size_type(-1))
len = m_size - index;
assert(len <= m_size - index); // weird order to avoid overflow
StringView result;
result.m_data = m_data + index;
result.m_size = len;
return result;
}
Vector<StringView> StringView::Split(char delim, bool allow_empties)
{
Vector<StringView> result;
size_type start = 0;
for (size_type i = 0; i < m_size; i++)
{
if (m_data[i] == delim)
{
if (allow_empties || start != i)
result.PushBack(this->Substring(start, i - start));
start = i + 1;
}
}
if (start != m_size)
result.PushBack(this->Substring(start));
return result;
}
StringView::size_type StringView::Count(char ch) const
{
size_type result = 0;
for (size_type i = 0; i < m_size; i++)
if (m_data[i] == ch)
result++;
return result;
}
bool StringView::Empty() const
{
return m_size == 0;
}
StringView::size_type StringView::Size() const
{
return m_size;
}
const char* StringView::Data() const
{
return m_data;
}
}