File tree 1 file changed +8
-7
lines changed
compiler/rustc_data_structures/src
1 file changed +8
-7
lines changed Original file line number Diff line number Diff line change @@ -14,24 +14,25 @@ const BASE_64: &[u8; MAX_BASE as usize] =
14
14
15
15
#[ inline]
16
16
pub fn push_str ( mut n : u128 , base : usize , output : & mut String ) {
17
- debug_assert ! ( base >= 2 && base <= MAX_BASE ) ;
17
+ assert ! ( base >= 2 && base <= MAX_BASE ) ;
18
18
let mut s = [ 0u8 ; 128 ] ;
19
- let mut index = 0 ;
19
+ let mut first_index = 0 ;
20
20
21
21
let base = base as u128 ;
22
22
23
- loop {
24
- s [ index ] = BASE_64 [ ( n % base) as usize ] ;
25
- index += 1 ;
23
+ for idx in ( 0 .. 128 ) . rev ( ) {
24
+ // SAFETY: given `base <= MAX_BASE`, so ` n % base < MAX_BASE`
25
+ s [ idx ] = unsafe { * BASE_64 . get_unchecked ( ( n % base ) as usize ) } ;
26
26
n /= base;
27
27
28
28
if n == 0 {
29
+ first_index = idx;
29
30
break ;
30
31
}
31
32
}
32
- s[ 0 ..index] . reverse ( ) ;
33
33
34
- output. push_str ( str:: from_utf8 ( & s[ 0 ..index] ) . unwrap ( ) ) ;
34
+ // SAFETY: all chars in given range is nonnull ascii
35
+ output. push_str ( unsafe { str:: from_utf8_unchecked ( & s[ first_index..] ) } ) ;
35
36
}
36
37
37
38
#[ inline]
You can’t perform that action at this time.
0 commit comments