015
19.09.2003, 17:26 Uhr
~wissender
Gast
|
Habe auch mal implementiert, ist etwas umfangreicher und größtenteils ungetestet , scheint aber soweit zu funktionieren
C++: |
#include <string> #include <stdexcept> #include <iostream> #include <vector> #include <algorithm> #include <iterator> #include <limits> using namespace std;
class StringTokenizer { public: //Drei Konstuktoren in einem, C++ rockt hier! StringTokenizer(const string str_ = "", const string delim_ = "\t\n\r\f" , bool returnDelims_ = false) throw(); StringTokenizer(const StringTokenizer & ) throw(); StringTokenizer& operator=(const StringTokenizer & ) throw(); bool operator==(const StringTokenizer & ) const throw(); bool operator!=(const StringTokenizer & ) const throw(); friend ostream& operator<<(ostream &, const StringTokenizer & ); unsigned int countTokens() const throw(); bool hasMoreTokens() const throw(); string nextToken() throw (runtime_error); string nextToken(string &newDelimiter) throw (runtime_error); private: string str; string delim; vector<string> tokens; unsigned int pos; bool returnDelims; private: bool isDelim(char) const throw(); void makeTokens() throw(); };
StringTokenizer :: StringTokenizer(const string str_, const string delim_, bool returnDelims_) throw() : str(str_), delim(delim_), tokens(), returnDelims(returnDelims_) { makeTokens(); } StringTokenizer :: StringTokenizer(const StringTokenizer &tokenizer) throw() : str(tokenizer.str), delim(tokenizer.delim), tokens(tokenizer.tokens), pos(tokenizer.pos), returnDelims(tokenizer.returnDelims) {} StringTokenizer& StringTokenizer :: operator=(const StringTokenizer &tokenizer) throw() { if(this != &tokenizer) { str = tokenizer.str; delim = tokenizer.delim; tokens = tokenizer.tokens; pos = tokenizer.pos; returnDelims = tokenizer.returnDelims; } return *this; }
bool StringTokenizer :: operator==(const StringTokenizer &tokenizer) const throw() { return str == tokenizer.str && delim == tokenizer.delim && pos == tokenizer.pos && returnDelims == tokenizer.returnDelims; }
bool StringTokenizer :: operator!=(const StringTokenizer &tokenizer) const throw() { return !(*this == tokenizer); }
ostream& operator<<(ostream &out, const StringTokenizer &tokenizer) { if(!(tokenizer.tokens.begin() + tokenizer.pos >= tokenizer.tokens.end())) copy(tokenizer.tokens.begin() + tokenizer.pos, tokenizer.tokens.end(), ostream_iterator<string>(out, "\n")); return out; }
inline unsigned int StringTokenizer :: countTokens() const throw() { return tokens.size() - pos; }
inline bool StringTokenizer :: hasMoreTokens() const throw() { return pos < tokens.size(); }
string StringTokenizer :: nextToken() throw (runtime_error) { if(pos == tokens.size()) { runtime_error err("No more tokens!"); throw err; } return tokens[pos++]; }
string StringTokenizer :: nextToken(string &newDelimiter) throw (runtime_error) { if(pos == tokens.size()) { runtime_error err("No more tokens!"); throw err; } //Wir müssen die Position im String finden, bei der der aktuelle Token mit //dem alten Delimiter beginnt. unsigned int tokencounter = 0; unsigned int currPosInString = 0; while(currPosInString < str.size() && tokencounter <= pos) { if(isDelim(str[currPosInString])) ++tokencounter; ++currPosInString; } delim = newDelimiter; str = str.substr(currPosInString); makeTokens(); return tokens[pos++]; }
bool StringTokenizer :: isDelim(char possDelim) const throw() { for(unsigned int i = 0; i < delim.size(); ++i) { if(possDelim == delim[i]) return true; } return false; }
void StringTokenizer :: makeTokens() throw() { //Aufräumen tokens.clear(); pos = 0; string token; for(unsigned int i = 0; i < str.size(); ) { while(isDelim(str[i]) && i < str.size()) { if(returnDelims) { token = str[i]; tokens.push_back(token); } ++i; } token = ""; while(!isDelim(str[i]) && i < str.size()) token += str[i++]; tokens.push_back(token); } }
int main() { StringTokenizer tok("hAxnhnheh-hKhazthrhihn", "hxz"); cout << tok; while(tok.hasMoreTokens()) cout << tok.nextToken(); return 0; }
|
--edit: Pablo. smilies entfernt -- Dieser Post wurde am 19.09.2003 um 17:35 Uhr von Pablo Yanez Trujillo editiert. |