about summary refs log tree commit diff
diff options
context:
space:
mode:
authorKarl Meakin <karl.meakin@arm.com>2025-08-07 00:05:22 +0100
committerKarl Meakin <karl.meakin@arm.com>2025-08-07 00:08:11 +0100
commitbf5020937f17fd4c2f8ae5e7703fd9b8d15f0502 (patch)
tree2b4b06b76d0a233bedd9a8c507ee0c4aafec3ed7
parent29cdc6a109ee98a382f974bf89d3971b4385399c (diff)
downloadrust-bf5020937f17fd4c2f8ae5e7703fd9b8d15f0502.tar.gz
rust-bf5020937f17fd4c2f8ae5e7703fd9b8d15f0502.zip
Optimize `char::is_alphanumeric`
Avoid an unnecessary call to `unicode::Alphabetic` when `self` is an
ASCII digit (ie `0..=9`).
-rw-r--r--library/core/src/char/methods.rs6
1 files changed, 5 insertions, 1 deletions
diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs
index b0752a85faf..7ee0962721f 100644
--- a/library/core/src/char/methods.rs
+++ b/library/core/src/char/methods.rs
@@ -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()
+        } else {
+            unicode::Alphabetic(self) || unicode::N(self)
+        }
     }
 
     /// Returns `true` if this `char` has the general category for control codes.