Skip to content
Topaz6.0 · Before release
Download

Search documentation

Specification 6.0 · Not released · Tested product 5.21.3

Use arrow keys to choose a result. Press Enter to open.

Type an API name, diagnostic number, rule, or project topic.

    Library · std.bytes

    Bytes.decodeUtf8

    Decodes this byte sequence as UTF-8 text.

    decodeUtf8() -> Result<string, string>
    Module
    std.bytes
    Receiver
    Bytes
    Minimum arity
    0
    Maximum arity
    0

    Declaration and parameters

    Link

    Availability

    Included by default: Pending

    Import std.bytes in language mode 5.21.

    The current compiler selects std/bytes.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.

    Source evidence

    Receiver

    Bytes

    Parameter constraints

    Bytes.decodeUtf8 returns Ok with the exact Unicode scalar sequence encoded by valid UTF-8 bytes.

    Empty Bytes returns Ok with an empty string. A leading UTF-8 encoding of U+FEFF remains in the returned string.

    Overlong encodings, isolated continuation bytes, incomplete sequences, surrogate encodings, and values above U+10FFFF return Err with a string.

    Decoding does not insert replacement characters, normalize scalars, alter newline sequences, or discard a byte order mark.

    For a string, encodeUtf8 followed by decodeUtf8 returns the original string. Re-encoding any successfully decoded sequence recovers its original bytes.

    The receiver evaluates once and remains unchanged. No text is exposed before the entire sequence has passed validation.

    Result

    The declared result type is Result<string, string>.

    Effects

    Failures

    Result and effects

    Link

    Return the exact decoded scalar sequence in Ok. Malformed UTF-8 returns Err without replacement text or a decoded prefix.

    Resolved contracts

    Link

    Related entries

    Link

    std.bytes

    Decode UTF-8 without replacing data

    Link
    Ruletopaz.stdlib.contract.bytes-utf8.rule

    Decode UTF-8 without replacing data

    Bytes.decodeUtf8 returns Ok with the exact Unicode scalar sequence encoded by valid UTF-8 bytes.

    Empty Bytes returns Ok with an empty string. A leading UTF-8 encoding of U+FEFF remains in the returned string.

    Overlong encodings, isolated continuation bytes, incomplete sequences, surrogate encodings, and values above U+10FFFF return Err with a string.

    Decoding does not insert replacement characters, normalize scalars, alter newline sequences, or discard a byte order mark.

    For a string, encodeUtf8 followed by decodeUtf8 returns the original string. Re-encoding any successfully decoded sequence recovers its original bytes.

    The receiver evaluates once and remains unchanged. No text is exposed before the entire sequence has passed validation.

    Why

    Lossless decoding keeps malformed input visible and preserves distinctions needed by file formats, checksums, and text layout.

    Source evidence

    Retain a byte order mark and combining sequence

    Link
    main.tpzRetain a byte order mark and combining sequenceOpen in Playground
    1. import std.bytes { Bytes }
    2. import std.io
    3. import std.test
    4. for hex in (["", "efbbbf", "65cc81", "0d0a", "f09f9880"]) {
    5. let decoded = test.assertOk(test.assertOk(Bytes.fromHex(hex)).decodeUtf8())
    6. io.writeLine(decoded.encodeUtf8().toHex())
    7. }
    Expected outputexit 0
    
    efbbbf
    65cc81
    0d0a
    f09f9880
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. efbbbf
    2. 65cc81
    3. 0d0a
    4. f09f9880
    Source evidence

    Reject malformed UTF-8 categories

    Link
    main.tpzReject malformed UTF-8 categoriesOpen in Playground
    1. import std.bytes { Bytes }
    2. import std.io
    3. import std.test
    4. for hex in (["80", "c080", "e282", "eda080", "f4908080"]) {
    5. match (test.assertOk(Bytes.fromHex(hex)).decodeUtf8()) {
    6. case Err(_) => io.writeLine("invalid UTF-8")
    7. case Ok(_) => io.writeLine("accepted")
    8. }
    9. }
    Expected outputexit 0
    invalid UTF-8
    invalid UTF-8
    invalid UTF-8
    invalid UTF-8
    invalid UTF-8
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. invalid UTF-8
    2. invalid UTF-8
    3. invalid UTF-8
    4. invalid UTF-8
    5. invalid UTF-8
    Source evidence

    Menu

    Download