about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGiles Cope <gilescope@gmail.com>2021-02-08 12:21:36 +0000
committerGiles Cope <gilescope@gmail.com>2021-02-08 12:21:36 +0000
commitcadcf5ed990dc02ad86cbb9f31423959a5517f50 (patch)
tree59b8ae7bb4da57abaa0d589ae80b6b47effc193a
parentf30c51abe8ce62756f86abbbd6623a9131d3954c (diff)
downloadrust-cadcf5ed990dc02ad86cbb9f31423959a5517f50.tar.gz
rust-cadcf5ed990dc02ad86cbb9f31423959a5517f50.zip
Unify way to flip 6th bit. (Same assembly generated)
-rw-r--r--library/core/benches/ascii.rs6
-rw-r--r--library/core/src/char/methods.rs5
-rw-r--r--library/core/src/num/mod.rs5
-rw-r--r--library/core/src/unicode/mod.rs3
4 files changed, 11 insertions, 8 deletions
diff --git a/library/core/benches/ascii.rs b/library/core/benches/ascii.rs
index bc59c378609..64938745a4a 100644
--- a/library/core/benches/ascii.rs
+++ b/library/core/benches/ascii.rs
@@ -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]) {}
 
@@ -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)
         }
     }
 
@@ -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
         }
     }
 
diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs
index 4032e777077..bbdb2a5d41b 100644
--- a/library/core/src/char/methods.rs
+++ b/library/core/src/char/methods.rs
@@ -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.
diff --git a/library/core/src/num/mod.rs b/library/core/src/num/mod.rs
index 6bdfa18fa43..7563a742b9a 100644
--- a/library/core/src/num/mod.rs
+++ b/library/core/src/num/mod.rs
@@ -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 {
@@ -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.
@@ -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.
diff --git a/library/core/src/unicode/mod.rs b/library/core/src/unicode/mod.rs
index 37ca0a0779b..b333b463105 100644
--- a/library/core/src/unicode/mod.rs
+++ b/library/core/src/unicode/mod.rs
@@ -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,