from Base64(s: string) - > Result<Bytes, string>- Module
- std.
bytes - Receiver
- Not applicable
- Minimum arity
- 1
- Maximum arity
- 1
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· 1523 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 52 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1523 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 84 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1973
Receiver
Parameter constraints
Bytes.toBase64 uses the RFC 4648 standard alphabet with plus and slash. Its output has no whitespace or line wrapping.
A final group containing one byte ends with two equals signs. A final group containing two bytes ends with one equals sign.
Bytes.fromBase64 accepts complete groups of four characters. Padding appears at the end of the final group and all unused bits are zero.
Missing or misplaced padding, nonzero unused bits, whitespace, non-ASCII characters, minus signs, and underscores return Err with a string.
Empty bytes encode as empty text and empty text decodes to Ok with empty Bytes. Invalid text never returns a partial byte sequence.
The input is evaluated once and is unchanged. Encoding then decoding recovers the input bytes exactly without any external effects.
Related rule: Encode and validate padded Base64Result
The declared result type is Result<Bytes, string>.
Effects
Failures
Result and effects
LinkDecode canonical padded Base64 using the standard alphabet. Invalid alphabet, padding, or unused bits return Err.
Resolved contracts
LinkRelated entries
LinkEncode and validate padded Base64
LinkEncode and validate padded Base64
Bytes.toBase64 uses the RFC 4648 standard alphabet with plus and slash. Its output has no whitespace or line wrapping.
A final group containing one byte ends with two equals signs. A final group containing two bytes ends with one equals sign.
Bytes.fromBase64 accepts complete groups of four characters. Padding appears at the end of the final group and all unused bits are zero.
Missing or misplaced padding, nonzero unused bits, whitespace, non-ASCII characters, minus signs, and underscores return Err with a string.
Empty bytes encode as empty text and empty text decodes to Ok with empty Bytes. Invalid text never returns a partial byte sequence.
The input is evaluated once and is unchanged. Encoding then decoding recovers the input bytes exactly without any external effects.
Why
A precise alphabet and padding rule let independent implementations exchange the same bytes. Rejecting nonzero unused bits prevents several texts from representing one accepted encoding.
Source evidence
- compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 158 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 164 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 195 -
topaz.stdlib.contract.bytes-base64atworking-tree
Use standard Base64 vectors
Linkimport std.bytes { Bytes }import std.ioimport std.testfor text in (["", "f", "fo", "foo"]) {io.writeLine(text.encodeUtf8().toBase64())}for text in (["", "Zg==", "Zm8=", "Zm9v"]) {io.writeLine(test.assertOk(Bytes.fromBase64(text)).toHex())}
Zg== Zm8= Zm9v 66 666f 666f6f
Expected result
Expected result
Execution completes. Exit code 0.
Zg==Zm8=Zm9v66666f666f6f
Source evidence
- compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 158 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 164 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 195 -
topaz.stdlib.contract.bytes-base64atworking-tree
Reject padding and alphabet errors
Linkimport std.bytes { Bytes }import std.iofor text in (["Zg", "Zh==", "Zg== ", "_w==", "Zg==Zg==", "=g=="]) {match (Bytes.fromBase64(text)) {case Err(_) => io.writeLine("invalid Base64")case Ok(_) => io.writeLine("accepted")}}
invalid Base64 invalid Base64 invalid Base64 invalid Base64 invalid Base64 invalid Base64
Expected result
Expected result
Execution completes. Exit code 0.
invalid Base64invalid Base64invalid Base64invalid Base64invalid Base64invalid Base64
Source evidence
- compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 158 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 164 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 195 -
topaz.stdlib.contract.bytes-base64atworking-tree