Rewrite replace_path_separator function

This commit is contained in:
sharkdp 2020-04-03 23:01:57 +02:00
parent 2ea77e7cdc
commit e44f2f8540
2 changed files with 10 additions and 11 deletions

View file

@ -93,13 +93,8 @@ pub fn strip_current_dir(pathbuf: &PathBuf) -> &Path {
iter.as_path() iter.as_path()
} }
pub fn replace_path_separator<'a>(path_separator: &Option<String>, path: &mut Cow<'a, str>) { pub fn replace_path_separator<'a>(path: &str, new_path_separator: &str) -> String {
match &path_separator { path.replace(std::path::MAIN_SEPARATOR, &new_path_separator)
None => {}
Some(sep) => {
*path.to_mut() = path.replace(std::path::MAIN_SEPARATOR, &sep);
}
}
} }
#[cfg(test)] #[cfg(test)]

View file

@ -53,7 +53,9 @@ fn print_entry_colorized(
.unwrap_or(default_style); .unwrap_or(default_style);
let mut path_string = component.to_string_lossy(); let mut path_string = component.to_string_lossy();
replace_path_separator(&config.path_separator, &mut path_string); if let Some(ref separator) = config.path_separator {
*path_string.to_mut() = replace_path_separator(&path_string, &separator);
}
write!(stdout, "{}", style.paint(path_string))?; write!(stdout, "{}", style.paint(path_string))?;
// TODO: can we move this out of the if-statement? Why do we call it that often? // TODO: can we move this out of the if-statement? Why do we call it that often?
@ -78,7 +80,9 @@ fn print_entry_uncolorized(
) -> io::Result<()> { ) -> io::Result<()> {
let separator = if config.null_separator { "\0" } else { "\n" }; let separator = if config.null_separator { "\0" } else { "\n" };
let mut path_str = path.to_string_lossy(); let mut path_string = path.to_string_lossy();
replace_path_separator(&config.path_separator, &mut path_str); if let Some(ref separator) = config.path_separator {
write!(stdout, "{}{}", path_str, separator) *path_string.to_mut() = replace_path_separator(&path_string, &separator);
}
write!(stdout, "{}{}", path_string, separator)
} }