From: Arjen Baart Date: Sat, 13 Mar 2021 12:25:14 +0000 (+0100) Subject: Added SuperString::insert() and SuperString::erase() X-Git-Url: http://www.andromeda.nl/gitweb/?a=commitdiff_plain;h=4d17d4bdabc4d0821fc75cf68d3d5340004ae7f8;p=libacl.git Added SuperString::insert() and SuperString::erase() --- diff --git a/doc/superstring.xml b/doc/superstring.xml index 467c767..f6d5529 100644 --- a/doc/superstring.xml +++ b/doc/superstring.xml @@ -85,7 +85,13 @@ Access out of range will throw a std::out_of_range exception.
-Adding to SuperStrings +Adding and removing Strings + + +Just like std::vector objects, Strings can be inserted into and erased from +a SuperString with the insert(int position, const &String) and +erase(int position) methods. + SuperString objects can be added together, i.e. concatenated, with the + @@ -100,6 +106,9 @@ SuperString y("def"); z = x + y; // z = [ "abc", "def" ] y += z; // y = [ "def", "abc", "def" ] + +the + operater can add strings and superstrings to append or prepend the one to the other. the result us always a superstring. +
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 diff --git a/src/String.h b/src/String.h index b419b68..e264f2f 100644 --- a/src/String.h +++ b/src/String.h @@ -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&); diff --git a/test/superstring_cat.cpp b/test/superstring_cat.cpp index 194154f..1921ce8 100644 --- a/test/superstring_cat.cpp +++ b/test/superstring_cat.cpp @@ -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 index 0000000..80fc828 --- /dev/null +++ b/test/superstring_cat.exp @@ -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)