Skip to content
Myr

Fix --exec

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by J.W · committed by David Peter · parent 87ba4e3 · Browse files · 4 files+47 added−91 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+47 added−91 removed

  • other:+47 added−91 removed

4 files+47 added−91 removed

modified:src/exec/input.rs+38 added-91 removed
7 unmodified lines
89use std::path::MAIN_SEPARATOR;10use std::borrow::Cow;1112#[cfg(windows)]13const ESCAPE: char = '^';12use shell_escape::escape;1315#[cfg(not(windows))]16const ESCAPE: char = '\\';1714/// A builder for efficiently generating input strings.15///16/// After choosing your required specs, the `get()` method will escape special characters found17/// in the input. Allocations will only occur if special characters are found that need to be
65 unmodified lines
83 self84 }8586 pub fn get(&'a self) -> Cow<'a, str> {91 fn char_is_quotable(x: char) -> bool {92 [93 ' ',94 '(',95 ')',96 '[',97 ']',98 '&',99 '$',100 '@',101 '{',102 '}',103 '<',104 '>',105 '|',106 ';',107 '"',108 '\'',109 '#',110 '*',111 '%',112 '?',113 '`',114 ].contains(&x)115 };87 escape(Cow::Borrowed(self.data))88 }89117 // If a quotable character is found, we will use that position for allocating.118 let pos = match self.data.find(char_is_quotable) {119 Some(pos) => pos,120 // Otherwise, we will return the contents of `data` without allocating.121 None => return Cow::Borrowed(self.data),122 };123124 // When building the input string, we will start by adding the characters that125 // we've already verified to be free of special characters.126 let mut owned = String::with_capacity(self.data.len());127 owned.push_str(&self.data[..pos]);128 owned.push(ESCAPE);129130 // This slice contains the data that is left to be scanned for special characters.131 // If multiple characters are found, this slice will be sliced and updated multiple times.132 let mut slice = &self.data[pos..];133134 // Repeatedly search for special characters until all special characters have been found,135 // appending and inserting the escape character each time, as well as updating our136 // starting position.137 while let Some(pos) = slice[1..].find(char_is_quotable) {138 owned.push_str(&slice[..pos + 1]);139 owned.push(ESCAPE);140 slice = &slice[pos + 1..];141 }142143 // Finally, we return our newly-allocated input string.144 owned.push_str(slice);145 Cow::Owned(owned)90 #[cfg(test)]91 fn get_private(&'a self) -> Cow<'a, str> {92 Cow::Borrowed(self.data)93 }94}9596#[cfg(test)]
7 unmodified lines
104 }105106 #[test]107 fn path_remove_ext_simple() {161 assert_eq!(&Input::new("foo.txt").remove_extension().get(), "foo");108 assert_eq!(109 &Input::new("foo.txt").remove_extension().get_private(),110 "foo"111 );112 }113114 #[test]115 fn path_remove_ext_dir() {116 assert_eq!(167 &Input::new(&correct("dir/foo.txt")).remove_extension().get(),117 &Input::new(&correct("dir/foo.txt"))118 .remove_extension()119 .get_private(),120 &correct("dir/foo")121 );122 }123124 #[test]125 fn path_hidden() {174 assert_eq!(&Input::new(".foo").remove_extension().get(), ".foo")126 assert_eq!(&Input::new(".foo").remove_extension().get_private(), ".foo")127 }128129 #[test]130 fn path_remove_ext_utf8() {179 assert_eq!(&Input::new("💖.txt").remove_extension().get(), "💖");131 assert_eq!(132 &Input::new("💖.txt").remove_extension().get_private(),133 "💖"134 );135 }136137 #[test]138 fn path_remove_ext_empty() {184 assert_eq!(&Input::new("").remove_extension().get(), "");139 assert_eq!(&Input::new("").remove_extension().get_private(), "");140 }141142 #[test]143 fn path_basename_simple() {189 assert_eq!(&Input::new("foo.txt").basename().get(), "foo.txt");144 assert_eq!(&Input::new("foo.txt").basename().get_private(), "foo.txt");145 }146147 #[test]148 fn path_basename_dir() {149 assert_eq!(195 &Input::new(&correct("dir/foo.txt")).basename().get(),150 &Input::new(&correct("dir/foo.txt")).basename().get_private(),151 "foo.txt"152 );153 }154155 #[test]156 fn path_basename_empty() {202 assert_eq!(&Input::new("").basename().get(), "");157 assert_eq!(&Input::new("").basename().get_private(), "");158 }159160 #[test]161 fn path_basename_utf8() {162 assert_eq!(208 &Input::new(&correct("💖/foo.txt")).basename().get(),163 &Input::new(&correct("💖/foo.txt"))164 .basename()165 .get_private(),166 "foo.txt"167 );168 assert_eq!(212 &Input::new(&correct("dir/💖.txt")).basename().get(),169 &Input::new(&correct("dir/💖.txt"))170 .basename()171 .get_private(),172 "💖.txt"173 );174 }175176 #[test]177 fn path_dirname_simple() {219 assert_eq!(&Input::new("foo.txt").dirname().get(), ".");178 assert_eq!(&Input::new("foo.txt").dirname().get_private(), ".");179 }180181 #[test]182 fn path_dirname_dir() {224 assert_eq!(&Input::new(&correct("dir/foo.txt")).dirname().get(), "dir");183 assert_eq!(184 &Input::new(&correct("dir/foo.txt")).dirname().get_private(),185 "dir"186 );187 }188189 #[test]190 fn path_dirname_utf8() {191 assert_eq!(230 &Input::new(&correct("💖/foo.txt")).dirname().get(),192 &Input::new(&correct("💖/foo.txt")).dirname().get_private(),193 "💖"194 );233 assert_eq!(&Input::new(&correct("dir/💖.txt")).dirname().get(), "dir");234 }235236 #[test]237 fn path_dirname_empty() {238 assert_eq!(&Input::new("").dirname().get(), ".");239 }240241 #[cfg(windows)]242 #[test]243 fn path_special_chars() {195 assert_eq!(245 &Input::new("A Directory\\And A File").get(),246 "A^ Directory\\And^ A^ File"196 &Input::new(&correct("dir/💖.txt")).dirname().get_private(),197 "dir"198 );199 }200250 #[cfg(not(windows))]201 #[test]252 fn path_special_chars() {253 assert_eq!(254 &Input::new("A Directory/And A File").get(),255 "A\\ Directory/And\\ A\\ File"256 );202 fn path_dirname_empty() {203 assert_eq!(&Input::new("").dirname().get_private(), ".");204 }205}
modified:src/main.rs+1 added
16 unmodified lines
17extern crate libc;18extern crate num_cpus;19extern crate regex;20extern crate regex_syntax;21extern crate shell_escape;22#[cfg(windows)]23extern crate windows;2425pub mod fshelper;
138 unmodified lines
modified:Cargo.lock+7 added
10 unmodified lines
11 "libc 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",12 "num_cpus 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)",13 "regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",14 "regex-syntax 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",15 "shell-escape 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",16 "tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",17 "windows 0.0.0",18]19
206 unmodified lines
226 "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",227]228229[[package]]230name = "shell-escape"231version = "0.1.3"232source = "registry+https://github.com/rust-lang/crates.io-index"233234[[package]]235name = "strsim"236version = "0.6.0"237source = "registry+https://github.com/rust-lang/crates.io-index"238
126 unmodified lines
365"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"366"checksum regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1731164734096285ec2a5ec7fea5248ae2f5485b3feeb0115af4fda2183b2d1b"367"checksum regex-syntax 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad890a5eef7953f55427c50575c680c42841653abd2b028b68cd223d157f62db"368"checksum same-file 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d931a44fdaa43b8637009e7632a02adc4f2b2e0733c08caa4cf00e8da4a117a7"369"checksum shell-escape 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "dd5cc96481d54583947bfe88bf30c23d53f883c6cd0145368b69989d97b84ef8"370"checksum strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d15c810519a91cf877e7e36e63fe068815c678181439f2f29e2562147c3694"371"checksum tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "87974a6f5c1dfb344d733055601650059a3363de2a6104819293baff662132d6"372"checksum term_size 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2b6b55df3198cc93372e85dd2ed817f0e38ce8cc0f22eb32391bfad9c4bf209"373"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
10 unmodified lines
modified:Cargo.toml+1 added