Copyright | Lennart Kolmodin Ross Paterson George Giorgidze |
---|---|
License | BSD3 |
Maintainer | George Giorgidze <http://cs.nott.ac.uk/~ggg/> |
Stability | experimental |
Portability | Portable |
Safe Haskell | None |
Language | Haskell98 |
Efficient construction of lazy bytestrings.
- data Builder
- toLazyByteString :: Builder -> ByteString
- empty :: Builder
- singleton :: Word8 -> Builder
- putWord8 :: Word8 -> Builder
- putInt8 :: Int8 -> Builder
- append :: Builder -> Builder -> Builder
- fromByteString :: ByteString -> Builder
- fromLazyByteString :: ByteString -> Builder
- putString :: String -> Builder
- flush :: Builder
- putWord16be :: Word16 -> Builder
- putWord24be :: Word32 -> Builder
- putWord32be :: Word32 -> Builder
- putWord64be :: Word64 -> Builder
- putInt16be :: Int16 -> Builder
- putInt32be :: Int32 -> Builder
- putInt64be :: Int64 -> Builder
- putWord16le :: Word16 -> Builder
- putWord24le :: Word32 -> Builder
- putWord32le :: Word32 -> Builder
- putWord64le :: Word64 -> Builder
- putInt16le :: Int16 -> Builder
- putInt32le :: Int32 -> Builder
- putInt64le :: Int64 -> Builder
- putWordHost :: Word -> Builder
- putWord16host :: Word16 -> Builder
- putWord32host :: Word32 -> Builder
- putWord64host :: Word64 -> Builder
- putVarLenBe :: Word64 -> Builder
- putVarLenLe :: Word64 -> Builder
The Builder type
A Builder
is an efficient way to build lazy ByteString
s.
There are several functions for constructing Builder
s, but only one
to inspect them: to extract any data, you have to turn them into lazy
ByteString
s using toLazyByteString
.
Internally, a Builder
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 Builder
.
toLazyByteString :: Builder -> ByteString Source #
O(n). Extract a lazy ByteString
from a Builder
.
The construction work takes place if and when the relevant part of
the lazy ByteString
is demanded.
Constructing Builders
singleton :: Word8 -> Builder Source #
O(1). A Builder taking a single byte, satisfying
toLazyByteString
(singleton
b) =singleton
b
append :: Builder -> Builder -> Builder Source #
O(1). The concatenation of two Builders, an associative operation
with identity empty
, satisfying
toLazyByteString
(append
x y) =append
(toLazyByteString
x) (toLazyByteString
y)
fromByteString :: ByteString -> Builder Source #
O(1). A Builder taking a ByteString
, satisfying
toLazyByteString
(fromByteString
bs) =fromChunks
[bs]
fromLazyByteString :: ByteString -> Builder Source #
O(1). A Builder taking a lazy ByteString
, satisfying
toLazyByteString
(fromLazyByteString
bs) = bs
Flushing the buffer state
O(1). Pop the ByteString
we have constructed so far, if any,
yielding a new chunk in the result lazy ByteString
.
Derived Builders
Big-endian writes
putWord16be :: Word16 -> Builder Source #
Write a Word16 in big endian format
putWord24be :: Word32 -> Builder Source #
Write a 24 bit number in big endian format represented as Word32
putWord32be :: Word32 -> Builder Source #
Write a Word32 in big endian format
putWord64be :: Word64 -> Builder Source #
Write a Word64 in big endian format
putInt16be :: Int16 -> Builder Source #
putInt32be :: Int32 -> Builder Source #
putInt64be :: Int64 -> Builder Source #
Little-endian writes
putWord16le :: Word16 -> Builder Source #
Write a Word16 in little endian format
putWord24le :: Word32 -> Builder Source #
Write a 24 bit number in little endian format represented as Word32
putWord32le :: Word32 -> Builder Source #
Write a Word32 in little endian format
putWord64le :: Word64 -> Builder Source #
Write a Word64 in little endian format
putInt16le :: Int16 -> Builder Source #
putInt32le :: Int32 -> Builder Source #
putInt64le :: Int64 -> Builder Source #
Host-endian, unaligned writes
putWordHost :: Word -> Builder Source #
O(1). A Builder taking a single native machine word. The word is written in host order, host endian form, for the machine you're on. On a 64 bit machine the Word is an 8 byte value, on a 32 bit machine, 4 bytes. Values written this way are not portable to different endian or word sized machines, without conversion.
putWord16host :: Word16 -> Builder Source #
Write a Word16 in native host order and host endianness. 2 bytes will be written, unaligned.
putWord32host :: Word32 -> Builder Source #
Write a Word32 in native host order and host endianness. 4 bytes will be written, unaligned.
putWord64host :: Word64 -> Builder Source #
Write a Word64 in native host order. On a 32 bit machine we write two host order Word32s, in big endian form. 8 bytes will be written, unaligned.
putVarLenBe :: Word64 -> Builder Source #
putVarLenLe :: Word64 -> Builder Source #