Skip to content

Commit 3fa01c2

Browse files
Apply more suggestions. Add a test for non-utf8 chars for escape.
1 parent 2279c34 commit 3fa01c2

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/escape.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ pub fn escape(s: &OsStr) -> Cow<'_, OsStr> {
3232
let all_whitelisted = s.iter().copied().all(whitelisted);
3333

3434
if !s.is_empty() && all_whitelisted {
35-
return OsString::from_vec(s.to_vec()).into();
35+
return Cow::Borrowed(s);
3636
}
3737

3838
let mut escaped = Vec::with_capacity(s.len() + 2);
39+
escaped.reserve(4);
3940
escaped.push(b'\'');
4041

4142
for &b in s {
@@ -65,6 +66,13 @@ mod tests {
6566
assert_eq!(observed_os_str, expected_os_str);
6667
}
6768

69+
fn test_escape_from_bytes(input: &[u8], expected: &[u8]) {
70+
let input_os_str = OsStr::from_bytes(input);
71+
let observed_os_str = escape(input_os_str);
72+
let expected_os_str = OsStr::from_bytes(expected);
73+
assert_eq!(observed_os_str, expected_os_str);
74+
}
75+
6876
// These tests are courtesy of the `shell-escape` crate.
6977
#[test]
7078
fn test_escape() {
@@ -96,5 +104,10 @@ mod tests {
96104
" ",
97105
r#"' '"#
98106
);
107+
108+
test_escape_from_bytes(
109+
&[0x66, 0x6f, 0x80, 0x6f],
110+
&[b'\'', 0x66, 0x6f, 0x80, 0x6f, b'\'']
111+
);
99112
}
100113
}

0 commit comments

Comments
 (0)