about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2025-06-06 23:53:20 +0200
committerGitHub <noreply@github.com>2025-06-06 23:53:20 +0200
commit6bbef981aa0bed103f1173a539c7aacefbb4df00 (patch)
treea33e99fbeb001d605557c973e78808ab901f74e2
parentb6ace054b813a700a34b97af00b21c61ce61f8aa (diff)
parent28a2e1e9c9e03b9b640fd771a58de6c2e1c3ff21 (diff)
downloadrust-6bbef981aa0bed103f1173a539c7aacefbb4df00.tar.gz
rust-6bbef981aa0bed103f1173a539c7aacefbb4df00.zip
Rollup merge of #142114 - GuillaumeGomez:u128-const, r=Urgau
Compute number of digits instead of relying on constant value for u128 display code

As discussed in https://github.com/rust-lang/rust/pull/142098/files#r2132084991, the code should reuse the same logic as the rest of file instead of using a constant value.

r? `@tamird`
-rw-r--r--library/core/src/fmt/num.rs4
1 files changed, 2 insertions, 2 deletions
diff --git a/library/core/src/fmt/num.rs b/library/core/src/fmt/num.rs
index ba30518d70b..dd9c379b666 100644
--- a/library/core/src/fmt/num.rs
+++ b/library/core/src/fmt/num.rs
@@ -678,8 +678,8 @@ impl fmt::Display for i128 {
 /// It also has to handle 1 last item, as 10^40 > 2^128 > 10^39, whereas
 /// 10^20 > 2^64 > 10^19.
 fn fmt_u128(n: u128, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-    // 2^128 is about 3*10^38, so 39 gives an extra byte of space
-    let mut buf = [MaybeUninit::<u8>::uninit(); 39];
+    const MAX_DEC_N: usize = u128::MAX.ilog(10) as usize + 1;
+    let mut buf = [MaybeUninit::<u8>::uninit(); MAX_DEC_N];
     let mut curr = buf.len();
 
     let (n, rem) = udiv_1e19(n);