@@ -61,16 +61,16 @@ unsafe trait GenericRadix: Sized {
6161 let zero = T :: zero ( ) ;
6262 let is_nonnegative = x >= zero;
6363 let mut buf = [ MaybeUninit :: < u8 > :: uninit ( ) ; 128 ] ;
64- let mut curr = buf. len ( ) ;
64+ let mut offset = buf. len ( ) ;
6565 let base = T :: from_u8 ( Self :: BASE ) ;
6666 if is_nonnegative {
6767 // Accumulate each digit of the number from the least significant
6868 // to the most significant figure.
6969 loop {
7070 let n = x % base; // Get the current place value.
7171 x = x / base; // Deaccumulate the number.
72- curr -= 1 ;
73- buf[ curr ] . write ( Self :: digit ( n. to_u8 ( ) ) ) ; // Store the digit in the buffer.
72+ offset -= 1 ;
73+ buf[ offset ] . write ( Self :: digit ( n. to_u8 ( ) ) ) ; // Store the digit in the buffer.
7474 if x == zero {
7575 // No more digits left to accumulate.
7676 break ;
@@ -81,27 +81,17 @@ unsafe trait GenericRadix: Sized {
8181 loop {
8282 let n = zero - ( x % base) ; // Get the current place value.
8383 x = x / base; // Deaccumulate the number.
84- curr -= 1 ;
85- buf[ curr ] . write ( Self :: digit ( n. to_u8 ( ) ) ) ; // Store the digit in the buffer.
84+ offset -= 1 ;
85+ buf[ offset ] . write ( Self :: digit ( n. to_u8 ( ) ) ) ; // Store the digit in the buffer.
8686 if x == zero {
8787 // No more digits left to accumulate.
8888 break ;
8989 } ;
9090 }
9191 }
92- // SAFETY: `curr` is initialized to `buf.len()` and is only decremented, so it can't overflow. It is
93- // decremented exactly once for each digit. Since u128 is the widest fixed width integer format supported,
94- // the maximum number of digits (bits) is 128 for base-2, so `curr` won't underflow as well.
95- let buf = unsafe { buf. get_unchecked ( curr..) } ;
96- // SAFETY: The only chars in `buf` are created by `Self::digit` which are assumed to be
97- // valid UTF-8
98- let buf = unsafe {
99- str:: from_utf8_unchecked ( slice:: from_raw_parts (
100- MaybeUninit :: slice_as_ptr ( buf) ,
101- buf. len ( ) ,
102- ) )
103- } ;
104- f. pad_integral ( is_nonnegative, Self :: PREFIX , buf)
92+ // SAFETY: Starting from `offset`, all elements of the slice have been set.
93+ let buf_slice = unsafe { slice_buffer_to_str ( & buf, offset) } ;
94+ f. pad_integral ( is_nonnegative, Self :: PREFIX , buf_slice)
10595 }
10696}
10797
0 commit comments