@std/data/utf8.ms#
Import each required symbol with import Name from "@std/data/utf8.ms".
Utf8Error Enum#
Distinguishes text that needs more bytes from text that cannot form a valid scalar.
Example: decode_first(Bytes([194])) returns Utf8Error.Incomplete.
enum Utf8Error {
InvalidData
Incomplete
}Utf8Error.Incomplete#
No bytes are available, or the selected bytes are a proper prefix of one scalar.
IncompleteUtf8Error.InvalidData#
The requested sequence is malformed, overlong, a surrogate or outside Unicode.
InvalidDataUtf8Scalar Type#
A decoded Unicode scalar and the size of its original shortest UTF-8 encoding.
Example: decode_first("é".to_bytes()) returns Utf8Scalar(scalar: 233, width: 2).
type Utf8Scalar {
scalar Int
width Int
}Utf8Scalar.scalar#
Unicode scalar value, excluding surrogates, from zero through U+10FFFF.
scalar IntUtf8Scalar.width#
Number of consumed bytes, from one through four; not a scalar index or grapheme count.
width Intdecode_first Function#
Decodes only the scalar beginning at absolute byte offset at (zero by default). Later malformed bytes do not invalidate an earlier scalar. End of input returns Incomplete. Requires 0 <= at <= data.length(); an offset outside that range faults (negative offsets do not wrap).
fn decode_first(data Bytes, at Int = 0) Result[Utf8Scalar, Utf8Error] { ... }decode_last Function#
Decodes only the scalar ending at exclusive absolute byte offset end, or at the byte length when omitted. Earlier malformed bytes do not invalidate a valid final scalar. An empty prefix returns Incomplete. Requires 0 <= end <= data.length(); an offset outside that range faults. A trailing proper prefix of a scalar returns Incomplete.
fn decode_last(data Bytes, end Int? = null) Result[Utf8Scalar, Utf8Error] { ... }is_valid Function#
Tests the entire byte sequence for strict, shortest UTF-8 without allocating decoded text. Empty bytes are valid. Surrogates, overlong sequences, out-of-range scalars and incomplete sequences are invalid.
fn is_valid(data Bytes) Bool { ... }