module Ptr.Read
( Read,
Status (..),
runOnPtr,
runOnByteString,
runOnByteStringFinishing,
skip,
skipWhile,
byteString,
byteStringWhile,
foldlWhile',
word8,
int16InBe,
int32InBe,
int64InBe,
nullTerminatedByteString,
asciiIntegral,
)
where
import qualified Data.ByteString.Internal as ByteString
import qualified Ptr.IO as IO
import Ptr.Prelude hiding (Read)
import qualified Ptr.Util.ByteString as ByteString
import qualified Ptr.Util.Word8Predicates as Word8Predicates
newtype Read a
= Read (Ptr Word8 -> Ptr Word8 -> IO (Status a))
instance Functor Read where
fmap :: forall a b. (a -> b) -> Read a -> Read b
fmap a -> b
f (Read Ptr Word8 -> Ptr Word8 -> IO (Status a)
cont) =
forall a. (Ptr Word8 -> Ptr Word8 -> IO (Status a)) -> Read a
Read (\Ptr Word8
start Ptr Word8
end -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) (Ptr Word8 -> Ptr Word8 -> IO (Status a)
cont Ptr Word8
start Ptr Word8
end))
instance Applicative Read where
pure :: forall a. a -> Read a
pure a
a =
forall a. (Ptr Word8 -> Ptr Word8 -> IO (Status a)) -> Read a
Read (\Ptr Word8
s Ptr Word8
e -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a. Ptr Word8 -> a -> Status a
FinishedStatus Ptr Word8
s a
a))
Read Ptr Word8 -> Ptr Word8 -> IO (Status (a -> b))
lGetStatus <*> :: forall a b. Read (a -> b) -> Read a -> Read b
<*> Read Ptr Word8 -> Ptr Word8 -> IO (Status a)
rGetStatus =
forall a. (Ptr Word8 -> Ptr Word8 -> IO (Status a)) -> Read a
Read forall a b. (a -> b) -> a -> b
$ \Ptr Word8
start Ptr Word8
end -> do
Ptr Word8 -> Ptr Word8 -> IO (Status (a -> b))
lGetStatus Ptr Word8
start Ptr Word8
end forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
FinishedStatus Ptr Word8
lAfter a -> b
lRes ->
Ptr Word8 -> Ptr Word8 -> IO (Status a)
rGetStatus Ptr Word8
lAfter Ptr Word8
end forall a b. a -> (a -> b) -> b
& forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
lRes)
UnfinishedStatus Read (a -> b)
lNextPeek ->
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Read a -> Status a
UnfinishedStatus (Read (a -> b)
lNextPeek forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. (Ptr Word8 -> Ptr Word8 -> IO (Status a)) -> Read a
Read Ptr Word8 -> Ptr Word8 -> IO (Status a)
rGetStatus))
instance Monad Read where
return :: forall a. a -> Read a
return = forall (f :: * -> *) a. Applicative f => a -> f a
pure
Read Ptr Word8 -> Ptr Word8 -> IO (Status a)
lGetStatus >>= :: forall a b. Read a -> (a -> Read b) -> Read b
>>= a -> Read b
k =
forall a. (Ptr Word8 -> Ptr Word8 -> IO (Status a)) -> Read a
Read forall a b. (a -> b) -> a -> b
$ \Ptr Word8
start Ptr Word8
end ->
Ptr Word8 -> Ptr Word8 -> IO (Status a)
lGetStatus Ptr Word8
start Ptr Word8
end forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
FinishedStatus Ptr Word8
lAfter a
lRes ->
a -> Read b
k a
lRes forall a b. a -> (a -> b) -> b
& \(Read Ptr Word8 -> Ptr Word8 -> IO (Status b)
rGetStatus) -> Ptr Word8 -> Ptr Word8 -> IO (Status b)
rGetStatus Ptr Word8
lAfter Ptr Word8
end
UnfinishedStatus Read a
lNextPeek ->
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Read a -> Status a
UnfinishedStatus (Read a
lNextPeek forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> Read b
k))
data Status a
= FinishedStatus {-# UNPACK #-} !(Ptr Word8) a
| UnfinishedStatus (Read a)
deriving (forall a b. a -> Status b -> Status a
forall a b. (a -> b) -> Status a -> Status b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Status b -> Status a
$c<$ :: forall a b. a -> Status b -> Status a
fmap :: forall a b. (a -> b) -> Status a -> Status b
$cfmap :: forall a b. (a -> b) -> Status a -> Status b
Functor)
runOnPtr :: Read a -> Ptr Word8 -> Ptr Word8 -> IO (Status a)
runOnPtr :: forall a. Read a -> Ptr Word8 -> Ptr Word8 -> IO (Status a)
runOnPtr =
coerce :: forall a b. Coercible a b => a -> b
coerce
runOnByteString :: Read a -> ByteString -> Either (Read a) (a, ByteString)
runOnByteString :: forall a. Read a -> ByteString -> Either (Read a) (a, ByteString)
runOnByteString (Read Ptr Word8 -> Ptr Word8 -> IO (Status a)
read) (ByteString.PS ForeignPtr Word8
bsFp Int
bsOff Int
bsSize) =
forall a. IO a -> a
unsafePerformIO
forall a b. (a -> b) -> a -> b
$ forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
bsFp
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
p ->
let startP :: Ptr Word8
startP = forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
p Int
bsOff
endP :: Ptr Word8
endP = forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
startP Int
bsSize
in Ptr Word8 -> Ptr Word8 -> IO (Status a)
read Ptr Word8
startP Ptr Word8
endP forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
FinishedStatus Ptr Word8
newStartP a
res ->
let newBsOff :: Int
newBsOff = forall a b. Ptr a -> Ptr b -> Int
minusPtr Ptr Word8
newStartP Ptr Word8
p
newBs :: ByteString
newBs = ForeignPtr Word8 -> Int -> Int -> ByteString
ByteString.PS ForeignPtr Word8
bsFp Int
newBsOff (Int
bsSize forall a. Num a => a -> a -> a
- (Int
newBsOff forall a. Num a => a -> a -> a
- Int
bsOff))
in forall a b. b -> Either a b
Right (a
res, ByteString
newBs)
UnfinishedStatus Read a
next ->
forall a b. a -> Either a b
Left Read a
next
runOnByteStringFinishing :: Read a -> ByteString -> Maybe a
runOnByteStringFinishing :: forall a. Read a -> ByteString -> Maybe a
runOnByteStringFinishing Read a
read ByteString
byteString =
forall a. Read a -> ByteString -> Either (Read a) (a, ByteString)
runOnByteString Read a
read ByteString
byteString
forall a b. a -> (a -> b) -> b
& forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const forall a. Maybe a
Nothing) (forall a. a -> Maybe a
Just forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a b. (a, b) -> a
fst)
skip ::
Int ->
Read ()
skip :: Int -> Read ()
skip =
forall a. (Ptr Word8 -> Ptr Word8 -> IO (Status a)) -> Read a
Read forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Int -> Ptr Word8 -> Ptr Word8 -> IO (Status ())
loop
where
loop :: Int -> Ptr Word8 -> Ptr Word8 -> IO (Status ())
loop Int
needed Ptr Word8
start Ptr Word8
end =
if Ptr Word8
post forall a. Ord a => a -> a -> Bool
<= Ptr Word8
end
then forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Ptr Word8 -> a -> Status a
FinishedStatus Ptr Word8
post ())
else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Read a -> Status a
UnfinishedStatus (forall a. (Ptr Word8 -> Ptr Word8 -> IO (Status a)) -> Read a
Read (Int -> Ptr Word8 -> Ptr Word8 -> IO (Status ())
loop Int
nextNeeded)))
where
post :: Ptr Word8
post = forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
start Int
needed
nextNeeded :: Int
nextNeeded = forall a b. Ptr a -> Ptr b -> Int
minusPtr Ptr Word8
post Ptr Word8
end
skipWhile :: (Word8 -> Bool) -> Read ()
skipWhile :: (Word8 -> Bool) -> Read ()
skipWhile Word8 -> Bool
predicate =
forall a. (Ptr Word8 -> Ptr Word8 -> IO (Status a)) -> Read a
Read Ptr Word8 -> Ptr Word8 -> IO (Status ())
loop
where
loop :: Ptr Word8 -> Ptr Word8 -> IO (Status ())
loop Ptr Word8
start Ptr Word8
end =
if Ptr Word8
post forall a. Ord a => a -> a -> Bool
<= Ptr Word8
end
then do
Word8
w <- Ptr Word8 -> IO Word8
IO.peekWord8 Ptr Word8
start
if Word8 -> Bool
predicate Word8
w
then Ptr Word8 -> Ptr Word8 -> IO (Status ())
loop Ptr Word8
post Ptr Word8
end
else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Ptr Word8 -> a -> Status a
FinishedStatus Ptr Word8
start ())
else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Read a -> Status a
UnfinishedStatus (forall a. (Ptr Word8 -> Ptr Word8 -> IO (Status a)) -> Read a
Read Ptr Word8 -> Ptr Word8 -> IO (Status ())
loop))
where
post :: Ptr Word8
post = forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
start Int
1
byteString ::
Int ->
Read ByteString
byteString :: Int -> Read ByteString
byteString Int
totalNeededSize =
forall a. (Ptr Word8 -> Ptr Word8 -> IO (Status a)) -> Read a
Read (Int
-> List ByteString
-> Ptr Word8
-> Ptr Word8
-> IO (Status ByteString)
collectChunks Int
totalNeededSize forall a. List a
Nil)
where
collectChunks :: Int
-> List ByteString
-> Ptr Word8
-> Ptr Word8
-> IO (Status ByteString)
collectChunks Int
neededSize List ByteString
chunks Ptr Word8
startPtr Ptr Word8
endPtr =
let nextPtr :: Ptr Word8
nextPtr = forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
startPtr Int
neededSize
in
if Ptr Word8
nextPtr forall a. Ord a => a -> a -> Bool
<= Ptr Word8
endPtr
then
let lastChunkLength :: Int
lastChunkLength = forall a b. Ptr a -> Ptr b -> Int
minusPtr Ptr Word8
nextPtr Ptr Word8
startPtr
!chunk :: ByteString
chunk = Int -> Ptr Word8 -> ByteString
ByteString.fromPtr Int
lastChunkLength Ptr Word8
startPtr
merged :: ByteString
merged = ByteString -> Int -> List ByteString -> ByteString
ByteString.fromReverseStrictListWithHead ByteString
chunk (Int
totalNeededSize forall a. Num a => a -> a -> a
- Int
lastChunkLength) List ByteString
chunks
in forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Ptr Word8 -> a -> Status a
FinishedStatus Ptr Word8
nextPtr ByteString
merged)
else
let lastChunkLength :: Int
lastChunkLength = forall a b. Ptr a -> Ptr b -> Int
minusPtr Ptr Word8
endPtr Ptr Word8
startPtr
!chunk :: ByteString
chunk = Int -> Ptr Word8 -> ByteString
ByteString.fromPtr Int
lastChunkLength Ptr Word8
startPtr
newNeededSize :: Int
newNeededSize = Int
neededSize forall a. Num a => a -> a -> a
- Int
lastChunkLength
newChunks :: List ByteString
newChunks = forall a. a -> List a -> List a
Cons ByteString
chunk List ByteString
chunks
loop :: Ptr Word8 -> Ptr Word8 -> IO (Status ByteString)
loop = Int
-> List ByteString
-> Ptr Word8
-> Ptr Word8
-> IO (Status ByteString)
collectChunks Int
newNeededSize List ByteString
newChunks
in forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Read a -> Status a
UnfinishedStatus (forall a. (Ptr Word8 -> Ptr Word8 -> IO (Status a)) -> Read a
Read Ptr Word8 -> Ptr Word8 -> IO (Status ByteString)
loop))
byteStringWhile :: (Word8 -> Bool) -> Read ByteString
byteStringWhile :: (Word8 -> Bool) -> Read ByteString
byteStringWhile Word8 -> Bool
predicate =
forall a. (Ptr Word8 -> Ptr Word8 -> IO (Status a)) -> Read a
Read (Int
-> List ByteString
-> Ptr Word8
-> Ptr Word8
-> IO (Status ByteString)
collectChunks Int
0 forall a. List a
Nil)
where
collectChunks :: Int
-> List ByteString
-> Ptr Word8
-> Ptr Word8
-> IO (Status ByteString)
collectChunks Int
totalLength List ByteString
chunks Ptr Word8
startPtr Ptr Word8
endPtr =
Ptr Word8 -> IO (Status ByteString)
populateChunk Ptr Word8
startPtr
where
populateChunk :: Ptr Word8 -> IO (Status ByteString)
populateChunk Ptr Word8
curPtr =
if Ptr Word8
curPtr forall a. Ord a => a -> a -> Bool
< Ptr Word8
endPtr
then do
Word8
w <- Ptr Word8 -> IO Word8
IO.peekWord8 Ptr Word8
curPtr
if Word8 -> Bool
predicate Word8
w
then Ptr Word8 -> IO (Status ByteString)
populateChunk (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
curPtr Int
1)
else
let chunkLength :: Int
chunkLength =
forall a b. Ptr a -> Ptr b -> Int
minusPtr Ptr Word8
curPtr Ptr Word8
startPtr
!chunk :: ByteString
chunk =
Int -> Ptr Word8 -> ByteString
ByteString.fromPtr Int
chunkLength Ptr Word8
startPtr
merged :: ByteString
merged =
ByteString -> Int -> List ByteString -> ByteString
ByteString.fromReverseStrictListWithHead ByteString
chunk Int
totalLength List ByteString
chunks
in forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Ptr Word8 -> a -> Status a
FinishedStatus Ptr Word8
curPtr ByteString
merged)
else
let chunkLength :: Int
chunkLength =
forall a b. Ptr a -> Ptr b -> Int
minusPtr Ptr Word8
endPtr Ptr Word8
startPtr
!chunk :: ByteString
chunk =
Int -> Ptr Word8 -> ByteString
ByteString.fromPtr Int
chunkLength Ptr Word8
startPtr
newTotalLength :: Int
newTotalLength =
Int
totalLength forall a. Num a => a -> a -> a
+ Int
chunkLength
newChunks :: List ByteString
newChunks =
forall a. a -> List a -> List a
Cons ByteString
chunk List ByteString
chunks
in forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Read a -> Status a
UnfinishedStatus (forall a. (Ptr Word8 -> Ptr Word8 -> IO (Status a)) -> Read a
Read (Int
-> List ByteString
-> Ptr Word8
-> Ptr Word8
-> IO (Status ByteString)
collectChunks Int
newTotalLength List ByteString
newChunks)))
foldlWhile' :: (Word8 -> Bool) -> (acc -> Word8 -> acc) -> acc -> Read acc
foldlWhile' :: forall acc.
(Word8 -> Bool) -> (acc -> Word8 -> acc) -> acc -> Read acc
foldlWhile' Word8 -> Bool
predicate acc -> Word8 -> acc
step =
forall a. (Ptr Word8 -> Ptr Word8 -> IO (Status a)) -> Read a
Read forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. acc -> Ptr Word8 -> Ptr Word8 -> IO (Status acc)
loop
where
loop :: acc -> Ptr Word8 -> Ptr Word8 -> IO (Status acc)
loop !acc
acc Ptr Word8
start Ptr Word8
end =
if Ptr Word8
post forall a. Ord a => a -> a -> Bool
<= Ptr Word8
end
then do
Word8
w <- Ptr Word8 -> IO Word8
IO.peekWord8 Ptr Word8
start
if Word8 -> Bool
predicate Word8
w
then acc -> Ptr Word8 -> Ptr Word8 -> IO (Status acc)
loop (acc -> Word8 -> acc
step acc
acc Word8
w) Ptr Word8
post Ptr Word8
end
else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Ptr Word8 -> a -> Status a
FinishedStatus Ptr Word8
start acc
acc)
else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Read a -> Status a
UnfinishedStatus (forall a. (Ptr Word8 -> Ptr Word8 -> IO (Status a)) -> Read a
Read (acc -> Ptr Word8 -> Ptr Word8 -> IO (Status acc)
loop acc
acc)))
where
post :: Ptr Word8
post = forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
start Int
1
word8 :: Read Word8
word8 :: Read Word8
word8 =
forall a. (Ptr Word8 -> Ptr Word8 -> IO (Status a)) -> Read a
Read forall a b. (a -> b) -> a -> b
$ \Ptr Word8
start Ptr Word8
end ->
if Ptr Word8
end forall a. Ord a => a -> a -> Bool
> Ptr Word8
start
then Ptr Word8 -> IO Word8
IO.peekWord8 Ptr Word8
start forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> forall a. Ptr Word8 -> a -> Status a
FinishedStatus (forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
start Int
1)
else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Read a -> Status a
UnfinishedStatus Read Word8
word8)
int16InBe :: Read Int16
int16InBe :: Read Int16
int16InBe =
forall a. (Ptr Word8 -> Ptr Word8 -> IO (Status a)) -> Read a
Read Ptr Word8 -> Ptr Word8 -> IO (Status Int16)
inWhole
where
inWhole :: Ptr Word8 -> Ptr Word8 -> IO (Status Int16)
inWhole Ptr Word8
start Ptr Word8
end =
if Ptr Word8
inWholePost forall a. Ord a => a -> a -> Bool
<= Ptr Word8
end
then Ptr Word8 -> IO Int16
IO.peekBEInt16 Ptr Word8
start forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> forall a. Ptr Word8 -> a -> Status a
FinishedStatus Ptr Word8
inWholePost
else forall {a} {t}.
(Bits a, Ord t, Num a, Num t, Enum t) =>
t -> a -> Ptr Word8 -> Ptr Word8 -> IO (Status a)
bytely Integer
2 Int16
0 Ptr Word8
start Ptr Word8
end
where
inWholePost :: Ptr Word8
inWholePost = forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
start Int
2
bytely :: t -> a -> Ptr Word8 -> Ptr Word8 -> IO (Status a)
bytely !t
needed !a
acc Ptr Word8
start Ptr Word8
end =
if Ptr Word8
start forall a. Ord a => a -> a -> Bool
< Ptr Word8
end
then do
Word8
w <- Ptr Word8 -> IO Word8
IO.peekWord8 Ptr Word8
start
let newAcc :: a
newAcc = forall a. Bits a => a -> Int -> a
unsafeShiftL a
acc Int
8 forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
w
newStart :: Ptr Word8
newStart = forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
start Int
1
in if t
needed forall a. Ord a => a -> a -> Bool
> t
1
then t -> a -> Ptr Word8 -> Ptr Word8 -> IO (Status a)
bytely (forall a. Enum a => a -> a
pred t
needed) a
newAcc Ptr Word8
newStart Ptr Word8
end
else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Ptr Word8 -> a -> Status a
FinishedStatus Ptr Word8
newStart a
newAcc)
else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Read a -> Status a
UnfinishedStatus (forall a. (Ptr Word8 -> Ptr Word8 -> IO (Status a)) -> Read a
Read (t -> a -> Ptr Word8 -> Ptr Word8 -> IO (Status a)
bytely t
needed a
acc)))
int32InBe :: Read Int32
int32InBe :: Read Int32
int32InBe =
forall a. (Ptr Word8 -> Ptr Word8 -> IO (Status a)) -> Read a
Read Ptr Word8 -> Ptr Word8 -> IO (Status Int32)
inWhole
where
inWhole :: Ptr Word8 -> Ptr Word8 -> IO (Status Int32)
inWhole Ptr Word8
start Ptr Word8
end =
if Ptr Word8
inWholePost forall a. Ord a => a -> a -> Bool
<= Ptr Word8
end
then Ptr Word8 -> IO Int32
IO.peekBEInt32 Ptr Word8
start forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> forall a. Ptr Word8 -> a -> Status a
FinishedStatus Ptr Word8
inWholePost
else forall {a} {t}.
(Bits a, Ord t, Num a, Num t, Enum t) =>
t -> a -> Ptr Word8 -> Ptr Word8 -> IO (Status a)
bytely Integer
4 Int32
0 Ptr Word8
start Ptr Word8
end
where
inWholePost :: Ptr Word8
inWholePost = forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
start Int
4
bytely :: t -> a -> Ptr Word8 -> Ptr Word8 -> IO (Status a)
bytely !t
needed !a
acc Ptr Word8
start Ptr Word8
end =
if Ptr Word8
start forall a. Ord a => a -> a -> Bool
< Ptr Word8
end
then do
Word8
w <- Ptr Word8 -> IO Word8
IO.peekWord8 Ptr Word8
start
let newAcc :: a
newAcc = forall a. Bits a => a -> Int -> a
unsafeShiftL a
acc Int
8 forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
w
newStart :: Ptr Word8
newStart = forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
start Int
1
in if t
needed forall a. Ord a => a -> a -> Bool
> t
1
then t -> a -> Ptr Word8 -> Ptr Word8 -> IO (Status a)
bytely (forall a. Enum a => a -> a
pred t
needed) a
newAcc Ptr Word8
newStart Ptr Word8
end
else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Ptr Word8 -> a -> Status a
FinishedStatus Ptr Word8
newStart a
newAcc)
else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Read a -> Status a
UnfinishedStatus (forall a. (Ptr Word8 -> Ptr Word8 -> IO (Status a)) -> Read a
Read (t -> a -> Ptr Word8 -> Ptr Word8 -> IO (Status a)
bytely t
needed a
acc)))
int64InBe :: Read Int64
int64InBe :: Read Int64
int64InBe =
forall a. (Ptr Word8 -> Ptr Word8 -> IO (Status a)) -> Read a
Read Ptr Word8 -> Ptr Word8 -> IO (Status Int64)
inWhole
where
inWhole :: Ptr Word8 -> Ptr Word8 -> IO (Status Int64)
inWhole Ptr Word8
start Ptr Word8
end =
if Ptr Word8
inWholePost forall a. Ord a => a -> a -> Bool
<= Ptr Word8
end
then Ptr Word8 -> IO Int64
IO.peekBEInt64 Ptr Word8
start forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> forall a. Ptr Word8 -> a -> Status a
FinishedStatus Ptr Word8
inWholePost
else forall {a} {t}.
(Bits a, Ord t, Num a, Num t, Enum t) =>
t -> a -> Ptr Word8 -> Ptr Word8 -> IO (Status a)
bytely Integer
8 Int64
0 Ptr Word8
start Ptr Word8
end
where
inWholePost :: Ptr Word8
inWholePost = forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
start Int
8
bytely :: t -> a -> Ptr Word8 -> Ptr Word8 -> IO (Status a)
bytely !t
needed !a
acc Ptr Word8
start Ptr Word8
end =
if Ptr Word8
start forall a. Ord a => a -> a -> Bool
< Ptr Word8
end
then do
Word8
w <- Ptr Word8 -> IO Word8
IO.peekWord8 Ptr Word8
start
let newAcc :: a
newAcc = forall a. Bits a => a -> Int -> a
unsafeShiftL a
acc Int
8 forall a. Bits a => a -> a -> a
.|. forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
w
newStart :: Ptr Word8
newStart = forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
start Int
1
in if t
needed forall a. Ord a => a -> a -> Bool
> t
1
then t -> a -> Ptr Word8 -> Ptr Word8 -> IO (Status a)
bytely (forall a. Enum a => a -> a
pred t
needed) a
newAcc Ptr Word8
newStart Ptr Word8
end
else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Ptr Word8 -> a -> Status a
FinishedStatus Ptr Word8
newStart a
newAcc)
else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Read a -> Status a
UnfinishedStatus (forall a. (Ptr Word8 -> Ptr Word8 -> IO (Status a)) -> Read a
Read (t -> a -> Ptr Word8 -> Ptr Word8 -> IO (Status a)
bytely t
needed a
acc)))
nullTerminatedByteString :: Read ByteString
nullTerminatedByteString :: Read ByteString
nullTerminatedByteString =
(Word8 -> Bool) -> Read ByteString
byteStringWhile (forall a. Eq a => a -> a -> Bool
/= Word8
0) forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Int -> Read ()
skip Int
1
asciiIntegral :: (Integral a) => Read a
asciiIntegral :: forall a. Integral a => Read a
asciiIntegral =
forall acc.
(Word8 -> Bool) -> (acc -> Word8 -> acc) -> acc -> Read acc
foldlWhile' Word8 -> Bool
Word8Predicates.asciiDigit forall {a} {a}. (Integral a, Num a) => a -> a -> a
step a
0
where
step :: a -> a -> a
step a
acc a
byte =
a
acc forall a. Num a => a -> a -> a
* a
10 forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral a
byte forall a. Num a => a -> a -> a
- a
48