From 4552c0f9fd7913d173e7daa91cbca3dc58f4d13a Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Fri, 3 Apr 2026 18:53:18 +0200 Subject: [PATCH] numfmt: use repeat_n for padding and simplify suffix validation --- src/uu/numfmt/src/format.rs | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/src/uu/numfmt/src/format.rs b/src/uu/numfmt/src/format.rs index 3336e21d1..4b5892408 100644 --- a/src/uu/numfmt/src/format.rs +++ b/src/uu/numfmt/src/format.rs @@ -302,18 +302,13 @@ fn split_mergeable_suffix<'a>(s: &'a str, options: &NumfmtOptions) -> Option<(&' return None; } - let is_suffix = match field.len() { - 1 => true, - 2 => field.ends_with('i'), - _ => false, - }; - if !is_suffix { - return None; + let first_char = field.chars().next()?; + RawSuffix::try_from(&first_char).ok()?; + match field.len() { + 1 => {} + 2 if field.ends_with('i') => {} + _ => return None, } - field - .chars() - .next() - .filter(|c| RawSuffix::try_from(c).is_ok())?; Some((prefix, field)) } @@ -552,15 +547,11 @@ fn pad_string(s: &str, width: usize, fill: char, right_align: bool) -> String { let pad = width - len; let mut result = String::with_capacity(width); if right_align { - for _ in 0..pad { - result.push(fill); - } + result.extend(std::iter::repeat_n(fill, pad)); result.push_str(s); } else { result.push_str(s); - for _ in 0..pad { - result.push(fill); - } + result.extend(std::iter::repeat_n(fill, pad)); } result }