Decoding and encoding UTF-8 text, one character at a time.
Strings elsewhere in the standard library are plain bytes; this module is where bytes become code
points and back. Decoding is lenient unless asked to be strict, and the iterator utf8_iter is
always strict. Procedures that allocate use the context allocator and the caller frees the result.
Decodes the character that starts at s, where source_length bytes are available. Returns the code
point, the number of bytes it took and how it went. A call with bytes available always takes at least one.
Writes the UTF-8 form of ch into the buffer preallocated_string points to, and sets its count to the
number of bytes used (1 to 4). The buffer must have room for them, or an assert fails; four bytes always do.
A pointer to the byte after the character that starts at s, going by its first byte only. Nothing is
checked: a truncated or malformed character at the end of a buffer gives a pointer past the end.
Decodes the first character of s and removes it from the start of s. Returns its code point and how
decoding went, as character_utf8_to_utf32 does without strict.
text := "a\xc3\xa9";
code, status := utf8_next_character(*text);
assert(status == .CONVERSION_OK && code == #char"a");
code, status = utf8_next_character(*text);
assert(status == .CONVERSION_OK && code == 0xe9 && text.count == 0);
Loops over the code points of a string: for: utf8_iter text { ... }. The body sees it, the code point,
and it_index, the number of characters before it (not the byte offset). for <: goes from the last
character to the first, and it_index is -1 there. Iterating by pointer is not allowed.
The highest value the six byte form of UTF-8 can carry, 0x7FFFFFFF. No decoder here returns a value above
UNI_MAX_UTF16; the longer forms decode as UNI_REPLACEMENT_CHAR.
How many continuation bytes follow a first byte, for each byte value. It is 0 for ASCII and for continuation
bytes themselves, 1 to 3 for the lead bytes of valid sequences and 4 or 5 for the bytes of the old
five and six byte forms (0xF8 and up).
For a sequence of n + 1 bytes, the amount that classic decoders subtract after adding up the bytes
shifted by six bits each. A table for hand-written decoders; the procedures here do not need it.