From cadcf5ed990dc02ad86cbb9f31423959a5517f50 Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Mon, 8 Feb 2021 12:21:36 +0000 Subject: [PATCH] Unify way to flip 6th bit. (Same assembly generated) --- library/core/benches/ascii.rs | 6 ++++-- library/core/src/char/methods.rs | 5 +---- library/core/src/num/mod.rs | 5 +++-- library/core/src/unicode/mod.rs | 3 +++ 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/library/core/benches/ascii.rs b/library/core/benches/ascii.rs index bc59c378609..64938745a4a 100644 --- a/library/core/benches/ascii.rs +++ b/library/core/benches/ascii.rs @@ -66,6 +66,8 @@ macro_rules! benches { use test::black_box; use test::Bencher; +const ASCII_CASE_MASK: u8 = 0b0010_0000; + benches! { fn case00_alloc_only(_bytes: &mut [u8]) {} @@ -204,7 +206,7 @@ benches! { } } for byte in bytes { - *byte &= !((is_ascii_lowercase(*byte) as u8) << 5) + *byte &= !((is_ascii_lowercase(*byte) as u8) * ASCII_CASE_MASK) } } @@ -216,7 +218,7 @@ benches! { } } for byte in bytes { - *byte -= (is_ascii_lowercase(*byte) as u8) << 5 + *byte -= (is_ascii_lowercase(*byte) as u8) * ASCII_CASE_MASK } } diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index 4032e777077..bbdb2a5d41b 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -3,13 +3,10 @@ use crate::slice; use crate::str::from_utf8_unchecked_mut; use crate::unicode::printable::is_printable; -use crate::unicode::{self, conversions}; +use crate::unicode::{self, conversions, ASCII_CASE_MASK}; use super::*; -/// If 6th bit set ascii is upper case. -const ASCII_CASE_MASK: u8 = 0b10_0000u8; - #[lang = "char"] impl char { /// The highest valid code point a `char` can have. diff --git a/library/core/src/num/mod.rs b/library/core/src/num/mod.rs index 6bdfa18fa43..7563a742b9a 100644 --- a/library/core/src/num/mod.rs +++ b/library/core/src/num/mod.rs @@ -5,6 +5,7 @@ use crate::intrinsics; use crate::mem; use crate::str::FromStr; +use crate::unicode::ASCII_CASE_MASK; // Used because the `?` operator is not allowed in a const context. macro_rules! try_opt { @@ -195,7 +196,7 @@ impl u8 { #[inline] pub fn to_ascii_uppercase(&self) -> u8 { // Unset the fifth bit if this is a lowercase letter - *self & !((self.is_ascii_lowercase() as u8) << 5) + *self & !((self.is_ascii_lowercase() as u8) * ASCII_CASE_MASK) } /// Makes a copy of the value in its ASCII lower case equivalent. @@ -218,7 +219,7 @@ impl u8 { #[inline] pub fn to_ascii_lowercase(&self) -> u8 { // Set the fifth bit if this is an uppercase letter - *self | ((self.is_ascii_uppercase() as u8) << 5) + *self | (self.is_ascii_uppercase() as u8 * ASCII_CASE_MASK) } /// Checks that two values are an ASCII case-insensitive match. diff --git a/library/core/src/unicode/mod.rs b/library/core/src/unicode/mod.rs index 37ca0a0779b..b333b463105 100644 --- a/library/core/src/unicode/mod.rs +++ b/library/core/src/unicode/mod.rs @@ -17,6 +17,9 @@ 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,