Fix Input::remove_extension and Input::dirname

This commit is contained in:
J.W 2017-10-26 15:18:06 +08:00 committed by David Peter
parent d702d849ea
commit dc29ef70c4
2 changed files with 30 additions and 6 deletions

View File

@ -45,11 +45,13 @@ impl<'a> Input<'a> {
/// Removes the extension from the path
pub fn remove_extension(&'a mut self) -> &'a mut Self {
let mut has_dir = false;
let mut dir_index = 0;
let mut ext_index = 0;
for (id, character) in self.data.char_indices() {
if character == MAIN_SEPARATOR {
has_dir = true;
dir_index = id;
}
if character == '.' {
@ -58,7 +60,7 @@ impl<'a> Input<'a> {
}
// Account for hidden files and directories
if ext_index != 0 && dir_index + 2 <= ext_index {
if ext_index != 0 && (!has_dir || dir_index + 2 <= ext_index) {
self.data = &self.data[0..ext_index];
}
@ -67,15 +69,19 @@ impl<'a> Input<'a> {
/// Removes the basename from the path.
pub fn dirname(&'a mut self) -> &'a mut Self {
let mut has_dir = false;
let mut index = 0;
for (id, character) in self.data.char_indices() {
if character == MAIN_SEPARATOR {
has_dir = true;
index = id;
}
}
self.data = if index == 0 {
self.data = if !has_dir {
"."
} else if index == 0 {
&self.data[..1]
} else {
&self.data[0..index]
};
@ -144,6 +150,17 @@ mod tests {
assert_eq!(&Input::new("foo.txt").basename().get_private(), "foo.txt");
}
#[test]
fn path_basename_no_ext() {
assert_eq!(
&Input::new("foo.txt")
.basename()
.remove_extension()
.get_private(),
"foo"
);
}
#[test]
fn path_basename_dir() {
assert_eq!(
@ -202,4 +219,14 @@ mod tests {
fn path_dirname_empty() {
assert_eq!(&Input::new("").dirname().get_private(), ".");
}
#[test]
fn path_dirname_root() {
#[cfg(windows)]
assert_eq!(&Input::new("C:\\").dirname().get_private(), "C:");
#[cfg(windows)]
assert_eq!(&Input::new("\\").dirname().get_private(), "\\");
#[cfg(not(windows))]
assert_eq!(&Input::new("/").dirname().get_private(), "/");
}
}

View File

@ -667,9 +667,6 @@ fn test_exec() {
one/two/three",
);
te.assert_output(
&["e1", "--exec", "printf '%s.%s\\n'"],
"e1 e2."
);
te.assert_output(&["e1", "--exec", "printf '%s.%s\\n'"], "e1 e2.");
}
}