-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib.rs
84 lines (72 loc) · 2.33 KB
/
lib.rs
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*!
# Execute Command Tokens
Parse command strings.
See [`execute`](https://crates.io/crates/execute).
*/
/// Parse a command string.
pub fn command_tokens<S: AsRef<str>>(cmd: S) -> Vec<String> {
let cmd = cmd.as_ref();
let mut tokens = Vec::with_capacity(1);
let mut string_buffer = String::new();
let mut append_mode = false;
let mut quote_mode = false;
let mut quote_mode_ending = false; // to deal with '123''456' -> 123456
let mut quote_char = ' ';
let mut escaping = false;
for c in cmd.chars() {
if escaping {
append_mode = true;
escaping = false;
string_buffer.push(c);
} else if c.is_whitespace() {
if append_mode {
if quote_mode {
string_buffer.push(c);
} else {
append_mode = false;
tokens.push(string_buffer);
string_buffer = String::new();
}
} else if quote_mode_ending {
quote_mode_ending = false;
tokens.push(string_buffer);
string_buffer = String::new();
}
} else {
match c {
'"' | '\'' => {
if append_mode {
if quote_mode {
if quote_char == c {
append_mode = false;
quote_mode = false;
quote_mode_ending = true;
} else {
string_buffer.push(c);
}
} else {
quote_mode = true;
quote_char = c;
}
} else {
append_mode = true;
quote_mode = true;
quote_char = c;
}
},
'\\' => {
escaping = true;
},
_ => {
append_mode = true;
escaping = false;
string_buffer.push(c);
},
}
}
}
if append_mode || quote_mode_ending {
tokens.push(string_buffer);
}
tokens
}