Skip to content
Myr

Fix Input::remove_extension and Input::dirname

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 d702d84 · Browse files · 2 files+30 added−6 removed
This commit

State

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

Diff stats

2 files+30 added−6 removed

  • tests:+1 added−4 removed
  • other:+29 added−2 removed

2 files+30 added−6 removed

modified:src/exec/input.rs+29 added-2 removed
43 unmodified lines
44 }4546 /// Removes the extension from the path47 pub fn remove_extension(&'a mut self) -> &'a mut Self {48 let mut has_dir = false;49 let mut dir_index = 0;50 let mut ext_index = 0;5152 for (id, character) in self.data.char_indices() {53 if character == MAIN_SEPARATOR {54 has_dir = true;55 dir_index = id;56 }57 if character == '.' {58 ext_index = id;59 }60 }6162 // Account for hidden files and directories61 if ext_index != 0 && dir_index + 2 <= ext_index {63 if ext_index != 0 && (!has_dir || dir_index + 2 <= ext_index) {64 self.data = &self.data[0..ext_index];65 }6667 self68 }6970 /// Removes the basename from the path.71 pub fn dirname(&'a mut self) -> &'a mut Self {72 let mut has_dir = false;73 let mut index = 0;74 for (id, character) in self.data.char_indices() {75 if character == MAIN_SEPARATOR {76 has_dir = true;77 index = id;78 }79 }8077 self.data = if index == 0 {81 self.data = if !has_dir {82 "."83 } else if index == 0 {84 &self.data[..1]85 } else {86 &self.data[0..index]87 };88
61 unmodified lines
150 assert_eq!(&Input::new("foo.txt").basename().get_private(), "foo.txt");151 }152153 #[test]154 fn path_basename_no_ext() {155 assert_eq!(156 &Input::new("foo.txt")157 .basename()158 .remove_extension()159 .get_private(),160 "foo"161 );162 }163164 #[test]165 fn path_basename_dir() {166 assert_eq!(167 &Input::new(&correct("dir/foo.txt")).basename().get_private(),168 "foo.txt"
49 unmodified lines
218 #[test]219 fn path_dirname_empty() {220 assert_eq!(&Input::new("").dirname().get_private(), ".");221 }222223 #[test]224 fn path_dirname_root() {225 #[cfg(windows)]226 assert_eq!(&Input::new("C:\\").dirname().get_private(), "C:");227 #[cfg(windows)]228 assert_eq!(&Input::new("\\").dirname().get_private(), "\\");229 #[cfg(not(windows))]230 assert_eq!(&Input::new("/").dirname().get_private(), "/");231 }232}
modified:tests/tests.rs+1 added-4 removed
665 unmodified lines
666 one/two/three667 one/two/three",668 );669670 te.assert_output(671 &["e1", "--exec", "printf '%s.%s\\n'"],672 "e1 e2."673 );670 te.assert_output(&["e1", "--exec", "printf '%s.%s\\n'"], "e1 e2.");671 }672}