1
1
//! Escape characters that may have special meaning in a shell, including spaces.
2
2
//! This is a modified version of the [`shell-escape::unix`] module of [`shell-escape`] crate.
3
- //!
3
+ //!
4
4
//! [`shell-escape`]: https://crates.io/crates/shell-escape
5
5
//! [`shell-escape::unix`]: https://docs.rs/shell-escape/latest/src/shell_escape/lib.rs.html#101
6
6
7
7
use std:: {
8
- borrow:: Cow ,
8
+ borrow:: Cow ,
9
9
ffi:: { OsStr , OsString } ,
10
10
os:: unix:: ffi:: OsStrExt ,
11
11
os:: unix:: ffi:: OsStringExt ,
12
12
} ;
13
13
14
-
15
14
fn whitelisted ( byte : u8 ) -> bool {
16
15
matches ! ( byte, b'a' ..=b'z' | b'A' ..=b'Z' | b'0' ..=b'9' | b'-' | b'_' | b'=' | b'/' | b',' | b'.' | b'+' )
17
16
}
18
17
19
18
/// Escape characters that may have special meaning in a shell, including spaces.
20
- ///
19
+ ///
21
20
/// **Note**: This function is an adaptation of [`shell-escape::unix::escape`].
22
21
/// This function exists only for type compatibility and the implementation is
23
22
/// almost exactly the same as [`shell-escape::unix::escape`].
24
- ///
23
+ ///
25
24
/// [`shell-escape::unix::escape`]: https://docs.rs/shell-escape/latest/src/shell_escape/lib.rs.html#101
26
- ///
25
+ ///
27
26
pub ( crate ) fn escape ( s : & OsStr ) -> Cow < ' _ , OsStr > {
28
27
let as_bytes = s. as_bytes ( ) ;
29
28
let all_whitelisted = as_bytes. iter ( ) . copied ( ) . all ( whitelisted) ;
@@ -51,7 +50,6 @@ pub(crate) fn escape(s: &OsStr) -> Cow<'_, OsStr> {
51
50
OsString :: from_vec ( escaped) . into ( )
52
51
}
53
52
54
-
55
53
#[ cfg( test) ]
56
54
mod tests {
57
55
use super :: * ;
@@ -75,36 +73,21 @@ mod tests {
75
73
fn test_escape ( ) {
76
74
test_escape_case (
77
75
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_=/,.+" ,
78
- "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_=/,.+"
79
- ) ;
80
- test_escape_case (
81
- "--aaa=bbb-ccc" ,
82
- "--aaa=bbb-ccc"
76
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_=/,.+" ,
83
77
) ;
78
+ test_escape_case ( "--aaa=bbb-ccc" , "--aaa=bbb-ccc" ) ;
84
79
test_escape_case (
85
80
"linker=gcc -L/foo -Wl,bar" ,
86
- r#"'linker=gcc -L/foo -Wl,bar'"#
87
- ) ;
88
- test_escape_case (
89
- r#"--features="default""# ,
90
- r#"'--features="default"'"#
91
- ) ;
92
- test_escape_case (
93
- r#"'!\$`\\\n "# ,
94
- r#"''\'''\!'\$`\\\n '"#
95
- ) ;
96
- test_escape_case (
97
- "" ,
98
- r#"''"#
99
- ) ;
100
- test_escape_case (
101
- " " ,
102
- r#"' '"#
81
+ r#"'linker=gcc -L/foo -Wl,bar'"# ,
103
82
) ;
83
+ test_escape_case ( r#"--features="default""# , r#"'--features="default"'"# ) ;
84
+ test_escape_case ( r#"'!\$`\\\n "# , r#"''\'''\!'\$`\\\n '"# ) ;
85
+ test_escape_case ( "" , r#"''"# ) ;
86
+ test_escape_case ( " " , r#"' '"# ) ;
104
87
105
88
test_escape_from_bytes (
106
89
& [ 0x66 , 0x6f , 0x80 , 0x6f ] ,
107
- & [ b'\'' , 0x66 , 0x6f , 0x80 , 0x6f , b'\'' ]
90
+ & [ b'\'' , 0x66 , 0x6f , 0x80 , 0x6f , b'\'' ] ,
108
91
) ;
109
92
}
110
93
}
0 commit comments