Skip to main content

60  unicode standard library

Strings in Edva are raw UTF-8 byte sequences. The unicode module provides codepoint classification, UTF-8 byte decoding, and rune boundary helpers.

#use unicode

60.1 Function catalog

Category Function Signature Return Type Description
Classification unicode::is_alpha(r) Rune => <> Flag <> True if rune is Unicode alphabetic
unicode::is_digit(r) Rune => <> Flag <> True if rune is Unicode decimal digit
unicode::is_upper(r) Rune => <> Flag <> True if rune is Unicode uppercase letter
unicode::is_lower(r) Rune => <> Flag <> True if rune is Unicode lowercase letter
unicode::is_space(r) Rune => <> Flag <> True if rune is Unicode whitespace
Decoding unicode::decode_rune(s, off) String, Int => Rune Rune Decodes the UTF-8 codepoint starting at byte offset off
unicode::utf8_width(r) Rune => Int Int Returns the encoded UTF-8 byte width (1 to 4 bytes)

60.2 Codepoint operations & operators

Operation Syntax Description Example
Codepoint count ??s Unary prefix ?? counts Unicode codepoints (not bytes) ??("café") \(\rightarrow\) 4
Byte count ?s Unary prefix ? counts raw UTF-8 bytes ?("café") \(\rightarrow\) 5
Rune literal 'λ' 32-bit scalar Unicode character code r: Rune = 'λ'

60.3 Iterating over Unicode codepoints

Because strings are byte-indexed (s(i) returns a byte), decoding a multibyte UTF-8 string requires advancing by the rune’s decoded byte width:

#use unicode
#use io

text = "Hello, 世界! 🌍"

// Walk all codepoints in a UTF-8 string:
byte_len = ?text
offset' = 0
0.. @
   offset' !< byte_len | -->
   r = unicode::decode_rune(text, offset')
   width = unicode::utf8_width(r)

   io::outf((Int(r), width), "Rune codepoint {} (width {} bytes)")
   offset' = offset' + width