Skip to content

Optimize char::is_alphanumeric #145027

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

Merged
merged 1 commit into from
Aug 9, 2025
Merged
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
6 changes: 5 additions & 1 deletion library/core/src/char/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,11 @@ impl char {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn is_alphanumeric(self) -> bool {
self.is_alphabetic() || self.is_numeric()
if self.is_ascii() {
self.is_ascii_alphanumeric()
Comment on lines +923 to +924
Copy link
Member

Choose a reason for hiding this comment

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

unsure: with https://github.com/rust-lang/rust/pull/143467/files#diff-5f02a23b56d56296cfa0b9f2dc32075d26638c5aa232c4aa551131785dad8887R759 about to land, should this instead go via that?

Suggested change
if self.is_ascii() {
self.is_ascii_alphanumeric()
if let Some(a) = self.as_ascii() {
a.is_alphanumeric()

(Don't know how much it matters, but I always prefer using types to avoid boolean blindness, where possible.)

} else {
unicode::Alphabetic(self) || unicode::N(self)
}
}

/// Returns `true` if this `char` has the general category for control codes.
Expand Down
Loading