Skip to content

Commit 0235e2f

Browse files
committed
ls: optimize canonicalize_indicator_value to avoid unnecessary allocations
1 parent 4696b57 commit 0235e2f

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/uu/ls/src/colors.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// file that was distributed with this source code.
55
use super::PathData;
66
use lscolors::{Indicator, LsColors, Style};
7+
use std::borrow::Cow;
78
use std::collections::HashMap;
89
use std::env;
910
use std::ffi::OsString;
@@ -758,22 +759,22 @@ fn parse_indicator_codes() -> (HashMap<Indicator, String>, bool) {
758759
}
759760
continue;
760761
}
761-
indicator_codes.insert(indicator, canonicalize_indicator_value(value));
762+
indicator_codes.insert(indicator, canonicalize_indicator_value(value).into_owned());
762763
}
763764
}
764765
}
765766

766767
(indicator_codes, ln_color_from_target)
767768
}
768769

769-
fn canonicalize_indicator_value(value: &str) -> String {
770+
fn canonicalize_indicator_value(value: &str) -> Cow<'_, str> {
770771
if value.len() == 1 && value.chars().all(|c| c.is_ascii_digit()) {
771772
let mut canonical = String::with_capacity(2);
772773
canonical.push('0');
773774
canonical.push_str(value);
774-
canonical
775+
Cow::Owned(canonical)
775776
} else {
776-
value.to_string()
777+
Cow::Borrowed(value)
777778
}
778779
}
779780

0 commit comments

Comments
 (0)