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

14
BAN/include/BAN/Forward.h Normal file
View File

@@ -0,0 +1,14 @@
#pragma once
template<typename T> class ErrorOr;
namespace BAN
{
class String;
class StringView;
template<typename T> class Vector;
template<typename T> class Queue;
}

19
BAN/include/BAN/Move.h Normal file
View File

@@ -0,0 +1,19 @@
#pragma once
namespace BAN
{
template <class T>
struct RemoveReference { typedef T type; };
template <class T>
struct RemoveReference<T&> { typedef T type; };
template <class T>
struct RemoveReference<T&&> { typedef T type; };
template<class T>
typename RemoveReference<T>::type&&
Move( T&& Arg ) { return (typename RemoveReference<T>::type&&)Arg; }
}

View File

@@ -1,6 +1,8 @@
#pragma once
#include <BAN/Errors.h>
#include <BAN/Forward.h>
#include <BAN/Formatter.h>
namespace BAN
{
@@ -12,9 +14,15 @@ namespace BAN
public:
String();
String(const char*);
String(const String&);
String(String&&);
String(const StringView&);
String(const char*, size_type = -1);
~String();
String& operator=(const String&);
String& operator=(String&&);
ErrorOr<void> PushBack(char);
ErrorOr<void> Insert(char, size_type);
ErrorOr<void> Append(const char*);
@@ -23,12 +31,18 @@ namespace BAN
void PopBack();
void Remove(size_type);
void Clear();
char operator[](size_type) const;
char& operator[](size_type);
bool operator==(const String&) const;
ErrorOr<void> Resize(size_type, char = '\0');
ErrorOr<void> Reserve(size_type);
StringView SV() const;
bool Empty() const;
size_type Size() const;
size_type Capasity() const;
@@ -38,10 +52,24 @@ namespace BAN
private:
ErrorOr<void> EnsureCapasity(size_type);
ErrorOr<void> copy_impl(const char*, size_type);
void move_impl(String&&);
private:
char* m_data = nullptr;
size_type m_capasity = 0;
size_type m_size = 0;
};
}
}
namespace BAN::Formatter
{
template<void(*PUTC_LIKE)(char)> void print_argument_impl(const String& string, const ValueFormat&)
{
for (String::size_type i = 0; i < string.Size(); i++)
PUTC_LIKE(string[i]);
}
}

View File

@@ -0,0 +1,50 @@
#pragma once
#include <BAN/Forward.h>
#include <BAN/Formatter.h>
namespace BAN
{
class StringView
{
public:
using size_type = size_t;
public:
StringView();
StringView(const String&);
StringView(const char*, size_type = -1);
char operator[](size_type) const;
bool operator==(const StringView&) const;
StringView Substring(size_type, size_type = -1) const;
Vector<StringView> Split(char, bool = false);
size_type Count(char) const;
bool Empty() const;
size_type Size() const;
const char* Data() const;
private:
const char* m_data = nullptr;
size_type m_size = 0;
};
}
namespace BAN::Formatter
{
template<void(*PUTC_LIKE)(char)> void print_argument_impl(const StringView& sv, const ValueFormat&)
{
for (StringView::size_type i = 0; i < sv.Size(); i++)
PUTC_LIKE(sv[i]);
}
}