std.ascii: make toLower toUpper branchless (#21369)

Co-authored-by: WX\shixi <shixi1@cnwxsoft.com>
This commit is contained in:
CrazyboyQCD 2024-09-14 08:22:19 +08:00 committed by GitHub
parent b56a667ecd
commit 8ddce90e62
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -182,20 +182,14 @@ pub const isASCII = isAscii;
/// Uppercases the character and returns it as-is if already uppercase or not a letter.
pub fn toUpper(c: u8) u8 {
if (isLower(c)) {
return c & 0b11011111;
} else {
return c;
}
const mask = @as(u8, @intFromBool(isLower(c))) << 5;
return c ^ mask;
}
/// Lowercases the character and returns it as-is if already lowercase or not a letter.
pub fn toLower(c: u8) u8 {
if (isUpper(c)) {
return c | 0b00100000;
} else {
return c;
}
const mask = @as(u8, @intFromBool(isUpper(c))) << 5;
return c | mask;
}
test "ASCII character classes" {