Skip to content

Commit da36d84

Browse files
committed
Match BEAM binary:split option handling
Signed-off-by: Peter M <petermm@gmail.com>
1 parent 12cd858 commit da36d84

3 files changed

Lines changed: 4 additions & 4 deletions

File tree

src/libAtomVM/nifs.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3564,6 +3564,8 @@ static term nif_binary_split(Context *ctx, int argc, term argv[])
35643564
if (UNLIKELY(!term_is_list(options))) {
35653565
RAISE_ERROR(BADARG_ATOM);
35663566
}
3567+
// Match BEAM semantics and ignore an improper tail after a valid
3568+
// option prefix, e.g. [global | foo].
35673569
while (term_is_nonempty_list(options)) {
35683570
term head = term_get_list_head(options);
35693571
switch (head) {
@@ -3581,9 +3583,6 @@ static term nif_binary_split(Context *ctx, int argc, term argv[])
35813583
}
35823584
options = term_get_list_tail(options);
35833585
}
3584-
if (UNLIKELY(!term_is_nil(options))) {
3585-
RAISE_ERROR(BADARG_ATOM);
3586-
}
35873586
}
35883587

35893588
size_t pattern_count = 1;

tests/erlang_tests/test_binary_split.erl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ start() ->
3737
ok = split_compare_expected([<<"a">>], <<":a::">>, <<":">>, [global, trim_all]),
3838
ok = split_compare_expected([], <<>>, <<":">>, [trim]),
3939
ok = split_compare_expected([<<"abc">>], <<"abc">>, [<<"z">>, <<"y">>], [trim_all]),
40+
ok = split_compare_expected([<<"a">>], <<"a">>, <<":">>, [global | foo]),
4041
ok = fail_split(<<>>),
4142
ok = fail_split([]),
4243
ok = fail_split([<<>>]),

tests/libs/estdlib/test_binary.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ test_split() ->
4444
?ASSERT_EXCEPTION(binary:split(<<"a">>, [<<>>]), error, badarg),
4545
?ASSERT_EXCEPTION(binary:split(<<"a">>, [foo]), error, badarg),
4646
?ASSERT_EXCEPTION(binary:split(<<"a">>, <<":">>, [foo]), error, badarg),
47-
?ASSERT_EXCEPTION(binary:split(<<"a">>, <<":">>, [global | foo]), error, badarg),
47+
?ASSERT_MATCH(binary:split(<<"a">>, <<":">>, [global | foo]), [<<"a">>]),
4848
?ASSERT_MATCH(binary:split(<<"aba">>, [<<"a">>, <<"ab">>]), [<<>>, <<"a">>]),
4949
?ASSERT_MATCH(binary:split(<<"aba">>, [<<"ab">>, <<"a">>]), [<<>>, <<"a">>]),
5050
?ASSERT_MATCH(binary:split(<<":">>, <<":">>, [trim]), []),

0 commit comments

Comments
 (0)