Repository navigation
Expand file tree
/
Copy pathshlex.rs
More file actions
129 lines (124 loc) · 4.5 KB
/
Copy pathshlex.rs
File metadata and controls
129 lines (124 loc) · 4.5 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//! Port of Python's `shlex.split(s)` (POSIX mode, no comments).
/// Split like `shlex.split`. Errors carry CPython's messages.
pub fn split(input: &str) -> Result<Vec<String>, String> {
const WHITESPACE: &[char] = &[' ', '\t', '\r', '\n'];
let chars: Vec<char> = input.chars().collect();
let mut pos = 0;
let mut tokens = Vec::new();
// `state` mirrors shlex: Some(' '), Some('a'), Some(quote), Some('\\'), None (EOF).
let mut state: Option<char> = Some(' ');
loop {
let mut token = String::new();
let mut quoted = false;
let mut escaped_state = ' ';
loop {
let next = chars.get(pos).copied();
pos += 1;
match state {
None => {
token.clear();
break;
}
Some(' ') => match next {
None => {
state = None;
break;
}
Some(c) if WHITESPACE.contains(&c) => {
if !token.is_empty() || quoted {
break;
}
}
Some('\\') => {
escaped_state = 'a';
state = Some('\\');
}
Some(c) if c == '\'' || c == '"' => state = Some(c),
Some(c) => {
token.push(c);
state = Some('a');
}
},
Some(q) if q == '\'' || q == '"' => {
quoted = true;
match next {
None => return Err("No closing quotation".to_string()),
Some(c) if c == q => state = Some('a'),
Some('\\') if q == '"' => {
escaped_state = q;
state = Some('\\');
}
Some(c) => token.push(c),
}
}
Some('\\') => {
let Some(c) = next else {
return Err("No escaped character".to_string());
};
if (escaped_state == '\'' || escaped_state == '"')
&& c != '\\'
&& c != escaped_state
{
token.push('\\');
}
token.push(c);
state = Some(escaped_state);
}
Some(_) => match next {
// state 'a'
None => {
state = None;
break;
}
Some(c) if WHITESPACE.contains(&c) => {
state = Some(' ');
if !token.is_empty() || quoted {
break;
}
}
Some(c) if c == '\'' || c == '"' => state = Some(c),
Some('\\') => {
escaped_state = 'a';
state = Some('\\');
}
Some(c) => token.push(c),
},
}
}
if !quoted && token.is_empty() {
if state.is_none() {
return Ok(tokens);
}
continue;
}
tokens.push(token);
}
}
#[cfg(test)]
mod tests {
use super::split;
fn s(v: &[&str]) -> Vec<String> {
v.iter().map(|x| x.to_string()).collect()
}
#[test]
fn splits_like_python() {
assert_eq!(
split("version --client --short").unwrap(),
s(&["version", "--client", "--short"])
);
assert_eq!(split(" -V ").unwrap(), s(&["-V"]));
assert_eq!(split("a 'b c' \"d e\"").unwrap(), s(&["a", "b c", "d e"]));
assert_eq!(split("x ''").unwrap(), s(&["x", ""]));
assert_eq!(split("a\\ b").unwrap(), s(&["a b"]));
assert_eq!(split("\"a\\\"b\\c\"").unwrap(), s(&["a\"b\\c"]));
assert_eq!(split("'a\\b'").unwrap(), s(&["a\\b"]));
assert_eq!(split("a'b'c").unwrap(), s(&["abc"]));
assert!(split("").unwrap().is_empty());
assert!(split(" ").unwrap().is_empty());
}
#[test]
fn errors_like_python() {
assert_eq!(split("'abc").unwrap_err(), "No closing quotation");
assert_eq!(split("abc\\").unwrap_err(), "No escaped character");
}
}