UTF-8
Signature
utf8.encode(text: string): u8[]Signature
utf8.decode(bytes: u8[]): Result<string, EncodingError>Signature
utf8.decodeLossy(bytes: u8[]): stringImport utf8 from tyrus:encoding. encode(text) returns the UTF-8 bytes of a string. decode(bytes) accepts only well-formed UTF-8 and returns Err(EncodingError) otherwise. decodeLossy(bytes) instead replaces each malformed sequence with U+FFFD.
tyrus
import { EncodingError, utf8 } from "tyrus:encoding";
const bytes: u8[] = utf8.encode("hello 🌍");
const malformed: u8[] = [255];
try {
print(utf8.decode(bytes)); // hello 🌍
} catch (error: EncodingError) {
print(error.code);
}
print(utf8.decodeLossy(malformed)); // �UTF-8 has no little-endian or big-endian form: its encoded values are sequences of individually defined bytes. Endianness applies only when a character encoding uses multi-byte code units, such as UTF-16.
