mirror of
https://github.com/ziglang/zig.git
synced 2024-11-25 05:40:16 +00:00
std.ascii
: make toLower
toUpper
branchless (#21369)
Co-authored-by: WX\shixi <shixi1@cnwxsoft.com>
This commit is contained in:
parent
b56a667ecd
commit
8ddce90e62
@ -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" {
|
||||
|
Loading…
Reference in New Issue
Block a user