encoding::utf8
encoding::utf8 provides helper functions for working with UTF-8-encoded runes,
strings, and slices.
Index
Types
type more;
type decoder;
Errors
type invalid;
Functions
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;
Types
type more
type more = void;
Returned when more data is needed, i.e. when an incomplete UTF-8 sequence is
encountered.
type decoder
Show undocumented member
type decoder = struct {
offs: size,
src: []u8,
};
Errors
type invalid
type invalid = !void;
Returned when an invalid UTF-8 sequence was found.
Functions
fn decode
fn decode(src: (str | []u8)) decoder;
Initializes a new UTF-8 decoder.
fn encoderune
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
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
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
fn runesz(r: rune) size;
Returns the size of a rune, in octets, when encoded as UTF-8.
fn utf8sz
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
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.