BAN: Now actually build libban :D

This commit is contained in:
Bananymous 2022-12-13 16:13:38 +02:00
parent 57b5e7ecf3
commit 941238830e
4 changed files with 171 additions and 165 deletions

155
BAN/BAN/String.cpp Normal file
View File

@ -0,0 +1,155 @@
#include <BAN/Memory.h>
#include <BAN/String.h>
#include <assert.h>
#include <string.h>
#include <sys/param.h>
namespace BAN
{
String::String()
{
MUST(EnsureCapasity(1));
m_data[0] = '\0';
m_size = 0;
}
String::String(const char* string)
{
size_type len = strlen(string);
MUST(EnsureCapasity(len + 1));
memcpy(m_data, string, len);
m_data[len] = '\0';
m_size = len;
}
String::~String()
{
BAN::deallocator(m_data);
}
ErrorOr<void> String::PushBack(char ch)
{
TRY(EnsureCapasity(m_size + 2));
m_data[m_size] = ch;
m_data[m_size + 1] = '\0';
m_size++;
return {};
}
ErrorOr<void> String::Insert(char ch, size_type index)
{
assert(index <= m_size);
TRY(EnsureCapasity(m_size + 2));
memmove(m_data + index + 1, m_data + index, m_size - index);
m_data[index] = ch;
m_data[m_size + 1] = '\0';
m_size++;
return {};
}
ErrorOr<void> String::Append(const char* string)
{
size_t len = strlen(string);
TRY(EnsureCapasity(m_size + len + 1));
memcpy(m_data + m_size, string, len);
m_data[m_size + len] = '\0';
m_size += len;
return {};
}
ErrorOr<void> String::Append(const String& string)
{
TRY(Append(string.Data()));
return {};
}
void String::PopBack()
{
assert(m_size > 0);
m_data[m_size - 1] = '\0';
m_size--;
}
void String::Remove(size_type index)
{
assert(index < m_size);
memmove(m_data + index, m_data + index + 1, m_size - index - 1);
m_data[m_size - 1] = '\0';
m_size--;
}
char String::operator[](size_type index) const
{
assert(index < m_size);
return m_data[index];
}
char& String::operator[](size_type index)
{
assert(index < m_size);
return m_data[index];
}
ErrorOr<void> String::Resize(size_type size, char ch)
{
if (size < m_size)
{
m_data[size] = '\0';
m_size = size;
}
else if (size > m_size)
{
TRY(EnsureCapasity(size + 1));
for (size_type i = m_size; i < size; i++)
m_data[i] = ch;
m_data[size] = '\0';
m_size = size;
}
m_size = size;
return {};
}
ErrorOr<void> String::Reserve(size_type size)
{
TRY(EnsureCapasity(size + 1));
return {};
}
bool String::Empty() const
{
return m_size == 0;
}
String::size_type String::Size() const
{
return m_size;
}
String::size_type String::Capasity() const
{
return m_capasity;
}
const char* String::Data() const
{
return m_data;
}
ErrorOr<void> String::EnsureCapasity(size_type size)
{
if (m_capasity >= size)
return {};
size_type new_cap = MAX(size, m_capasity * 1.5f);
void* new_data = BAN::allocator(new_cap);
if (new_data == nullptr)
return Error::FromString("String: Could not allocate memory");
memcpy(new_data, m_data, m_size + 1);
BAN::deallocator(m_data);
m_data = (char*)new_data;
m_capasity = new_cap;
return {};
}
}

View File

@ -13,10 +13,10 @@ EXEC_PREFIX?=$(PREFIX)
INCLUDEDIR?=$(PREFIX)/include
LIBDIR?=$(EXEC_PREFIX)/lib
CFLAGS:=$(CFLAGS) -D__is_ban -Iinclude -ffreestanding -Wall -Wextra
CFLAGS:=$(CFLAGS) -Iinclude -ffreestanding -Wall -Wextra
CPPFLAGS:=$(CPPFLAGS)
LIBK_CFLAGS:=$(CFLAGS) -D__is_kernel
LIBK_CPPFLAGS:=$(CPPFLAGS)
LIBBANK_CFLAGS:=$(CFLAGS) -D__is_kernel -Iinclude -ffreestanding -Wall -Wextra
LIBBANK_CPPFLAGS:=$(CPPFLAGS) -fno-rtti -fno-exceptions
ARCHDIR=arch/$(HOSTARCH)
@ -24,13 +24,14 @@ include $(ARCHDIR)/make.config
CFLAGS:=$(CFLAGS) $(ARCH_CFLAGS)
CPPFLAGS:=$(CPPFLAGS) $(ARCH_CPPFLAGS)
BANK_CFLAGS:=$(BANK_CFLAGS) $(KERNEL_ARCH_CFLAGS)
BANK_CPPFLAGS:=$(BANK_CPPFLAGS) $(KERNEL_ARCH_CPPFLAGS)
LIBBANK_CFLAGS:=$(LIBBANK_CFLAGS) $(KERNEL_ARCH_CFLAGS)
LIBBANK_CPPFLAGS:=$(LIBBANK_CPPFLAGS) $(KERNEL_ARCH_CPPFLAGS)
BUILDDIR=$(abspath build)
FREEOBJS= \
$(ARCH_FREEOBJS) \
BAN/String.o \
HOSTEDOBJS=\
$(ARCH_HOSTEDOBJS) \
@ -39,7 +40,7 @@ OBJS=\
$(FREEOBJS) \
$(HOSTEDOBJS) \
BANK_OBJS=$(FREEOBJS:.o=.bank.o)
LIBBANK_OBJS=$(FREEOBJS:.o=.bank.o)
BINARIES=libbank.a
@ -51,8 +52,8 @@ all: $(BINARIES)
libban.a: always $(OBJS)
cd $(BUILDDIR) && $(AR) rcs $@ $(OBJS)
libbank.a: always $(LIBK_OBJS)
cd $(BUILDDIR) && $(AR) rcs $@ $(LIBK_OBJS)
libbank.a: always $(LIBBANK_OBJS)
cd $(BUILDDIR) && $(AR) rcs $@ $(LIBBANK_OBJS)
.cpp.o:
$(CXX) -MD -c $< -o $(BUILDDIR)/$@ $(CFLAGS) $(CPPFLAGS)
@ -61,16 +62,16 @@ libbank.a: always $(LIBK_OBJS)
$(CXX) -MD -c $< -o $(BUILDDIR)/$@ $(CFLAGS) $(CPPFLAGS)
.cpp.bank.o:
$(CXX) -MD -c $< -o $(BUILDDIR)/$@ $(LIBK_CFLAGS) $(LIBK_CPPFLAGS)
$(CXX) -MD -c $< -o $(BUILDDIR)/$@ $(LIBBANK_CFLAGS) $(LIBBANK_CPPFLAGS)
.S.bank.o:
$(CXX) -MD -c $< -o $(BUILDDIR)/$@ $(LIBK_CFLAGS) $(LIBK_CPPFLAGS)
$(CXX) -MD -c $< -o $(BUILDDIR)/$@ $(LIBBANK_CFLAGS) $(LIBBANK_CPPFLAGS)
clean:
rm -rf $(BUILDDIR)
always:
mkdir -p $(BUILDDIR)
mkdir -p $(BUILDDIR)/BAN
install: install-headers install-libs
@ -83,4 +84,4 @@ install-libs: $(BINARIES)
cp $(BUILDDIR)/$(BINARIES) $(DESTDIR)$(LIBDIR)
-include $(OBJS:.o=.d)
-include $(LIBK_OBJS:.o=.d)
-include $(LIBBANK_OBJS:.o=.d)

