std.encoding

Base64 and hexadecimal encoding.

Importuse std.encoding

Functions

fn base64_encode(s: string) -> string#

Standard-alphabet base64 with '=' padding.

  • s: Bytes to encode.
let encoded = std.encoding.base64_encode("moon")
println(encoded)                            // bW9vbg==
println(std.encoding.base64_decode(encoded)) // moon
match std.encoding.try_base64_decode("not base64") {
  ok(value) => println(value),
  err(failure) => println(failure.message),
}
fn base64_decode(s: string) -> string#

Decodes padded standard-alphabet base64; errors on malformed input.

  • s: Padded standard-alphabet text to decode.
fn try_base64_decode(s: string) -> Result<string, Error>#

base64_decode as a value: malformed input is err.Encoding, not a terminated process. Decoders run on input from outside the program, so the sentinel form is the wrong default for anything holding a request.

  • s: Padded standard-alphabet text to decode.
fn base64_url_encode(s: string) -> string#

URL-safe-alphabet ('-'/'_') base64 with '=' padding.

  • s: Bytes to encode.
let padded = std.encoding.base64_url_encode("moon?>")
println(std.encoding.base64_url_decode(padded))
println(std.encoding.try_base64_url_decode(padded).unwrap())

let token = std.encoding.base64_raw_url_encode("moon?>")
println(std.encoding.try_base64_raw_url_decode(token).unwrap())
fn base64_url_decode(s: string) -> string#

Decodes padded URL-safe base64; errors on malformed input.

  • s: Padded URL-safe text to decode.
fn try_base64_url_decode(s: string) -> Result<string, Error>#

base64_url_decode as a value. See try_base64_decode.

  • s: Padded URL-safe text to decode.
fn base64_raw_url_encode(s: string) -> string#

URL-safe base64 with the padding left off — Go's base64.RawURLEncoding, and what a value that travels in a URL path is usually spelled as.

  • s: Bytes to encode.
fn try_base64_raw_url_decode(s: string) -> Result<string, Error>#

The strict decoder for the above, and strict on purpose: it rejects padding, the standard alphabet, and a length ≡ 1 (mod 4), because a permissive one resolves two spellings of an identifier to one value. That matters wherever the identifier names a resource — two URLs that should be distinct stop being.

Result-returning only. Input in this shape comes from outside the program by definition, so a sentinel form would be the wrong default (see try_base64_decode).

  • s: Unpadded URL-safe text; padding, the standard alphabet, and a length ≡ 1 (mod 4) are all rejected.
fn hex_encode(s: string) -> string#

Lowercase hex, two digits per byte.

  • s: Bytes to encode.
let encoded = std.encoding.hex_encode("moon")
println(encoded)                         // 6d6f6f6e
println(std.encoding.hex_decode(encoded)) // moon
println(std.encoding.try_hex_decode("xyz").is_err())
fn hex_decode(s: string) -> string#

Decodes hex (either case); errors on odd length or an invalid digit.

  • s: Hex text of even length, in either case.
fn try_hex_decode(s: string) -> Result<string, Error>#

hex_decode as a value. See try_base64_decode.

  • s: Hex text of even length, in either case.