Bladeren bron

Fix typo, use more string standard functions

Kajetan Johannes Hammerle 4 weken geleden
bovenliggende
commit
12dbbfb529
3 gewijzigde bestanden met toevoegingen van 43 en 54 verwijderingen
  1. 2 2
      include/core/utils/ArrayString.hpp
  2. 17 28
      src/ArrayString.cpp
  3. 24 24
      test/modules/ArrayStringTests.cpp

+ 2 - 2
include/core/utils/ArrayString.hpp

@@ -102,7 +102,7 @@ namespace Core {
             return copyFormat(*this, s, Core::forward<Args>(args)...);
         }
 
-        bool startsWidth(const CharString& other, int from = 0) const;
+        bool startsWith(const CharString& other, int from = 0) const;
         int search(const CharString& other, int from = 0) const;
         bool contains(const CharString& other, int from = 0) const;
         int search(char u, int from = 0) const;
@@ -160,7 +160,7 @@ namespace Core {
             return copyFormat(*this, s, Core::forward<Args>(args)...);
         }
 
-        bool startsWidth(const Char32String& other, int from = 0) const;
+        bool startsWith(const Char32String& other, int from = 0) const;
         int search(const Char32String& other, int from = 0) const;
         bool contains(const Char32String& other, int from = 0) const;
         int search(c32 u, int from = 0) const;

+ 17 - 28
src/ArrayString.cpp

