{-# 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 " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show String
msg
show (Partial ByteString -> Result r
_) = String
"Partial _"
show (Done r
r ByteString
bs) = String
"Done " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show r
r forall a. [a] -> [a] -> [a]
++ String
" " forall a. [a] -> [a] -> [a]
++ 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) = forall r. String -> ByteString -> Result r
Fail String
msg ByteString
rest
fmap a -> b
f (Partial ByteString -> Result a
k) = forall r. (ByteString -> Result r) -> Result r
Partial (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Result a
k)
fmap a -> b
f (Done a
r ByteString
bs) = 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 = forall a. a -> Maybe a
Just ByteString
B.empty
extendBuffer :: Buffer -> B.ByteString -> Buffer
extendBuffer :: Buffer -> ByteString -> Buffer
extendBuffer Buffer
buf ByteString
chunk =
do ByteString
bs <- Buffer
buf
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! ByteString
bs ByteString -> ByteString -> ByteString
`B.append` ByteString
chunk
{-# INLINE extendBuffer #-}
append :: Buffer -> Buffer -> Buffer
append :: Buffer -> Buffer -> Buffer
append Buffer
l Buffer
r = ByteString -> ByteString -> ByteString
B.append forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Buffer
l 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 = 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
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: More -> More -> Bool
$c/= :: More -> More -> Bool
== :: More -> More -> Bool
$c== :: 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 -> 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 a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get forall a b. (a -> b) -> a -> b
$ \ ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf Success b r
ks ->
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 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 a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get 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 a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get forall a b. (a -> b) -> a -> b
$ \ ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf Success b r
ks ->
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 forall a b. (a -> b) -> a -> b
$ \ ByteString
s1 Buffer
b1 More
m1 Int
w1 a -> b
g ->
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 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 a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get forall a b. (a -> b) -> a -> b
$ \ ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf Success b r
ks ->
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 forall a b. (a -> b) -> a -> b
$ \ ByteString
s1 Buffer
b1 More
m1 Int
w1 a
_ -> 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 = forall a. String -> Get a
failDesc String
"empty"
{-# INLINE empty #-}
<|> :: 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 = 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 a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get forall a b. (a -> b) -> a -> b
$ \ ByteString
s0 Buffer
b0 More
m0 Int
w0 Failure r
kf Success b r
ks ->
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 forall a b. (a -> b) -> a -> b
$ \ ByteString
s1 Buffer
b1 More
m1 Int
w1 a
a -> 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
(>>) = 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 = forall a. String -> Get a
failDesc
{-# INLINE fail #-}
instance M.MonadPlus Get where
mzero :: forall a. Get a
mzero = 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 a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get 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
_ = 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 forall {p}. p -> Buffer -> More -> [String] -> String -> Result r
kf' Success a r
ks'
in 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 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" forall a. [a] -> [a] -> [a]
++ forall a. [a] -> [[a]] -> [a]
intercalate String
"\n\t" [String]
ls forall a. [a] -> [a] -> [a]
++ String
"\n"
get :: Get B.ByteString
get :: Get ByteString
get = 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 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 a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get 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
lforall a. a -> [a] -> [a]
:[String]
ls)
in 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 = 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 =
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 forall a.
Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
m ByteString
str forall a. Maybe a
Nothing More
Complete Int
0 forall a. Failure a
failK forall a. Success a a
finalK of
Fail String
i ByteString
_ -> forall a b. a -> Either a b
Left String
i
Done a
a ByteString
_ -> forall a b. b -> Either a b
Right a
a
Partial{} -> 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 = forall a.
Get a
-> forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r
unGet Get a
m ByteString
str forall a. Maybe a
Nothing (Maybe Int -> More
Incomplete Maybe Int
mbLen) Int
0 forall a. Failure a
failK 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 = forall a. Get a -> Maybe Int -> ByteString -> Result a
runGetChunk Get a
m 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 forall a.
Get a -> ByteString -> Int -> (Either String a, ByteString)
runGetState' Get a
m ByteString
str Int
off of
(Right a
a,ByteString
bs) -> forall a b. b -> Either a b
Right (a
a,ByteString
bs)
(Left String
i,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 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) forall a. Maybe a
Nothing More
Complete Int
0 forall a. Failure a
failK forall a. Success a a
finalK of
Fail String
i ByteString
bs -> (forall a b. a -> Either a b
Left String
i,ByteString
bs)
Done a
a ByteString
bs -> (forall a b. b -> Either a b
Right a
a, ByteString
bs)
Partial{} -> (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] -> forall {a}. (a, ByteString) -> (a, ByteString)
wrapStrict (forall a.
Get a -> ByteString -> Int -> (Either String a, ByteString)
runGetState' Get a
m ByteString
c Int
0)
[] -> forall {a}. (a, ByteString) -> (a, ByteString)
wrapStrict (forall a.
Get a -> ByteString -> Int -> (Either String a, ByteString)
runGetState' Get a
m ByteString
B.empty Int
0)
ByteString
c:[ByteString]
cs -> forall {b}.
Result b -> [ByteString] -> (Either String b, ByteString)
loop (forall a. Get a -> Maybe Int -> ByteString -> Result a
runGetChunk Get a
m (forall a. a -> Maybe a
Just (Int
len forall a. Num a => a -> a -> a
- ByteString -> Int
B.length ByteString
c)) ByteString
c) [ByteString]
cs
where
len :: Int
len = 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 -> (forall a b. a -> Either a b
Left String
str, [ByteString] -> ByteString
L.fromChunks (ByteString
rest 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 -> (forall a b. b -> Either a b
Right b
r, [ByteString] -> ByteString
L.fromChunks (ByteString
rest 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 = forall a b. (a, b) -> a
fst (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 forall a. Get a -> ByteString -> (Either String a, ByteString)
runGetLazy' Get a
m ByteString
lstr of
(Right a
a,ByteString
rest) -> forall a b. b -> Either a b
Right (a
a,ByteString
rest)
(Left String
err,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 seq :: forall a b. a -> b -> b
`seq` forall a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get 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 forall a. Num a => a -> a -> a
- ByteString -> Int
B.length ByteString
s0
in if Int
n' 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 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 (forall a. [a] -> [a]
reverse (ByteString
s0 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 (forall a. [a] -> [a]
reverse (forall a. [a] -> [a]
init (ByteString
s0 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 -> forall r. (ByteString -> Result r) -> Result r
Partial 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 -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$! Int
l forall a. Num a => a -> a -> a
- ByteString -> Int
B.length ByteString
s
Maybe Int
Nothing -> 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 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 forall a. Num a => a -> a -> a
- ByteString -> Int
B.length ByteString
s0
in if Int
n' 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
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
M.when (Int
n forall a. Ord a => a -> a -> Bool
< Int
0) (forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Attempted to isolate a negative number of bytes")
ByteString
s <- Int -> Get ByteString
ensure Int
n
let (ByteString
s',ByteString
rest) = Int -> ByteString -> (ByteString, ByteString)
B.splitAt Int
n ByteString
s
Int
cur <- Get Int
bytesRead
ByteString -> Int -> Get ()
put ByteString
s' Int
cur
a
a <- Get a
m
ByteString
used <- Get ByteString
get
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteString -> Bool
B.null ByteString
used) (forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"not all bytes parsed in isolate")
ByteString -> Int -> Get ()
put ByteString
rest (Int
cur forall a. Num a => a -> a -> a
+ Int
n)
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
failDesc :: String -> Get a
failDesc :: forall a. String -> Get a
failDesc String
err = do
let msg :: String
msg = String
"Failed reading: " forall a. [a] -> [a] -> [a]
++ String
err
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
ByteString
s <- Int -> Get ByteString
ensure Int
n
Int
cur <- Get Int
bytesRead
ByteString -> Int -> Get ()
put (Int -> ByteString -> ByteString
B.drop Int
n ByteString
s) (Int
cur forall a. Num a => a -> a -> a
+ Int
n)
uncheckedSkip :: Int -> Get ()
uncheckedSkip :: Int -> Get ()
uncheckedSkip Int
n = do
ByteString
s <- Get ByteString
get
Int
cur <- Get Int
bytesRead
ByteString -> Int -> Get ()
put (Int -> ByteString -> ByteString
B.drop Int
n ByteString
s) (Int
cur forall a. Num a => a -> a -> a
+ Int
n)
lookAhead :: Get a -> Get a
lookAhead :: forall a. Get a -> Get a
lookAhead Get a
ga = forall a.
(forall r.
ByteString
-> Buffer -> More -> Int -> Failure r -> Success a r -> Result r)
-> Get a
Get 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 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 forall {p}. p -> Buffer -> More -> [String] -> String -> Result r
kf' 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
ByteString
s <- Get ByteString
get
Int
pre <- Get Int
bytesRead
Maybe a
ma <- Get (Maybe a)
gma
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
M.when (forall a. Maybe a -> Bool
isNothing Maybe a
ma) (ByteString -> Int -> Get ()
put ByteString
s Int
pre)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
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
ByteString
s <- Get ByteString
get
Int
pre <- Get Int
bytesRead
Either a b
ea <- Get (Either a b)
gea
case Either a b
ea of
Left a
_ -> ByteString -> Int -> Get ()
put ByteString
s Int
pre
Either a b
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
forall (m :: * -> *) a. Monad m => a -> m a
return Either a b
ea
uncheckedLookAhead :: Int -> Get B.ByteString
uncheckedLookAhead :: Int -> Get ByteString
uncheckedLookAhead Int
n = do
ByteString
s <- Get ByteString
get
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> ByteString -> ByteString
B.take Int
n ByteString
s)
remaining :: Get Int
remaining :: Get Int
remaining = 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 forall a. Num a => a -> a -> a
+ More -> Int
moreLength More
m0))
isEmpty :: Get Bool
isEmpty :: Get Bool
isEmpty = 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 forall a. Eq a => a -> a -> Bool
== Int
0))
getByteString :: Int -> Get B.ByteString
getByteString :: Int -> Get ByteString
getByteString Int
n = do
ByteString
bs <- Int -> Get ByteString
getBytes Int
n
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! ByteString -> ByteString
B.copy ByteString
bs
getLazyByteString :: Int64 -> Get L.ByteString
getLazyByteString :: Int64 -> Get ByteString
getLazyByteString Int64
n = ByteString -> ByteString
f forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Int -> Get ByteString
getByteString (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
ByteString
bs <- Int -> Get ByteString
getBytes Int
n
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! ByteString -> ShortByteString
BS.toShort ByteString
bs
getBytes :: Int -> Get B.ByteString
getBytes :: Int -> Get ByteString
getBytes Int
n | Int
n forall a. Ord a => a -> a -> Bool
< Int
0 = forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"getBytes: negative length requested"
getBytes Int
n = do
ByteString
s <- Int -> Get ByteString
ensure Int
n
let consume :: ByteString
consume = Int -> ByteString -> ByteString
B.unsafeTake Int
n ByteString
s
rest :: ByteString
rest = Int -> ByteString -> ByteString
B.unsafeDrop Int
n ByteString
s
Int
cur <- Get Int
bytesRead
ByteString -> Int -> Get ()
put ByteString
rest (Int
cur forall a. Num a => a -> a -> a
+ Int
n)
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
consume
{-# INLINE getBytes #-}
getPtr :: Storable a => Int -> Get a
getPtr :: forall a. Storable a => Int -> Get a
getPtr Int
n = do
(ForeignPtr Word8
fp,Int
o,Int
_) <- ByteString -> (ForeignPtr Word8, Int, Int)
B.toForeignPtr forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Int -> Get ByteString
getBytes Int
n
let k :: Ptr a -> IO a
k Ptr a
p = forall a. Storable a => Ptr a -> IO a
peek (forall a b. Ptr a -> Ptr b
castPtr (Ptr a
p forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
o))
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. IO a -> a
unsafeDupablePerformIO (forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
fp forall {a} {a}. Storable a => Ptr a -> IO a
k))
{-# INLINE getPtr #-}
getInt8 :: Get Int8
getInt8 :: Get Int8
getInt8 = do
ByteString
s <- Int -> Get ByteString
getBytes Int
1
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Word8
B.unsafeHead ByteString
s)
getInt16be :: Get Int16
getInt16be :: Get Int16
getInt16be = do
ByteString
s <- Int -> Get ByteString
getBytes Int
2
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! (forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) forall a. Bits a => a -> Int -> a
`shiftL` Int
8) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1) )
getInt16le :: Get Int16
getInt16le :: Get Int16
getInt16le = do
ByteString
s <- Int -> Get ByteString
getBytes Int
2
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! (forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1) forall a. Bits a => a -> Int -> a
`shiftL` Int
8) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) )
getInt32be :: Get Int32
getInt32be :: Get Int32
getInt32be = do
ByteString
s <- Int -> Get ByteString
getBytes Int
4
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! (forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) forall a. Bits a => a -> Int -> a
`shiftL` Int
24) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1) forall a. Bits a => a -> Int -> a
`shiftL` Int
16) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
2) forall a. Bits a => a -> Int -> a
`shiftL` Int
8) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
3) )
getInt32le :: Get Int32
getInt32le :: Get Int32
getInt32le = do
ByteString
s <- Int -> Get ByteString
getBytes Int
4
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! (forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
3) forall a. Bits a => a -> Int -> a
`shiftL` Int
24) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
2) forall a. Bits a => a -> Int -> a
`shiftL` Int
16) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1) forall a. Bits a => a -> Int -> a
`shiftL` Int
8) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) )
getInt64be :: Get Int64
getInt64be :: Get Int64
getInt64be = do
ByteString
s <- Int -> Get ByteString
getBytes Int
8
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! (forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) forall a. Bits a => a -> Int -> a
`shiftL` Int
56) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1) forall a. Bits a => a -> Int -> a
`shiftL` Int
48) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
2) forall a. Bits a => a -> Int -> a
`shiftL` Int
40) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
3) forall a. Bits a => a -> Int -> a
`shiftL` Int
32) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
4) forall a. Bits a => a -> Int -> a
`shiftL` Int
24) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
5) forall a. Bits a => a -> Int -> a
`shiftL` Int
16) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
6) forall a. Bits a => a -> Int -> a
`shiftL` Int
8) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
7) )
getInt64le :: Get Int64
getInt64le :: Get Int64
getInt64le = do
ByteString
s <- Int -> Get ByteString
getBytes Int
8
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! (forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
7) forall a. Bits a => a -> Int -> a
`shiftL` Int
56) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
6) forall a. Bits a => a -> Int -> a
`shiftL` Int
48) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
5) forall a. Bits a => a -> Int -> a
`shiftL` Int
40) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
4) forall a. Bits a => a -> Int -> a
`shiftL` Int
32) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
3) forall a. Bits a => a -> Int -> a
`shiftL` Int
24) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
2) forall a. Bits a => a -> Int -> a
`shiftL` Int
16) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1) forall a. Bits a => a -> Int -> a
`shiftL` Int
8) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) )
{-# INLINE getInt8 #-}
{-# INLINE getInt16be #-}
{-# INLINE getInt16le #-}
{-# INLINE getInt32be #-}
{-# INLINE getInt32le #-}
{-# INLINE getInt64be #-}
{-# INLINE getInt64le #-}
getWord8 :: Get Word8
getWord8 :: Get Word8
getWord8 = do
ByteString
s <- Int -> Get ByteString
getBytes Int
1
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> Word8
B.unsafeHead ByteString
s)
getWord16be :: Get Word16
getWord16be :: Get Word16
getWord16be = do
ByteString
s <- Int -> Get ByteString
getBytes Int
2
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! (forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) Word16 -> Int -> Word16
`shiftl_w16` Int
8) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1))
getWord16le :: Get Word16
getWord16le :: Get Word16
getWord16le = do
ByteString
s <- Int -> Get ByteString
getBytes Int
2
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! (forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1) Word16 -> Int -> Word16
`shiftl_w16` Int
8) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) )
getWord32be :: Get Word32
getWord32be :: Get Word32
getWord32be = do
ByteString
s <- Int -> Get ByteString
getBytes Int
4
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! (forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) Word32 -> Int -> Word32
`shiftl_w32` Int
24) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1) Word32 -> Int -> Word32
`shiftl_w32` Int
16) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
2) Word32 -> Int -> Word32
`shiftl_w32` Int
8) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
3) )
getWord32le :: Get Word32
getWord32le :: Get Word32
getWord32le = do
ByteString
s <- Int -> Get ByteString
getBytes Int
4
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! (forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
3) Word32 -> Int -> Word32
`shiftl_w32` Int
24) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
2) Word32 -> Int -> Word32
`shiftl_w32` Int
16) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1) Word32 -> Int -> Word32
`shiftl_w32` Int
8) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) )
getWord64be :: Get Word64
getWord64be :: Get Word64
getWord64be = do
ByteString
s <- Int -> Get ByteString
getBytes Int
8
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! (forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) Word64 -> Int -> Word64
`shiftl_w64` Int
56) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1) Word64 -> Int -> Word64
`shiftl_w64` Int
48) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
2) Word64 -> Int -> Word64
`shiftl_w64` Int
40) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
3) Word64 -> Int -> Word64
`shiftl_w64` Int
32) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
4) Word64 -> Int -> Word64
`shiftl_w64` Int
24) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
5) Word64 -> Int -> Word64
`shiftl_w64` Int
16) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
6) Word64 -> Int -> Word64
`shiftl_w64` Int
8) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
7) )
getWord64le :: Get Word64
getWord64le :: Get Word64
getWord64le = do
ByteString
s <- Int -> Get ByteString
getBytes Int
8
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! (forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
7) Word64 -> Int -> Word64
`shiftl_w64` Int
56) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
6) Word64 -> Int -> Word64
`shiftl_w64` Int
48) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
5) Word64 -> Int -> Word64
`shiftl_w64` Int
40) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
4) Word64 -> Int -> Word64
`shiftl_w64` Int
32) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
3) Word64 -> Int -> Word64
`shiftl_w64` Int
24) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
2) Word64 -> Int -> Word64
`shiftl_w64` Int
16) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
1) Word64 -> Int -> Word64
`shiftl_w64` Int
8) forall a. Bits a => a -> a -> a
.|.
(forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
s ByteString -> Int -> Word8
`B.unsafeIndex` Int
0) )
{-# INLINE getWord8 #-}
{-# INLINE getWord16be #-}
{-# INLINE getWord16le #-}
{-# INLINE getWord32be #-}
{-# INLINE getWord32le #-}
{-# INLINE getWord64be #-}
{-# INLINE getWord64le #-}
getWordhost :: Get Word
getWordhost :: Get Word
getWordhost = forall a. Storable a => Int -> Get a
getPtr (forall a. Storable a => a -> Int
sizeOf (forall a. HasCallStack => a
undefined :: Word))
getWord16host :: Get Word16
getWord16host :: Get Word16
getWord16host = forall a. Storable a => Int -> Get a
getPtr (forall a. Storable a => a -> Int
sizeOf (forall a. HasCallStack => a
undefined :: Word16))
getWord32host :: Get Word32
getWord32host :: Get Word32
getWord32host = forall a. Storable a => Int -> Get a
getPtr (forall a. Storable a => a -> Int
sizeOf (forall a. HasCallStack => a
undefined :: Word32))
getWord64host :: Get Word64
getWord64host :: Get Word64
getWord64host = forall a. Storable a => Int -> Get a
getPtr (forall a. Storable a => a -> Int
sizeOf (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 (W64# w) (I# i) = W64# (w `uncheckedShiftL64#` i)
#else
shiftl_w64 :: Word64 -> Int -> Word64
shiftl_w64 (W64# Word#
w) (I# Int#
i) = Word# -> Word64
W64# (Word#
w Word# -> Int# -> Word#
`uncheckedShiftL#` Int#
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 = 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 = forall {t}. (Eq t, Num t) => [a] -> t -> Get [a]
go [] 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 = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! forall a. [a] -> [a]
reverse [a]
as
go [a]
as t
i = do a
x <- Get a
m
a
x seq :: forall a b. a -> b -> b
`seq` [a] -> t -> Get [a]
go (a
xforall a. a -> [a] -> [a]
:[a]
as) (t
i forall a. Num a => a -> a -> a
- t
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 = forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
M.liftM2 forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
(i, i) -> [e] -> a i e
listArray (forall a b. Get a -> Get b -> Get (a, b)
getTwoOf Get i
ix Get i
ix) (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 = forall {t}. (Eq t, Num t) => Seq a -> t -> Get (Seq a)
go forall a. Seq a
Seq.empty 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 = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! Seq a
xs
go Seq a
xs t
n = Seq a
xs seq :: forall a b. a -> b -> b
`seq` t
n seq :: forall a b. a -> b -> b
`seq` do
a
x <- Get a
m
Seq a -> t -> Get (Seq a)
go (Seq a
xs forall a. Seq a -> a -> Seq a
Seq.|> a
x) (t
n forall a. Num a => a -> a -> a
- t
1)
getTreeOf :: Get a -> Get (T.Tree a)
getTreeOf :: forall a. Get a -> Get (Tree a)
getTreeOf Get a
m = forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
M.liftM2 forall a. a -> [Tree a] -> Tree a
T.Node Get a
m (forall a. Get a -> Get [a]
getListOf (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 = forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` forall a. Get a -> Get [a]
getListOf (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 = forall a. [(Int, a)] -> IntMap a
IntMap.fromList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` forall a. Get a -> Get [a]
getListOf (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 = forall a. Ord a => [a] -> Set a
Set.fromList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` 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 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` 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
Word8
tag <- Get Word8
getWord8
case Word8
tag of
Word8
0 -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
Word8
_ -> forall a. a -> Maybe a
Just 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
Word8
tag <- Get Word8
getWord8
case Word8
tag of
Word8
0 -> forall a b. a -> Either a b
Left forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Get a
ma
Word8
_ -> forall a b. b -> Either a b
Right 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
Int
n <- Get Int
getLen
forall a. Int -> Get a -> Get a
isolate Int
n Get a
getVal
bytesRead :: Get Int
bytesRead :: Get Int
bytesRead = 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)