savi
savi copied to clipboard
Bit pattern matching
Just looking at msgpack spec, which defines a table like this to decode the type of the following value:
positive fixint | 0xxxxxxx
fixmap | 1000xxxx
...
nil | 11000000
The code to test for example for "positive fixint" looks like
first_byte.bit_and(0b10000000).is_nonzero
which IMHO is un-intuitive.
It would be nice to write something along the lines:
first_byte =~ b/0xxxxxxx/
This would require a new BitMask type and a new literal to create it. Once we have custom string literals (#247), we could create e.g. a BitMask using something like '0xxxxxxx'_bitmask. This would actually be simple if we had some kind of comptime evaluations, then we could just add a method bitmask! on String.
Or something like Ocaml's Bitstring: https://ocaml.org/p/bitstring/latest/doc/Bitstring/index.html