Skip to content

Commit

Permalink
Also add disclaimer on string.from_bytearray
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Mar 14, 2024
1 parent fc3b9d5 commit a80414d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/aiken/string.ak
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ test concat_3() {

/// Convert a `ByteArray` into a `String`
///
/// <br/>⚠️<pre>WARNING</pre> | This functions fails if the underlying `ByteArray` isn't UTF-8-encoded. <br/>In particular, you cannot convert arbitrary hash digests using this function. <br/>For converting arbitrary `ByteArray`s, use [bytearray.to_hex](../bytearray.html#to_hex).
/// --- | ---
///
/// ```aiken
/// string.from_bytearray("foo") == @"foo"
///
/// string.from_bytearray(#"666f6f") == @"foo"
///
/// string.from_bytearray(some_hash) -> fail
/// ```
pub fn from_bytearray(bytes: ByteArray) -> String {
decode_utf8(bytes)
Expand Down
24 changes: 24 additions & 0 deletions lib/scratchpad.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use aiken/bytearray.{concat, push}
use aiken/cbor

pub fn serialise_int(value: Int) -> ByteArray {
let bytes =
#""
|> push(value) // >> 0
|> push(value / 256) // >> 8
|> push(value / 65536) // >> 16
|> push(value / 16777216)
bytes
}

pub fn serialise_long(value: Int) -> ByteArray {
serialise_int(value / 4294967296) |> concat(serialise_int(value))
}

test serialise_long_1() {
serialise_long(10737418240) == #"0000000280000000"
}

test serialise_builtin() {
cbor.serialise(10737418240) == #"1b0000000280000000"
}

0 comments on commit a80414d

Please sign in to comment.