optimize char::to_digit and assert radix is at least 2

approved by t-libs: https://github.com/rust-lang/libs-team/issues/475#issuecomment-2457858458
This commit is contained in:
Jacob Lifshay 2024-11-06 12:09:15 -08:00
parent 116fc31c5c
commit aeffff8ecf
No known key found for this signature in database

View file

@ -301,7 +301,7 @@ impl char {
/// ///
/// # Panics /// # Panics
/// ///
/// Panics if given a radix larger than 36. /// Panics if given a radix smaller than 2 or larger than 36.
/// ///
/// # Examples /// # Examples
/// ///
@ -319,6 +319,13 @@ impl char {
/// // this panics /// // this panics
/// '1'.is_digit(37); /// '1'.is_digit(37);
/// ``` /// ```
///
/// Passing a small radix, causing a panic:
///
/// ```should_panic
/// // this panics
/// '1'.is_digit(1);
/// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_char_classify", issue = "132241")] #[rustc_const_unstable(feature = "const_char_classify", issue = "132241")]
#[inline] #[inline]
@ -345,7 +352,7 @@ impl char {
/// ///
/// # Panics /// # Panics
/// ///
/// Panics if given a radix larger than 36. /// Panics if given a radix smaller than 2 or larger than 36.
/// ///
/// # Examples /// # Examples
/// ///
@ -369,24 +376,35 @@ impl char {
/// // this panics /// // this panics
/// let _ = '1'.to_digit(37); /// let _ = '1'.to_digit(37);
/// ``` /// ```
/// Passing a small radix, causing a panic:
///
/// ```should_panic
/// // this panics
/// let _ = '1'.to_digit(1);
/// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const_char_convert", since = "1.67.0")] #[rustc_const_stable(feature = "const_char_convert", since = "1.67.0")]
#[must_use = "this returns the result of the operation, \ #[must_use = "this returns the result of the operation, \
without modifying the original"] without modifying the original"]
#[inline] #[inline]
pub const fn to_digit(self, radix: u32) -> Option<u32> { pub const fn to_digit(self, radix: u32) -> Option<u32> {
// If not a digit, a number greater than radix will be created. assert!(
let mut digit = (self as u32).wrapping_sub('0' as u32); radix >= 2 && radix <= 36,
if radix > 10 { "to_digit: invalid radix -- radix must be in the range 2 to 36 inclusive"
assert!(radix <= 36, "to_digit: radix is too high (maximum 36)"); );
if digit < 10 { // check radix to remove letter handling code when radix is a known constant
return Some(digit); let value = if self > '9' && radix > 10 {
} // convert ASCII letters to lowercase
// Force the 6th bit to be set to ensure ascii is lower case. let lower = self as u32 | 0x20;
digit = (self as u32 | 0b10_0000).wrapping_sub('a' as u32).saturating_add(10); // convert an ASCII letter to the corresponding value,
} // non-letters convert to values > 36
lower.wrapping_sub('a' as u32) as u64 + 10
} else {
// convert digit to value, non-digits wrap to values > 36
(self as u32).wrapping_sub('0' as u32) as u64
};
// FIXME(const-hack): once then_some is const fn, use it here // FIXME(const-hack): once then_some is const fn, use it here
if digit < radix { Some(digit) } else { None } if value < radix as u64 { Some(value as u32) } else { None }
} }
/// Returns an iterator that yields the hexadecimal Unicode escape of a /// Returns an iterator that yields the hexadecimal Unicode escape of a