utf8.bend checks
raw source on the hub · import 0x5cad3183e43311709dddaded42dc4e56/utf8.bend as Utf8
1 import
import Base
Definitions
def can_encode_as_one_byte source · line 4 · raw
@+cp:U32 -> Bool
Encoding capacity is separate from shortest-form validity.
def can_encode_as_two_bytes source · line 7 · raw
@+cp:U32 -> Bool
def can_encode_as_three_bytes source · line 10 · raw
@+cp:U32 -> Bool
def is_continuation_byte source · line 19 · raw
@+b:U32 -> Bool
10xxxxxx: mask 0xFFFFFFC0, bits 0x80.
def is_two_byte_lead source · line 23 · raw
@+b:U32 -> Bool
110xxxxx: mask 0xFFFFFFE0, bits 0xC0.
def is_three_byte_lead source · line 27 · raw
@+b:U32 -> Bool
1110xxxx: mask 0xFFFFFFF0, bits 0xE0.
def is_four_byte_lead source · line 31 · raw
@+b:U32 -> Bool
11110xxx: mask 0xFFFFFFF8, bits 0xF0.
def has_two_byte_shape source · line 34 · raw
@b1:U32 -> @b2:U32 -> Bool
def has_three_byte_shape source · line 37 · raw
@b1:U32 -> @b2:U32 -> @b3:U32 -> Bool
def has_four_byte_shape source · line 41 · raw
@b1:U32 -> @b2:U32 -> @b3:U32 -> @b4:U32 -> Bool
def append_continuation source · line 46 · raw
@+acc:U32 -> @+b:U32 -> U32
Shift in the six payload bits of a continuation byte.
def decode_two_bytes source · line 50 · raw
@+b1:U32 -> @+b2:U32 -> @-valid_shape:{has_two_byte_shape(b1, b2) == True{} : Bool} -> U32Decode only pairs with the two-byte shape, including overlong encodings.
def decode_three_bytes source · line 55 · raw
@+b1:U32 -> @+b2:U32 -> @+b3:U32 -> @-valid_shape:{has_three_byte_shape(b1, b2, b3) == True{} : Bool} -> U32Decode only triples with the three-byte shape, including invalid encodings.
def decode_four_bytes source · line 60 · raw
@+b1:U32 -> @+b2:U32 -> @+b3:U32 -> @+b4:U32 -> @-valid_shape:{has_four_byte_shape(b1, b2, b3, b4) == True{} : Bool} -> U32Decode only sequences with the four-byte shape, including invalid encodings.
def is_surrogate source · line 66 · raw
@+cp:U32 -> Bool
Unicode scalars: at most U+10FFFF, excluding the surrogates U+D800..U+DFFF.
def is_scalar source · line 69 · raw
@+cp:U32 -> Bool
def DecodeState source · line 78 · raw
Type
def decode_start_state source · line 81 · raw
DecodeState
def code_point_to_string source · line 84 · raw
@cp:U32 -> String
def replacement_char_string source · line 88 · raw
String
U+FFFD REPLACEMENT CHARACTER, as a one-character string.
def scalar_if source · line 104 · raw
@+cp:U32 -> @valid:Bool -> Maybe<&2, U32>
Some{cp} when valid, otherwise None.
def one_byte_scalar source · line 112 · raw
@+b1:U32 -> Maybe<&2, U32>
A byte 00..7F is a one-byte sequence, encoding itself.
def two_byte_scalar.of source · line 115 · raw
@+cp:U32 -> Maybe<&2, U32>
def two_byte_scalar.if source · line 118 · raw
@+b1:U32 -> @+b2:U32 -> @shape:Bool -> @-checked:{has_two_byte_shape(b1, b2) == shape : Bool} -> Maybe<&2, U32>
def two_byte_scalar source · line 126 · raw
@+b1:U32 -> @+b2:U32 -> Maybe<&2, U32>
def three_byte_scalar.of source · line 129 · raw
@+cp:U32 -> Maybe<&2, U32>
def three_byte_scalar.if source · line 132 · raw
@+b1:U32 -> @+b2:U32 -> @+b3:U32 -> @shape:Bool -> @-checked:{has_three_byte_shape(b1, b2, b3) == shape : Bool} -> Maybe<&2, U32>
def three_byte_scalar source · line 140 · raw
@+b1:U32 -> @+b2:U32 -> @+b3:U32 -> Maybe<&2, U32>
def four_byte_scalar.of source · line 143 · raw
@+cp:U32 -> Maybe<&2, U32>
def four_byte_scalar.if source · line 146 · raw
@+b1:U32 -> @+b2:U32 -> @+b3:U32 -> @+b4:U32 -> @shape:Bool -> @-checked:{has_four_byte_shape(b1, b2, b3, b4) == shape : Bool} -> Maybe<&2, U32>
def four_byte_scalar source · line 154 · raw
@+b1:U32 -> @+b2:U32 -> @+b3:U32 -> @+b4:U32 -> Maybe<&2, U32>
def sequence_scalar source · line 159 · raw
@seq:List<&2, U32> -> Maybe<&2, U32>
The scalar that seq encodes when seq is exactly one valid sequence,
otherwise None.
def continuation_quarter source · line 196 · raw
@+b:U32 -> U32
Bits 5 and 4 of a continuation byte, which say which quarter of 80..BF it lies in: 0x00 for 80..8F, 0x10 for 90..9F, 0x20 for A0..AF and 0x30 for B0..BF. (Masking keeps the proofs simple: unlike a comparison, the AND doesn't depend on the low bits.)
def is_multibyte_lead source · line 201 · raw
@+b1:U32 -> Bool
Whether b1 begins a sequence of two to four bytes: C2..DF, E0..EF
or F0..F4.
def is_three_byte_second source · line 208 · raw
@+b1:U32 -> @+b2:U32 -> Bool
For a three-byte lead byte b1 and continuation byte b2: whether
b2 is in the second byte's range, A0..BF after E0 and 80..9F after
ED.
def is_four_byte_second source · line 216 · raw
@+b1:U32 -> @+b2:U32 -> Bool
For a four-byte lead byte b1 and continuation byte b2: whether
b1 is F0..F4 and b2 in the second byte's range, 90..BF after F0
and 80..8F after F4.
def is_incomplete_sequence source · line 224 · raw
@seq:List<&2, U32> -> Bool
Whether seq is incomplete: not itself a valid sequence, but the
start of one, so that some more bytes would make it a valid sequence.
def decode_buffer.Step source · line 242 · raw
Type
The text now resolved, and the new pending bytes.
def decode_buffer.prepend source · line 245 · raw
@text:String -> @step:decode_buffer.Step -> decode_buffer.Step
def decode_buffer.keep source · line 251 · raw
@+seq:List<&2, U32> -> @incomplete:Bool -> @otherwise:(@_:Unit -> decode_buffer.Step) -> decode_buffer.Step
Keep an incomplete seq pending, or else do otherwise.
def decode_buffer.advance source · line 263 · raw
@+seq:List<&2, U32> -> @scalar:Maybe<&2, U32> -> @otherwise:(@_:Unit -> decode_buffer.Step) -> decode_buffer.Step
seq is the pending bytes followed by the new byte, and scalar is
sequence_scalar(seq). Emit a complete sequence, keep an incomplete
one, or else do otherwise. The incompleteness test runs only when
seq isn't complete.
def decode_buffer.start source · line 273 · raw
@+b:U32 -> decode_buffer.Step
A byte with nothing pending; on its own it is either a sequence, the start of one, or replaced.
def decode_buffer.extend source · line 280 · raw
@pending:List<&2, U32> -> @+b:U32 -> decode_buffer.Step
A byte after pending bytes. If it cannot continue them, the pending bytes are replaced and the byte starts afresh.
def decode_buffer.step source · line 285 · raw
@pending:List<&2, U32> -> @+b:U32 -> decode_buffer.Step
def decode_buffer.bytes source · line 296 · raw
@bytes:List<&2, U32> -> @stepped:decode_buffer.Step -> @out:String -> decode_buffer.Step
The byte loop, a tail call per byte. stepped is the previous
step's text and pending bytes, and out is the text before that,
reversed so that each step adds to it in time proportional to its own
text. The final text is out reversed, then stepped's text.
def decode_buffer.all_bytes source · line 307 · raw
@bytes:List<&2, U32> -> Bool
def decode_buffer.result source · line 310 · raw
@step:decode_buffer.Step -> Pair(DecodeState, String)
def decode_buffer source · line 315 · raw
@state:DecodeState -> @+buffer:List<&2, U32> -> @-valid:{decode_buffer.all_bytes(buffer) == True{} : Bool} -> Pair(DecodeState, String)Decode the next buffer, whose values must all be bytes.
def decode_done source · line 321 · raw
@state:DecodeState -> String
An unfinished sequence at the end of the input is replaced.