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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
/// A trait for splitting strings into arguments, respecting quotes and escapes.
///
/// # Examples
///
/// ```
/// use mingling_core::utils::ArgumentSplitter;
///
/// let args = "echo \"hello world\"".split_args();
/// assert_eq!(args, vec!["echo", "hello world"]);
/// ```
pub trait ArgumentSplitter {
/// Splits the input string into a vector of argument strings.
///
/// # Examples
///
/// ```
/// use mingling_core::utils::ArgumentSplitter;
///
/// let args = "a 'b c' d".split_args();
/// assert_eq!(args, vec!["a", "b c", "d"]);
/// ```
fn split_args(self) -> Vec<String>;
}
impl<S: AsRef<str>> ArgumentSplitter for S {
/// Splits the string into arguments, respecting single quotes, double
/// quotes, and backslash escaping.
///
/// # Examples
///
/// ```
/// use mingling_core::utils::ArgumentSplitter;
///
/// let args = r#"cmd --flag "value with spaces""#.split_args();
/// assert_eq!(args, vec!["cmd", "--flag", "value with spaces"]);
/// ```
///
/// Escaped characters are unescaped:
///
/// ```
/// use mingling_core::utils::ArgumentSplitter;
///
/// let args = r#"echo a\ b"#.split_args();
/// assert_eq!(args, vec!["echo", "a b"]);
/// ```
fn split_args(self) -> Vec<String> {
split_args(self.as_ref())
}
}
/// Splits a string input into arguments, respecting single quotes, double quotes,
/// and backslash escaping.
fn split_args(input: &str) -> Vec<String> {
let mut result: Vec<String> = Vec::new();
let mut current = String::new();
let mut chars = input.chars();
while let Some(ch) = chars.next() {
match ch {
'\\' => {
// Take the next character literally (if any) and add it to current.
if let Some(next) = chars.next() {
current.push(next);
}
// If there's no next character, the backslash is just ignored/lost.
}
'"' | '\'' => {
// Start of a quoted segment.
let quote_char = ch;
let mut escaped = false;
loop {
match chars.next() {
None => break,
Some(c) => {
if escaped {
current.push(c);
escaped = false;
} else if c == '\\' {
escaped = true;
} else if c == quote_char {
break;
} else {
current.push(c);
}
}
}
}
}
' ' => {
if !current.is_empty() {
result.push(current.clone());
current.clear();
}
}
_ => {
current.push(ch);
}
}
}
if !current.is_empty() {
result.push(current);
}
result
}
#[cfg(test)]
mod splitter_tests {
use crate::utils::splitter::split_args;
#[test]
fn test_split_with_double_quotes() {
let input = r#"a "b c" d"#;
let result = split_args(input);
assert_eq!(result, vec!["a", "b c", "d"]);
}
#[test]
fn test_split_with_single_quotes() {
let input = "a 'b c' d";
let result = split_args(input);
assert_eq!(result, vec!["a", "b c", "d"]);
}
#[test]
fn test_empty_input() {
assert!(split_args("").is_empty());
}
#[test]
fn test_no_quotes() {
let result = split_args("hello world");
assert_eq!(result, vec!["hello", "world"]);
}
#[test]
fn test_double_quotes_at_edges() {
let result = split_args(r#""hello world" foo"#);
assert_eq!(result, vec!["hello world", "foo"]);
}
#[test]
fn test_single_quotes_at_edges() {
let result = split_args("'hello world' foo");
assert_eq!(result, vec!["hello world", "foo"]);
}
#[test]
fn test_multiple_double_quoted_parts() {
let result = split_args(r#"a "b c" d "e f g""#);
assert_eq!(result, vec!["a", "b c", "d", "e f g"]);
}
#[test]
fn test_multiple_single_quoted_parts() {
let result = split_args("a 'b c' d 'e f g'");
assert_eq!(result, vec!["a", "b c", "d", "e f g"]);
}
#[test]
fn test_backslash_escaped_space() {
let result = split_args("a b\\ c d");
assert_eq!(result, vec!["a", "b c", "d"]);
}
#[test]
fn test_backslash_escaped_double_quote() {
let result = split_args(r#"a b\"c d"#);
assert_eq!(result, vec!["a", r#"b"c"#, "d"]);
}
#[test]
fn test_backslash_escaped_single_quote() {
let result = split_args("a b\\'c d");
assert_eq!(result, vec!["a", "b'c", "d"]);
}
}
|