#include "core/utils/ArrayString.hpp" #include "core/utils/Error.hpp" using CharString = Core::CharString; using Char32String = Core::Char32String; using Error = Core::Error; static c32 read(const char*& s) { if(*s == '\0') { return 0; } return static_cast(*(s++)); } Error Core::readUnicode(c32& u, const char*& s) { u = read(s); if((u & 0x80) == 0) { return Error::NONE; } if((u & 0xE0) == 0xC0) { c32 u2 = read(s); if(u2 == 0) { return Error::INVALID_CHAR; } u = ((u & 0x1F) << 6) | (u2 & 0x3F); return Error::NONE; } else if((u & 0xF0) == 0xE0) { c32 u2 = read(s); c32 u3 = read(s); if(u2 == 0 || u3 == 0) { return Error::INVALID_CHAR; } u = ((u & 0xF) << 12) | ((u2 & 0x3F) << 6) | (u3 & 0x3F); return Error::NONE; } else if((u & 0xF8) == 0xF0) { c32 u2 = read(s); c32 u3 = read(s); c32 u4 = read(s); if(u2 == 0 || u3 == 0 || u4 == 0) { return Error::INVALID_CHAR; } u = ((u & 0x07) << 18) | ((u2 & 0x3F) << 12) | ((u3 & 0x3F) << 6) | (u4 & 0x3F); return Error::NONE; } return Error::INVALID_CHAR; } template static 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(((c >> 0) & 0x7F) | 0x0); buffer[1] = '\0'; } else if(c < (1 << 11)) { buffer[0] = static_cast(((c >> 6) & 0x1F) | 0xC0); buffer[1] = static_cast(((c >> 0) & 0x3F) | 0x80); buffer[2] = '\0'; } else if(c < (1 << 16)) { buffer[0] = static_cast(((c >> 12) & 0x0F) | 0xE0); buffer[1] = static_cast(((c >> 6) & 0x3F) | 0x80); buffer[2] = static_cast(((c >> 0) & 0x3F) | 0x80); buffer[3] = '\0'; } else if(c < (1 << 21)) { buffer[0] = static_cast(((c >> 18) & 0x07) | 0xF0); buffer[1] = static_cast(((c >> 12) & 0x3F) | 0x80); buffer[2] = static_cast(((c >> 6) & 0x3F) | 0x80); buffer[3] = static_cast(((c >> 0) & 0x3F) | 0x80); buffer[4] = '\0'; } } static Error printChar(c32 u, u32 shift, u32 a, u32 o) { return Core::putChar(static_cast(((u >> shift) & a) | o)); } CharString::CharString(char* buffer, i32 bufferSize) : length(0), capacity(bufferSize), hash(0), data(buffer) { data[0] = '\0'; } Error CharString::copyFrom(const CharString& s) { clear(); return s.toString(*this); } bool CharString::operator==(const char* s) const { const char* p = data; while(*s == *p && *s != '\0') { s++; p++; } return *s == *p; } bool CharString::operator==(const CharString& 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 CharString::operator!=(const char* s) const { return !((*this) == s); } bool CharString::operator!=(const CharString& other) const { return !((*this) == other); } char CharString::operator[](int index) const { return data[index]; } int CharString::getLength() const { return length; } int CharString::getCapacity() const { return capacity - 1; } Error CharString::append(char c) { if(length >= capacity - 1) { return Error::CAPACITY_REACHED; } data[length++] = c; data[length] = '\0'; addToHash(static_cast(c)); return Error::NONE; } Error CharString::append(signed char c) { return append(static_cast(c)); } Error CharString::append(unsigned char c) { return append(static_cast(c)); } Error CharString::append(wchar_t c) { return append(static_cast(c)); } Error CharString::append(c32 c) { char buffer[5]; unicodeToChar(c, buffer); return append(static_cast(buffer)); } Error CharString::append(const char* 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; } Error CharString::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; } Error CharString::append(const signed char* s) { return append(reinterpret_cast(s)); } Error CharString::append(const unsigned char* s) { return append(reinterpret_cast(s)); } Error CharString::append(bool b) { return b ? append("true") : append("false"); } Error CharString::append(Error e) { return append(getErrorName(e)); } Error CharString::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; } Error CharString::toString(Char32String& s) const { return s.append(static_cast(data)); } void CharString::clear() { length = 0; hash = 0; data[0] = '\0'; } u32 CharString::hashCode() const { return hash; } Error CharString::print() const { for(int i = 0; i < length; i++) { CORE_RETURN_ERROR(Core::putChar(data[i])); } return Error::NONE; } Error CharString::printLine() const { CORE_RETURN_ERROR(print()); CORE_RETURN_ERROR(Core::putChar('\n')); return Error::NONE; } bool CharString::startsWidth(const CharString& other, int from) 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; } int CharString::search(const CharString& other, int from) const { for(int i = from; i < length; i++) { if(startsWidth(other, i)) { return i; } } return -1; } bool CharString::contains(const CharString& other, int from) const { return search(other, from) >= 0; } int CharString::search(char u, int from) const { for(int i = from; i < length; i++) { if(data[i] == u) { return i; } } return -1; } bool CharString::contains(char u, int from) const { return search(u, from) >= 0; } Error CharString::substring(CharString& s, int from, int to) const { s.clear(); from = Math::max(from, 0); to = Math::min(to, length - 1); for(int i = from; i <= to; i++) { CORE_RETURN_ERROR(s.append(data[i])); } return Error::NONE; } Error CharString::substring(CharString& s, int from) const { return substring(s, from, length - 1); } Error CharString::replace(CharString& s, const CharString& search, const CharString& replace) { 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++; } } return copyFrom(s); } void CharString::replace(char search, char replace) { hash = 0; for(int i = 0; i < length; i++) { if(data[i] == search) { data[i] = replace; } addToHash(static_cast(data[i])); } } CharString::operator const char*() const { return data; } void CharString::addToHash(c32 u) { hash = static_cast(2120251889) * hash + static_cast(u); } Error CharString::formatBuffer(CharString& s, int index) { while(index < length) { CORE_RETURN_ERROR(s.append(data[index++])); } return Error::NONE; } Char32String::Char32String(c32* buffer, i32 bufferSize) : length(0), capacity(bufferSize), hash(0), data(buffer) { data[0] = '\0'; } Error Char32String::copyFrom(const Char32String& s) { clear(); return s.toString(*this); } bool Char32String::operator==(const c32* s) const { const c32* p = data; while(*s == *p && *s != '\0') { s++; p++; } return *s == *p; } bool Char32String::operator==(const Char32String& 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 Char32String::operator!=(const c32* s) const { return !((*this) == s); } bool Char32String::operator!=(const Char32String& other) const { return !((*this) == other); } c32 Char32String::operator[](int index) const { return data[index]; } int Char32String::getLength() const { return length; } int Char32String::getCapacity() const { return capacity - 1; } Error Char32String::append(char c) { return add(static_cast(c)); } Error Char32String::append(signed char c) { return append(static_cast(c)); } Error Char32String::append(unsigned char c) { return append(static_cast(c)); } Error Char32String::append(wchar_t c) { return append(static_cast(c)); } Error Char32String::append(c32 c) { if constexpr(IsSame) { char buffer[5]; unicodeToChar(c, buffer); return append(static_cast(buffer)); } else { return add(c); } } Error Char32String::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)); } } Error Char32String::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; } Error Char32String::append(const signed char* s) { return append(reinterpret_cast(s)); } Error Char32String::append(const unsigned char* s) { return append(reinterpret_cast(s)); } Error Char32String::append(bool b) { return b ? append("true") : append("false"); } Error Char32String::append(Error e) { return append(getErrorName(e)); } Error Char32String::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; } Error Char32String::toString(Char32String& 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 Char32String::clear() { length = 0; hash = 0; data[0] = '\0'; } u32 Char32String::hashCode() const { return hash; } Error Char32String::print() const { 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; } Error Char32String::printLine() const { CORE_RETURN_ERROR(print()); CORE_RETURN_ERROR(Core::putChar('\n')); return Error::NONE; } bool Char32String::startsWidth(const Char32String& other, int from) 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; } int Char32String::search(const Char32String& other, int from) const { for(int i = from; i < length; i++) { if(startsWidth(other, i)) { return i; } } return -1; } bool Char32String::contains(const Char32String& other, int from) const { return search(other, from) >= 0; } int Char32String::search(c32 u, int from) const { for(int i = from; i < length; i++) { if(data[i] == u) { return i; } } return -1; } bool Char32String::contains(c32 u, int from) const { return search(u, from) >= 0; } Error Char32String::substring(Char32String& s, int from, int to) const { s.clear(); from = Math::max(from, 0); to = Math::min(to, length - 1); for(int i = from; i <= to; i++) { CORE_RETURN_ERROR(s.append(data[i])); } return Error::NONE; } Error Char32String::substring(Char32String& s, int from) const { return substring(s, from, length - 1); } Error Char32String::replace(Char32String& s, const Char32String& search, const Char32String& replace) { 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++; } } return copyFrom(s); } void Char32String::replace(c32 search, c32 replace) { hash = 0; for(int i = 0; i < length; i++) { if(data[i] == search) { data[i] = replace; } addToHash(static_cast(data[i])); } } Char32String::operator const c32*() const { return data; } Error Char32String::add(c32 c) { if(length >= capacity - 1) { return Error::CAPACITY_REACHED; } data[length++] = c; data[length] = '\0'; addToHash(static_cast(c)); return Error::NONE; } void Char32String::addToHash(c32 u) { hash = static_cast(2120251889) * hash + static_cast(u); } Error Char32String::formatBuffer(Char32String& s, int index) { while(index < length) { CORE_RETURN_ERROR(s.append(data[index++])); } return Error::NONE; }