Skip to content

Commit

Permalink
seq: compute correct width for -0e0
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jfinkels committed Sep 18, 2021
1 parent 88a6890 commit 426c7ca
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/uu/seq/src/seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?;
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/by-util/test_seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down

0 comments on commit 426c7ca

Please sign in to comment.