Skip to content

Commit cadcf5e

Browse files
committed
Unify way to flip 6th bit. (Same assembly generated)
1 parent f30c51a commit cadcf5e

File tree

4 files changed

+11
-8
lines changed

4 files changed

+11
-8
lines changed

library/core/benches/ascii.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ macro_rules! benches {
6666
use test::black_box;
6767
use test::Bencher;
6868

69+
const ASCII_CASE_MASK: u8 = 0b0010_0000;
70+
6971
benches! {
7072
fn case00_alloc_only(_bytes: &mut [u8]) {}
7173

@@ -204,7 +206,7 @@ benches! {
204206
}
205207
}
206208
for byte in bytes {
207-
*byte &= !((is_ascii_lowercase(*byte) as u8) << 5)
209+
*byte &= !((is_ascii_lowercase(*byte) as u8) * ASCII_CASE_MASK)
208210
}
209211
}
210212

@@ -216,7 +218,7 @@ benches! {
216218
}
217219
}
218220
for byte in bytes {
219-
*byte -= (is_ascii_lowercase(*byte) as u8) << 5
221+
*byte -= (is_ascii_lowercase(*byte) as u8) * ASCII_CASE_MASK
220222
}
221223
}
222224

library/core/src/char/methods.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
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};
6+
use crate::unicode::{self, conversions, ASCII_CASE_MASK};
77

88
use super::*;
99

10-
/// If 6th bit set ascii is upper case.
11-
const ASCII_CASE_MASK: u8 = 0b10_0000u8;
12-
1310
#[lang = "char"]
1411
impl char {
1512
/// The highest valid code point a `char` can have.

library/core/src/num/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use crate::intrinsics;
66
use crate::mem;
77
use crate::str::FromStr;
8+
use crate::unicode::ASCII_CASE_MASK;
89

910
// Used because the `?` operator is not allowed in a const context.
1011
macro_rules! try_opt {
@@ -195,7 +196,7 @@ impl u8 {
195196
#[inline]
196197
pub fn to_ascii_uppercase(&self) -> u8 {
197198
// Unset the fifth bit if this is a lowercase letter
198-
*self & !((self.is_ascii_lowercase() as u8) << 5)
199+
*self & !((self.is_ascii_lowercase() as u8) * ASCII_CASE_MASK)
199200
}
200201

201202
/// Makes a copy of the value in its ASCII lower case equivalent.
@@ -218,7 +219,7 @@ impl u8 {
218219
#[inline]
219220
pub fn to_ascii_lowercase(&self) -> u8 {
220221
// Set the fifth bit if this is an uppercase letter
221-
*self | ((self.is_ascii_uppercase() as u8) << 5)
222+
*self | (self.is_ascii_uppercase() as u8 * ASCII_CASE_MASK)
222223
}
223224

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

library/core/src/unicode/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ 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+
2023
// For use in liballoc, not re-exported in libstd.
2124
pub use unicode_data::{
2225
case_ignorable::lookup as Case_Ignorable, cased::lookup as Cased, conversions,

0 commit comments

Comments
 (0)