Slightly more explicit

This commit is contained in:
Giles Cope 2021-02-12 13:42:42 +00:00
parent cadcf5ed99
commit daa55acdb0
No known key found for this signature in database
GPG Key ID: DF85161DAE0FF36B
3 changed files with 20 additions and 7 deletions

View File

@ -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::*;
@ -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.
@ -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.

View File

@ -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 {
@ -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)`.

View File

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