Skip to content

Commit 8ddce90

Browse files
CrazyboyQCDWX\shixi
and
WX\shixi
authored
std.ascii: make toLower toUpper branchless (#21369)
Co-authored-by: WX\shixi <[email protected]>
1 parent b56a667 commit 8ddce90

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

lib/std/ascii.zig

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,20 +182,14 @@ pub const isASCII = isAscii;
182182

183183
/// Uppercases the character and returns it as-is if already uppercase or not a letter.
184184
pub fn toUpper(c: u8) u8 {
185-
if (isLower(c)) {
186-
return c & 0b11011111;
187-
} else {
188-
return c;
189-
}
185+
const mask = @as(u8, @intFromBool(isLower(c))) << 5;
186+
return c ^ mask;
190187
}
191188

192189
/// Lowercases the character and returns it as-is if already lowercase or not a letter.
193190
pub fn toLower(c: u8) u8 {
194-
if (isUpper(c)) {
195-
return c | 0b00100000;
196-
} else {
197-
return c;
198-
}
191+
const mask = @as(u8, @intFromBool(isUpper(c))) << 5;
192+
return c | mask;
199193
}
200194

201195
test "ASCII character classes" {

0 commit comments

Comments
 (0)