decode Utf8() - > Result<string, string>- Module
- std.
bytes - Receiver
- Bytes
- Minimum arity
- 0
- Maximum arity
- 0
Declaration and parameters
LinkAvailability
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
- compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 966 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 52 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 966 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 410 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1296 - compiler/crates/topaz_value/src/value/members.rs at
b48527d0a174· 245
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.
Related rule: Decode UTF-8 without replacing dataResult
The declared result type is Result<string, string>.
Effects
Failures
Result and effects
LinkReturn the exact decoded scalar sequence in Ok. Malformed UTF-8 returns Err without replacement text or a decoded prefix.
Resolved contracts
LinkRelated entries
LinkDecode UTF-8 without replacing data
LinkDecode 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
- compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 107 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 114 -
topaz.stdlib.contract.bytes-utf8atworking-tree
Retain a byte order mark and combining sequence
Linkimport std.bytes { Bytes }import std.ioimport std.testfor hex in (["", "efbbbf", "65cc81", "0d0a", "f09f9880"]) {let decoded = test.assertOk(test.assertOk(Bytes.fromHex(hex)).decodeUtf8())io.writeLine(decoded.encodeUtf8().toHex())}
efbbbf 65cc81 0d0a f09f9880
Expected result
Expected result
Execution completes. Exit code 0.
efbbbf65cc810d0af09f9880
Source evidence
- compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 107 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 114 -
topaz.stdlib.contract.bytes-utf8atworking-tree
Reject malformed UTF-8 categories
Linkimport std.bytes { Bytes }import std.ioimport std.testfor hex in (["80", "c080", "e282", "eda080", "f4908080"]) {match (test.assertOk(Bytes.fromHex(hex)).decodeUtf8()) {case Err(_) => io.writeLine("invalid UTF-8")case Ok(_) => io.writeLine("accepted")}}
invalid UTF-8 invalid UTF-8 invalid UTF-8 invalid UTF-8 invalid UTF-8
Expected result
Expected result
Execution completes. Exit code 0.
invalid UTF-8invalid UTF-8invalid UTF-8invalid UTF-8invalid UTF-8
Source evidence
- compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 107 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 114 -
topaz.stdlib.contract.bytes-utf8atworking-tree