-----------------------------------------------------------------------------

-- |

-- Module      : Data.ByteString.Parser

-- Copyright   : Lennart Kolmodin, George Giorgidze

-- License     : BSD3

--

-- Maintainer  : George Giorgidze <http://cs.nott.ac.uk/~ggg/>

-- Stability   : experimental

-- Portability : Portable

--

-- A monad for efficiently building structures from

-- encoded lazy ByteStrings.

--

-----------------------------------------------------------------------------


module Codec.ByteString.Parser (

    -- * The Parser type

      Parser
    , runParser
    , runParserState

    -- * Parsing

    , choice
    , expect
    , skip
    , lookAhead
    , lookAheadM
    , lookAheadE

    -- * Utility

    , bytesRead
    , getBytes
    , remaining
    , isEmpty

    -- * Parsing particular types

    , satisfy
    , getString
    , getStringNul
    , string
    , getWord8
    , getInt8
    , word8
    , int8

    -- ** ByteStrings

    , getByteString
    , getLazyByteString
    , getLazyByteStringNul
    , getRemainingLazyByteString

    -- ** Big-endian reads

    , getWord16be
    , word16be
    , getWord24be
    , word24be
    , getWord32be
    , word32be
    , getWord64be
    , word64be

    , getInt16be
    , int16be
    , getInt32be
    , int32be
    , getInt64be
    , int64be

    -- ** Little-endian reads

    , getWord16le
    , word16le
    , getWord24le
    , word24le
    , getWord32le
    , word32le
    , getWord64le
    , word64le

    , getInt16le
    , int16le
    , getInt32le
    , int32le
    , getInt64le
    , int64le

    -- ** Host-endian, unaligned reads

    , getWordHost
    , wordHost
    , getWord16host
    , word16host
    , getWord32host
    , word32host
    , getWord64host
    , word64host

    -- Variable length reads

    , getVarLenBe
    , varLenBe
    , getVarLenLe
    , varLenLe
  ) where

import qualified Data.ByteString               as B
import qualified Data.ByteString.Lazy          as L
import qualified Data.ByteString.Internal      as B
import qualified Data.ByteString.Lazy.Internal as L

import Prelude                 hiding (fail)

import Foreign.Storable        (Storable, peek, sizeOf)
import Foreign.Ptr             (plusPtr, castPtr)
import Foreign.ForeignPtr      (withForeignPtr)
import Control.Monad.ST        (runST)
import Control.Monad.ST.Unsafe (unsafeInterleaveST)
import Control.Monad.Fail

import Control.Monad           hiding (fail)
import Control.Applicative
import Data.STRef
import Data.Word
import Data.Int
import Data.Bits
import Data.Maybe

import System.IO.Unsafe        (unsafePerformIO)

-- | The parse state

