]> git.lizzy.rs Git - rust.git/blobdiff - library/core/src/num/mod.rs
Make char methods const
[rust.git] / library / core / src / num / mod.rs
index 7563a742b9a900dcd2fecadd3ef0b455d8bcf175..8b81d41b8c067ffc49fd95b1136a0e783ed0b07e 100644 (file)
@@ -5,7 +5,6 @@
 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 {
@@ -153,6 +152,9 @@ impl isize {
      usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
 }
 
+/// If 6th bit set ascii is upper case.
+const ASCII_CASE_MASK: u8 = 0b0010_0000;
+
 #[lang = "u8"]
 impl u8 {
     uint_impl! { u8, u8, 8, 255, 2, "0x82", "0xa", "0x12", "0x12", "0x48", "[0x12]",
@@ -193,8 +195,9 @@ pub const fn is_ascii(&self) -> bool {
     ///
     /// [`make_ascii_uppercase`]: #method.make_ascii_uppercase
     #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
+    #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.50.0")]
     #[inline]
-    pub fn to_ascii_uppercase(&self) -> u8 {
+    pub const fn to_ascii_uppercase(&self) -> u8 {
         // Unset the fifth bit if this is a lowercase letter
         *self & !((self.is_ascii_lowercase() as u8) * ASCII_CASE_MASK)
     }
@@ -216,12 +219,19 @@ pub fn to_ascii_uppercase(&self) -> u8 {
     ///
     /// [`make_ascii_lowercase`]: #method.make_ascii_lowercase
     #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
+    #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.50.0")]
     #[inline]
-    pub fn to_ascii_lowercase(&self) -> u8 {
+    pub const fn to_ascii_lowercase(&self) -> u8 {
         // Set the fifth bit if this is an uppercase letter
         *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)`.
@@ -235,8 +245,9 @@ pub fn to_ascii_lowercase(&self) -> u8 {
     /// assert!(lowercase_a.eq_ignore_ascii_case(&uppercase_a));
     /// ```
     #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
+    #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.50.0")]
     #[inline]
-    pub fn eq_ignore_ascii_case(&self, other: &u8) -> bool {
+    pub const fn eq_ignore_ascii_case(&self, other: &u8) -> bool {
         self.to_ascii_lowercase() == other.to_ascii_lowercase()
     }