123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- #ifndef CORE_STRING_H
- #define CORE_STRING_H
- #include <stdarg.h>
- #include <stdio.h>
- #include "math/Math.h"
- #include "utils/Check.h"
- #include "utils/Types.h"
- #include "utils/Utility.h"
- namespace Core {
- template<int N>
- class ArrayString final {
- public:
- int length;
- Hash hash;
- static constexpr int DATA_LENGTH =
- (N - Math::max(IntSize(length), IntSize(hash))) / IntSize(u32);
- static_assert(DATA_LENGTH > 0, "Size of array string too small");
- u32 data[static_cast<unsigned int>(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<int L>
- bool operator==(const ArrayString<L>& 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<int L>
- bool operator!=(const ArrayString<L>& 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<u32>(c));
- }
- // returns true on error
- check_return bool append(signed char c) {
- if(c < 0) {
- return true;
- }
- return appendUnicode(static_cast<u32>(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<const char*>(s));
- }
- // returns true on error
- check_return bool append(const unsigned char* s) {
- return append(reinterpret_cast<const char*>(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<double>(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<typename T>
- check_return bool append(const T& t) {
- return t.toString(*this);
- }
- // returns true on error
- template<int L>
- check_return bool toString(ArrayString<L>& 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';
- }
- Hash 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<int>(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<typename... Args>
- check_return bool format(Args&&... args) {
- ArrayString s;
- if(formatBuffer(s, 0, Core::forward<Args>(args)...)) {
- return true;
- }
- *this = s;
- return false;
- }
- private:
- static bool printChar(u32 u, u32 shift, u32 a, u32 o) {
- return putchar(static_cast<int>(((u >> shift) & a) | o)) == EOF;
- }
- static u32 read(const char*& s) {
- if(*s == '\0') {
- return 0;
- }
- return static_cast<u32>(*(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<Hash>(2120251889) * hash + static_cast<Hash>(u);
- }
- // returns true on error
- template<typename T, typename... Args>
- 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 && data[index] != '#') {
- break;
- }
- index++;
- }
- if(s.appendUnicode(u)) {
- return true;
- }
- }
- if(s.append(t)) {
- return true;
- }
- return formatBuffer(s, index, Core::forward<Args>(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<const char*>(buffer));
- }
- };
- }
- template<int N>
- bool operator==(const char* cs, const Core::ArrayString<N>& s) {
- return s == cs;
- }
- template<int N>
- bool operator!=(const char* cs, const Core::ArrayString<N>& s) {
- return s != cs;
- }
- #endif
|