data S = S {-# UNPACK #-} !B.ByteString  -- current chunk

           L.ByteString                  -- the rest of the input

           {-# UNPACK #-} !Int64         -- bytes read


-- | The Get monad is just a State monad carrying around the input ByteString

newtype Parser a = Parser { forall a. Parser a -> S -> Either String (a, S)
unParser :: S -> Either String (a, S) }

instance Functor Parser where
    fmap :: forall a b. (a -> b) -> Parser a -> Parser b
fmap a -> b
f Parser a
m = (S -> Either String (b, S)) -> Parser b
forall a. (S -> Either String (a, S)) -> Parser a
Parser ((S -> Either String (b, S)) -> Parser b)
-> (S -> Either String (b, S)) -> Parser b
forall a b. (a -> b) -> a -> b
$ \S
s -> case Parser a -> S -> Either String (a, S)
forall a. Parser a -> S -> Either String (a, S)
unParser Parser a
m S
s of
      Left String
e -> String -> Either String (b, S)
forall a b. a -> Either a b
Left String
e
      Right (a
a, S
s') -> (b, S) -> Either String (b, S)
forall a b. b -> Either a b
Right (a -> b
f a
a, S
s')

instance Monad Parser where
    return :: forall a. a -> Parser a
return a
a  = (S -> Either String (a, S)) -> Parser a
forall a. (S -> Either String (a, S)) -> Parser a
Parser (\S
s -> (a, S) -> Either String (a, S)
forall a b. b -> Either a b
Right (a
a, S
s))
    Parser a
m >>= :: forall a b. Parser a -> (a -> Parser b) -> Parser b
>>= a -> Parser b
k   = (S -> Either String (b, S)) -> Parser b
forall a. (S -> Either String (a, S)) -> Parser a
Parser ((S -> Either String (b, S)) -> Parser b)
-> (S -> Either String (b, S)) -> Parser b
forall a b. (a -> b) -> a -> b
$ \S
s -> case (Parser a -> S -> Either String (a, S)
forall a. Parser a -> S -> Either String (a, S)
unParser Parser a
m) S
s of
      Left String
e -> String -> Either String (b, S)
forall a b. a -> Either a b
Left String
e
      Right (a
a, S
s') -> (Parser b -> S -> Either String (b, S)
forall a. Parser a -> S -> Either String (a, S)
unParser (a -> Parser b
k a
a)) S
s'
instance MonadPlus Parser where
  mzero :: forall a. Parser a
mzero = (S -> Either String (a, S)) -> Parser a
forall a. (S -> Either String (a, S)) -> Parser a
Parser ((S -> Either String (a, S)) -> Parser a)
-> (S -> Either String (a, S)) -> Parser a
forall a b. (a -> b) -> a -> b
$ \S
_ -> String -> Either String (a, S)
forall a b. a -> Either a b
Left []
  mplus :: forall a. Parser a -> Parser a -> Parser a
mplus Parser a
p1 Parser a
p2 = (S -> Either String (a, S)) -> Parser a
forall a. (S -> Either String (a, S)) -> Parser a
Parser ((S -> Either String (a, S)) -> Parser a)
-> (S -> Either String (a, S)) -> Parser a
forall a b. (a -> b) -> a -> b
$ \S
s -> case (Parser a -> S -> Either String (a, S)
forall a. Parser a -> S -> Either String (a, S)
unParser Parser a
p1 S
s) of
    Left String
e1 -> case (Parser a -> S -> Either String (a, S)
forall a. Parser a -> S -> Either String (a, S)
unParser Parser a
p2 S
s) of
      Left String
e2 -> String -> Either String (a, S)
forall a b. a -> Either a b
Left (String
e1 String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
e2)
      Either String (a, S)
ok -> Either String (a, S)
ok
    Either String (a, S)
ok -> Either String (a, S)
ok

instance MonadFail Parser where
  fail :: forall a. String -> Parser a
fail String
err = (S -> Either String (a, S)) -> Parser a
forall a. (S -> Either String (a, S)) -> Parser a
Parser ((S -> Either String (a, S)) -> Parser a)
-> (S -> Either String (a, S)) -> Parser a
forall a b. (a -> b) -> a -> b
$ \(S ByteString
_ ByteString
_ Int64
bytes) ->
    String -> Either String (a, S)
forall a b. a -> Either a b
Left (String
err String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
". Failed reading at byte position " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int64 -> String
forall a. Show a => a -> String
show Int64
bytes)

instance Applicative Parser where
  pure :: forall a. a -> Parser a
pure  = a -> Parser a
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return
  <*> :: forall a b. Parser (a -> b) -> Parser a -> Parser b
(<*>) = Parser (a -> b) -> Parser a -> Parser b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap

instance Alternative Parser where
  empty :: forall a. Parser a
empty = Parser a
forall a. Parser a
forall (m :: * -> *) a. MonadPlus m => m a
mzero
  <|> :: forall a. Parser a -> Parser a -> Parser a
(<|>) = Parser a -> Parser a -> Parser a
forall a. Parser a -> Parser a -> Parser a
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
mplus

------------------------------------------------------------------------


get :: Parser S
get :: Parser S
get   = (S -> Either String (S, S)) -> Parser S
forall a. (S -> Either String (a, S)) -> Parser a
Parser ((S -> Either String (S, S)) -> Parser S)
-> (S -> Either String (S, S)) -> Parser S
forall a b. (a -> b) -> a -> b
$ \S
s -> (S, S) -> Either String (S, S)
forall a b. b -> Either a b
Right (S
s, S
s)

put :: S -> Parser ()
put :: S -> Parser ()
put S
s = (S -> Either String ((), S)) -> Parser ()
forall a. (S -> Either String (a, S)) -> Parser a
Parser ((S -> Either String ((), S)) -> Parser ())
-> (S -> Either String ((), S)) -> Parser ()
forall a b. (a -> b) -> a -> b
$ \S
_ -> ((), S) -> Either String ((), S)
forall a b. b -> Either a b
Right ((), S
s)

------------------------------------------------------------------------


initState :: L.ByteString -> S
initState :: ByteString -> S
initState ByteString
xs = ByteString -> Int64 -> S
mkState ByteString
xs Int64
0

mkState :: L.ByteString -> Int64 -> S
mkState :: ByteString -> Int64 -> S
mkState ByteString
l = case ByteString
l of
    ByteString
L.Empty      -> ByteString -> ByteString -> Int64 -> S
S ByteString
B.empty ByteString
L.empty
    L.Chunk ByteString
x ByteString
xs -> ByteString -> ByteString -> Int64 -> S
S ByteString
x ByteString
xs

-- | Run the Get monad applies a 'get'-based parser on the input ByteString

runParser :: Parser a -> L.ByteString -> Either String a
runParser :: forall a. Parser a -> ByteString -> Either String a
runParser Parser a
m ByteString
str = case Parser a -> S -> Either String (a, S)
forall a. Parser a -> S -> Either String (a, S)
unParser Parser a
m (ByteString -> S
initState ByteString
str) of
  Left String
e -> String -> Either String a
forall a b. a -> Either a b
Left String
e
  Right (a
a, S
_) -> a -> Either String a
forall a b. b -> Either a b
Right a
a

-- | Run the Get monad applies a 'get'-based parser on the input

-- ByteString. Additional to the result of get it returns the number of

-- consumed bytes and the rest of the input.

runParserState :: Parser a -> L.ByteString -> Int64 -> Either String (a, L.ByteString, Int64)
runParserState :: forall a.
Parser a
-> ByteString -> Int64 -> Either String (a, ByteString, Int64)
runParserState Parser a
m ByteString
str Int64
off =
    case Parser a -> S -> Either String (a, S)
forall a. Parser a -> S -> Either String (a, S)
unParser Parser a
m (ByteString -> Int64 -> S
mkState ByteString
str Int64
off) of
      Left String
e -> String -> Either String (a, ByteString, Int64)
forall a b. a -> Either a b
Left String
e
      Right (a
a, ~(S ByteString
s ByteString
ss Int64
newOff)) -> (a, ByteString, Int64) -> Either String (a, ByteString, Int64)
forall a b. b -> Either a b
Right (a
a, ByteString
s ByteString -> ByteString -> ByteString
`bsJoin` ByteString
ss, Int64
newOff)

------------------------------------------------------------------------


choice :: [Parser a] -> Parser a
choice :: forall a. [Parser a] -> Parser a
choice = (Parser a -> Parser a -> Parser a)
-> Parser a -> [Parser a] -> Parser a
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl Parser a -> Parser a -> Parser a
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>) Parser a
forall a. Parser a
forall (m :: * -> *) a. MonadPlus m => m a
mzero

-- | Skip ahead @n@ bytes. Fails if fewer than @n@ bytes are available.

skip :: Word64 -> Parser ()
skip :: Word64 -> Parser ()
skip Word64
n = Int -> (ByteString -> ()) -> Parser ()
forall a. Int -> (ByteString -> a) -> Parser a
readN (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n) (() -> ByteString -> ()
forall a b. a -> b -> a
const ())

-- | Run @ga@, but return without consuming its input.

-- Fails if @ga@ fails.

lookAhead :: Parser a -> Parser a
lookAhead :: forall a. Parser a -> Parser a
lookAhead Parser a
ga = do
    s <- Parser S
get
    a <- ga
    put s
    return a

-- | Like 'lookAhead', but consume the input if @gma@ returns 'Just _'.

-- Fails if @gma@ fails.

lookAheadM :: Parser (Maybe a) -> Parser (Maybe a)
lookAheadM :: forall a. Parser (Maybe a) -> Parser (Maybe a)
lookAheadM Parser (Maybe a)
gma = do
    s <- Parser S
get
    ma <- gma
    when (isNothing ma) $ put s
    return ma

-- | Like 'lookAhead', but consume the input if @gea@ returns 'Right _'.

-- Fails if @gea@ fails.

lookAheadE :: Parser (Either a b) -> Parser (Either a b)
lookAheadE :: forall a b. Parser (Either a b) -> Parser (Either a b)
lookAheadE Parser (Either a b)
gea = do
    s <- Parser S
get
    ea <- gea
    case ea of
        Left a
_ -> S -> Parser ()
put S
s
        Either a b
_      -> () -> Parser ()
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    return ea

expect :: (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect :: forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect a -> Bool
f Parser a
p = do
  v <- Parser a
p
  when (not $ f v) $ fail $ show v ++ " was not expected."
  return v

getString :: Int -> Parser String
getString :: Int -> Parser String
getString Int
l = do
  bs <- Int64 -> Parser ByteString
getLazyByteString (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
l)
  return $! map B.w2c (L.unpack bs)

getStringNul :: Parser String
getStringNul :: Parser String
getStringNul = do
  bs <- Parser ByteString
getLazyByteStringNul
  return $! map B.w2c (L.unpack bs)

string :: String -> Parser String
string :: String -> Parser String
string String
s = (String -> Bool) -> Parser String -> Parser String
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (String
s String -> String -> Bool
forall a. Eq a => a -> a -> Bool
==) (Int -> Parser String
getString (Int -> Parser String) -> Int -> Parser String
forall a b. (a -> b) -> a -> b
$ String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s)

-- Utility


-- | Get the total number of bytes read to this point.

bytesRead :: Parser Int64
bytesRead :: Parser Int64
bytesRead = do
    S _ _ b <- Parser S
get
    return b

-- | Get the number of remaining unparsed bytes.

-- Useful for checking whether all input has been consumed.

-- Note that this forces the rest of the input.

remaining :: Parser Int64
remaining :: Parser Int64
remaining = do
    S s ss _ <- Parser S
get
    return $! (fromIntegral (B.length s) + L.length ss)

-- | Test whether all input has been consumed,

-- i.e. there are no remaining unparsed bytes.

isEmpty :: Parser Bool
isEmpty :: Parser Bool
isEmpty = do
    S s ss _ <- Parser S
get
    return $! (B.null s && L.null ss)

------------------------------------------------------------------------

-- Utility with ByteStrings


-- | An efficient 'get' method for strict ByteStrings. Fails if fewer

-- than @n@ bytes are left in the input.

getByteString :: Int -> Parser B.ByteString
getByteString :: Int -> Parser ByteString
getByteString Int
n = Int -> (ByteString -> ByteString) -> Parser ByteString
forall a. Int -> (ByteString -> a) -> Parser a
readN Int
n ByteString -> ByteString
forall a. a -> a
id

-- | An efficient 'get' method for lazy ByteStrings. Does not fail if fewer than

-- @n@ bytes are left in the input.

getLazyByteString :: Int64 -> Parser L.ByteString
getLazyByteString :: Int64 -> Parser ByteString
getLazyByteString Int64
n = do
    S s ss bytes <- Parser S
get
    let big = ByteString
s ByteString -> ByteString -> ByteString
`bsJoin` ByteString
ss
    case splitAtST n big of
      (ByteString
consume, ByteString
rest) -> do S -> Parser ()
put (S -> Parser ()) -> S -> Parser ()
forall a b. (a -> b) -> a -> b
$ ByteString -> Int64 -> S
mkState ByteString
rest (Int64
bytes Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int64
n)
                            ByteString -> Parser ByteString
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
consume

-- | Get a lazy ByteString that is terminated with a NUL byte. Fails

-- if it reaches the end of input without hitting a NUL.

getLazyByteStringNul :: Parser L.ByteString
getLazyByteStringNul :: Parser ByteString
getLazyByteStringNul = do
    S s ss bytes <- Parser S
get
    let big = ByteString
s ByteString -> ByteString -> ByteString
`bsJoin` ByteString
ss
        (consume, t) = L.break (== 0) big
        (h, rest) = L.splitAt 1 t
    when (L.null h) $ fail "too few bytes"
    put $ mkState rest (bytes + L.length consume + 1)
    return consume

-- | Get the remaining bytes as a lazy ByteString

getRemainingLazyByteString :: Parser L.ByteString
getRemainingLazyByteString :: Parser ByteString
getRemainingLazyByteString = do
    S s ss _ <- Parser S
get
    return $! (s `bsJoin` ss)

------------------------------------------------------------------------

-- Helpers


-- | Pull @n@ bytes from the input, as a strict ByteString.

getBytes :: Int -> Parser B.ByteString
getBytes :: Int -> Parser ByteString
getBytes Int
n = do
    S s ss bytes <- Parser S
get
    if n <= B.length s
        then do let (consume,rest) = B.splitAt n s
                put $! S rest ss (bytes + fromIntegral n)
                return $! consume
        else
              case L.splitAt (fromIntegral n) (s `bsJoin` ss) of
                (ByteString
consuming, ByteString
rest) ->
                    do let now :: ByteString
now = [ByteString] -> ByteString
B.concat ([ByteString] -> ByteString)
-> (ByteString -> [ByteString]) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [ByteString]
L.toChunks (ByteString -> ByteString) -> ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ ByteString
consuming
                       S -> Parser ()
put (S -> Parser ()) -> S -> Parser ()
forall a b. (a -> b) -> a -> b
$! ByteString -> Int64 -> S
mkState ByteString
rest (Int64
bytes Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n)
                       -- forces the next chunk before this one is returned

                       Bool -> Parser () -> Parser ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ByteString -> Int
B.length ByteString
now Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n) (Parser () -> Parser ()) -> Parser () -> Parser ()
forall a b. (a -> b) -> a -> b
$ String -> Parser ()
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"too few bytes"
                       ByteString -> Parser ByteString
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
now

bsJoin :: B.ByteString -> L.ByteString -> L.ByteString
bsJoin :: ByteString -> ByteString -> ByteString
bsJoin ByteString
bb ByteString
lb
    | ByteString -> Bool
B.null ByteString
bb = ByteString
lb
    | Bool
otherwise = ByteString -> ByteString -> ByteString
L.Chunk ByteString
bb ByteString
lb

-- | Split a ByteString. If the first result is consumed before the --

-- second, this runs in constant heap space.

--

-- You must force the returned tuple for that to work, e.g.

--

-- > case splitAtST n xs of

-- >    (ys,zs) -> consume ys ... consume zs

--

splitAtST :: Int64 -> L.ByteString -> (L.ByteString, L.ByteString)
splitAtST :: Int64 -> ByteString -> (ByteString, ByteString)
splitAtST Int64
i ByteString
ps | Int64
i Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Int64
0 = (ByteString
L.empty, ByteString
ps)
splitAtST Int64
i ByteString
ps          = (forall s. ST s (ByteString, ByteString))
-> (ByteString, ByteString)
forall a. (forall s. ST s a) -> a
runST (
     do r  <- ByteString -> ST s (STRef s ByteString)
forall a s. a -> ST s (STRef s a)
newSTRef ByteString
forall a. HasCallStack => a
undefined
        xs <- first r i ps
        ys <- unsafeInterleaveST (readSTRef r)
        return (xs, ys))

  where
        first :: STRef s ByteString -> Int64 -> ByteString -> ST s ByteString
first STRef s ByteString
r Int64
0 xs :: ByteString
xs@(L.Chunk ByteString
_ ByteString
_) = STRef s ByteString -> ByteString -> ST s ()
forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s ByteString
r ByteString
xs    ST s () -> ST s ByteString -> ST s ByteString
forall a b. ST s a -> ST s b -> ST s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ByteString -> ST s ByteString
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
L.Empty
        first STRef s ByteString
r Int64
_ ByteString
L.Empty          = STRef s ByteString -> ByteString -> ST s ()
forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s ByteString
r ByteString
L.Empty ST s () -> ST s ByteString -> ST s ByteString
forall a b. ST s a -> ST s b -> ST s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ByteString -> ST s ByteString
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
L.Empty

        first STRef s ByteString
r Int64
n (L.Chunk ByteString
x ByteString
xs)
          | Int64
n Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
< Int64
l     = do STRef s ByteString -> ByteString -> ST s ()
forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s ByteString
r (ByteString -> ByteString -> ByteString
L.Chunk (Int -> ByteString -> ByteString
B.drop (Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
n) ByteString
x) ByteString
xs)
                           ByteString -> ST s ByteString
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> ST s ByteString) -> ByteString -> ST s ByteString
forall a b. (a -> b) -> a -> b
$! ByteString -> ByteString -> ByteString
L.Chunk (Int -> ByteString -> ByteString
B.take (Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
n) ByteString
x) ByteString
L.Empty
          | Bool
otherwise = do STRef s ByteString -> ByteString -> ST s ()
forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s ByteString
r (Int64 -> ByteString -> ByteString
L.drop (Int64
n Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- Int64
l) ByteString
xs)
                           (ByteString -> ByteString) -> ST s ByteString -> ST s ByteString
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (ByteString -> ByteString -> ByteString
L.Chunk ByteString
x) (ST s ByteString -> ST s ByteString)
-> ST s ByteString -> ST s ByteString
forall a b. (a -> b) -> a -> b
$ ST s ByteString -> ST s ByteString
forall s a. ST s a -> ST s a
unsafeInterleaveST (STRef s ByteString -> Int64 -> ByteString -> ST s ByteString
first STRef s ByteString
r (Int64
n Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- Int64
l) ByteString
xs)

         where l :: Int64
l = Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
B.length ByteString
x)

-- Pull n bytes from the input, and apply a parser to those bytes,

-- yielding a value. If less than @n@ bytes are available, fail with an

-- error. This wraps @getBytes@.

readN :: Int -> (B.ByteString -> a) -> Parser a
readN :: forall a. Int -> (ByteString -> a) -> Parser a
readN Int
n ByteString -> a
f = (ByteString -> a) -> Parser ByteString -> Parser a
forall a b. (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> a
f (Parser ByteString -> Parser a) -> Parser ByteString -> Parser a
forall a b. (a -> b) -> a -> b
$ Int -> Parser ByteString
getBytes Int
n


------------------------------------------------------------------------

-- Primtives


-- helper, get a raw Ptr onto a strict ByteString copied out of the

-- underlying lazy byteString. So many indirections from the raw parser

-- state that my head hurts...


getPtr :: Storable a => Int -> Parser a
getPtr :: forall a. Storable a => Int -> Parser a
getPtr Int
n = do
    (fp,o,_) <- Int
-> (ByteString -> (ForeignPtr Word8, Int, Int))
-> Parser (ForeignPtr Word8, Int, Int)
forall a. Int -> (ByteString -> a) -> Parser a
readN Int
n ByteString -> (ForeignPtr Word8, Int, Int)
B.toForeignPtr
    return . unsafePerformIO $ withForeignPtr fp $ \Ptr Word8
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 (ZonkAny 0) -> Ptr a) -> Ptr (ZonkAny 0) -> Ptr a
forall a b. (a -> b) -> a -> b
$ Ptr Word8
p Ptr Word8 -> Int -> Ptr (ZonkAny 0)
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
o)

