From 941238830e2ce0a907eb6e8f6e2555e796a72b33 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 13 Dec 2022 16:13:38 +0200 Subject: [PATCH] BAN: Now actually build libban :D --- BAN/BAN/String.cpp | 155 +++++++++++++++++++++++++++++++++++++++ BAN/Makefile | 29 ++++---- BAN/include/BAN/Errors.h | 2 +- BAN/include/BAN/String.h | 150 ------------------------------------- 4 files changed, 171 insertions(+), 165 deletions(-) create mode 100644 BAN/BAN/String.cpp diff --git a/BAN/BAN/String.cpp b/BAN/BAN/String.cpp new file mode 100644 index 000000000..5837f65f9 --- /dev/null +++ b/BAN/BAN/String.cpp @@ -0,0 +1,155 @@ +#include +#include + +#include +#include +#include + +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 String::PushBack(char ch) + { + TRY(EnsureCapasity(m_size + 2)); + m_data[m_size] = ch; + m_data[m_size + 1] = '\0'; + m_size++; + return {}; + } + + ErrorOr 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 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 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 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 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 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 {}; + } + +} diff --git a/BAN/Makefile b/BAN/Makefile index a97a49566..e8f223fa8 100644 --- a/BAN/Makefile +++ b/BAN/Makefile @@ -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) \ +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) \ No newline at end of file +-include $(LIBBANK_OBJS:.o=.d) \ No newline at end of file diff --git a/BAN/include/BAN/Errors.h b/BAN/include/BAN/Errors.h index 4eac6be90..6ed44afa9 100644 --- a/BAN/include/BAN/Errors.h +++ b/BAN/include/BAN/Errors.h @@ -71,7 +71,7 @@ private: namespace BAN::Formatter { template - void print_argument_impl(const Error& error, const ValueFormat& format) + void print_argument_impl(const Error& error, const ValueFormat&) { if (error.GetErrorCode() == 0xFF) print(error.GetMessage()); diff --git a/BAN/include/BAN/String.h b/BAN/include/BAN/String.h index 2e9af01e6..dbf877639 100644 --- a/BAN/include/BAN/String.h +++ b/BAN/include/BAN/String.h @@ -1,12 +1,6 @@ #pragma once #include -#include -#include - -#include -#include -#include 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 String::PushBack(char ch) - { - TRY(EnsureCapasity(m_size + 2)); - m_data[m_size] = ch; - m_data[m_size + 1] = '\0'; - m_size++; - return {}; - } - - ErrorOr 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 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 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 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 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 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 {}; - } - } \ No newline at end of file