utf8
lib/utf8.vklib/utf8.vk Pure-Veko RFC 3629 UTF-8 validation, codepoint (rune) decoding/encoding, rune counting, and multi-byte-safe slicing. Zero capabilities required.
RuneStep = struct { codepoint i64, width i64 }
Decoded Unicode codepoint and the number of UTF-8 bytes consumed (1..4).
decode-rune = fn [raw bytes, off i64] RuneStep
Decodes one UTF-8 codepoint from raw starting at byte offset off.
Rejects overlong encodings, UTF-16 surrogates (0xD800..0xDFFF), and codepoints > 0x10FFFF.
use [utf8]
b = utf8/encode-rune 0x03BB // Greek lambda
st = utf8/decode-rune b 0
if st.codepoint != 0x03BB or st.width != 2 [ err 'decode-rune' ] else [ .ok ]
encode-rune = fn [cp i64] bytes
Encodes a single Unicode scalar value cp into 1..4 UTF-8 bytes.
valid? = fn [raw bytes] bool
Returns true if raw consists exclusively of valid RFC 3629 UTF-8 codepoints.
use [utf8]
bad = bytes/fill 2 0xFF
if (not (utf8/valid? (bytes/from-str 'veko'))) or (utf8/valid? bad) [ err 'utf8 valid?' ] else [ .ok ]
rune-count = fn [s str] i64
Returns the number of Unicode scalar values (runes) in UTF-8 string s.
from-runes = fn [cps block[i64]] str
Encodes a block[i64] of Unicode codepoints back into a UTF-8 string.
rune-slice = fn [s str, start i64, end_idx i64] str
Slices string s safely by rune indices [start..end_idx) without ever splitting multi-byte codepoints.
use [utf8]
s = utf8/from-runes [65 0x03B1 0x03B2 66]
part = utf8/rune-slice s 1 3
if (utf8/rune-count part) != 2 or (utf8/runes part) != [0x03B1 0x03B2] [ err 'rune-slice' ] else [ .ok ]