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

Chris Newcombe chris.newcombe@REDACTED
Tue Jun 30 21:30:53 CEST 2009


Ulf wrote:

> It wouldn't be too hard to write a parse transform that
> does it, if you want to use it extensively.


I'm using your ct_expand.erl for this (
http://forum.trapexit.org/viewtopic.php?p=20260#20260 )

e.g.

re_pattern_for_validation() ->

    %% ct_expand will turn this into a constant at compilation time
    %% and R13B will put it into the module's constant pool.

    ct_expand:term(
      re:compile(<<"^\([^,]+, *[^,]+, *[^,]+\).*$">>),
     ).

validate_clause_pattern(Clause) ->

    {ok, RE} = re_pattern_for_validation(),

    case re:run(DebugClause, RE, [{capture,none}]) of
        match -> ok;
        Error -> throw...
    end.

Chris


On Wed, Jun 24, 2009 at 5:40 AM, Ulf
Wiger<ulf.wiger@REDACTED> wrote:
> 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
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>
>


More information about the erlang-questions mailing list