[erlang-questions] bit syntax: 0-sized segments
Richard A. O'Keefe
ok@REDACTED
Fri Mar 8 00:09:13 CET 2013
Surely zero-length segments *have* to be allowed?
Suppose you have
<<N:16, B:N/bytes, R/binary>> = Whatever
Is this supposed to crash if N = 0?
Why?
I cannot understand what is supposed to be 'weird' about
zero-length segments.
Look at example 4 from the Bit Syntax documentation:
-define(IP_VERSION, 4).
-define(IP_MIN_HDR_LEN, 5).
DgramSize = byte_size(Dgram),
case Dgram of
<<?IP_VERSION:4, HLen:4, SrvcType:8, TotLen:16,
ID:16, Flgs:3, FragOff:13,
TTL:8, Proto:8, HdrChkSum:16,
SrcIP:32,
DestIP:32, RestDgram/binary>> when HLen>=5, 4*HLen=<DgramSize ->
OptsLen = 4*(HLen - ?IP_MIN_HDR_LEN),
<<Opts:OptsLen/binary,Data/binary>> = RestDgram,
...
end.
If there are no options, so that HLen == IP_MIN_HDR_LEN and OptsLen == 0,
this reduces to
<<Opts:0/binary,Data/binary>> = RestDgram.
Is there anything "weird" about a datagram with no options?
If the issue is construction rather than matching,
is there anything "weird" about constructing a datagram with no options?
Zero is a perfectly good size. It would be a serious defect in Erlang
if it _didn't_ allow zero-size segments. And since zero *is* a
perfectly sensible size for things, it seems to me that failing to
handle zero sensibly is the thing which would have needed special
mention in the documentation, while treating zero sensibly is no
odder than treating one sensibly.
On 8/03/2013, at 2:43 AM, Kostis Sagonas wrote:
> In revising some HiPE test suites for bit syntax, I noticed some pattern matching with 0-sized segments. They can be illustrated by the following:
>
> Eshell V5.10.2 (abort with ^G)
> 1> <<X:0>> = <<>>.
More information about the erlang-questions
mailing list