[erlang-questions] splitting binaries on a separator

Robert Virding rvirding@REDACTED
Thu May 28 18:09:50 CEST 2009


Couldn't you just first step down the binary until you find the index of the
separator and then do a split_binary/2 on the original? This would save
incrementally building the leading binary. Or have I missed something?

Robert

2009/5/28 Joel Reymont <joelr1@REDACTED>

> Is there a more efficient way of splitting binaries on a separator
> sequence?
>
>        Thanks, Joel
>
> ---
>
> split_bin(Sep, Bin) ->
>    split_bin(Sep, byte_size(Sep), Bin).
>
> split_bin(Sep, SepSize, Bin) ->
>    case Bin of
>        <<Sep:SepSize/binary, Rest/binary>> ->
>            {ok, <<>>, Rest};
>        _ ->
>            split_bin(1, Sep, SepSize, Bin)
>    end.
>
> split_bin(N, Sep, SepSize, Bin)
>  when N < byte_size(Bin) ->
>    case Bin of
>        <<Bin1:N/binary, Sep:SepSize/binary>> ->
>            {ok, Bin1, <<>>};
>        <<Bin1:N/binary, Sep:SepSize/binary, Rest/binary>> ->
>            {ok, Bin1, Rest};
>        _ ->
>            split_bin(N + 1, Sep, SepSize, Bin)
>    end;
>
> split_bin(_, _, _, Bin) ->
>    {more, Bin}.
>
> ---
> Mac hacker with a performance bent
> http://www.linkedin.com/in/joelreymont
>
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>
>


More information about the erlang-questions mailing list