@@ -565,11 +565,12 @@ mod imp {
565
565
}
566
566
impl_Exp ! ( i128 , u128 as u128 via to_u128 named exp_u128) ;
567
567
568
+ const U128_MAX_DEC_N : usize = u128:: MAX . ilog ( 10 ) as usize + 1 ;
569
+
568
570
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
569
571
impl fmt:: Display for u128 {
570
572
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 ] ;
573
574
574
575
f. pad_integral ( true , "" , self . _fmt ( & mut buf) )
575
576
}
@@ -579,9 +580,8 @@ impl fmt::Display for u128 {
579
580
impl fmt:: Display for i128 {
580
581
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
581
582
// 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 ] ;
585
585
586
586
let is_nonnegative = * self >= 0 ;
587
587
f. pad_integral ( is_nonnegative, "" , self . unsigned_abs ( ) . _fmt ( & mut buf) )
@@ -598,8 +598,6 @@ impl u128 {
598
598
issue = "none"
599
599
) ]
600
600
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
-
603
601
// Optimize common-case zero, which would also need special treatment due to
604
602
// its "leading" zero.
605
603
if self == 0 {
@@ -609,26 +607,26 @@ impl u128 {
609
607
// Take the 16 least-significant decimals.
610
608
let ( quot_1e16, mod_1e16) = div_rem_1e16 ( self ) ;
611
609
let ( mut remain, mut offset) = if quot_1e16 == 0 {
612
- ( mod_1e16, MAX_DEC_N )
610
+ ( mod_1e16, U128_MAX_DEC_N )
613
611
} else {
614
612
// 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) ;
616
614
617
615
// Take another 16 decimals.
618
616
let ( quot2, mod2) = div_rem_1e16 ( quot_1e16) ;
619
617
if quot2 == 0 {
620
- ( mod2, MAX_DEC_N - 16 )
618
+ ( mod2, U128_MAX_DEC_N - 16 )
621
619
} else {
622
620
// 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) ;
624
622
// 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 )
626
624
}
627
625
} ;
628
626
629
627
// Format per four digits from the lookup table.
630
628
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
632
630
// and the while condition ensures at least 4 more decimals.
633
631
unsafe { core:: hint:: assert_unchecked ( offset >= 4 ) }
634
632
// SAFETY: The offset counts down from its initial buf.len()
@@ -649,7 +647,7 @@ impl u128 {
649
647
650
648
// Format per two digits from the lookup table.
651
649
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
653
651
// and the if condition ensures at least 2 more decimals.
654
652
unsafe { core:: hint:: assert_unchecked ( offset >= 2 ) }
655
653
// SAFETY: The offset counts down from its initial buf.len()
@@ -665,7 +663,7 @@ impl u128 {
665
663
666
664
// Format the last remaining digit, if any.
667
665
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
669
667
// and the if condition ensures (at least) 1 more decimals.
670
668
unsafe { core:: hint:: assert_unchecked ( offset >= 1 ) }
671
669
// SAFETY: The offset counts down from its initial buf.len()
0 commit comments