Introduction
The class CTokenizer
I am presenting in this article is capable of tokenizing an STL string
when the set of character separators is specified by a predicate class. This is a very generally designed
as a class template:
template <class Pred>
void CTokenizer { };
The separating (tokenizing) criteria being implemented in the argument predicate class Pred
.
The predicate classes are usually derived from unary_function<char, bool>
and implement the () operator. I am
giving only three examples of predicate classes: CIsSpace
where the set of separators contains the white spaces
0x09-0x0D and 0x20, CIsComma
where the separator is the comma character ',' and CIsFromString
where the
set of separators is specified by the characters in a STL string. Other predicate classes can be easily added as needed.
Implementation
First I will present the implemented predicates.
For the case when the separators are white spaces 0x09-0x0D and 0x20;
class CIsSpace : public unary_function<char, bool>
{
public:
bool operator()(char c) const;
};
inline bool CIsSpace::operator()(char c) const
{
return isspace<char>(c);
}
For the case where the separator is the comma character ',':
class CIsComma : public unary_function<char, bool>
{
public:
bool operator()(char c) const;
};
inline bool CIsComma::operator()(char c) const
{
return (',' == c);
}
For the case where the separator is a character from a set of characters given in a STL string:
class CIsFromString : public unary_function<char, bool>
{
public:
CIsFromString::CIsFromString(string const& rostr) : m_ostr(rostr) {}
bool operator()(char c) const;
private:
string m_ostr;
};
inline bool CIsFromString::operator()(char c) const
{
int iFind = m_ostr.find(c);
if(iFind != string::npos)
return true;
else
return false;
}
Finally the string tokenizer class implementing the Tokenize()
function is a static member function.
Notice that CIsSpace
is the default predicate for the Tokenize()
function.
template <class Pred=CIsSpace>
class CTokenizer
{
public:
static void Tokenize(vector<string>& roResult, string const& rostr,
Pred const& roPred=Pred());
};
template <class Pred>
inline void CTokenizer<Pred>::Tokenize(vector<string>& roResult,
string const& rostr, Pred const& roPred)
{
roResult.clear();
string::const_iterator it = rostr.begin();
string::const_iterator itTokenEnd = rostr.begin();
while(it != rostr.end())
{
while(roPred(*it))
it++;
itTokenEnd = find_if(it, rostr.end(), roPred);
if(it < itTokenEnd)
roResult.push_back(string(it, itTokenEnd));
it = itTokenEnd;
}
}
How to use
The following code snippet is showing some simple usage examples, one for each one of the implemented predicates:
cout << "Test CIsSpace() predicate:" << endl;
vector<string> oResult;
CTokenizer<>::Tokenize(oResult, " wqd \t hgwh \t sdhw \r\n kwqo \r\n dk ");
for(int i=0; i<oResult.size(); i++)
cout << oResult[i] << endl;
cout << "Test CIsComma() predicate:" << endl;
vector<string> oResult;
CTokenizer<CIsComma>::Tokenize(oResult, "wqd,hgwh,sdhw,kwqo,dk", CIsComma());
for(int i=0; i<oResult.size(); i++)
cout << oResult[i] << endl;
cout << "Test CIsFromString() predicate:" << endl;
vector<string> oResult;
CTokenizer<CIsFromString>::Tokenize(oResult, ":wqd,;hgwh,:,sdhw,:;kwqo;dk,",
CIsFromString(",;:"));
cout << "Display strings:" << endl;
for(int i=0; i<oResult.size(); i++)
cout << oResult[i] << endl;
Conclusion
The project StringTok.zip attached to this article includes the source code of the
presented CTokenizer
class and some test code. I am interested in any opinions and new
ideas about this implementation.