Safe Haskell | Trustworthy |
---|---|
Language | Haskell98 |
- data Get e a
- runGetOrFail :: ByteOffset -> Get e a -> ByteString -> Either (ByteString, ByteOffset, Either String e) (ByteString, ByteOffset, a)
- type ByteOffset = Int64
- data Decoder e a
- = Fail !ByteString !ByteOffset (Either String e)
- | Partial (Maybe ByteString -> Decoder e a)
- | Done !ByteString !ByteOffset a
- runGetIncremental :: ByteOffset -> Get e a -> Decoder e a
- pushChunk :: Decoder e a -> ByteString -> Decoder e a
- pushChunks :: Decoder e a -> ByteString -> Decoder e a
- pushEndOfInput :: Decoder e a -> Decoder e a
- skip :: Int -> Get () ()
- isEmpty :: Get e Bool
- bytesRead :: Get e Int64
- totalBytesRead :: Get e Int64
- isolate :: Int -> Get e a -> (Int -> e) -> Get e a
- lookAhead :: Get e a -> Get e a
- lookAheadM :: Get e (Maybe a) -> Get e (Maybe a)
- lookAheadE :: Get e (Either a b) -> Get e (Either a b)
- label :: String -> Get String a -> Get String a
- onError :: (e -> e') -> Get e a -> Get e' a
- withError :: Get () a -> e -> Get e a
- failG :: e -> Get e a
- getByteString :: Int -> Get () ByteString
- getLazyByteString :: Int64 -> Get () ByteString
- getLazyByteStringNul :: Get () ByteString
- getRemainingLazyByteString :: Get e ByteString
- getWord8 :: Get () Word8
- getWord16be :: Get () Word16
- getWord32be :: Get () Word32
- getWord64be :: Get () Word64
- getWord16le :: Get () Word16
- getWord32le :: Get () Word32
- getWord64le :: Get () Word64
- getWordhost :: Get () Word
- getWord16host :: Get () Word16
- getWord32host :: Get () Word32
- getWord64host :: Get () Word64
- getInt8 :: Get () Int8
- getInt16be :: Get () Int16
- getInt32be :: Get () Int32
- getInt64be :: Get () Int64
- getInt16le :: Get () Int16
- getInt32le :: Get () Int32
- getInt64le :: Get () Int64
- getInthost :: Get () Int
- getInt16host :: Get () Int16
- getInt32host :: Get () Int32
- getInt64host :: Get () Int64
- getFloatbe :: Get () Float
- getFloatle :: Get () Float
- getFloathost :: Get () Float
- getDoublebe :: Get () Double
- getDoublele :: Get () Double
- getDoublehost :: Get () Double
The Get monad
The lazy input interface
The lazy interface consumes a single lazy ByteString
. It's the easiest
interface to get started with, but it doesn't support interleaving I/O and
parsing, unless lazy I/O is used.
There is no way to provide more input other than the initial data. To be able to incrementally give more data, see the incremental input interface.
runGetOrFail :: ByteOffset -> Get e a -> ByteString -> Either (ByteString, ByteOffset, Either String e) (ByteString, ByteOffset, a) Source #
type ByteOffset = Int64 Source #
An offset, counted in bytes.
The incremental input interface
The incremental interface gives you more control over how input is provided during parsing. This lets you e.g. interleave parsing and I/O.
The incremental interface consumes a strict ByteString
at a time, each
being part of the total amount of input. If your decoder needs more input to
finish it will return a Partial
with a continuation.
If there is no more input, provide it Nothing
.
Fail
will be returned if it runs into an error, together with a message,
the position and the remaining input.
If it succeeds it will return Done
with the resulting value,
the position and the remaining input.
A decoder procuced by running a Get
monad.
Fail !ByteString !ByteOffset (Either String e) | The decoder ran into an error. The decoder either used
|
Partial (Maybe ByteString -> Decoder e a) | The decoder has consumed the available input and needs
more to continue. Provide |
Done !ByteString !ByteOffset a | The decoder has successfully finished. Except for the output value you also get any unused input as well as the number of bytes consumed. |
runGetIncremental :: ByteOffset -> Get e a -> Decoder e a Source #
Run a Get
monad. See Decoder
for what to do next, like providing
input, handling decoder errors and to get the output value.
Hint: Use the helper functions pushChunk
, pushChunks
and
pushEndOfInput
.
Providing input
pushChunk :: Decoder e a -> ByteString -> Decoder e a Source #
Feed a Decoder
with more input. If the Decoder
is Done
or Fail
it
will add the input to ByteString
of unconsumed input.
runGetIncremental
myParser `pushChunk` myInput1 `pushChunk` myInput2
pushChunks :: Decoder e a -> ByteString -> Decoder e a Source #
Feed a Decoder
with more input. If the Decoder
is Done
or Fail
it
will add the input to ByteString
of unconsumed input.
runGetIncremental
myParser `pushChunks` myLazyByteString
pushEndOfInput :: Decoder e a -> Decoder e a Source #
Decoding
isEmpty :: Get e Bool Source #
Test whether all input has been consumed, i.e. there are no remaining undecoded bytes.
totalBytesRead :: Get e Int64 Source #
Get e the total number of bytes read to this point.
lookAhead :: Get e a -> Get e a Source #
Run the given decoder, but without consuming its input. If the given decoder fails, then so will this function.
Since: 0.7.0.0
onError :: (e -> e') -> Get e a -> Get e' a Source #
Convert decoder error. If the decoder fails, the given function will be applied to the error message.
withError :: Get () a -> e -> Get e a Source #
Set decoder error. If the decoder fails, the given error will be used as the error message.
ByteStrings
getByteString :: Int -> Get () ByteString Source #
An efficient get method for strict ByteStrings. Fails if fewer than n
bytes are left in the input. If n <= 0
then the empty string is returned.
getLazyByteString :: Int64 -> Get () ByteString Source #
An efficient get method for lazy ByteStrings. Fails if fewer than n
bytes are left in the input.
getLazyByteStringNul :: Get () ByteString Source #
Get a lazy ByteString that is terminated with a NUL byte. The returned string does not contain the NUL byte. Fails if it reaches the end of input without finding a NUL.
getRemainingLazyByteString :: Get e ByteString Source #
Get the remaining bytes as a lazy ByteString. Note that this can be an expensive function to use as it forces reading all input and keeping the string in-memory.
Decoding Words
Big-endian decoding
getWord16be :: Get () Word16 Source #
Read a Word16 in big endian format
getWord32be :: Get () Word32 Source #
Read a Word32 in big endian format
getWord64be :: Get () Word64 Source #
Read a Word64 in big endian format
Little-endian decoding
getWord16le :: Get () Word16 Source #
Read a Word16 in little endian format
getWord32le :: Get () Word32 Source #
Read a Word32 in little endian format
getWord64le :: Get () Word64 Source #
Read a Word64 in little endian format
Host-endian, unaligned decoding
getWordhost :: Get () Word Source #
O(1). Read a single native machine word. The word is read 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.
getWord16host :: Get () Word16 Source #
O(1). Read a 2 byte Word16 in native host order and host endianness.
getWord32host :: Get () Word32 Source #
O(1). Read a Word32 in native host order and host endianness.
getWord64host :: Get () Word64 Source #
O(1). Read a Word64 in native host order and host endianess.
Decoding Ints
Big-endian decoding
getInt16be :: Get () Int16 Source #
Read an Int16 in big endian format.
getInt32be :: Get () Int32 Source #
Read an Int32 in big endian format.
getInt64be :: Get () Int64 Source #
Read an Int64 in big endian format.
Little-endian decoding
getInt16le :: Get () Int16 Source #
Read an Int16 in little endian format.
getInt32le :: Get () Int32 Source #
Read an Int32 in little endian format.
getInt64le :: Get () Int64 Source #
Read an Int64 in little endian format.
Host-endian, unaligned decoding
getInthost :: Get () Int Source #
O(1). Read a single native machine word in native host
order. It works in the same way as getWordhost
.
getInt16host :: Get () Int16 Source #
O(1). Read a 2 byte Int16 in native host order and host endianness.
getInt32host :: Get () Int32 Source #
O(1). Read an Int32 in native host order and host endianness.
getInt64host :: Get () Int64 Source #
O(1). Read an Int64 in native host order and host endianess.