README

#UTF-16

Encoding and decoding between strings and UTF-16 byte sequences, with configurable byte order and BOM handling.

#Encoding

Use encode to convert a string to UTF-16 bytes. Default endianness is little-endian.

///|
test "encode" {
let bytes = @utf16.encode("hi")
inspect(bytes, content="b\"h\\x00i\\x00\"")
}

Use endianness to specify byte order and bom=true to prepend a Byte Order Mark:

///|
test "encode_big_endian_with_bom" {
let bytes = @utf16.encode("hi", endianness=Big, bom=true)
inspect(bytes, content="b\"\\xfe\\xff\\x00h\\x00i\"")
}

#Decoding

Use decode to convert UTF-16 bytes back to a string. Raises Malformed on invalid sequences.

///|
test "decode" {
let bytes : Bytes = b"\x68\x00\x69\x00"
let s = @utf16.decode(bytes)
inspect(s, content="hi")
}

Set ignore_bom=true to strip a leading BOM, or use endianness to specify the byte order explicitly:

///|
test "decode_big_endian" {
let bytes : Bytes = b"\x00\x68\x00\x69"
let s = @utf16.decode(bytes, endianness=Big)
inspect(s, content="hi")
}

#Lossy Decoding

Use decode_lossy to decode bytes that may contain invalid UTF-16, replacing invalid sequences with the Unicode replacement character (U+FFFD).

///|
test "decode_lossy" {
let bytes : Bytes = b"\x00\xD8\x68\x00"
let s = @utf16.decode_lossy(bytes)
inspect(s, content="\u{FFFD}h")
}

#
Malformed

pub suberror Malformed {
Malformed(BytesView)
} derive(
Debug
)

Error type Malformed.

#
Endian

pub(all) enum Endian {
Little
Big
} derive(
Debug
)

Type Endian used by this package APIs.

#
decode

fn decode(bytes : BytesView, ignore_bom? : Bool, endianness? : Endian) -> String raise Malformed

Decode input bytes/text into structured output.

#
decode_lossy

fn decode_lossy(bytes : BytesView, ignore_bom? : Bool, endianness? : Endian) -> String

References :
  • https://www.unicode.org/versions/Unicode16.0.0/core-spec/chapter-3/#G66453

#
encode

fn encode(str : StringView, bom? : Bool, endianness? : Endian) -> Bytes

Encodes a string into a UTF-16 byte array.

Assuming the string is valid.