[erlang-questions] Binary:split and re:split woes

Carl-Johan Kjellander <>
Thu Dec 5 14:09:30 CET 2013


http://www.erlang.org/doc/efficiency_guide/binaryhandling.html

In section 4.1: A *sub binary* is created by split_binary/2 and when a
binary is matched out in a binary pattern. A sub binary is a reference into
a part of another binary (refc or heap binary, never into a another sub
binary). Therefore, matching out a binary is relatively cheap because the
actual binary data is never copied.

So hopefully it is cheap. I also hate the beginning <<>>, so annoying. I
have code in tons of places that removes it.
/cjk


On Thu, Dec 5, 2013 at 1:41 PM, Robert Virding <
> wrote:

> A follow up question is whether and when the splits create new binaries.
> So if the input to split is a binary and the outputs are also binary are
> they completely new binaries or just references into the old binary sharing
> the actual binary data?
>
> Robert
>
> ----- Original Message -----
> > From: "Robert Virding" <>
> >
> > These two functions allow you give a pattern to split binaries (and
> lists in
> > re) into a list of sub binaries. If the split pattern occurs at the
> > beginning or end of the binary then you get empty parts at the beginning
> or
> > end. So:
> >
> > binary:split(<<" abc def ghi ">>, <<" ">>, [global]) ==>
> >     [<<>>,<<"abc">>,<<"def">>,<<"ghi">>,<<>>]
> >
> > This is fine and logical. Often you don't want these empty parts so
> there is
> > a 'trim' options which removes them. But it only trims at the end and
> not at
> > the beginning. So:
> >
> > binary:split(<<" abc def ghi ">>, <<" ">>, [global,trim]) ==>
> >     [<<>>,<<"abc">>,<<"def">>,<<"ghi">>]
> >
> > This is stupid! Saying it it done like this Perl is hardly a good excuse
> for
> > bad behaviour.
> >
> > Sigh,
> > Robert
> > _______________________________________________
> > erlang-questions mailing list
> > 
> > http://erlang.org/mailman/listinfo/erlang-questions
> >
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20131205/345ff868/attachment.html>


More information about the erlang-questions mailing list