diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index b5e76b7d3..1cebeb38a 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -1056,7 +1056,9 @@ impl PathData { ) -> Self { let name = p_buf .file_name() - .map_or(String::new(), |s| s.to_string_lossy().into_owned()); + .unwrap_or_else(|| p_buf.iter().next_back().unwrap()) + .to_string_lossy() + .into_owned(); let must_dereference = match &config.dereference { Dereference::All => true, Dereference::Args => command_line, @@ -1355,8 +1357,7 @@ fn display_item_long( ) { let md = match item.md() { None => { - let filename = get_file_name(&item.p_buf); - show_error!("could not show file: {}", filename); + show_error!("could not show file: {}", &item.p_buf.display()); return; } Some(md) => md, @@ -1550,13 +1551,6 @@ fn display_file_type(file_type: FileType) -> char { } } -fn get_file_name(name: &Path) -> String { - name.file_name() - .unwrap_or(name.iter().last().unwrap()) - .to_string_lossy() - .into() -} - #[cfg(unix)] fn file_is_executable(md: &Metadata) -> bool { // Mode always returns u32, but the flags might not be, based on the platform @@ -1594,7 +1588,7 @@ fn classify_file(path: &PathData) -> Option { } fn display_file_name(path: &PathData, config: &Config) -> Option { - let mut name = escape_name(get_file_name(&path.p_buf), &config.quoting_style); + let mut name = escape_name(&path.file_name, &config.quoting_style); #[cfg(unix)] { diff --git a/src/uu/ls/src/quoting_style.rs b/src/uu/ls/src/quoting_style.rs index 49456fc22..01bffa7ec 100644 --- a/src/uu/ls/src/quoting_style.rs +++ b/src/uu/ls/src/quoting_style.rs @@ -171,7 +171,7 @@ impl Iterator for EscapedChar { } } -fn shell_without_escape(name: String, quotes: Quotes, show_control_chars: bool) -> (String, bool) { +fn shell_without_escape(name: &str, quotes: Quotes, show_control_chars: bool) -> (String, bool) { let mut must_quote = false; let mut escaped_str = String::with_capacity(name.len()); @@ -201,7 +201,7 @@ fn shell_without_escape(name: String, quotes: Quotes, show_control_chars: bool) (escaped_str, must_quote) } -fn shell_with_escape(name: String, quotes: Quotes) -> (String, bool) { +fn shell_with_escape(name: &str, quotes: Quotes) -> (String, bool) { // We need to keep track of whether we are in a dollar expression // because e.g. \b\n is escaped as $'\b\n' and not like $'b'$'n' let mut in_dollar = false; @@ -249,7 +249,7 @@ fn shell_with_escape(name: String, quotes: Quotes) -> (String, bool) { (escaped_str, must_quote) } -pub(super) fn escape_name(name: String, style: &QuotingStyle) -> String { +pub(super) fn escape_name(name: &str, style: &QuotingStyle) -> String { match style { QuotingStyle::Literal { show_control } => { if !show_control { @@ -257,7 +257,7 @@ pub(super) fn escape_name(name: String, style: &QuotingStyle) -> String { .flat_map(|c| EscapedChar::new_literal(c).hide_control()) .collect() } else { - name + name.into() } } QuotingStyle::C { quotes } => { @@ -354,7 +354,7 @@ mod tests { fn check_names(name: &str, map: Vec<(&str, &str)>) { assert_eq!( map.iter() - .map(|(_, style)| escape_name(name.to_string(), &get_style(style))) + .map(|(_, style)| escape_name(name, &get_style(style))) .collect::>(), map.iter() .map(|(correct, _)| correct.to_string())