Skip to content

Commit

Permalink
Update to moonc v0.1.20241202+8756d160d
Browse files Browse the repository at this point in the history
Signed-off-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com>
  • Loading branch information
gmlewis committed Dec 3, 2024
1 parent d586dc4 commit 846ca4e
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ The code has been updated to support compiler:

```bash
$ moon version --all
moon 0.1.20241115 (67c2b06 2024-11-15) ~/.moon/bin/moon
moonc v0.1.20241115+351cfb074 ~/.moon/bin/moonc
moonrun 0.1.20241115 (67c2b06 2024-11-15) ~/.moon/bin/moonrun
moon 0.1.20241202 (28bfce6 2024-12-02) ~/.moon/bin/moon
moonc v0.1.20241202+8756d160d ~/.moon/bin/moonc
moonrun 0.1.20241202 (28bfce6 2024-12-02) ~/.moon/bin/moonrun
```

Use `moonup` to manage `moon` compiler versions:
Expand Down
18 changes: 12 additions & 6 deletions base64-decode.mbt
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/// This package is based on the Go implementation found here:
///| 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.

pub(all) type! CorruptInputError String derive(Show)

/// `std_decode2bytes` base64-decodes the provided bytes using Standard encoding.
///| `std_decode2bytes` base64-decodes the provided bytes using Standard encoding.
/// Setting `no_padding=true` is used for raw decoding.
pub fn std_decode2bytes(
src : String,
Expand All @@ -22,7 +21,7 @@ pub fn std_decode2bytes(
Bytes::from_iter(buf.iter().take(n))
}

/// `std_decode2str` base64-decodes the provided bytes using Standard encoding and returns a String.
///| `std_decode2str` base64-decodes the provided bytes using Standard encoding and returns a String.
/// Setting `no_padding=true` is used for raw decoding.
pub fn std_decode2str(
src : String,
Expand All @@ -31,7 +30,7 @@ pub fn std_decode2str(
bytes2str(std_decode2bytes!(src, no_padding~))
}

/// `url_decode2bytes` base64-decodes the provided bytes using URL encoding.
///| `url_decode2bytes` base64-decodes the provided bytes using URL encoding.
/// Setting `no_padding=true` is used for raw decoding.
pub fn url_decode2bytes(
src : String,
Expand All @@ -46,7 +45,7 @@ pub fn url_decode2bytes(
Bytes::from_iter(buf.iter().take(n))
}

/// `url_decode2str` base64-decodes the provided bytes using URL encoding and returns a String.
///| `url_decode2str` base64-decodes the provided bytes using URL encoding and returns a String.
/// Setting `no_padding=true` is used for raw decoding.
pub fn url_decode2str(
src : String,
Expand All @@ -55,6 +54,7 @@ pub fn url_decode2str(
bytes2str(url_decode2bytes!(src, no_padding~))
}

///|
fn decode_quantum(
decode_map : FixedArray[Byte],
dst : FixedArray[Byte],
Expand Down Expand Up @@ -152,6 +152,7 @@ fn decode_quantum(
return (si, dlen - 1)
}

///|
fn decode_buf(
decode_map : FixedArray[Byte],
dst : FixedArray[Byte],
Expand Down Expand Up @@ -196,6 +197,7 @@ fn decode_buf(
n
}

///|
fn be_put_uint32(b : FixedArray[Byte], offset : Int, value : UInt) -> Unit {
b[offset + 3] = (value & 0xff).to_byte()
b[offset + 2] = ((value >> 8) & 0xff).to_byte()
Expand All @@ -206,6 +208,7 @@ fn be_put_uint32(b : FixedArray[Byte], offset : Int, value : UInt) -> Unit {
// assemble32 assembles 4 base64 digits into 3 bytes.
// Each digit comes from the decode map, and will be 0xff
// if it came from an invalid character.
///|
fn assemble32(n1 : Byte, n2 : Byte, n3 : Byte, n4 : Byte) -> (UInt, Bool) {
// Check that all the digits are valid. If any of them was 0xff, their
// bitwise OR will be 0xff.
Expand All @@ -219,13 +222,15 @@ fn assemble32(n1 : Byte, n2 : Byte, n3 : Byte, n4 : Byte) -> (UInt, Bool) {
)
}

///|
fn decoded_len(n : Int, no_padding : Bool) -> Int {
if no_padding {
return n / 4 * 3 + n % 4 * 6 / 8
}
n / 4 * 3
}

///|
let std_decode_map : FixedArray[Byte] = [
b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff',
b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff',
Expand Down Expand Up @@ -258,6 +263,7 @@ let std_decode_map : FixedArray[Byte] = [
b'\xff', b'\xff', b'\xff', b'\xff',
]

///|
let url_decode_map : FixedArray[Byte] = [
b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff',
b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff', b'\xff',
Expand Down
13 changes: 9 additions & 4 deletions base64-encode.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// Use of this source code is governed by a BSD-style
/// license that can be found in the LICENSE file.

/// `std_encode2bytes` base64-encodes the provided bytes using Standard encoding.
///| `std_encode2bytes` base64-encodes the provided bytes using Standard encoding.
/// Setting `no_padding=true` is used for raw encoding.
pub fn std_encode2bytes(
src : FixedArray[Byte],
Expand All @@ -19,7 +19,7 @@ pub fn std_encode2bytes(
Bytes::from_iter(buf.iter())
}

/// `std_encode2str` base64-encodes the provided bytes using Standard encoding and returns a String.
///| `std_encode2str` base64-encodes the provided bytes using Standard encoding and returns a String.
/// Setting `no_padding=true` is used for raw encoding.
pub fn std_encode2str(
src : FixedArray[Byte],
Expand All @@ -28,7 +28,7 @@ pub fn std_encode2str(
bytes2str(std_encode2bytes(src, no_padding~))
}

/// `url_encode2bytes` base64-encodes the provided bytes using URL encoding.
///| `url_encode2bytes` base64-encodes the provided bytes using URL encoding.
/// Setting `no_padding=true` is used for raw encoding.
pub fn url_encode2bytes(
src : FixedArray[Byte],
Expand All @@ -42,7 +42,7 @@ pub fn url_encode2bytes(
Bytes::from_iter(buf.iter())
}

/// `url_encode2str` base64-encodes the provided bytes using URL encoding and returns a String.
///| `url_encode2str` base64-encodes the provided bytes using URL encoding and returns a String.
/// Setting `no_padding=true` is used for raw encoding.
pub fn url_encode2str(
src : FixedArray[Byte],
Expand All @@ -51,6 +51,7 @@ pub fn url_encode2str(
bytes2str(url_encode2bytes(src, no_padding~))
}

///|
fn encode_buf(
encode : FixedArray[Byte],
dst : FixedArray[Byte],
Expand Down Expand Up @@ -99,6 +100,7 @@ fn encode_buf(
}
}

///|
fn encoded_len(n : Int, no_padding : Bool) -> Int {
if no_padding {
n / 3 * 4 + (n % 3 * 8 + 5) / 6 // minimum # chars at 6 bits per char
Expand All @@ -107,16 +109,19 @@ fn encoded_len(n : Int, no_padding : Bool) -> Int {
}
}

///|
let std_encoding : FixedArray[Byte] = FixedArray::from_array(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
.to_array()
.map(fn(c) { c.to_int().to_byte() }),
)

///|
let url_encoding : FixedArray[Byte] = FixedArray::from_array(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
.to_array()
.map(fn(c) { c.to_int().to_byte() }),
)

///|
let pad_char = b'='
9 changes: 9 additions & 0 deletions base64-encode_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ test "std_encode2str: empty input" {
inspect!(@base64.std_encode2str(input), content="")
}

///|
let pairs = [
// RFC 3548 examples
(b"\x14\xfb\x9c\x03\xd9\x7e", "FPucA9l+"),
Expand Down Expand Up @@ -114,39 +115,47 @@ let pairs = [
]

// Do nothing to a reference base64 string (leave in standard format)
///|
fn std_ref(s : String) -> String {
s
}

// Convert a reference string to URL-encoding
///|
fn url_ref(s : String) -> String {
let s = @string.replace_all(s, old="+", new="-")
let s = @string.replace_all(s, old="/", new="_")
s
}

// Convert a reference string to raw, unpadded format
///|
fn raw_ref(s : String) -> String {
@string.trim_end(s, "=")
}

// Both URL and unpadding conversions
///|
fn raw_url_ref(s : String) -> String {
raw_ref(url_ref(s))
}

///|
fn std_encode2str(b : FixedArray[Byte]) -> String {
@base64.std_encode2str(b)
}

///|
fn url_encode2str(b : FixedArray[Byte]) -> String {
@base64.url_encode2str(b)
}

///|
fn raw_std_encode2str(b : FixedArray[Byte]) -> String {
@base64.std_encode2str(b, no_padding=true)
}

///|
fn raw_url_encode2str(b : FixedArray[Byte]) -> String {
@base64.url_encode2str(b, no_padding=true)
}
Expand Down
2 changes: 1 addition & 1 deletion bytes-2-str.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// `bytes2str` decodes a UTF-8 `Bytes` to a UTF-16 `String`.
///| `bytes2str` decodes a UTF-8 `Bytes` to a UTF-16 `String`.
pub fn bytes2str(b : Bytes) -> String {
let chars = (b.iter() |> @decoder.utf8()).collect()
let buf = @buffer.new(size_hint=chars.length())
Expand Down
1 change: 1 addition & 0 deletions decoder/lib.mbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2024 peter-jerry-ye
// SPDX-License-Identifier: MIT

///|
pub fn utf8(bytes : Iter[Byte]) -> Iter[Char] {
Iter::new(
fn(f) {
Expand Down
1 change: 1 addition & 0 deletions encoder/lib.mbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2024 peter-jerry-ye
// SPDX-License-Identifier: MIT

///|
pub fn utf8(string : Iter[Char]) -> Iter[Byte] {
Iter::new(
fn(f) {
Expand Down
2 changes: 1 addition & 1 deletion moon.mod.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gmlewis/base64",
"version": "0.10.0",
"version": "0.11.0",
"deps": {},
"readme": "README.md",
"repository": "https://github.com/gmlewis/moonbit-base64",
Expand Down
2 changes: 1 addition & 1 deletion str-2-bytes.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// `str2bytes` encodes a UTF-16 `String` as a UTF-8 `Bytes`.
///| `str2bytes` encodes a UTF-16 `String` as a UTF-8 `Bytes`.
pub fn str2bytes(s : String) -> Bytes {
s.iter() |> @encoder.utf8() |> Bytes::from_iter()
}
Expand Down

0 comments on commit 846ca4e

Please sign in to comment.