Skip to content

Commit

Permalink
Issue erlang#8099: Add comment explaining compiler optimisation and a…
Browse files Browse the repository at this point in the history
…dd guard to ensure Separator is a binary
  • Loading branch information
onno-vos-dev committed Feb 8, 2024
1 parent fa6d3b7 commit b0983bc
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/stdlib/src/binary.erl
Original file line number Diff line number Diff line change
Expand Up @@ -950,8 +950,9 @@ _Example:_
-doc(#{since => <<"OTP 27.0">>}).
-spec join([binary()], binary()) -> binary().
join([], _Separator) -> <<>>;
join([H], _Separator) when is_binary(H) -> H;
join([H | T], Separator) ->
join([H], Separator) when is_binary(H) andalso is_binary(Separator) -> H;
join([H | T], Separator) when is_binary(Separator) ->
%% Starting with an empty binary convinces the compiler to use the new "private append" optimisation
Acc = <<>>,
join(T, Separator, <<Acc/binary, H/binary>>);
join(Arg, Separator) ->
Expand Down
1 change: 1 addition & 0 deletions lib/stdlib/test/binary_module_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ badargs(Config) when is_list(Config) ->
badarg = ?MASK_ERROR(binary:encode_hex(#{})),
badarg = ?MASK_ERROR(binary:encode_hex(foo)),

badarg = ?MASK_ERROR(binary:join(<<"">>, ",")),
badarg = ?MASK_ERROR(binary:join([""], <<",">>)),
badarg = ?MASK_ERROR(binary:join([123], <<",">>)),
badarg = ?MASK_ERROR(binary:join(123, <<",">>)),
Expand Down

0 comments on commit b0983bc

Please sign in to comment.