authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2019-12-28 14:34:00+11:00
committergravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2019-12-28 14:39:38+11:00
log5843a6e3bc1a6353f76ebca57d6099337b90139a
treef1a0a0f0dce77adc395c5ae10bb1721732363641
parent8b72eedc76f46b978843344f92605acb4ee4061a
signature Commit is signed but in an unrecognized format.

std: optimise utf8ByteSequenceLength

Also tested (but not as fast): ```zig pub fn utf8ByteSequenceLength(first_byte: u8) !u3 { const len = @clz(u8, ~first_byte); if (len == 0) return 1; if (len < 4) return @intCast(u3, len); return error.Utf8InvalidStartByte; } ```

1 files changed, 7 insertions(+), 5 deletions(-)

lib/std/unicode.zig+7-5
...@@ -18,11 +18,13 @@ pub fn utf8CodepointSequenceLength(c: u32) !u3 {...@@ -18,11 +18,13 @@ pub fn utf8CodepointSequenceLength(c: u32) !u3 {
18/// returns a number 1-4 indicating the total length of the codepoint in bytes.18/// returns a number 1-4 indicating the total length of the codepoint in bytes.
19/// If this byte does not match the form of a UTF-8 start byte, returns Utf8InvalidStartByte.19/// If this byte does not match the form of a UTF-8 start byte, returns Utf8InvalidStartByte.
20pub fn utf8ByteSequenceLength(first_byte: u8) !u3 {20pub fn utf8ByteSequenceLength(first_byte: u8) !u3 {
21 if (first_byte < 0b10000000) return @as(u3, 1);21 return switch (@clz(u8, ~first_byte)) {
22 if (first_byte & 0b11100000 == 0b11000000) return @as(u3, 2);22 0 => 1,
23 if (first_byte & 0b11110000 == 0b11100000) return @as(u3, 3);23 2 => 2,
24 if (first_byte & 0b11111000 == 0b11110000) return @as(u3, 4);24 3 => 3,
25 return error.Utf8InvalidStartByte;25 4 => 4,
26 else => error.Utf8InvalidStartByte,
27 };
26}28}
2729
28/// Encodes the given codepoint into a UTF-8 byte sequence.30/// Encodes the given codepoint into a UTF-8 byte sequence.