diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 6da32df57efb7..f7f4d12843463 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -113,6 +113,7 @@ #![feature(fmt_internals)] #![feature(fn_traits)] #![feature(inplace_iteration)] +#![feature(int_log)] #![feature(iter_advance_by)] #![feature(layout_for_ptr)] #![feature(maybe_uninit_slice)] diff --git a/library/core/src/fmt/num.rs b/library/core/src/fmt/num.rs index 05ca50a97a644..caaa594f0642a 100644 --- a/library/core/src/fmt/num.rs +++ b/library/core/src/fmt/num.rs @@ -309,18 +309,13 @@ macro_rules! impl_Exp { let (added_precision, subtracted_precision) = match f.precision() { Some(fmt_prec) => { // number of decimal digits minus 1 - let mut tmp = n; - let mut prec = 0; - while tmp >= 10 { - tmp /= 10; - prec += 1; - } + let prec = n.checked_log10().unwrap_or(0) as usize; (fmt_prec.saturating_sub(prec), prec.saturating_sub(fmt_prec)) } - None => (0,0) + None => (0, 0) }; for _ in 1..subtracted_precision { - n/=10; + n /= 10; exponent += 1; } if subtracted_precision != 0 { @@ -392,7 +387,7 @@ macro_rules! impl_Exp { // SAFETY: In either case, `exp_buf` is written within bounds and `exp_ptr[..len]` // is contained within `exp_buf` since `len <= 3`. let exp_slice = unsafe { - *exp_ptr.offset(0) = if upper {b'E'} else {b'e'}; + *exp_ptr.offset(0) = if upper { b'E' } else { b'e' }; let len = if exponent < 10 { *exp_ptr.offset(1) = (exponent as u8) + b'0'; 2 @@ -482,7 +477,8 @@ impl_Exp!(i128, u128 as u128 via to_u128 named exp_u128); fn parse_u64_into(mut n: u64, buf: &mut [MaybeUninit; N], curr: &mut isize) { let buf_ptr = MaybeUninit::slice_as_mut_ptr(buf); let lut_ptr = DEC_DIGITS_LUT.as_ptr(); - assert!(*curr > 19); + const MIN_SIZE: isize = u64::MAX.log10() as isize; + assert!(*curr > MIN_SIZE); // SAFETY: // Writes at most 19 characters into the buffer. Guaranteed that any ptr into LUT is at most diff --git a/src/librustdoc/html/sources.rs b/src/librustdoc/html/sources.rs index bae04f2095a3d..85cf17e46950d 100644 --- a/src/librustdoc/html/sources.rs +++ b/src/librustdoc/html/sources.rs @@ -273,11 +273,7 @@ crate fn print_src( let lines = s.lines().count(); let mut line_numbers = Buffer::empty_from(buf); let mut cols = 0; - let mut tmp = lines; - while tmp > 0 { - cols += 1; - tmp /= 10; - } + cols += lines.checked_log10().map(|cols| cols + 1).unwrap_or(0) as usize; line_numbers.write_str("
");
     match source_context {
         SourceContext::Standalone => {
diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs
index 7eff725989cc9..8e18084139e21 100644
--- a/src/librustdoc/lib.rs
+++ b/src/librustdoc/lib.rs
@@ -19,6 +19,7 @@
 #![feature(iter_intersperse)]
 #![feature(type_alias_impl_trait)]
 #![feature(generic_associated_types)]
+#![feature(int_log)]
 #![recursion_limit = "256"]
 #![warn(rustc::internal)]
 #![allow(clippy::collapsible_if, clippy::collapsible_else_if)]