[erlang-questions] matching Rest/binary using bit syntax - why does it have to be the last element specified if all other elements are sized ?
ami
m3oucat@REDACTED
Fri Jun 27 12:03:06 CEST 2014
Hi Andreas,
You wrote:
> As you demonstrated, no reverse operation is needed. The compiler
> could actually be smart enough to generate above code by itself
> when given a match like:
>
> <<H/binary, T:24>> = BinC
But what do you mean under “the compiler could actually be smart enough"
But when I’m trying
6> A = <<1,2,3,4,5>>.
<<1,2,3,4,5>>
7> <<H/binary, T:16>> = A.
Im getting an error:
* 1: a binary field without size is only allowed at the end of a binary pattern
(Erlang/OTP 17 [erts-6.0] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false])
The same thing if I try not just try out the code in erlang shell, but if I try to
write a file & compile it:
-module(test).
-export([mytest/0]).
mytest() ->
A = <<1,2,3,4,5>>,
<<H/binary, T:16>> = A,
[H,T].
a1$ erlc test.erl
test.erl:7: a binary field without size is only allowed at the end of a binary pattern
Kind regards,
Alex
On Jun 25, 2014, at 10:37, Andreas Schultz <aschultz@REDACTED> wrote:
> Hi,
>
> ----- Original Message -----
>> Hello,
>>
>> Here is one way to make it:
>>
>> Head = byte_size(BinC) * 8 - 24.
>> <<H:Head, T:24>> = BinC.
>>
>> The reverse operation for binary is expensive.
>
> As you demonstrated, no reverse operation is needed. The compiler
> could actually be smart enough to generate above code by itself
> when given a match like:
>
> <<H/binary, T:24>> = BinC
>
> Even having the variable length part (as long as there is only
> one) somewhere in the middle, would still allow the compiler
> to construct the expression.
>
> Things would become a bit more complicated in case or guard
> matches.
>
> Question to experts of the Erlang internals: Is there a non trivial
> reason the compiler/runtime system can't handle such matches?
>
> Regards
> Andreas
>
>>
>> Best Regards,
>> Dmitry >-|-|-(*>
>>
>>
>> On 25.6.2014, at 4.11, Richard McLean < rmcl2303@REDACTED > wrote:
>>
>>
>>
>>
>> This is my first question to the mailing list so apologies if there are any
>> newbie errors.
>>
>> Regarding bit syntax and pattern matching, you can match the remaining part
>> of the binary using the Rest/binary idiom as long as the element of
>> unspecified size (ie Rest/binary) is at the end of the pattern (ie. the very
>> last specified element)...
>>
>> eg. suppose I have a binary of unknown size (here I'm using <<1,2,3,4>> as
>> the example) and I want to separate it into 2 binaries - one containing the
>> first 8 bits, and the other containing the remainder...
>>
>> eg.
>> 1> BinA = <<1,2,3,4>>.
>> <<1,2,3,4>>
>> 2> <<BinB:8/binary-unit:1,BinC/binary>> = BinA.
>> <<1,2,3,4>>
>> 3> BinB.
>> <<1>>
>> 4> BinC.
>> <<2,3,4>>
>>
>> So far so good...
>>
>> Now suppose I want to take the same input binary (of unknown size) and this
>> time I want to separate it into 2 binaries - one containing the LAST 24 bits
>> (say some sort of footer), and the other containing the remainder (ie all
>> the PRECEDING data)...
>>
>> This should still be possible from the the compiler/erlang VM point of view
>> as we are still specifying all the element sizes except one, but erlang
>> generates an error if the binary element of unspecified size is anything but
>> the last match element.
>>
>> eg.
>> 1> BinA = <<1,2,3,4>>.
>> <<1,2,3,4>>
>> 2> <<BinB/binary, BinC:8/binary-unit:3>> = BinA.
>> * 1: a binary field without size is only allowed at the end of a binary
>> pattern
>>
>> What is the easiest way to achieve this sort of thing ?
>> I'm supposing the binary could be reversed, the front X bits extracted, then
>> reversing the extracted binary and the remainder, but is there an easier way
>> ?
>>
>> And if there are any of the erlang maintainers reading this, would it be
>> possible in a future release to change the rules on the Rest/binary idiom to
>> allow Rest/binary to be any element of the match pattern as long as all
>> other elements are size specified ?
>>
>> I don't think it would be a big change to the implement but perhaps there are
>> hidden gotchas I'm not considering ?
>>
>> Regards
>>
>> Richard
>>
>>
>>
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>
> --
> --
> Dipl. Inform.
> Andreas Schultz
>
> email: as@REDACTED
> phone: +49-391-819099-224
> mobil: +49-170-2226073
>
> ------------------- enabling your networks -------------------
>
> Travelping GmbH phone: +49-391-819099229
> Roentgenstr. 13 fax: +49-391-819099299
> D-39108 Magdeburg email: info@REDACTED
> GERMANY web: http://www.travelping.com
>
> Company Registration: Amtsgericht Stendal Reg No.: HRB 10578
> Geschaeftsfuehrer: Holger Winkelmann | VAT ID No.: DE236673780
> --------------------------------------------------------------
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
More information about the erlang-questions
mailing list