Comment

Comment by rwmj

All I can say that OCaml bitstring can handle fields of any bit width aligned to any bit and with any endianness. It will optimize the aligned cases at compile time. I don't think it suffers from the problem in your article because each field is treated separately (but TBH I wrote this 13 years ago, so you may be better looking at the sources).

It's possible to use variable-bit-length fields and alignment, eg:

  {| len : 4; i : len+1 |}
would match a 4 bit unsigned length field followed by a "len+1" (1-16) bit big-endian integer (although this cannot be optimized at compile time, so will be rather slow). Note that the i field starts at the 4th bit.

Even variable endianness is possible, eg:

  {| len : 4;
     e : 1;
     i : len+1 : endian(if e then LittleEndian else BigEndian) |}
where e is a single bit flag controlling endianness and the integer starts at the 5th bit.

Writing this drove me slightly round the bend, so I don't envy the current maintainer :-)

parent rwmj 6 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton