binary的这个bug R13A还没有修复

xiaoxiao2022-12-02  219

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 难道他们不想修????我等只能凑合用。 相关资源:敏捷开发V1.0.pptx
转载请注明原文地址: https://www.6miu.com/read-4979244.html

最新回复(0)