1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

seq: add is_first_iteration to avoid comparisons

Add the `is_first_iteration` Boolean variable to the `print_seq()`
function in order to avoid unnecessary comparisons. Specifically, before
this change, the `done_printing()` function was called twice on each
iteration of the main loop. After this change, it is only called once
per iteration.

Furthermore, this change makes the `print_seq()` function similar in
structure to the `print_seq_integers()` function.

Co-authored-by: Jan Verbeek <jan.verbeek@posteo.nl>
This commit is contained in:
Jeffrey Finkelstein 2021-09-14 20:54:31 -04:00
parent 5c97c1ccc4
commit 53a91be2df

View file

@ -287,7 +287,12 @@ fn print_seq(
let (first, increment, last) = range;
let mut i = 0isize;
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)?;
}
is_first_iteration = false;
let istr = format!("{:.*}", largest_dec, value);
let ilen = istr.len();
let before_dec = istr.find('.').unwrap_or(ilen);
@ -299,11 +304,8 @@ fn print_seq(
write!(stdout, "{}", istr)?;
i += 1;
value = first + i as f64 * increment;
if !done_printing(&value, &increment, &last) {
write!(stdout, "{}", separator)?;
}
}
if (first >= last && increment < 0f64) || (first <= last && increment > 0f64) {
if !is_first_iteration {
write!(stdout, "{}", terminator)?;
}
stdout.flush()?;