Documentation and test for String::index() and String::rindex()
authorArjen Baart <arjen@andromeda.nl>
Sat, 20 Mar 2021 08:49:49 +0000 (09:49 +0100)
committerArjen Baart <arjen@andromeda.nl>
Sat, 20 Mar 2021 08:49:49 +0000 (09:49 +0100)
TODO
doc/string.xml
src/String.h
src/string.cpp
test/Makefile.am
test/string_search.cpp [new file with mode: 0644]
test/string_search.exp [new file with mode: 0644]

diff --git a/TODO b/TODO
index 52cb9be..168a1d7 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,7 +1,6 @@
 Things to do:
 =============
 
-- String: index and rindex methods with String argument
 - String: format operator % like in python
 - String: assign fstream objects to read and write a file
 - String: convert to and from std::sstream and std::string
index 57d1060..9958702 100644 (file)
@@ -219,9 +219,23 @@ Convert the string to all lower-case characters.
 <section>
 <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
-found. Returns -1 if x was not found in the string. see also strstr().
+<item tag="int index(char c)">
+Find the character 'c' within the string. Returns the index where the first occurance of
+<emph>c</emph> was found. Returns -1 if <emph>c</emph> was not found in the string.
+See also index(3).
+</item>
+<item tag="int rindex(char c)">
+Find the character 'c' within the string. Returns the index where the last occurance of
+<emph>c</emph> was found. Returns -1 if <emph>c</emph> was not found in the string.
+See also rindex(3).
+</item>
+<item tag="int index(String &amp;x)">
+Find the first occurance of string x within the string. Returns the index where <emph>x</emph> was
+found. Returns -1 if x was not found in the string. see also strstr(3).
+</item>
+<item tag="int rindex(String &amp;x)">
+Find the last occurance of string x within the string. Returns the index where <emph>x</emph> was
+found. Returns -1 if x was not found in the string. see also strstr(3).
 </item>
 </description>
 </section>
index e264f2f..f352162 100644 (file)
@@ -300,13 +300,28 @@ public:
    String unescape(); //  Remove backslashes from escape codes.
 
    /*
-    *  Character searching and Pattern matching
+    *  Character and String searching
     */
 
    int index(char c);
    int rindex(char c);
 
-   int in(String & x);
+   int index(const char * x);
+   int index(String & x)
+   {
+      return index(x.p->s);
+   }
+
+   int rindex(const char * x);
+   int rindex(String & x)
+   {
+      return rindex(x.p->s);
+   }
+
+   int in(String & x)
+   {
+      return index(x);
+   }
 
    //  Regular expression pattern matching
  
index f1a662d..9f002bb 100644 (file)
@@ -681,19 +681,41 @@ int String::rindex(char c)
       return -1;
 }
 
-/* In: see if I am part of x, return -1 if not found */
+/* Find the first occurance of x */
 
-int String::in(String & x)
+int String::index(const char * x)
 {
    char *match;
 
-   match = strstr(x.p->s, p->s);
+   match = strstr(p->s, x);
    if (match)
-      return match - x.p->s;
+      return match - p->s;
    else
       return -1;
 }
 
