File tree Expand file tree Collapse file tree 1 file changed +6
-7
lines changed Expand file tree Collapse file tree 1 file changed +6
-7
lines changed Original file line number Diff line number Diff line change 1
1
//! impl char {}
2
2
3
+ use crate :: intrinsics:: likely;
3
4
use crate :: slice;
4
5
use crate :: str:: from_utf8_unchecked_mut;
5
6
use crate :: unicode:: printable:: is_printable;
@@ -330,16 +331,14 @@ impl char {
330
331
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
331
332
#[ inline]
332
333
pub fn to_digit ( self , radix : u32 ) -> Option < u32 > {
334
+ assert ! ( radix <= 36 , "to_digit: radix is too high (maximum 36)" ) ;
335
+ const ASCII_DIGIT_MASK : u32 = 0b11_0000 ;
333
336
// the code is split up here to improve execution speed for cases where
334
337
// the `radix` is constant and 10 or smaller
335
- let val = if radix <= 10 {
336
- match self {
337
- '0' ..='9' => self as u32 - '0' as u32 ,
338
- _ => return None ,
339
- }
338
+ let val = if likely ( radix <= 10 ) {
339
+ // If not a digit, a number greater than radix will be created.
340
+ self as u32 ^ ASCII_DIGIT_MASK
340
341
} else {
341
- assert ! ( radix <= 36 , "to_digit: radix is too high (maximum 36)" ) ;
342
-
343
342
match self {
344
343
'0' ..='9' => self as u32 - '0' as u32 ,
345
344
'a' ..='z' => self as u32 - 'a' as u32 + 10 ,
You can’t perform that action at this time.
0 commit comments