Unify way to flip 6th bit. (Same assembly generated)

This commit is contained in:
Giles Cope 2021-02-08 12:21:36 +00:00
parent f30c51abe8
commit cadcf5ed99
No known key found for this signature in database
GPG Key ID: DF85161DAE0FF36B
4 changed files with 11 additions and 8 deletions

View File

@ -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
}
}

View File

@ -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.

View File

@ -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.

View File

@ -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,