Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add split function to gleam/bit_array #571

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/gleam/bit_array.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ pub fn slice(
take length: Int,
) -> Result(BitArray, Nil)

/// Creates a list of `bit array`s by splitting a given bit array on a given bit
/// array.
///
/// Returns Error(Nil) if the subpattern is an empty bit array.
///
/// ## Examples
///
/// ```gleam
/// split("<<home/gleam/desktop/":utf8>>, on: <<"/":utf8>>)
/// // -> [<<"home":utf8>>, <<"gleam":utf8>>, <<"desktop":utf8>>, <<>>]
/// ```
@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>>])
}
Loading