From befc877768c0994c83e66ac80e1129114d983eee Mon Sep 17 00:00:00 2001 From: Arjen Baart Date: Thu, 18 Jun 2020 21:02:00 +0200 Subject: [PATCH] SuperString join() --- src/String.h | 1 + src/superstring.cpp | 16 ++++++++++++++++ test/string_split_join.cpp | 6 ++++++ test/string_split_join.exp | 4 ++++ 4 files changed, 27 insertions(+) create mode 100644 test/string_split_join.exp diff --git a/src/String.h b/src/String.h index bd66c53..2aac0d0 100644 --- a/src/String.h +++ b/src/String.h @@ -485,6 +485,7 @@ public: return x._ss > y._ss; } + String join(const String &separator); }; #endif /* STRING_H */ diff --git a/src/superstring.cpp b/src/superstring.cpp index b71fd5f..c2d6696 100644 --- a/src/superstring.cpp +++ b/src/superstring.cpp @@ -62,3 +62,19 @@ SuperString& SuperString::operator+=(const String& x) return *this; } + +String SuperString::join(const String &separator) +{ + String joined; + + if (_ss.size() != 0) + { + joined = _ss[0]; + for (int i = 1; i < _ss.size(); i++) + { + joined += separator + _ss[i]; + } + } + + return joined; +} diff --git a/test/string_split_join.cpp b/test/string_split_join.cpp index 63aa7d9..361dd3c 100644 --- a/test/string_split_join.cpp +++ b/test/string_split_join.cpp @@ -37,6 +37,12 @@ int main() assert(ss0[2] == "To"); assert(ss0[3] == "Split"); + String s1; + + s1 = ss0.join(" "); + std::cout << "Joined with ' ': " << s1 << "\n"; + assert(s1 == "The String To Split"); + return 0; } diff --git a/test/string_split_join.exp b/test/string_split_join.exp new file mode 100644 index 0000000..0f1205d --- /dev/null +++ b/test/string_split_join.exp @@ -0,0 +1,4 @@ +The string The_String_To_Split split on "_": +["The","String","To","Split"] +Joined with ' ': The String To Split +PASS string_split_join (exit status: 0) -- 2.20.1