Skip to content

Commit

Permalink
Unify way to flip 6th bit. (Same assembly generated)
Browse files Browse the repository at this point in the history
  • Loading branch information
gilescope committed Feb 8, 2021
1 parent f30c51a commit cadcf5e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
6 changes: 4 additions & 2 deletions library/core/benches/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {}

Expand Down Expand Up @@ -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)
}
}

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

Expand Down
5 changes: 1 addition & 4 deletions library/core/src/char/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions library/core/src/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions library/core/src/unicode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit cadcf5e

Please sign in to comment.