Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for dynamic size in binary pattern matching #211

Open
cmkarlsson opened this issue Oct 7, 2017 · 4 comments
Open

Add support for dynamic size in binary pattern matching #211

cmkarlsson opened this issue Oct 7, 2017 · 4 comments

Comments

@cmkarlsson
Copy link

cmkarlsson commented Oct 7, 2017

I often have the need to pattern match binaries on dynamic sizes. Currently this is not possible in alpaca (which gives {invalid_bin_qualifier,<<"s">>})

An example:

let s = 5 in
match bin with
   <<x: type=binary unit=8 size=s, rest: type=binary>>

I think it would make sense to have the size being expressed as any expression returning an integer.

Tested with alpaca 620b9fa

@NobbZ
Copy link
Contributor

NobbZ commented Oct 9, 2017

As far as I know, this is not possible in the BEAM.

Binary sizes for everything but the "tail" has to be known at compiletime.

@utkarshkukreti
Copy link

This is definitely possible in the BEAM. Elixir:

iex(1)> s = 5
5
iex(2)> <<x::binary-size(s), rest::binary>> = "foobarbaz"
"foobarbaz"
iex(3)> x
"fooba"
iex(4)> rest
"rbaz"

@NobbZ
Copy link
Contributor

NobbZ commented Oct 9, 2017

Oh, either I missed a change in the BEAM or I am missremembering some details around this...

@utkarshkukreti
Copy link

BEAM does not support having a variable length for any chunk except the last even if it can be easily calculated manually by subtracting the size of the binary and the size of the last chunk. You may be misremembering that.

iex(5)> <<x::binary, rest::binary-size(2)>> = "foobarbaz"
** (CompileError) iex:5: a binary field without size is only allowed at the end of a binary pattern and never allowed in binary generators

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants