From 0b86afa858be5e9149573b06a15b80ee129329c9 Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Tue, 5 Oct 2021 20:59:51 -0400 Subject: [PATCH] seq: correct fixed-width spacing for inf sequences Pad infinity and negative infinity values with spaces when using the `-w` option to `seq`. This corrects the behavior of `seq` to match that of the GNU version: $ seq -w 1.000 inf inf | head -n 4 1.000 inf inf inf Previously, it incorrectly padded with 0s instead of spaces. --- src/uu/seq/src/seq.rs | 8 ++++++++ tests/by-util/test_seq.rs | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/uu/seq/src/seq.rs b/src/uu/seq/src/seq.rs index f28b4d6e8..75e9b1598 100644 --- a/src/uu/seq/src/seq.rs +++ b/src/uu/seq/src/seq.rs @@ -235,6 +235,14 @@ fn write_value_float( width = if width > 0 { width - 1 } else { width }, precision = precision, ) + } else if *value == ExtendedBigDecimal::Infinity || *value == ExtendedBigDecimal::MinusInfinity + { + format!( + "{value:>width$.precision$}", + value = value, + width = width, + precision = precision, + ) } else { format!( "{value:>0width$.precision$}", diff --git a/tests/by-util/test_seq.rs b/tests/by-util/test_seq.rs index 2a2e31f83..312707753 100644 --- a/tests/by-util/test_seq.rs +++ b/tests/by-util/test_seq.rs @@ -530,6 +530,22 @@ fn test_inf() { run(&["inf"], b"1\n2\n3\n"); } +#[test] +fn test_inf_width() { + run( + &["-w", "1.000", "inf", "inf"], + b"1.000\n inf\n inf\n inf\n", + ); +} + +#[test] +fn test_neg_inf_width() { + run( + &["-w", "1.000", "-inf", "-inf"], + b"1.000\n -inf\n -inf\n -inf\n", + ); +} + #[test] fn test_ignore_leading_whitespace() { new_ucmd!()