packages feed

binary-strict 0.2.2 → 0.2.3

raw patch · 3 files changed

+25/−31 lines, 3 files

Files

binary-strict.cabal view
@@ -1,9 +1,10 @@ name:            binary-strict-version:         0.2.2+version:         0.2.3 license:         BSD3 license-file:    LICENSE author:          Lennart Kolmodin <kolmodin@dtek.chalmers.se> maintainer:      Adam Langley <agl@imperialviolet.org>+homepage:        http://darcs.imperialviolet.org/binary-strict description:     This is a strict version of the Get monad from the binary                  package. It's pretty much just a copy and paste job from the                  original source code. The binary team are currently unsure
src/Data/Binary/Strict/BitGet.hs view
@@ -71,7 +71,6 @@  data S = S {-# UNPACK #-} !B.ByteString  -- ^ input            {-# UNPACK #-} !Word8  -- ^ bit offset in current byte-           deriving (Show)  newtype BitGet a = BitGet { unGet :: S -> (Either String a, S) } 
src/Data/Binary/Strict/IncrementalGet.hs view
@@ -24,15 +24,15 @@ -- Take the following example -- -- > testParse = do--- >   getWord16be >>= yield--- >   testParse+-- >   a <- getWord16be+-- >   b <- getWord16be+-- >   return $ a + b -- > -- > test = runGet testParse $ B.pack [1,0,0] ----- Here, @testParse@ never completes, but yields Word16 values forever. It's--- started with a 3 byte ByteString and will yield a single value before--- running out of data. Thus, @test = Partial cont [256]@. Calling @cont@--- with a single extra byte will yield another Word16 value etc.+-- Here @testParse@ needs to read 4 bytes in order to complete, so test is+-- a Partial, which includes the continuation function, so which you can pass+-- more data until it completes -- -- The lookahead functions have been removed from this parser because of their -- incompatibility with the incremental monad at the moment.@@ -52,7 +52,6 @@      -- * Utility     , skip-    , yield     , bytesRead     , remaining     , isEmpty@@ -92,18 +91,17 @@  #ifndef __HADDOCK__ -- | The parse state-data S r = S {-# UNPACK #-} !B.ByteString  -- ^ input-             {-# UNPACK #-} !Int  -- ^ bytes read-             [r]  -- ^ results so far, in reverse order+data S = S {-# UNPACK #-} !B.ByteString  -- ^ input+           {-# UNPACK #-} !Int  -- ^ bytes read #endif  -- | The result of a partial parse data Result a = Failed String                 -- ^ the parse failed with the given error message-              | Finished B.ByteString [a]+              | Finished B.ByteString a                 -- ^ the parse finished and produced the given list of                 --   results doing so. Any unparsed data is returned.-              | Partial (B.ByteString -> Result a) [a]+              | Partial (B.ByteString -> Result a)                 -- ^ the parse ran out of data before finishing, but produced                 --   the given list of results before doing so. To continue the                 --   parse pass more data to the given continuation@@ -111,9 +109,9 @@ instance (Show a) => Show (Result a) where   show (Failed err) = "Failed " ++ err   show (Finished _ rs) = "Finished " ++ show rs-  show (Partial _ rs) = "Partial " ++ show rs+  show (Partial _) = "Partial" -newtype Get r a = Get { unGet :: S r -> (a -> S r -> Result r) -> Result r }+newtype Get r a = Get { unGet :: S -> (a -> S -> Result r) -> Result r }  instance Functor (Get r) where     fmap f m = Get (\s -> \cont -> unGet m s (cont . f))@@ -123,21 +121,17 @@   m >>= k = Get (\s -> \cont -> unGet m s (\a -> \s' -> unGet (k a) s' cont))   fail err = Get (const $ const $ Failed err) -get :: Get r (S r)+get :: Get r S get = Get (\s -> \k -> k s s) --- | Return a value from the parse-yield :: r -> Get r ()-yield v = Get (\(S a b c) -> \cont -> cont () (S a b (v : c)))--initState :: B.ByteString -> S r-initState input = S input 0 []+initState :: B.ByteString -> S+initState input = S input 0 {-# INLINE initState #-}  -- | Start a parser and return the first Result.-runGet :: Get r a -> B.ByteString -> Result r+runGet :: Get r r -> B.ByteString -> Result r runGet m input =-  unGet m (initState input) (const $ \(S s _ rs) -> Finished s $ reverse rs)+  unGet m (initState input) (\v -> (\(S s _) -> Finished s v))  -- | Skip ahead @n@ bytes. Fails if fewer than @n@ bytes are available. skip :: Int -> Get r ()@@ -146,21 +140,21 @@ -- | Get the total number of bytes read to this point. bytesRead :: Get r Int bytesRead = do-  S _ b _ <- get+  S _ b <- get   return b  -- | Get the number of remaining unparsed bytes. -- Useful for checking whether all input has been consumed. remaining :: Get r Int remaining = do-  S s _ _ <- get+  S s _ <- get   return (fromIntegral (B.length s))  -- | Test whether all input has been consumed, -- i.e. there are no remaining unparsed bytes. isEmpty :: Get r Bool isEmpty = do-  S s _ _ <- get+  S s _ <- get   return $ B.null s  ------------------------------------------------------------------------@@ -174,11 +168,11 @@  -- | Pull @n@ bytes from the input, as a strict ByteString. getBytes :: Int -> Get r B.ByteString-getBytes n = Get $ \(S s offset values) -> \cont ->+getBytes n = Get $ \(S s offset) -> \cont ->   if n <= B.length s      then let (consume, rest) = B.splitAt n s-           in cont consume $ S rest (offset + fromIntegral n) values-     else Partial (\s' -> unGet (getBytes n) (S (B.append s s') offset []) cont) $ reverse values+           in cont consume $ S rest (offset + fromIntegral n)+     else Partial (\s' -> unGet (getBytes n) (S (B.append s s') offset) cont) {-# INLINE getBytes #-}  -- Pull n bytes from the input, and apply a parser to those bytes,