Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

The text module

Converting text to and from bytes in a given encoding.

Bring it in with import text. A String always holds UTF-8; these types encode one into the bytes of an encoding (u8[]) and decode bytes back. Each decode is strict and gives none when the bytes are not valid in the encoding (code page 437’s always succeeds, since every byte is a character); each decode_lossy replaces what is invalid with U+FFFD and always succeeds.

Example

import text;

main(): int {
    var bytes = text.Utf8.encode("héllo");
    assert(bytes.count == 6, "é takes two bytes");
    assert(text.Utf8.decode(bytes).value() == "héllo", "round trip");
    return 0;
}

Source: runtime/text/module.pep