std1.0.0-beta-7 SafeC docs

Module encoding::utf8

#include <std/encoding/utf8.h> source

UTF-8 encode/decode

A "codepoint" here is a plain Unicode scalar value (0..0x10FFFF, excluding the surrogate range 0xD800..0xDFFF) packed into an 'unsigned int'. There is no separate 'rune'/'char32_t' type in SafeC — this mirrors Go's 'rune'-as- int32 convention rather than introducing a new primitive type.

Functions

utf8_encodeEncodes 'cp' into 'out' (caller-provided buffer, must have room for at least 4 bytes — UTF-8's longest encoding). Returns the number of bytes written (1-4), or 0 if 'cp' is not a valid Unicode scalar value (> 0x10FFFF or inside the surrogate range 0xD800..0xDFFF).
utf8_decodeDecodes one codepoint starting at 's[pos]'. On success, writes the codepoint to '*cp_out' and returns the number of bytes consumed (1-4). On a malformed sequence (bad continuation byte, overlong encoding, encoded surrogate, or truncated multi-byte sequence at a NUL/end-of-buffer), returns 0 and '*cp_out' is unspecified.
utf8_is_validReturns 1 if the entire NUL-terminated string is well-formed UTF-8.
utf8_lenCounts codepoints (not bytes) in a NUL-terminated, well-formed UTF-8 string. Returns 0 if the string is not valid UTF-8 (use utf8_is_valid first if you need to distinguish that from a genuinely empty string).
utf8_encoded_lenNumber of bytes a codepoint encodes to (1-4), or 0 if invalid — the size utf8_encode would need without actually writing anything.