mirror of https://github.com/sharkdp/fd.git
Fix Input::remove_extension and Input::dirname
This commit is contained in:
parent
d702d849ea
commit
dc29ef70c4
|
@ -45,11 +45,13 @@ impl<'a> Input<'a> {
|
||||||
|
|
||||||
/// Removes the extension from the path
|
/// Removes the extension from the path
|
||||||
pub fn remove_extension(&'a mut self) -> &'a mut Self {
|
pub fn remove_extension(&'a mut self) -> &'a mut Self {
|
||||||
|
let mut has_dir = false;
|
||||||
let mut dir_index = 0;
|
let mut dir_index = 0;
|
||||||
let mut ext_index = 0;
|
let mut ext_index = 0;
|
||||||
|
|
||||||
for (id, character) in self.data.char_indices() {
|
for (id, character) in self.data.char_indices() {
|
||||||
if character == MAIN_SEPARATOR {
|
if character == MAIN_SEPARATOR {
|
||||||
|
has_dir = true;
|
||||||
dir_index = id;
|
dir_index = id;
|
||||||
}
|
}
|
||||||
if character == '.' {
|
if character == '.' {
|
||||||
|
@ -58,7 +60,7 @@ impl<'a> Input<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Account for hidden files and directories
|
// 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];
|
self.data = &self.data[0..ext_index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,15 +69,19 @@ impl<'a> Input<'a> {
|
||||||
|
|
||||||
/// Removes the basename from the path.
|
/// Removes the basename from the path.
|
||||||
pub fn dirname(&'a mut self) -> &'a mut Self {
|
pub fn dirname(&'a mut self) -> &'a mut Self {
|
||||||
|
let mut has_dir = false;
|
||||||
let mut index = 0;
|
let mut index = 0;
|
||||||
for (id, character) in self.data.char_indices() {
|
for (id, character) in self.data.char_indices() {
|
||||||
if character == MAIN_SEPARATOR {
|
if character == MAIN_SEPARATOR {
|
||||||
|
has_dir = true;
|
||||||
index = id;
|
index = id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.data = if index == 0 {
|
self.data = if !has_dir {
|
||||||
"."
|
"."
|
||||||
|
} else if index == 0 {
|
||||||
|
&self.data[..1]
|
||||||
} else {
|
} else {
|
||||||
&self.data[0..index]
|
&self.data[0..index]
|
||||||
};
|
};
|
||||||
|
@ -144,6 +150,17 @@ mod tests {
|
||||||
assert_eq!(&Input::new("foo.txt").basename().get_private(), "foo.txt");
|
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]
|
#[test]
|
||||||
fn path_basename_dir() {
|
fn path_basename_dir() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -202,4 +219,14 @@ mod tests {
|
||||||
fn path_dirname_empty() {
|
fn path_dirname_empty() {
|
||||||
assert_eq!(&Input::new("").dirname().get_private(), ".");
|
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(), "/");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -667,9 +667,6 @@ fn test_exec() {
|
||||||
one/two/three",
|
one/two/three",
|
||||||
);
|
);
|
||||||
|
|
||||||
te.assert_output(
|
te.assert_output(&["e1", "--exec", "printf '%s.%s\\n'"], "e1 e2.");
|
||||||
&["e1", "--exec", "printf '%s.%s\\n'"],
|
|
||||||
"e1 e2."
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue