Skip to content

Commit e48c684

Browse files
committed
Add a check for ASCII characters in to_upper and to_lower
This extra check has better performance. See discussion here: https://internals.rust-lang.org/t/to-upper-speed/13896
1 parent cecdb18 commit e48c684

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

library/core/src/unicode/unicode_data.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -549,16 +549,24 @@ pub mod white_space {
549549
#[rustfmt::skip]
550550
pub mod conversions {
551551
pub fn to_lower(c: char) -> [char; 3] {
552-
match bsearch_case_table(c, LOWERCASE_TABLE) {
553-
None => [c, '\0', '\0'],
554-
Some(index) => LOWERCASE_TABLE[index].1,
552+
if c.is_ascii() {
553+
[(c as u8).to_ascii_lowercase() as char, '\0', '\0']
554+
} else {
555+
match bsearch_case_table(c, LOWERCASE_TABLE) {
556+
None => [c, '\0', '\0'],
557+
Some(index) => LOWERCASE_TABLE[index].1,
558+
}
555559
}
556560
}
557561

558562
pub fn to_upper(c: char) -> [char; 3] {
559-
match bsearch_case_table(c, UPPERCASE_TABLE) {
560-
None => [c, '\0', '\0'],
561-
Some(index) => UPPERCASE_TABLE[index].1,
563+
if c.is_ascii() {
564+
[(c as u8).to_ascii_uppercase() as char, '\0', '\0']
565+
} else {
566+
match bsearch_case_table(c, UPPERCASE_TABLE) {
567+
None => [c, '\0', '\0'],
568+
Some(index) => UPPERCASE_TABLE[index].1,
569+
}
562570
}
563571
}
564572

0 commit comments

Comments
 (0)