Support big-endian and little-endian encoding of 16-bit and 32-bit signed integers

This commit is contained in:
Andrew Martin 2019-10-10 10:28:01 -04:00
parent 71568edb50
commit 691749e27a
3 changed files with 66 additions and 2 deletions

View file

@ -4,10 +4,10 @@
* Introduce `consLensBE32` for efficient serialization of wire protocols
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 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
converting byte sequences to builders.

View file

@ -57,21 +57,31 @@ module Data.ByteArray.Builder
, word32BE
, word16BE
, int64BE
, int32BE
, int16BE
-- **** Little Endian
, word64LE
, word32LE
, word16LE
, int64LE
, int32LE
, int16LE
-- *** Many
, word8Array
-- **** Big Endian
, word16ArrayBE
, word32ArrayBE
, word64ArrayBE
, int64ArrayBE
, int32ArrayBE
, int16ArrayBE
-- **** Little Endian
, word16ArrayLE
, word32ArrayLE
, word64ArrayLE
, int64ArrayLE
, int32ArrayLE
, int16ArrayLE
-- ** Prefixing with Length
, consLength32BE
, consLength64BE
@ -228,6 +238,24 @@ insert (Bytes (ByteArray src# ) (I# soff# ) (I# slen# )) = Builder
word8Array :: PrimArray Word8 -> Int -> Int -> Builder
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 src@(PrimArray arr) soff0 slen0 = case targetByteOrder of
LittleEndian -> bytes (Bytes (ByteArray arr) (soff0 * 8) (slen0 * 8))
@ -536,11 +564,31 @@ shrinkMutableByteArray (MutableByteArray arr) (I# sz) =
int64LE :: Int64 -> Builder
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
-- signed integer in a big-endian fashion.
int64BE :: Int64 -> Builder
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
-- word in a little-endian fashion.
word64LE :: Word64 -> Builder

View file

@ -59,11 +59,15 @@ module Data.ByteArray.Builder.Bounded
, word32BE
, word16BE
, int64BE
, int32BE
, int16BE
-- **** Little Endian
, word64LE
, word32LE
, word16LE
, int64LE
, int32LE
, int16LE
-- * Encode Floating-Point Types
, doubleDec
) where
@ -590,9 +594,21 @@ char c
int64BE :: Int64 -> Builder 8
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 (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
-- word in a little-endian fashion.
word64LE :: Word64 -> Builder 8