[erlang-questions] efficient binary escape

Wes James comptekki@REDACTED
Tue Apr 17 23:20:05 CEST 2012


On Tue, Apr 17, 2012 at 2:43 PM, Dmitry Kolesnikov
<dmkolesnikov@REDACTED> wrote:
> Hello Erlangers!
>
> I'd like to get your opinion on the binary escaping techniques using erlang-only.
>
> Let's say that I have binary   <<"ab,cdef,qwert,yuiop,....">> I wish to escape (substitute) comma by \, to get <<"ab\\,cdef\\,qwert\\,yuiop,....">>. In other words, a single byte of binary sequence has to be replaced by multiple bytes.
>
> and I do have about 35 - 50M tokens to procedure. The straight forward approaches like
>  * binary:replace
>  * sub(<<$,, Rest/binary>>, Acc) ->
>      sub(Rest, <<Acc/binary, $\, $,>>)
>    ....
> two slow for me!
>
> Do you have any suggestion? What is you experience?


How about:

re:replace(<<"ab,cdef,qwert,yuiop,">>,",","\\\\,",[{return, binary},global]).

-wes



More information about the erlang-questions mailing list