mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37: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:
parent
bfb1327ad4
commit
7ea2bfbe26
2 changed files with 17 additions and 9 deletions
|
@ -317,6 +317,7 @@ fn print_seq(
|
||||||
let mut i = 0isize;
|
let mut i = 0isize;
|
||||||
let is_first_minus_zero = first == -0.0 && first.is_sign_negative();
|
let is_first_minus_zero = first == -0.0 && first.is_sign_negative();
|
||||||
let mut value = first + i as f64 * increment;
|
let mut value = first + i as f64 * increment;
|
||||||
|
let padding = if pad { padding + 1 + largest_dec } else { 0 };
|
||||||
let mut is_first_iteration = true;
|
let mut is_first_iteration = true;
|
||||||
while !done_printing(&value, &increment, &last) {
|
while !done_printing(&value, &increment, &last) {
|
||||||
if !is_first_iteration {
|
if !is_first_iteration {
|
||||||
|
@ -328,15 +329,13 @@ fn print_seq(
|
||||||
width -= 1;
|
width -= 1;
|
||||||
}
|
}
|
||||||
is_first_iteration = false;
|
is_first_iteration = false;
|
||||||
let istr = format!("{:.*}", largest_dec, value);
|
write!(
|
||||||
let ilen = istr.len();
|
stdout,
|
||||||
let before_dec = istr.find('.').unwrap_or(ilen);
|
"{value:>0width$.precision$}",
|
||||||
if pad && before_dec < width {
|
value = value,
|
||||||
for _ in 0..(width - before_dec) {
|
width = width,
|
||||||
write!(stdout, "0")?;
|
precision = largest_dec,
|
||||||
}
|
)?;
|
||||||
}
|
|
||||||
write!(stdout, "{}", istr)?;
|
|
||||||
i += 1;
|
i += 1;
|
||||||
value = first + i as f64 * increment;
|
value = first + i as f64 * increment;
|
||||||
}
|
}
|
||||||
|
|
|
@ -426,6 +426,15 @@ fn test_width_decimal_scientific_notation_trailing_zeros_end() {
|
||||||
.no_stderr();
|
.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.
|
// TODO This is duplicated from `test_yes.rs`; refactor them.
|
||||||
/// Run `seq`, capture some of the output, close the pipe, and verify it.
|
/// Run `seq`, capture some of the output, close the pipe, and verify it.
|
||||||
fn run(args: &[&str], expected: &[u8]) {
|
fn run(args: &[&str], expected: &[u8]) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue