2022-12-13 15:15:36 +02:00
|
|
|
#pragma once
|
2022-12-13 14:33:38 +02:00
|
|
|
|
2023-01-17 11:21:21 +02:00
|
|
|
#include <BAN/ForwardList.h>
|
2022-12-13 20:41:32 +02:00
|
|
|
#include <BAN/Formatter.h>
|
2022-12-13 14:33:38 +02:00
|
|
|
|
|
|
|
namespace BAN
|
|
|
|
{
|
|
|
|
|
|
|
|
class String
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using size_type = size_t;
|
|
|
|
|
|
|
|
public:
|
2022-12-13 15:07:35 +02:00
|
|
|
String();
|
2022-12-13 20:41:32 +02:00
|
|
|
String(const String&);
|
|
|
|
String(String&&);
|
2023-01-12 23:57:07 +02:00
|
|
|
String(StringView);
|
2022-12-13 14:33:38 +02:00
|
|
|
~String();
|
|
|
|
|
2022-12-27 20:01:05 +02:00
|
|
|
template<typename... Args>
|
2023-02-01 21:05:44 +02:00
|
|
|
static String formatted(const char* format, const Args&... args);
|
2022-12-27 20:01:05 +02:00
|
|
|
|
2022-12-13 20:41:32 +02:00
|
|
|
String& operator=(const String&);
|
|
|
|
String& operator=(String&&);
|
2023-01-13 00:55:58 +02:00
|
|
|
String& operator=(StringView);
|
2022-12-13 20:41:32 +02:00
|
|
|
|
2023-02-19 20:10:30 +02:00
|
|
|
ErrorOr<void> push_back(char);
|
|
|
|
ErrorOr<void> insert(char, size_type);
|
|
|
|
ErrorOr<void> insert(StringView, size_type);
|
|
|
|
ErrorOr<void> append(StringView);
|
|
|
|
ErrorOr<void> append(const String&);
|
2023-02-22 02:07:05 +02:00
|
|
|
|
2023-02-01 21:05:44 +02:00
|
|
|
void pop_back();
|
|
|
|
void remove(size_type);
|
|
|
|
void erase(size_type, size_type);
|
2022-12-13 14:33:38 +02:00
|
|
|
|
2023-02-01 21:05:44 +02:00
|
|
|
void clear();
|
2022-12-13 20:41:32 +02:00
|
|
|
|
2022-12-13 14:33:38 +02:00
|
|
|
char operator[](size_type) const;
|
|
|
|
char& operator[](size_type);
|
|
|
|
|
2022-12-13 20:41:32 +02:00
|
|
|
bool operator==(const String&) const;
|
2022-12-13 20:55:09 +02:00
|
|
|
bool operator==(StringView) const;
|
|
|
|
bool operator==(const char*) const;
|
2022-12-13 20:41:32 +02:00
|
|
|
|
2023-02-19 20:10:30 +02:00
|
|
|
ErrorOr<void> resize(size_type, char = '\0');
|
|
|
|
ErrorOr<void> reserve(size_type);
|
2023-02-22 02:07:05 +02:00
|
|
|
ErrorOr<void> shrink_to_fit();
|
2022-12-13 14:33:38 +02:00
|
|
|
|
2023-02-01 21:05:44 +02:00
|
|
|
StringView sv() const;
|
2022-12-13 20:41:32 +02:00
|
|
|
|
2023-02-01 21:05:44 +02:00
|
|
|
bool empty() const;
|
|
|
|
size_type size() const;
|
|
|
|
size_type capacity() const;
|
2022-12-13 14:33:38 +02:00
|
|
|
|
2023-02-01 21:05:44 +02:00
|
|
|
const char* data() const;
|
2022-12-13 14:33:38 +02:00
|
|
|
|
|
|
|
private:
|
2023-02-19 20:10:30 +02:00
|
|
|
ErrorOr<void> ensure_capacity(size_type);
|
2022-12-13 14:33:38 +02:00
|
|
|
|
2023-02-19 20:10:30 +02:00
|
|
|
ErrorOr<void> copy_impl(StringView);
|
2022-12-13 20:41:32 +02:00
|
|
|
void move_impl(String&&);
|
|
|
|
|
2022-12-13 14:33:38 +02:00
|
|
|
private:
|
|
|
|
char* m_data = nullptr;
|
2023-02-01 21:05:44 +02:00
|
|
|
size_type m_capacity = 0;
|
2022-12-13 14:33:38 +02:00
|
|
|
size_type m_size = 0;
|
|
|
|
};
|
|
|
|
|
2022-12-27 20:01:05 +02:00
|
|
|
template<typename... Args>
|
2023-02-01 21:05:44 +02:00
|
|
|
String String::formatted(const char* format, const Args&... args)
|
2022-12-27 20:01:05 +02:00
|
|
|
{
|
|
|
|
String result;
|
2023-02-01 21:05:44 +02:00
|
|
|
BAN::Formatter::print([&](char c){ result.push_back(c); }, format, args...);
|
2022-12-27 20:01:05 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-12-13 20:41:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace BAN::Formatter
|
|
|
|
{
|
|
|
|
|
2022-12-27 19:55:07 +02:00
|
|
|
template<typename F>
|
2023-03-08 21:31:26 +02:00
|
|
|
void print_argument(F putc, const String& string, const ValueFormat&)
|
2022-12-13 20:41:32 +02:00
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
for (String::size_type i = 0; i < string.size(); i++)
|
2022-12-27 19:55:07 +02:00
|
|
|
putc(string[i]);
|
2022-12-13 20:41:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|