From ded0a5cfa0a73a1292b91016efa710f103dd6868 Mon Sep 17 00:00:00 2001 From: Serag Ahmed Date: Mon, 30 Nov 2026 04:21:28 +0300 Subject: [PATCH 1/2] numfmt: only take a unit from the next field when converting this one With a blank --unit-separator, a field followed by a word that looks like a suffix ("42 R") was merged with it even when the field was not selected, which shifted the numbering of every later field. --- src/uu/numfmt/src/format.rs | 31 ++++++++++++++++++++++--------- tests/by-util/test_numfmt.rs | 15 +++++++++++++++ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/uu/numfmt/src/format.rs b/src/uu/numfmt/src/format.rs index ad64c1236e3..188113edb14 100644 --- a/src/uu/numfmt/src/format.rs +++ b/src/uu/numfmt/src/format.rs @@ -373,6 +373,18 @@ fn split_mergeable_suffix<'a>(s: &'a str, options: &NumfmtOptions) -> Option<(&' struct WhitespaceSplitter<'a, 'b> { s: Option<&'a str>, options: &'b NumfmtOptions, + /// The number of the field returned by the next call. + field: usize, +} + +impl<'a, 'b> WhitespaceSplitter<'a, 'b> { + fn new(s: &'a str, options: &'b NumfmtOptions) -> Self { + Self { + s: Some(s), + options, + field: 1, + } + } } impl<'a> Iterator for WhitespaceSplitter<'a, '_> { @@ -381,13 +393,20 @@ impl<'a> Iterator for WhitespaceSplitter<'a, '_> { fn next(&mut self) -> Option { let haystack = self.s?; let (prefix, field, rest) = split_next_field(haystack); + let field_number = self.field; + self.field += 1; if field.is_empty() { self.s = None; return Some((prefix, field)); } - if let Some((suffix_prefix, suffix_field)) = split_mergeable_suffix(rest, self.options) { + // Only a field that gets converted can take the next one as its unit; + // otherwise a word like "R" would shift the numbering of later fields. + let mergeable = uucore::ranges::contain(&self.options.fields, field_number) + .then(|| split_mergeable_suffix(rest, self.options)) + .flatten(); + if let Some((suffix_prefix, suffix_field)) = mergeable { let merged_len = prefix.len() + field.len() + suffix_prefix.len() + suffix_field.len(); let merged_field = &haystack[prefix.len()..merged_len]; self.s = Some(&haystack[merged_len..]).filter(|rest| !rest.is_empty()); @@ -1005,10 +1024,7 @@ pub fn write_formatted_with_whitespace( scratch: &mut FormatScratch, ) -> std::result::Result { let mut saw_invalid = false; - for (n, (prefix, field)) in (1..).zip(WhitespaceSplitter { - s: Some(s), - options, - }) { + for (n, (prefix, field)) in (1..).zip(WhitespaceSplitter::new(s, options)) { let field_selected = uucore::ranges::contain(&options.fields, n); if field_selected { @@ -1064,10 +1080,7 @@ pub fn write_unconverted_with_whitespace( s: &str, options: &NumfmtOptions, ) -> std::io::Result<()> { - for (n, (prefix, field)) in (1..).zip(WhitespaceSplitter { - s: Some(s), - options, - }) { + for (n, (prefix, field)) in (1..).zip(WhitespaceSplitter::new(s, options)) { let prefix = if n > 1 { write_separator(writer, prefix)? } else { diff --git a/tests/by-util/test_numfmt.rs b/tests/by-util/test_numfmt.rs index 4c35418d233..75daafd3986 100644 --- a/tests/by-util/test_numfmt.rs +++ b/tests/by-util/test_numfmt.rs @@ -1107,6 +1107,21 @@ fn test_unit_separator() { } } +#[test] +fn test_unit_separator_does_not_shift_fields() { + // "R" is a valid suffix, but it is a field of its own, not the unit of "42" + new_ucmd!() + .args(&[ + "--field=3", + "--from-unit=1024", + "--to=iec-i", + "--unit-separator= ", + ]) + .pipe_in("41 S 8192 2048\n42 R 8192 2048\n") + .succeeds() + .stdout_only("41 S 8.0 Mi 2048\n42 R 8.0 Mi 2048\n"); +} + #[test] fn test_debug_warnings() { new_ucmd!() From 40fb6cf4699b7c46ae2b38f084e88451e049b035 Mon Sep 17 00:00:00 2001 From: Serag Ahmed Date: Tue, 1 Dec 2026 09:07:07 +0300 Subject: [PATCH 2/2] numfmt: use a let-chain for the unit-merge check --- src/uu/numfmt/src/format.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/uu/numfmt/src/format.rs b/src/uu/numfmt/src/format.rs index 188113edb14..f2400160e53 100644 --- a/src/uu/numfmt/src/format.rs +++ b/src/uu/numfmt/src/format.rs @@ -403,10 +403,9 @@ impl<'a> Iterator for WhitespaceSplitter<'a, '_> { // Only a field that gets converted can take the next one as its unit; // otherwise a word like "R" would shift the numbering of later fields. - let mergeable = uucore::ranges::contain(&self.options.fields, field_number) - .then(|| split_mergeable_suffix(rest, self.options)) - .flatten(); - if let Some((suffix_prefix, suffix_field)) = mergeable { + if uucore::ranges::contain(&self.options.fields, field_number) + && let Some((suffix_prefix, suffix_field)) = split_mergeable_suffix(rest, self.options) + { let merged_len = prefix.len() + field.len() + suffix_prefix.len() + suffix_field.len(); let merged_field = &haystack[prefix.len()..merged_len]; self.s = Some(&haystack[merged_len..]).filter(|rest| !rest.is_empty());