------------------------------------------------------------------------


satisfy :: (Word8 -> Bool) -> Parser Word8
satisfy :: (Word8 -> Bool) -> Parser Word8
satisfy Word8 -> Bool
f = do
  w <- Parser Word8
getWord8
  guard (f w)
  return w

-- | Read a Word8 from the monad state

getWord8 :: Parser Word8
getWord8 :: Parser Word8
getWord8 = Int -> Parser Word8
forall a. Storable a => Int -> Parser a
getPtr (Word8 -> Int
forall a. Storable a => a -> Int
sizeOf (Word8
forall a. HasCallStack => a
undefined :: Word8))

word8 :: Word8 -> Parser Word8
word8 :: Word8 -> Parser Word8
word8 Word8
w = (Word8 -> Bool) -> Parser Word8 -> Parser Word8
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
==) Parser Word8
getWord8

-- | Read a Word16 in big endian format

getWord16be :: Parser Word16
getWord16be :: Parser Word16
getWord16be = do
    s <- Int -> (ByteString -> ByteString) -> Parser ByteString
forall a. Int -> (ByteString -> a) -> Parser a
readN Int
2 ByteString -> ByteString
forall a. a -> a
id
    return $! (fromIntegral (s `B.index` 0) `shiftL` 8) .|.
              (fromIntegral (s `B.index` 1))

