Added SuperString::insert() and SuperString::erase()
authorArjen Baart <arjen@andromeda.nl>
Sat, 13 Mar 2021 12:25:14 +0000 (13:25 +0100)
committerArjen Baart <arjen@andromeda.nl>
Sat, 13 Mar 2021 12:25:14 +0000 (13:25 +0100)
doc/superstring.xml
src/String.h
test/superstring_cat.cpp
test/superstring_cat.exp [new file with mode: 0644]

index 467c767..f6d5529 100644 (file)
@@ -85,7 +85,13 @@ Access out of range will throw a std::out_of_range exception.
 </section>
 
 <section>
-<heading>Adding to SuperStrings</heading>
+<heading>Adding  and removing Strings</heading>
+
+<para>
+Just like <code>std::vector</code> objects, Strings can be inserted into and erased from
+a SuperString with the <code>insert(int position, const &amp;String)</code> and
+<code>erase(int position)</code> methods.
+</para>
 
 <para>
 SuperString objects can be added together, i.e. concatenated, with the <strong>+</strong>
@@ -100,6 +106,9 @@ SuperString y(&quot;def&quot;);
 z = x + y;   //  z = [ &quot;abc&quot;, &quot;def&quot; ]
 y += z;      //  y = [ &quot;def&quot;, &quot;abc&quot;, &quot;def&quot; ]
 </example>
+<para>
+the + operater can add strings and superstrings to append or prepend the one to the other. the result us always a superstring.
+</para>
 </section>
 <para>
  split on a regex ? how to split on an empty string ?
@@ -108,7 +117,6 @@ split and join for csv formats. quoting and escape , ?
 
 
 
-the + operater can add strings and superstrings to append or prepend the one to the other. the result us always a superstring.
 
 add characters and strings ?    ->  TODO
 
index b419b68..e264f2f 100644 (file)
@@ -431,6 +431,18 @@ public:
       return _ss.at(i);
    }
 
+   // Inserting and erasing Strings
+
+   void insert(int position, const String &s)
+   {
+      _ss.insert(_ss.begin() + position, s);
+   }
+
+   void erase(int position)
+   {
+      _ss.erase(_ss.begin() + position);
+   }
+
    // Adding Strings and SuperStrings
 
    friend SuperString operator+(const SuperString&, const String&);
index 194154f..1921ce8 100644 (file)
@@ -89,6 +89,38 @@ int main()
    print_ss(ss9);
    std::cout << "\n";
 
+   //  insert method
+
+   SuperString ss10(2);
+   String      s10("123");
+
+   ss10[0] = "abc";
+   ss10[1] = "def";
+   print_ss(ss10);
+   std::cout << " --> After inserting " << s10 << ": ";
+   ss10.insert(1, s10);
+   print_ss(ss10);
+   std::cout << "\n";
+
+   assert(~ss10 == 3);
+   assert(ss10[1] == "123");
+   assert(ss10[2] == "def");
+
+   //  erase method
+   
+   SuperString ss11(3);
+   ss11[0] = "abc";
+   ss11[1] = "def";
+   ss11[2] = "ghi";
+   print_ss(ss11);
+   std::cout << " --> After erasing position1: ";
+   ss11.erase(1);
+   print_ss(ss11);
+   std::cout << "\n";
+
+   assert(~ss11 == 2);
+   assert(ss11[1] == "ghi");
+
    return 0;
 }
 
diff --git a/test/superstring_cat.exp b/test/superstring_cat.exp
new file mode 100644 (file)
index 0000000..80fc828
--- /dev/null
@@ -0,0 +1,8 @@
+["abc"] + def = ["abc","def"]
+["uvw"]
+["uvw","xyz"]
+def + ["abc"] = ["def","abc"]
+["abc","def"] + ["uvw","xyz"] = ["abc","def","uvw","xyz"]
+["abc","def"] --> After inserting 123: ["abc","123","def"]
+["abc","def","ghi"] --> After erasing position1: ["abc","ghi"]
+PASS superstring_cat (exit status: 0)