License | MIT |
---|---|
Maintainer | Joachim Breitner |
Safe Haskell | None |
Language | Haskell2010 |
| This module implements encoding and decoding of Natural
and Integer
values according to LEB128 and SLEB128. See
https://en.wikipedia.org/wiki/LEB128 for a specification.
The module provides conversion to and from strict bytestrings.
Additionally, to integrate these into your own parsers and serializers, you
can use the interfaces based on Builder
as well as cereal
's Get
and Put
monad.
The decoders will fail if the input is not in canonical representation, i.e. longer than necessary.
This code is inspired by Andreas Klebinger's LEB128 implementation in GHC.
Synopsis
- class (Bits a, Num a, Integral a) => LEB128 a
- class (Bits a, Num a, Integral a) => SLEB128 a
- toLEB128 :: LEB128 a => a -> ByteString
- fromLEB128 :: LEB128 a => ByteString -> Either String a
- toSLEB128 :: SLEB128 a => a -> ByteString
- fromSLEB128 :: SLEB128 a => ByteString -> Either String a
- buildLEB128 :: LEB128 a => a -> Builder
- buildSLEB128 :: SLEB128 a => a -> Builder
- getLEB128 :: forall a. LEB128 a => Get a
- getSLEB128 :: forall a. SLEB128 a => Get a
- putLEB128 :: LEB128 a => Putter a
- putSLEB128 :: SLEB128 a => Putter a
The class of encodable and decodable types
class (Bits a, Num a, Integral a) => LEB128 a Source #
Unsigned number types can be LEB128-encoded
Instances
LEB128 Natural Source # | |
Defined in Data.Serialize.LEB128 | |
LEB128 Word Source # | |
Defined in Data.Serialize.LEB128 | |
LEB128 Word8 Source # | |
Defined in Data.Serialize.LEB128 | |
LEB128 Word16 Source # | |
Defined in Data.Serialize.LEB128 | |
LEB128 Word32 Source # | |
Defined in Data.Serialize.LEB128 | |
LEB128 Word64 Source # | |
Defined in Data.Serialize.LEB128 |
class (Bits a, Num a, Integral a) => SLEB128 a Source #
Signed number types can be SLEB128-encoded
Instances
SLEB128 Int Source # | |
Defined in Data.Serialize.LEB128 | |
SLEB128 Int8 Source # | |
Defined in Data.Serialize.LEB128 | |
SLEB128 Int16 Source # | |
Defined in Data.Serialize.LEB128 | |
SLEB128 Int32 Source # | |
Defined in Data.Serialize.LEB128 | |
SLEB128 Int64 Source # | |
Defined in Data.Serialize.LEB128 | |
SLEB128 Integer Source # | |
Defined in Data.Serialize.LEB128 |
Bytestring-based interface
toLEB128 :: LEB128 a => a -> ByteString Source #
LEB128-encodes a natural number to a strict bytestring
fromLEB128 :: LEB128 a => ByteString -> Either String a Source #
LEB128-decodes a natural number from a strict bytestring
toSLEB128 :: SLEB128 a => a -> ByteString Source #
SLEB128-encodes an integer to a strict bytestring
fromSLEB128 :: SLEB128 a => ByteString -> Either String a Source #
SLEB128-decodes an integer from a strict bytestring
Builder interface
buildLEB128 :: LEB128 a => a -> Builder Source #
LEB128-encodes a natural number via a builder
buildSLEB128 :: SLEB128 a => a -> Builder Source #
SLEB128-encodes an integer via a builder
Cereal interface
getSLEB128 :: forall a. SLEB128 a => Get a Source #
SLEB128-decodes an integer via cereal