Skip to content

Commit 4e2f797

Browse files
committed
numfmt: address review: one-line comment, let-else for the suffix, add --to=iec case
1 parent 9885a71 commit 4e2f797

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

‎src/uu/numfmt/src/format.rs‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -533,13 +533,11 @@ fn consider_suffix(
533533

534534
// check if rounding pushed us into the next base
535535
if v.abs() >= bases[1] {
536-
// Rounding promoted the value to the next base. When already at the
537-
// largest base there is no suffix above it, so the value is too large
538-
// to represent (rather than indexing `suffixes` out of bounds).
539-
if i >= suffixes.len() {
536+
// Rounding past the largest suffix leaves no suffix to promote to.
537+
let Some(&suffix) = suffixes.get(i) else {
540538
return Err(translate!("numfmt-error-number-too-big"));
541-
}
542-
Ok((v / bases[1], Some((suffixes[i], with_i))))
539+
};
540+
Ok((v / bases[1], Some((suffix, with_i))))
543541
} else {
544542
Ok((v, Some((suffixes[i - 1], with_i))))
545543
}

‎tests/by-util/test_numfmt.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,15 @@ fn test_to_si() {
100100
}
101101

102102
#[test]
103-
fn test_to_si_rounding_past_top_suffix_is_too_big() {
103+
fn test_rounding_past_top_suffix() {
104104
new_ucmd!()
105105
.args(&["--to=si", "999500000000000000000000000000000"])
106106
.fails_with_code(2)
107107
.stderr_only("numfmt: Number is too big and unsupported\n");
108+
new_ucmd!()
109+
.args(&["--to=iec", "999500000000000000000000000000000"])
110+
.succeeds()
111+
.stdout_only("789Q\n");
108112
}
109113

110114
#[test]

0 commit comments

Comments
 (0)