Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
renatillas committed Apr 25, 2024
1 parent 00af688 commit e023f34
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/gleam/bit_array.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ pub fn slice(
take length: Int,
) -> Result(BitArray, Nil)

@external(erlang, "gleam_stdlib", "bit_array_split")
@external(javascript, "../gleam_stdlib.mjs", "bit_array_split")
pub fn split(
bits: BitArray,
on subpattern: BitArray,
) -> Result(List(BitArray), Nil)

/// Tests to see whether a bit array is valid UTF-8.
///
pub fn is_utf8(bits: BitArray) -> Bool {
Expand Down
7 changes: 6 additions & 1 deletion src/gleam_stdlib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
decode_tuple5/1, decode_tuple6/1, tuple_get/2, classify_dynamic/1, print/1,
println/1, print_error/1, println_error/1, inspect/1, float_to_string/1,
int_from_base_string/2, utf_codepoint_list_to_string/1, contains_string/2,
crop_string/2, base16_decode/1, string_replace/3
crop_string/2, base16_decode/1, string_replace/3, bit_array_split/2
]).

%% Taken from OTP's uri_string module
Expand Down Expand Up @@ -206,6 +206,11 @@ bit_array_slice(Bin, Pos, Len) ->
catch error:badarg -> {error, nil}
end.

bit_array_split(Bin, Sub) ->
try {ok, binary:split(Bin, Sub, [global])}
catch error:badarg -> {error, nil}
end.

bit_array_int_to_u32(I) when 0 =< I, I < 4294967296 ->
{ok, <<I:32>>};
bit_array_int_to_u32(_) ->
Expand Down
37 changes: 37 additions & 0 deletions src/gleam_stdlib.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,43 @@ export function bit_array_slice(bits, position, length) {
return new Ok(new BitArray(buffer));
}

export function bit_array_split(bits, subpattern) {
const slices = [];
if (bits.length == 0 && subpattern.length == 0) {
return new Ok(List.fromArray([new BitArray(new Uint8Array(0))]));
}
if (subpattern.length == 0) {
return new Error(Nil);
}

let subpatternIndex = 0;
let startIndex = 0;
for (let i = 0; i < bits.length; i++) {
if (bits.buffer[i] == subpattern.buffer[subpatternIndex]) {
subpatternIndex++;
}
if (subpatternIndex == subpattern.length) {
const new_slice = new Uint8Array(
bits.buffer.buffer,
startIndex,
i - startIndex - subpattern.length + 1,
);
slices.push(new BitArray(new_slice));
startIndex = i + 1;
subpatternIndex = 0;

if (i == bits.length - 1) {
slices.push(new BitArray(new Uint8Array(0)));
}
}
}
if (startIndex < bits.length || bits.length == 0) {
slices.push(new BitArray(new Uint8Array(bits.buffer.buffer, startIndex)));
}

return new Ok(List.fromArray(slices));
}

export function codepoint(int) {
return new UtfCodepoint(int);
}
Expand Down
36 changes: 36 additions & 0 deletions test/gleam/bit_array_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,39 @@ pub fn inspect_partial_bytes_test() {
bit_array.inspect(<<5:3, 11:4, 1:2>>)
|> should.equal("<<182, 1:size(1)>>")
}

pub fn split_test() {
bit_array.split(<<"ABaABabABc":utf8>>, <<"AB":utf8>>)
|> should.be_ok
|> should.equal([<<"":utf8>>, <<"a":utf8>>, <<"ab":utf8>>, <<"c":utf8>>])
}

pub fn split_ends_in_pattern_test() {
bit_array.split(<<"ABaABabAB":utf8>>, <<"AB":utf8>>)
|> should.be_ok
|> should.equal([<<"":utf8>>, <<"a":utf8>>, <<"ab":utf8>>, <<"":utf8>>])
}

pub fn split_empty_subpattern_test() {
bit_array.split(<<"ABC":utf8>>, <<>>)
|> should.be_error
|> should.equal(Nil)
}

pub fn split_same_pattern_test() {
bit_array.split(<<"ABC":utf8>>, <<"ABC":utf8>>)
|> should.be_ok
|> should.equal([<<"":utf8>>, <<"":utf8>>])
}

pub fn split_empty_bit_array_with_empty_subpattern_test() {
bit_array.split(<<>>, <<>>)
|> should.be_ok
|> should.equal([<<>>])
}

pub fn split_empty_bit_array_with_pattern_test() {
bit_array.split(<<>>, <<"ABC":utf8>>)
|> should.be_ok
|> should.equal([<<"":utf8>>])
}

0 comments on commit e023f34

Please sign in to comment.