-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmpw-shell-quote.rl
64 lines (49 loc) · 1016 Bytes
/
mpw-shell-quote.rl
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
62
63
64
#include <string>
bool must_quote(const std::string &s){
%%{
machine must_quote;
alphtype unsigned char;
quotable = (
[ \t\r\n]
| 0x00
| [0x80-0xff]
| [+#;&|()'"/\\{}`?*<>]
| '-'
| '['
| ']'
);
#simpler just to say what's ok.
normal = [A-Za-z0-9_.:];
main := normal*;
}%%
%%write data;
int cs;
const unsigned char *p = (const unsigned char *)s.data();
const unsigned char *pe = (const unsigned char *)s.data() + s.size();
const unsigned char *eof = nullptr;
%%write init;
%%write exec;
return cs == must_quote_error;
}
#if 0
std::string quote(const std::string &s) {
std::string tmp(s);
return quote(std::move(tmp));
}
#endif
std::string quote(const std::string &s) {
const char q = '\'';
const char *escape_q = "'\xd8''";
if (!must_quote(s)) return s;
std::string out;
out.reserve(s.length() + (s.length() >> 1));
out.push_back(q);
for (char c : s) {
if (c == q) {
out.append(escape_q);
} else
out.push_back(c);
}
out.push_back(q);
return out;
}