LEARN VOID

Text and byte encodings

Trim a label, count its Unicode scalars and choose an encoding for bytes.

text-encoding.voidDownload source
use text

func entry(extra: i32) -> i32:
    var label = text.trim("  été  ")
    return (label.length as i32) + extra
Input 0Expected result 3

Measure the label

text.trim returns text with surrounding whitespace removed. The label below has three Unicode scalars. Its UTF-8 byte count is larger because é takes two bytes.

Choose a representation

Use to_utf8() for an owned byte list and string.from_utf8(bytes) for validated decoding. For printable byte representations import encoding/hex as hex or encoding/base64 as base64.

Handle malformed input

hex.decode(value) returns a result. Check the error before treating external text as bytes. A byte offset, a Unicode scalar index and a user-perceived character position can all differ.

Continue learning with Strings and Unicode · Binary memory.