From: Arjen Baart Date: Sun, 4 Oct 2020 08:04:52 +0000 (+0200) Subject: String::Split() documentation and test X-Git-Url: http://www.andromeda.nl/gitweb/?a=commitdiff_plain;h=951cc12a228f4eba0164089b06570a54dacb46de;p=libacl.git String::Split() documentation and test --- diff --git a/TODO b/TODO index 1b87c40..52cb9be 100644 --- 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 diff --git a/doc/main.css b/doc/main.css index 42e1a28..d5b9b71 100644 --- a/doc/main.css +++ b/doc/main.css @@ -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 diff --git a/doc/string.xml b/doc/string.xml index 998bfcd..e4ceabc 100644 --- a/doc/string.xml +++ b/doc/string.xml @@ -217,7 +217,7 @@ Convert the string to all lower-case characters.
-Pattern matching +Searching in Strings Find the string x within the string. Returns the index where x was @@ -225,6 +225,32 @@ found. Returns -1 if x was not found in the string. see also strstr().
+ +
+Splitting Strings into SuperStrings + + +Return a list of the words in the string, using separator as the delimiter string. + +String numbers("2,4,7"); +numbers.split(","); // "2" "4" "7" + +Splitting an empty string will result in a SuperString with an empty string as its only element. +The separator can be a multiple character string, for example: + +String somexml("A&B&C"); +somexml.split("&"); // "A" "B" "C" + +Consecutive separators are not grouped together and are deemed to delimit empty strings, for example: + +String numbers("3.14 42"); // Note 3 spaces +numbers.split(" "); // "3.14" "" "" "42" + +This also happens with separators at the start and the end of the string. + + +
+
Stream I/O diff --git a/test/string_split_join.cpp b/test/string_split_join.cpp index 361dd3c..fbe7946 100644 --- a/test/string_split_join.cpp +++ b/test/string_split_join.cpp @@ -43,6 +43,48 @@ int main() std::cout << "Joined with ' ': " << s1 << "\n"; assert(s1 == "The String To Split"); + String s2("A&B&C"); + SuperString ss2 = s2.split("&"); // "A" "B" "C" + std::cout << "The string \"" << s2 << "\" split on \"&\":\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; } diff --git a/test/string_split_join.exp b/test/string_split_join.exp index 0f1205d..5765ad7 100644 --- a/test/string_split_join.exp +++ b/test/string_split_join.exp @@ -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&B&C" split on "&": +["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)