Skip to content

Commit 4e2410b

Browse files
committed
Allocate less strings when formatting item names and hyperlinks
1 parent 3bab3a8 commit 4e2410b

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

src/uu/ls/src/ls.rs

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2605,6 +2605,22 @@ impl ExtendPad for Vec<u8> {
26052605
}
26062606
}
26072607

2608+
impl ExtendPad for String {
2609+
fn extend_pad_left(&mut self, string: &str, count: usize) {
2610+
if string.len() < count {
2611+
self.extend(iter::repeat_n(' ', count - string.len()));
2612+
}
2613+
self.push_str(string);
2614+
}
2615+
2616+
fn extend_pad_right(&mut self, string: &str, count: usize) {
2617+
self.push_str(string);
2618+
if string.len() < count {
2619+
self.extend(iter::repeat_n(' ', count - string.len()));
2620+
}
2621+
}
2622+
}
2623+
26082624
// TODO: Consider converting callers to use ExtendPad instead, as it avoids
26092625
// additional copies.
26102626
fn pad_left(string: &str, count: usize) -> String {
@@ -2643,26 +2659,28 @@ fn display_additional_leading_info(
26432659
{
26442660
if config.inode {
26452661
let i = if let Some(md) = item.metadata() {
2646-
get_inode(md)
2662+
&get_inode(md)
26472663
} else {
2648-
"?".to_owned()
2664+
"?"
26492665
};
2650-
write!(result, "{} ", pad_left(&i, padding.inode)).unwrap();
2666+
result.extend_pad_left(i, padding.inode);
2667+
result.push(' ');
26512668
}
26522669
}
26532670

26542671
if config.alloc_size {
26552672
let s = if let Some(md) = item.metadata() {
2656-
display_size(get_block_size(md, config), config)
2673+
&display_size(get_block_size(md, config), config)
26572674
} else {
2658-
"?".to_owned()
2675+
"?"
26592676
};
26602677
// extra space is insert to align the sizes, as needed for all formats, except for the comma format.
26612678
if config.format == Format::Commas {
2662-
write!(result, "{s} ").unwrap();
2679+
result.push_str(s);
26632680
} else {
2664-
write!(result, "{} ", pad_left(&s, padding.block_size)).unwrap();
2681+
result.extend_pad_left(s, padding.block_size);
26652682
}
2683+
result.push(' ');
26662684
}
26672685
Ok(result)
26682686
}
@@ -3372,7 +3390,7 @@ fn display_item_name(
33723390
};
33733391

33743392
if let Some(c) = char_opt {
3375-
name.push(OsStr::new(&c.to_string()));
3393+
name.write_char(c).unwrap();
33763394
}
33773395
}
33783396

@@ -3444,14 +3462,13 @@ fn display_item_name(
34443462
// to get correct alignment from later calls to`display_grid()`.
34453463
if config.context {
34463464
if let Some(pad_count) = prefix_context {
3447-
let security_context = if matches!(config.format, Format::Commas) {
3448-
path.security_context(config).to_string()
3465+
let old_name = name;
3466+
name = if matches!(config.format, Format::Commas) {
3467+
path.security_context(config).into()
34493468
} else {
3450-
pad_left(path.security_context(config), pad_count)
3469+
pad_left(path.security_context(config), pad_count).into()
34513470
};
3452-
3453-
let old_name = name;
3454-
name = format!("{security_context} ").into();
3471+
name.push(" ");
34553472
name.push(old_name);
34563473
}
34573474
}
@@ -3472,19 +3489,17 @@ fn create_hyperlink(name: &OsStr, path: &PathData) -> OsString {
34723489
let unencoded_chars = "_-.:~/\\";
34733490

34743491
// percentage encoding of path
3475-
let absolute_path: String = absolute_path
3476-
.chars()
3477-
.map(|c| {
3478-
if c.is_alphanumeric() || unencoded_chars.contains(c) {
3479-
c.to_string()
3480-
} else {
3481-
format!("%{:02x}", c as u8)
3482-
}
3483-
})
3484-
.collect();
3492+
let mut absolute_path_encoded = String::with_capacity(absolute_path.len() * 2);
3493+
for c in absolute_path.chars() {
3494+
if c.is_alphanumeric() || unencoded_chars.contains(c) {
3495+
absolute_path_encoded.push(c);
3496+
} else {
3497+
let _ = write!(&mut absolute_path_encoded, "%{:02x}", c as u8);
3498+
}
3499+
}
34853500

34863501
// \x1b = ESC, \x07 = BEL
3487-
let mut ret: OsString = format!("\x1b]8;;file://{hostname}{absolute_path}\x07").into();
3502+
let mut ret: OsString = format!("\x1b]8;;file://{hostname}{absolute_path_encoded}\x07").into();
34883503
ret.push(name);
34893504
ret.push("\x1b]8;;\x07");
34903505
ret

0 commit comments

Comments
 (0)