# include <String.h> ... typedef int (*StringSearchFunc)(const String &s, int &matchlen) const; ... class StringSearch { public: StringSearch(); StringSearch(const String &s); StringSearch(StringSearchFunc func); virtual int search(const String &s, int &matchlen) const; private: StringSearchFunc ssf; String str; int str_flag; }; ...
Declaring a new class derived from StringSearch is useful if you need
all the functionality that comes along with a new class, such as
constructors, memeber functions, and private class data. If all you
want to do is have a function get called that performs the search,
then you can simply declare a StringSearch object, and pass it the
address of your search function as a argument to the StringSearch
constructor. Your function
should be of type StringSearchFunc. It should search the String and return the
position within the String a match was made, and set matchlen to the length
of the match. If no match is made, then it should set matchlen to 0 and
return a -1.
The last way is to simply declare a StringSearch object and pass it a String.
The StringSearch object will then search for this String whenever it is called.
Note this class has some very useful applications. For example, you could make a symbol table class which is derived from the StringSearch class. Now, all you have to do is write the search function in your symbol table class, and you'll be able to pass a symbol table to all the String functions, which will then call your search routine to see if a symbol exists in the String.
class SSwhitespace : public StringSearch {
public:
SSwhitespace() {}
int search(const String &s, int &matchlen) const ;
};
int SSwhitespace::search(const String &s, int &len) const { len=0; int p1; StringIterator next(s); char ch;
while (next(ch))
if (isspace(ch)) {
p1=next.pos();
while (next(ch) && isspace(ch));
len=next.pos()-p1;
return p1;
}
return -1;
}
SSwhitespace SSwhite;
String s1("This is a test");
s1.at(SSwhite)=" ";
// s1 now equals "This is a test"
Note how the class re-defines the virtual search function. Also note that
an object from the class is declared (SSwhite), and passed to the String
function 'at'. Since the 'at' function is declared to take a StringSearch
object as a parameter, the derived class SSwhitespace object is passed
as a StringSearch object. Now the 'at' funtion will correctly call the
SSwhitespace::search function, and return the SubString within the String
that contains whitespace.
The following example shows how to create a function that searchs
a String for a integer value.
int SearchInt(const String &s, int &len) const
{
len=0;
int pos=0,p1;
StringIterator next(s);
char ch;
while (next(ch)) if (isdigit(ch) || ch=='-') {
p1=next.pos();
while (next(ch) && isdigit(ch));
len=next.pos()-p1;
return p1;
}
return -1;
}
const StringSearch Sint(SearchInt); String s1("this is 1234"); s1.at(Sint)="one two three four"; // s1 now equals "this is one two three four"
Note that is a very simple example. StringSearch functions could be
designed that use hash tables, binary trees, call yylex functions, etc.