Support big-endian and little-endian encoding of 16-bit and 32-bit signed integers
This commit is contained in:
parent
71568edb50
commit
691749e27a
3 changed files with 66 additions and 2 deletions
|
@ -4,10 +4,10 @@
|
||||||
|
|
||||||
* Introduce `consLensBE32` for efficient serialization of wire protocols
|
* Introduce `consLensBE32` for efficient serialization of wire protocols
|
||||||
that require prefixing a payload with its length.
|
that require prefixing a payload with its length.
|
||||||
* Add `int64BE` as a convenience.
|
* Add `int{16,32,64}BE` and `int{16,32,64LE}` as conveniences.
|
||||||
* Add little-endian encoding functions for `Word16`, `Word32`, and `Word64`.
|
* Add little-endian encoding functions for `Word16`, `Word32`, and `Word64`.
|
||||||
* Add big-endian and little-endian functions for copying a
|
* Add big-endian and little-endian functions for copying a
|
||||||
`PrimArray` of numbers into a builder.
|
`PrimArray` of numbers (both signed and unsigned) into a builder.
|
||||||
* Add `flush`, `copy`, and `insert` for better control when
|
* Add `flush`, `copy`, and `insert` for better control when
|
||||||
converting byte sequences to builders.
|
converting byte sequences to builders.
|
||||||
|
|
||||||
|
|
|
@ -57,21 +57,31 @@ module Data.ByteArray.Builder
|
||||||
, word32BE
|
, word32BE
|
||||||
, word16BE
|
, word16BE
|
||||||
, int64BE
|
, int64BE
|
||||||
|
, int32BE
|
||||||
|
, int16BE
|
||||||
-- **** Little Endian
|
-- **** Little Endian
|
||||||
, word64LE
|
, word64LE
|
||||||
, word32LE
|
, word32LE
|
||||||
, word16LE
|
, word16LE
|
||||||
, int64LE
|
, int64LE
|
||||||
|
, int32LE
|
||||||
|
, int16LE
|
||||||
-- *** Many
|
-- *** Many
|
||||||
, word8Array
|
, word8Array
|
||||||
-- **** Big Endian
|
-- **** Big Endian
|
||||||
, word16ArrayBE
|
, word16ArrayBE
|
||||||
, word32ArrayBE
|
, word32ArrayBE
|
||||||
, word64ArrayBE
|
, word64ArrayBE
|
||||||
|
, int64ArrayBE
|
||||||
|
, int32ArrayBE
|
||||||
|
, int16ArrayBE
|
||||||
-- **** Little Endian
|
-- **** Little Endian
|
||||||
, word16ArrayLE
|
, word16ArrayLE
|
||||||
, word32ArrayLE
|
, word32ArrayLE
|
||||||
, word64ArrayLE
|
, word64ArrayLE
|
||||||
|
, int64ArrayLE
|
||||||
|
, int32ArrayLE
|
||||||
|
, int16ArrayLE
|
||||||
-- ** Prefixing with Length
|
-- ** Prefixing with Length
|
||||||
, consLength32BE
|
, consLength32BE
|
||||||
, consLength64BE
|
, consLength64BE
|
||||||
|
@ -228,6 +238,24 @@ insert (Bytes (ByteArray src# ) (I# soff# ) (I# slen# )) = Builder
|
||||||
word8Array :: PrimArray Word8 -> Int -> Int -> Builder
|
word8Array :: PrimArray Word8 -> Int -> Int -> Builder
|
||||||
word8Array (PrimArray arr) off len = bytes (Bytes (ByteArray arr) off len)
|
word8Array (PrimArray arr) off len = bytes (Bytes (ByteArray arr) off len)
|
||||||
|
|
||||||
|
int64ArrayLE :: PrimArray Int64 -> Int -> Int -> Builder
|
||||||
|
int64ArrayLE (PrimArray x) = word64ArrayLE (PrimArray x)
|
||||||
|
|
||||||
|
int64ArrayBE :: PrimArray Int64 -> Int -> Int -> Builder
|
||||||
|
int64ArrayBE (PrimArray x) = word64ArrayBE (PrimArray x)
|
||||||
|
|
||||||
|
int32ArrayLE :: PrimArray Int32 -> Int -> Int -> Builder
|
||||||
|
int32ArrayLE (PrimArray x) = word32ArrayLE (PrimArray x)
|
||||||
|
|
||||||
|
int32ArrayBE :: PrimArray Int32 -> Int -> Int -> Builder
|
||||||
|
int32ArrayBE (PrimArray x) = word32ArrayBE (PrimArray x)
|
||||||
|
|
||||||
|
int16ArrayLE :: PrimArray Int16 -> Int -> Int -> Builder
|
||||||
|
int16ArrayLE (PrimArray x) = word16ArrayLE (PrimArray x)
|
||||||
|
|
||||||
|
int16ArrayBE :: PrimArray Int16 -> Int -> Int -> Builder
|
||||||
|
int16ArrayBE (PrimArray x) = word16ArrayBE (PrimArray x)
|
||||||
|
|
||||||
word64ArrayLE :: PrimArray Word64 -> Int -> Int -> Builder
|
word64ArrayLE :: PrimArray Word64 -> Int -> Int -> Builder
|
||||||
word64ArrayLE src@(PrimArray arr) soff0 slen0 = case targetByteOrder of
|
word64ArrayLE src@(PrimArray arr) soff0 slen0 = case targetByteOrder of
|
||||||
LittleEndian -> bytes (Bytes (ByteArray arr) (soff0 * 8) (slen0 * 8))
|
LittleEndian -> bytes (Bytes (ByteArray arr) (soff0 * 8) (slen0 * 8))
|
||||||
|
@ -536,11 +564,31 @@ shrinkMutableByteArray (MutableByteArray arr) (I# sz) =
|
||||||
int64LE :: Int64 -> Builder
|
int64LE :: Int64 -> Builder
|
||||||
int64LE w = fromBounded Nat.constant (Bounded.int64LE w)
|
int64LE w = fromBounded Nat.constant (Bounded.int64LE w)
|
||||||
|
|
||||||
|
-- | Requires exactly 4 bytes. Dump the octets of a 32-bit
|
||||||
|
-- signed integer in a little-endian fashion.
|
||||||
|
int32LE :: Int32 -> Builder
|
||||||
|
int32LE w = fromBounded Nat.constant (Bounded.int32LE w)
|
||||||
|
|
||||||
|
-- | Requires exactly 2 bytes. Dump the octets of a 16-bit
|
||||||
|
-- signed integer in a little-endian fashion.
|
||||||
|
int16LE :: Int16 -> Builder
|
||||||
|
int16LE w = fromBounded Nat.constant (Bounded.int16LE w)
|
||||||
|
|
||||||
-- | Requires exactly 8 bytes. Dump the octets of a 64-bit
|
-- | Requires exactly 8 bytes. Dump the octets of a 64-bit
|
||||||
-- signed integer in a big-endian fashion.
|
-- signed integer in a big-endian fashion.
|
||||||
int64BE :: Int64 -> Builder
|
int64BE :: Int64 -> Builder
|
||||||
int64BE w = fromBounded Nat.constant (Bounded.int64BE w)
|
int64BE w = fromBounded Nat.constant (Bounded.int64BE w)
|
||||||
|
|
||||||
|
-- | Requires exactly 4 bytes. Dump the octets of a 32-bit
|
||||||
|
-- signed integer in a big-endian fashion.
|
||||||
|
int32BE :: Int32 -> Builder
|
||||||
|
int32BE w = fromBounded Nat.constant (Bounded.int32BE w)
|
||||||
|
|
||||||
|
-- | Requires exactly 2 bytes. Dump the octets of a 16-bit
|
||||||
|
-- signed integer in a big-endian fashion.
|
||||||
|
int16BE :: Int16 -> Builder
|
||||||
|
int16BE w = fromBounded Nat.constant (Bounded.int16BE w)
|
||||||
|
|
||||||
-- | Requires exactly 8 bytes. Dump the octets of a 64-bit
|
-- | Requires exactly 8 bytes. Dump the octets of a 64-bit
|
||||||
-- word in a little-endian fashion.
|
-- word in a little-endian fashion.
|
||||||
word64LE :: Word64 -> Builder
|
word64LE :: Word64 -> Builder
|
||||||
|
|
|
@ -59,11 +59,15 @@ module Data.ByteArray.Builder.Bounded
|
||||||
, word32BE
|
, word32BE
|
||||||
, word16BE
|
, word16BE
|
||||||
, int64BE
|
, int64BE
|
||||||
|
, int32BE
|
||||||
|
, int16BE
|
||||||
-- **** Little Endian
|
-- **** Little Endian
|
||||||
, word64LE
|
, word64LE
|
||||||
, word32LE
|
, word32LE
|
||||||
, word16LE
|
, word16LE
|
||||||
, int64LE
|
, int64LE
|
||||||
|
, int32LE
|
||||||
|
, int16LE
|
||||||
-- * Encode Floating-Point Types
|
-- * Encode Floating-Point Types
|
||||||
, doubleDec
|
, doubleDec
|
||||||
) where
|
) where
|
||||||
|
@ -590,9 +594,21 @@ char c
|
||||||
int64BE :: Int64 -> Builder 8
|
int64BE :: Int64 -> Builder 8
|
||||||
int64BE (I64# i) = word64BE (W64# (int2Word# i))
|
int64BE (I64# i) = word64BE (W64# (int2Word# i))
|
||||||
|
|
||||||
|
int32BE :: Int32 -> Builder 4
|
||||||
|
int32BE (I32# i) = word32BE (W32# (int2Word# i))
|
||||||
|
|
||||||
|
int16BE :: Int16 -> Builder 2
|
||||||
|
int16BE (I16# i) = word16BE (W16# (int2Word# i))
|
||||||
|
|
||||||
int64LE :: Int64 -> Builder 8
|
int64LE :: Int64 -> Builder 8
|
||||||
int64LE (I64# i) = word64LE (W64# (int2Word# i))
|
int64LE (I64# i) = word64LE (W64# (int2Word# i))
|
||||||
|
|
||||||
|
int32LE :: Int32 -> Builder 4
|
||||||
|
int32LE (I32# i) = word32LE (W32# (int2Word# i))
|
||||||
|
|
||||||
|
int16LE :: Int16 -> Builder 2
|
||||||
|
int16LE (I16# i) = word16LE (W16# (int2Word# i))
|
||||||
|
|
||||||
-- | Requires exactly 8 bytes. Dump the octets of a 64-bit
|
-- | Requires exactly 8 bytes. Dump the octets of a 64-bit
|
||||||
-- word in a little-endian fashion.
|
-- word in a little-endian fashion.
|
||||||
word64LE :: Word64 -> Builder 8
|
word64LE :: Word64 -> Builder 8
|
||||||
|
|
Loading…
Reference in a new issue