From 426c7cad955af052da01d73160b8c066b815b1b7 Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Sat, 11 Sep 2021 12:49:12 -0400 Subject: [PATCH] seq: compute correct width for -0e0 Compute the correct width when an input argument is `-0e0`, that is, floating point negative zero in scientific notation. For example, $ seq -w -0e0 1 -0 01 --- src/uu/seq/src/seq.rs | 11 +++++++++-- tests/by-util/test_seq.rs | 9 +++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/uu/seq/src/seq.rs b/src/uu/seq/src/seq.rs index 0cea9083867..51ec131e279 100644 --- a/src/uu/seq/src/seq.rs +++ b/src/uu/seq/src/seq.rs @@ -286,18 +286,25 @@ fn print_seq( let mut stdout = stdout.lock(); let (first, increment, last) = range; let mut i = 0isize; + let is_first_minus_zero = first == -0.0 && first.is_sign_negative(); let mut value = first + i as f64 * increment; let mut is_first_iteration = true; while !done_printing(&value, &increment, &last) { if !is_first_iteration { write!(stdout, "{}", separator)?; } + let mut width = padding; + if is_first_iteration && is_first_minus_zero { + write!(stdout, "-")?; + width -= 1; + } is_first_iteration = false; + let istr = format!("{:.*}", largest_dec, value); let ilen = istr.len(); let before_dec = istr.find('.').unwrap_or(ilen); - if pad && before_dec < padding { - for _ in 0..(padding - before_dec) { + if pad && before_dec < width { + for _ in 0..(width - before_dec) { write!(stdout, "0")?; } } diff --git a/tests/by-util/test_seq.rs b/tests/by-util/test_seq.rs index 65f2451f009..9acb9233f43 100644 --- a/tests/by-util/test_seq.rs +++ b/tests/by-util/test_seq.rs @@ -178,6 +178,15 @@ fn test_width_negative_zero() { .no_stderr(); } +#[test] +fn test_width_negative_zero_scientific_notation() { + new_ucmd!() + .args(&["-w", "-0e0", "1"]) + .succeeds() + .stdout_is("-0\n01\n") + .no_stderr(); +} + // TODO This is duplicated from `test_yes.rs`; refactor them. /// Run `seq`, capture some of the output, close the pipe, and verify it. fn run(args: &[&str], expected: &[u8]) {