From 0955b7993035008471f080b77894ab1feab5aeab Mon Sep 17 00:00:00 2001 From: Arjen Baart Date: Sat, 20 Mar 2021 09:49:49 +0100 Subject: [PATCH] Documentation and test for String::index() and String::rindex() --- TODO | 1 - doc/string.xml | 20 ++++++++++-- src/String.h | 19 +++++++++-- src/string.cpp | 30 +++++++++++++++--- test/Makefile.am | 3 +- test/string_search.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++ test/string_search.exp | 11 +++++++ 7 files changed, 144 insertions(+), 11 deletions(-) create mode 100644 test/string_search.cpp create mode 100644 test/string_search.exp diff --git a/TODO b/TODO index 52cb9be..168a1d7 100644 --- 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 diff --git a/doc/string.xml b/doc/string.xml index 57d1060..9958702 100644 --- a/doc/string.xml +++ b/doc/string.xml @@ -219,9 +219,23 @@ Convert the string to all lower-case characters.
Searching in Strings - -Find the string x within the string. Returns the index where x was -found. Returns -1 if x was not found in the string. see also strstr(). + +Find the character 'c' within the string. Returns the index where the first occurance of +c was found. Returns -1 if c was not found in the string. +See also index(3). + + +Find the character 'c' within the string. Returns the index where the last occurance of +c was found. Returns -1 if c was not found in the string. +See also rindex(3). + + +Find the first occurance of string x within the string. Returns the index where x was +found. Returns -1 if x was not found in the string. see also strstr(3). + + +Find the last occurance of string x within the string. Returns the index where x was +found. Returns -1 if x was not found in the string. see also strstr(3).
diff --git a/src/String.h b/src/String.h index e264f2f..f352162 100644 --- a/src/String.h +++ b/src/String.h @@ -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 diff --git a/src/string.cpp b/src/string.cpp index f1a662d..9f002bb 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -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; diff --git a/test/Makefile.am b/test/Makefile.am index 1576eae..dce3f4a 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -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 index 0000000..c7ffad9 --- /dev/null +++ b/test/string_search.cpp @@ -0,0 +1,71 @@ +/******************************************************* + * Unit test for the String class + * + * test search operations + ****************************************************** + * + */ + +#include "String.h" +#include + +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 index 0000000..564db6c --- /dev/null +++ b/test/string_search.exp @@ -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) -- 2.20.1