Skip to content

Commit d25bc41

Browse files
committed
seq: compute correct width for -0e0
1 parent 5c97c1c commit d25bc41

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/uu/seq/src/seq.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,22 @@ fn print_seq(
286286
let mut stdout = stdout.lock();
287287
let (first, increment, last) = range;
288288
let mut i = 0isize;
289+
let is_first_minus_zero = first == -0.0 && first.is_sign_negative();
289290
let mut value = first + i as f64 * increment;
291+
let mut is_first_iteration = true;
290292
while !done_printing(&value, &increment, &last) {
293+
let mut width = padding;
294+
if is_first_iteration && is_first_minus_zero {
295+
write!(stdout, "-");
296+
width -= 1;
297+
}
298+
is_first_iteration = false;
299+
291300
let istr = format!("{:.*}", largest_dec, value);
292301
let ilen = istr.len();
293302
let before_dec = istr.find('.').unwrap_or(ilen);
294-
if pad && before_dec < padding {
295-
for _ in 0..(padding - before_dec) {
303+
if pad && before_dec < width {
304+
for _ in 0..(width - before_dec) {
296305
write!(stdout, "0")?;
297306
}
298307
}

tests/by-util/test_seq.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@ fn test_width_negative_zero() {
178178
.no_stderr();
179179
}
180180

181+
#[test]
182+
fn test_width_negative_zero_scientific_notation() {
183+
new_ucmd!()
184+
.args(&["-w", "-0e0", "1"])
185+
.succeeds()
186+
.stdout_is("-0\n01\n")
187+
.no_stderr();
188+
}
189+
181190
// TODO This is duplicated from `test_yes.rs`; refactor them.
182191
/// Run `seq`, capture some of the output, close the pipe, and verify it.
183192
fn run(args: &[&str], expected: &[u8]) {

0 commit comments

Comments
 (0)