[erlang-questions] Smart way to trim a binary?

Ulf Wiger <>
Wed Jun 24 14:40:23 CEST 2009


Kostis Sagonas wrote:
> Christian wrote:
>> I prefer this approach for implementing is_whitespace. Saves a few
>> lines and is quite readable. If I'm lucky the compiler is sufficiently
>> smart to optimize it into submission.
>>
>> is_whitespace(C) -> lists:member(C, "\s\t\r\n").
> 
> Actually, the compiler is quite smart NOT to turn it into the form you 
> are suggesting because the version with lists:member/2 is linear on the 
> length of the list while the version with many clauses uses pattern 
> matching compilation to access the proper clause in sub-linear time.

The way I understood the suggestion, it was that the
compiler could notice that the list is hard-coded and
replace the function call with pattern-matching
compilation.

I can imagine that this sort of optimization would rank
fairly low on the list of worthy candidates though.
It wouldn't be too hard to write a parse transform that
does it, if you want to use it extensively.

BTW, regarding the trimming of binaries, perhaps someone
with more regexp skill could improve on this:

1> re:split(<<"  \tabc\t  ">>, "[(^\\h*)(\\h$)]",[trim]).
[<<>>,<<>>,<<>>,<<"abc">>]


BR,
Ulf W
-- 
Ulf Wiger
CTO, Erlang Training & Consulting Ltd
http://www.erlang-consulting.com


More information about the erlang-questions mailing list