@@ -61,16 +61,16 @@ unsafe trait GenericRadix: Sized {
61
61
let zero = T :: zero ( ) ;
62
62
let is_nonnegative = x >= zero;
63
63
let mut buf = [ MaybeUninit :: < u8 > :: uninit ( ) ; 128 ] ;
64
- let mut curr = buf. len ( ) ;
64
+ let mut offset = buf. len ( ) ;
65
65
let base = T :: from_u8 ( Self :: BASE ) ;
66
66
if is_nonnegative {
67
67
// Accumulate each digit of the number from the least significant
68
68
// to the most significant figure.
69
69
loop {
70
70
let n = x % base; // Get the current place value.
71
71
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.
74
74
if x == zero {
75
75
// No more digits left to accumulate.
76
76
break ;
@@ -81,27 +81,17 @@ unsafe trait GenericRadix: Sized {
81
81
loop {
82
82
let n = zero - ( x % base) ; // Get the current place value.
83
83
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.
86
86
if x == zero {
87
87
// No more digits left to accumulate.
88
88
break ;
89
89
} ;
90
90
}
91
91
}
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)
105
95
}
106
96
}
107
97
0 commit comments