#ifndef CORE_STRING_H #define CORE_STRING_H #include #include #include "math/Math.h" #include "utils/Check.h" #include "utils/Types.h" #include "utils/Utility.h" namespace Core { template class ArrayString final { public: int length; u32 hash; static constexpr int DATA_LENGTH = (N - Math::max(CORE_SIZE(length), CORE_SIZE(hash))) / CORE_SIZE(u32); static_assert(DATA_LENGTH > 0, "Size of array string too small"); u32 data[static_cast(DATA_LENGTH)]; public: ArrayString() : length(0), hash(0) { data[0] = '\0'; } bool operator==(const char* s) const { for(int i = 0; i < length; i++) { u32 u = 0; if(readUnicode(u, s) || data[i] != u) { return false; } } return read(s) == 0; } template bool operator==(const ArrayString& other) const { if(length != other.length) { return false; } for(int i = 0; i < length; i++) { if(data[i] != other[i]) { return false; } } return true; } bool operator!=(const char* s) const { return !((*this) == s); } template bool operator!=(const ArrayString& other) const { return !((*this) == other); } u32 operator[](int index) const { return data[index]; } int getLength() const { return length; } constexpr int getCapacity() const { return DATA_LENGTH; } // returns true on error check_return bool append(char c) { if(c < 0) { return true; } return appendUnicode(static_cast(c)); } // returns true on error check_return bool append(signed char c) { if(c < 0) { return true; } return appendUnicode(static_cast(c)); } // returns true on error check_return bool append(unsigned char c) { return appendUnicode(c); } // returns true on error check_return bool append(const char* s) { while(true) { u32 u = 0; if(readUnicode(u, s)) { return true; } else if(u == 0) { return false; } else if(appendUnicode(u)) { return true; } } return false; } // returns true on error check_return bool append(const signed char* s) { return append(reinterpret_cast(s)); } // returns true on error check_return bool append(const unsigned char* s) { return append(reinterpret_cast(s)); } // returns true on error check_return bool append(signed short s) { return appendFormat("%hd", s); } // returns true on error check_return bool append(unsigned short s) { return appendFormat("%hu", s); } // returns true on error check_return bool append(signed int i) { return appendFormat("%d", i); } // returns true on error check_return bool append(unsigned int i) { return appendFormat("%u", i); } // returns true on error check_return bool append(signed long l) { return appendFormat("%ld", l); } // returns true on error check_return bool append(unsigned long l) { return appendFormat("%lu", l); } // returns true on error check_return bool append(signed long long ll) { return appendFormat("%lld", ll); } // returns true on error check_return bool append(unsigned long long ll) { return appendFormat("%llu", ll); } // returns true on error check_return bool append(float f) { return appendFormat("%.2f", static_cast(f)); } // returns true on error check_return bool append(double d) { return appendFormat("%.2f", d); } // returns true on error check_return bool append(long double ld) { return appendFormat("%.2Lf", ld); } // returns true on error check_return bool append(bool b) { return b ? append("true") : append("false"); } // returns true on error check_return bool appendUnicode(u32 c) { if(length >= DATA_LENGTH) { return true; } data[length++] = c; addToHash(c); return false; } // returns true on error template check_return bool append(const T& t) { return t.toString(*this); } // returns true on error template check_return bool toString(ArrayString& s) const { int l = length; // length changes if &s == this for(int i = 0; i < l; i++) { if(s.appendUnicode(data[i])) { return true; } } return false; } void clear() { length = 0; hash = 0; data[0] = '\0'; } u32 hashCode() const { return hash; } // returns true on error check_return bool print() const { for(int i = 0; i < length; i++) { u32 c = data[i]; if(c < (1 << 7)) { if(putchar(static_cast(c & 0x7F)) == EOF) { return true; } } else if(c < (1 << 11)) { if(printChar(c, 6, 0x1F, 0xC0) || printChar(c, 0, 0x3F, 0x80)) { return true; } } else if(c < (1 << 16)) { if(printChar(c, 12, 0x0F, 0xE0) || printChar(c, 6, 0x3F, 0x80) || printChar(c, 0, 0x3F, 0x80)) { return true; } } else if(c < (1 << 21)) { if(printChar(c, 18, 0x07, 0xF0) || printChar(c, 12, 0x3F, 0x80) || printChar(c, 6, 0x3F, 0x80) || printChar(c, 0, 0x3F, 0x80)) { return true; } } } return false; } // returns true on error check_return bool printLine() const { return print() || putchar('\n') == EOF; } // returns true on error template check_return bool format(Args&&... args) { ArrayString s; if(formatBuffer(s, 0, Core::forward(args)...)) { return true; } *this = s; return false; } private: static bool printChar(u32 u, u32 shift, u32 a, u32 o) { return putchar(static_cast(((u >> shift) & a) | o)) == EOF; } static u32 read(const char*& s) { if(*s == '\0') { return 0; } return static_cast(*(s++)); } // returns true on error static bool readUnicode(u32& u, const char*& s) { u = read(s); if((u & 0x80) == 0) { return false; } if((u & 0xE0) == 0xC0) { u32 u2 = read(s); if(u2 == 0) { return true; } u = ((u & 0x1F) << 6) | (u2 & 0x3F); return false; } else if((u & 0xF0) == 0xE0) { u32 u2 = read(s); u32 u3 = read(s); if(u2 == 0 || u3 == 0) { return true; } u = ((u & 0xF) << 12) | ((u2 & 0x3F) << 6) | (u3 & 0x3F); return false; } else if((u & 0xF8) == 0xF0) { u32 u2 = read(s); u32 u3 = read(s); u32 u4 = read(s); if(u2 == 0 || u3 == 0 || u4 == 0) { return true; } u = ((u & 0x07) << 18) | ((u2 & 0x3F) << 12) | ((u3 & 0x3F) << 6) | (u4 & 0x3F); return false; } return true; } void addToHash(u32 u) { hash = static_cast(2120251889) * hash + static_cast(u); } // returns true on error template check_return bool formatBuffer(ArrayString& s, int index, const T& t, Args&&... args) { while(index < length) { u32 u = data[index++]; if(u == '#') { if(index >= length || (index < length && data[index] != '#')) { break; } index++; } if(s.appendUnicode(u)) { return true; } } if(s.append(t)) { return true; } return formatBuffer(s, index, Core::forward(args)...); } // returns true on error check_return bool formatBuffer(ArrayString& s, int index) { while(index < length) { if(s.appendUnicode(data[index++])) { return true; } } return false; } // returns true on error check_format(2, 3) check_return bool appendFormat(const char* format, ...) { constexpr int BUFFER_SIZE = 64; char buffer[BUFFER_SIZE]; va_list args; va_start(args, format); int written = vsnprintf(buffer, BUFFER_SIZE, format, args); va_end(args); return written >= BUFFER_SIZE || append(static_cast(buffer)); } }; } template bool operator==(const char* cs, const Core::ArrayString& s) { return s == cs; } template bool operator!=(const char* cs, const Core::ArrayString& s) { return s != cs; } #endif