| Copyright | Lennart Kolmodin Ross Paterson Adam Langley |
|---|---|
| License | BSD3-style (see LICENSE) |
| Maintainer | Adam Langley <agl@imperialviolet.org> |
| Stability | experimental |
| Portability | portable to Hugs and GHC |
| Safe Haskell | None |
| Language | Haskell2010 |
Data.Binary.BitBuilder
Description
Efficient construction of lazy bytestrings, bit by bit.
Synopsis
- data BitBuilder
- toLazyByteString :: BitBuilder -> ByteString
- empty :: BitBuilder
- singleton :: Bool -> BitBuilder
- append :: BitBuilder -> BitBuilder -> BitBuilder
- fromByteString :: (ByteString, Int) -> BitBuilder
- fromLazyByteString :: ByteString -> BitBuilder
- fromBits :: (Integral a, Bits a) => Int -> a -> BitBuilder
- flush :: BitBuilder
The Builder type
data BitBuilder Source #
A BitBuilder is an efficient way to build lazy ByteStrings.
There are several functions for constructing BitBuilders, but only one
to inspect them: to extract any data, you have to turn them into lazy
ByteStrings using toLazyByteString.
Internally, a BitBuilder constructs a lazy Bytestring by filling byte
arrays piece by piece. As each buffer is filled, it is 'popped'
off, to become a new chunk of the resulting lazy ByteString.
All this is hidden from the user of the BitBuilder.
This is closely based on the Builder monad, but this one deals with single bits at a time.
Instances
| Show BitBuilder Source # | |
Defined in Data.Binary.BitBuilder Methods showsPrec :: Int -> BitBuilder -> ShowS # show :: BitBuilder -> String # showList :: [BitBuilder] -> ShowS # | |
| Semigroup BitBuilder Source # | |
Defined in Data.Binary.BitBuilder Methods (<>) :: BitBuilder -> BitBuilder -> BitBuilder # sconcat :: NonEmpty BitBuilder -> BitBuilder # stimes :: Integral b => b -> BitBuilder -> BitBuilder # | |
| Monoid BitBuilder Source # | |
Defined in Data.Binary.BitBuilder Methods mempty :: BitBuilder # mappend :: BitBuilder -> BitBuilder -> BitBuilder # mconcat :: [BitBuilder] -> BitBuilder # | |
toLazyByteString :: BitBuilder -> ByteString Source #
O(n). Extract a lazy ByteString from a BitBuilder.
The construction work takes place if and when the relevant part of
the lazy ByteString is demanded.
Constructing Builders
empty :: BitBuilder Source #
O(1). The empty BitBuilder, satisfying
singleton :: Bool -> BitBuilder Source #
O(1). A BitBuilder taking a single bit, satisfying
toLazyByteString(singletonb) =singletonb
append :: BitBuilder -> BitBuilder -> BitBuilder Source #
O(1). The concatenation of two BitBuilders, an associative operation
with identity empty, satisfying
toLazyByteString(appendx y) =append(toLazyByteStringx) (toLazyByteStringy)
fromByteString :: (ByteString, Int) -> BitBuilder Source #
fromLazyByteString :: ByteString -> BitBuilder Source #
O(1). A BitBuilder taking a lazy ByteString, satisfying
toLazyByteString(fromLazyByteStringbs) = bs
fromBits :: (Integral a, Bits a) => Int -> a -> BitBuilder Source #
Construct a BitBuilder by taking the bottom n bits of a Bits instance. If the instance has less than n bits, this acts as if there was an infinite zero filled prefix
Flushing the buffer state
flush :: BitBuilder Source #
O(1). Pop the ByteString we have constructed so far, if any,
yielding a new chunk in the result lazy ByteString.