소스 검색

common string functions

Kajetan Johannes Hammerle 2 년 전
부모
커밋
f11963c48a
2개의 변경된 파일161개의 추가작업 그리고 2개의 파일을 삭제
  1. 119 0
      tests/ArrayStringTests.cpp
  2. 42 2
      utils/ArrayString.h

+ 119 - 0
tests/ArrayStringTests.cpp

@@ -263,6 +263,120 @@ static void testAsHashMapKey() {
     }
 }
 
+static void testStartsWith() {
+    String s;
+    CORE_TEST_FALSE(s.append("0123456789"));
+
+    String s2;
+    CORE_TEST_FALSE(s2.append("123"));
+    String s3;
+    CORE_TEST_FALSE(s3.append("234"));
+    String s4;
+    CORE_TEST_FALSE(s4.append("789"));
+    String s5;
+    CORE_TEST_FALSE(s5.append("124"));
+    String s6;
+    String s7;
+    CORE_TEST_FALSE(s7.append("7891"));
+
+    CORE_TEST_FALSE(s.startsWidth(s2));
+    CORE_TEST_TRUE(s.startsWidth(s2, 1));
+
+    CORE_TEST_FALSE(s.startsWidth(s3));
+    CORE_TEST_TRUE(s.startsWidth(s3, 2));
+
+    CORE_TEST_FALSE(s.startsWidth(s4));
+    CORE_TEST_TRUE(s.startsWidth(s4, 7));
+
+    CORE_TEST_FALSE(s.startsWidth(s5));
+    CORE_TEST_FALSE(s.startsWidth(s5, 3));
+
+    CORE_TEST_TRUE(s.startsWidth(s6));
+    CORE_TEST_TRUE(s.startsWidth(s6, 3));
+
+    CORE_TEST_FALSE(s.startsWidth(s7));
+    CORE_TEST_FALSE(s.startsWidth(s7, 7));
+}
+
+static void testSearch() {
+    String s;
+    CORE_TEST_FALSE(s.append("0123456789"));
+
+    String s2;
+    CORE_TEST_FALSE(s2.append("123"));
+    String s3;
+    CORE_TEST_FALSE(s3.append("234"));
+    String s4;
+    CORE_TEST_FALSE(s4.append("789"));
+    String s5;
+    CORE_TEST_FALSE(s5.append("124"));
+    String s6;
+    String s7;
+    CORE_TEST_FALSE(s7.append("7891"));
+
+    CORE_TEST_EQUAL(1, s.search(s2));
+    CORE_TEST_EQUAL(2, s.search(s3));
+    CORE_TEST_EQUAL(7, s.search(s4));
+    CORE_TEST_EQUAL(-1, s.search(s5));
+    CORE_TEST_EQUAL(0, s.search(s6));
+    CORE_TEST_EQUAL(-1, s.search(s7));
+
+    CORE_TEST_EQUAL(-1, s.search(s2, 3));
+    CORE_TEST_EQUAL(-1, s.search(s3, 3));
+    CORE_TEST_EQUAL(7, s.search(s4, 3));
+    CORE_TEST_EQUAL(-1, s.search(s5, 3));
+    CORE_TEST_EQUAL(3, s.search(s6, 3));
+    CORE_TEST_EQUAL(-1, s.search(s7, 3));
+}
+
+static void testContains() {
+    String s;
+    CORE_TEST_FALSE(s.append("0123456789"));
+
+    String s2;
+    CORE_TEST_FALSE(s2.append("123"));
+    String s3;
+    CORE_TEST_FALSE(s3.append("234"));
+    String s4;
+    CORE_TEST_FALSE(s4.append("789"));
+    String s5;
+    CORE_TEST_FALSE(s5.append("124"));
+    String s6;
+    String s7;
+    CORE_TEST_FALSE(s7.append("7891"));
+
+    CORE_TEST_TRUE(s.contains(s2));
+    CORE_TEST_TRUE(s.contains(s3));
+    CORE_TEST_TRUE(s.contains(s4));
+    CORE_TEST_FALSE(s.contains(s5));
+    CORE_TEST_TRUE(s.contains(s6));
+    CORE_TEST_FALSE(s.contains(s7));
+}
+
+static void testSearchChar() {
+    String s;
+    CORE_TEST_FALSE(s.append("01üää3ä"));
+
+    CORE_TEST_EQUAL(0, s.search(U'0'));
+    CORE_TEST_EQUAL(1, s.search(U'1'));
+    CORE_TEST_EQUAL(2, s.search(U'ü'));
+    CORE_TEST_EQUAL(3, s.search(U'ä'));
+    CORE_TEST_EQUAL(4, s.search(U'ä', 4));
+    CORE_TEST_EQUAL(5, s.search(U'3'));
+    CORE_TEST_EQUAL(6, s.search(U'ä', 5));
+}
+
+static void testContainsChar() {
+    String s;
+    CORE_TEST_FALSE(s.append("01üää3ä"));
+
+    CORE_TEST_TRUE(s.contains(U'0'));
+    CORE_TEST_TRUE(s.contains(U'1'));
+    CORE_TEST_TRUE(s.contains(U'ü'));
+    CORE_TEST_TRUE(s.contains(U'ä'));
+    CORE_TEST_FALSE(s.contains(U'ö'));
+}
+
 void Core::ArrayStringTests::test() {
     testEquality();
     testUnicodeEquality();
@@ -292,4 +406,9 @@ void Core::ArrayStringTests::test() {
     testHashCode();
     testAddSelf();
     testAsHashMapKey();
+    testStartsWith();
+    testSearch();
+    testContains();
+    testSearchChar();
+    testContainsChar();
 }

+ 42 - 2
utils/ArrayString.h

@@ -8,7 +8,6 @@
 namespace Core {
     template<int N>
     class ArrayString final {
-    public:
         int length;
         u32 hash;
         static constexpr int DATA_LENGTH =
@@ -34,7 +33,7 @@ namespace Core {
 
         template<int L>
         bool operator==(const ArrayString<L>& other) const {
-            if(length != other.length) {
+            if(length != other.getLength()) {
                 return false;
             }
             for(int i = 0; i < length; i++) {
@@ -257,6 +256,47 @@ namespace Core {
             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;
+        }
+
     private:
         // returns true on error and calls the error callback
         check_return static bool printChar(u32 u, u32 shift, u32 a, u32 o) {