module watt.text.utf

Functions for decoding and encoding UTF-8 strings and characters.

Code Map

//! Functions for decoding and encoding UTF-8 strings and characters.
module watt.text.utf;


//! Encode c as UTF-8.
alias encodeNoGC = vrt_encode_static_u8;

//! Retrieve a UTF-8 character from a particular index.
fn decode(str: scope (StrArg), index: size_t) dchar { }
//! Determine how many codepoints are in a given UTF-8 string.
fn count(s: scope (StrArg)) size_t { }
//! Throws a MalformedUTF8Exception if s is not valid UTF-8.
fn validate(s: scope (StrArg)) { }
//! Encode c onto the end of buf.
fn encode(buf: char[], c: dchar) { }
//! Encode an array of codepoints into a string.
fn encode(arr: dchar[]) string { }
//! Encode c as UTF-8 and add it to arr, starting at index.
fn encode(arr: char[], index: size_t, c: dchar) { }
//! Encode c as UTF-8.
fn encode(c: dchar) string { }
//! Add c to a sink.
fn encode(dgt: scope (Sink), c: dchar) { }
fn decode(str: scope (StrArg), index: size_t) dchar

Retrieve a UTF-8 character from a particular index.

Example

str := "このworldは楽しい";
i: size_t;
assert(decode(str, ref i) == 'こ');
assert(i > 1);

Parameters

str

The string to decode from.

index

The index to decode from. Will be updated to the next character.

fn count(s: scope (StrArg)) size_t

Determine how many codepoints are in a given UTF-8 string.

Example

assert(count("この") == 2);
fn validate(s: scope (StrArg))

Throws a MalformedUTF8Exception if s is not valid UTF-8.

fn encode(buf: char[], c: dchar)

Encode c onto the end of buf.

fn encode(arr: dchar[]) string

Encode an array of codepoints into a string.

fn encode(arr: char[], index: size_t, c: dchar)

Encode c as UTF-8 and add it to arr, starting at index.

fn encode(c: dchar) string

Encode c as UTF-8.

fn encode(dgt: scope (Sink), c: dchar)

Add c to a sink.

alias encodeNoGC

Encode c as UTF-8.