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
<section>
<heading>Searching in Strings</heading>
<description>
-<item tag="int in(String &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 &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 &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>
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
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;
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 \
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
--- /dev/null
+/*******************************************************
+ * 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;
+}
+
--- /dev/null
+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)