Closed qsantos closed 1 year ago
Ah, the CI is targeting 1.51.0 but checked_ilog10
is from 1.67.0. Did you have something else in mind @klensy?
Patch coverage: 87.50
% and no project coverage change
Comparison is base (
74b8694
) 82.73% compared to head (28c2d6c
) 82.73%.
:umbrella: View full report at Codecov.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.
It can be something like https://stackoverflow.com/a/69298721 where you simply multiply by 10 and compare.
I have made it a fallback with some tests for edge cases. What do you think?
How about a simple match statement? https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b2fffe2e3cc3683b55cf4d0e0789795b
fn count_digits(n: u16) -> u8 {
match n {
0..=9 => 1,
10..=99 => 2,
100..=999 => 3,
1000..=9999 => 4,
10000..=65535 => 5,
}
}
They compile to the same binary! In fact, even for u32 or u64, the compiler just unrolls the loop. It does use a trick for u64 because there are no CMP instructions with an immediate 64 bit value on x86_64 (if I understand correctly).
In any case, your version looks much nicer, and it is much easier to convince oneself that it is correct, so I have pushed it.
Following https://github.com/servo/rust-url/pull/817#pullrequestreview-1325304510, it was raise that
port.to_string().len()
could be optimized to avoid allocating a string.