Skip to content

Commit e023f34

Browse files
committed
cleanup
1 parent 00af688 commit e023f34

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

src/gleam/bit_array.gleam

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

48+
@external(erlang, "gleam_stdlib", "bit_array_split")
49+
@external(javascript, "../gleam_stdlib.mjs", "bit_array_split")
50+
pub fn split(
51+
bits: BitArray,
52+
on subpattern: BitArray,
53+
) -> Result(List(BitArray), Nil)
54+
4855
/// Tests to see whether a bit array is valid UTF-8.
4956
///
5057
pub fn is_utf8(bits: BitArray) -> Bool {

src/gleam_stdlib.erl

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
decode_tuple5/1, decode_tuple6/1, tuple_get/2, classify_dynamic/1, print/1,
1414
println/1, print_error/1, println_error/1, inspect/1, float_to_string/1,
1515
int_from_base_string/2, utf_codepoint_list_to_string/1, contains_string/2,
16-
crop_string/2, base16_decode/1, string_replace/3
16+
crop_string/2, base16_decode/1, string_replace/3, bit_array_split/2
1717
]).
1818

1919
%% Taken from OTP's uri_string module
@@ -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, Sub, [global])}
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(_) ->

src/gleam_stdlib.mjs

+37
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,43 @@ export function bit_array_slice(bits, position, length) {
366366
return new Ok(new BitArray(buffer));
367367
}
368368

369+
export function bit_array_split(bits, subpattern) {
370+
const slices = [];
371+
if (bits.length == 0 && subpattern.length == 0) {
372+
return new Ok(List.fromArray([new BitArray(new Uint8Array(0))]));
373+
}
374+
if (subpattern.length == 0) {
375+
return new Error(Nil);
376+
}
377+
378+
let subpatternIndex = 0;
379+
let startIndex = 0;
380+
for (let i = 0; i < bits.length; i++) {
381+
if (bits.buffer[i] == subpattern.buffer[subpatternIndex]) {
382+
subpatternIndex++;
383+
}
384+
if (subpatternIndex == subpattern.length) {
385+
const new_slice = new Uint8Array(
386+
bits.buffer.buffer,
387+
startIndex,
388+
i - startIndex - subpattern.length + 1,
389+
);
390+
slices.push(new BitArray(new_slice));
391+
startIndex = i + 1;
392+
subpatternIndex = 0;
393+
394+
if (i == bits.length - 1) {
395+
slices.push(new BitArray(new Uint8Array(0)));
396+
}
397+
}
398+
}
399+
if (startIndex < bits.length || bits.length == 0) {
400+
slices.push(new BitArray(new Uint8Array(bits.buffer.buffer, startIndex)));
401+
}
402+
403+
return new Ok(List.fromArray(slices));
404+
}
405+
369406
export function codepoint(int) {
370407
return new UtfCodepoint(int);
371408
}

test/gleam/bit_array_test.gleam

+36
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,39 @@ 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+
pub fn split_test() {
307+
bit_array.split(<<"ABaABabABc":utf8>>, <<"AB":utf8>>)
308+
|> should.be_ok
309+
|> should.equal([<<"":utf8>>, <<"a":utf8>>, <<"ab":utf8>>, <<"c":utf8>>])
310+
}
311+
312+
pub fn split_ends_in_pattern_test() {
313+
bit_array.split(<<"ABaABabAB":utf8>>, <<"AB":utf8>>)
314+
|> should.be_ok
315+
|> should.equal([<<"":utf8>>, <<"a":utf8>>, <<"ab":utf8>>, <<"":utf8>>])
316+
}
317+
318+
pub fn split_empty_subpattern_test() {
319+
bit_array.split(<<"ABC":utf8>>, <<>>)
320+
|> should.be_error
321+
|> should.equal(Nil)
322+
}
323+
324+
pub fn split_same_pattern_test() {
325+
bit_array.split(<<"ABC":utf8>>, <<"ABC":utf8>>)
326+
|> should.be_ok
327+
|> should.equal([<<"":utf8>>, <<"":utf8>>])
328+
}
329+
330+
pub fn split_empty_bit_array_with_empty_subpattern_test() {
331+
bit_array.split(<<>>, <<>>)
332+
|> should.be_ok
333+
|> should.equal([<<>>])
334+
}
335+
336+
pub fn split_empty_bit_array_with_pattern_test() {
337+
bit_array.split(<<>>, <<"ABC":utf8>>)
338+
|> should.be_ok
339+
|> should.equal([<<"":utf8>>])
340+
}

0 commit comments

Comments
 (0)