Closed singpolyma closed 11 years ago
I use binary-strict for the Data.Binary.Strict.BitGet and BitPut monads for parsing bit-aligned data. The binary and cereal packages seems to only support byte-aligned data. Parsing of bit-aligned data is required for Huffman encoding.
Strangely, there is a Data.Binary.BitPut, but no Data.Binary.BitGet?
Looking at the code, it seems the whole package dependency is only used for implementing bitpack
and bitunpack
, which are more succinctly (and without any dependency) written as:
bitpack :: [Bool] -> B.ByteString
bitpack = B.pack . map packByte . takeWhile (not . null) . unfoldr (Just . splitAt 8)
where
packByte = foldl' (\i b -> (i `shiftL` 1) .|. (fromIntegral $ fromEnum b)) 0
bitunpack :: S.ByteString -> Either String [Bool]
bitunpack = Right . concatMap (\byte -> map (testBit byte) [7,6..0]) . S.unpack
I can prepare a pull request for these versions (and dropping the binary-strict
) dependency, if this meets with your approval.
You will also note that bitunpack
never fails, so the type signature could be changed to reflect that.
The bitpack
dependency also happens to be used in Dust.Model.Packet
, but that can be easily replaced with calls to cereal
(which is already a dependency anyway). I would include such a change in any pull request.
Sounds good!
Done!
This package currently uses
binary-strict
, an older package that simply clonedbinary
because the error handling inbinary
was abysmal at the time. Thebinary
package has improved considerably, and I would recommend moving to it.The
cereal
package is also another (more actively maintained) option, but I believe things will be moving back tobinary
in general, now that the package is so much better.