word16be :: Word16 -> Parser Word16
word16be :: Word16 -> Parser Word16
word16be Word16
w = (Word16 -> Bool) -> Parser Word16 -> Parser Word16
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (Word16
w Word16 -> Word16 -> Bool
forall a. Eq a => a -> a -> Bool
==) Parser Word16
getWord16be

-- | Read a Word16 in little endian format

getWord16le :: Parser Word16
getWord16le :: Parser Word16
getWord16le = do
    s <- Int -> (ByteString -> ByteString) -> Parser ByteString
forall a. Int -> (ByteString -> a) -> Parser a
readN Int
2 ByteString -> ByteString
forall a. a -> a
id
    return $! (fromIntegral (s `B.index` 1) `shiftL` 8) .|.
              (fromIntegral (s `B.index` 0) )

word16le :: Word16 -> Parser Word16
word16le :: Word16 -> Parser Word16
word16le Word16
w = (Word16 -> Bool) -> Parser Word16 -> Parser Word16
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (Word16
w Word16 -> Word16 -> Bool
forall a. Eq a => a -> a -> Bool
==) Parser Word16
getWord16le

-- | Read a 24 bit word into Word32 in big endian format

getWord24be :: Parser Word32
getWord24be :: Parser Word32
getWord24be = do
    s <- Int -> (ByteString -> ByteString) -> Parser ByteString
