Safe Haskell | None |
---|---|
Language | Haskell98 |
Parse bits easily. Parsing can be done either in a monadic style, or more
efficiently, using the Applicative
style.
For the monadic style, write your parser as a BitGet
monad using the
functions and run it with runBitGet
.
For the applicative style, compose the fuctions
to make a Block
.
Use block
to turn it into the BitGet
monad to be able to run it with
runBitGet
.
Synopsis
- data BitGet a
- runBitGet :: BitGet a -> Get a
- getBool :: BitGet Bool
- getWord8 :: Int -> BitGet Word8
- getWord16be :: Int -> BitGet Word16
- getWord32be :: Int -> BitGet Word32
- getWord64be :: Int -> BitGet Word64
- data Block a
- block :: Block a -> BitGet a
- bool :: Block Bool
- word8 :: Int -> Block Word8
- word16be :: Int -> Block Word16
- word32be :: Int -> Block Word32
- word64be :: Int -> Block Word64
- byteString :: Int -> Block ByteString
- getByteString :: Int -> BitGet ByteString
- getLazyByteString :: Int -> BitGet ByteString
- isEmpty :: BitGet Bool
BitGet monad
Parse bits using a monad.
myBitParser ::Get
(Word8
,Word8
) myBitParser =runGetBit
parse4by4 parse4by4 ::BitGet
(Word8
,Word8
) parse4by4 = do bits <-getWord8
4 more <-getWord8
4 return (bits,more)
Get bytes
Blocks
Parse more efficiently in blocks. Each block is read with only one boundry check (checking that there is enough input) as the size of the block can be calculated statically. This is somewhat limiting as you cannot make the parsing depend on the input being parsed.
data IPV6Header = IPV6Header { ipv6Version ::Word8
, ipv6TrafficClass ::Word8
, ipv6FlowLabel :: 'Word32 , ipv6PayloadLength ::Word16
, ipv6NextHeader ::Word8
, ipv6HopLimit ::Word8
, ipv6SourceAddress ::ByteString
, ipv6DestinationAddress ::ByteString
} ipv6headerblock = IPV6Header<$>
word8
4<*>
word8
8<*>
word32be
24<*>
word16be
16<*>
word8
8<*>
word8
8<*>
byteString
16<*>
byteString
16 ipv6Header ::Get
IPV6Header ipv6Header =runBitGet
(block
ipv6headerblock)
A block that will be read with only one boundry check. Needs to know the number of bits in advance.
block :: Block a -> BitGet a Source #
Get a block. Will be read with one single boundry check, and
therefore requires a statically known number of bits.
Build blocks using bool
, word8
, word16be
, word32be
, word64be
,
byteString
and Applicative
.
Read in Blocks
byteString :: Int -> Block ByteString Source #
Read n
bytes as a ByteString
.
getByteString :: Int -> BitGet ByteString Source #
Get n
bytes as a ByteString
.
getLazyByteString :: Int -> BitGet ByteString Source #
Get n
bytes as a lazy ByteString.