1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

seq: replace loops with a single format string

Replace two loops that print leading and trailing 0s when printing a
number in fixed-width mode with a single call to `write!()` with the
appropriate formatting parameters.
This commit is contained in:
Jan Verbeek 2021-09-13 21:19:49 -04:00 committed by Jeffrey Finkelstein
parent bfb1327ad4
commit 7ea2bfbe26
2 changed files with 17 additions and 9 deletions

View file

@ -317,6 +317,7 @@ fn print_seq(
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 padding = if pad { padding + 1 + largest_dec } else { 0 };
let mut is_first_iteration = true;
while !done_printing(&value, &increment, &last) {
if !is_first_iteration {
@ -328,15 +329,13 @@ fn print_seq(
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 < width {
for _ in 0..(width - before_dec) {
write!(stdout, "0")?;
}
}
write!(stdout, "{}", istr)?;
write!(
stdout,
"{value:>0width$.precision$}",
value = value,
width = width,
precision = largest_dec,
)?;
i += 1;
value = first + i as f64 * increment;
}

View file

@ -426,6 +426,15 @@ fn test_width_decimal_scientific_notation_trailing_zeros_end() {
.no_stderr();
}
#[test]
fn test_width_floats() {
new_ucmd!()
.args(&["-w", "9.0", "10.0"])
.succeeds()
.stdout_is("09.0\n10.0\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]) {