forall a. Int -> (ByteString -> a) -> Parser a
readN Int
3 ByteString -> ByteString
forall a. a -> a
id
    return $! (fromIntegral (s `B.index` 0) `shiftL` 16) .|.
              (fromIntegral (s `B.index` 1) `shiftL`  8) .|.
              (fromIntegral (s `B.index` 2) )

word24be :: Word32 -> Parser Word32
word24be :: Word32 -> Parser Word32
word24be Word32
w = (Word32 -> Bool) -> Parser Word32 -> Parser Word32
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (Word32
w Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
==) Parser Word32
getWord24be

getWord24le :: Parser Word32
getWord24le :: Parser Word32
getWord24le = do
    s <- Int -> (ByteString -> ByteString) -> Parser ByteString
forall a. Int -> (ByteString -> a) -> Parser a
readN Int
3 ByteString -> ByteString
forall a. a -> a
id
    return $! (fromIntegral (s `B.index` 2) `shiftL` 16) .|.
              (fromIntegral (s `B.index` 1) `shiftL`  8) .|.
              (fromIntegral (s `B.index` 0) )

word24le :: Word32 -> Parser Word32
word24le :: Word32 -> Parser Word32
word24le Word32
w = (Word32 -> Bool) -> Parser Word32 -> Parser Word32
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (Word32
w Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
==) Parser Word32
getWord24le

