Skip to content

Optimize is_ascii_digit() and is_ascii_hexdigit() #67143

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

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/libcore/char/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,7 @@ impl char {
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
#[inline]
pub fn is_ascii_digit(&self) -> bool {
self.is_ascii() && (*self as u8).is_ascii_digit()
self.is_digit(10)
}

/// Checks if the value is an ASCII hexadecimal digit:
Expand Down Expand Up @@ -1245,7 +1245,14 @@ impl char {
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
#[inline]
pub fn is_ascii_hexdigit(&self) -> bool {
self.is_ascii() && (*self as u8).is_ascii_hexdigit()
if !self.is_ascii() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not change this like is_ascii_digit to self.is_digit(16)? Does that end up with worse codegen?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, currently is_digit(x) checks two separate alphabetic ranges instead of one bit-manipulated one, but i could change that if desired

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, seems like it'd be best to fix is_digit.

return false;
}
if self.is_digit(10) {
return true;
}
let code = (*self as u8) & !0x20; // 0x20 is the case bit
code >= b'A' && code <= b'F'
}

/// Checks if the value is an ASCII punctuation character:
Expand Down