silk/utf8_decoder
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc.
Incremental UTF-8 decoding across byte-chunk boundaries without allocation.
When to use
Use Utf8Decoder when bytes arrive in chunks and a sequence can cross a chunk boundary.
Use String.fromUtf8 when all bytes are available in one complete slice.
Details
Feed bytes in order with step. A complete scalar resets the decoder for the next byte.
The caller supplies each byte's offset so an invalid result identifies that byte.
Gotchas
If input ends while pending returns true, the caller must report incomplete input.
After an invalid result, create a new decoder before processing more bytes.
Import as Utf8Decoder with import silk.utf8_decoder { Utf8Decoder }.
Public declarations: 2.
Utf8Step
pub union Utf8StepThe result of validating one UTF-8 byte.
Scalar
Utf8Step.Scalar { value: u32 }: Utf8StepA complete Unicode scalar value.
Field value
pub value: u32The decoded scalar value.
NeedMore
Utf8Step.NeedMore: Utf8StepMore continuation bytes are required to complete the scalar.
Invalid
Utf8Step.Invalid { offset: usize }: Utf8StepThe byte cannot complete a valid UTF-8 sequence.
Field offset
pub offset: usizeThe caller-supplied offset of the invalid byte.
Utf8Decoder
pub struct Utf8DecoderAllocation-free state for one UTF-8 scalar at a time across byte chunks.
Details
A completed scalar leaves this state ready for the next sequence. An incomplete sequence remains available for the next chunk. This state does not retain a byte slice.
Associated function Utf8Decoder.make
pub fn make() -> Utf8DecoderCreates a decoder ready for the first byte of a UTF-8 sequence.
Method Utf8Decoder.pending
pub fn pending<'life0>(self: &'life0 silk/utf8_decoder.Utf8Decoder) -> boolReturns true if the current scalar still needs continuation bytes.
Method Utf8Decoder.step
pub fn step<'life0>(self: &'life0 mut silk/utf8_decoder.Utf8Decoder, byte: u8, offset: usize) -> Utf8StepValidates one byte and returns a scalar, a need-more result, or the invalid byte offset.
Details
Supply consecutive bytes in order, including across chunk boundaries. offset is the
absolute position of byte in the caller's input. A complete scalar resets the decoder.
Gotchas
Overlong sequences, surrogate values, and values above 0x10FFFF are invalid. If input
ends while pending is true, the caller must report incomplete input.