-- | Read a Word32 in big endian format

getWord32be :: Parser Word32
getWord32be :: Parser Word32
getWord32be = do
    s <- Int -> (ByteString -> ByteString) -> Parser ByteString
forall a. Int -> (ByteString -> a) -> Parser a
readN Int
4 ByteString -> ByteString
forall a. a -> a
id
    return $! (fromIntegral (s `B.index` 0) `shiftL` 24) .|.
              (fromIntegral (s `B.index` 1) `shiftL` 16) .|.
              (fromIntegral (s `B.index` 2) `shiftL`  8) .|.
              (fromIntegral (s `B.index` 3) )

word32be :: Word32 -> Parser Word32
word32be :: Word32 -> Parser Word32
word32be Word32
w = (Word32 -> Bool) -> Parser Word32 -> Parser Word32
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (Word32
w Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
==) Parser Word32
getWord32be

-- | Read a Word32 in little endian format

getWord32le :: Parser Word32
getWord32le :: Parser Word32
getWord32le = do
    s <- Int -> (ByteString -> ByteString) -> Parser ByteString
forall a. Int -> (ByteString -> a) -> Parser a
readN Int
4 ByteString -> ByteString
forall a. a -> a
id
    return $! (fromIntegral (s `B.index` 3) `shiftL` 24) .|.
              (fromIntegral (s `B.index` 2) `shiftL` 16) .|.
              (fromIntegral (s `B.index` 1) `shiftL`  8) .|.
              (fromIntegral (s `B.index` 0) )

word32le :: Word32 -> Parser Word32
word32le :: Word32 -> Parser Word32
word32le Word32
w = (Word32 -> Bool) -> Parser Word32 -> Parser Word32
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (Word32
w Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
==) Parser Word32
getWord32le


-- | Read a Word64 in big endian format

getWord64be :: Parser Word64
getWord64be :: Parser Word64
getWord64be = do
    s <- Int -> (ByteString -> ByteString) -> Parser ByteString
forall a. Int -> (ByteString -> a) -> Parser a
readN Int
8 ByteString -> ByteString
forall a. a -> a
id
    return $! (fromIntegral (s `B.index` 0) `shiftL` 56) .|.
              (fromIntegral (s `B.index` 1) `shiftL` 48) .|.
              (fromIntegral (s `B.index` 2) `shiftL` 40) .|.
              (fromIntegral (s `B.index` 3) `shiftL` 32) .|.
              (fromIntegral (s `B.index` 4) `shiftL` 24) .|.
              (fromIntegral (s `B.index` 5) `shiftL` 16) .|.
              (fromIntegral (s `B.index` 6) `shiftL`  8) .|.
              (fromIntegral (s `B.index` 7) )

word64be :: Word64 -> Parser Word64
word64be :: Word64 -> Parser Word64
word64be Word64
w = (Word64 -> Bool) -> Parser Word64 -> Parser Word64
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (Word64
w Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
==) Parser Word64
getWord64be

-- | Read a Word64 in little endian format

getWord64le :: Parser Word64
getWord64le :: Parser Word64
getWord64le = do
    s <- Int -> (ByteString -> ByteString) -> Parser ByteString
