splitting binaries on a separator

Joel Reymont <>
Thu May 28 15:17:31 CEST 2009


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



More information about the erlang-questions mailing list