123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- #ifndef CORE_STRING_H
- #define CORE_STRING_H
- #include "math/Math.h"
- #include "utils/Check.h"
- #include "utils/Utility.h"
- namespace Core {
- template<int N>
- class ArrayString final {
- 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<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.getLength()) {
- 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 and calls the error callback
- check_return bool append(char c) {
- if(c < 0) {
- return CORE_ERROR(Error::NEGATIVE_ARGUMENT);
- }
- return appendUnicode(static_cast<u32>(c));
- }
- // returns true on error and calls the error callback
- check_return bool append(signed char c) {
- if(c < 0) {
- return CORE_ERROR(Error::NEGATIVE_ARGUMENT);
- }
- return appendUnicode(static_cast<u32>(c));
- }
- // returns true on error and calls the error callback
- check_return bool append(unsigned char c) {
- return appendUnicode(c);
- }
- // returns true on error and calls the error callback
- 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 and calls the error callback
- check_return bool append(const signed char* s) {
- return append(reinterpret_cast<const char*>(s));
- }
- // returns true on error and calls the error callback
- check_return bool append(const unsigned char* s) {
- return append(reinterpret_cast<const char*>(s));
- }
- // returns true on error and calls the error callback
- check_return bool append(signed short s) {
- return convertAppend(s);
- }
- // returns true on error and calls the error callback
- check_return bool append(unsigned short s) {
- return convertAppend(s);
- }
- // returns true on error and calls the error callback
- check_return bool append(signed int i) {
- return convertAppend(i);
- }
- // returns true on error and calls the error callback
- check_return bool append(unsigned int i) {
- return convertAppend(i);
- }
- // returns true on error and calls the error callback
- check_return bool append(signed long l) {
- return convertAppend(l);
- }
- // returns true on error and calls the error callback
- check_return bool append(unsigned long l) {
- return convertAppend(l);
- }
- // returns true on error and calls the error callback
- check_return bool append(signed long long ll) {
- return convertAppend(ll);
- }
- // returns true on error and calls the error callback
- check_return bool append(unsigned long long ll) {
- return convertAppend(ll);
- }
- // returns true on error and calls the error callback
- check_return bool append(float f) {
- return convertAppend(static_cast<double>(f));
- }
- // returns true on error and calls the error callback
- check_return bool append(double d) {
- return convertAppend(d);
- }
- // returns true on error and calls the error callback
- check_return bool append(long double ld) {
- return convertAppend(ld);
- }
- // returns true on error and calls the error callback
- check_return bool append(bool b) {
- return b ? append("true") : append("false");
- }
- // returns true on error and calls the error callback
- check_return bool appendUnicode(u32 c) {
- if(length >= DATA_LENGTH) {
- return CORE_ERROR(Error::CAPACITY_REACHED);
- }
- data[length++] = c;
- addToHash(c);
- return false;
- }
- // returns true on error and calls the error callback
- template<typename T>
- check_return bool append(const T& t) {
- return t.toString(*this);
- }
- // returns true on error and calls the error callback
- 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';
- }
- u32 hashCode() const {
- return hash;
- }
- // returns true on error and calls the error callback
- check_return bool print() const {
- for(int i = 0; i < length; i++) {
- u32 c = data[i];
- if(c < (1 << 7)) {
- if(Core::putChar(static_cast<int>(c & 0x7F))) {
- 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 and calls the error callback
- check_return bool printLine() const {
- return print() || Core::putChar('\n');
- }
- // returns true on error and calls the error callback
- 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;
- }
- template<int L>
- bool startsWidth(const ArrayString<L>& other, int from = 0) const {
- if(from + other.getLength() > length) {
- 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 ArrayString<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 ArrayString<L>& other, int from = 0) const {
- return search(other, from) >= 0;
- }
- int search(u32 u, int from = 0) const {
- for(int i = from; i < length; i++) {
- if(data[i] == u) {
- return i;
- }
- }
- return -1;
- }
- bool contains(u32 u, int from = 0) const {
- return search(u, from) >= 0;
- }
- ArrayString substring(int from, int to) const {
- from = Math::max(from, 0);
- to = Math::min(to, length - 1);
- ArrayString<N> s;
- for(int i = from; i <= to; i++) {
- (void)s.appendUnicode(data[i]);
- }
- return s;
- }
- ArrayString substring(int from = 0) const {
- return substring(from, length - 1);
- }
- template<int L1, int L2>
- bool replace(const ArrayString<L1>& search,
- const ArrayString<L2>& replace) {
- ArrayString<N> s;
- int i = 0;
- while(i < length) {
- if(startsWidth(search, i)) {
- if(s.append(replace)) {
- return true;
- }
- i += search.getLength();
- } else {
- if(s.appendUnicode(data[i])) {
- return true;
- }
- i++;
- }
- }
- *this = s;
- return false;
- }
- void replace(u32 search, u32 replace) {
- hash = 0;
- for(int i = 0; i < length; i++) {
- if(data[i] == search) {
- data[i] = replace;
- }
- addToHash(data[i]);
- }
- }
- private:
- // returns true on error and calls the error callback
- check_return static bool printChar(u32 u, u32 shift, u32 a, u32 o) {
- return Core::putChar(static_cast<int>(((u >> shift) & a) | o));
- }
- static u32 read(const char*& s) {
- if(*s == '\0') {
- return 0;
- }
- return static_cast<u32>(*(s++));
- }
- // returns true on error and calls the error callback
- 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 CORE_ERROR(Error::INVALID_CHAR);
- }
- 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 CORE_ERROR(Error::INVALID_CHAR);
- }
- 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 CORE_ERROR(Error::INVALID_CHAR);
- }
- u = ((u & 0x07) << 18) | ((u2 & 0x3F) << 12) |
- ((u3 & 0x3F) << 6) | (u4 & 0x3F);
- return false;
- }
- return CORE_ERROR(Error::INVALID_CHAR);
- }
- void addToHash(u32 u) {
- hash = static_cast<u32>(2120251889) * hash + static_cast<u32>(u);
- }
- // returns true on error and calls the error callback
- 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 ||
- (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 and calls the error callback
- check_return bool formatBuffer(ArrayString& s, int index) {
- while(index < length) {
- if(s.appendUnicode(data[index++])) {
- return true;
- }
- }
- return false;
- }
- // returns true on error and calls the error callback
- template<typename T>
- check_return bool convertAppend(T t) {
- constexpr int BUFFER_SIZE = 64;
- char buffer[BUFFER_SIZE];
- if(Core::toString(t, buffer, BUFFER_SIZE)) {
- return true;
- }
- return 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
|