binary的这个bug R13A还没有修复

本文探讨了Erlang R12B-3版本中关于位元组匹配的bug,具体涉及如何正确使用相同的变量进行位元组大小匹配,并通过修改代码解决了编译错误问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Edwin , thanks for your response! :)

2008/7/19 Edwin Fine <emofine@gmail.com>:

Litao,

I think this is a bug in Erlang R12B-3. Certainly the documentation can be misleading, because in Programming Examples (Section 4.6: Matching Binaries), it specifically says that this construct is not allowed:

"Size must be an integer literal, or a previously bound variable. Note that the following is not allowed:

foo(N, <<X:N,T/binary>>) ->
{X,T}.

The two occurrences of N are not related. The compiler will complain that the N in the size field is unbound."

That being said, if you rewrite the expression as shown below, it will compile (but does not work). If I understand correctly, binary, bits, and bitstream are the same, except that the default bit size for binary is 8, and for bitstring it is 1. Since you are overriding the default size anyway, you can use binary-unit:11 in place of bits-unit:11.

decode(<<N:5,Chans:N/binary-unit:11,_/bits>>) ->
[Chan || <<Chan:11>> <- Chans].


103> Chans3 = <<3:5,2:11,3:11,4:11>>.
<<24,2,0,96,4:6>>
104> bb:decode(Chans3).
N:3, Chans:<<0,64,12,2,0:1>>
** exception error: no case clause matching {<<0,64,12,2,0:1>>}
in function bb:'-decode/1-lc$^0/1-0-'/1
105>

The code is:

-module(bb).
-compile([export_all]).

decode(<<N:5,Chans:N/binary-unit:11,_/bits>>) ->
io:format("N:~p, Chans:~p~n", [N, Chans]),
[Chan || <<Chan:11>> <- Chans].

So even though the programming examples say that you can't use the same N in the match, it actually does work, but the list comprehension does not. I found out this is because a bitstring generator has to use "<=" and not "<-". So the final working code is:

-module(bb).
-compile([export_all]).

decode(<<N:5,Chans:N/[color=red]binary-unit[/color]:11,_/bits>>) ->
[Chan || <<Chan:11>> [color=red]<=[/color] Chans].

118> c(bb).
{ok,bb}
119> Chans3 = <<3:5,2:11,3:11,4:11>>.
<<24,2,0,96,4:6>>
120> bb:decode(Chans3).
[2,3,4]

Hope this helps.


2008/7/18 litao cheng <litaocheng@gmail.com>:

- Hide quoted text -
hi, all.
when I read this paper: Programming Efficiently with Binaries and Bit Strings http://www.erlang.se/euc/07/papers/1700Gustafsson.pdf, I encounter a compile error, the code snipes is a example to parse the IS 683-PRL protocol:

decode(<<N:5,Chans:N/bits-unit:11,_/bits>>) ->
[Chan || <<Chan:11>> <- Chans].

the compiler says:
bit type mismatch (unit) between 11 and 1

I read the erlang reference mannual, the bits unit default is 1, I think the unit can be set, why this compile error occur? thank you!
my erlang emulator is 5.6.3(R12B-3).

_______________________________________________
erlang-questions mailing list
erlang-questions@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions

难道他们不想修????我等只能凑合用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值