forall a. Int -> (ByteString -> a) -> Parser a
readN Int
8 ByteString -> ByteString
forall a. a -> a
id
    return $! (fromIntegral (s `B.index` 7) `shiftL` 56) .|.
              (fromIntegral (s `B.index` 6) `shiftL` 48) .|.
              (fromIntegral (s `B.index` 5) `shiftL` 40) .|.
              (fromIntegral (s `B.index` 4) `shiftL` 32) .|.
              (fromIntegral (s `B.index` 3) `shiftL` 24) .|.
              (fromIntegral (s `B.index` 2) `shiftL` 16) .|.
              (fromIntegral (s `B.index` 1) `shiftL`  8) .|.
              (fromIntegral (s `B.index` 0) )

word64le :: Word64 -> Parser Word64
word64le :: Word64 -> Parser Word64
word64le Word64
w = (Word64 -> Bool) -> Parser Word64 -> Parser Word64
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (Word64
w Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
==) Parser Word64
getWord64le
------------------------------------------------------------------------

getInt8 :: Parser Int8
getInt8 :: Parser Int8
getInt8 = Parser Word8
getWord8 Parser Word8 -> (Word8 -> Parser Int8) -> Parser Int8
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int8 -> Parser Int8
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int8 -> Parser Int8) -> (Word8 -> Int8) -> Word8 -> Parser Int8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Int8
forall a b. (Integral a, Num b) => a -> b
fromIntegral

int8 :: Int8 -> Parser Int8
int8 :: Int8 -> Parser Int8
int8 Int8
i = (Int8 -> Bool) -> Parser Int8 -> Parser Int8
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (Int8
i Int8 -> Int8 -> Bool
forall a. Eq a => a -> a -> Bool
==) Parser Int8
getInt8

getInt16le :: Parser Int16
getInt16le :: Parser Int16
getInt16le = Parser Word16
getWord16le Parser Word16 -> (Word16 -> Parser Int16) -> Parser Int16
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int16 -> Parser Int16
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int16 -> Parser Int16)
-> (Word16 -> Int16) -> Word16 -> Parser Int16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word16 -> Int16
forall a b. (Integral a, Num b) => a -> b
fromIntegral

int16le :: Int16 -> Parser Int16
int16le :: Int16 -> Parser Int16
int16le Int16
i = (Int16 -> Bool) -> Parser Int16 -> Parser Int16
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (Int16
i Int16 -> Int16 -> Bool
forall a. Eq a => a -> a -> Bool
==) Parser Int16
getInt16le

getInt16be :: Parser Int16
getInt16be :: Parser Int16
getInt16be = Parser Word16
getWord16be Parser Word16 -> (Word16 -> Parser Int16) -> Parser Int16
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int16 -> Parser Int16
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int16 -> Parser Int16)
-> (Word16 -> Int16) -> Word16 -> Parser Int16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word16 -> Int16
forall a b. (Integral a, Num b) => a -> b
fromIntegral

int16be :: Int16 -> Parser Int16
int16be :: Int16 -> Parser Int16
int16be Int16
i = (Int16 -> Bool) -> Parser Int16 -> Parser Int16
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (Int16
i Int16 -> Int16 -> Bool
forall a. Eq a => a -> a -> Bool
==) Parser Int16
getInt16be

getInt32le :: Parser Int32
getInt32le :: Parser Int32
getInt32le = Parser Word32
getWord32le Parser Word32 -> (Word32 -> Parser Int32) -> Parser Int32
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int32 -> Parser Int32
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int32 -> Parser Int32)
-> (Word32 -> Int32) -> Word32 -> Parser Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral

int32le :: Int32 -> Parser Int32
int32le :: Int32 -> Parser Int32
int32le Int32
i = (Int32 -> Bool) -> Parser Int32 -> Parser Int32
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (Int32
i Int32 -> Int32 -> Bool
forall a. Eq a => a -> a -> Bool
==) Parser Int32
getInt32le

getInt32be :: Parser Int32
getInt32be :: Parser Int32
getInt32be = Parser Word32
getWord32be Parser Word32 -> (Word32 -> Parser Int32) -> Parser Int32
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int32 -> Parser Int32
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int32 -> Parser Int32)
-> (Word32 -> Int32) -> Word32 -> Parser Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral

int32be :: Int32 -> Parser Int32
int32be :: Int32 -> Parser Int32
int32be Int32
i = (Int32 -> Bool) -> Parser Int32 -> Parser Int32
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (Int32
i Int32 -> Int32 -> Bool
forall a. Eq a => a -> a -> Bool
==) Parser Int32
getInt32be

getInt64le :: Parser Int64
getInt64le :: Parser Int64
getInt64le = Parser Word64
getWord64le Parser Word64 -> (Word64 -> Parser Int64) -> Parser Int64
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int64 -> Parser Int64
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int64 -> Parser Int64)
-> (Word64 -> Int64) -> Word64 -> Parser Int64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral

int64le :: Int64 -> Parser Int64
int64le :: Int64 -> Parser Int64
int64le Int64
i = (Int64 -> Bool) -> Parser Int64 -> Parser Int64
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (Int64
i Int64 -> Int64 -> Bool
forall a. Eq a => a -> a -> Bool
==) Parser Int64
getInt64le

getInt64be :: Parser Int64
getInt64be :: Parser Int64
getInt64be = Parser Word64
getWord64be Parser Word64 -> (Word64 -> Parser Int64) -> Parser Int64
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int64 -> Parser Int64
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int64 -> Parser Int64)
-> (Word64 -> Int64) -> Word64 -> Parser Int64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral

int64be :: Int64 -> Parser Int64
int64be :: Int64 -> Parser Int64
int64be Int64
i = (Int64 -> Bool) -> Parser Int64 -> Parser Int64
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (Int64
i Int64 -> Int64 -> Bool
forall a. Eq a => a -> a -> Bool
==) Parser Int64
getInt64be

------------------------------------------------------------------------

-- Host-endian reads


-- | /O(1)./ Read a single native machine word. The word is read in

-- host order, host endian form, for the machine you're on. On a 64 bit

-- machine the Word is an 8 byte value, on a 32 bit machine, 4 bytes.

getWordHost :: Parser Word
getWordHost :: Parser Word
getWordHost = Int -> Parser Word
forall a. Storable a => Int -> Parser a
getPtr (Word -> Int
forall a. Storable a => a -> Int
sizeOf (Word
forall a. HasCallStack => a
undefined :: Word))

wordHost :: Word -> Parser Word
wordHost :: Word -> Parser Word
wordHost Word
w = (Word -> Bool) -> Parser Word -> Parser Word
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (Word
w Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
==) Parser Word
getWordHost

-- | /O(1)./ Read a 2 byte Word16 in native host order and host endianness.

getWord16host :: Parser Word16
getWord16host :: Parser Word16
getWord16host = Int -> Parser Word16
forall a. Storable a => Int -> Parser a
getPtr (Word16 -> Int
forall a. Storable a => a -> Int
sizeOf (Word16
forall a. HasCallStack => a
undefined :: Word16))

word16host :: Word16 -> Parser Word16
word16host :: Word16 -> Parser Word16
word16host Word16
w = (Word16 -> Bool) -> Parser Word16 -> Parser Word16
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (Word16
w Word16 -> Word16 -> Bool
forall a. Eq a => a -> a -> Bool
==) Parser Word16
getWord16host

-- | /O(1)./ Read a Word32 in native host order and host endianness.

getWord32host :: Parser Word32
getWord32host :: Parser Word32
getWord32host = Int -> Parser Word32
forall a. Storable a => Int -> Parser a
getPtr  (Word32 -> Int
forall a. Storable a => a -> Int
sizeOf (Word32
forall a. HasCallStack => a
undefined :: Word32))

word32host :: Word32 -> Parser Word32
word32host :: Word32 -> Parser Word32
word32host Word32
w = (Word32 -> Bool) -> Parser Word32 -> Parser Word32
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (Word32
w Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
==) Parser Word32
getWord32host

-- | /O(1)./ Read a Word64 in native host order and host endianess.

getWord64host   :: Parser Word64
getWord64host :: Parser Word64
getWord64host = Int -> Parser Word64
forall a. Storable a => Int -> Parser a
getPtr  (Word64 -> Int
forall a. Storable a => a -> Int
sizeOf (Word64
forall a. HasCallStack => a
undefined :: Word64))

word64host :: Word64 -> Parser Word64
word64host :: Word64 -> Parser Word64
word64host Word64
w = (Word64 -> Bool) -> Parser Word64 -> Parser Word64
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (Word64
w Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
==) Parser Word64
getWord64host

-- Variable length numbers


getVarLenBe :: Parser Word64
getVarLenBe :: Parser Word64
getVarLenBe = Word64 -> Parser Word64
f Word64
0
  where
  f :: Word64 -> Parser Word64
  f :: Word64 -> Parser Word64
f Word64
acc =  do
    w <- Parser Word8
getWord8 Parser Word8 -> (Word8 -> Parser Word64) -> Parser Word64
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Word64 -> Parser Word64
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return (Word64 -> Parser Word64)
-> (Word8 -> Word64) -> Word8 -> Parser Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral
    if testBit w 7
      then f      $! (shiftL acc 7) .|. (clearBit w 7)
      else return $! (shiftL acc 7) .|. w

varLenBe :: Word64 -> Parser Word64
varLenBe :: Word64 -> Parser Word64
varLenBe Word64
a = (Word64 -> Bool) -> Parser Word64 -> Parser Word64
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (Word64
a Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
==) Parser Word64
getVarLenBe

getVarLenLe :: Parser Word64
getVarLenLe :: Parser Word64
getVarLenLe = do
  w <- Parser Word8
getWord8 Parser Word8 -> (Word8 -> Parser Word64) -> Parser Word64
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Word64 -> Parser Word64
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return (Word64 -> Parser Word64)
-> (Word8 -> Word64) -> Word8 -> Parser Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral
  if testBit w 7
    then do
      w' <- getVarLenLe
      return $! (clearBit w 7) .|. (shiftL w' 7)
    else return $! w

varLenLe :: Word64 -> Parser Word64
varLenLe :: Word64 -> Parser Word64
varLenLe Word64
a = (Word64 -> Bool) -> Parser Word64 -> Parser Word64
forall a. (Show a, Eq a) => (a -> Bool) -> Parser a -> Parser a
expect (Word64
a Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
==) Parser Word64
getVarLenLe