Skip to content

Commit 1009c0d

Browse files
committed
use log10 for improvements
1 parent e08d569 commit 1009c0d

File tree

4 files changed

+9
-15
lines changed

4 files changed

+9
-15
lines changed

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
#![feature(fmt_internals)]
114114
#![feature(fn_traits)]
115115
#![feature(inplace_iteration)]
116+
#![feature(int_log)]
116117
#![feature(iter_advance_by)]
117118
#![feature(layout_for_ptr)]
118119
#![feature(maybe_uninit_slice)]

library/core/src/fmt/num.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -309,18 +309,13 @@ macro_rules! impl_Exp {
309309
let (added_precision, subtracted_precision) = match f.precision() {
310310
Some(fmt_prec) => {
311311
// number of decimal digits minus 1
312-
let mut tmp = n;
313-
let mut prec = 0;
314-
while tmp >= 10 {
315-
tmp /= 10;
316-
prec += 1;
317-
}
312+
let prec = n.checked_log10().unwrap_or(0) as usize;
318313
(fmt_prec.saturating_sub(prec), prec.saturating_sub(fmt_prec))
319314
}
320-
None => (0,0)
315+
None => (0, 0)
321316
};
322317
for _ in 1..subtracted_precision {
323-
n/=10;
318+
n /= 10;
324319
exponent += 1;
325320
}
326321
if subtracted_precision != 0 {
@@ -392,7 +387,7 @@ macro_rules! impl_Exp {
392387
// SAFETY: In either case, `exp_buf` is written within bounds and `exp_ptr[..len]`
393388
// is contained within `exp_buf` since `len <= 3`.
394389
let exp_slice = unsafe {
395-
*exp_ptr.offset(0) = if upper {b'E'} else {b'e'};
390+
*exp_ptr.offset(0) = if upper { b'E' } else { b'e' };
396391
let len = if exponent < 10 {
397392
*exp_ptr.offset(1) = (exponent as u8) + b'0';
398393
2
@@ -482,7 +477,8 @@ impl_Exp!(i128, u128 as u128 via to_u128 named exp_u128);
482477
fn parse_u64_into<const N: usize>(mut n: u64, buf: &mut [MaybeUninit<u8>; N], curr: &mut isize) {
483478
let buf_ptr = MaybeUninit::slice_as_mut_ptr(buf);
484479
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
485-
assert!(*curr > 19);
480+
const MIN_SIZE: isize = u64::MAX.log10() as isize;
481+
assert!(*curr > MIN_SIZE);
486482

487483
// SAFETY:
488484
// Writes at most 19 characters into the buffer. Guaranteed that any ptr into LUT is at most

src/librustdoc/html/sources.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,7 @@ crate fn print_src(
273273
let lines = s.lines().count();
274274
let mut line_numbers = Buffer::empty_from(buf);
275275
let mut cols = 0;
276-
let mut tmp = lines;
277-
while tmp > 0 {
278-
cols += 1;
279-
tmp /= 10;
280-
}
276+
cols += lines.checked_log10().map(|cols| cols + 1).unwrap_or(0) as usize;
281277
line_numbers.write_str("<pre class=\"line-numbers\">");
282278
match source_context {
283279
SourceContext::Standalone => {

src/librustdoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#![feature(iter_intersperse)]
2020
#![feature(type_alias_impl_trait)]
2121
#![feature(generic_associated_types)]
22+
#![feature(int_log)]
2223
#![recursion_limit = "256"]
2324
#![warn(rustc::internal)]
2425
#![allow(clippy::collapsible_if, clippy::collapsible_else_if)]

0 commit comments

Comments
 (0)