Skip to content

Commit

Permalink
Slightly more explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
gilescope committed Feb 12, 2021
1 parent cadcf5e commit daa55ac
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
14 changes: 11 additions & 3 deletions library/core/src/char/methods.rs
Expand Up @@ -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::*;

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 9 additions & 1 deletion library/core/src/num/mod.rs
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)`.
Expand Down
3 changes: 0 additions & 3 deletions library/core/src/unicode/mod.rs
Expand Up @@ -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,
Expand Down

0 comments on commit daa55ac

Please sign in to comment.