utf8.bend checks
raw source on the hub · import bend-bpe-tokenizer@0.1.0.0/utf8.bend as Utf8
UTF-8: bytes to code points, refusing ill-formed input with the offset of the first byte of the bad sequence (as Python's strict decoder reports it), and code points back to bytes. Well-formed means RFC 3629: no overlong forms, no surrogates (U+D800 to U+DFFF), nothing above U+10FFFF, no stray continuation byte, no sequence cut short.
1 import
import Base
Types
type St source · line 6 · raw
Data
SOk@need:U32 -> @cp:U32 -> @lo:U32 -> @hi:U32 -> @start:U32 -> @out:List<&2, U32> -> St
need: continuation bytes still to come; cp: the code point so far; lo, hi: the range the next continuation byte must fall in; start: the offset of the sequence's first byte; out: the code points so far, last first
SErr@at:U32 -> St
Definitions
def between source · line 12 · raw
@+b:U32 -> @+lo:U32 -> @+hi:U32 -> Bool
def lead source · line 16 · raw
@+b:U32 -> @+i:U32 -> @+out:List<&2, U32> -> St
a lead byte: how many continuation bytes follow, and the range of the first one
def cont_done source · line 24 · raw
@+cp:U32 -> @done:Bool -> @need:U32 -> @+start:U32 -> @out:List<&2, U32> -> St
a continuation byte: in range, it adds 6 bits, and the code point is out when none is left to come
def cont source · line 31 · raw
@ok:Bool -> @+b:U32 -> @+need:U32 -> @+cp:U32 -> @+start:U32 -> @out:List<&2, U32> -> St
def step_ok source · line 38 · raw
@z:Bool -> @+b:U32 -> @+i:U32 -> @+need:U32 -> @+cp:U32 -> @+lo:U32 -> @+hi:U32 -> @+start:U32 -> @out:List<&2, U32> -> St
def step source · line 45 · raw
@st:St -> @+b:U32 -> @+i:U32 -> St
def finish_ok source · line 52 · raw
@z:Bool -> @+start:U32 -> @out:List<&2, U32> -> Result<&2, &2, U32, List<&2, U32>>
def finish source · line 59 · raw
@st:St -> Result<&2, &2, U32, List<&2, U32>>
def decode_go source · line 66 · raw
@bs:List<&2, U32> -> @+i:U32 -> @st:St -> St
def decode source · line 74 · raw
@bs:List<&2, U32> -> Result<&2, &2, U32, List<&2, U32>>
the code points of bs, or the offset of the first ill-formed sequence
def enc1 source · line 78 · raw
@+c:U32 -> List<&2, U32>
one code point's bytes (a code point is assumed valid: at most U+10FFFF, not a surrogate)
def encode source · line 85 · raw
@cs:List<&2, U32> -> List<&2, U32>
def ok_or_nil source · line 92 · raw
@r:Result<&2, &2, U32, List<&2, U32>> -> List<&2, U32>
def decode_ok source · line 100 · raw
@bs:List<&2, U32> -> List<&2, U32>
the code points of well-formed text ([] for ill-formed)