Skip to content

Commit 9d824ff

Browse files
okanecoGKFXscottmcm
committed
Refactor some char, u8 ascii functions to be branchless
Decompose singular `matches!` with or-patterns to individual `matches!` statements to enable branchless code output. The following functions were changed: - `is_ascii_alphanumeric` - `is_ascii_hexdigit` - `is_ascii_punctuation` Add codegen tests Co-authored-by: George Bateman <george.bateman16@gmail.com> Co-authored-by: scottmcm <scottmcm@users.noreply.github.com>
1 parent 76c6439 commit 9d824ff

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

core/src/char/methods.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ impl char {
14501450
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
14511451
#[inline]
14521452
pub const fn is_ascii_alphanumeric(&self) -> bool {
1453-
matches!(*self, '0'..='9' | 'A'..='Z' | 'a'..='z')
1453+
matches!(*self, '0'..='9') | matches!(*self, 'A'..='Z') | matches!(*self, 'a'..='z')
14541454
}
14551455

14561456
/// Checks if the value is an ASCII decimal digit:
@@ -1553,7 +1553,7 @@ impl char {
15531553
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
15541554
#[inline]
15551555
pub const fn is_ascii_hexdigit(&self) -> bool {
1556-
matches!(*self, '0'..='9' | 'A'..='F' | 'a'..='f')
1556+
matches!(*self, '0'..='9') | matches!(*self, 'A'..='F') | matches!(*self, 'a'..='f')
15571557
}
15581558

15591559
/// Checks if the value is an ASCII punctuation character:
@@ -1591,7 +1591,10 @@ impl char {
15911591
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
15921592
#[inline]
15931593
pub const fn is_ascii_punctuation(&self) -> bool {
1594-
matches!(*self, '!'..='/' | ':'..='@' | '['..='`' | '{'..='~')
1594+
matches!(*self, '!'..='/')
1595+
| matches!(*self, ':'..='@')
1596+
| matches!(*self, '['..='`')
1597+
| matches!(*self, '{'..='~')
15951598
}
15961599

15971600
/// Checks if the value is an ASCII graphic character:

core/src/num/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ impl u8 {
791791
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
792792
#[inline]
793793
pub const fn is_ascii_alphanumeric(&self) -> bool {
794-
matches!(*self, b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z')
794+
matches!(*self, b'0'..=b'9') | matches!(*self, b'A'..=b'Z') | matches!(*self, b'a'..=b'z')
795795
}
796796

797797
/// Checks if the value is an ASCII decimal digit:
@@ -894,7 +894,7 @@ impl u8 {
894894
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
895895
#[inline]
896896
pub const fn is_ascii_hexdigit(&self) -> bool {
897-
matches!(*self, b'0'..=b'9' | b'A'..=b'F' | b'a'..=b'f')
897+
matches!(*self, b'0'..=b'9') | matches!(*self, b'A'..=b'F') | matches!(*self, b'a'..=b'f')
898898
}
899899

900900
/// Checks if the value is an ASCII punctuation character:
@@ -932,7 +932,10 @@ impl u8 {
932932
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
933933
#[inline]
934934
pub const fn is_ascii_punctuation(&self) -> bool {
935-
matches!(*self, b'!'..=b'/' | b':'..=b'@' | b'['..=b'`' | b'{'..=b'~')
935+
matches!(*self, b'!'..=b'/')
936+
| matches!(*self, b':'..=b'@')
937+
| matches!(*self, b'['..=b'`')
938+
| matches!(*self, b'{'..=b'~')
936939
}
937940

938941
/// Checks if the value is an ASCII graphic character:

0 commit comments

Comments
 (0)