diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index 224bc9effe61e..015c4b041b53e 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -1510,7 +1510,9 @@ impl char { #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")] #[inline] pub const fn is_ascii_hexdigit(&self) -> bool { - matches!(*self, '0'..='9' | 'A'..='F' | 'a'..='f') + // Bitwise or converts A-Z to a-z, avoiding need for branches in compiled code. + ((*self as u32).wrapping_sub('0' as u32) < 10) + | ((*self as u32 | 0x20).wrapping_sub('a' as u32) < 6) } /// Checks if the value is an ASCII punctuation character: diff --git a/library/core/src/num/mod.rs b/library/core/src/num/mod.rs index 311c5fa5b6834..82731fd9436c0 100644 --- a/library/core/src/num/mod.rs +++ b/library/core/src/num/mod.rs @@ -688,7 +688,8 @@ impl u8 { #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")] #[inline] pub const fn is_ascii_hexdigit(&self) -> bool { - matches!(*self, b'0'..=b'9' | b'A'..=b'F' | b'a'..=b'f') + // Bitwise or converts A-Z to a-z, avoiding need for branches in compiled code. + (self.wrapping_sub(b'0') < 10) | ((*self | 0x20).wrapping_sub(b'a') < 6) } /// Checks if the value is an ASCII punctuation character: