Skip to content

Commit

Permalink
Suggested fixes
Browse files Browse the repository at this point in the history
* Add proper error handling and error information.

* Remove spec for the non-exported function `join/3`.
  • Loading branch information
bjorng committed Oct 31, 2024
1 parent d7bb143 commit 83b55e7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
19 changes: 11 additions & 8 deletions lib/stdlib/src/binary.erl
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ unhex(X) ->
-doc """
Joins a list of binaries together by a specified `Separator`.

Equivalent to `iolist_to_binary(lists:join(Separator, Binaries))` with `binary:join/2` being the faster of the two.
Equivalent to `iolist_to_binary(lists:join(Separator, Binaries))`, but faster.

_Example:_

Expand All @@ -947,18 +947,21 @@ _Example:_
<<"a, b, c">>
```
""".
-doc(#{since => <<"OTP 27.0">>}).
-doc(#{since => <<"OTP 28.0">>}).
-spec join([binary()], binary()) -> binary().
join([], _Separator) -> <<>>;
join([], Separator) when is_binary(Separator) -> <<>>;
join([H], Separator) when is_binary(H), 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([H | T]=List, Separator) when is_binary(Separator) ->
try
Acc = <<>>, %Enable private-append optimization
join(T, Separator, <<Acc/binary, H/binary>>)
catch
error:_ ->
badarg_with_info([List, Separator])
end;
join(Arg, Separator) ->
badarg_with_info([Arg, Separator]).
-spec join([binary()], binary(), binary()) -> binary().
join([], _Separator, Acc) -> Acc;
join([H | T], Separator, Acc) ->
join(T, Separator, <<Acc/binary, Separator/binary, H/binary>>).
Expand Down
14 changes: 14 additions & 0 deletions lib/stdlib/src/erl_stdlib_errors.erl
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ format_binary_error(last, [Subject], _) ->
<<>> -> empty_binary;
_ -> must_be_binary(Subject)
end];
format_binary_error(join, [Binaries,Separator], _) ->
case must_be_binary(Separator) of
[] when is_list(Binaries) ->
case must_be_list(Binaries) of
[] ->
[~"not a list of binaries", []];
Error ->
[Error, []]
end;
[] ->
[must_be_list(Binaries), []];
Error ->
[[], Error]
end;
format_binary_error(list_to_bin, [_], _) ->
[not_iodata];
format_binary_error(longest_common_prefix, [_], _) ->
Expand Down
13 changes: 9 additions & 4 deletions lib/stdlib/test/binary_module_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,12 @@ error_info(_Config) ->
{last,[<<1:1>>]},
{last,[<<>>]},

{join,[no_list,<<>>]},
{join,[[a|b],<<>>]},
{join,[[a],<<>>]},
{join,[[],<<1:7>>]},
{join,[[],bad_separator]},

{list_to_bin,[<<1,2,3>>]},
{list_to_bin,[{1,2,3}]},

Expand Down Expand Up @@ -1588,11 +1594,10 @@ do_hex_roundtrip(Bytes) ->
ok
end.

%% Test join/2.
join(Config) when is_list(Config) ->
<<"a, b, c">> = binary:join([<<"a">>, <<"b">>, <<"c">>], <<", ">>),
<<"a">> = binary:join([<<"a">>], <<", ">>),
<<>> = binary:join([], <<", ">>).
~"a, b, c" = binary:join([~"a", ~"b", ~"c"], ~", "),
~"a" = binary:join([~"a"], ~", "),
~"" = binary:join([], ~", ").

%%%
%%% Utilities.
Expand Down

0 comments on commit 83b55e7

Please sign in to comment.