base64

MoonBit base64: provide simple base64 operations.

base64
moonbit
moon add gmlewis/base64@0.16.11
Download zip
Author
Version
0.16.11
License
Apache-2.0
Last updated
29 days ago
Downloads
34K
README

#gmlewis/base64

check

This is a simple base64 encoder based on Go's implementation: https://cs.opensource.google/go/go/+/master:src/encoding/base64/base64.go which has the copyright notice:

// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file.

The UTF-16 / UTF-8 encoder/decoders are provided with permission by @peter-jerry-ye from this repo: https://github.com/peter-jerry-ye/jstream

#Status

The code has been updated to support compiler:

$ moon version --all moon 0.1.20260330 (c527f57 2026-03-30) ~/.moon/bin/moon moonc v0.8.4+4d98d95d4 (2026-03-30) ~/.moon/bin/moonc moonrun 0.1.20260330 (c527f57 2026-03-30) ~/.moon/bin/moonrun moon-pilot 0.0.1-df92511 (2026-03-30) ~/.moon/bin/moon-pilot

#
CorruptInputError

pub(all) suberror CorruptInputError {
CorruptInputError(String)
} derive(Show)

This package is based on the Go implementation found here: https://cs.opensource.google/go/go/+/master:src/encoding/base64/base64.go which has the copyright notice: Copyright 2009 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

#
array2str

fn array2str(b : ArrayView[Byte]) -> String

array2str decodes a UTF-8 ArrayView[Byte] to a UTF-16 String. This is a more convenient version of bytes2str for when you have an ArrayView[Byte]. If you have an Array[Byte], you can use array2str(arr[:]).

Examples:
let arr = b"+/ Hello, 世界! 🌍".to_array() inspect(array2str(arr[:]), content="+/ Hello, 世界! 🌍")

#
bytes2str

fn bytes2str(b : Bytes) -> String

bytes2str decodes a UTF-8 Bytes to a UTF-16 String.

Examples:
inspect(bytes2str("+/ Hello, 世界! 🌍"), content="+/ Hello, 世界! 🌍")

#
std_decode2bytes

fn std_decode2bytes(src : String, no_padding? : Bool) -> Bytes raise CorruptInputError

std_decode2bytes base64-decodes the provided bytes using Standard encoding. Setting no_padding=true is used for raw decoding.

Note that standard encoding uses + and / characters whereas URL encoding uses - and _.

Examples:
let b = "+/ Hello, 世界! 🌍" inspect(std_decode2bytes("Ky8gSGVsbG8sIOS4lueVjCEg8J+MjQ==") |> bytes2str(), content=b) inspect(std_decode2bytes("Ky8gSGVsbG8sIOS4lueVjCEg8J+MjQ", no_padding=true) |> bytes2str(), content=b)

#
std_decode2str

fn std_decode2str(src : String, no_padding? : Bool) -> String raise CorruptInputError

std_decode2str base64-decodes the provided bytes using Standard encoding and returns a String. Setting no_padding=true is used for raw decoding.

Note that standard encoding uses + and / characters whereas URL encoding uses - and _.

Examples:
let b = "+/ Hello, 世界! 🌍" inspect(std_decode2str("Ky8gSGVsbG8sIOS4lueVjCEg8J+MjQ=="), content=b) inspect(std_decode2str("Ky8gSGVsbG8sIOS4lueVjCEg8J+MjQ", no_padding=true), content=b)

#
std_encode2bytes

fn std_encode2bytes(src : FixedArray[Byte], no_padding? : Bool) -> Bytes

std_encode2bytes base64-encodes the provided bytes using Standard encoding. Setting no_padding=true is used for raw encoding.

Note that standard encoding uses + and / characters whereas URL encoding uses - and _.

Examples:
let b : Bytes = "+/ Hello, 世界! 🌍" inspect(std_encode2bytes(b.to_fixedarray()) |> bytes2str(), content="Ky8gSGVsbG8sIOS4lueVjCEg8J+MjQ==") inspect(std_encode2bytes(b.to_fixedarray(), no_padding=true) |> bytes2str(), content="Ky8gSGVsbG8sIOS4lueVjCEg8J+MjQ")

#
std_encode2str

