Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3442652

Browse files
committedApr 25, 2024
bit array for erlang
1 parent 00af688 commit 3442652

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed
 

‎src/gleam/bit_array.gleam

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ pub fn slice(
4545
take length: Int,
4646
) -> Result(BitArray, Nil)
4747

48+
@external(erlang, "gleam_stdlib", "bit_array_split")
49+
pub fn split(x: BitArray, on subpattern: BitArray) -> List(BitArray)
50+
4851
/// Tests to see whether a bit array is valid UTF-8.
4952
///
5053
pub fn is_utf8(bits: BitArray) -> Bool {

‎src/gleam_stdlib.erl

+5
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ bit_array_slice(Bin, Pos, Len) ->
206206
catch error:badarg -> {error, nil}
207207
end.
208208

209+
bit_array_split(Bin, Sub) ->
210+
try {ok, binary:split(Bin, Pos, Len)}
211+
catch error:badarg -> {error, nil}
212+
end.
213+
209214
bit_array_int_to_u32(I) when 0 =< I, I < 4294967296 ->
210215
{ok, <<I:32>>};
211216
bit_array_int_to_u32(_) ->

‎test/gleam/bit_array_test.gleam

+12
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,15 @@ pub fn inspect_partial_bytes_test() {
302302
bit_array.inspect(<<5:3, 11:4, 1:2>>)
303303
|> should.equal("<<182, 1:size(1)>>")
304304
}
305+
306+
@target(erlang)
307+
pub fn split_test() {
308+
bit_array.split(<<"ILOVEGLEAM!":utf8>>, <<"GLEAM":utf8>>)
309+
|> should.equal([<<"ILOVE":utf8>>, <<"!":utf8>>])
310+
311+
bit_array.inspect(<<100, 5:3>>)
312+
|> should.equal("<<100, 5:size(3)>>")
313+
314+
bit_array.inspect(<<5:3, 11:4, 1:2>>)
315+
|> should.equal("<<182, 1:size(1)>>")
316+
}

0 commit comments

Comments
 (0)
Please sign in to comment.