Selaa lähdekoodia

New String::Split implementation; keeps empty splits by default, optionally omit-able using splitMode parameter

Colin Barrett 10 vuotta sitten
vanhempi
sitoutus
d83f9d0a41
2 muutettua tiedostoa jossa 26 lisäystä ja 41 poistoa
  1. 17 39
      Source/Urho3D/Container/Str.cpp
  2. 9 2
      Source/Urho3D/Container/Str.h

+ 17 - 39
Source/Urho3D/Container/Str.cpp

@@ -558,9 +558,9 @@ String String::ToUpper() const
     return ret;
     return ret;
 }
 }
 
 
-Vector<String> String::Split(char separator) const
+Vector<String> String::Split(char separator, StringSplit splitMode) const
 {
 {
-    return Split(CString(), separator);
+    return Split(CString(), separator, splitMode);
 }
 }
 
 
 void String::Join(const Vector<String>& subStrings, const String& glue)
 void String::Join(const Vector<String>& subStrings, const String& glue)
@@ -1041,49 +1041,27 @@ unsigned String::DecodeUTF16(const wchar_t*& src)
 }
 }
 #endif
 #endif
 
 
-Vector<String> String::Split(const char* str, char separator)
+Vector<String> String::Split(const char* str, char separator, StringSplit splitMode)
 {
 {
     Vector<String> ret;
     Vector<String> ret;
-    unsigned pos = 0;
-    unsigned length = CStringLength(str);
-
-    while (pos < length)
-    {
-        if (str[pos] != separator)
-            break;
-        ++pos;
-    }
-
-    while (pos < length)
+    const char* strEnd = str + String::CStringLength(str);
+    for (const char* splitEnd = str; splitEnd != strEnd; ++splitEnd)
     {
     {
-        unsigned start = pos;
-
-        while (start < length)
-        {
-            if (str[start] == separator)
-                break;
-
-            ++start;
-        }
-
-        if (start == length)
+        if (*splitEnd == separator)
         {
         {
-            ret.Push(String(&str[pos]));
-            break;
-        }
-
-        unsigned end = start;
-
-        while (end < length)
-        {
-            if (str[end] != separator)
-                break;
-
-            ++end;
+            const ptrdiff_t splitLen = splitEnd - str;
+            if (splitLen > 0 || splitMode == SPLIT_KEEP_EMPTY)
+            {
+                ret.Push(String(str, splitLen));
+            }
+            str = splitEnd + 1;
         }
         }
+    }
 
 
-        ret.Push(String(&str[pos], start - pos));
-        pos = end;
+    const ptrdiff_t splitLen = strEnd - str;
+    if (splitLen > 0 || splitMode == SPLIT_KEEP_EMPTY)
+    {
+        ret.Push(String(str, splitLen));
     }
     }
 
 
     return ret;
     return ret;

+ 9 - 2
Source/Urho3D/Container/Str.h

@@ -36,6 +36,13 @@ static const int MATRIX_CONVERSION_BUFFER_LENGTH = 256;
 
 
 class WString;
 class WString;
 
 
+/// Mode for String::Split (default is to keep empty splits)
+enum StringSplit
+{
+    SPLIT_KEEP_EMPTY = 0,
+    SPLIT_OMIT_EMPTY
+};
+
 /// %String class.
 /// %String class.
 class URHO3D_API String
 class URHO3D_API String
 {
 {
@@ -388,7 +395,7 @@ public:
     /// Return string in lowercase.
     /// Return string in lowercase.
     String ToLower() const;
     String ToLower() const;
     /// Return substrings split by a separator char.
     /// Return substrings split by a separator char.
-    Vector<String> Split(char separator) const;
+    Vector<String> Split(char separator, StringSplit splitMode = SPLIT_KEEP_EMPTY) const;
     /// Join substrings with a 'glue' string.
     /// Join substrings with a 'glue' string.
     void Join(const Vector<String>& subStrings, const String& glue);
     void Join(const Vector<String>& subStrings, const String& glue);
     /// Return index to the first occurrence of a string, or NPOS if not found.
     /// Return index to the first occurrence of a string, or NPOS if not found.
@@ -463,7 +470,7 @@ public:
     }
     }
 
 
     /// Return substrings split by a separator char.
     /// Return substrings split by a separator char.
-    static Vector<String> Split(const char* str, char separator);
+    static Vector<String> Split(const char* str, char separator, StringSplit splitMode = SPLIT_KEEP_EMPTY);
     /// Return a string by joining substrings with a 'glue' string.
     /// Return a string by joining substrings with a 'glue' string.
     static String Joined(const Vector<String>& subStrings, const String& glue);
     static String Joined(const Vector<String>& subStrings, const String& glue);
     /// Encode Unicode character to UTF8. Pointer will be incremented.
     /// Encode Unicode character to UTF8. Pointer will be incremented.