-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Make is_ascii_hexdigit branchless #103024
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1510,7 +1510,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') | ||
// Bitwise or can avoid need for branches in compiled code. | ||
matches!(*self, '0'..='9') || matches!(*self as u32 | 0x20, 0x61..=0x66) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make this more maintainable, how about using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately that would be a syntax error. I can't figure out a nice-looking way to use character literals here; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can still use that range in a pattern if you define it as a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One could make it slightly more concise: let lower = *self as u32 | 0x20;
matches!(*self, '0'..='9') || (lower >= 'a' as u32 && lower <= 'f' as u32) As an aside, there's a (currently private) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
doesn't seem to compile? I can get it to work with |
||
} | ||
|
||
/// Checks if the value is an ASCII punctuation character: | ||
|
Uh oh!
There was an error while loading. Please reload this page.