Functions
Whether s is well-formed UTF-8 (rejecting overlongs, surrogates, and codepoints above U+10FFFF).
s: Bytes to validate.
println(std.unicode.is_valid("héllo")) // true
println(std.unicode.is_valid(std.strings.from_code(255))) // falseNumber of Unicode codepoints, or -1 when s is invalid UTF-8.
s: Bytes to count codepoints in.
println(std.unicode.len("A☾é")) // 3
println("A☾é".len()) // 6 — string.len() counts bytesUTF-8 codepoint slices. Invalid bytes are retained as one-element slices.
s: Bytes to divide into codepoint slices.
println(std.unicode.chars("A☾é")) // ["A", "☾", "é"]Codepoint-position index as its original UTF-8 slice, or "" out of range. Invalid bytes occupy one position and are returned byte-faithfully.
s: Bytes to read from.index: Zero-based codepoint position.
println(std.unicode.at("A☾é", 1)) // ☾
println(std.unicode.codepoint_at("A☾é", 1)) // 9790Numeric codepoint at index, or -1 out of range. Invalid bytes decode to U+FFFD.
s: Bytes to read from.index: Zero-based codepoint position.
Half-open codepoint range [start, end), preserving original bytes. Invalid or reversed positions produce ""; an end beyond the string is clamped to its byte length.
s: Bytes to slice.start: First codepoint position.end: One past the last codepoint position; clamped to the byte length.
println(std.unicode.slice("A☾é", 1, 3)) // ☾éUTF-8 encoding of cp, or "" for a negative value, surrogate, or a value above U+10FFFF.
cp: Scalar value in 0..U+10FFFF, excluding the surrogate range.
println(std.unicode.from_codepoint(0x263E)) // ☾
println(std.unicode.from_codepoint(0xD800) == "") // true — surrogateUppercase using moon's ASCII + Latin-1 table (ß expands to SS). Other codepoints and invalid bytes pass through unchanged.
s: Bytes to convert.
println(std.unicode.to_upper("straße")) // STRASSE
println(std.unicode.to_lower("CAFÉ")) // caféLowercase using moon's ASCII + Latin-1 table. Other codepoints and invalid bytes pass through unchanged.
s: Bytes to convert.