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