1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-01 13:37:48 +00:00

ls: further reduce OsStr -> String conversions

This commit is contained in:
Terts Diepraam 2021-04-26 18:03:56 +02:00
parent e4c0069493
commit 4023e40174
2 changed files with 10 additions and 16 deletions

View file

@ -1056,7 +1056,9 @@ impl PathData {
) -> Self { ) -> Self {
let name = p_buf let name = p_buf
.file_name() .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 { let must_dereference = match &config.dereference {
Dereference::All => true, Dereference::All => true,
Dereference::Args => command_line, Dereference::Args => command_line,
@ -1355,8 +1357,7 @@ fn display_item_long(
) { ) {
let md = match item.md() { let md = match item.md() {
None => { None => {
let filename = get_file_name(&item.p_buf); show_error!("could not show file: {}", &item.p_buf.display());
show_error!("could not show file: {}", filename);
return; return;
} }
Some(md) => md, 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)] #[cfg(unix)]
fn file_is_executable(md: &Metadata) -> bool { fn file_is_executable(md: &Metadata) -> bool {
// Mode always returns u32, but the flags might not be, based on the platform // Mode always returns u32, but the flags might not be, based on the platform
@ -1594,7 +1588,7 @@ fn classify_file(path: &PathData) -> Option<char> {
} }
fn display_file_name(path: &PathData, config: &Config) -> Option<Cell> { fn display_file_name(path: &PathData, config: &Config) -> Option<Cell> {
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)] #[cfg(unix)]
{ {

View file

@ -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 must_quote = false;
let mut escaped_str = String::with_capacity(name.len()); 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) (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 // 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' // because e.g. \b\n is escaped as $'\b\n' and not like $'b'$'n'
let mut in_dollar = false; let mut in_dollar = false;
@ -249,7 +249,7 @@ fn shell_with_escape(name: String, quotes: Quotes) -> (String, bool) {
(escaped_str, must_quote) (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 { match style {
QuotingStyle::Literal { show_control } => { QuotingStyle::Literal { show_control } => {
if !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()) .flat_map(|c| EscapedChar::new_literal(c).hide_control())
.collect() .collect()
} else { } else {
name name.into()
} }
} }
QuotingStyle::C { quotes } => { QuotingStyle::C { quotes } => {
@ -354,7 +354,7 @@ mod tests {
fn check_names(name: &str, map: Vec<(&str, &str)>) { fn check_names(name: &str, map: Vec<(&str, &str)>) {
assert_eq!( assert_eq!(
map.iter() map.iter()
.map(|(_, style)| escape_name(name.to_string(), &get_style(style))) .map(|(_, style)| escape_name(name, &get_style(style)))
.collect::<Vec<String>>(), .collect::<Vec<String>>(),
map.iter() map.iter()
.map(|(correct, _)| correct.to_string()) .map(|(correct, _)| correct.to_string())