Skip to content

Commit daa55ac

Browse files
committed
Slightly more explicit
1 parent cadcf5e commit daa55ac

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

library/core/src/char/methods.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::slice;
44
use crate::str::from_utf8_unchecked_mut;
55
use crate::unicode::printable::is_printable;
6-
use crate::unicode::{self, conversions, ASCII_CASE_MASK};
6+
use crate::unicode::{self, conversions};
77

88
use super::*;
99

@@ -1090,7 +1090,11 @@ impl char {
10901090
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
10911091
#[inline]
10921092
pub fn to_ascii_uppercase(&self) -> char {
1093-
if self.is_ascii_lowercase() { ((*self as u8) & !ASCII_CASE_MASK) as char } else { *self }
1093+
if self.is_ascii_lowercase() {
1094+
(*self as u8).ascii_change_case_unchecked() as char
1095+
} else {
1096+
*self
1097+
}
10941098
}
10951099

10961100
/// Makes a copy of the value in its ASCII lower case equivalent.
@@ -1118,7 +1122,11 @@ impl char {
11181122
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
11191123
#[inline]
11201124
pub fn to_ascii_lowercase(&self) -> char {
1121-
if self.is_ascii_uppercase() { ((*self as u8) | ASCII_CASE_MASK) as char } else { *self }
1125+
if self.is_ascii_uppercase() {
1126+
(*self as u8).ascii_change_case_unchecked() as char
1127+
} else {
1128+
*self
1129+
}
11221130
}
11231131

11241132
/// Checks that two values are an ASCII case-insensitive match.

library/core/src/num/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use crate::intrinsics;
66
use crate::mem;
77
use crate::str::FromStr;
8-
use crate::unicode::ASCII_CASE_MASK;
8+
9+
/// If 6th bit set ascii is upper case.
10+
const ASCII_CASE_MASK: u8 = 0b0010_0000;
911

1012
// Used because the `?` operator is not allowed in a const context.
1113
macro_rules! try_opt {
@@ -222,6 +224,12 @@ impl u8 {
222224
*self | (self.is_ascii_uppercase() as u8 * ASCII_CASE_MASK)
223225
}
224226

227+
/// Assumes self is ascii
228+
#[inline]
229+
pub(crate) fn ascii_change_case_unchecked(&self) -> u8 {
230+
*self ^ ASCII_CASE_MASK
231+
}
232+
225233
/// Checks that two values are an ASCII case-insensitive match.
226234
///
227235
/// This is equivalent to `to_ascii_lowercase(a) == to_ascii_lowercase(b)`.

library/core/src/unicode/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ mod unicode_data;
1717
#[stable(feature = "unicode_version", since = "1.45.0")]
1818
pub const UNICODE_VERSION: (u8, u8, u8) = unicode_data::UNICODE_VERSION;
1919

20-
/// If 6th bit set ascii is upper case.
21-
pub(crate) const ASCII_CASE_MASK: u8 = 0b0010_0000;
22-
2320
// For use in liballoc, not re-exported in libstd.
2421
pub use unicode_data::{
2522
case_ignorable::lookup as Case_Ignorable, cased::lookup as Cased, conversions,

0 commit comments

Comments
 (0)