module Pipes.ByteString.Parse (
nextByte
, drawByte
, unDrawByte
, peekByte
, isEndOfBytes
, take
, takeWhile
) where
import Control.Monad.Trans.State.Strict (StateT, modify)
import qualified Data.ByteString as BS
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeTake, unsafeDrop)
import Data.Word (Word8)
import Pipes
import qualified Pipes.Parse as PP
import Prelude hiding (take, takeWhile)
nextByte
:: (Monad m)
=> Producer ByteString m r
-> m (Either r (Word8, Producer ByteString m r))
nextByte = go
where
go p = do
x <- next p
case x of
Left r -> return (Left r)
Right (bs, p') -> case (BS.uncons bs) of
Nothing -> go p'
Just (w8, bs') -> return (Right (w8, yield bs' >> p'))
drawByte :: (Monad m) => StateT (Producer ByteString m r) m (Either r Word8)
drawByte = do
x <- PP.draw
case x of
Left r -> return (Left r)
Right bs -> case (BS.uncons bs) of
Nothing -> drawByte
Just (w8, bs') -> do
PP.unDraw bs'
return (Right w8)
unDrawByte :: (Monad m) => Word8 -> StateT (Producer ByteString m r) m ()
unDrawByte w8 = modify (yield (BS.singleton w8) >>)
peekByte :: (Monad m) => StateT (Producer ByteString m r) m (Either r Word8)
peekByte = do
x <- drawByte
case x of
Left _ -> return ()
Right w8 -> unDrawByte w8
return x
isEndOfBytes :: (Monad m) => StateT (Producer ByteString m r) m Bool
isEndOfBytes = do
x <- peekByte
return (case x of
Left _ -> True
Right _ -> False )
take :: (Monad m, Integral a) => a -> Pipe ByteString ByteString (StateT (Producer ByteString m r) m) ()
take n0 = go n0 where
go n
| n <= 0 = return ()
| otherwise = do
bs <- await
let len = fromIntegral (BS.length bs)
if (len > n)
then do
let n' = fromIntegral n
lift . PP.unDraw $ unsafeDrop n' bs
yield $ unsafeTake n' bs
else do
yield bs
go (n len)
takeWhile
:: (Monad m)
=> (Word8 -> Bool)
-> Pipe ByteString ByteString (StateT (Producer ByteString m r) m) ()
takeWhile predicate = go
where
go = do
bs <- await
let (prefix, suffix) = BS.span predicate bs
if (BS.null suffix)
then do
yield bs
go
else do
lift $ PP.unDraw suffix
yield prefix