utf8.bend source
utf8.bend on the hub · documented module
# 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.import Basetype St is Data: # 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 SOk{need: U32, cp: U32, lo: U32, hi: U32, start: U32, out: List<&2, U32>} SErr{at: U32}def between(+b: U32, +lo: U32, +hi: U32) -> Bool: Bool.and(U32.is_ge(b, lo), U32.is_le(b, hi))# a lead byte: how many continuation bytes follow, and the range of the first onedef lead(+b: U32, +i: U32, +out: List<&2, U32>) -> St: Bool.pick(St, U32.is_lt(b, 128), SOk{0, 0, 128, 191, i, b <> out}, Bool.pick(St, between(b, 194, 223), SOk{1, U32.and(b, 31), 128, 191, i, out}, Bool.pick(St, between(b, 224, 239), SOk{2, U32.and(b, 15), Bool.pick(U32, U32.is_eq(b, 224), 160, 128), Bool.pick(U32, U32.is_eq(b, 237), 159, 191), i, out}, Bool.pick(St, between(b, 240, 244), SOk{3, U32.and(b, 7), Bool.pick(U32, U32.is_eq(b, 240), 144, 128), Bool.pick(U32, U32.is_eq(b, 244), 143, 191), i, out}, SErr{i}))))# a continuation byte: in range, it adds 6 bits, and the code point is out when none is left to comedef cont_done(+cp: U32, done: Bool, need: U32, +start: U32, out: List<&2, U32>) -> St: match done: case True{}: SOk{0, 0, 128, 191, start, cp <> out} case False{}: SOk{need, cp, 128, 191, start, out}def cont(ok: Bool, +b: U32, +need: U32, +cp: U32, +start: U32, out: List<&2, U32>) -> St: match ok: case True{}: cont_done(U32.or((cp * 64 : U32), U32.and(b, 63)), U32.is_eq(need, 1), (need - 1 : U32), start, out) case False{}: SErr{start}def step_ok(z: Bool, +b: U32, +i: U32, +need: U32, +cp: U32, +lo: U32, +hi: U32, +start: U32, out: List<&2, U32>) -> St: match z: case True{}: lead(b, i, out) case False{}: cont(between(b, lo, hi), b, need, cp, start, out)def step(st: St, +b: U32, +i: U32) -> St: match st: case SOk{+need, cp, lo, hi, start, out}: step_ok(U32.is_eq(need, 0), b, i, need, cp, lo, hi, start, out) case SErr{at}: SErr{at}def finish_ok(z: Bool, +start: U32, out: List<&2, U32>) -> Result<&2, &2, U32, List<&2, U32>>: match z: case True{}: Done{List.reverse(&2, U32, out)} case False{}: Fail{start}def finish(st: St) -> Result<&2, &2, U32, List<&2, U32>>: match st: case SOk{need, cp, lo, hi, start, out}: finish_ok(U32.is_eq(need, 0), start, out) case SErr{at}: Fail{at}def decode_go(bs: List<&2, U32>, +i: U32, st: St) -> St: match bs: case []: st case +b <> t: decode_go(t, (i + 1 : U32), step(st, b, i))# the code points of bs, or the offset of the first ill-formed sequencedef decode(bs: List<&2, U32>) -> Result<&2, &2, U32, List<&2, U32>>: finish(decode_go(bs, 0, SOk{0, 0, 128, 191, 0, []}))# one code point's bytes (a code point is assumed valid: at most U+10FFFF, not a surrogate)def enc1(+c: U32) -> List<&2, U32>: Bool.pick(List<&2, U32>, U32.is_lt(c, 128), [c], Bool.pick(List<&2, U32>, U32.is_lt(c, 2048), [U32.or(192, (c / 64 : U32)), U32.or(128, U32.and(c, 63))], Bool.pick(List<&2, U32>, U32.is_lt(c, 65536), [U32.or(224, (c / 4096 : U32)), U32.or(128, U32.and((c / 64 : U32), 63)), U32.or(128, U32.and(c, 63))], [U32.or(240, (c / 262144 : U32)), U32.or(128, U32.and((c / 4096 : U32), 63)), U32.or(128, U32.and((c / 64 : U32), 63)), U32.or(128, U32.and(c, 63))])))def encode(cs: List<&2, U32>) -> List<&2, U32>: match cs: case []: [] case c <> t: List.append(&2, U32, enc1(c), encode(t))def ok_or_nil(r: Result<&2, &2, U32, List<&2, U32>>) -> List<&2, U32>: match r: case Done{cs}: cs case Fail{at}: []# the code points of well-formed text ([] for ill-formed)def decode_ok(bs: List<&2, U32>) -> List<&2, U32>: ok_or_nil(decode(bs))