Skip to content

Commit 4552c0f

Browse files
committed
numfmt: use repeat_n for padding and simplify suffix validation
1 parent 8e5412b commit 4552c0f

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

src/uu/numfmt/src/format.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -302,18 +302,13 @@ fn split_mergeable_suffix<'a>(s: &'a str, options: &NumfmtOptions) -> Option<(&'
302302
return None;
303303
}
304304

305-
let is_suffix = match field.len() {
306-
1 => true,
307-
2 => field.ends_with('i'),
308-
_ => false,
309-
};
310-
if !is_suffix {
311-
return None;
305+
let first_char = field.chars().next()?;
306+
RawSuffix::try_from(&first_char).ok()?;
307+
match field.len() {
308+
1 => {}
309+
2 if field.ends_with('i') => {}
310+
_ => return None,
312311
}
313-
field
314-
.chars()
315-
.next()
316-
.filter(|c| RawSuffix::try_from(c).is_ok())?;
317312

318313
Some((prefix, field))
319314
}
@@ -552,15 +547,11 @@ fn pad_string(s: &str, width: usize, fill: char, right_align: bool) -> String {
552547
let pad = width - len;
553548
let mut result = String::with_capacity(width);
554549
if right_align {
555-
for _ in 0..pad {
556-
result.push(fill);
557-
}
550+
result.extend(std::iter::repeat_n(fill, pad));
558551
result.push_str(s);
559552
} else {
560553
result.push_str(s);
561-
for _ in 0..pad {
562-
result.push(fill);
563-
}
554+
result.extend(std::iter::repeat_n(fill, pad));
564555
}
565556
result
566557
}

0 commit comments

Comments
 (0)