encoding::utf8 +linux +x86_64

encoding::utf8 provides helper functions for working with UTF-8-encoded runes, strings, and slices.



type more;

// Undocumented types:
type decoder;


type invalid;


fn decode((str | []u8)) decoder;
fn encoderune(rune) []u8;
fn next(*decoder) (rune | void | more | invalid);
fn prev(*decoder) (rune | void | more | invalid);
fn runesz(rune) size;
fn utf8sz(u8) (size | void);
fn valid((str | []u8)) bool;


type more[link]

type more = void;

Returned when more data is needed, i.e. when an incomplete UTF-8 sequence is encountered.

type decoder[link]

Show undocumented member
type decoder = struct {
	offs: size,
	src: []u8,


type invalid[link]

type invalid = !void;

Returned when an invalid UTF-8 sequence was found.


fn decode[link]

fn decode(src: (str | []u8)) decoder;

Initializes a new UTF-8 decoder.

fn encoderune[link]

fn encoderune(r: rune) []u8;

Encodes a rune as UTF-8 and returns the result as a slice. The return value is statically allocated, and will not be consistent after subsequent calls to encoderune.

fn next[link]

fn next(d: *decoder) (rune | void |
more | invalid);

Returns the next rune from a decoder. void is returned when there are no remaining codepoints.

fn prev[link]

fn prev(d: *decoder) (rune | void |
more | invalid);

Returns the previous rune from a decoder. void is returned when there are no previous codepoints.

fn runesz[link]

fn runesz(r: rune) size;

Returns the size of a rune, in octets, when encoded as UTF-8.

fn utf8sz[link]

fn utf8sz(c: u8) (size | void);

Returns the expected length of a UTF-8 codepoint in bytes given its first byte, or void if the given byte doesn't begin a valid UTF-8 sequence.

fn valid[link]

fn valid(src: (str | []u8)) bool;

Returns true if a given string or byte slice contains only valid UTF-8 sequences. Note that Hare strings (str) are always valid UTF-8 - if this returns false for a str type, something funny is going on.