+/* Find the last occurance of x */
+
+int String::rindex(const char * x)
+{
+   char *match, *lastmatch;
+
+   match = strstr(p->s, x);
+   lastmatch = 0;
+
+   while (match)
+   {
+      lastmatch = match;
+      match = strstr(match + 1, x);
+   }
+
+   if (lastmatch)
+      return lastmatch - p->s;
+   else
+      return -1;
+}
+
+
 SuperString String::split(const String &separator)
 {
    SuperString splitted;
index 1576eae..dce3f4a 100644 (file)
@@ -3,7 +3,7 @@ TESTS = $(check_PROGRAMS) date_today hour_now check_output
 AM_CPPFLAGS = -I../src
 LDADD = ../src/.libs/libACL.la
 check_PROGRAMS = string_assign string_basics string_compare string_cat string_convert string_modify \
-                 string_shift string_substring string_regex \
+                 string_shift string_substring string_regex string_search \
                  superstring_assign superstring_basics superstring_cat superstring_compare string_split_join \
                  date_assign date_parse date_compare date_arithmetic date_attributes date_check_today \
                  hour_assign hour_parse hour_compare hour_arithmetic hour_check_now \
@@ -21,6 +21,7 @@ string_modify_SOURCES      = string_modify.cpp
 string_shift_SOURCES       = string_shift.cpp
 string_substring_SOURCES   = string_substring.cpp
 string_regex_SOURCES       = string_regex.cpp
+string_search_SOURCES      = string_search.cpp
 
 superstring_assign_SOURCES      = superstring_assign.cpp
 superstring_basics_SOURCES      = superstring_basics.cpp
diff --git a/test/string_search.cpp b/test/string_search.cpp
new file mode 100644 (file)
index 0000000..c7ffad9
--- /dev/null
@@ -0,0 +1,71 @@
+/*******************************************************
+ *  Unit test for the String class
+ *
+ * test search operations
+ ******************************************************
+ *
+ */
+
+#include "String.h"
+#include <assert.h>
+
+int main()
+{
+   int i;
+
+   // Search for characters
+
+   String s1("abcdabcd");
+
+   i = s1.index('c');
+   std::cout << "First occurance of 'c' in " << s1 << " is " << i << "\n";
+   assert(i == 2);
+
+   i = s1.index('f');
+   std::cout << "First occurance of 'f' in " << s1 << " is " << i << "\n";
+   assert(i == -1);
+
+   i = s1.rindex('c');
+   std::cout << "Last occurance of 'c' in " << s1 << " is " << i << "\n";
+   assert(i == 6);
+
+   i = s1.rindex('f');
+   std::cout << "Last occurance of 'f' in " << s1 << " is " << i << "\n";
+   assert(i == -1);
+
+   //   Search for strings
+
+   String s2("abcdef-abcdefghi");
+
+   i = s2.index("def");
+   std::cout << "First occurance of \"def\" in " << s2 << " is " << i << "\n";
+   assert(i == 3);
+
+   i = s2.index("xyz");
+   std::cout << "First occurance of \"xyz\" in " << s2 << " is " << i << "\n";
+   assert(i == -1);
+
+   i = s2.rindex("def");
+   std::cout << "Last occurance of \"def\" in " << s2 << " is " << i << "\n";
+   assert(i == 10);
+
+   i = s2.rindex("xyz");
+   std::cout << "Last occurance of \"xyz\" in " << s2 << " is " << i << "\n";
+   assert(i == -1);
+
+   //  check matches at the start of a string
+
+   String s3("abcdefghi");
+   String s4("ab");
+
+   i = s3.index(s4);
+   std::cout << "First occurance of \"" << s4 << "\" in " << s3 << " is " << i << "\n";
+   assert(i == 0);
+
+   i = s3.rindex(s4);
+   std::cout << "Last occurance of \"" << s4 << "\" in " << s3 << " is " << i << "\n";
+   assert(i == 0);
+
+   return 0;
+}
+
diff --git a/test/string_search.exp b/test/string_search.exp
new file mode 100644 (file)
index 0000000..564db6c
--- /dev/null
@@ -0,0 +1,11 @@
+First occurance of 'c' in abcdabcd is 2
+First occurance of 'f' in abcdabcd is -1
+Last occurance of 'c' in abcdabcd is 6
+Last occurance of 'f' in abcdabcd is -1
+First occurance of "def" in abcdef-abcdefghi is 3
+First occurance of "xyz" in abcdef-abcdefghi is -1
+Last occurance of "def" in abcdef-abcdefghi is 10
+Last occurance of "xyz" in abcdef-abcdefghi is -1
+First occurance of "ab" in abcdefghi is 0
+Last occurance of "ab" in abcdefghi is 0
+PASS string_search (exit status: 0)