byte-order 0.1.0.0 → 0.1.1.0
raw patch · 5 files changed
+157/−3 lines, 5 filesdep +primitive-unalignedPVP ok
version bump matches the API change (PVP)
Dependencies added: primitive-unaligned
API changes (from Hackage documentation)
+ Data.Primitive.ByteArray.BigEndian: indexByteArray :: (Prim a, Bytes a) => ByteArray -> Int -> a
+ Data.Primitive.ByteArray.BigEndian: indexUnalignedByteArray :: (PrimUnaligned a, Bytes a) => ByteArray -> Int -> a
+ Data.Primitive.ByteArray.BigEndian: readByteArray :: (PrimMonad m, Prim a, Bytes a) => MutableByteArray (PrimState m) -> Int -> m a
+ Data.Primitive.ByteArray.BigEndian: readUnalignedByteArray :: (PrimMonad m, PrimUnaligned a, Bytes a) => MutableByteArray (PrimState m) -> Int -> m a
+ Data.Primitive.ByteArray.BigEndian: writeByteArray :: (PrimMonad m, Prim a, Bytes a) => MutableByteArray (PrimState m) -> Int -> a -> m ()
+ Data.Primitive.ByteArray.BigEndian: writeUnalignedByteArray :: (PrimMonad m, PrimUnaligned a, Bytes a) => MutableByteArray (PrimState m) -> Int -> a -> m ()
+ Data.Primitive.ByteArray.LittleEndian: indexByteArray :: (Prim a, Bytes a) => ByteArray -> Int -> a
+ Data.Primitive.ByteArray.LittleEndian: indexUnalignedByteArray :: (PrimUnaligned a, Bytes a) => ByteArray -> Int -> a
+ Data.Primitive.ByteArray.LittleEndian: readByteArray :: (PrimMonad m, Prim a, Bytes a) => MutableByteArray (PrimState m) -> Int -> m a
+ Data.Primitive.ByteArray.LittleEndian: readUnalignedByteArray :: (PrimMonad m, PrimUnaligned a, Bytes a) => MutableByteArray (PrimState m) -> Int -> m a
+ Data.Primitive.ByteArray.LittleEndian: writeByteArray :: (PrimMonad m, Prim a, Bytes a) => MutableByteArray (PrimState m) -> Int -> a -> m ()
+ Data.Primitive.ByteArray.LittleEndian: writeUnalignedByteArray :: (PrimMonad m, PrimUnaligned a, Bytes a) => MutableByteArray (PrimState m) -> Int -> a -> m ()
+ System.ByteOrder: instance (System.ByteOrder.Class.FixedOrdering b, Data.Primitive.ByteArray.Unaligned.PrimUnaligned a, System.ByteOrder.Class.Bytes a) => Data.Primitive.ByteArray.Unaligned.PrimUnaligned (System.ByteOrder.Fixed b a)
+ System.ByteOrder: instance GHC.Classes.Eq a => GHC.Classes.Eq (System.ByteOrder.Fixed b a)
+ System.ByteOrder: instance GHC.Classes.Ord a => GHC.Classes.Ord (System.ByteOrder.Fixed b a)
+ System.ByteOrder: instance GHC.Enum.Enum a => GHC.Enum.Enum (System.ByteOrder.Fixed b a)
+ System.ByteOrder: instance GHC.Num.Num a => GHC.Num.Num (System.ByteOrder.Fixed b a)
+ System.ByteOrder: instance GHC.Real.Integral a => GHC.Real.Integral (System.ByteOrder.Fixed b a)
+ System.ByteOrder: instance GHC.Real.Real a => GHC.Real.Real (System.ByteOrder.Fixed b a)
Files
- CHANGELOG.md +10/−2
- byte-order.cabal +4/−1
- src/Data/Primitive/ByteArray/BigEndian.hs +60/−0
- src/Data/Primitive/ByteArray/LittleEndian.hs +62/−0
- src/System/ByteOrder.hs +21/−0
CHANGELOG.md view
@@ -1,5 +1,13 @@ # Revision history for byte-order -## 0.1.0.0 -- YYYY-mm-dd+## 0.1.1.0 -- YYYY-mm-dd -* First version. Released on an unsuspecting world.+* Add `PrimUnaligned` instance for `Fixed`.+* Add modules for more convenient interface to reading and writing+ fixed-endianness elements to byte arrays:+ `Data.Primitive.ByteArray.LittleEndian` and+ `Data.Primitive.ByteArray.BigEndian`.++## 0.1.0.0 -- 2019-05-29++* Initial release.
byte-order.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.2 name: byte-order-version: 0.1.0.0+version: 0.1.1.0 synopsis: Portable big-endian and little-endian conversions description: This library provides an interface to portably work with byte@@ -19,11 +19,14 @@ library exposed-modules:+ Data.Primitive.ByteArray.BigEndian+ Data.Primitive.ByteArray.LittleEndian System.ByteOrder System.ByteOrder.Class build-depends: , base >=4.11.1.0 && <5 , primitive >=0.6.4 && <0.8+ , primitive-unaligned >=0.1.1 && <0.2 hs-source-dirs: src default-language: Haskell2010 ghc-options: -O2 -Wall
+ src/Data/Primitive/ByteArray/BigEndian.hs view
@@ -0,0 +1,60 @@+-- | This is drop-in replacement for the read, write, and index functions+-- present in @Data.Primitive.ByteArray@ and @Data.Primitive.ByteArray.Unaligned@.+-- While the functions from those modules use native byte order, the functions+-- in this one use big-endian byte order (most significant byte first).+module Data.Primitive.ByteArray.BigEndian+ ( -- * Aligned+ writeByteArray+ , readByteArray+ , indexByteArray+ -- * Unaligned+ , writeUnalignedByteArray+ , readUnalignedByteArray+ , indexUnalignedByteArray+ ) where++import Data.Primitive (Prim,MutableByteArray,ByteArray)+import Control.Monad.Primitive (PrimState,PrimMonad)+import System.ByteOrder (Bytes,toBigEndian,fromBigEndian)+import Data.Primitive.ByteArray.Unaligned (PrimUnaligned)+import qualified Data.Primitive as PM+import qualified Data.Primitive.ByteArray.Unaligned as PMU++-- | Write a primitive value to the byte array. The offset is given+-- in elements of type @a@ rather than in bytes. The most significant+-- byte in the value comes first.+writeByteArray :: (PrimMonad m, Prim a, Bytes a) => MutableByteArray (PrimState m) -> Int -> a -> m ()+writeByteArray arr ix v = PM.writeByteArray arr ix (toBigEndian v)++-- | Read a primitive value from the byte array, interpreting the first+-- byte as the most significant one. The offset is given in elements of+-- type @a@ rather than in bytes.+readByteArray :: (PrimMonad m, Prim a, Bytes a) => MutableByteArray (PrimState m) -> Int -> m a+readByteArray arr ix = fromBigEndian <$> PM.readByteArray arr ix++-- | Read a primitive value from the byte array, interpreting the first+-- byte as the most significant one. The offset is given in elements of+-- type @a@ rather than in bytes.+indexByteArray :: (Prim a, Bytes a) => ByteArray -> Int -> a+indexByteArray arr ix = fromBigEndian (PM.indexByteArray arr ix)++-- | Write a primitive value to the byte array. The offset is given+-- in bytes rather than in elements of type @a@. The most significant+-- byte in the value comes first.+writeUnalignedByteArray :: (PrimMonad m, PrimUnaligned a, Bytes a)+ => MutableByteArray (PrimState m) -> Int -> a -> m ()+writeUnalignedByteArray arr ix v = PMU.writeUnalignedByteArray arr ix (toBigEndian v)++-- | Read a primitive value from the byte array, interpreting the first+-- byte as the most significant one. The offset is given in bytes rather+-- than in elements of type @a@.+readUnalignedByteArray :: (PrimMonad m, PrimUnaligned a, Bytes a)+ => MutableByteArray (PrimState m) -> Int -> m a+readUnalignedByteArray arr ix = fromBigEndian <$> PMU.readUnalignedByteArray arr ix++-- | Read a primitive value from the byte array, interpreting the first+-- byte as the most significant one. The offset is given in bytes rather+-- than in elements of type @a@.+indexUnalignedByteArray :: (PrimUnaligned a, Bytes a)+ => ByteArray -> Int -> a+indexUnalignedByteArray arr ix = fromBigEndian (PMU.indexUnalignedByteArray arr ix)
+ src/Data/Primitive/ByteArray/LittleEndian.hs view
@@ -0,0 +1,62 @@+-- | This is drop-in replacement for the read, write, and index functions+-- present in @Data.Primitive.ByteArray@ and @Data.Primitive.ByteArray.Unaligned@.+-- While the functions from those modules use native byte order, the functions+-- in this one use little-endian byte order (least significant byte first).+module Data.Primitive.ByteArray.LittleEndian+ ( -- * Aligned+ writeByteArray+ , readByteArray+ , indexByteArray+ -- * Unaligned+ , writeUnalignedByteArray+ , readUnalignedByteArray+ , indexUnalignedByteArray+ ) where++import Data.Primitive (Prim,MutableByteArray,ByteArray)+import Control.Monad.Primitive (PrimState,PrimMonad)+import System.ByteOrder (Bytes,toLittleEndian,fromLittleEndian)+import Data.Primitive.ByteArray.Unaligned (PrimUnaligned)+import qualified Data.Primitive as PM+import qualified Data.Primitive.ByteArray.Unaligned as PMU++-- | Write a primitive value to the byte array. The offset is given+-- in elements of type @a@ rather than in bytes. The least significant+-- byte in the value comes first.+writeByteArray :: (PrimMonad m, Prim a, Bytes a)+ => MutableByteArray (PrimState m) -> Int -> a -> m ()+writeByteArray arr ix v = PM.writeByteArray arr ix (toLittleEndian v)++-- | Read a primitive value from the byte array, interpreting the first+-- byte as the least significant one. The offset is given in elements of+-- type @a@ rather than in bytes.+readByteArray :: (PrimMonad m, Prim a, Bytes a)+ => MutableByteArray (PrimState m) -> Int -> m a+readByteArray arr ix = fromLittleEndian <$> PM.readByteArray arr ix++-- | Read a primitive value from the byte array, interpreting the first+-- byte as the least significant one. The offset is given in elements of+-- type @a@ rather than in bytes.+indexByteArray :: (Prim a, Bytes a) => ByteArray -> Int -> a+indexByteArray arr ix = fromLittleEndian (PM.indexByteArray arr ix)++-- | Write a primitive value to the byte array. The offset is given+-- in bytes rather than in elements of type @a@. The least significant+-- byte in the value comes first.+writeUnalignedByteArray :: (PrimMonad m, PrimUnaligned a, Bytes a)+ => MutableByteArray (PrimState m) -> Int -> a -> m ()+writeUnalignedByteArray arr ix v = PMU.writeUnalignedByteArray arr ix (toLittleEndian v)++-- | Read a primitive value from the byte array, interpreting the first+-- byte as the least significant one. The offset is given in bytes rather+-- than in elements of type @a@.+readUnalignedByteArray :: (PrimMonad m, PrimUnaligned a, Bytes a)+ => MutableByteArray (PrimState m) -> Int -> m a+readUnalignedByteArray arr ix = fromLittleEndian <$> PMU.readUnalignedByteArray arr ix++-- | Read a primitive value from the byte array, interpreting the first+-- byte as the least significant one. The offset is given in bytes rather+-- than in elements of type @a@.+indexUnalignedByteArray :: (PrimUnaligned a, Bytes a)+ => ByteArray -> Int -> a+indexUnalignedByteArray arr ix = fromLittleEndian (PMU.indexUnalignedByteArray arr ix)
src/System/ByteOrder.hs view
@@ -1,9 +1,12 @@ {-# language DataKinds #-}+{-# language DerivingStrategies #-}+{-# language GeneralizedNewtypeDeriving #-} {-# language GADTSyntax #-} {-# language KindSignatures #-} {-# language MagicHash #-} {-# language RoleAnnotations #-} {-# language ScopedTypeVariables #-}+{-# language StandaloneDeriving #-} {-# language TypeApplications #-} {-# language UnboxedTuples #-} @@ -48,6 +51,7 @@ ) where import Data.Kind (Type)+import Data.Primitive.ByteArray.Unaligned (PrimUnaligned) import Data.Primitive.Types (Prim) import Foreign.Ptr (Ptr,castPtr) import Foreign.Storable (Storable)@@ -55,6 +59,7 @@ import System.ByteOrder.Class (Bytes(..),FixedOrdering,toFixedEndian) import qualified Data.Primitive.Types as PM+import qualified Data.Primitive.ByteArray.Unaligned as PMU import qualified Foreign.Storable as FS -- | Convert from a big-endian word to a native-endian word.@@ -74,6 +79,13 @@ type role Fixed phantom representational +deriving newtype instance Num a => Num (Fixed b a)+deriving newtype instance Real a => Real (Fixed b a)+deriving newtype instance Integral a => Integral (Fixed b a)+deriving newtype instance Ord a => Ord (Fixed b a)+deriving newtype instance Enum a => Enum (Fixed b a)+deriving newtype instance Eq a => Eq (Fixed b a)+ instance (FixedOrdering b, Prim a, Bytes a) => Prim (Fixed b a) where {-# inline sizeOf# #-} {-# inline alignment# #-}@@ -97,6 +109,15 @@ (# s1, x #) -> (# s1, Fixed (toFixedEndian @b x) #) writeOffAddr# a i (Fixed x) = PM.writeOffAddr# a i (toFixedEndian @b x) setOffAddr# a i n (Fixed x) = PM.setOffAddr# a i n (toFixedEndian @b x)++instance (FixedOrdering b, PrimUnaligned a, Bytes a) => PrimUnaligned (Fixed b a) where+ {-# inline indexUnalignedByteArray# #-}+ {-# inline readUnalignedByteArray# #-}+ {-# inline writeUnalignedByteArray# #-}+ indexUnalignedByteArray# a i = Fixed (toFixedEndian @b (PMU.indexUnalignedByteArray# a i))+ readUnalignedByteArray# a i s0 = case PMU.readUnalignedByteArray# a i s0 of+ (# s1, x #) -> (# s1, Fixed (toFixedEndian @b x) #)+ writeUnalignedByteArray# a i (Fixed x) = PMU.writeUnalignedByteArray# a i (toFixedEndian @b x) instance (FixedOrdering b, Storable a, Bytes a) => Storable (Fixed b a) where {-# inline sizeOf #-}