Skip to content

Commit

Permalink
Fix bug unsafe string
Browse files Browse the repository at this point in the history
  • Loading branch information
t0anhh committed Mar 15, 2024
1 parent 02f5181 commit 8d67352
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/stringify.ak
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ fn do_value(self: Value) {
fn(asset) {
let (pid, name, qty) = asset
[
string.from_bytearray(pid) |> with_single_quotes,
string.from_bytearray(name) |> with_single_quotes,
to_safe_string(pid) |> with_single_quotes,
to_safe_string(name) |> with_single_quotes,
string.from_int(qty),
]
|> string.join(@", ")
Expand Down Expand Up @@ -508,11 +508,20 @@ pub fn do_format_cbor(bytes) {
|> first_of_quad
}

pub fn to_safe_code(code: Int) -> Int {
fn to_safe_code(code: Int) -> Int {
if code > 127 {
// fallback to '?' when the code is a special character
63
} else {
code
}
}

fn to_safe_string(bytes: ByteArray) {
bytearray.foldr(
bytes,
"",
fn(byte, normed_bytes) { bytearray.push(normed_bytes, to_safe_code(byte)) },
)
|> bytearray.to_string
}

0 comments on commit 8d67352

Please sign in to comment.