@@ -153,10 +153,7 @@ void CharString::clear() {
 }
 
 Error CharString::print() const {
-    for(int i = 0; i < length; i++) {
-        CORE_RETURN_ERROR(Core::putChar(data[i]));
-    }
-    return ErrorCode::NONE;
+    return Core::putChars(data);
 }
 
 Error CharString::printLine() const {
@@ -164,25 +161,18 @@ Error CharString::printLine() const {
     return Core::putChar('\n');
 }
 
-bool CharString::startsWidth(const CharString& other, int from) 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;
+bool CharString::startsWith(const CharString& other, int from) const {
+    return from >= 0 && length - from >= other.getLength() &&
+           strncmp(data + from, other.data,
+                   static_cast<size_t>(other.getLength())) == 0;
 }
 
 int CharString::search(const CharString& other, int from) const {
-    for(int i = from; i < length; i++) {
-        if(startsWidth(other, i)) {
-            return i;
-        }
+    if(from < 0) {
+        return false;
     }
-    return -1;
+    char* f = strstr(data + from, other.data);
+    return f == nullptr ? -1 : static_cast<int>(f - data);
 }
 
 bool CharString::contains(const CharString& other, int from) const {
@@ -190,12 +180,11 @@ bool CharString::contains(const CharString& other, int from) const {
 }
 
 int CharString::search(char u, int from) const {
-    for(int i = from; i < length; i++) {
-        if(data[i] == u) {
-            return i;
-        }
+    if(from < 0) {
+        return false;
     }
-    return -1;
+    char* f = strchr(data + from, u);
+    return f == nullptr ? -1 : static_cast<int>(f - data);
 }
 
 bool CharString::contains(char u, int from) const {
@@ -220,7 +209,7 @@ Error CharString::replace(CharString& s, const CharString& search,
                           const CharString& replace) {
     int i = 0;
     while(i < length) {
-        if(startsWidth(search, i)) {
+        if(startsWith(search, i)) {
             CORE_RETURN_ERROR(s.append(replace));
             i += search.getLength();
         } else {
@@ -386,7 +375,7 @@ Error Char32String::printLine() const {
     return ErrorCode::NONE;
 }
 
-bool Char32String::startsWidth(const Char32String& other, int from) const {
+bool Char32String::startsWith(const Char32String& other, int from) const {
     if(from > length - other.getLength()) {
         return false;
     }
@@ -400,7 +389,7 @@ bool Char32String::startsWidth(const Char32String& other, int from) const {
 
 int Char32String::search(const Char32String& other, int from) const {
     for(int i = from; i < length; i++) {
-        if(startsWidth(other, i)) {
+        if(startsWith(other, i)) {
             return i;
         }
     }
@@ -442,7 +431,7 @@ Error Char32String::replace(Char32String& s, const Char32String& search,
                             const Char32String& replace) {
     int i = 0;
     while(i < length) {
-        if(startsWidth(search, i)) {
+        if(startsWith(search, i)) {
             CORE_RETURN_ERROR(s.append(replace));
             i += search.getLength();
         } else {

+ 24 - 24
test/modules/ArrayStringTests.cpp

@@ -247,23 +247,23 @@ static void testStartsWith8() {
     String8 s7;
     CORE_TEST_ERROR(s7.append("7891"));
 
-    CORE_TEST_FALSE(s.startsWidth(s2));
-    CORE_TEST_TRUE(s.startsWidth(s2, 1));
+    CORE_TEST_FALSE(s.startsWith(s2));
+    CORE_TEST_TRUE(s.startsWith(s2, 1));
 
-    CORE_TEST_FALSE(s.startsWidth(s3));
-    CORE_TEST_TRUE(s.startsWidth(s3, 2));
+    CORE_TEST_FALSE(s.startsWith(s3));
+    CORE_TEST_TRUE(s.startsWith(s3, 2));
 
-    CORE_TEST_FALSE(s.startsWidth(s4));
-    CORE_TEST_TRUE(s.startsWidth(s4, 7));
+    CORE_TEST_FALSE(s.startsWith(s4));
+    CORE_TEST_TRUE(s.startsWith(s4, 7));
 
-    CORE_TEST_FALSE(s.startsWidth(s5));
-    CORE_TEST_FALSE(s.startsWidth(s5, 3));
+    CORE_TEST_FALSE(s.startsWith(s5));
+    CORE_TEST_FALSE(s.startsWith(s5, 3));
 
-    CORE_TEST_TRUE(s.startsWidth(s6));
-    CORE_TEST_TRUE(s.startsWidth(s6, 3));
+    CORE_TEST_TRUE(s.startsWith(s6));
+    CORE_TEST_TRUE(s.startsWith(s6, 3));
 
-    CORE_TEST_FALSE(s.startsWidth(s7));
-    CORE_TEST_FALSE(s.startsWidth(s7, 7));
+    CORE_TEST_FALSE(s.startsWith(s7));
+    CORE_TEST_FALSE(s.startsWith(s7, 7));
 }
 
 static void testSearch8() {
@@ -698,23 +698,23 @@ static void testStartsWith32() {
     String32 s7;
     CORE_TEST_ERROR(s7.append(U"7891"));
 
-    CORE_TEST_FALSE(s.startsWidth(s2));
-    CORE_TEST_TRUE(s.startsWidth(s2, 1));
+    CORE_TEST_FALSE(s.startsWith(s2));
+    CORE_TEST_TRUE(s.startsWith(s2, 1));
 
-    CORE_TEST_FALSE(s.startsWidth(s3));
-    CORE_TEST_TRUE(s.startsWidth(s3, 2));
+    CORE_TEST_FALSE(s.startsWith(s3));
+    CORE_TEST_TRUE(s.startsWith(s3, 2));
 
-    CORE_TEST_FALSE(s.startsWidth(s4));
-    CORE_TEST_TRUE(s.startsWidth(s4, 7));
+    CORE_TEST_FALSE(s.startsWith(s4));
+    CORE_TEST_TRUE(s.startsWith(s4, 7));
 
-    CORE_TEST_FALSE(s.startsWidth(s5));
-    CORE_TEST_FALSE(s.startsWidth(s5, 3));
+    CORE_TEST_FALSE(s.startsWith(s5));
+    CORE_TEST_FALSE(s.startsWith(s5, 3));
 
-    CORE_TEST_TRUE(s.startsWidth(s6));
-    CORE_TEST_TRUE(s.startsWidth(s6, 3));
+    CORE_TEST_TRUE(s.startsWith(s6));
+    CORE_TEST_TRUE(s.startsWith(s6, 3));
 
-    CORE_TEST_FALSE(s.startsWidth(s7));
-    CORE_TEST_FALSE(s.startsWidth(s7, 7));
+    CORE_TEST_FALSE(s.startsWith(s7));
+    CORE_TEST_FALSE(s.startsWith(s7, 7));
 }
 
 static void testSearch32() {