Last change
on this file since 884 was
790,
checked in by nicolasc, 17 years ago
|
merged FICN back into trunk
awaiting release.
|
File size:
1.1 KB
|
Line | |
---|
1 | #ifndef _Tokenizer_H__ |
---|
2 | #define _Tokenizer_H__ |
---|
3 | |
---|
4 | #include <string> |
---|
5 | #include <iostream> |
---|
6 | #include <vector> |
---|
7 | |
---|
8 | /** |
---|
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 | */ |
---|
17 | |
---|
18 | inline std::vector<std::string> tokenize(const std::string& str, const std::string& delimiters = ",") |
---|
19 | { |
---|
20 | std::vector<std::string> tokens; |
---|
21 | |
---|
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); |
---|
35 | } |
---|
36 | return tokens; |
---|
37 | } |
---|
38 | |
---|
39 | |
---|
40 | #endif /* _Tokenizer_H__ */ |
---|
Note: See
TracBrowser
for help on using the repository browser.