{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE BangPatterns #-}
#if defined(__GLASGOW_HASKELL__) && !defined(__HADDOCK__)
#include "MachDeps.h"
#endif
module Data.Serialize.Get (
Get
, runGet
, runGetLazy
, runGetState
, runGetLazyState
, Result(..)
, runGetPartial
, runGetChunk
, ensure
, isolate
, label
, skip
, uncheckedSkip
, lookAhead
, lookAheadM
, lookAheadE
, uncheckedLookAhead
, bytesRead
, getBytes
, remaining
, isEmpty
, getWord8
, getInt8
, getByteString
, getLazyByteString
, getShortByteString
, getWord16be
, getWord32be
, getWord64be
, getInt16be
, getInt32be
, getInt64be
, getWord16le
, getWord32le
, getWord64le
, getInt16le
, getInt32le
, getInt64le
, getWordhost
, getWord16host
, getWord32host
, getWord64host
, getTwoOf
, getListOf
, getIArrayOf
, getTreeOf
, getSeqOf
, getMapOf
, getIntMapOf
, getSetOf
, getIntSetOf
, getMaybeOf
, getEitherOf
, getNested
) where
import qualified Control.Applicative as A
import qualified Control.Monad as M
import Control.Monad (unless)
import qualified Control.Monad.Fail as Fail
import Data.Array.IArray (IArray,listArray)
import Data.Ix (Ix)
import Data.List (intercalate)
import Data.Maybe (isNothing,fromMaybe)
import Foreign
import System.IO.Unsafe (unsafeDupablePerformIO)
import qualified Data.ByteString as B
import qualified Data.ByteString.Internal as B
import qualified Data.ByteString.Unsafe as B
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Short as BS
import qualified Data.IntMap as IntMap
import qualified Data.IntSet as IntSet
import qualified Data.Map as Map
import qualified Data.Sequence as Seq
import qualified Data.Set as Set
import qualified Data.Tree as T
#if defined(__GLASGOW_HASKELL__) && !defined(__HADDOCK__)
import GHC.Base
import GHC.Word
#endif
data Result r = Fail String B.ByteString
| Partial (B.ByteString -> Result r)
| Done r B.ByteString
instance Show r => Show (Result r) where
show :: Result r -> String
show (Fail String
msg ByteString
_) = String
"Fail " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ShowS
forall a. Show a => a -> String
show String
msg
show (Partial ByteString -> Result r
_) = String
"Partial _"
show (Done r
r ByteString
bs) = String
"Done " String -> ShowS
forall a. [a] -> [a] -> [a]
++ r -> String
forall a. Show a => a -> String
show r
r String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ByteString -> String
forall a. Show a => a -> String
show ByteString
bs
instance Functor Result where
fmap :: forall a b. (a -> b) -> Result a -> Result b
fmap a -> b
_ (Fail String
msg ByteString
rest) = String -> ByteString -> Result b
forall r. String -> ByteString -> Result r
Fail String
msg ByteString
rest
fmap a -> b
f (Partial ByteString -> Result a
k) = (ByteString -> Result b) -> Result b
forall r. (ByteString -> Result r) -> Result r
Partial ((a -> b) -> Result a -> Result b
forall a b. (a -> b) -> Result a -> Result b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f (Result a -> Result b)
-> (ByteString -> Result a) -> ByteString -> Result b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Result a
k)
fmap a -> b
f (Done a
r ByteString
bs) = b -> ByteString -> Result b
forall r. r -> ByteString -> Result r
Done (a -> b
f a
r) ByteString
bs
newtype Get a = Get
{ forall a.
Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet :: forall r. Input -> Buffer -> More
-> Int -> Failure r
-> Success a r -> Result r }
type Input = B.ByteString
type Buffer = Maybe B.ByteString
emptyBuffer :: Buffer
emptyBuffer :: Buffer
emptyBuffer = ByteString -> Buffer
forall a. a -> Maybe a
Just ByteString
B.empty
extendBuffer :: Buffer -> B.ByteString -> Buffer
extendBuffer :: Buffer -> ByteString -> Buffer
extendBuffer Buffer
buf ByteString
chunk =
do bs <- Buffer
buf
return $! bs `B.append` chunk
{-# INLINE extendBuffer #-}
append :: Buffer -> Buffer -> Buffer
append :: Buffer -> Buffer -> Buffer
append Buffer
l Buffer
r = ByteString -> ByteString -> ByteString
B.append (ByteString -> ByteString -> ByteString)
-> Buffer -> Maybe (ByteString -> ByteString)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Buffer
l Maybe (ByteString -> ByteString) -> Buffer -> Buffer
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
A.<*> Buffer
r
{-# INLINE append #-}
bufferBytes :: Buffer -> B.ByteString
bufferBytes :: Buffer -> ByteString
bufferBytes = ByteString -> Buffer -> ByteString
forall a. a -> Maybe a -> a
fromMaybe ByteString
B.empty
{-# INLINE bufferBytes #-}
type Failure r = Input -> Buffer -> More -> [String] -> String -> Result r
type Success a r = Input -> Buffer -> More -> Int -> a -> Result r
data More
= Complete
| Incomplete (Maybe Int)
deriving (More -> More -> Bool
(More -> More -> Bool) -> (More -> More -> Bool) -> Eq More
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: More -> More -> Bool
== :: More -> More -> Bool
$c/= :: More -> More -> Bool
/= :: More -> More -> Bool
Eq)
moreLength :: More -> Int
moreLength :: More -> Int
moreLength More
m = case More
m of
More
Complete -> Int
0
Incomplete Maybe Int
mb -> Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe Int
0 Maybe Int
mb
instance Functor Get where
fmap :: forall a b. (a -> b) -> Get a -> Get b
fmap a -> b
p Get a
m = (forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
-> Get b
forall a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get ((forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
-> Get b)
-> (forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
-> Get b
forall a b. (a -> b) -> a -> b
$ \ ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf Success b r
ks ->
Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
forall a.
Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
m ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf (Success a r -> Result r) -> Success a r -> Result r
forall a b. (a -> b) -> a -> b
$ \ ByteString
s1 Buffer
b1 More
m1 Int
w1 a
a -> Success b r
ks ByteString
s1 Buffer
b1 More
m1 Int
w1 (a -> b
p a
a)
instance A.Applicative Get where
pure :: forall a. a -> Get a
pure a
a = (forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
forall a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get ((forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a)
-> (forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
forall a b. (a -> b) -> a -> b
$ \ ByteString
s0 Buffer
b0 More
m0 Int
w Failure r
_ Success a r
ks -> Success a r
ks ByteString
s0 Buffer
b0 More
m0 Int
w a
a
{-# INLINE pure #-}
Get (a -> b)
f <*> :: forall a b. Get (a -> b) -> Get a -> Get b
<*> Get a
x = (forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
-> Get b
forall a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get ((forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
-> Get b)
-> (forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
-> Get b
forall a b. (a -> b) -> a -> b
$ \ ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf Success b r
ks ->
Get (a -> b)
-> forall r.
ByteString
-> Buffer
-> More
-> Int
-> Failure r
-> Success (a -> b) r
-> Result r
forall a.
Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get (a -> b)
f ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf (Success (a -> b) r -> Result r) -> Success (a -> b) r -> Result r
forall a b. (a -> b) -> a -> b
$ \ ByteString
s1 Buffer
b1 More
m1 Int
w1 a -> b
g ->
Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
forall a.
Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
x ByteString
s1 Buffer
b1 More
m1 Int
w1 Failure r
kf (Success a r -> Result r) -> Success a r -> Result r
forall a b. (a -> b) -> a -> b
$ \ ByteString
s2 Buffer
b2 More
m2 Int
w2 a
y -> Success b r
ks ByteString
s2 Buffer
b2 More
m2 Int
w2 (a -> b
g a
y)
{-# INLINE (<*>) #-}
Get a
m *> :: forall a b. Get a -> Get b -> Get b
*> Get b
k = (forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
-> Get b
forall a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get ((forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
-> Get b)
-> (forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
-> Get b
forall a b. (a -> b) -> a -> b
$ \ ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf Success b r
ks ->
Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
forall a.
Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
m ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf (Success a r -> Result r) -> Success a r -> Result r
forall a b. (a -> b) -> a -> b
$ \ ByteString
s1 Buffer
b1 More
m1 Int
w1 a
_ -> Get b
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success b r -> Result r
forall a.
Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get b
k ByteString
s1 Buffer
b1 More
m1 Int
w1 Failure r
kf Success b r
ks
{-# INLINE (*>) #-}
instance A.Alternative Get where
empty :: forall a. Get a
empty = String -> Get a
forall a. String -> Get a
failDesc String
"empty"
{-# INLINE empty #-}
<|> :: forall a. Get a -> Get a -> Get a
(<|>) = Get a -> Get a -> Get a
forall a. Get a -> Get a -> Get a
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
M.mplus
{-# INLINE (<|>) #-}
instance Monad Get where
return :: forall a. a -> Get a
return = a -> Get a
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
A.pure
{-# INLINE return #-}
Get a
m >>= :: forall a b. Get a -> (a -> Get b) -> Get b
>>= a -> Get b
g = (forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
-> Get b
forall a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get ((forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
-> Get b)
-> (forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success b r -> Result r)
-> Get b
forall a b. (a -> b) -> a -> b
$ \ ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf Success b r
ks ->
Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
forall a.
Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
m ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf (Success a r -> Result r) -> Success a r -> Result r
forall a b. (a -> b) -> a -> b
$ \ ByteString
s1 Buffer
b1 More
m1 Int
w1 a
a -> Get b
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success b r -> Result r
forall a.
Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet (a -> Get b
g a
a) ByteString
s1 Buffer
b1 More
m1 Int
w1 Failure r
kf Success b r
ks
{-# INLINE (>>=) #-}
>> :: forall a b. Get a -> Get b -> Get b
(>>) = Get a -> Get b -> Get b
forall a b. Get a -> Get b -> Get b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(A.*>)
{-# INLINE (>>) #-}
#if !(MIN_VERSION_base(4,13,0))
fail = Fail.fail
{-# INLINE fail #-}
#endif
instance Fail.MonadFail Get where
fail :: forall a. String -> Get a
fail = String -> Get a
forall a. String -> Get a
failDesc
{-# INLINE fail #-}
instance M.MonadPlus Get where
mzero :: forall a. Get a
mzero = String -> Get a
forall a. String -> Get a
failDesc String
"mzero"
{-# INLINE mzero #-}
mplus :: forall a. Get a -> Get a -> Get a
mplus Get a
a Get a
b =
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
forall a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get ((forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a)
-> (forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
forall a b. (a -> b) -> a -> b
$ \ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf Success a r
ks ->
let ks' :: Success a r
ks' ByteString
s1 Buffer
b1 = Success a r
ks ByteString
s1 (Buffer
b0 Buffer -> Buffer -> Buffer
`append` Buffer
b1)
kf' :: p -> Buffer -> More -> [String] -> String -> Result r
kf' p
_ Buffer
b1 More
m1 = Failure r
kf (ByteString
s0 ByteString -> ByteString -> ByteString
`B.append` Buffer -> ByteString
bufferBytes Buffer
b1)
(Buffer
b0 Buffer -> Buffer -> Buffer
`append` Buffer
b1) More
m1
try :: p -> Buffer -> More -> p -> p -> Result r
try p
_ Buffer
b1 More
m1 p
_ p
_ = Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
forall a.
Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
b (ByteString
s0 ByteString -> ByteString -> ByteString
`B.append` Buffer -> ByteString
bufferBytes Buffer
b1)
Buffer
b1 More
m1 Int
w0 Failure r
forall {p}. p -> Buffer -> More -> [String] -> String -> Result r
kf' Success a r
ks'
in Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
forall a.
Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
a ByteString
s0 Buffer
emptyBuffer More
m0 Int
w0 Failure r
forall {p} {p} {p}. p -> Buffer -> More -> p -> p -> Result r
try Success a r
ks'
{-# INLINE mplus #-}
formatTrace :: [String] -> String
formatTrace :: [String] -> String
formatTrace [] = String
"Empty call stack"
formatTrace [String]
ls = String
"From:\t" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\n\t" [String]
ls String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"\n"
get :: Get B.ByteString
get :: Get ByteString
get = (forall r.
ByteString
-> Buffer
-> More
-> Int
-> Failure r
-> Success ByteString r
-> Result r)
-> Get ByteString
forall a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get (\ByteString
s0 Buffer
b0 More
m0 Int
w Failure r
_ Success ByteString r
k -> Success ByteString r
k ByteString
s0 Buffer
b0 More
m0 Int
w ByteString
s0)
{-# INLINE get #-}
put :: B.ByteString -> Int -> Get ()
put :: ByteString -> Int -> Get ()
put ByteString
s !Int
w = (forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success () r -> Result r)
-> Get ()
forall a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get (\ByteString
_ Buffer
b0 More
m Int
_ Failure r
_ Success () r
k -> Success () r
k ByteString
s Buffer
b0 More
m Int
w ())
{-# INLINE put #-}
label :: String -> Get a -> Get a
label :: forall a. String -> Get a -> Get a
label String
l Get a
m =
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
forall a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get ((forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a)
-> (forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
forall a b. (a -> b) -> a -> b
$ \ ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf Success a r
ks ->
let kf' :: Failure r
kf' ByteString
s1 Buffer
b1 More
m1 [String]
ls = Failure r
kf ByteString
s1 Buffer
b1 More
m1 (String
lString -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
ls)
in Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
forall a.
Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
m ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf' Success a r
ks
finalK :: Success a a
finalK :: forall a. Success a a
finalK ByteString
s Buffer
_ More
_ Int
_ a
a = a -> ByteString -> Result a
forall r. r -> ByteString -> Result r
Done a
a ByteString
s
failK :: Failure a
failK :: forall a. Failure a
failK ByteString
s Buffer
b More
_ [String]
ls String
msg =
String -> ByteString -> Result a
forall r. String -> ByteString -> Result r
Fail ([String] -> String
unlines [String
msg, [String] -> String
formatTrace [String]
ls]) (ByteString
s ByteString -> ByteString -> ByteString
`B.append` Buffer -> ByteString
bufferBytes Buffer
b)
runGet :: Get a -> B.ByteString -> Either String a
runGet :: forall a. Get a -> ByteString -> Either String a
runGet Get a
m ByteString
str =
case Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
forall a.
Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
m ByteString
str Buffer
forall a. Maybe a
Nothing More
Complete Int
0 Failure a
forall a. Failure a
failK Success a a
forall a. Success a a
finalK of
Fail String
i ByteString
_ -> String -> Either String a
forall a b. a -> Either a b
Left String
i
Done a
a ByteString
_ -> a -> Either String a
forall a b. b -> Either a b
Right a
a
Partial{} -> String -> Either String a
forall a b. a -> Either a b
Left String
"Failed reading: Internal error: unexpected Partial."
{-# INLINE runGet #-}
runGetChunk :: Get a -> Maybe Int -> B.ByteString -> Result a
runGetChunk :: forall a. Get a -> Maybe Int -> ByteString -> Result a
runGetChunk Get a
m Maybe Int
mbLen ByteString
str = Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
forall a.
Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
m ByteString
str Buffer
forall a. Maybe a
Nothing (Maybe Int -> More
Incomplete Maybe Int
mbLen) Int
0 Failure a
forall a. Failure a
failK Success a a
forall a. Success a a
finalK
{-# INLINE runGetChunk #-}
runGetPartial :: Get a -> B.ByteString -> Result a
runGetPartial :: forall a. Get a -> ByteString -> Result a
runGetPartial Get a
m = Get a -> Maybe Int -> ByteString -> Result a
forall a. Get a -> Maybe Int -> ByteString -> Result a
runGetChunk Get a
m Maybe Int
forall a. Maybe a
Nothing
{-# INLINE runGetPartial #-}
runGetState :: Get a -> B.ByteString -> Int
-> Either String (a, B.ByteString)
runGetState :: forall a.
Get a -> ByteString -> Int -> Either String (a, ByteString)
runGetState Get a
m ByteString
str Int
off = case Get a -> ByteString -> Int -> (Either String a, ByteString)
forall a.
Get a -> ByteString -> Int -> (Either String a, ByteString)
runGetState' Get a
m ByteString
str Int
off of
(Right a
a,ByteString
bs) -> (a, ByteString) -> Either String (a, ByteString)
forall a b. b -> Either a b
Right (a
a,ByteString
bs)
(Left String
i,ByteString
_) -> String -> Either String (a, ByteString)
forall a b. a -> Either a b
Left String
i
{-# INLINE runGetState #-}
runGetState' :: Get a -> B.ByteString -> Int
-> (Either String a, B.ByteString)
runGetState' :: forall a.
Get a -> ByteString -> Int -> (Either String a, ByteString)
runGetState' Get a
m ByteString
str Int
off =
case Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
forall a.
Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
m (Int -> ByteString -> ByteString
B.drop Int
off ByteString
str) Buffer
forall a. Maybe a
Nothing More
Complete Int
0 Failure a
forall a. Failure a
failK Success a a
forall a. Success a a
finalK of
Fail String
i ByteString
bs -> (String -> Either String a
forall a b. a -> Either a b
Left String
i,ByteString
bs)
Done a
a ByteString
bs -> (a -> Either String a
forall a b. b -> Either a b
Right a
a, ByteString
bs)
Partial{} -> (String -> Either String a
forall a b. a -> Either a b
Left String
"Failed reading: Internal error: unexpected Partial.",ByteString
B.empty)
{-# INLINE runGetState' #-}
runGetLazy' :: Get a -> L.ByteString -> (Either String a,L.ByteString)
runGetLazy' :: forall a. Get a -> ByteString -> (Either String a, ByteString)
runGetLazy' Get a
m ByteString
lstr =
case ByteString -> [ByteString]
L.toChunks ByteString
lstr of
[ByteString
c] -> (Either String a, ByteString) -> (Either String a, ByteString)
forall {a}. (a, ByteString) -> (a, ByteString)
wrapStrict (Get a -> ByteString -> Int -> (Either String a, ByteString)
forall a.
Get a -> ByteString -> Int -> (Either String a, ByteString)
runGetState' Get a
m ByteString
c Int
0)
[] -> (Either String a, ByteString) -> (Either String a, ByteString)
forall {a}. (a, ByteString) -> (a, ByteString)
wrapStrict (Get a -> ByteString -> Int -> (Either String a, ByteString)
forall a.
Get a -> ByteString -> Int -> (Either String a, ByteString)
runGetState' Get a
m ByteString
B.empty Int
0)
ByteString
c:[ByteString]
cs -> Result a -> [ByteString] -> (Either String a, ByteString)
forall {b}.
Result b -> [ByteString] -> (Either String b, ByteString)
loop (Get a -> Maybe Int -> ByteString -> Result a
forall a. Get a -> Maybe Int -> ByteString -> Result a
runGetChunk Get a
m (Int -> Maybe Int
forall a. a -> Maybe a
Just (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
B.length ByteString
c)) ByteString
c) [ByteString]
cs
where
len :: Int
len = Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int64
L.length ByteString
lstr)
wrapStrict :: (a, ByteString) -> (a, ByteString)
wrapStrict (a
e,ByteString
s) = (a
e,[ByteString] -> ByteString
L.fromChunks [ByteString
s])
loop :: Result b -> [ByteString] -> (Either String b, ByteString)
loop Result b
result [ByteString]
chunks = case Result b
result of
Fail String
str ByteString
rest -> (String -> Either String b
forall a b. a -> Either a b
Left String
str, [ByteString] -> ByteString
L.fromChunks (ByteString
rest ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
chunks))
Partial ByteString -> Result b
k -> case [ByteString]
chunks of
ByteString
c:[ByteString]
cs -> Result b -> [ByteString] -> (Either String b, ByteString)
loop (ByteString -> Result b
k ByteString
c) [ByteString]
cs
[] -> Result b -> [ByteString] -> (Either String b, ByteString)
loop (ByteString -> Result b
k ByteString
B.empty) []
Done b
r ByteString
rest -> (b -> Either String b
forall a b. b -> Either a b
Right b
r, [ByteString] -> ByteString
L.fromChunks (ByteString
rest ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
chunks))
{-# INLINE runGetLazy' #-}
runGetLazy :: Get a -> L.ByteString -> Either String a
runGetLazy :: forall a. Get a -> ByteString -> Either String a
runGetLazy Get a
m ByteString
lstr = (Either String a, ByteString) -> Either String a
forall a b. (a, b) -> a
fst (Get a -> ByteString -> (Either String a, ByteString)
forall a. Get a -> ByteString -> (Either String a, ByteString)
runGetLazy' Get a
m ByteString
lstr)
{-# INLINE runGetLazy #-}
runGetLazyState :: Get a -> L.ByteString -> Either String (a,L.ByteString)
runGetLazyState :: forall a. Get a -> ByteString -> Either String (a, ByteString)
runGetLazyState Get a
m ByteString
lstr = case Get a -> ByteString -> (Either String a, ByteString)
forall a. Get a -> ByteString -> (Either String a, ByteString)
runGetLazy' Get a
m ByteString
lstr of
(Right a
a,ByteString
rest) -> (a, ByteString) -> Either String (a, ByteString)
forall a b. b -> Either a b
Right (a
a,ByteString
rest)
(Left String
err,ByteString
_) -> String -> Either String (a, ByteString)
forall a b. a -> Either a b
Left String
err
{-# INLINE runGetLazyState #-}
{-# INLINE ensure #-}
ensure :: Int -> Get B.ByteString
ensure :: Int -> Get ByteString
ensure Int
n0 = Int
n0 Int -> Get ByteString -> Get ByteString
forall a b. a -> b -> b
`seq` (forall r.
ByteString
-> Buffer
-> More
-> Int
-> Failure r
-> Success ByteString r
-> Result r)
-> Get ByteString
forall a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get ((forall r.
ByteString
-> Buffer
-> More
-> Int
-> Failure r
-> Success ByteString r
-> Result r)
-> Get ByteString)
-> (forall r.
ByteString
-> Buffer
-> More
-> Int
-> Failure r
-> Success ByteString r
-> Result r)
-> Get ByteString
forall a b. (a -> b) -> a -> b
$ \ ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf Success ByteString r
ks -> let
n' :: Int
n' = Int
n0 Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
B.length ByteString
s0
in if Int
n' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0
then Success ByteString r
ks ByteString
s0 Buffer
b0 More
m0 Int
w0 ByteString
s0
else Int
-> ByteString
-> [ByteString]
-> Buffer
-> More
-> Int
-> Failure r
-> Success ByteString r
-> Result r
forall {t} {r}.
Int
-> ByteString
-> [ByteString]
-> Buffer
-> More
-> t
-> (ByteString -> Buffer -> More -> [String] -> String -> Result r)
-> (ByteString -> Buffer -> More -> t -> ByteString -> Result r)
-> Result r
getMore Int
n' ByteString
s0 [] Buffer
b0 More
m0 Int
w0 Failure r
kf Success ByteString r
ks
where
finalInput :: ByteString -> [ByteString] -> ByteString
finalInput ByteString
s0 [ByteString]
ss = [ByteString] -> ByteString
B.concat ([ByteString] -> [ByteString]
forall a. [a] -> [a]
reverse (ByteString
s0 ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
ss))
finalBuffer :: Buffer -> ByteString -> [ByteString] -> Buffer
finalBuffer Buffer
b0 ByteString
s0 [ByteString]
ss = Buffer -> ByteString -> Buffer
extendBuffer Buffer
b0 ([ByteString] -> ByteString
B.concat ([ByteString] -> [ByteString]
forall a. [a] -> [a]
reverse ([ByteString] -> [ByteString]
forall a. HasCallStack => [a] -> [a]
init (ByteString
s0 ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
ss))))
getMore :: Int
-> ByteString
-> [ByteString]
-> Buffer
-> More
-> t
-> (ByteString -> Buffer -> More -> [String] -> String -> Result r)
-> (ByteString -> Buffer -> More -> t -> ByteString -> Result r)
-> Result r
getMore !Int
n ByteString
s0 [ByteString]
ss Buffer
b0 More
m0 t
w0 ByteString -> Buffer -> More -> [String] -> String -> Result r
kf ByteString -> Buffer -> More -> t -> ByteString -> Result r
ks = let
tooFewBytes :: Result r
tooFewBytes = let
!s :: ByteString
s = ByteString -> [ByteString] -> ByteString
finalInput ByteString
s0 [ByteString]
ss
!b :: Buffer
b = Buffer -> ByteString -> [ByteString] -> Buffer
finalBuffer Buffer
b0 ByteString
s0 [ByteString]
ss
in ByteString -> Buffer -> More -> [String] -> String -> Result r
kf ByteString
s Buffer
b More
m0 [String
"demandInput"] String
"too few bytes"
in case More
m0 of
More
Complete -> Result r
tooFewBytes
Incomplete Maybe Int
mb -> (ByteString -> Result r) -> Result r
forall r. (ByteString -> Result r) -> Result r
Partial ((ByteString -> Result r) -> Result r)
-> (ByteString -> Result r) -> Result r
forall a b. (a -> b) -> a -> b
$ \ByteString
s ->
if ByteString -> Bool
B.null ByteString
s
then Result r
tooFewBytes
else let
!mb' :: Maybe Int
mb' = case Maybe Int
mb of
Just Int
l -> Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> Int -> Maybe Int
forall a b. (a -> b) -> a -> b
$! Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
B.length ByteString
s
Maybe Int
Nothing -> Maybe Int
forall a. Maybe a
Nothing
in Int
-> ByteString
-> [ByteString]
-> Buffer
-> More
-> t
-> (ByteString -> Buffer -> More -> [String] -> String -> Result r)
-> (ByteString -> Buffer -> More -> t -> ByteString -> Result r)
-> Result r
checkIfEnough Int
n ByteString
s (ByteString
s0 ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
ss) Buffer
b0 (Maybe Int -> More
Incomplete Maybe Int
mb') t
w0 ByteString -> Buffer -> More -> [String] -> String -> Result r
kf ByteString -> Buffer -> More -> t -> ByteString -> Result r
ks
checkIfEnough :: Int
-> ByteString
-> [ByteString]
-> Buffer
-> More
-> t
-> (ByteString -> Buffer -> More -> [String] -> String -> Result r)
-> (ByteString -> Buffer -> More -> t -> ByteString -> Result r)
-> Result r
checkIfEnough !Int
n ByteString
s0 [ByteString]
ss Buffer
b0 More
m0 t
w0 ByteString -> Buffer -> More -> [String] -> String -> Result r
kf ByteString -> Buffer -> More -> t -> ByteString -> Result r
ks = let
n' :: Int
n' = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
B.length ByteString
s0
in if Int
n' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0
then let
!s :: ByteString
s = ByteString -> [ByteString] -> ByteString
finalInput ByteString
s0 [ByteString]
ss
!b :: Buffer
b = Buffer -> ByteString -> [ByteString] -> Buffer
finalBuffer Buffer
b0 ByteString
s0 [ByteString]
ss
in ByteString -> Buffer -> More -> t -> ByteString -> Result r
ks ByteString
s Buffer
b More
m0 t
w0 ByteString
s
else Int
-> ByteString
-> [ByteString]
-> Buffer
-> More
-> t
-> (ByteString -> Buffer -> More -> [String] -> String -> Result r)
-> (ByteString -> Buffer -> More -> t -> ByteString -> Result r)
-> Result r
getMore Int
n' ByteString
s0 [ByteString]
ss Buffer
b0 More
m0 t
w0 ByteString -> Buffer -> More -> [String] -> String -> Result r
kf ByteString -> Buffer -> More -> t -> ByteString -> Result r
ks
isolate :: Int -> Get a -> Get a
isolate :: forall a. Int -> Get a -> Get a
isolate Int
n Get a
m = do
Bool -> Get () -> Get ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
M.when (Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0) (String -> Get ()
forall a. String -> Get a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Attempted to isolate a negative number of bytes")
s <- Int -> Get ByteString
ensure Int
n
let (s',rest) = B.splitAt n s
cur <- bytesRead
put s' cur
a <- m
used <- get
unless (B.null used) (fail "not all bytes parsed in isolate")
put rest (cur + n)
return a
failDesc :: String -> Get a
failDesc :: forall a. String -> Get a
failDesc String
err = do
let msg :: String
msg = String
"Failed reading: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
err
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
forall a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get (\ByteString
s0 Buffer
b0 More
m0 Int
_ Failure r
kf Success a r
_ -> Failure r
kf ByteString
s0 Buffer
b0 More
m0 [] String
msg)
skip :: Int -> Get ()
skip :: Int -> Get ()
skip Int
n = do
s <- Int -> Get ByteString
ensure Int
n
cur <- bytesRead
put (B.drop n s) (cur + n)
uncheckedSkip :: Int -> Get ()
uncheckedSkip :: Int -> Get ()
uncheckedSkip Int
n = do
s <- Get ByteString
get
cur <- bytesRead
put (B.drop n s) (cur + n)
lookAhead :: Get a -> Get a
lookAhead :: forall a. Get a -> Get a
lookAhead Get a
ga = (forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
forall a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get ((forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a)
-> (forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
forall a b. (a -> b) -> a -> b
$ \ ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf Success a r
ks ->
let ks' :: p -> Buffer -> More -> Int -> a -> Result r
ks' p
_ Buffer
b1 = Success a r
ks (ByteString
s0 ByteString -> ByteString -> ByteString
`B.append` Buffer -> ByteString
bufferBytes Buffer
b1) (Buffer
b0 Buffer -> Buffer -> Buffer
`append` Buffer
b1)
kf' :: p -> Buffer -> More -> [String] -> String -> Result r
kf' p
_ Buffer
b1 = Failure r
kf ByteString
s0 (Buffer
b0 Buffer -> Buffer -> Buffer
`append` Buffer
b1)
in Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
forall a.
Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
ga ByteString
s0 Buffer
emptyBuffer More
m0 Int
w0 Failure r
forall {p}. p -> Buffer -> More -> [String] -> String -> Result r
kf' Success a r
forall {p}. p -> Buffer -> More -> Int -> a -> Result r
ks'
lookAheadM :: Get (Maybe a) -> Get (Maybe a)
lookAheadM :: forall a. Get (Maybe a) -> Get (Maybe a)
lookAheadM Get (Maybe a)
gma = do
s <- Get ByteString
get
pre <- bytesRead
ma <- gma
M.when (isNothing ma) (put s pre)
return ma
lookAheadE :: Get (Either a b) -> Get (Either a b)
lookAheadE :: forall a b. Get (Either a b) -> Get (Either a b)
lookAheadE Get (Either a b)
gea = do
s <- Get ByteString
get
pre <- bytesRead
ea <- gea
case ea of
Left a
_ -> ByteString -> Int -> Get ()
put ByteString
s Int
pre
Either a b
_ -> () -> Get ()
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
return ea
uncheckedLookAhead :: Int -> Get B.ByteString
uncheckedLookAhead :: Int -> Get ByteString
uncheckedLookAhead Int
n = do
s <- Get ByteString
get
return (B.take n s)
remaining :: Get Int
remaining :: Get Int
remaining = (forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success Int r -> Result r)
-> Get Int
forall a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get (\ ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
_ Success Int r
ks -> Success Int r
ks ByteString
s0 Buffer
b0 More
m0 Int
w0 (ByteString -> Int
B.length ByteString
s0 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ More -> Int
moreLength More
m0))
isEmpty :: Get Bool
isEmpty :: Get Bool
isEmpty = (forall r.
ByteString
-> Buffer
-> More
-> Int
-> Failure r
-> Success Bool r
-> Result r)
-> Get Bool
forall a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get (\ ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
_ Success Bool r
ks -> Success Bool r
ks ByteString
s0 Buffer
b0 More
m0 Int
w0 (ByteString -> Bool
B.null ByteString
s0 Bool -> Bool -> Bool
&& More -> Int
moreLength More
m0 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0))
getByteString :: Int -> Get B.ByteString
getByteString :: Int -> Get ByteString
getByteString Int
n = do
bs <- Int -> Get ByteString
getBytes Int
n
return $! B.copy bs
getLazyByteString :: Int64 -> Get L.ByteString
getLazyByteString :: Int64 -> Get ByteString
getLazyByteString Int64
n = ByteString -> ByteString
f (ByteString -> ByteString) -> Get ByteString -> Get ByteString
forall a b. (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Int -> Get ByteString
getByteString (Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
n)
where f :: ByteString -> ByteString
f ByteString
bs = [ByteString] -> ByteString
L.fromChunks [ByteString
bs]
getShortByteString :: Int -> Get BS.ShortByteString
getShortByteString :: Int -> Get ShortByteString
getShortByteString Int
n = do
bs <- Int -> Get ByteString
getBytes Int
n
return $! BS.toShort bs
getBytes :: Int -> Get B.ByteString
getBytes :: Int -> Get ByteString
getBytes Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = String -> Get ByteString
forall a. String -> Get a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"getBytes: negative length requested"
getBytes Int
n = do
s <- Int -> Get ByteString
ensure Int
n
let consume = Int -> ByteString -> ByteString
B.unsafeTake Int
n ByteString
s
rest = Int -> ByteString -> ByteString
B.unsafeDrop Int
n ByteString
s
cur <- bytesRead
put rest (cur + n)
return consume
{-# INLINE getBytes #-}
getPtr :: Storable a => Int -> Get a
getPtr :: forall a. Storable a => Int -> Get a
getPtr Int
n = do
(fp,o,_) <- ByteString -> (ForeignPtr Word8, Int, Int)
B.toForeignPtr (ByteString -> (ForeignPtr Word8, Int, Int))
-> Get ByteString -> Get (ForeignPtr Word8, Int, Int)
forall a b. (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Int -> Get ByteString
getBytes Int
n
let k Ptr a
p = Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peek (Ptr (ZonkAny 0) -> Ptr a
forall a b. Ptr a -> Ptr b
castPtr (Ptr a
p Ptr a -> Int -> Ptr (ZonkAny 0)
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
o))
return (unsafeDupablePerformIO (withForeignPtr fp k))
{-# INLINE getPtr #-}
getInt8 :: Get Int8
getInt8 :: Get Int8
getInt8 = do
s <- Int -> Get ByteString
getBytes Int
1
return $! fromIntegral (B.unsafeHead s)
getInt16be :: Get Int16
getInt16be :: Get Int16
getInt16be = do
s <- Int -> Get ByteString
getBytes Int
2
return $! (fromIntegral (s `B.unsafeIndex` 0) `shiftL` 8) .|.
(fromIntegral (s `B.unsafeIndex` 1) )
getInt16le :: Get Int16
getInt16le :: Get Int16
getInt16le = do
s <- Int -> Get ByteString
getBytes Int
2
return $! (fromIntegral (s `B.unsafeIndex` 1) `shiftL` 8) .|.
(fromIntegral (s `B.unsafeIndex` 0) )
getInt32be :: Get Int32
getInt32be :: Get Int32
getInt32be = do
s <- Int -> Get ByteString
getBytes Int
4
return $! (fromIntegral (s `B.unsafeIndex` 0) `shiftL` 24) .|.
(fromIntegral (s `B.unsafeIndex` 1) `shiftL` 16) .|.
(fromIntegral (s `B.unsafeIndex` 2) `shiftL` 8) .|.
(fromIntegral (s `B.unsafeIndex` 3) )
getInt32le :: Get Int32
getInt32le :: Get Int32
getInt32le = do
s <- Int -> Get ByteString
getBytes Int
4
return $! (fromIntegral (s `B.unsafeIndex` 3) `shiftL` 24) .|.
(fromIntegral (s `B.unsafeIndex` 2) `shiftL` 16) .|.
(fromIntegral (s `B.unsafeIndex` 1) `shiftL` 8) .|.
(fromIntegral (s `B.unsafeIndex` 0) )
getInt64be :: Get Int64
getInt64be :: Get Int64
getInt64be = do
s <- Int -> Get ByteString
getBytes Int
8
return $! (fromIntegral (s `B.unsafeIndex` 0) `shiftL` 56) .|.
(fromIntegral (s `B.unsafeIndex` 1) `shiftL` 48) .|.
(fromIntegral (s `B.unsafeIndex` 2) `shiftL` 40) .|.
(fromIntegral (s `B.unsafeIndex` 3) `shiftL` 32) .|.
(fromIntegral (s `B.unsafeIndex` 4) `shiftL` 24) .|.
(fromIntegral (s `B.unsafeIndex` 5) `shiftL` 16) .|.
(fromIntegral (s `B.unsafeIndex` 6) `shiftL` 8) .|.
(fromIntegral (s `B.unsafeIndex` 7) )
getInt64le :: Get Int64
getInt64le :: Get Int64
getInt64le = do
s <- Int -> Get ByteString
getBytes Int
8
return $! (fromIntegral (s `B.unsafeIndex` 7) `shiftL` 56) .|.
(fromIntegral (s `B.unsafeIndex` 6) `shiftL` 48) .|.
(fromIntegral (s `B.unsafeIndex` 5) `shiftL` 40) .|.
(fromIntegral (s `B.unsafeIndex` 4) `shiftL` 32) .|.
(fromIntegral (s `B.unsafeIndex` 3) `shiftL` 24) .|.
(fromIntegral (s `B.unsafeIndex` 2) `shiftL` 16) .|.
(fromIntegral (s `B.unsafeIndex` 1) `shiftL` 8) .|.
(fromIntegral (s `B.unsafeIndex` 0) )
{-# INLINE getInt8 #-}
{-# INLINE getInt16be #-}
{-# INLINE getInt16le #-}
{-# INLINE getInt32be #-}
{-# INLINE getInt32le #-}
{-# INLINE getInt64be #-}
{-# INLINE getInt64le #-}
getWord8 :: Get Word8
getWord8 :: Get Word8
getWord8 = do
s <- Int -> Get ByteString
getBytes Int
1
return (B.unsafeHead s)
getWord16be :: Get Word16
getWord16be :: Get Word16
getWord16be = do
s <- Int -> Get ByteString
getBytes Int
2
return $! (fromIntegral (s `B.unsafeIndex` 0) `shiftl_w16` 8) .|.
(fromIntegral (s `B.unsafeIndex` 1))
getWord16le :: Get Word16
getWord16le :: Get Word16
getWord16le = do
s <- Int -> Get ByteString
getBytes Int
2
return $! (fromIntegral (s `B.unsafeIndex` 1) `shiftl_w16` 8) .|.
(fromIntegral (s `B.unsafeIndex` 0) )
getWord32be :: Get Word32
getWord32be :: Get Word32
getWord32be = do
s <- Int -> Get ByteString
getBytes Int
4
return $! (fromIntegral (s `B.unsafeIndex` 0) `shiftl_w32` 24) .|.
(fromIntegral (s `B.unsafeIndex` 1) `shiftl_w32` 16) .|.
(fromIntegral (s `B.unsafeIndex` 2) `shiftl_w32` 8) .|.
(fromIntegral (s `B.unsafeIndex` 3) )
getWord32le :: Get Word32
getWord32le :: Get Word32
getWord32le = do
s <- Int -> Get ByteString
getBytes Int
4
return $! (fromIntegral (s `B.unsafeIndex` 3) `shiftl_w32` 24) .|.
(fromIntegral (s `B.unsafeIndex` 2) `shiftl_w32` 16) .|.
(fromIntegral (s `B.unsafeIndex` 1) `shiftl_w32` 8) .|.
(fromIntegral (s `B.unsafeIndex` 0) )
getWord64be :: Get Word64
getWord64be :: Get Word64
getWord64be = do
s <- Int -> Get ByteString
getBytes Int
8
return $! (fromIntegral (s `B.unsafeIndex` 0) `shiftl_w64` 56) .|.
(fromIntegral (s `B.unsafeIndex` 1) `shiftl_w64` 48) .|.
(fromIntegral (s `B.unsafeIndex` 2) `shiftl_w64` 40) .|.
(fromIntegral (s `B.unsafeIndex` 3) `shiftl_w64` 32) .|.
(fromIntegral (s `B.unsafeIndex` 4) `shiftl_w64` 24) .|.
(fromIntegral (s `B.unsafeIndex` 5) `shiftl_w64` 16) .|.
(fromIntegral (s `B.unsafeIndex` 6) `shiftl_w64` 8) .|.
(fromIntegral (s `B.unsafeIndex` 7) )
getWord64le :: Get Word64
getWord64le :: Get Word64
getWord64le = do
s <- Int -> Get ByteString
getBytes Int
8
return $! (fromIntegral (s `B.unsafeIndex` 7) `shiftl_w64` 56) .|.
(fromIntegral (s `B.unsafeIndex` 6) `shiftl_w64` 48) .|.
(fromIntegral (s `B.unsafeIndex` 5) `shiftl_w64` 40) .|.
(fromIntegral (s `B.unsafeIndex` 4) `shiftl_w64` 32) .|.
(fromIntegral (s `B.unsafeIndex` 3) `shiftl_w64` 24) .|.
(fromIntegral (s `B.unsafeIndex` 2) `shiftl_w64` 16) .|.
(fromIntegral (s `B.unsafeIndex` 1) `shiftl_w64` 8) .|.
(fromIntegral (s `B.unsafeIndex` 0) )
{-# INLINE getWord8 #-}
{-# INLINE getWord16be #-}
{-# INLINE getWord16le #-}
{-# INLINE getWord32be #-}
{-# INLINE getWord32le #-}
{-# INLINE getWord64be #-}
{-# INLINE getWord64le #-}
getWordhost :: Get Word
getWordhost :: Get Word
getWordhost = Int -> Get Word
forall a. Storable a => Int -> Get a
getPtr (Word -> Int
forall a. Storable a => a -> Int
sizeOf (Word
forall a. HasCallStack => a
undefined :: Word))
getWord16host :: Get Word16
getWord16host :: Get Word16
getWord16host = Int -> Get Word16
forall a. Storable a => Int -> Get a
getPtr (Word16 -> Int
forall a. Storable a => a -> Int
sizeOf (Word16
forall a. HasCallStack => a
undefined :: Word16))
getWord32host :: Get Word32
getWord32host :: Get Word32
getWord32host = Int -> Get Word32
forall a. Storable a => Int -> Get a
getPtr (Word32 -> Int
forall a. Storable a => a -> Int
sizeOf (Word32
forall a. HasCallStack => a
undefined :: Word32))
getWord64host :: Get Word64
getWord64host :: Get Word64
getWord64host = Int -> Get Word64
forall a. Storable a => Int -> Get a
getPtr (Word64 -> Int
forall a. Storable a => a -> Int
sizeOf (Word64
forall a. HasCallStack => a
undefined :: Word64))
shiftl_w16 :: Word16 -> Int -> Word16
shiftl_w32 :: Word32 -> Int -> Word32
shiftl_w64 :: Word64 -> Int -> Word64
#if defined(__GLASGOW_HASKELL__) && !defined(__HADDOCK__)
#if MIN_VERSION_base(4,16,0)
shiftl_w16 :: Word16 -> Int -> Word16
shiftl_w16 (W16# Word16#
w) (I# Int#
i) = Word16# -> Word16
W16# (Word16#
w Word16# -> Int# -> Word16#
`uncheckedShiftLWord16#` Int#
i)
shiftl_w32 :: Word32 -> Int -> Word32
shiftl_w32 (W32# Word32#
w) (I# Int#
i) = Word32# -> Word32
W32# (Word32#
w Word32# -> Int# -> Word32#
`uncheckedShiftLWord32#` Int#
i)
#else
shiftl_w16 (W16# w) (I# i) = W16# (w `uncheckedShiftL#` i)
shiftl_w32 (W32# w) (I# i) = W32# (w `uncheckedShiftL#` i)
#endif
#if WORD_SIZE_IN_BITS < 64
shiftl_w64 (W64# w) (I# i) = W64# (w `uncheckedShiftL64#` i)
#if __GLASGOW_HASKELL__ <= 606
foreign import ccall unsafe "stg_uncheckedShiftL64"
uncheckedShiftL64# :: Word64# -> Int# -> Word64#
#endif
#else
#if MIN_VERSION_base(4,17,0)
shiftl_w64 :: Word64 -> Int -> Word64
shiftl_w64 (W64# Word64#
w) (I# Int#
i) = Word64# -> Word64
W64# (Word64#
w Word64# -> Int# -> Word64#
`uncheckedShiftL64#` Int#
i)
#else
shiftl_w64 (W64# w) (I# i) = W64# (w `uncheckedShiftL#` i)
#endif
#endif
#else
shiftl_w16 = shiftL
shiftl_w32 = shiftL
shiftl_w64 = shiftL
#endif
getTwoOf :: Get a -> Get b -> Get (a,b)
getTwoOf :: forall a b. Get a -> Get b -> Get (a, b)
getTwoOf Get a
ma Get b
mb = (a -> b -> (a, b)) -> Get a -> Get b -> Get (a, b)
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
M.liftM2 (,) Get a
ma Get b
mb
getListOf :: Get a -> Get [a]
getListOf :: forall a. Get a -> Get [a]
getListOf Get a
m = [a] -> Word64 -> Get [a]
forall {t}. (Eq t, Num t) => [a] -> t -> Get [a]
go [] (Word64 -> Get [a]) -> Get Word64 -> Get [a]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Get Word64
getWord64be
where
go :: [a] -> t -> Get [a]
go [a]
as t
0 = [a] -> Get [a]
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return ([a] -> Get [a]) -> [a] -> Get [a]
forall a b. (a -> b) -> a -> b
$! [a] -> [a]
forall a. [a] -> [a]
reverse [a]
as
go [a]
as t
i = do x <- Get a
m
x `seq` go (x:as) (i - 1)
getIArrayOf :: (Ix i, IArray a e) => Get i -> Get e -> Get (a i e)
getIArrayOf :: forall i (a :: * -> * -> *) e.
(Ix i, IArray a e) =>
Get i -> Get e -> Get (a i e)
getIArrayOf Get i
ix Get e
e = ((i, i) -> [e] -> a i e) -> Get (i, i) -> Get [e] -> Get (a i e)
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
M.liftM2 (i, i) -> [e] -> a i e
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
(i, i) -> [e] -> a i e
listArray (Get i -> Get i -> Get (i, i)
forall a b. Get a -> Get b -> Get (a, b)
getTwoOf Get i
ix Get i
ix) (Get e -> Get [e]
forall a. Get a -> Get [a]
getListOf Get e
e)
getSeqOf :: Get a -> Get (Seq.Seq a)
getSeqOf :: forall a. Get a -> Get (Seq a)
getSeqOf Get a
m = Seq a -> Word64 -> Get (Seq a)
forall {t}. (Eq t, Num t) => Seq a -> t -> Get (Seq a)
go Seq a
forall a. Seq a
Seq.empty (Word64 -> Get (Seq a)) -> Get Word64 -> Get (Seq a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Get Word64
getWord64be
where
go :: Seq a -> t -> Get (Seq a)
go Seq a
xs t
0 = Seq a -> Get (Seq a)
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return (Seq a -> Get (Seq a)) -> Seq a -> Get (Seq a)
forall a b. (a -> b) -> a -> b
$! Seq a
xs
go Seq a
xs t
n = Seq a
xs Seq a -> Get (Seq a) -> Get (Seq a)
forall a b. a -> b -> b
`seq` t
n t -> Get (Seq a) -> Get (Seq a)
forall a b. a -> b -> b
`seq` do
x <- Get a
m
go (xs Seq.|> x) (n - 1)
getTreeOf :: Get a -> Get (T.Tree a)
getTreeOf :: forall a. Get a -> Get (Tree a)
getTreeOf Get a
m = (a -> [Tree a] -> Tree a) -> Get a -> Get [Tree a] -> Get (Tree a)
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
M.liftM2 a -> [Tree a] -> Tree a
forall a. a -> [Tree a] -> Tree a
T.Node Get a
m (Get (Tree a) -> Get [Tree a]
forall a. Get a -> Get [a]
getListOf (Get a -> Get (Tree a)
forall a. Get a -> Get (Tree a)
getTreeOf Get a
m))
getMapOf :: Ord k => Get k -> Get a -> Get (Map.Map k a)
getMapOf :: forall k a. Ord k => Get k -> Get a -> Get (Map k a)
getMapOf Get k
k Get a
m = [(k, a)] -> Map k a
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(k, a)] -> Map k a) -> Get [(k, a)] -> Get (Map k a)
forall a b. (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Get (k, a) -> Get [(k, a)]
forall a. Get a -> Get [a]
getListOf (Get k -> Get a -> Get (k, a)
forall a b. Get a -> Get b -> Get (a, b)
getTwoOf Get k
k Get a
m)
getIntMapOf :: Get Int -> Get a -> Get (IntMap.IntMap a)
getIntMapOf :: forall a. Get Int -> Get a -> Get (IntMap a)
getIntMapOf Get Int
i Get a
m = [(Int, a)] -> IntMap a
forall a. [(Int, a)] -> IntMap a
IntMap.fromList ([(Int, a)] -> IntMap a) -> Get [(Int, a)] -> Get (IntMap a)
forall a b. (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Get (Int, a) -> Get [(Int, a)]
forall a. Get a -> Get [a]
getListOf (Get Int -> Get a -> Get (Int, a)
forall a b. Get a -> Get b -> Get (a, b)
getTwoOf Get Int
i Get a
m)
getSetOf :: Ord a => Get a -> Get (Set.Set a)
getSetOf :: forall a. Ord a => Get a -> Get (Set a)
getSetOf Get a
m = [a] -> Set a
forall a. Ord a => [a] -> Set a
Set.fromList ([a] -> Set a) -> Get [a] -> Get (Set a)
forall a b. (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Get a -> Get [a]
forall a. Get a -> Get [a]
getListOf Get a
m
getIntSetOf :: Get Int -> Get IntSet.IntSet
getIntSetOf :: Get Int -> Get IntSet
getIntSetOf Get Int
m = [Int] -> IntSet
IntSet.fromList ([Int] -> IntSet) -> Get [Int] -> Get IntSet
forall a b. (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Get Int -> Get [Int]
forall a. Get a -> Get [a]
getListOf Get Int
m
getMaybeOf :: Get a -> Get (Maybe a)
getMaybeOf :: forall a. Get a -> Get (Maybe a)
getMaybeOf Get a
m = do
tag <- Get Word8
getWord8
case tag of
Word8
0 -> Maybe a -> Get (Maybe a)
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
Word8
_ -> a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> Get a -> Get (Maybe a)
forall a b. (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Get a
m
getEitherOf :: Get a -> Get b -> Get (Either a b)
getEitherOf :: forall a b. Get a -> Get b -> Get (Either a b)
getEitherOf Get a
ma Get b
mb = do
tag <- Get Word8
getWord8
case tag of
Word8
0 -> a -> Either a b
forall a b. a -> Either a b
Left (a -> Either a b) -> Get a -> Get (Either a b)
forall a b. (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Get a
ma
Word8
_ -> b -> Either a b
forall a b. b -> Either a b
Right (b -> Either a b) -> Get b -> Get (Either a b)
forall a b. (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Get b
mb
getNested :: Get Int -> Get a -> Get a
getNested :: forall a. Get Int -> Get a -> Get a
getNested Get Int
getLen Get a
getVal = do
n <- Get Int
getLen
isolate n getVal
bytesRead :: Get Int
bytesRead :: Get Int
bytesRead = (forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success Int r -> Result r)
-> Get Int
forall a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get (\ByteString
i Buffer
b More
m Int
w Failure r
_ Success Int r
k -> Success Int r
k ByteString
i Buffer
b More
m Int
w Int
w)