diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index bbdb2a5d41b..3ddf0e63894 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -3,7 +3,7 @@ use crate::slice; use crate::str::from_utf8_unchecked_mut; use crate::unicode::printable::is_printable; -use crate::unicode::{self, conversions, ASCII_CASE_MASK}; +use crate::unicode::{self, conversions}; use super::*; @@ -1090,7 +1090,11 @@ impl char { #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] #[inline] pub fn to_ascii_uppercase(&self) -> char { - if self.is_ascii_lowercase() { ((*self as u8) & !ASCII_CASE_MASK) as char } else { *self } + if self.is_ascii_lowercase() { + (*self as u8).ascii_change_case_unchecked() as char + } else { + *self + } } /// Makes a copy of the value in its ASCII lower case equivalent. @@ -1118,7 +1122,11 @@ impl char { #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] #[inline] pub fn to_ascii_lowercase(&self) -> char { - if self.is_ascii_uppercase() { ((*self as u8) | ASCII_CASE_MASK) as char } else { *self } + if self.is_ascii_uppercase() { + (*self as u8).ascii_change_case_unchecked() as char + } else { + *self + } } /// Checks that two values are an ASCII case-insensitive match. diff --git a/library/core/src/num/mod.rs b/library/core/src/num/mod.rs index 7563a742b9a..42ccdd00bbd 100644 --- a/library/core/src/num/mod.rs +++ b/library/core/src/num/mod.rs @@ -5,7 +5,9 @@ use crate::intrinsics; use crate::mem; use crate::str::FromStr; -use crate::unicode::ASCII_CASE_MASK; + +/// If 6th bit set ascii is upper case. +const ASCII_CASE_MASK: u8 = 0b0010_0000; // Used because the `?` operator is not allowed in a const context. macro_rules! try_opt { @@ -222,6 +224,12 @@ impl u8 { *self | (self.is_ascii_uppercase() as u8 * ASCII_CASE_MASK) } + /// Assumes self is ascii + #[inline] + pub(crate) fn ascii_change_case_unchecked(&self) -> u8 { + *self ^ ASCII_CASE_MASK + } + /// Checks that two values are an ASCII case-insensitive match. /// /// This is equivalent to `to_ascii_lowercase(a) == to_ascii_lowercase(b)`. diff --git a/library/core/src/unicode/mod.rs b/library/core/src/unicode/mod.rs index b333b463105..37ca0a0779b 100644 --- a/library/core/src/unicode/mod.rs +++ b/library/core/src/unicode/mod.rs @@ -17,9 +17,6 @@ mod unicode_data; #[stable(feature = "unicode_version", since = "1.45.0")] pub const UNICODE_VERSION: (u8, u8, u8) = unicode_data::UNICODE_VERSION; -/// If 6th bit set ascii is upper case. -pub(crate) const ASCII_CASE_MASK: u8 = 0b0010_0000; - // For use in liballoc, not re-exported in libstd. pub use unicode_data::{ case_ignorable::lookup as Case_Ignorable, cased::lookup as Cased, conversions,