Browse Source

substring and string replace

Kajetan Johannes Hammerle 1 year ago
parent
commit
83738d1044
2 changed files with 105 additions and 0 deletions
  1. 59 0
      tests/ArrayStringTests.cpp
  2. 46 0
      utils/ArrayString.h

+ 59 - 0
tests/ArrayStringTests.cpp

@@ -377,6 +377,62 @@ static void testContainsChar() {
     CORE_TEST_FALSE(s.contains(U'ö'));
 }
 
+static void testSubString() {
+    String s;
+    CORE_TEST_FALSE(s.append("01üää3ä"));
+
+    CORE_TEST_STRING("01üää3ä", s.substring(-2));
+    CORE_TEST_STRING("1üää3ä", s.substring(1));
+    CORE_TEST_STRING("üää3ä", s.substring(2));
+    CORE_TEST_STRING("ää3ä", s.substring(3));
+    CORE_TEST_STRING("ä3ä", s.substring(4));
+
+    CORE_TEST_STRING("01üää3ä", s.substring(0, 6));
+    CORE_TEST_STRING("1üää3", s.substring(1, 5));
+    CORE_TEST_STRING("üää", s.substring(2, 4));
+    CORE_TEST_STRING("ä", s.substring(3, 3));
+    CORE_TEST_STRING("", s.substring(4, 2));
+    CORE_TEST_STRING("ä3ä", s.substring(4, 23));
+}
+
+static void testReplace() {
+    String s;
+    CORE_TEST_FALSE(s.append("0äääää1üää3ä"));
+
+    String search;
+    CORE_TEST_FALSE(search.append("ää"));
+
+    String replace;
+    CORE_TEST_FALSE(replace.append("ABCD"));
+
+    CORE_TEST_FALSE(s.replace(search, replace));
+
+    CORE_TEST_STRING("0ABCDABCDä1üABCD3ä", s);
+
+    String s2;
+    CORE_TEST_FALSE(s2.append("0ABCDABCDä1üABCD3ä"));
+    CORE_TEST_EQUAL(s2.hashCode(), s.hashCode());
+}
+
+static void testReplaceChar() {
+    String s;
+    CORE_TEST_FALSE(s.append("01üää3ä"));
+    s.replace(U'0', U'A');
+    CORE_TEST_STRING("A1üää3ä", s);
+    s.replace(U'1', U'B');
+    CORE_TEST_STRING("ABüää3ä", s);
+    s.replace(U'ü', U'C');
+    CORE_TEST_STRING("ABCää3ä", s);
+    s.replace(U'ä', U'D');
+    CORE_TEST_STRING("ABCDD3D", s);
+    s.replace(U'3', U'E');
+    CORE_TEST_STRING("ABCDDED", s);
+
+    String s2;
+    CORE_TEST_FALSE(s2.append("ABCDDED"));
+    CORE_TEST_EQUAL(s2.hashCode(), s.hashCode());
+}
+
 void Core::ArrayStringTests::test() {
     testEquality();
     testUnicodeEquality();
@@ -411,4 +467,7 @@ void Core::ArrayStringTests::test() {
     testContains();
     testSearchChar();
     testContainsChar();
+    testSubString();
+    testReplace();
+    testReplaceChar();
 }

+ 46 - 0
utils/ArrayString.h

@@ -297,6 +297,52 @@ namespace Core {
             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) {