From 8d673529814736cc2601f62e013f3793999effb3 Mon Sep 17 00:00:00 2001 From: Toanh Date: Fri, 15 Mar 2024 17:27:37 +0700 Subject: [PATCH] Fix bug unsafe string --- lib/stringify.ak | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/stringify.ak b/lib/stringify.ak index 14c2892..84e0a97 100644 --- a/lib/stringify.ak +++ b/lib/stringify.ak @@ -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(@", ") @@ -508,7 +508,7 @@ 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 @@ -516,3 +516,12 @@ pub fn to_safe_code(code: Int) -> Int { 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 +}