[erlang-questions] splitting binaries on a separator

Per Melin <>
Thu May 28 19:43:23 CEST 2009


Joel Reymont:
>
> On May 28, 2009, at 5:09 PM, Robert Virding wrote:
>
>> 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?
>
> I'm dense today. How would you step down the binary to find the index of the
> separator?

This is faster than your code for the situations I tested, but still
much slower than the regexp module:

split_bin2(Sep, Bin) ->
    split_bin2(Bin, Bin, Sep, byte_size(Sep), 0).

split_bin2(Sub, Bin, Sep, SepSize, Pos) ->
    case Sub of
        <<Sep:SepSize/binary, Rest/binary>> ->
            <<Before:Pos/binary, _/binary>> = Bin,
            {ok, Before, Rest};
        <<>> ->
            {more, Bin};
        <<_:1/binary, Rest/binary>> ->
            split_bin2(Rest, Bin, Sep, SepSize, Pos + 1)
    end.


More information about the erlang-questions mailing list