From 6546d1184dbc3ec9129a3c5d9d6795c9d2a71897 Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Sun, 17 Apr 2022 10:13:45 +0100 Subject: [PATCH 1/4] tighter is ascii methods --- library/core/src/char/methods.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index 5809ed1f33b07..4d74e84fa3e8a 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -1275,7 +1275,8 @@ impl char { #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")] #[inline] pub const fn is_ascii_alphabetic(&self) -> bool { - matches!(*self, 'A'..='Z' | 'a'..='z') + //Godbolt: https://rust.godbolt.org/z/nMdjWz4aG + (*self as u32 | 0b10_0000).wrapping_sub('a' as u32) < 26 } /// Checks if the value is an ASCII uppercase character: @@ -1380,7 +1381,8 @@ impl char { #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")] #[inline] pub const fn is_ascii_alphanumeric(&self) -> bool { - matches!(*self, '0'..='9' | 'A'..='Z' | 'a'..='z') + //Godbolt: https://rust.godbolt.org/z/dejn8P9q5 + self.is_ascii_digit() || self.is_ascii_alphabetic() } /// Checks if the value is an ASCII decimal digit: @@ -1451,7 +1453,8 @@ impl char { #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")] #[inline] pub const fn is_ascii_hexdigit(&self) -> bool { - matches!(*self, '0'..='9' | 'A'..='F' | 'a'..='f') + // Godbolt: https://rust.godbolt.org/z/nYsdEPj4e + self.is_ascii_digit() || (*self as u32 | 0b10_0000).wrapping_sub('a' as u32) <= 6 } /// Checks if the value is an ASCII punctuation character: From 0bae9900aeb59051e1155b9dabd40e9c30a2322e Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Sun, 17 Apr 2022 10:27:08 +0100 Subject: [PATCH 2/4] remove godbolt comments --- library/core/src/char/methods.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index 4d74e84fa3e8a..40d090ae3a08b 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -1275,7 +1275,6 @@ impl char { #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")] #[inline] pub const fn is_ascii_alphabetic(&self) -> bool { - //Godbolt: https://rust.godbolt.org/z/nMdjWz4aG (*self as u32 | 0b10_0000).wrapping_sub('a' as u32) < 26 } @@ -1381,7 +1380,6 @@ impl char { #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")] #[inline] pub const fn is_ascii_alphanumeric(&self) -> bool { - //Godbolt: https://rust.godbolt.org/z/dejn8P9q5 self.is_ascii_digit() || self.is_ascii_alphabetic() } @@ -1453,7 +1451,6 @@ impl char { #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")] #[inline] pub const fn is_ascii_hexdigit(&self) -> bool { - // Godbolt: https://rust.godbolt.org/z/nYsdEPj4e self.is_ascii_digit() || (*self as u32 | 0b10_0000).wrapping_sub('a' as u32) <= 6 } From 29b4697a458c22808c0f3a7d1fcdaf11b49676ca Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Sun, 17 Apr 2022 14:14:19 +0100 Subject: [PATCH 3/4] cargo fmt --- library/core/src/char/methods.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index 40d090ae3a08b..8e4007f8f1903 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -1275,7 +1275,7 @@ impl char { #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")] #[inline] pub const fn is_ascii_alphabetic(&self) -> bool { - (*self as u32 | 0b10_0000).wrapping_sub('a' as u32) < 26 + (*self as u32 | 0b10_0000).wrapping_sub('a' as u32) < 26 } /// Checks if the value is an ASCII uppercase character: From debfc23690efb36ee060d496f7c8c10dd66aee11 Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Sun, 17 Apr 2022 15:28:10 +0100 Subject: [PATCH 4/4] exclusive --- library/core/src/char/methods.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index 8e4007f8f1903..3519f21fd95b8 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -1451,7 +1451,7 @@ impl char { #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")] #[inline] pub const fn is_ascii_hexdigit(&self) -> bool { - self.is_ascii_digit() || (*self as u32 | 0b10_0000).wrapping_sub('a' as u32) <= 6 + self.is_ascii_digit() || (*self as u32 | 0b10_0000).wrapping_sub('a' as u32) < 6 } /// Checks if the value is an ASCII punctuation character: