Skip to content

Commit aec3248

Browse files
Specialize ToString implementation on u128 and i128
1 parent 0e141a2 commit aec3248

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

library/alloc/src/string.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2870,6 +2870,7 @@ impl_to_string! {
28702870
i32, u32,
28712871
i64, u64,
28722872
isize, usize,
2873+
i128, u128,
28732874
}
28742875

28752876
#[cfg(not(no_global_oom_handling))]

library/core/src/fmt/num.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -565,11 +565,12 @@ mod imp {
565565
}
566566
impl_Exp!(i128, u128 as u128 via to_u128 named exp_u128);
567567

568+
const U128_MAX_DEC_N: usize = u128::MAX.ilog(10) as usize + 1;
569+
568570
#[stable(feature = "rust1", since = "1.0.0")]
569571
impl fmt::Display for u128 {
570572
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
571-
const MAX_DEC_N: usize = u128::MAX.ilog(10) as usize + 1;
572-
let mut buf = [MaybeUninit::<u8>::uninit(); MAX_DEC_N];
573+
let mut buf = [MaybeUninit::<u8>::uninit(); U128_MAX_DEC_N];
573574

574575
f.pad_integral(true, "", self._fmt(&mut buf))
575576
}
@@ -579,9 +580,8 @@ impl fmt::Display for u128 {
579580
impl fmt::Display for i128 {
580581
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
581582
// This is not a typo, we use the maximum number of digits of `u128`, hence why we use
582-
// `u128::MAX`.
583-
const MAX_DEC_N: usize = u128::MAX.ilog(10) as usize + 1;
584-
let mut buf = [MaybeUninit::<u8>::uninit(); MAX_DEC_N];
583+
// `U128_MAX_DEC_N`.
584+
let mut buf = [MaybeUninit::<u8>::uninit(); U128_MAX_DEC_N];
585585

586586
let is_nonnegative = *self >= 0;
587587
f.pad_integral(is_nonnegative, "", self.unsigned_abs()._fmt(&mut buf))
@@ -598,8 +598,6 @@ impl u128 {
598598
issue = "none"
599599
)]
600600
pub fn _fmt<'a>(self, buf: &'a mut [MaybeUninit<u8>]) -> &'a str {
601-
const MAX_DEC_N: usize = u128::MAX.ilog(10) as usize + 1;
602-
603601
// Optimize common-case zero, which would also need special treatment due to
604602
// its "leading" zero.
605603
if self == 0 {
@@ -609,26 +607,26 @@ impl u128 {
609607
// Take the 16 least-significant decimals.
610608
let (quot_1e16, mod_1e16) = div_rem_1e16(self);
611609
let (mut remain, mut offset) = if quot_1e16 == 0 {
612-
(mod_1e16, MAX_DEC_N)
610+
(mod_1e16, U128_MAX_DEC_N)
613611
} else {
614612
// Write digits at buf[23..39].
615-
enc_16lsd::<{ MAX_DEC_N - 16 }>(buf, mod_1e16);
613+
enc_16lsd::<{ U128_MAX_DEC_N - 16 }>(buf, mod_1e16);
616614

617615
// Take another 16 decimals.
618616
let (quot2, mod2) = div_rem_1e16(quot_1e16);
619617
if quot2 == 0 {
620-
(mod2, MAX_DEC_N - 16)
618+
(mod2, U128_MAX_DEC_N - 16)
621619
} else {
622620
// Write digits at buf[7..23].
623-
enc_16lsd::<{ MAX_DEC_N - 32 }>(buf, mod2);
621+
enc_16lsd::<{ U128_MAX_DEC_N - 32 }>(buf, mod2);
624622
// Quot2 has at most 7 decimals remaining after two 1e16 divisions.
625-
(quot2 as u64, MAX_DEC_N - 32)
623+
(quot2 as u64, U128_MAX_DEC_N - 32)
626624
}
627625
};
628626

629627
// Format per four digits from the lookup table.
630628
while remain > 999 {
631-
// SAFETY: All of the decimals fit in buf due to MAX_DEC_N
629+
// SAFETY: All of the decimals fit in buf due to U128_MAX_DEC_N
632630
// and the while condition ensures at least 4 more decimals.
633631
unsafe { core::hint::assert_unchecked(offset >= 4) }
634632
// SAFETY: The offset counts down from its initial buf.len()
@@ -649,7 +647,7 @@ impl u128 {
649647

650648
// Format per two digits from the lookup table.
651649
if remain > 9 {
652-
// SAFETY: All of the decimals fit in buf due to MAX_DEC_N
650+
// SAFETY: All of the decimals fit in buf due to U128_MAX_DEC_N
653651
// and the if condition ensures at least 2 more decimals.
654652
unsafe { core::hint::assert_unchecked(offset >= 2) }
655653
// SAFETY: The offset counts down from its initial buf.len()
@@ -665,7 +663,7 @@ impl u128 {
665663

666664
// Format the last remaining digit, if any.
667665
if remain != 0 {
668-
// SAFETY: All of the decimals fit in buf due to MAX_DEC_N
666+
// SAFETY: All of the decimals fit in buf due to U128_MAX_DEC_N
669667
// and the if condition ensures (at least) 1 more decimals.
670668
unsafe { core::hint::assert_unchecked(offset >= 1) }
671669
// SAFETY: The offset counts down from its initial buf.len()

0 commit comments

Comments
 (0)