module Ptr.Peek where
import qualified Ptr.IO as A
import qualified Ptr.Parse as C
import qualified Ptr.ParseUnbound as D
import qualified Ptr.PokeAndPeek as B
import Ptr.Prelude hiding (take)
data Peek output
= Peek {-# UNPACK #-} !Int !(Ptr Word8 -> IO output)
instance Functor Peek where
{-# INLINE fmap #-}
fmap :: forall a b. (a -> b) -> Peek a -> Peek b
fmap a -> b
fn (Peek Int
size Ptr Word8 -> IO a
io) =
forall output. Int -> (Ptr Word8 -> IO output) -> Peek output
Peek Int
size (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
fn forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Ptr Word8 -> IO a
io)
instance Applicative Peek where
{-# INLINE pure #-}
pure :: forall a. a -> Peek a
pure a
x =
forall output. Int -> (Ptr Word8 -> IO output) -> Peek output
Peek Int
0 (forall a b. a -> b -> a
const (forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x))
{-# INLINE (<*>) #-}
<*> :: forall a b. Peek (a -> b) -> Peek a -> Peek b
(<*>) (Peek Int
leftSize Ptr Word8 -> IO (a -> b)
leftIO) (Peek Int
rightSize Ptr Word8 -> IO a
rightIO) =
forall output. Int -> (Ptr Word8 -> IO output) -> Peek output
Peek (Int
leftSize forall a. Num a => a -> a -> a
+ Int
rightSize) Ptr Word8 -> IO b
io
where
io :: Ptr Word8 -> IO b
io Ptr Word8
ptr =
Ptr Word8 -> IO (a -> b)
leftIO Ptr Word8
ptr forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Ptr Word8 -> IO a
rightIO (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
ptr Int
leftSize)
{-# INLINE int8 #-}
int8 :: Peek Int8
int8 :: Peek Int8
int8 =
{-# SCC "int8" #-}
forall output. Int -> (Ptr Word8 -> IO output) -> Peek output
Peek Int
1 forall storable. Storable storable => Ptr Word8 -> IO storable
A.peekStorable
{-# INLINE beInt16 #-}
beInt16 :: Peek Int16
beInt16 :: Peek Int16
beInt16 =
{-# SCC "beInt16" #-}
forall output. Int -> (Ptr Word8 -> IO output) -> Peek output
Peek Int
2 Ptr Word8 -> IO Int16
A.peekBEInt16
{-# INLINE beInt32 #-}
beInt32 :: Peek Int32
beInt32 :: Peek Int32
beInt32 =
{-# SCC "beInt32" #-}
forall output. Int -> (Ptr Word8 -> IO output) -> Peek output
Peek Int
4 Ptr Word8 -> IO Int32
A.peekBEInt32
{-# INLINE beInt64 #-}
beInt64 :: Peek Int64
beInt64 :: Peek Int64
beInt64 =
{-# SCC "beInt64" #-}
forall output. Int -> (Ptr Word8 -> IO output) -> Peek output
Peek Int
8 Ptr Word8 -> IO Int64
A.peekBEInt64
{-# INLINE leInt16 #-}
leInt16 :: Peek Int16
leInt16 :: Peek Int16
leInt16 =
{-# SCC "leInt16" #-}
forall output. Int -> (Ptr Word8 -> IO output) -> Peek output
Peek Int
2 Ptr Word8 -> IO Int16
A.peekLEInt16
{-# INLINE leInt32 #-}
leInt32 :: Peek Int32
leInt32 :: Peek Int32
leInt32 =
{-# SCC "leInt32" #-}
forall output. Int -> (Ptr Word8 -> IO output) -> Peek output
Peek Int
4 Ptr Word8 -> IO Int32
A.peekLEInt32
{-# INLINE leInt64 #-}
leInt64 :: Peek Int64
leInt64 :: Peek Int64
leInt64 =
{-# SCC "leInt64" #-}
forall output. Int -> (Ptr Word8 -> IO output) -> Peek output
Peek Int
8 Ptr Word8 -> IO Int64
A.peekLEInt64
{-# INLINE word8 #-}
word8 :: Peek Word8
word8 :: Peek Word8
word8 =
{-# SCC "word8" #-}
forall output. Int -> (Ptr Word8 -> IO output) -> Peek output
Peek Int
1 Ptr Word8 -> IO Word8
A.peekWord8
{-# INLINE beWord16 #-}
beWord16 :: Peek Word16
beWord16 :: Peek Word16
beWord16 =
{-# SCC "beWord16" #-}
forall output. Int -> (Ptr Word8 -> IO output) -> Peek output
Peek Int
2 Ptr Word8 -> IO Word16
A.peekBEWord16
{-# INLINE beWord32 #-}
beWord32 :: Peek Word32
beWord32 :: Peek Word32
beWord32 =
{-# SCC "beWord32" #-}
forall output. Int -> (Ptr Word8 -> IO output) -> Peek output
Peek Int
4 Ptr Word8 -> IO Word32
A.peekBEWord32
{-# INLINE beWord64 #-}
beWord64 :: Peek Word64
beWord64 :: Peek Word64
beWord64 =
{-# SCC "beWord64" #-}
forall output. Int -> (Ptr Word8 -> IO output) -> Peek output
Peek Int
8 Ptr Word8 -> IO Word64
A.peekBEWord64
{-# INLINE leWord16 #-}
leWord16 :: Peek Word16
leWord16 :: Peek Word16
leWord16 =
{-# SCC "leWord16" #-}
forall output. Int -> (Ptr Word8 -> IO output) -> Peek output
Peek Int
2 Ptr Word8 -> IO Word16
A.peekLEWord16
{-# INLINE leWord32 #-}
leWord32 :: Peek Word32
leWord32 :: Peek Word32
leWord32 =
{-# SCC "leWord32" #-}
forall output. Int -> (Ptr Word8 -> IO output) -> Peek output
Peek Int
4 Ptr Word8 -> IO Word32
A.peekLEWord32
{-# INLINE leWord64 #-}
leWord64 :: Peek Word64
leWord64 :: Peek Word64
leWord64 =
{-# SCC "leWord64" #-}
forall output. Int -> (Ptr Word8 -> IO output) -> Peek output
Peek Int
8 Ptr Word8 -> IO Word64
A.peekLEWord64
{-# INLINE bytes #-}
bytes :: Int -> Peek ByteString
bytes :: Int -> Peek ByteString
bytes !Int
amount =
{-# SCC "bytes" #-}
forall output. Int -> (Ptr Word8 -> IO output) -> Peek output
Peek Int
amount (\Ptr Word8
ptr -> Ptr Word8 -> Int -> IO ByteString
A.peekBytes Ptr Word8
ptr Int
amount)
{-# INLINE shortByteString #-}
shortByteString :: Int -> Peek ShortByteString
shortByteString :: Int -> Peek ShortByteString
shortByteString !Int
amount =
{-# SCC "shortByteString" #-}
forall output. Int -> (Ptr Word8 -> IO output) -> Peek output
Peek Int
amount (\Ptr Word8
ptr -> Ptr Word8 -> Int -> IO ShortByteString
A.peekShortByteString Ptr Word8
ptr Int
amount)
{-# INLINE pokeAndPeek #-}
pokeAndPeek :: B.PokeAndPeek input output -> Peek output
pokeAndPeek :: forall input output. PokeAndPeek input output -> Peek output
pokeAndPeek (B.PokeAndPeek Int
size Ptr Word8 -> input -> IO ()
_ Ptr Word8 -> IO output
io) =
{-# SCC "pokeAndPeek" #-}
forall output. Int -> (Ptr Word8 -> IO output) -> Peek output
Peek Int
size Ptr Word8 -> IO output
io
{-# INLINE parse #-}
parse :: Int -> C.Parse a -> (Int -> a) -> (Text -> a) -> Peek a
parse :: forall a. Int -> Parse a -> (Int -> a) -> (Text -> a) -> Peek a
parse Int
amount (C.Parse Int
-> Ptr Word8
-> forall result.
(Int -> IO result)
-> (Text -> IO result)
-> (a -> Int -> Ptr Word8 -> IO result)
-> IO result
parseIO) Int -> a
eoi Text -> a
error =
{-# SCC "parse" #-}
forall output. Int -> (Ptr Word8 -> IO output) -> Peek output
Peek Int
amount forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ptr ->
Int
-> Ptr Word8
-> forall result.
(Int -> IO result)
-> (Text -> IO result)
-> (a -> Int -> Ptr Word8 -> IO result)
-> IO result
parseIO Int
amount Ptr Word8
ptr (forall (m :: * -> *) a. Monad m => a -> m a
return forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Int -> a
eoi) (forall (m :: * -> *) a. Monad m => a -> m a
return forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> a
error) (\a
result Int
_ Ptr Word8
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return a
result)
{-# INLINE parseUnbound #-}
parseUnbound :: Int -> D.ParseUnbound a -> (Int -> a) -> (Text -> a) -> Peek a
parseUnbound :: forall a.
Int -> ParseUnbound a -> (Int -> a) -> (Text -> a) -> Peek a
parseUnbound Int
sizeBound (D.ParseUnbound Ptr Word8
-> forall result.
(Text -> IO result) -> (a -> Int -> IO result) -> IO result
parseIO) Int -> a
eoi Text -> a
error =
{-# SCC "parse" #-}
forall output. Int -> (Ptr Word8 -> IO output) -> Peek output
Peek Int
sizeBound forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ptr ->
Ptr Word8
-> forall result.
(Text -> IO result) -> (a -> Int -> IO result) -> IO result
parseIO
Ptr Word8
ptr
(forall (m :: * -> *) a. Monad m => a -> m a
return forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> a
error)
( \a
result Int
size ->
if Int
size forall a. Ord a => a -> a -> Bool
<= Int
sizeBound
then forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> a
eoi (Int
size forall a. Num a => a -> a -> a
- Int
sizeBound))
else forall (m :: * -> *) a. Monad m => a -> m a
return a
result
)
{-# INLINE peekAmountAndParse #-}
peekAmountAndParse :: Peek Int -> C.Parse a -> (Int -> a) -> (Text -> a) -> Peek (Peek a)
peekAmountAndParse :: forall a.
Peek Int -> Parse a -> (Int -> a) -> (Text -> a) -> Peek (Peek a)
peekAmountAndParse Peek Int
peekAmount Parse a
parse_ Int -> a
eoi Text -> a
error =
{-# SCC "peekAmountAndParse" #-}
forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Peek Int
peekAmount forall a b. (a -> b) -> a -> b
$ \Int
amount ->
forall a. Int -> Parse a -> (Int -> a) -> (Text -> a) -> Peek a
parse Int
amount Parse a
parse_ Int -> a
eoi Text -> a
error