fn std_encode2str(src : FixedArray[Byte], no_padding? : Bool) -> String

std_encode2str base64-encodes the provided bytes using Standard encoding and returns a String. Setting no_padding=true is used for raw encoding.

Note that standard encoding uses + and / characters whereas URL encoding uses - and _.

Examples:
let b : Bytes = "+/ Hello, 世界! 🌍" inspect(std_encode2str(b.to_fixedarray()), content="Ky8gSGVsbG8sIOS4lueVjCEg8J+MjQ==") inspect(std_encode2str(b.to_fixedarray(), no_padding=true), content="Ky8gSGVsbG8sIOS4lueVjCEg8J+MjQ")

#
str2array

fn str2array(s : String) -> Array[Byte]

str2array encodes a UTF-16 String as a UTF-8 Array[Byte]. This is a more convenient version of str2bytes for when you want an Array[Byte].

Examples:
inspect(str2array("🌍"), content="[b'\\xF0', b'\\x9F', b'\\x8C', b'\\x8D']")

#
str2bytes

fn str2bytes(s : String) -> Bytes

str2bytes encodes a UTF-16 String as a UTF-8 Bytes.

Examples:
inspect(str2bytes("+/ Hello, 世界! 🌍"), content=( #|b"+/ Hello, \xe4\xb8\x96\xe7\x95\x8c! \xf0\x9f\x8c\x8d" ))

#
url_decode2bytes

fn url_decode2bytes(src : String, no_padding? : Bool) -> Bytes raise CorruptInputError

url_decode2bytes base64-decodes the provided bytes using URL encoding. Setting no_padding=true is used for raw decoding.

Note that standard encoding uses + and / characters whereas URL encoding uses - and _.

Examples:
let b = "+/ Hello, 世界! 🌍" inspect(url_decode2bytes("Ky8gSGVsbG8sIOS4lueVjCEg8J-MjQ==") |> bytes2str(), content=b) inspect(url_decode2bytes("Ky8gSGVsbG8sIOS4lueVjCEg8J-MjQ", no_padding=true) |> bytes2str(), content=b)

#
url_decode2str

fn url_decode2str(src : String, no_padding? : Bool) -> String raise CorruptInputError

url_decode2str base64-decodes the provided bytes using URL encoding and returns a String. Setting no_padding=true is used for raw decoding.

Note that standard encoding uses + and / characters whereas URL encoding uses - and _.

Examples:
let b = "+/ Hello, 世界! 🌍" inspect(url_decode2str("Ky8gSGVsbG8sIOS4lueVjCEg8J-MjQ=="), content=b) inspect(url_decode2str("Ky8gSGVsbG8sIOS4lueVjCEg8J-MjQ", no_padding=true), content=b)

#
url_encode2bytes

fn url_encode2bytes(src : FixedArray[Byte], no_padding? : Bool) -> Bytes

url_encode2bytes base64-encodes the provided bytes using URL encoding. Setting no_padding=true is used for raw encoding.

Note that standard encoding uses + and / characters whereas URL encoding uses - and _.

Examples:
let b : Bytes = "+/ Hello, 世界! 🌍" inspect(url_encode2bytes(b.to_fixedarray()) |> bytes2str(), content="Ky8gSGVsbG8sIOS4lueVjCEg8J-MjQ==") inspect(url_encode2bytes(b.to_fixedarray(), no_padding=true) |> bytes2str(), content="Ky8gSGVsbG8sIOS4lueVjCEg8J-MjQ")

#
url_encode2str

fn url_encode2str(src : FixedArray[Byte], no_padding? : Bool) -> String

url_encode2str base64-encodes the provided bytes using URL encoding and returns a String. Setting no_padding=true is used for raw encoding.

Note that standard encoding uses + and / characters whereas URL encoding uses - and _.

Examples:
let b : Bytes = "+/ Hello, 世界! 🌍" inspect(url_encode2str(b.to_fixedarray()), content="Ky8gSGVsbG8sIOS4lueVjCEg8J-MjQ==") inspect(url_encode2str(b.to_fixedarray(), no_padding=true), content="Ky8gSGVsbG8sIOS4lueVjCEg8J-MjQ")