Skip to content

add runGetIncremental‘ #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Data/Binary/Get.hs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ module Data.Binary.Get (
-- $incrementalinterface
, Decoder(..)
, runGetIncremental
, runGetIncremental'

-- ** Providing input
, pushChunk
Expand Down Expand Up @@ -228,7 +229,7 @@ import qualified Data.ByteString.Unsafe as B
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Internal as L

import Data.Binary.Get.Internal hiding ( Decoder(..), runGetIncremental )
import Data.Binary.Get.Internal hiding ( Decoder(..), runGetIncremental, runGetIncremental')
import qualified Data.Binary.Get.Internal as I

#if defined(__GLASGOW_HASKELL__) && !defined(__HADDOCK__)
Expand Down Expand Up @@ -284,6 +285,11 @@ data Decoder a = Fail !B.ByteString {-# UNPACK #-} !ByteOffset String
runGetIncremental :: Get a -> Decoder a
runGetIncremental = calculateOffset . I.runGetIncremental

-- | Similar to 'runGetIncremental', but accept an initial chunk, it's faster
-- than feeding initial chunk after 'runGetIncremental'.
runGetIncremental' :: Get a -> B.ByteString -> Decoder a
runGetIncremental' g = calculateOffset . I.runGetIncremental' g

calculateOffset :: I.Decoder a -> Decoder a
calculateOffset r0 = go r0 0
where
Expand Down
9 changes: 7 additions & 2 deletions src/Data/Binary/Get/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Data.Binary.Get.Internal (
, runCont
, Decoder(..)
, runGetIncremental
, runGetIncremental'

, readN
, readNWith
Expand Down Expand Up @@ -145,8 +146,12 @@ instance (Show a) => Show (Decoder a) where
-- | Run a 'Get' monad. See 'Decoder' for what to do next, like providing
-- input, handling decoding errors and to get the output value.
runGetIncremental :: Get a -> Decoder a
runGetIncremental g = noMeansNo $
runCont g B.empty (\i a -> Done i a)
runGetIncremental g = noMeansNo $ runCont g B.empty Done

-- | Similar to 'runGetIncremental', but accept an initial chunk, it's faster
-- than feeding initial chunk after 'runGetIncremental'.
runGetIncremental' :: Get a -> B.ByteString -> Decoder a
runGetIncremental' g bs = noMeansNo $ runCont g bs Done

-- | Make sure we don't have to pass Nothing to a Partial twice.
-- This way we don't need to pass around an EOF value in the Get monad, it
Expand Down