Skip to content

Commit 9ff4114

Browse files
Socialprankercakebaker
authored andcommitted
numfmt: zero-pad the number, not the unit suffix
GNU's --help states that a zero width "will zero pad the number", so the unit from --to and the --suffix text sit outside the field width, unlike space padding, which applies to the whole output. uutils counted them in, so --format=%05.1f --to=si 1234567 printed 01.3M instead of 001.3M.
1 parent e0ff0eb commit 9ff4114

2 files changed

Lines changed: 70 additions & 4 deletions

File tree

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,18 @@ fn pad_string(s: &str, width: usize, fill: char, right_align: bool) -> String {
762762
result
763763
}
764764

765+
/// Split a scaled value into its numeric part and the unit suffix that trails
766+
/// it (`k`, `Mi`, ...).
767+
///
768+
/// A value with no digits at all, such as `inf`, has no numeric part to pad, so
769+
/// it is returned whole and is padded as before.
770+
fn split_number_and_units(s: &str) -> (&str, &str) {
771+
match s.rfind(|c: char| c.is_ascii_digit()) {
772+
Some(last_digit) => s.split_at(last_digit + 1),
773+
None => (s, ""),
774+
}
775+
}
776+
765777
fn format_string(
766778
source: &str,
767779
options: &NumfmtOptions,
@@ -816,11 +828,30 @@ fn format_string(
816828
let padded_number = match padding {
817829
0 => number_with_suffix,
818830
p if p > 0 && options.format.zero_padding => {
819-
let zero_padded = if let Some(unsigned) = number_with_suffix.strip_prefix(['-', '+']) {
820-
let sign = &number_with_suffix[..1];
821-
format!("{sign}{}", pad_string(unsigned, p as usize - 1, '0', true))
831+
// GNU zero-pads the number itself: "Optional zero (%010f) width
832+
// will zero pad the number". A unit suffix from --to and the
833+
// --suffix text sit outside the width, unlike space padding,
834+
// which applies to the whole output.
835+
// The --suffix text is user-supplied and may itself contain
836+
// digits, so peel it off before locating the number.
837+
let (scaled, user_suffix) = match &options.suffix {
838+
Some(suffix) => number_with_suffix
839+
.strip_suffix(suffix.as_str())
840+
.map_or((number_with_suffix.as_str(), ""), |rest| {
841+
(rest, suffix.as_str())
842+
}),
843+
None => (number_with_suffix.as_str(), ""),
844+
};
845+
let (number, unit) = split_number_and_units(scaled);
846+
let trailing = format!("{unit}{user_suffix}");
847+
let zero_padded = if let Some(unsigned) = number.strip_prefix(['-', '+']) {
848+
let sign = &number[..1];
849+
format!(
850+
"{sign}{}{trailing}",
851+
pad_string(unsigned, (p as usize).saturating_sub(1), '0', true)
852+
)
822853
} else {
823-
pad_string(&number_with_suffix, p as usize, '0', true)
854+
format!("{}{trailing}", pad_string(number, p as usize, '0', true))
824855
};
825856

826857
match implicit_padding.unwrap_or(options.padding) {

‎tests/by-util/test_numfmt.rs‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,41 @@ fn test_format_with_zero_padding_and_suffix() {
11141114
.stdout_is("001234 ?\n");
11151115
}
11161116

1117+
// GNU zero-pads the number only: "Optional zero (%010f) width will zero pad
1118+
// the number". The unit from --to and the --suffix text stay outside the width.
1119+
#[test]
1120+
fn test_format_with_zero_padding_excludes_unit_suffix() {
1121+
let cases = [
1122+
(vec!["--format=%05.1f", "--to=si", "1234567"], "001.3M"),
1123+
(vec!["--format=%06.1f", "--to=si", "1234"], "0001.3k"),
1124+
(
1125+
vec!["--format=%08.1f", "--to=iec-i", "1234567"],
1126+
"000001.2Mi",
1127+
),
1128+
(
1129+
vec!["--format=%08.1f", "--to=si", "--", "-1234567"],
1130+
"-00001.3M",
1131+
),
1132+
(vec!["--format=%03.1f", "--to=si", "1234567"], "1.3M"),
1133+
(
1134+
vec!["--format=%05.1f", "--to=si", "--suffix=B", "1234567"],
1135+
"001.3MB",
1136+
),
1137+
(vec!["--format=%05.1f", "--suffix=B", "1.5"], "001.5B"),
1138+
(
1139+
vec!["--format=%08.1f", "--to=si", "--padding=12", "1234567"],
1140+
" 000001.3M",
1141+
),
1142+
];
1143+
1144+
for (args, expected) in cases {
1145+
new_ucmd!()
1146+
.args(&args)
1147+
.succeeds()
1148+
.stdout_only(format!("{expected}\n"));
1149+
}
1150+
}
1151+
11171152
#[test]
11181153
fn test_format_with_precision() {
11191154
let values = vec![("0.99", "1.0"), ("1", "1.0"), ("1.01", "1.1")];

0 commit comments

Comments
 (0)