29
Util::split(const string& src, char delim, vector<string>& elementVec, bool fuzzy)
37
// Simple case: we want to enforce the value of 'delim' strictly
40
std::stringstream ss(src);
42
while(std::getline(ss, item, delim))
43
elementVec.push_back(item);
29
* State machine for bash-like quoted string escaping:
32
* -----------> +---------+
33
* | ---------- | Escaped |
34
* | | *,ESC +---------+
37
* +--------+ ---> +--------------+ -----
38
* | Normal | <--- | SingleQuoted | | *, ESC
39
* +--------+ ' +--------------+ <----
42
* | | " +--------------+ ----
43
* | ---------- | DoubleQuoted | | *, ESC
44
* -----------> +--------------+ <---
48
* +---------------------+
49
* | DoubleQuotedEscaped |
50
* +---------------------+
52
* ESC: Mark character as Escaped
55
fill_escape_vector(const string &str, vector<bool> &esc_vec)
61
StateDoubleQuotedEscaped,
65
State state = StateNormal;
67
for (string::const_iterator iter = str.begin();
77
state = StateDoubleQuoted;
81
state = StateSingleQuoted;
87
case StateDoubleQuoted:
91
state = StateDoubleQuotedEscaped;
95
case StateDoubleQuotedEscaped:
97
state = StateDoubleQuoted;
99
case StateSingleQuoted:
108
esc_vec.push_back(esc);
113
split_normal(const string& src, char delim, vector<string>& elementVec)
115
std::stringstream ss(src);
117
while(std::getline(ss, item, delim))
118
elementVec.push_back(item);
122
split_fuzzy(const string& src, char delim, vector<string>& elementVec)
47
124
// Fuzzy case: Initialize our delimiter string based upon the caller's plus
48
125
// a space to allow for more flexibility.
49
126
string delimiter(" ");
76
153
elementVec.push_back(str);
157
split_quoted(const string& src, char delim, vector<string>& elementVec)
159
std::stringstream ss;
162
/* Mark characters in the string as escaped or not */
163
fill_escape_vector(src, escVec);
165
/* Sanity check... */
166
if (src.length() != escVec.size())
169
for (vector<bool>::const_iterator iter = escVec.begin();
170
iter != escVec.end();
173
bool escaped = static_cast<bool>(*iter);
174
char c = src[iter - escVec.begin()];
176
/* Output all characters, except unescaped ",\,' */
177
if ((c != '"' && c != '\\' && c != '\'') || escaped) {
178
/* If we reach an unescaped delimiter character, do a split */
179
if (c == delim && !escaped) {
180
elementVec.push_back(ss.str());
191
/* Handle final element, delimited by end of string */
192
const string &finalElement(ss.str());
193
if (!finalElement.empty())
194
elementVec.push_back(finalElement);
198
Util::split(const string& src, char delim, vector<string>& elementVec,
199
Util::SplitMode mode)
209
case Util::SplitModeNormal:
210
return split_normal(src, delim, elementVec);
211
case Util::SplitModeFuzzy:
212
return split_fuzzy(src, delim, elementVec);
213
case Util::SplitModeQuoted:
214
return split_quoted(src, delim, elementVec);
80
221
Util::get_timestamp_us()