1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

Merge pull request #5245 from cakebaker/wc_ilog10

wc: use Rust's ilog10(), remove custom ilog10 fn
This commit is contained in:
Sylvestre Ledru 2023-09-05 09:51:13 +02:00 committed by GitHub
commit ce6be522af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -702,7 +702,7 @@ fn compute_number_width(inputs: &Inputs, settings: &Settings) -> usize {
if total == 0 {
minimum_width
} else {
let total_width = (1 + ilog10_u64(total))
let total_width = (1 + total.ilog10())
.try_into()
.expect("ilog of a u64 should fit into a usize");
max(total_width, minimum_width)
@ -857,29 +857,3 @@ fn print_stats(
writeln!(stdout)
}
}
// TODO: remove and just use usize::ilog10 once the MSRV is >= 1.67.
fn ilog10_u64(mut u: u64) -> u32 {
if u == 0 {
panic!("cannot compute log of 0")
}
let mut log = 0;
if u >= 10_000_000_000 {
log += 10;
u /= 10_000_000_000;
}
if u >= 100_000 {
log += 5;
u /= 100_000;
}
// Rust's standard library in versions >= 1.67 does something even more clever than this, but
// this should work just fine for the time being.
log + match u {
1..=9 => 0,
10..=99 => 1,
100..=999 => 2,
1000..=9999 => 3,
10000..=99999 => 4,
_ => unreachable!(),
}
}