Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/uu/numfmt/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, '_> {
Expand All @@ -381,13 +393,19 @@ impl<'a> Iterator for WhitespaceSplitter<'a, '_> {
fn next(&mut self) -> Option<Self::Item> {
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.
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());
Expand Down Expand Up @@ -1005,10 +1023,7 @@ pub fn write_formatted_with_whitespace<W: std::io::Write + ?Sized>(
scratch: &mut FormatScratch,
) -> std::result::Result<bool, WriteError> {
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 {
Expand Down Expand Up @@ -1064,10 +1079,7 @@ pub fn write_unconverted_with_whitespace<W: std::io::Write + ?Sized>(
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 {
Expand Down
15 changes: 15 additions & 0 deletions tests/by-util/test_numfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!()
Expand Down
Loading