1 |
#ifdef HAVE_CONFIG_H |
2 |
#include <config.h> |
3 |
#endif |
4 |
|
5 |
#include <string> |
6 |
#include <vector> |
7 |
#include <cassert> |
8 |
|
9 |
using namespace std; |
10 |
|
11 |
/** |
12 |
* Split string s according to given delimiters. Mostly borrowed |
13 |
* from C++ Programming HOWTO 7.3. |
14 |
*/ |
15 |
void tokenize(const string& s, const string& delims, vector<string>& ss) { |
16 |
string::size_type lastPos = s.find_first_not_of(delims, 0); |
17 |
string::size_type pos = s.find_first_of(delims, lastPos); |
18 |
while (string::npos != pos || string::npos != lastPos) { |
19 |
ss.push_back(s.substr(lastPos, pos - lastPos)); |
20 |
lastPos = s.find_first_not_of(delims, pos); |
21 |
pos = s.find_first_of(delims, lastPos); |
22 |
} |
23 |
} |
24 |
|
25 |
/** |
26 |
* Split string s according to given delimiters. If two delimiters occur in |
27 |
* succession, sticks an empty string token at that position in ss |
28 |
*/ |
29 |
void tokenize_strict(const string& s, const string& delims, vector<string>& ss) { |
30 |
string::size_type lastPos = s.find_first_not_of(delims, 0); |
31 |
string::size_type pos = s.find_first_of(delims, lastPos); |
32 |
while (lastPos < s.length() || pos < s.length()) { |
33 |
ss.push_back(s.substr(lastPos, pos - lastPos)); |
34 |
if (pos == string::npos) |
35 |
break; |
36 |
lastPos = pos + 1; |
37 |
pos = s.find_first_of(delims, lastPos); |
38 |
} |
39 |
} |