packer 0.1.3 → 0.1.4
raw patch · 4 files changed
+78/−7 lines, 4 filesdep +array
Dependencies added: array
Files
- Data/Packer.hs +40/−0
- Data/Packer/Internal.hs +12/−2
- Tests/Tests.hs +24/−4
- packer.cabal +2/−1
Data/Packer.hs view
@@ -45,6 +45,10 @@ , getRemaining , getRemainingCopy , getStorable+ , getFloat32LE+ , getFloat32BE+ , getFloat64LE+ , getFloat64BE , isolate -- * Packing functions , packGetPosition@@ -66,6 +70,10 @@ , putHoleWord64BE , putBytes , putStorable+ , putFloat32LE+ , putFloat32BE+ , putFloat64LE+ , putFloat64BE , fillHole ) where @@ -74,6 +82,7 @@ import Data.Packer.Unsafe import Data.Packer.IO import Data.Packer.Endian+import Data.Packer.IEEE754 import Foreign.Ptr import Foreign.ForeignPtr import Data.ByteString (ByteString)@@ -158,6 +167,22 @@ getWord64BE = unpackCheckAct 8 (peekAnd be64Host . castPtr) {-# INLINE getWord64BE #-} +-- | Read a Float in little endian IEEE-754 format+getFloat32LE :: Unpacking Float+getFloat32LE = wordToFloat <$> getWord32LE++-- | Read a Float in big endian IEEE-754 format+getFloat32BE :: Unpacking Float+getFloat32BE = wordToFloat <$> getWord32BE++-- | Read a Double in little endian IEEE-754 format+getFloat64LE :: Unpacking Double+getFloat64LE = wordToDouble <$> getWord64LE++-- | Read a Double in big endian IEEE-754 format+getFloat64BE :: Unpacking Double+getFloat64BE = wordToDouble <$> getWord64BE+ -- | Get a number of bytes in bytestring format. -- -- The original block of memory is expected to live for the life of this bytestring,@@ -296,6 +321,21 @@ -- | Put a Word64 Hole in little endian putHoleWord64LE = putHoleWord64_ le64Host +-- | Write a Float in little endian IEEE-754 format+putFloat32LE :: Float -> Packing ()+putFloat32LE = putWord32LE . floatToWord++-- | Write a Float in big endian IEEE-754 format+putFloat32BE :: Float -> Packing ()+putFloat32BE = putWord32BE . floatToWord++-- | Write a Double in little endian IEEE-754 format+putFloat64LE :: Double -> Packing ()+putFloat64LE = putWord64LE . doubleToWord++-- | Write a Double in big endian IEEE-754 format+putFloat64BE :: Double -> Packing ()+putFloat64BE = putWord64BE . doubleToWord -- | Put a Bytestring. putBytes :: ByteString -> Packing ()
Data/Packer/Internal.hs view
@@ -41,9 +41,9 @@ import Foreign.ForeignPtr import Data.Data import Data.Word-import Control.Exception (Exception, throwIO)+import Control.Exception (Exception, throwIO, try, SomeException) import Control.Monad.Trans-import Control.Applicative (Applicative(..), (<$>), (<*>))+import Control.Applicative (Alternative(..), Applicative(..), (<$>), (<*>)) import Control.Concurrent.MVar import Control.Monad (when) @@ -68,6 +68,7 @@ pure = returnPacking (<*>) = apPacking +bindPacking :: Packing a -> (a -> Packing b) -> Packing b bindPacking m1 m2 = Packing $ \cst st -> do (a, st2) <- runPacking_ m1 cst st runPacking_ (m2 a) cst st2@@ -102,6 +103,15 @@ pure = returnUnpacking (<*>) = apUnpacking +instance Alternative Unpacking where+ empty = error "Data.Packer (Alternative): empty"+ f <|> g = Unpacking $ \cst st ->+ tryRunUnpacking f cst st >>= either (const $ runUnpacking_ g cst st) return++tryRunUnpacking :: Unpacking a -> (ForeignPtr Word8, Memory) -> Memory -> IO (Either SomeException (a,Memory))+tryRunUnpacking f cst st = try $ runUnpacking_ f cst st++bindUnpacking :: Unpacking a -> (a -> Unpacking b) -> Unpacking b bindUnpacking m1 m2 = Unpacking $ \cst st -> do (a, st2) <- runUnpacking_ m1 cst st runUnpacking_ (m2 a) cst st2
Tests/Tests.hs view
@@ -43,6 +43,10 @@ | W64 Word64 | W64BE Word64 | W64LE Word64+ | F32LE Float+ | F32BE Float+ | F64LE Double+ | F64BE Double | Bytes B.ByteString deriving (Show,Eq) @@ -66,6 +70,10 @@ , W64 <$> arbitrary , W64BE <$> arbitrary , W64LE <$> arbitrary+ , F32LE <$> arbitrary+ , F32LE <$> arbitrary+ , F64BE <$> arbitrary+ , F64BE <$> arbitrary , Bytes <$> arbitraryBS ] @@ -87,6 +95,10 @@ process (W32BE w) = putWord32BE w process (W64BE w) = putWord64BE w process (Bytes b) = putBytes b+ process (F32LE w) = putFloat32LE w+ process (F32BE w) = putFloat32BE w+ process (F64LE w) = putFloat64LE w+ process (F64BE w) = putFloat64BE w sumLen a (W8 _) = a + 1 sumLen a (W16 _) = a + 2@@ -95,6 +107,10 @@ sumLen a (W32 _) = a + 4 sumLen a (W32LE _) = a + 4 sumLen a (W32BE _) = a + 4+ sumLen a (F32LE _) = a + 4+ sumLen a (F32BE _) = a + 4+ sumLen a (F64LE _) = a + 8+ sumLen a (F64BE _) = a + 8 sumLen a (W64 _) = a + 8 sumLen a (W64LE _) = a + 8 sumLen a (W64BE _) = a + 8@@ -103,16 +119,20 @@ unpackDataStream :: DataStream -> B.ByteString -> DataStream unpackDataStream (DataStream atoms) bs = DataStream $ runUnpacking (mapM process atoms) bs where process :: DataAtom -> Unpacking DataAtom- process (W8 _) = W8 <$> getWord8- process (W16 _) = W16 <$> getWord16- process (W32 _) = W32 <$> getWord32- process (W64 _) = W64 <$> getWord64+ process (W8 _) = W8 <$> getWord8+ process (W16 _) = W16 <$> getWord16+ process (W32 _) = W32 <$> getWord32+ process (W64 _) = W64 <$> getWord64 process (W16LE _) = W16LE <$> getWord16LE process (W32LE _) = W32LE <$> getWord32LE process (W64LE _) = W64LE <$> getWord64LE process (W16BE _) = W16BE <$> getWord16BE process (W32BE _) = W32BE <$> getWord32BE process (W64BE _) = W64BE <$> getWord64BE+ process (F32LE _) = F32LE <$> getFloat32LE+ process (F32BE _) = F32BE <$> getFloat32BE+ process (F64LE _) = F64LE <$> getFloat64LE+ process (F64BE _) = F64BE <$> getFloat64BE process (Bytes b) = Bytes <$> getBytes (B.length b) assertException msg filterE act =
packer.cabal view
@@ -1,5 +1,5 @@ Name: packer-Version: 0.1.3+Version: 0.1.4 Description: Fast byte serializer and unserializer License: BSD3 License-file: LICENSE@@ -16,6 +16,7 @@ Library Build-Depends: base >= 3 && < 5 , bytestring+ , array , mtl Exposed-modules: Data.Packer Data.Packer.Unsafe