Introduction of class SuperString
[libacl.git] / src / string.cpp
index dd159d0..6ad0fd3 100644 (file)
@@ -432,7 +432,6 @@ std::istream& operator>>(std::istream& s, String& x)
  *   The subscript operator is provided for access to individual characters
  */
 
-// TODO: boundary check.
 // TODO: Assignment to an individual character does not decouple references (BUG)
 
 char& String::operator[](size_t i)
@@ -717,3 +716,36 @@ int String::in(String & x)
    else
       return -1;
 }
+
+SuperString String::split(const String &separator)
+{
+   SuperString splitted;
+   char *sep, *part;
+
+   part = p->s;
+   sep = strstr(p->s, separator.p->s);
+
+   while (sep != NULL)
+   {
+      //  Create a new string from the part until the separator
+
+      int  len  = sep - part;
+      char *str = new char[len + 1];
+
+      strncpy(str, part, len);
+      str[len] = '\0';
+      splitted += String(str);
+
+      // Look for the next separator
+
+      sep += ~separator;
+      part = sep;
+      sep = strstr(sep, separator.p->s);
+   }
+
+   // Add the leftover after the last separator.
+
+   splitted += String(part);
+
+   return splitted;
+}