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 | |
parent | 694e2faea0a03ed2a98e0e1b0ef9bdde70321204 (diff) | |
download | gleam_stdlib-b69639b12d9ebd96d430cfcdebd69417713ac9ed.tar.gz gleam_stdlib-b69639b12d9ebd96d430cfcdebd69417713ac9ed.zip |
switch part to return result
-rw-r--r-- | src/gleam/binary.gleam | 19 | ||||
-rw-r--r-- | src/gleam/binary_native.erl | 7 | ||||
-rw-r--r-- | test/gleam/binary_test.gleam | 24 |
3 files changed, 38 insertions, 12 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>>}; diff --git a/test/gleam/binary_test.gleam b/test/gleam/binary_test.gleam index cf56076..20b7cc2 100644 --- a/test/gleam/binary_test.gleam +++ b/test/gleam/binary_test.gleam @@ -12,19 +12,35 @@ pub fn length_test() { pub fn part_test() { binary.from_string("hello") |> binary.part(0, 5) - |> should.equal(binary.from_string("hello")) + |> should.equal(Ok(binary.from_string("hello"))) binary.from_string("hello") |> binary.part(0, 0) - |> should.equal(binary.from_string("")) + |> should.equal(Ok(binary.from_string(""))) binary.from_string("hello") |> binary.part(2, 2) - |> should.equal(binary.from_string("ll")) + |> should.equal(Ok(binary.from_string("ll"))) binary.from_string("hello") |> binary.part(5, -2) - |> should.equal(binary.from_string("lo")) + |> should.equal(Ok(binary.from_string("lo"))) + + binary.from_string("") + |> binary.part(0, 0) + |> should.equal(Ok(binary.from_string(""))) + + binary.from_string("hello") + |> binary.part(6, 0) + |> should.equal(Error(Nil)) + + binary.from_string("hello") + |> binary.part(-1, 1) + |> should.equal(Error(Nil)) + + binary.from_string("hello") + |> binary.part(1, 6) + |> should.equal(Error(Nil)) } pub fn u32_test() { |