Browse Source

Separate char and char32_t string

Kajetan Johannes Hammerle 2 weeks ago
parent
commit
9494a24124
2 changed files with 437 additions and 52 deletions
  1. 430 45
      include/core/utils/ArrayString.hpp
  2. 7 7
      test/modules/ArrayStringTests.cpp

+ 430 - 45
include/core/utils/ArrayString.hpp

@@ -16,20 +16,392 @@ namespace Core {
 
     Error readUnicode(c32& u, const char*& s);
 
-    template<int N, typename CharType>
-    class ArrayString final {
+    template<int N>
+    class Char32String;
+
+    template<int N>
+    class CharString final {
+        int length;
+        u32 hash;
+        static_assert(N > 0, "size of array string must be positive");
+        char data[static_cast<unsigned int>(N)];
+
+    public:
+        CharString() : length(0), hash(0) {
+            data[0] = '\0';
+        }
+
+        bool operator==(const char* s) const {
+            const char* p = data;
+            while(*s == *p && *s != '\0') {
+                s++;
+                p++;
+            }
+            return *s == *p;
+        }
+
+        template<int L>
+        bool operator==(const CharString<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 CharString<L>& other) const {
+            return !((*this) == other);
+        }
+
+        char 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(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) {
+            char buffer[5];
+            unicodeToChar(c, buffer);
+            return append(static_cast<const char*>(buffer));
+        }
+
+        check_return Error 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;
+        }
+
+        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);
+        }
+
+        template<int L>
+        check_return Error toString(CharString<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;
+        }
+
+        template<int L>
+        check_return Error toString(Char32String<L>& s) const {
+            return s.append(data);
+        }
+
+        void clear() {
+            length = 0;
+            hash = 0;
+            data[0] = '\0';
+        }
+
+        u32 hashCode() const {
+            return hash;
+        }
+
+        check_return Error print() const {
+            for(int i = 0; i < length; i++) {
+                CORE_RETURN_ERROR(Core::putChar(data[i]));
+            }
+            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) {
+            CharString 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 CharString<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 CharString<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 CharString<L>& other, int from = 0) const {
+            return search(other, from) >= 0;
+        }
+
+        int search(char u, int from = 0) const {
+            for(int i = from; i < length; i++) {
+                if(data[i] == u) {
+                    return i;
+                }
+            }
+            return -1;
+        }
+
+        bool contains(char u, int from = 0) const {
+            return search(u, from) >= 0;
+        }
+
+        CharString substring(int from, int to) const {
+            from = Math::max(from, 0);
+            to = Math::min(to, length - 1);
+            CharString s;
+            for(int i = from; i <= to; i++) {
+                (void)s.append(data[i]);
+            }
+            return s;
+        }
+
+        CharString substring(int from = 0) const {
+            return substring(from, length - 1);
+        }
+
+        template<int L1, int L2>
+        check_return Error replace(const CharString<L1>& search,
+                                   const CharString<L2>& replace) {
+            CharString<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(char search, char 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 char*() const {
+            return data;
+        }
+
+    private:
+        Error add(char 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(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) {
+            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<int N>
+    class Char32String final {
         int length;
         u32 hash;
         static_assert(N > 0, "size of array string must be positive");
-        CharType data[static_cast<unsigned int>(N)];
+        c32 data[static_cast<unsigned int>(N)];
 
     public:
-        ArrayString() : length(0), hash(0) {
+        Char32String() : length(0), hash(0) {
             data[0] = '\0';
         }
 
-        bool operator==(const CharType* s) const {
-            const CharType* p = data;
+        bool operator==(const c32* s) const {
+            const c32* p = data;
             while(*s == *p && *s != '\0') {
                 s++;
                 p++;
@@ -38,7 +410,7 @@ namespace Core {
         }
 
         template<int L>
-        bool operator==(const ArrayString<L, CharType>& other) const {
+        bool operator==(const Char32String<L>& other) const {
             if(length != other.getLength()) {
                 return false;
             }
@@ -50,16 +422,16 @@ namespace Core {
             return true;
         }
 
-        bool operator!=(const CharType* s) const {
+        bool operator!=(const c32* s) const {
             return !((*this) == s);
         }
 
         template<int L>
-        bool operator!=(const ArrayString<L, CharType>& other) const {
+        bool operator!=(const Char32String<L>& other) const {
             return !((*this) == other);
         }
 
-        CharType operator[](int index) const {
+        c32 operator[](int index) const {
             return data[index];
         }
 
@@ -72,7 +444,7 @@ namespace Core {
         }
 
         check_return Error append(char c) {
-            return add(static_cast<CharType>(c));
+            return add(static_cast<c32>(c));
         }
 
         check_return Error append(signed char c) {
@@ -88,7 +460,7 @@ namespace Core {
         }
 
         check_return Error append(c32 c) {
-            if constexpr(IsSame<CharType, char>) {
+            if constexpr(IsSame<c32, char>) {
                 char buffer[5];
                 unicodeToChar(c, buffer);
                 return append(static_cast<const char*>(buffer));
@@ -98,7 +470,7 @@ namespace Core {
         }
 
         check_return Error append(const char* s) {
-            if constexpr(IsSame<CharType, char>) {
+            if constexpr(IsSame<c32, char>) {
                 // stringLength as s could be some part of data
                 for(int i = stringLength(s); i > 0; i--) {
                     CORE_RETURN_ERROR(append(*(s++)));
@@ -189,12 +561,17 @@ namespace Core {
             return t.toString(*this);
         }
 
-        template<int L, typename C>
-        check_return Error toString(ArrayString<L, C>& s) const {
-            if constexpr(IsSame<CharType, char> && !IsSame<C, char>) {
-                // utf32 to utf8
-                return s.append(data);
+        template<int L>
+        check_return Error toString(CharString<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;
+        }
+
+        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]));
@@ -213,7 +590,7 @@ namespace Core {
         }
 
         check_return Error print() const {
-            if constexpr(IsSame<CharType, char>) {
+            if constexpr(IsSame<c32, char>) {
                 for(int i = 0; i < length; i++) {
                     CORE_RETURN_ERROR(Core::putChar(data[i]));
                 }
@@ -249,7 +626,7 @@ namespace Core {
 
         template<typename... Args>
         check_return Error format(Args&&... args) {
-            ArrayString s;
+            Char32String s;
             Error e = formatBuffer(s, 0, Core::forward<Args>(args)...);
             if(e == Error::NONE || e == Error::CAPACITY_REACHED) {
                 *this = s;
@@ -258,8 +635,7 @@ namespace Core {
         }
 
         template<int L>
-        bool startsWidth(const ArrayString<L, CharType>& other,
-                         int from = 0) const {
+        bool startsWidth(const Char32String<L>& other, int from = 0) const {
             if(from > length - other.getLength()) {
                 return false;
             }
@@ -272,7 +648,7 @@ namespace Core {
         }
 
         template<int L>
-        int search(const ArrayString<L, CharType>& other, int from = 0) const {
+        int search(const Char32String<L>& other, int from = 0) const {
             for(int i = from; i < length; i++) {
                 if(startsWidth(other, i)) {
                     return i;
@@ -282,12 +658,11 @@ namespace Core {
         }
 
         template<int L>
-        bool contains(const ArrayString<L, CharType>& other,
-                      int from = 0) const {
+        bool contains(const Char32String<L>& other, int from = 0) const {
             return search(other, from) >= 0;
         }
 
-        int search(CharType u, int from = 0) const {
+        int search(c32 u, int from = 0) const {
             for(int i = from; i < length; i++) {
                 if(data[i] == u) {
                     return i;
@@ -296,28 +671,28 @@ namespace Core {
             return -1;
         }
 
-        bool contains(CharType u, int from = 0) const {
+        bool contains(c32 u, int from = 0) const {
             return search(u, from) >= 0;
         }
 
-        ArrayString substring(int from, int to) const {
+        Char32String substring(int from, int to) const {
             from = Math::max(from, 0);
             to = Math::min(to, length - 1);
-            ArrayString s;
+            Char32String s;
             for(int i = from; i <= to; i++) {
                 (void)s.append(data[i]);
             }
             return s;
         }
 
-        ArrayString substring(int from = 0) const {
+        Char32String substring(int from = 0) const {
             return substring(from, length - 1);
         }
 
         template<int L1, int L2>
-        check_return Error replace(const ArrayString<L1, CharType>& search,
-                                   const ArrayString<L2, CharType>& replace) {
-            ArrayString<N, CharType> s;
+        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)) {
@@ -332,7 +707,7 @@ namespace Core {
             return Error::NONE;
         }
 
-        void replace(CharType search, CharType replace) {
+        void replace(c32 search, c32 replace) {
             hash = 0;
             for(int i = 0; i < length; i++) {
                 if(data[i] == search) {
@@ -342,12 +717,12 @@ namespace Core {
             }
         }
 
-        operator const CharType*() const {
+        operator const c32*() const {
             return data;
         }
 
     private:
-        Error add(CharType c) {
+        Error add(c32 c) {
             if(length >= N - 1) {
                 return Error::CAPACITY_REACHED;
             }
@@ -391,10 +766,10 @@ namespace Core {
         }
 
         template<typename T, typename... Args>
-        check_return Error formatBuffer(ArrayString& s, int index, const T& t,
+        check_return Error formatBuffer(Char32String& s, int index, const T& t,
                                         Args&&... args) {
             while(index < length) {
-                CharType u = data[index++];
+                c32 u = data[index++];
                 if(u == '#') {
                     if(index >= length ||
                        (index < length && data[index] != '#')) {
@@ -408,7 +783,7 @@ namespace Core {
             return formatBuffer(s, index, Core::forward<Args>(args)...);
         }
 
-        check_return Error formatBuffer(ArrayString& s, int index) {
+        check_return Error formatBuffer(Char32String& s, int index) {
             while(index < length) {
                 CORE_RETURN_ERROR(s.append(data[index++]));
             }
@@ -439,19 +814,29 @@ namespace Core {
     }
 
     template<int N>
-    using String8 = ArrayString<N, char>;
+    using String8 = CharString<N>;
 
     template<int N>
-    using String32 = ArrayString<N, char32_t>;
+    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;
 }
 
-template<int N, typename CharType>
-bool operator==(const CharType* cs, const Core::ArrayString<N, CharType>& s) {
+template<int N>
+bool operator==(const char* cs, const Core::CharString<N>& s) {
     return s == cs;
 }
 
-template<int N, typename CharType>
-bool operator!=(const CharType* cs, const Core::ArrayString<N, CharType>& s) {
+template<int N>
+bool operator!=(const char* cs, const Core::CharString<N>& s) {
     return s != cs;
 }
 

+ 7 - 7
test/modules/ArrayStringTests.cpp

@@ -2,8 +2,8 @@
 #include "core/data/HashMap.hpp"
 #include "core/utils/ArrayString.hpp"
 
-template class Core::ArrayString<128, char>;
-template class Core::ArrayString<128, char32_t>;
+template class Core::CharString<128>;
+template class Core::Char32String<128>;
 
 using String8 = Core::String8<128>;
 using String32 = Core::String32<128>;
@@ -51,7 +51,7 @@ static void testStringAppend8() {
 }
 
 static void testStringAppendOverflow8() {
-    Core::ArrayString<6, char> s;
+    Core::CharString<6> s;
     CORE_TEST_ERROR(s.append("te"));
     CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, s.append("23334444"));
     CORE_TEST_TRUE(build("te23334444") != s);
@@ -193,7 +193,7 @@ static void testBool8() {
 }
 
 static void testIntOverflow8() {
-    Core::ArrayString<4, char> s;
+    Core::CharString<4> s;
     CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, s.append(123456));
 
     String8 o;
@@ -523,7 +523,7 @@ static void testStringAppend32() {
 }
 
 static void testStringAppendOverflow32() {
-    Core::ArrayString<6, c32> s;
+    Core::Char32String<6> s;
     CORE_TEST_ERROR(s.append(U"te"));
     CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, s.append(U"23334444"));
     CORE_TEST_TRUE(build(U"te23334444") != s);
@@ -665,7 +665,7 @@ static void testBool32() {
 }
 
 static void testIntOverflow32() {
-    Core::ArrayString<4, c32> s;
+    Core::Char32String<4> s;
     CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, s.append(123456));
 
     String32 o;
@@ -1086,4 +1086,4 @@ void Core::testArrayString() {
     testKeepHash32();
 
     testConversion();
-}
+}