Debian Codegraph @

encoding/utf8/rune.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 // Returns the size of a rune, in octets, when encoded as UTF-8.
5 export fn runesz(r: rune) size = {
6 const ch = r: u32;
7 return if (ch < 0x80) 1
8 else if (ch < 0x800) 2
9 else if (ch < 0x10000) 3
10 else 4;
11 };
12
13 // Returns the expected length of a UTF-8 codepoint in bytes given its first
14 // byte, or [[invalid]] if the given byte doesn't begin a valid UTF-8 sequence.
15 export fn utf8sz(c: u8) (size | invalid) = {
16 if (c < 0b1000_0000) return 1;
17 if (c < 0b1100_0010 || c >= 0b1111_1000) return invalid;
18 if (c < 0b1110_0000) return 2;
19 if (c < 0b1111_0000) return 3;
20 return 4;
21 };