123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619 |
- #ifndef CORE_ARRAY_STRING_HPP
- #define CORE_ARRAY_STRING_HPP
- #include "core/math/Math.hpp"
- #include "core/utils/Check.hpp"
- #include "core/utils/Error.hpp"
- #include "core/utils/Meta.hpp"
- #include "core/utils/Types.hpp"
- #include "core/utils/Utility.hpp"
- namespace Core {
- template<typename T>
- constexpr int stringLength(const T* c) {
- const T* i = c + 1;
- while(*(c++) != '\0') {}
- return static_cast<int>(c - i);
- }
- Error readUnicode(c32& u, const char*& s);
- template<int N>
- class Char32String;
- class CharString {
- protected:
- i32 length;
- i32 capacity;
- u32 hash;
- char* data;
- public:
- CharString(char* buffer, i32 bufferSize);
- CharString(const CharString&) = delete;
- CharString& operator=(const CharString&) = delete;
- check_return Error copyFrom(const CharString& s);
- bool operator==(const char* s) const;
- bool operator==(const CharString& other) const;
- bool operator!=(const char* s) const;
- bool operator!=(const CharString& other) const;
- char operator[](int index) const;
- int getLength() const;
- int getCapacity() const;
- check_return Error append(char c);
- check_return Error append(signed char c);
- check_return Error append(unsigned char c);
- check_return Error append(wchar_t c);
- check_return Error append(c32 c);
- check_return Error append(const char* s);
- check_return Error append(const c32* s);
- check_return Error append(const signed char* s);
- check_return Error append(const unsigned char* s);
- check_return Error append(signed short s);
- check_return Error append(unsigned short s);
- check_return Error append(signed int i);
- check_return Error append(unsigned int i);
- check_return Error append(signed long l);
- check_return Error append(unsigned long l);
- check_return Error append(signed long long ll);
- check_return Error append(unsigned long long ll);
- check_return Error append(float f);
- check_return Error append(double d);
- check_return Error append(long double ld);
- check_return Error append(bool b);
- check_return Error append(Error e);
- template<typename T>
- check_return Error append(const T& t) {
- return t.toString(*this);
- }
- check_return Error toString(CharString& s) const;
- template<int L>
- check_return Error toString(Char32String<L>& s) const {
- return s.append(static_cast<const char*>(data));
- }
- void clear();
- u32 hashCode() const;
- check_return Error print() const;
- check_return Error printLine() const;
- template<typename... Args>
- check_return Error format(CharString& s, Args&&... args) {
- Error e = formatBuffer(s, 0, Core::forward<Args>(args)...);
- if(e == Error::NONE) {
- return copyFrom(s);
- } else if(e == Error::CAPACITY_REACHED) {
- (void)copyFrom(s);
- }
- return e;
- }
- bool startsWidth(const CharString& other, int from = 0) const;
- int search(const CharString& other, int from = 0) const;
- bool contains(const CharString& other, int from = 0) const;
- int search(char u, int from = 0) const;
- bool contains(char u, int from = 0) const;
- check_return Error substring(CharString& s, int from, int to) const;
- check_return Error substring(CharString& s, int from = 0) const;
- check_return Error replace(CharString& s, const CharString& search,
- const CharString& replace);
- void replace(char search, char replace);
- operator const char*() const;
- private:
- Error add(char c);
- void addToHash(c32 u);
- template<typename T, typename... Args>
- check_return Error formatBuffer(CharString& s, int index, const T& t,
- Args&&... args) {
- while(index < length) {
- char u = data[index++];
- if(u == '#') {
- if(index >= length ||
- (index < length && data[index] != '#')) {
- break;
- }
- index++;
- }
- CORE_RETURN_ERROR(s.append(u));
- }
- CORE_RETURN_ERROR(s.append(t));
- return formatBuffer(s, index, Core::forward<Args>(args)...);
- }
- check_return Error formatBuffer(CharString& s, int index);
- template<typename T>
- check_return Error convertAppend(T t) {
- char buffer[64];
- CORE_RETURN_ERROR(Core::toString(t, buffer, CORE_SIZE(buffer)));
- return append(static_cast<const char*>(buffer));
- }
- };
- template<int N>
- class ArrayCharString final : public CharString {
- static_assert(N > 0, "size of array string must be positive");
- char data[static_cast<unsigned int>(N)];
- public:
- ArrayCharString() : CharString(data, N) {
- }
- ArrayCharString(const ArrayCharString& other) : CharString(data, N) {
- Core::memoryCopy(data, other.data, N);
- length = other.length;
- hash = other.hash;
- }
- ArrayCharString& operator=(const ArrayCharString& other) {
- if(this != &other) {
- Core::memoryCopy(data, other.data, N);
- length = other.length;
- hash = other.hash;
- }
- return *this;
- }
- template<typename... Args>
- check_return Error format(Args&&... args) {
- ArrayCharString s;
- return CharString::format(s, Core::forward<Args>(args)...);
- }
- check_return Error replace(const CharString& search,
- const CharString& replace) {
- ArrayCharString s;
- return CharString::replace(s, search, replace);
- }
- using CharString::replace;
- };
- template<int N>
- class Char32String final {
- int length;
- u32 hash;
- static_assert(N > 0, "size of array string must be positive");
- c32 data[static_cast<unsigned int>(N)];
- public:
- Char32String() : length(0), hash(0) {
- data[0] = '\0';
- }
- bool operator==(const c32* s) const {
- const c32* p = data;
- while(*s == *p && *s != '\0') {
- s++;
- p++;
- }
- return *s == *p;
- }
- template<int L>
- bool operator==(const Char32String<L>& other) const {
- if(length != other.getLength()) {
- return false;
- }
- for(int i = 0; i < length; i++) {
- if(data[i] != other[i]) {
- return false;
- }
- }
- return true;
- }
- bool operator!=(const c32* s) const {
- return !((*this) == s);
- }
- template<int L>
- bool operator!=(const Char32String<L>& other) const {
- return !((*this) == other);
- }
- c32 operator[](int index) const {
- return data[index];
- }
- int getLength() const {
- return length;
- }
- constexpr int getCapacity() const {
- return N - 1;
- }
- check_return Error append(char c) {
- return add(static_cast<c32>(c));
- }
- check_return Error append(signed char c) {
- return append(static_cast<char>(c));
- }
- check_return Error append(unsigned char c) {
- return append(static_cast<char>(c));
- }
- check_return Error append(wchar_t c) {
- return append(static_cast<c32>(c));
- }
- check_return Error append(c32 c) {
- if constexpr(IsSame<c32, char>) {
- char buffer[5];
- unicodeToChar(c, buffer);
- return append(static_cast<const char*>(buffer));
- } else {
- return add(c);
- }
- }
- check_return Error append(const char* s) {
- while(true) {
- c32 u = 0;
- CORE_RETURN_ERROR(readUnicode(u, s));
- if(u == 0) {
- return Error::NONE;
- }
- CORE_RETURN_ERROR(append(u));
- }
- }
- check_return Error append(const c32* s) {
- // stringLength as s could be some part of data
- for(int i = stringLength(s); i > 0; i--) {
- CORE_RETURN_ERROR(append(*(s++)));
- }
- return Error::NONE;
- }
- check_return Error append(const signed char* s) {
- return append(reinterpret_cast<const char*>(s));
- }
- check_return Error append(const unsigned char* s) {
- return append(reinterpret_cast<const char*>(s));
- }
- check_return Error append(signed short s) {
- return convertAppend(s);
- }
- check_return Error append(unsigned short s) {
- return convertAppend(s);
- }
- check_return Error append(signed int i) {
- return convertAppend(i);
- }
- check_return Error append(unsigned int i) {
- return convertAppend(i);
- }
- check_return Error append(signed long l) {
- return convertAppend(l);
- }
- check_return Error append(unsigned long l) {
- return convertAppend(l);
- }
- check_return Error append(signed long long ll) {
- return convertAppend(ll);
- }
- check_return Error append(unsigned long long ll) {
- return convertAppend(ll);
- }
- check_return Error append(float f) {
- return convertAppend(f);
- }
- check_return Error append(double d) {
- return convertAppend(d);
- }
- check_return Error append(long double ld) {
- return convertAppend(ld);
- }
- check_return Error append(bool b) {
- return b ? append("true") : append("false");
- }
- check_return Error append(Error e) {
- return append(getErrorName(e));
- }
- template<typename T>
- check_return Error append(const T& t) {
- return t.toString(*this);
- }
- check_return Error toString(CharString& s) const {
- int l = length; // length changes if &s == this
- for(int i = 0; i < l; i++) {
- CORE_RETURN_ERROR(s.append(data[i]));
- }
- return Error::NONE;
- }
- template<int L>
- check_return Error toString(Char32String<L>& s) const {
- int l = length; // length changes if &s == this
- for(int i = 0; i < l; i++) {
- CORE_RETURN_ERROR(s.append(data[i]));
- }
- return Error::NONE;
- }
- void clear() {
- length = 0;
- hash = 0;
- data[0] = '\0';
- }
- u32 hashCode() const {
- return hash;
- }
- check_return Error print() const {
- if constexpr(IsSame<c32, char>) {
- for(int i = 0; i < length; i++) {
- CORE_RETURN_ERROR(Core::putChar(data[i]));
- }
- return Error::NONE;
- } else {
- for(int i = 0; i < length; i++) {
- c32 c = data[i];
- if(c < (1 << 7)) {
- CORE_RETURN_ERROR(printChar(c, 0, 0x7F, 0x0));
- } else if(c < (1 << 11)) {
- CORE_RETURN_ERROR(printChar(c, 6, 0x1F, 0xC0));
- CORE_RETURN_ERROR(printChar(c, 0, 0x3F, 0x80));
- } else if(c < (1 << 16)) {
- CORE_RETURN_ERROR(printChar(c, 12, 0x0F, 0xE0));
- CORE_RETURN_ERROR(printChar(c, 6, 0x3F, 0x80));
- CORE_RETURN_ERROR(printChar(c, 0, 0x3F, 0x80));
- } else if(c < (1 << 21)) {
- CORE_RETURN_ERROR(printChar(c, 18, 0x07, 0xF0));
- CORE_RETURN_ERROR(printChar(c, 12, 0x3F, 0x80));
- CORE_RETURN_ERROR(printChar(c, 6, 0x3F, 0x80));
- CORE_RETURN_ERROR(printChar(c, 0, 0x3F, 0x80));
- }
- }
- return Error::NONE;
- }
- }
- check_return Error printLine() const {
- CORE_RETURN_ERROR(print());
- CORE_RETURN_ERROR(Core::putChar('\n'));
- return Error::NONE;
- }
- template<typename... Args>
- check_return Error format(Args&&... args) {
- Char32String s;
- Error e = formatBuffer(s, 0, Core::forward<Args>(args)...);
- if(e == Error::NONE || e == Error::CAPACITY_REACHED) {
- *this = s;
- }
- return e;
- }
- template<int L>
- bool startsWidth(const Char32String<L>& other, int from = 0) const {
- if(from > length - other.getLength()) {
- return false;
- }
- for(int i = 0; i < other.getLength(); i++) {
- if(data[from + i] != other[i]) {
- return false;
- }
- }
- return true;
- }
- template<int L>
- int search(const Char32String<L>& other, int from = 0) const {
- for(int i = from; i < length; i++) {
- if(startsWidth(other, i)) {
- return i;
- }
- }
- return -1;
- }
- template<int L>
- bool contains(const Char32String<L>& other, int from = 0) const {
- return search(other, from) >= 0;
- }
- int search(c32 u, int from = 0) const {
- for(int i = from; i < length; i++) {
- if(data[i] == u) {
- return i;
- }
- }
- return -1;
- }
- bool contains(c32 u, int from = 0) const {
- return search(u, from) >= 0;
- }
- Char32String substring(int from, int to) const {
- from = Math::max(from, 0);
- to = Math::min(to, length - 1);
- Char32String s;
- for(int i = from; i <= to; i++) {
- (void)s.append(data[i]);
- }
- return s;
- }
- Char32String substring(int from = 0) const {
- return substring(from, length - 1);
- }
- template<int L1, int L2>
- check_return Error replace(const Char32String<L1>& search,
- const Char32String<L2>& replace) {
- Char32String<N> s;
- int i = 0;
- while(i < length) {
- if(startsWidth(search, i)) {
- CORE_RETURN_ERROR(s.append(replace));
- i += search.getLength();
- } else {
- CORE_RETURN_ERROR(s.append(data[i]));
- i++;
- }
- }
- *this = s;
- return Error::NONE;
- }
- void replace(c32 search, c32 replace) {
- hash = 0;
- for(int i = 0; i < length; i++) {
- if(data[i] == search) {
- data[i] = replace;
- }
- addToHash(static_cast<c32>(data[i]));
- }
- }
- operator const c32*() const {
- return data;
- }
- private:
- Error add(c32 c) {
- if(length >= N - 1) {
- return Error::CAPACITY_REACHED;
- }
- data[length++] = c;
- data[length] = '\0';
- addToHash(static_cast<c32>(c));
- return Error::NONE;
- }
- template<unsigned int L>
- void unicodeToChar(c32 c, char (&buffer)[L]) {
- static_assert(L >= 5, "to small char buffer");
- buffer[0] = '\0';
- if(c < (1 << 7)) {
- buffer[0] = static_cast<char>(((c >> 0) & 0x7F) | 0x0);
- buffer[1] = '\0';
- } else if(c < (1 << 11)) {
- buffer[0] = static_cast<char>(((c >> 6) & 0x1F) | 0xC0);
- buffer[1] = static_cast<char>(((c >> 0) & 0x3F) | 0x80);
- buffer[2] = '\0';
- } else if(c < (1 << 16)) {
- buffer[0] = static_cast<char>(((c >> 12) & 0x0F) | 0xE0);
- buffer[1] = static_cast<char>(((c >> 6) & 0x3F) | 0x80);
- buffer[2] = static_cast<char>(((c >> 0) & 0x3F) | 0x80);
- buffer[3] = '\0';
- } else if(c < (1 << 21)) {
- buffer[0] = static_cast<char>(((c >> 18) & 0x07) | 0xF0);
- buffer[1] = static_cast<char>(((c >> 12) & 0x3F) | 0x80);
- buffer[2] = static_cast<char>(((c >> 6) & 0x3F) | 0x80);
- buffer[3] = static_cast<char>(((c >> 0) & 0x3F) | 0x80);
- buffer[4] = '\0';
- }
- }
- check_return static Error printChar(c32 u, u32 shift, u32 a, u32 o) {
- return Core::putChar(static_cast<int>(((u >> shift) & a) | o));
- }
- void addToHash(c32 u) {
- hash = static_cast<u32>(2120251889) * hash + static_cast<u32>(u);
- }
- template<typename T, typename... Args>
- check_return Error formatBuffer(Char32String& s, int index, const T& t,
- Args&&... args) {
- while(index < length) {
- c32 u = data[index++];
- if(u == '#') {
- if(index >= length ||
- (index < length && data[index] != '#')) {
- break;
- }
- index++;
- }
- CORE_RETURN_ERROR(s.append(u));
- }
- CORE_RETURN_ERROR(s.append(t));
- return formatBuffer(s, index, Core::forward<Args>(args)...);
- }
- check_return Error formatBuffer(Char32String& s, int index) {
- while(index < length) {
- CORE_RETURN_ERROR(s.append(data[index++]));
- }
- return Error::NONE;
- }
- template<typename T>
- check_return Error convertAppend(T t) {
- char buffer[64];
- CORE_RETURN_ERROR(Core::toString(t, buffer, CORE_SIZE(buffer)));
- return append(static_cast<const char*>(buffer));
- }
- };
- template<typename String, typename Iterable>
- check_return Error toString(String& s, const Iterable& i) {
- CORE_RETURN_ERROR(s.append("["));
- auto current = i.begin();
- auto end = i.end();
- while(current != end) {
- CORE_RETURN_ERROR(s.append(*current));
- ++current;
- if(current != end) {
- CORE_RETURN_ERROR(s.append(", "));
- }
- }
- return s.append("]");
- }
- template<int N>
- using String8 = ArrayCharString<N>;
- template<int N>
- using String32 = Char32String<N>;
- }
- template<int N>
- bool operator==(const c32* cs, const Core::Char32String<N>& s) {
- return s == cs;
- }
- template<int N>
- bool operator!=(const c32* cs, const Core::Char32String<N>& s) {
- return s != cs;
- }
- inline bool operator==(const char* cs, const Core::CharString& s) {
- return s == cs;
- }
- inline bool operator!=(const char* cs, const Core::CharString& s) {
- return s != cs;
- }
- #endif
|