Last change
on this file since 842 was
790,
checked in by nicolasc, 17 years ago
|
merged FICN back into trunk
awaiting release.
|
File size:
1.1 KB
|
Rev | Line | |
---|
[676] | 1 | #ifndef _Tokenizer_H__ |
---|
| 2 | #define _Tokenizer_H__ |
---|
[507] | 3 | |
---|
| 4 | #include <string> |
---|
| 5 | #include <iostream> |
---|
[682] | 6 | #include <vector> |
---|
[507] | 7 | |
---|
| 8 | /** |
---|
[659] | 9 | * String tokenizer |
---|
| 10 | * |
---|
| 11 | * Splits a given string into several smaller strings |
---|
| 12 | * using a delmiter (default is the comma). |
---|
| 13 | * Returns the result as a vector<string> object |
---|
| 14 | * |
---|
| 15 | * @author Nicolas Perrenoud<nicolape@ee.ethz.ch> |
---|
| 16 | */ |
---|
[507] | 17 | |
---|
| 18 | inline std::vector<std::string> tokenize(const std::string& str, const std::string& delimiters = ",") |
---|
| 19 | { |
---|
[612] | 20 | std::vector<std::string> tokens; |
---|
[592] | 21 | |
---|
[507] | 22 | // Skip delimiters at beginning. |
---|
| 23 | std::string::size_type lastPos = str.find_first_not_of(delimiters, 0); |
---|
| 24 | // Find first "non-delimiter". |
---|
| 25 | std::string::size_type pos = str.find_first_of(delimiters, lastPos); |
---|
| 26 | |
---|
| 27 | while (std::string::npos != pos || std::string::npos != lastPos) |
---|
| 28 | { |
---|
| 29 | // Found a token, add it to the vector. |
---|
| 30 | tokens.push_back(str.substr(lastPos, pos - lastPos)); |
---|
| 31 | // Skip delimiters. Note the "not_of" |
---|
| 32 | lastPos = str.find_first_not_of(delimiters, pos); |
---|
| 33 | // Find next "non-delimiter" |
---|
| 34 | pos = str.find_first_of(delimiters, lastPos); |
---|
[592] | 35 | } |
---|
| 36 | return tokens; |
---|
[507] | 37 | } |
---|
| 38 | |
---|
| 39 | |
---|
[676] | 40 | #endif /* _Tokenizer_H__ */ |
---|
Note: See
TracBrowser
for help on using the repository browser.