-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer.cpp
More file actions
61 lines (56 loc) · 1.29 KB
/
Copy pathtokenizer.cpp
File metadata and controls
61 lines (56 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <cctype>
#include <iostream>
#include <vector>
#include <sstream>
std::vector<std::string> tokenize(const std::string &line) {
enum State { NORMAL, IN_SINGLE, IN_DOUBLE } state = NORMAL;
std::vector<std::string> tokens;
std::string buf;
for (size_t i = 0; i < line.size(); ++i) {
char c = line[i];
switch (state) {
case NORMAL:
if (c == '\'') {
state = IN_SINGLE;
} else if (c == '"') {
state = IN_DOUBLE;
} else if (c == '|' || c == ';' || c == '<' || c == '>') {
if (!buf.empty()) {
tokens.push_back(buf);
buf.clear();
}
if (c == '>' && i + 1 < line.size() && line[i + 1] == '>') {
tokens.push_back(">>");
i++;
} else {
tokens.push_back(std::string(1, c));
}
} else if (std::isspace(c)) {
if (!buf.empty()) {
tokens.push_back(buf);
buf.clear();
}
} else {
buf += c;
}
break;
case IN_SINGLE:
if (c == '\'') {
state = NORMAL;
} else {
buf += c;
}
break;
case IN_DOUBLE:
if (c == '"') {
state = NORMAL;
} else {
buf += c;
}
break;
}
}
if (!buf.empty())
tokens.push_back(buf);
return tokens;
}