String::Split() documentation and test
authorArjen Baart <arjen@andromeda.nl>
Sun, 4 Oct 2020 08:04:52 +0000 (10:04 +0200)
committerArjen Baart <arjen@andromeda.nl>
Sun, 4 Oct 2020 08:04:52 +0000 (10:04 +0200)
TODO
doc/main.css
doc/string.xml
test/string_split_join.cpp
test/string_split_join.exp

diff --git a/TODO b/TODO
index 1b87c40..52cb9be 100644 (file)
--- a/TODO
+++ b/TODO
@@ -6,6 +6,10 @@ Things to do:
 - String: assign fstream objects to read and write a file
 - String: convert to and from std::sstream and std::string
 
+- SuperString: sort and uniq
+- SuperString: find (regex)
+- SuperString: remove empty elements or elements macthing a pattern
+
 - date: test parser for invalid dates and syntax errors
 - date: Parser for stream input
 - date: Yearday, Weekday and week number
index 42e1a28..d5b9b71 100644 (file)
@@ -51,11 +51,12 @@ p
 
 pre.example
 {
-   background : lightblue ;
+   background : #eeffee ;
+   border-width : 1px ;
    width      : 80% ;
-   margin-left: 5% ;
+   margin-left: 2% ;
    padding-left: 2% ;
-   padding-top: 100px% ;
+   padding-top: 1% ;
 }
 
 span.remark
index 998bfcd..e4ceabc 100644 (file)
@@ -217,7 +217,7 @@ Convert the string to all lower-case characters.
 </section>
 
 <section>
-<heading>Pattern matching</heading>
+<heading>Searching in Strings</heading>
 <description>
 <item tag="int in(String &amp;x)">
 Find the string x within the string. Returns the index where <emph>x</emph> was
@@ -225,6 +225,32 @@ found. Returns -1 if x was not found in the string. see also strstr().
 </item>
 </description>
 </section>
+
+<section>
+<heading>Splitting Strings into SuperStrings</heading>
+<description>
+<item tag="SuperString split(String &amp;separator)">
+Return a list of the words in the string, using <emph>separator</emph> as the delimiter string.
+<example>
+String numbers(&quot;2,4,7&quot;);
+numbers.split(",");  // &quot;2&quot; &quot;4&quot; &quot;7&quot;
+</example>
+Splitting an empty string will result in a SuperString with an empty string as its only element.
+The <emph>separator</emph> can be a multiple character string, for example:
+<example>
+String somexml(&quot;A&amp;amp;B&amp;amp;C&quot;);
+somexml.split("&amp;amp;");   // "A" "B" "C"
+</example>
+Consecutive separators are not grouped together and are deemed to delimit empty strings, for example:
+<example>
+String numbers(&quot;3.14   42&quot;);   //  Note 3 spaces
+numbers.split(" ");   // "3.14" "" ""  "42"
+</example>
+This also happens with separators at the start and the end of the string.
+</item>
+</description>
+</section>
+
 <section>
 <heading>Stream I/O</heading>
 <para>
index 361dd3c..fbe7946 100644 (file)
@@ -43,6 +43,48 @@ int main()
    std::cout << "Joined with ' ': " << s1 << "\n";
    assert(s1 == "The String To Split");
 
+   String s2("A&amp;B&amp;C");
+   SuperString ss2 = s2.split("&amp;");   // "A" "B" "C"
+   std::cout << "The string \"" << s2 << "\" split on \"&amp;\":\n";
+   print_ss(ss2);
+   std::cout << "\n";
+   assert(~ss2 == 3);
+
+   //  Split an empty string
+   String s3("");
+   SuperString ss3 = s3.split(",");
+   std::cout << "The string \"" << s3 << "\" split on \",\":\n";
+   print_ss(ss3);
+   std::cout << "\n";
+   assert(~ss3 == 1);
+
+   //  Test consequtive separators
+   String s4("3.14   42");   //  Note 3 spaces
+   SuperString ss4 = s4.split(" ");
+   std::cout << "The string \"" << s4 << "\" split on \" \":\n";
+   print_ss(ss4);
+   std::cout << "\n";
+
+   assert(~ss4 == 4);
+   assert(ss4[0] == "3.14");
+   assert(ss4[1] == "");
+   assert(ss4[2] == "");
+   assert(ss4[3] == "42");
+
+   //  Test a separator at the start and end of the string
+   String s5("-aaa-bbb-");   //  Note 3 spaces
+   SuperString ss5 = s5.split("-");
+   std::cout << "The string \"" << s5 << "\" split on \"-\":\n";
+   print_ss(ss5);
+   std::cout << "\n";
+
+   assert(~ss5 == 4);
+   assert(ss5[0] == "");
+   assert(ss5[1] == "aaa");
+   assert(ss5[2] == "bbb");
+   assert(ss5[3] == "");
+
+
    return 0;
 }
 
index 0f1205d..5765ad7 100644 (file)
@@ -1,4 +1,12 @@
 The string The_String_To_Split split on "_":
 ["The","String","To","Split"]
 Joined with ' ': The String To Split
+The string "A&amp;B&amp;C" split on "&amp;":
+["A","B","C"]
+The string "" split on ",":
+[""]
+The string "3.14   42" split on " ":
+["3.14","","","42"]
+The string "-aaa-bbb-" split on "-":
+["","aaa","bbb",""]
 PASS string_split_join (exit status: 0)