std.unicode

Unicode traversal, slicing, and case conversion.

Importuse std.unicode

Functions

fn is_valid(s: string) -> bool#

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))) // false
fn len(s: string) -> int#

Number 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 bytes
fn chars(s: string) -> [string]#

UTF-8 codepoint slices. Invalid bytes are retained as one-element slices.

  • s: Bytes to divide into codepoint slices.
println(std.unicode.chars("A☾é")) // ["A", "☾", "é"]
fn at(s: string, index: int) -> string#

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)) // 9790
fn codepoint_at(s: string, index: int) -> int#

Numeric codepoint at index, or -1 out of range. Invalid bytes decode to U+FFFD.

  • s: Bytes to read from.
  • index: Zero-based codepoint position.
fn slice(s: string, start: int, end: int) -> string#

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)) // ☾é
fn from_codepoint(cp: int) -> string#

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 — surrogate
fn to_upper(s: string) -> string#

Uppercase 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é
fn to_lower(s: string) -> string#

Lowercase using moon's ASCII + Latin-1 table. Other codepoints and invalid bytes pass through unchanged.

  • s: Bytes to convert.