-
-
Notifications
You must be signed in to change notification settings - Fork 186
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
Added split_once and index_of to bitarray #629
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -206,3 +206,36 @@ fn do_inspect(input: BitArray, accumulator: String) -> String { | |
_ -> accumulator | ||
} | ||
} | ||
|
||
/// Finds the position of a bit pattern within a bit array. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ```gleam | ||
/// index_of(<<0, 1, 2, 3, 4, 5, 6, 7>>, <<3, 4, 5>>) | ||
/// // -> "3" | ||
/// ``` | ||
/// | ||
@external(erlang, "gleam_stdlib", "bit_array_index_of") | ||
@external(javascript, "../gleam_stdlib.mjs", "bit_array_index_of") | ||
pub fn index_of(haystack: BitArray, needle: BitArray) -> Int | ||
|
||
// error is returned if not found. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment seems to have been misplaced |
||
/// Splits a bit array into left and right parts at the bit pattern provided, an | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ```gleam | ||
/// split_once(<<0, 1, 2, 3, 4, 5, 6, 7>>, <<3, 4, 5>>) | ||
/// // -> Ok(<<0, 1, 2>>, <<6, 7>>) | ||
/// | ||
/// split_once(<<0, 1, 2, 3, 4, 5, 6, 7>>, <<5, 4, 3>>) | ||
/// // -> Error(Nil) | ||
/// ``` | ||
/// | ||
@external(erlang, "gleam_stdlib", "bit_array_split_once") | ||
@external(javascript, "../gleam_stdlib.mjs", "bit_array_split_once") | ||
pub fn split_once( | ||
haystack: BitArray, | ||
needle: BitArray, | ||
) -> Result(#(BitArray, BitArray), Nil) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,8 @@ | |
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_index_of/2, | ||
bit_array_split_once/2 | ||
]). | ||
|
||
%% Taken from OTP's uri_string module | ||
|
@@ -536,3 +537,23 @@ base16_decode(String) -> | |
|
||
string_replace(String, Pattern, Replacement) -> | ||
string:replace(String, Pattern, Replacement, all). | ||
|
||
bit_array_index_of(Haystack, Needle) -> | ||
case binary:match(Haystack, Needle) of | ||
{Pos, _Len} -> Pos; | ||
_ -> -1 | ||
end. | ||
|
||
bit_array_split_once(Haystack, Needle) -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the built in split function please 🙏 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The built in function doesn't seem to allow splitting along non-byte aligned boundaries. e.g. splitting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's fine 👍 Performance is the priority here, and I'm not aware of any binary protocol that will use non-byte aligned data. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem, i removed the associated tests, and switched to using erlang's built in binary:split |
||
try | ||
case Needle of | ||
<<>> -> {ok, {<<>>, Haystack}}; | ||
_ -> case binary:split(Haystack, Needle) of | ||
[Part1, Part2] -> {ok, {Part1, Part2}}; | ||
_ -> {error, nil} | ||
end | ||
end | ||
catch | ||
error:badarg -> {error, nil} | ||
end. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -308,3 +308,35 @@ pub fn inspect_partial_bytes_test() { | |
bit_array.inspect(<<5:3, 11:4, 1:2>>) | ||
|> should.equal("<<182, 1:size(1)>>") | ||
} | ||
|
||
pub fn index_of_found_test() { | ||
<<"Hello, World":utf8>> | ||
|> bit_array.index_of(<<", ":utf8>>) | ||
|> should.equal(5) | ||
} | ||
|
||
pub fn index_of_not_found_test() { | ||
<<"Hello, World":utf8>> | ||
|> bit_array.index_of(<<"Joe":utf8>>) | ||
|> should.equal(-1) | ||
} | ||
|
||
pub fn split_once_found_test() { | ||
<<"Hello, World":utf8>> | ||
|> bit_array.split_once(<<", ":utf8>>) | ||
|> should.be_ok | ||
|> should.equal(#(<<"Hello":utf8>>, <<"World":utf8>>)) | ||
} | ||
|
||
pub fn split_once_empty_needle_test() { | ||
<<"Hello, World":utf8>> | ||
|> bit_array.split_once(<<>>) | ||
|> should.be_ok | ||
|> should.equal(#(<<>>, <<"Hello, World":utf8>>)) | ||
} | ||
|
||
pub fn split_once_not_found_test() { | ||
<<"Hello, World":utf8>> | ||
|> bit_array.split_once(<<"Joe":utf8>>) | ||
|> should.be_error | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you include tests for non-byte aligned bit arrays please |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this please 🙏 The ticket was only for
split_once
.