diff options
author | Peter Saxton <peterhsaxton@gmail.com> | 2020-05-28 16:45:00 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2020-05-29 13:06:51 +0100 |
commit | b69639b12d9ebd96d430cfcdebd69417713ac9ed (patch) | |
tree | 4bc957f895b75055d81ab0d2fcec1992e96392d1 /src | |
parent | 694e2faea0a03ed2a98e0e1b0ef9bdde70321204 (diff) | |
download | gleam_stdlib-b69639b12d9ebd96d430cfcdebd69417713ac9ed.tar.gz gleam_stdlib-b69639b12d9ebd96d430cfcdebd69417713ac9ed.zip |
switch part to return result
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/binary.gleam | 19 | ||||
-rw-r--r-- | src/gleam/binary_native.erl | 7 |
2 files changed, 18 insertions, 8 deletions
diff --git a/src/gleam/binary.gleam b/src/gleam/binary.gleam index a45ae5d..c36b941 100644 --- a/src/gleam/binary.gleam +++ b/src/gleam/binary.gleam @@ -7,20 +7,25 @@ pub external type Binary pub external fn from_string(String) -> Binary = "gleam_stdlib" "identity" +/// Returns an integer which is the number of bytes in the binary. +pub external fn byte_size(Binary) -> Int = +"erlang" "byte_size" + /// Extracts the part of a binary. /// /// Binary part will start at given position and continue up to specified length. /// A negative length can be used to extract bytes at the end of a binary: -pub external fn part(string: Binary, position: Int, length: Int) -> Binary = - "binary" "part" - -/// Returns an integer which is the number of bytes in the binary. -pub external fn byte_size(Binary) -> Int = - "erlang" "byte_size" +pub external fn part(string: Binary, position: Int, length: Int) -> Result(Binary, Nil) = +"binary_native" "part" -// Not sure these will be necessary with bit syntax for for now I think they are +/// Convert integer to unsigned 32 bits. +/// +/// Returns an error if integer is less than zero or equal to or larger than 2^32. pub external fn int_to_u32(Int) -> Result(Binary, Nil) = "binary_native" "int_to_u32" +/// Convert unsigned 32 bits to Integer +/// +/// Returns an error if the binary is not 32 bits in length. pub external fn int_from_u32(Binary) -> Result(Int, Nil) = "binary_native" "int_from_u32" diff --git a/src/gleam/binary_native.erl b/src/gleam/binary_native.erl index d7e774c..1969602 100644 --- a/src/gleam/binary_native.erl +++ b/src/gleam/binary_native.erl @@ -1,5 +1,10 @@ -module (binary_native). --export ([int_to_u32/1, int_from_u32/1]). +-export ([int_to_u32/1, int_from_u32/1, part/3]). + +part(Bin, Pos, Len) -> + try {ok, binary:part(Bin, Pos, Len)} catch + error:badarg -> {error, nil} + end. int_to_u32(I) when 0 =< I, I < 4294967296 -> {ok, <<I:32>>}; |