Browse Source

Move unicode reading into static function

Kajetan Johannes Hammerle 2 weeks ago
parent
commit
8595aeaf61
4 changed files with 49 additions and 42 deletions
  1. 1 0
      .gitignore
  2. 1 0
      CMakeLists.txt
  3. 3 42
      include/core/utils/ArrayString.hpp
  4. 44 0
      src/ArrayString.cpp

+ 1 - 0
.gitignore

@@ -4,3 +4,4 @@ install
 profile
 compiler
 .cache
+*.swp

+ 1 - 0
CMakeLists.txt

@@ -24,6 +24,7 @@ set(SRC
     "src/Mutex.cpp"
     "src/FileReader.cpp"
     "src/ErrorSimulator.cpp"
+    "src/ArrayString.cpp"
 )
 
 set(SRC_TESTS

+ 3 - 42
include/core/utils/ArrayString.hpp

@@ -17,6 +17,8 @@ namespace Core {
         return i;
     }
 
+    Error readUnicode(c32& u, const char*& s);
+
     template<int N, typename CharType>
     class ArrayString final {
         int length;
@@ -387,47 +389,6 @@ namespace Core {
             return Core::putChar(static_cast<int>(((u >> shift) & a) | o));
         }
 
-        static c32 read(const char*& s) {
-            if(*s == '\0') {
-                return 0;
-            }
-            return static_cast<c32>(*(s++));
-        }
-
-        static Error readUnicode(c32& u, const char*& s) {
-            u = read(s);
-            if((u & 0x80) == 0) {
-                return Error::NONE;
-            }
-            if((u & 0xE0) == 0xC0) {
-                c32 u2 = read(s);
-                if(u2 == 0) {
-                    return Error::INVALID_CHAR;
-                }
-                u = ((u & 0x1F) << 6) | (u2 & 0x3F);
-                return Error::NONE;
-            } else if((u & 0xF0) == 0xE0) {
-                c32 u2 = read(s);
-                c32 u3 = read(s);
-                if(u2 == 0 || u3 == 0) {
-                    return Error::INVALID_CHAR;
-                }
-                u = ((u & 0xF) << 12) | ((u2 & 0x3F) << 6) | (u3 & 0x3F);
-                return Error::NONE;
-            } else if((u & 0xF8) == 0xF0) {
-                c32 u2 = read(s);
-                c32 u3 = read(s);
-                c32 u4 = read(s);
-                if(u2 == 0 || u3 == 0 || u4 == 0) {
-                    return Error::INVALID_CHAR;
-                }
-                u = ((u & 0x07) << 18) | ((u2 & 0x3F) << 12) |
-                    ((u3 & 0x3F) << 6) | (u4 & 0x3F);
-                return Error::NONE;
-            }
-            return Error::INVALID_CHAR;
-        }
-
         void addToHash(c32 u) {
             hash = static_cast<u32>(2120251889) * hash + static_cast<u32>(u);
         }
@@ -497,4 +458,4 @@ bool operator!=(const CharType* cs, const Core::ArrayString<N, CharType>& s) {
     return s != cs;
 }
 
-#endif
+#endif

+ 44 - 0
src/ArrayString.cpp

@@ -0,0 +1,44 @@
+#include "core/utils/ArrayString.hpp"
+
+#include "core/utils/Error.hpp"
+
+static c32 read(const char*& s) {
+    if(*s == '\0') {
+        return 0;
+    }
+    return static_cast<c32>(*(s++));
+}
+
+Core::Error Core::readUnicode(c32& u, const char*& s) {
+    u = read(s);
+    if((u & 0x80) == 0) {
+        return Error::NONE;
+    }
+    if((u & 0xE0) == 0xC0) {
+        c32 u2 = read(s);
+        if(u2 == 0) {
+            return Error::INVALID_CHAR;
+        }
+        u = ((u & 0x1F) << 6) | (u2 & 0x3F);
+        return Error::NONE;
+    } else if((u & 0xF0) == 0xE0) {
+        c32 u2 = read(s);
+        c32 u3 = read(s);
+        if(u2 == 0 || u3 == 0) {
+            return Error::INVALID_CHAR;
+        }
+        u = ((u & 0xF) << 12) | ((u2 & 0x3F) << 6) | (u3 & 0x3F);
+        return Error::NONE;
+    } else if((u & 0xF8) == 0xF0) {
+        c32 u2 = read(s);
+        c32 u3 = read(s);
+        c32 u4 = read(s);
+        if(u2 == 0 || u3 == 0 || u4 == 0) {
+            return Error::INVALID_CHAR;
+        }
+        u = ((u & 0x07) << 18) | ((u2 & 0x3F) << 12) | ((u3 & 0x3F) << 6) |
+            (u4 & 0x3F);
+        return Error::NONE;
+    }
+    return Error::INVALID_CHAR;
+}