Avoid index check in char::to_lowercase and char::to_uppercase

This commit is contained in:
Marco A L Barbosa 2025-12-30 16:05:53 -03:00
parent f0864c89ea
commit 262426d535
2 changed files with 8 additions and 4 deletions

View file

@ -767,7 +767,8 @@ pub mod conversions {
LOWERCASE_TABLE
.binary_search_by(|&(key, _)| key.cmp(&c))
.map(|i| {
let u = LOWERCASE_TABLE[i].1;
// SAFETY: i is the result of the binary search
let u = unsafe { LOWERCASE_TABLE.get_unchecked(i) }.1;
char::from_u32(u).map(|c| [c, '\0', '\0']).unwrap_or_else(|| {
// SAFETY: Index comes from statically generated table
unsafe { *LOWERCASE_TABLE_MULTI.get_unchecked((u & (INDEX_MASK - 1)) as usize) }
@ -784,7 +785,8 @@ pub mod conversions {
UPPERCASE_TABLE
.binary_search_by(|&(key, _)| key.cmp(&c))
.map(|i| {
let u = UPPERCASE_TABLE[i].1;
// SAFETY: i is the result of the binary search
let u = unsafe { UPPERCASE_TABLE.get_unchecked(i) }.1;
char::from_u32(u).map(|c| [c, '\0', '\0']).unwrap_or_else(|| {
// SAFETY: Index comes from statically generated table
unsafe { *UPPERCASE_TABLE_MULTI.get_unchecked((u & (INDEX_MASK - 1)) as usize) }

View file

@ -91,7 +91,8 @@ pub fn to_lower(c: char) -> [char; 3] {
LOWERCASE_TABLE
.binary_search_by(|&(key, _)| key.cmp(&c))
.map(|i| {
let u = LOWERCASE_TABLE[i].1;
// SAFETY: i is the result of the binary search
let u = unsafe { LOWERCASE_TABLE.get_unchecked(i) }.1;
char::from_u32(u).map(|c| [c, '\0', '\0']).unwrap_or_else(|| {
// SAFETY: Index comes from statically generated table
unsafe { *LOWERCASE_TABLE_MULTI.get_unchecked((u & (INDEX_MASK - 1)) as usize) }
@ -108,7 +109,8 @@ pub fn to_upper(c: char) -> [char; 3] {
UPPERCASE_TABLE
.binary_search_by(|&(key, _)| key.cmp(&c))
.map(|i| {
let u = UPPERCASE_TABLE[i].1;
// SAFETY: i is the result of the binary search
let u = unsafe { UPPERCASE_TABLE.get_unchecked(i) }.1;
char::from_u32(u).map(|c| [c, '\0', '\0']).unwrap_or_else(|| {
// SAFETY: Index comes from statically generated table
unsafe { *UPPERCASE_TABLE_MULTI.get_unchecked((u & (INDEX_MASK - 1)) as usize) }