mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-18 02:34:37 +00:00
Further simplification of to_digit
This commit is contained in:
parent
b3ac52646f
commit
9c3d81e186
@ -1,6 +1,5 @@
|
||||
//! impl char {}
|
||||
|
||||
use crate::intrinsics::likely;
|
||||
use crate::slice;
|
||||
use crate::str::from_utf8_unchecked_mut;
|
||||
use crate::unicode::printable::is_printable;
|
||||
@ -332,21 +331,16 @@ impl char {
|
||||
#[inline]
|
||||
pub fn to_digit(self, radix: u32) -> Option<u32> {
|
||||
assert!(radix <= 36, "to_digit: radix is too high (maximum 36)");
|
||||
// the code is split up here to improve execution speed for cases where
|
||||
// the `radix` is constant and 10 or smaller
|
||||
let val = if likely(radix <= 10) {
|
||||
// If not a digit, a number greater than radix will be created.
|
||||
(self as u32).wrapping_sub('0' as u32)
|
||||
} else {
|
||||
match self {
|
||||
'0'..='9' => self as u32 - '0' as u32,
|
||||
'a'..='z' => self as u32 - 'a' as u32 + 10,
|
||||
'A'..='Z' => self as u32 - 'A' as u32 + 10,
|
||||
_ => return None,
|
||||
// If not a digit, a number greater than radix will be created.
|
||||
let mut digit = (self as u32).wrapping_sub('0' as u32);
|
||||
if radix > 10 {
|
||||
if digit < 10 {
|
||||
return Some(digit);
|
||||
}
|
||||
};
|
||||
|
||||
if val < radix { Some(val) } else { None }
|
||||
// Force the 6th bit to be set to ensure ascii is lower case.
|
||||
digit = (self as u32 | 0b10_0000).wrapping_sub('a' as u32).saturating_add(10);
|
||||
}
|
||||
(digit < radix).then_some(digit)
|
||||
}
|
||||
|
||||
/// Returns an iterator that yields the hexadecimal Unicode escape of a
|
||||
|
@ -66,6 +66,7 @@
|
||||
#![feature(allow_internal_unstable)]
|
||||
#![feature(arbitrary_self_types)]
|
||||
#![feature(asm)]
|
||||
#![feature(bool_to_option)]
|
||||
#![feature(cfg_target_has_atomic)]
|
||||
#![feature(const_heap)]
|
||||
#![feature(const_alloc_layout)]
|
||||
|
@ -67,10 +67,20 @@ fn test_to_digit() {
|
||||
assert_eq!('A'.to_digit(16), Some(10));
|
||||
assert_eq!('b'.to_digit(16), Some(11));
|
||||
assert_eq!('B'.to_digit(16), Some(11));
|
||||
assert_eq!('A'.to_digit(36), Some(10));
|
||||
assert_eq!('z'.to_digit(36), Some(35));
|
||||
assert_eq!('Z'.to_digit(36), Some(35));
|
||||
assert_eq!(' '.to_digit(10), None);
|
||||
assert_eq!('['.to_digit(36), None);
|
||||
assert_eq!('`'.to_digit(36), None);
|
||||
assert_eq!('{'.to_digit(36), None);
|
||||
assert_eq!('$'.to_digit(36), None);
|
||||
assert_eq!('@'.to_digit(16), None);
|
||||
assert_eq!('G'.to_digit(16), None);
|
||||
assert_eq!('g'.to_digit(16), None);
|
||||
assert_eq!(' '.to_digit(10), None);
|
||||
assert_eq!('/'.to_digit(10), None);
|
||||
assert_eq!(':'.to_digit(10), None);
|
||||
assert_eq!(':'.to_digit(11), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Loading…
Reference in New Issue
Block a user