Current section
Files
Jump to
Current section
Files
src/bucbinary.erl
-module(bucbinary).
-export([
join/2,
trim/2
]).
%% @doc
%% join a list of binaries with the given separator
%%
%% Example:
%%
%% <pre lang="erlang">
%% <<"toto-tata-titi">> = bucbinary:join([<<"toto">>, <<"tata">>, <<"titi">>], <<"-">>).
%% </pre>
%% @end
-spec join([binary()], binary()) -> binary().
join([], _) ->
<<>>;
join([Bin], _) when is_binary(Bin) ->
Bin;
join(List, Sep) when is_list(List), is_binary(Sep) ->
lists:foldr(fun
(<<>>, <<>>) -> <<>>;
(A, <<>>) -> A;
(<<>>, B) -> B;
(A, B) -> <<(bucs:to_binary(A))/binary,
Sep/binary,
(bucs:to_binary(B))/binary>>
end, <<>>, List);
join(Bin, _) when is_binary(Bin) ->
Bin.
- spec trim(binary(), left | right | both) -> binary().
trim(Binary, left) ->
trim_left(Binary);
trim(Binary, right) ->
trim_right(Binary);
trim(Binary, both) ->
trim_left(trim_right(Binary)).
trim_left(<<C, Rest/binary>>) when C =:= $\s orelse
C =:= $\n orelse
C =:= $\r orelse
C =:= $\t ->
trim_left(Rest);
trim_left(Binary) -> Binary.
trim_right(Binary) ->
trim_right(Binary, size(Binary)-1).
trim_right(Binary, Size) ->
case Binary of
<<Rest:Size/binary, C>> when C =:= $\s
orelse C =:= $\t
orelse C =:= $\n
orelse C =:= $\r ->
trim_right(Rest, Size - 1);
Other ->
Other
end.