Browse Source

Make array string more generic

Kajetan Johannes Hammerle 2 weeks ago
parent
commit
5b6904e505
2 changed files with 41 additions and 42 deletions
  1. 39 40
      include/core/utils/ArrayString.hpp
  2. 2 2
      test/modules/ArrayStringTests.cpp

+ 39 - 40
include/core/utils/ArrayString.hpp

@@ -123,45 +123,6 @@ namespace Core {
         check_return Error formatBuffer(CharString& s, int index);
     };
 
-    template<int N>
-    class ArrayCharString final : public CharString {
-        static_assert(N > 0, "size of array string must be positive");
-        char data[static_cast<unsigned int>(N)];
-
-    public:
-        ArrayCharString() : CharString(data, N) {
-        }
-
-        ArrayCharString(const ArrayCharString& other) : CharString(data, N) {
-            Core::memoryCopy(data, other.data, N);
-            length = other.length;
-            hash = other.hash;
-        }
-
-        ArrayCharString& operator=(const ArrayCharString& other) {
-            if(this != &other) {
-                Core::memoryCopy(data, other.data, N);
-                length = other.length;
-                hash = other.hash;
-            }
-            return *this;
-        }
-
-        template<typename... Args>
-        check_return Error format(Args&&... args) {
-            ArrayCharString s;
-            return CharString::format(s, Core::forward<Args>(args)...);
-        }
-
-        check_return Error replace(const CharString& search,
-                                   const CharString& replace) {
-            ArrayCharString s;
-            return CharString::replace(s, search, replace);
-        }
-
-        using CharString::replace;
-    };
-
     template<int N>
     class Char32String final {
         int length;
@@ -563,6 +524,44 @@ namespace Core {
         }
     };
 
+    template<int N, typename C, typename B>
+    class ArrayString final : public B {
+        static_assert(N > 0, "size of array string must be positive");
+        C data[static_cast<unsigned int>(N)];
+
+    public:
+        ArrayString() : B(data, N) {
+        }
+
+        ArrayString(const ArrayString& other) : B(data, N) {
+            Core::memoryCopy(data, other.data, sizeof(data));
+            B::length = other.length;
+            B::hash = other.hash;
+        }
+
+        ArrayString& operator=(const ArrayString& other) {
+            if(this != &other) {
+                Core::memoryCopy(data, other.data, sizeof(data));
+                B::length = other.length;
+                B::hash = other.hash;
+            }
+            return *this;
+        }
+
+        template<typename... Args>
+        check_return Error format(Args&&... args) {
+            ArrayString s;
+            return B::format(s, Core::forward<Args>(args)...);
+        }
+
+        check_return Error replace(const B& search, const B& replace) {
+            ArrayString s;
+            return B::replace(s, search, replace);
+        }
+
+        using B::replace;
+    };
+
     template<typename String, typename Iterable>
     check_return Error toString(String& s, const Iterable& i) {
         CORE_RETURN_ERROR(s.append("["));
@@ -579,7 +578,7 @@ namespace Core {
     }
 
     template<int N>
-    using String8 = ArrayCharString<N>;
+    using String8 = ArrayString<N, char, CharString>;
 
     template<int N>
     using String32 = Char32String<N>;

+ 2 - 2
test/modules/ArrayStringTests.cpp

@@ -50,7 +50,7 @@ static void testStringAppend8() {
 }
 
 static void testStringAppendOverflow8() {
-    Core::ArrayCharString<6> s;
+    Core::String8<6> s;
     CORE_TEST_ERROR(s.append("te"));
     CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, s.append("23334444"));
     CORE_TEST_TRUE(build("te23334444") != s);
@@ -192,7 +192,7 @@ static void testBool8() {
 }
 
 static void testIntOverflow8() {
-    Core::ArrayCharString<4> s;
+    Core::String8<4> s;
     CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, s.append(123456));
 
     String8 o;