View File

@ -71,7 +71,7 @@ private:
namespace BAN::Formatter
{
template<void(*PUTC_LIKE)(char)>
void print_argument_impl(const Error& error, const ValueFormat& format)
void print_argument_impl(const Error& error, const ValueFormat&)
{
if (error.GetErrorCode() == 0xFF)
print<PUTC_LIKE>(error.GetMessage());

View File

@ -1,12 +1,6 @@
#pragma once
#include <BAN/Errors.h>
#include <BAN/Formatter.h>
#include <BAN/Memory.h>
#include <assert.h>
#include <string.h>
#include <sys/param.h>
namespace BAN
{
@ -50,148 +44,4 @@ namespace BAN
size_type m_size = 0;
};
String::String()
{
MUST(EnsureCapasity(1));
m_data[0] = '\0';
m_size = 0;
}
String::String(const char* string)
{
size_type len = strlen(string);
MUST(EnsureCapasity(len + 1));
memcpy(m_data, string, len);
m_data[len] = '\0';
m_size = len;
}
String::~String()
{
BAN::deallocator(m_data);
}
ErrorOr<void> String::PushBack(char ch)
{
TRY(EnsureCapasity(m_size + 2));
m_data[m_size] = ch;
m_data[m_size + 1] = '\0';
m_size++;
return {};
}
ErrorOr<void> String::Insert(char ch, size_type index)
{
assert(index <= m_size);
TRY(EnsureCapasity(m_size + 2));
memmove(m_data + index + 1, m_data + index, m_size - index);
m_data[index] = ch;
m_data[m_size + 1] = '\0';
m_size++;
return {};
}
ErrorOr<void> String::Append(const char* string)
{
size_t len = strlen(string);
TRY(EnsureCapasity(m_size + len + 1));
memcpy(m_data + m_size, string, len);
m_data[m_size + len] = '\0';
m_size += len;
return {};
}
ErrorOr<void> String::Append(const String& string)
{
TRY(Append(string.Data()));
return {};
}
void String::PopBack()
{
assert(m_size > 0);
m_data[m_size - 1] = '\0';
m_size--;
}
void String::Remove(size_type index)
{
assert(index < m_size);
memmove(m_data + index, m_data + index + 1, m_size - index - 1);
m_data[m_size - 1] = '\0';
m_size--;
}
char String::operator[](size_type index) const
{
assert(index < m_size);
return m_data[index];
}
char& String::operator[](size_type index)
{
assert(index < m_size);
return m_data[index];
}
ErrorOr<void> String::Resize(size_type size, char ch)
{
if (size < m_size)
{
m_data[size] = '\0';
m_size = size;
}
else if (size > m_size)
{
TRY(EnsureCapasity(size + 1));
for (size_type i = m_size; i < size; i++)
m_data[i] = ch;
m_data[size] = '\0';
m_size = size;
}
m_size = size;
return {};
}
ErrorOr<void> String::Reserve(size_type size)
{
TRY(EnsureCapasity(size + 1));
return {};
}
bool String::Empty() const
{
return m_size == 0;
}
String::size_type String::Size() const
{
return m_size;
}
String::size_type String::Capasity() const
{
return m_capasity;
}
const char* String::Data() const
{
return m_data;
}
ErrorOr<void> String::EnsureCapasity(size_type size)
{
if (m_capasity >= size)
return {};
size_type new_cap = MAX(size, m_capasity * 1.5f);
void* new_data = BAN::allocator(new_cap);
if (new_data == nullptr)
return Error::FromString("String: Could not allocate memory");
memcpy(new_data, m_data, m_size + 1);
BAN::deallocator(m_data);
m_data = (char*)new_data;
m_capasity = new_cap;
return {};
}
}