Debian Codegraph @

src/shell.rs

raw symbols SCIP
1 // Copyright (c) 2017 Jimmy Cuadra
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20
21 use regexrust-analyzer cargo regex 1.11.1 crate/::Regexrust-analyzer cargo regex 1.11.1 regex/string/Regex#;
22
23 /// Escapes a string so it will be interpreted as a single word by the UNIX Bourne shell.
24 ///
25 /// If the input string is empty, this function returns an empty quoted string.
26 pub fn escaperust-analyzer cargo forensic-adb 0.8.1 shell/escape().(input: &str) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
27 // Stolen from
28 // https://docs.rs/shellwords/1.0.0/src/shellwords/lib.rs.html#24-37.
29 // Added space to the pattern to exclude spaces from being escaped
30 // which can cause problems when combining strings to form a full
31 // command.
32 let escape_pattern: Regexrust-analyzer cargo regex 1.11.1 regex/string/Regex# = Regexrust-analyzer cargo regex 1.11.1 regex/string/Regex#::newrust-analyzer cargo regex 1.11.1 regex/string/impl#[Regex]new().(r"([^A-Za-z0-9_\-.,:/@ \n])").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
33
34 if input.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() {
35 return "''".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().();
36 }
37
38 let output = &escape_pattern.replace_allrust-analyzer cargo regex 1.11.1 regex/string/impl#[Regex]replace_all().(input, "\\$1");
39
40 output.replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().("'\n'", r"\n")
41 }
42
43 #[cfg(test)]
44 mod testsrust-analyzer cargo forensic-adb 0.8.1 shell/tests/ {
45 use superrust-analyzer cargo forensic-adb 0.8.1 shell/::escaperust-analyzer cargo forensic-adb 0.8.1 shell/escape().;
46
47 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
48 fn empty_escaperust-analyzer cargo forensic-adb 0.8.1 shell/tests/empty_escape().() {
49 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(escaperust-analyzer cargo forensic-adb 0.8.1 shell/escape().(""), "''");
50 }
51
52 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
53 fn full_escaperust-analyzer cargo forensic-adb 0.8.1 shell/tests/full_escape().() {
54 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(escaperust-analyzer cargo forensic-adb 0.8.1 shell/escape().("foo '\"' bar"), "foo \\'\\\"\\' bar");
55 }
56
57 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
58 fn escape_multibyterust-analyzer cargo forensic-adb 0.8.1 shell/tests/escape_multibyte().() {
59 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(escaperust-analyzer cargo forensic-adb 0.8.1 shell/escape().("あい"), "\\あ\\い");
60 }
61
62 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
63 fn escape_newlinerust-analyzer cargo forensic-adb 0.8.1 shell/tests/escape_newline().() {
64 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(escaperust-analyzer cargo forensic-adb 0.8.1 shell/escape().(r"'\n'"), "\\\'\\\\n\\\'");
65 }
66 }