{-# LANGUAGE RankNTypes, Trustworthy #-}

{-| This module provides @pipes@ utilities for \"byte streams\", which are
    streams of strict 'ByteString's chunks.  Use byte streams to interact
    with both 'IO.Handle's and lazy 'ByteString's.

    To stream to or from 'IO.Handle's, use 'fromHandle' or 'toHandle'.  For
    example, the following program copies data from one file to another:

> import Pipes
> import qualified Pipes.ByteString as P
> import System.IO
>
> main =
>     withFile "inFile.txt"  ReadMode  $ \hIn  ->
>     withFile "outFile.txt" WriteMode $ \hOut ->
>     runEffect $ P.fromHandle hIn >-> P.toHandle hOut

    You can stream to and from 'stdin' and 'stdout' using the predefined 'stdin'
    and 'stdout' pipes, like in the following \"echo\" program:

> main = runEffect $ P.stdin >-> P.stdout

    You can also translate pure lazy 'BL.ByteString's to and from pipes:

> import qualified Data.ByteString.Lazy.Char8 as BL
>
> main = runEffect $ P.fromLazy (BL.pack "Hello, world!\n") >-> P.stdout

    In addition, this module provides many functions equivalent to lazy
    'ByteString' functions so that you can transform or fold byte streams.  For
    example, to stream only the first three lines of 'stdin' to 'stdout' you
    would write:

> import Lens.Family (over)
> import Pipes
> import qualified Pipes.ByteString as PB
> import Pipes.Group (takes)
>
> main = runEffect $ over PB.lines (takes 3) PB.stdin >-> PB.stdout

    The above program will never bring more than one chunk (~ 32 KB) into
    memory, no matter how long the lines are.

    Note that functions in this library are designed to operate on streams that
    are insensitive to chunk boundaries.  This means that they may freely split
    chunks into smaller chunks and /discard empty chunks/.  However, they will
    /never concatenate chunks/ in order to provide strict upper bounds on memory
    usage.
-}

module Pipes.ByteString (
    -- * Producers
      fromLazy
    , stdin
    , fromHandle
    , hGetSome
    , hGetNonBlocking
    , hGet
    , hGetRange

    -- * Servers
    , hGetSomeN
    , hGetN

    -- * Consumers
    , stdout
    , toHandle

    -- * Pipes
    , map
    , concatMap
    , take
    , takeWhile
    , filter
    , elemIndices
    , findIndices
    , scan

    -- * Folds
    , toLazy
    , toLazyM
    , toLazyM'
    , foldBytes
    , head
    , last
    , null
    , length
    , any
    , all
    , maximum
    , minimum
    , elem
    , notElem
    , find
    , index
    , elemIndex
    , findIndex
    , count

    -- * Parsing
    -- $parse
    , nextByte
    , drawByte
    , unDrawByte
    , peekByte
    , isEndOfBytes

    -- * Parsing Lenses
    , splitAt
    , span
    , break
    , breakOn
    , groupBy
    , group
    , word
    , line

    -- * Transforming Byte Streams
    , drop
    , dropWhile
    , intersperse
    , pack
    , unpack
    , chunksOf'

    -- * FreeT Transformations
    , chunksOf
    , splitsWith
    , splits
    , splitOn
    , groupsBy
    , groups
    , lines
    , unlines
    , words
    , unwords

    -- * Re-exports
    -- $reexports
    , module Data.ByteString
    , module Data.Word
    , module Pipes.Group
    , module Pipes.Parse
    ) where

import Control.Applicative ((<*))
import Control.Exception (throwIO, try)
import Control.Monad (liftM, join)
import Control.Monad.Trans.State.Strict (modify)
import qualified Data.ByteString as BS
import Data.ByteString (ByteString)
import Data.ByteString.Internal (isSpaceWord8)
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Search
import Data.ByteString.Lazy.Internal (foldrChunks, defaultChunkSize)
import Data.ByteString.Unsafe (unsafeTake)
import Data.Char (ord)
import Data.Monoid (mempty, (<>))
import Data.Functor.Constant (Constant(Constant, getConstant))
import Data.Functor.Identity (Identity)
import qualified Data.List as List
import Data.Word (Word8)
import Foreign.C.Error (Errno(Errno), ePIPE)
import qualified GHC.IO.Exception as G
import Pipes
import Pipes.Core (respond, Server')
import qualified Pipes.Group as PG
import Pipes.Group (concats, intercalates, FreeT)
import qualified Pipes.Parse as PP
import Pipes.Parse (Parser)
import qualified Pipes.Prelude as P
import qualified System.IO as IO
import Prelude hiding (
      all
    , any
    , break
    , concatMap
    , drop
    , dropWhile
    , elem
    , filter
    , head
    , last
    , lines
    , length
    , map
    , maximum
    , minimum
    , notElem
    , null
    , span
    , splitAt
    , take
    , takeWhile
    , unlines
    , unwords
    , words
    )

-- | Convert a lazy 'BL.ByteString' into a 'Producer' of strict 'ByteString's
fromLazy :: Monad m => BL.ByteString -> Producer' ByteString m ()
fromLazy :: ByteString -> Producer' ByteString m ()
fromLazy ByteString
bs = (ByteString
 -> Proxy x' x () ByteString m () -> Proxy x' x () ByteString m ())
-> Proxy x' x () ByteString m ()
-> ByteString
-> Proxy x' x () ByteString m ()
forall a. (ByteString -> a -> a) -> a -> ByteString -> a
foldrChunks (\ByteString
e Proxy x' x () ByteString m ()
a -> ByteString -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
e Proxy x' x () ByteString m ()
-> Proxy x' x () ByteString m () -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Proxy x' x () ByteString m ()
a) (() -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) ByteString
bs
{-# INLINABLE fromLazy #-}

-- | Stream bytes from 'stdin'
stdin :: MonadIO m => Producer' ByteString m ()
stdin :: Producer' ByteString m ()
stdin = Handle -> Producer' ByteString m ()
forall (m :: * -> *).
MonadIO m =>
Handle -> Producer' ByteString m ()
fromHandle Handle
IO.stdin
{-# INLINABLE stdin #-}

-- | Convert a 'IO.Handle' into a byte stream using a default chunk size
fromHandle :: MonadIO m => IO.Handle -> Producer' ByteString m ()
fromHandle :: Handle -> Producer' ByteString m ()
fromHandle = Int -> Handle -> Producer' ByteString m ()
forall (m :: * -> *).
MonadIO m =>
Int -> Handle -> Producer' ByteString m ()
hGetSome Int
defaultChunkSize
-- TODO: Test chunk size for performance
{-# INLINABLE fromHandle #-}

{-| Convert a handle into a byte stream using a maximum chunk size

    'hGetSome' forwards input immediately as it becomes available, splitting the
    input into multiple chunks if it exceeds the maximum chunk size.
-}
hGetSome :: MonadIO m => Int -> IO.Handle -> Producer' ByteString m ()
hGetSome :: Int -> Handle -> Producer' ByteString m ()
hGetSome Int
size Handle
h = Proxy x' x () ByteString m ()
Producer' ByteString m ()
go
  where
    go :: Proxy x' x () ByteString m ()
go = do
        ByteString
bs <- IO ByteString -> Proxy x' x () ByteString m ByteString
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> Int -> IO ByteString
BS.hGetSome Handle
h Int
size)
        if (ByteString -> Bool
BS.null ByteString
bs)
            then () -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
            else do
                ByteString -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
bs
                Proxy x' x () ByteString m ()
go
{-# INLINABLE hGetSome #-}

{-| Convert a handle into a byte stream using a fixed chunk size

    Similar to 'hGet' except that it will never block waiting for data
    to become available.
-}
hGetNonBlocking :: MonadIO m => Int -> IO.Handle -> Producer' ByteString m ()
hGetNonBlocking :: Int -> Handle -> Producer' ByteString m ()
hGetNonBlocking Int
size Handle
h = Proxy x' x () ByteString m ()
Producer' ByteString m ()
go where
    go :: Proxy x' x () ByteString m ()
go = do
        Bool
eof <- IO Bool -> Proxy x' x () ByteString m Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> IO Bool
IO.hIsEOF Handle
h)
        if Bool
eof
            then () -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
            else do
                ByteString
bs <- IO ByteString -> Proxy x' x () ByteString m ByteString
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> Int -> IO ByteString
BS.hGetNonBlocking Handle
h Int
size)
                ByteString -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
bs
                Proxy x' x () ByteString m ()
go
{-# INLINABLE hGetNonBlocking #-}

{-| Convert a handle into a byte stream using a fixed chunk size

    'hGet' waits until exactly the requested number of bytes are available for
    each chunk.
-}
hGet :: MonadIO m => Int -> IO.Handle -> Producer' ByteString m ()
hGet :: Int -> Handle -> Producer' ByteString m ()
hGet Int
size Handle
h = Proxy x' x () ByteString m ()
Producer' ByteString m ()
go
  where
    go :: Proxy x' x () ByteString m ()
go = do
        ByteString
bs <- IO ByteString -> Proxy x' x () ByteString m ByteString
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> Int -> IO ByteString
BS.hGet Handle
h Int
size)
        if (ByteString -> Bool
BS.null ByteString
bs)
            then () -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
            else do
                ByteString -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
bs
                Proxy x' x () ByteString m ()
go
{-# INLINABLE hGet #-}

{-| Like 'hGet' but with an extra parameter specifying an initial handle offset
-}
hGetRange
    :: MonadIO m
    => Int -- ^ Offset
    -> Int -- ^ Size
    -> IO.Handle
    -> Producer' ByteString m ()
hGetRange :: Int -> Int -> Handle -> Producer' ByteString m ()
hGetRange Int
offset Int
size Handle
h = do
    IO () -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> Proxy x' x () ByteString m ())
-> IO () -> Proxy x' x () ByteString m ()
forall a b. (a -> b) -> a -> b
$ Handle -> SeekMode -> Integer -> IO ()
IO.hSeek Handle
h SeekMode
IO.AbsoluteSeek (Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
offset)
    Int -> Handle -> Producer' ByteString m ()
forall (m :: * -> *).
MonadIO m =>
Int -> Handle -> Producer' ByteString m ()
hGet Int
size Handle
h
{-# INLINABLE hGetRange #-}

(^.) :: a -> ((b -> Constant b b) -> (a -> Constant b a)) -> b
a
a ^. :: a -> ((b -> Constant b b) -> a -> Constant b a) -> b
^. (b -> Constant b b) -> a -> Constant b a
lens = Constant b a -> b
forall a k (b :: k). Constant a b -> a
getConstant ((b -> Constant b b) -> a -> Constant b a
lens b -> Constant b b
forall k a (b :: k). a -> Constant a b
Constant a
a)

{-| Like 'hGetSome', except you can vary the maximum chunk size for each request
-}
hGetSomeN :: MonadIO m => IO.Handle -> Int -> Server' Int ByteString m ()
hGetSomeN :: Handle -> Int -> Server' Int ByteString m ()
hGetSomeN Handle
h Int
size = Int -> Proxy x' x Int ByteString m ()
forall (m :: * -> *) x' x.
MonadIO m =>
Int -> Proxy x' x Int ByteString m ()
go Int
size
  where
    go :: Int -> Proxy x' x Int ByteString m ()
go Int
size = do
        ByteString
bs <- IO ByteString -> Proxy x' x Int ByteString m ByteString
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> Int -> IO ByteString
BS.hGetSome Handle
h Int
size)
        if (ByteString -> Bool
BS.null ByteString
bs)
            then () -> Proxy x' x Int ByteString m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
            else do
                Int
size2 <- ByteString -> Proxy x' x Int ByteString m Int
forall (m :: * -> *) a x' x a'.
Functor m =>
a -> Proxy x' x a' a m a'
respond ByteString
bs
                Int -> Proxy x' x Int ByteString m ()
go Int
size2
{-# INLINABLE hGetSomeN #-}

-- | Like 'hGet', except you can vary the chunk size for each request
hGetN :: MonadIO m => IO.Handle -> Int -> Server' Int ByteString m ()
hGetN :: Handle -> Int -> Server' Int ByteString m ()
hGetN Handle
h Int
size = Int -> Proxy x' x Int ByteString m ()
forall (m :: * -> *) x' x.
MonadIO m =>
Int -> Proxy x' x Int ByteString m ()
go Int
size
  where
    go :: Int -> Proxy x' x Int ByteString m ()
go Int
size = do
        ByteString
bs <- IO ByteString -> Proxy x' x Int ByteString m ByteString
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> Int -> IO ByteString
BS.hGet Handle
h Int
size)
        if (ByteString -> Bool
BS.null ByteString
bs)
            then () -> Proxy x' x Int ByteString m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
            else do
                Int
size2 <- ByteString -> Proxy x' x Int ByteString m Int
forall (m :: * -> *) a x' x a'.
Functor m =>
a -> Proxy x' x a' a m a'
respond ByteString
bs
                Int -> Proxy x' x Int ByteString m ()
go Int
size2
{-# INLINABLE hGetN #-}

{-| Stream bytes to 'stdout'

    Unlike 'toHandle', 'stdout' gracefully terminates on a broken output pipe.
-}
stdout :: MonadIO m => Consumer' ByteString m ()
stdout :: Consumer' ByteString m ()
stdout = Proxy () ByteString y' y m ()
Consumer' ByteString m ()
go
  where
    go :: Proxy () ByteString y' y m ()
go = do
        ByteString
bs <- Proxy () ByteString y' y m ByteString
forall (m :: * -> *) a. Functor m => Consumer' a m a
await
        Either IOException ()
x  <- IO (Either IOException ())
-> Proxy () ByteString y' y m (Either IOException ())
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either IOException ())
 -> Proxy () ByteString y' y m (Either IOException ()))
-> IO (Either IOException ())
-> Proxy () ByteString y' y m (Either IOException ())
forall a b. (a -> b) -> a -> b
$ IO () -> IO (Either IOException ())
forall e a. Exception e => IO a -> IO (Either e a)
try (ByteString -> IO ()
BS.putStr ByteString
bs)
        case Either IOException ()
x of
            Left (G.IOError { ioe_type :: IOException -> IOErrorType
G.ioe_type  = IOErrorType
G.ResourceVanished
                            , ioe_errno :: IOException -> Maybe CInt
G.ioe_errno = Just CInt
ioe })
                 | CInt -> Errno
Errno CInt
ioe Errno -> Errno -> Bool
forall a. Eq a => a -> a -> Bool
== Errno
ePIPE
                     -> () -> Proxy () ByteString y' y m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
            Left  IOException
e  -> IO () -> Proxy () ByteString y' y m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IOException -> IO ()
forall e a. Exception e => e -> IO a
throwIO IOException
e)
            Right () -> Proxy () ByteString y' y m ()
go
{-# INLINABLE stdout #-}

{-| Convert a byte stream into a 'Handle'

> p >-> toHandle handle = for p (liftIO . hPutStr handle)
-}
toHandle :: MonadIO m => IO.Handle -> Consumer' ByteString m r
toHandle :: Handle -> Consumer' ByteString m r
toHandle Handle
h = Proxy () ByteString () ByteString m r
-> (ByteString -> Proxy () ByteString y' y m ())
-> Proxy () ByteString y' y m r
forall (m :: * -> *) x' x b' b a' c' c.
Functor m =>
Proxy x' x b' b m a'
-> (b -> Proxy x' x c' c m b') -> Proxy x' x c' c m a'
for Proxy () ByteString () ByteString m r
forall (m :: * -> *) a r. Functor m => Pipe a a m r
cat (IO () -> Proxy () ByteString y' y m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> Proxy () ByteString y' y m ())
-> (ByteString -> IO ())
-> ByteString
-> Proxy () ByteString y' y m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> ByteString -> IO ()
BS.hPut Handle
h)
{-# INLINABLE [1] toHandle #-}

{-# RULES "p >-> toHandle h" forall p h .
        p >-> toHandle h = for p (\bs -> liftIO (BS.hPut h bs))
  #-}

-- | Apply a transformation to each 'Word8' in the stream
map :: Monad m => (Word8 -> Word8) -> Pipe ByteString ByteString m r
map :: (Word8 -> Word8) -> Pipe ByteString ByteString m r
map Word8 -> Word8
f = (ByteString -> ByteString) -> Pipe ByteString ByteString m r
forall (m :: * -> *) a b r. Functor m => (a -> b) -> Pipe a b m r
P.map ((Word8 -> Word8) -> ByteString -> ByteString
BS.map Word8 -> Word8
f)
{-# INLINE map #-}

-- | Map a function over the byte stream and concatenate the results
concatMap :: Monad m => (Word8 -> ByteString) -> Pipe ByteString ByteString m r
concatMap :: (Word8 -> ByteString) -> Pipe ByteString ByteString m r
concatMap Word8 -> ByteString
f = (ByteString -> ByteString) -> Pipe ByteString ByteString m r
forall (m :: * -> *) a b r. Functor m => (a -> b) -> Pipe a b m r
P.map ((Word8 -> ByteString) -> ByteString -> ByteString
BS.concatMap Word8 -> ByteString
f)
{-# INLINABLE concatMap #-}

-- | @(take n)@ only allows @n@ bytes to pass
take :: (Monad m, Integral n) => n -> Pipe ByteString ByteString m ()
take :: n -> Pipe ByteString ByteString m ()
take n
n0 = n -> Pipe ByteString ByteString m ()
forall t (m :: * -> *).
(Functor m, Integral t) =>
t -> Proxy () ByteString () ByteString m ()
go n
n0 where
    go :: t -> Proxy () ByteString () ByteString m ()
go t
n
        | t
n t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
0    = () -> Proxy () ByteString () ByteString m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        | Bool
otherwise = do
            ByteString
bs <- Proxy () ByteString () ByteString m ByteString
forall (m :: * -> *) a. Functor m => Consumer' a m a
await
            let len :: t
len = Int -> t
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
BS.length ByteString
bs)
            if (t
len t -> t -> Bool
forall a. Ord a => a -> a -> Bool
> t
n)
                then ByteString -> Proxy () ByteString () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield (Int -> ByteString -> ByteString
unsafeTake (t -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral t
n) ByteString
bs)
                else do
                    ByteString -> Proxy () ByteString () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
bs
                    t -> Proxy () ByteString () ByteString m ()
go (t
n t -> t -> t
forall a. Num a => a -> a -> a
- t
len)
{-# INLINABLE take #-}

-- | Take bytes until they fail the predicate
takeWhile :: Monad m => (Word8 -> Bool) -> Pipe ByteString ByteString m ()
takeWhile :: (Word8 -> Bool) -> Pipe ByteString ByteString m ()
takeWhile Word8 -> Bool
predicate = Pipe ByteString ByteString m ()
go
  where
    go :: Pipe ByteString ByteString m ()
go = do
        ByteString
bs <- Proxy () ByteString () ByteString m ByteString
forall (m :: * -> *) a. Functor m => Consumer' a m a
await
        let (ByteString
prefix, ByteString
suffix) = (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
BS.span Word8 -> Bool
predicate ByteString
bs
        if (ByteString -> Bool
BS.null ByteString
suffix)
            then do
                ByteString -> Pipe ByteString ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
bs
                Pipe ByteString ByteString m ()
go
            else ByteString -> Pipe ByteString ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
prefix
{-# INLINABLE takeWhile #-}

-- | Only allows 'Word8's to pass if they satisfy the predicate
filter :: Monad m => (Word8 -> Bool) -> Pipe ByteString ByteString m r
filter :: (Word8 -> Bool) -> Pipe ByteString ByteString m r
filter Word8 -> Bool
predicate = (ByteString -> ByteString) -> Pipe ByteString ByteString m r
forall (m :: * -> *) a b r. Functor m => (a -> b) -> Pipe a b m r
P.map ((Word8 -> Bool) -> ByteString -> ByteString
BS.filter Word8 -> Bool
predicate)
{-# INLINABLE filter #-}

-- | Stream all indices whose elements match the given 'Word8'
elemIndices :: (Monad m, Num n) => Word8 -> Pipe ByteString n m r
elemIndices :: Word8 -> Pipe ByteString n m r
elemIndices Word8
w8 = (Word8 -> Bool) -> Pipe ByteString n m r
forall (m :: * -> *) n r.
(Monad m, Num n) =>
(Word8 -> Bool) -> Pipe ByteString n m r
findIndices (Word8
w8 Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
==)
{-# INLINABLE elemIndices #-}

-- | Stream all indices whose elements satisfy the given predicate
findIndices :: (Monad m, Num n) => (Word8 -> Bool) -> Pipe ByteString n m r
findIndices :: (Word8 -> Bool) -> Pipe ByteString n m r
findIndices Word8 -> Bool
predicate = n -> Pipe ByteString n m r
forall (m :: * -> *) a b.
(Functor m, Num a) =>
a -> Proxy () ByteString () a m b
go n
0
  where
    go :: a -> Proxy () ByteString () a m b
go a
n = do
        ByteString
bs <- Proxy () ByteString () a m ByteString
forall (m :: * -> *) a. Functor m => Consumer' a m a
await
        [a] -> Proxy () ByteString () a m ()
forall (m :: * -> *) (f :: * -> *) a x' x.
(Functor m, Foldable f) =>
f a -> Proxy x' x () a m ()
each ([a] -> Proxy () ByteString () a m ())
-> [a] -> Proxy () ByteString () a m ()
forall a b. (a -> b) -> a -> b
$ (Int -> a) -> [Int] -> [a]
forall a b. (a -> b) -> [a] -> [b]
List.map (\Int
i -> a
n a -> a -> a
forall a. Num a => a -> a -> a
+ Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i) ((Word8 -> Bool) -> ByteString -> [Int]
BS.findIndices Word8 -> Bool
predicate ByteString
bs)
        a -> Proxy () ByteString () a m b
go (a -> Proxy () ByteString () a m b)
-> a -> Proxy () ByteString () a m b
forall a b. (a -> b) -> a -> b
$! a
n a -> a -> a
forall a. Num a => a -> a -> a
+ Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
BS.length ByteString
bs)
{-# INLINABLE findIndices #-}

-- | Strict left scan over the bytes
scan
    :: Monad m
    => (Word8 -> Word8 -> Word8) -> Word8 -> Pipe ByteString ByteString m r
scan :: (Word8 -> Word8 -> Word8)
-> Word8 -> Pipe ByteString ByteString m r
scan Word8 -> Word8 -> Word8
step Word8
begin = do
    ByteString -> Proxy () ByteString () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield (Word8 -> ByteString
BS.singleton Word8
begin)
    Word8 -> Pipe ByteString ByteString m r
forall (m :: * -> *) b.
Functor m =>
Word8 -> Proxy () ByteString () ByteString m b
go Word8
begin
  where
    go :: Word8 -> Proxy () ByteString () ByteString m b
go Word8
w8 = do
        ByteString
bs <- Proxy () ByteString () ByteString m ByteString
forall (m :: * -> *) a. Functor m => Consumer' a m a
await
        let bs' :: ByteString
bs' = (Word8 -> Word8 -> Word8) -> Word8 -> ByteString -> ByteString
BS.scanl Word8 -> Word8 -> Word8
step Word8
w8 ByteString
bs
            w8' :: Word8
w8' = ByteString -> Word8
BS.last ByteString
bs'
        ByteString -> Proxy () ByteString () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield (ByteString -> ByteString
BS.tail ByteString
bs')
        Word8 -> Proxy () ByteString () ByteString m b
go Word8
w8'
{-# INLINABLE scan #-}

{-| Fold a pure 'Producer' of strict 'ByteString's into a lazy
    'BL.ByteString'
-}
toLazy :: Producer ByteString Identity () -> BL.ByteString
toLazy :: Producer ByteString Identity () -> ByteString
toLazy = [ByteString] -> ByteString
BL.fromChunks ([ByteString] -> ByteString)
-> (Producer ByteString Identity () -> [ByteString])
-> Producer ByteString Identity ()
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Producer ByteString Identity () -> [ByteString]
forall a. Producer a Identity () -> [a]
P.toList
{-# INLINABLE toLazy #-}

{-| Fold an effectful 'Producer' of strict 'ByteString's into a lazy
    'BL.ByteString'

    Note: 'toLazyM' is not an idiomatic use of @pipes@, but I provide it for
    simple testing purposes.  Idiomatic @pipes@ style consumes the chunks
    immediately as they are generated instead of loading them all into memory.
-}
toLazyM :: Monad m => Producer ByteString m () -> m BL.ByteString
toLazyM :: Producer ByteString m () -> m ByteString
toLazyM = ([ByteString] -> ByteString) -> m [ByteString] -> m ByteString
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [ByteString] -> ByteString
BL.fromChunks (m [ByteString] -> m ByteString)
-> (Producer ByteString m () -> m [ByteString])
-> Producer ByteString m ()
-> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Producer ByteString m () -> m [ByteString]
forall (m :: * -> *) a. Monad m => Producer a m () -> m [a]
P.toListM
{-# INLINABLE toLazyM #-}

{-| Fold an effectful 'Producer' of strict 'ByteString's into a lazy
    'BL.ByteString' alongside the return value

    Note: 'toLazyM'' is not an idiomatic use of @pipes@, but I provide it for
    simple testing purposes.  Idiomatic @pipes@ style consumes the chunks
    immediately as they are generated instead of loading them all into memory.
-}
toLazyM' :: Monad m => Producer ByteString m a -> m (BL.ByteString, a)
toLazyM' :: Producer ByteString m a -> m (ByteString, a)
toLazyM' Producer ByteString m a
p = do ([ByteString]
chunks, a
a) <- Producer ByteString m a -> m ([ByteString], a)
forall (m :: * -> *) a r. Monad m => Producer a m r -> m ([a], r)
P.toListM' Producer ByteString m a
p
                (ByteString, a) -> m (ByteString, a)
forall (m :: * -> *) a. Monad m => a -> m a
return ([ByteString] -> ByteString
BL.fromChunks [ByteString]
chunks, a
a)
{-# INLINABLE toLazyM' #-}

{-| Reduce the stream of bytes using a strict left fold

    Note: It's more efficient to use folds from @Control.Foldl.ByteString@ in
    conjunction with @Pipes.Prelude.'Pipes.Prelude.fold'@ when possible
-}
foldBytes
    :: Monad m
    => (x -> Word8 -> x) -> x -> (x -> r) -> Producer ByteString m () -> m r
foldBytes :: (x -> Word8 -> x)
-> x -> (x -> r) -> Producer ByteString m () -> m r
foldBytes x -> Word8 -> x
step x
begin x -> r
done = (x -> ByteString -> x)
-> x -> (x -> r) -> Producer ByteString m () -> m r
forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Producer a m () -> m b
P.fold (\x
x ByteString
bs -> (x -> Word8 -> x) -> x -> ByteString -> x
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
BS.foldl' x -> Word8 -> x
step x
x ByteString
bs) x
begin x -> r
done
{-# INLINABLE foldBytes #-}

-- | Retrieve the first 'Word8'
head :: Monad m => Producer ByteString m () -> m (Maybe Word8)
head :: Producer ByteString m () -> m (Maybe Word8)
head = Producer ByteString m () -> m (Maybe Word8)
forall (m :: * -> *) r.
Monad m =>
Producer ByteString m r -> m (Maybe Word8)
go
  where
    go :: Producer ByteString m r -> m (Maybe Word8)
go Producer ByteString m r
p = do
        Either r (Word8, Producer ByteString m r)
x <- Producer ByteString m r
-> m (Either r (Word8, Producer ByteString m r))
forall (m :: * -> *) r.
Monad m =>
Producer ByteString m r
-> m (Either r (Word8, Producer ByteString m r))
nextByte Producer ByteString m r
p
        Maybe Word8 -> m (Maybe Word8)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Word8 -> m (Maybe Word8)) -> Maybe Word8 -> m (Maybe Word8)
forall a b. (a -> b) -> a -> b
$ case Either r (Word8, Producer ByteString m r)
x of
            Left   r
_      -> Maybe Word8
forall a. Maybe a
Nothing
            Right (Word8
w8, Producer ByteString m r
_) -> Word8 -> Maybe Word8
forall a. a -> Maybe a
Just Word8
w8
{-# INLINABLE head #-}

-- | Retrieve the last 'Word8'
last :: Monad m => Producer ByteString m () -> m (Maybe Word8)
last :: Producer ByteString m () -> m (Maybe Word8)
last = Maybe Word8 -> Producer ByteString m () -> m (Maybe Word8)
forall (m :: * -> *).
Monad m =>
Maybe Word8 -> Producer ByteString m () -> m (Maybe Word8)
go Maybe Word8
forall a. Maybe a
Nothing
  where
    go :: Maybe Word8 -> Producer ByteString m () -> m (Maybe Word8)
go Maybe Word8
r Producer ByteString m ()
p = do
        Either () (ByteString, Producer ByteString m ())
x <- Producer ByteString m ()
-> m (Either () (ByteString, Producer ByteString m ()))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next Producer ByteString m ()
p
        case Either () (ByteString, Producer ByteString m ())
x of
            Left   ()      -> Maybe Word8 -> m (Maybe Word8)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Word8
r
            Right (ByteString
bs, Producer ByteString m ()
p') ->
                Maybe Word8 -> Producer ByteString m () -> m (Maybe Word8)
go (if ByteString -> Bool
BS.null ByteString
bs then Maybe Word8
r else (Word8 -> Maybe Word8
forall a. a -> Maybe a
Just (Word8 -> Maybe Word8) -> Word8 -> Maybe Word8
forall a b. (a -> b) -> a -> b
$ ByteString -> Word8
BS.last ByteString
bs)) Producer ByteString m ()
p'
                -- TODO: Change this to 'unsafeLast' when bytestring-0.10.2.0
                --       becomes more widespread
{-# INLINABLE last #-}

-- | Determine if the stream is empty
null :: Monad m => Producer ByteString m () -> m Bool
null :: Producer ByteString m () -> m Bool
null = (ByteString -> Bool) -> Producer ByteString m () -> m Bool
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Producer a m () -> m Bool
P.all ByteString -> Bool
BS.null
{-# INLINABLE null #-}

-- | Count the number of bytes
length :: (Monad m, Num n) => Producer ByteString m () -> m n
length :: Producer ByteString m () -> m n
length = (n -> ByteString -> n)
-> n -> (n -> n) -> Producer ByteString m () -> m n
forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Producer a m () -> m b
P.fold (\n
n ByteString
bs -> n
n n -> n -> n
forall a. Num a => a -> a -> a
+ Int -> n
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
BS.length ByteString
bs)) n
0 n -> n
forall a. a -> a
id
{-# INLINABLE length #-}

-- | Fold that returns whether 'M.Any' received 'Word8's satisfy the predicate
any :: Monad m => (Word8 -> Bool) -> Producer ByteString m () -> m Bool
any :: (Word8 -> Bool) -> Producer ByteString m () -> m Bool
any Word8 -> Bool
predicate = (ByteString -> Bool) -> Producer ByteString m () -> m Bool
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Producer a m () -> m Bool
P.any ((Word8 -> Bool) -> ByteString -> Bool
BS.any Word8 -> Bool
predicate)
{-# INLINABLE any #-}

-- | Fold that returns whether 'M.All' received 'Word8's satisfy the predicate
all :: Monad m => (Word8 -> Bool) -> Producer ByteString m () -> m Bool
all :: (Word8 -> Bool) -> Producer ByteString m () -> m Bool
all Word8 -> Bool
predicate = (ByteString -> Bool) -> Producer ByteString m () -> m Bool
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Producer a m () -> m Bool
P.all ((Word8 -> Bool) -> ByteString -> Bool
BS.all Word8 -> Bool
predicate)
{-# INLINABLE all #-}

-- | Return the maximum 'Word8' within a byte stream
maximum :: Monad m => Producer ByteString m () -> m (Maybe Word8)
maximum :: Producer ByteString m () -> m (Maybe Word8)
maximum = (Maybe Word8 -> ByteString -> Maybe Word8)
-> Maybe Word8
-> (Maybe Word8 -> Maybe Word8)
-> Producer ByteString m ()
-> m (Maybe Word8)
forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Producer a m () -> m b
P.fold Maybe Word8 -> ByteString -> Maybe Word8
step Maybe Word8
forall a. Maybe a
Nothing Maybe Word8 -> Maybe Word8
forall a. a -> a
id
  where
    step :: Maybe Word8 -> ByteString -> Maybe Word8
step Maybe Word8
mw8 ByteString
bs =
        if (ByteString -> Bool
BS.null ByteString
bs)
        then Maybe Word8
mw8
        else Word8 -> Maybe Word8
forall a. a -> Maybe a
Just (Word8 -> Maybe Word8) -> Word8 -> Maybe Word8
forall a b. (a -> b) -> a -> b
$ case Maybe Word8
mw8 of
            Maybe Word8
Nothing -> ByteString -> Word8
BS.maximum ByteString
bs
            Just Word8
w8 -> Word8 -> Word8 -> Word8
forall a. Ord a => a -> a -> a
max Word8
w8 (ByteString -> Word8
BS.maximum ByteString
bs)
{-# INLINABLE maximum #-}

-- | Return the minimum 'Word8' within a byte stream
minimum :: Monad m => Producer ByteString m () -> m (Maybe Word8)
minimum :: Producer ByteString m () -> m (Maybe Word8)
minimum = (Maybe Word8 -> ByteString -> Maybe Word8)
-> Maybe Word8
-> (Maybe Word8 -> Maybe Word8)
-> Producer ByteString m ()
-> m (Maybe Word8)
forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Producer a m () -> m b
P.fold Maybe Word8 -> ByteString -> Maybe Word8
step Maybe Word8
forall a. Maybe a
Nothing Maybe Word8 -> Maybe Word8
forall a. a -> a
id
  where
    step :: Maybe Word8 -> ByteString -> Maybe Word8
step Maybe Word8
mw8 ByteString
bs =
        if (ByteString -> Bool
BS.null ByteString
bs)
        then Maybe Word8
mw8
        else case Maybe Word8
mw8 of
            Maybe Word8
Nothing -> Word8 -> Maybe Word8
forall a. a -> Maybe a
Just (ByteString -> Word8
BS.minimum ByteString
bs)
            Just Word8
w8 -> Word8 -> Maybe Word8
forall a. a -> Maybe a
Just (Word8 -> Word8 -> Word8
forall a. Ord a => a -> a -> a
min Word8
w8 (ByteString -> Word8
BS.minimum ByteString
bs))
{-# INLINABLE minimum #-}

-- | Determine whether any element in the byte stream matches the given 'Word8'
elem :: Monad m => Word8 -> Producer ByteString m () -> m Bool
elem :: Word8 -> Producer ByteString m () -> m Bool
elem Word8
w8 = (ByteString -> Bool) -> Producer ByteString m () -> m Bool
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Producer a m () -> m Bool
P.any (Word8 -> ByteString -> Bool
BS.elem Word8
w8)
{-# INLINABLE elem #-}

{-| Determine whether all elements in the byte stream do not match the given
    'Word8'
-}
notElem :: Monad m => Word8 -> Producer ByteString m () -> m Bool
notElem :: Word8 -> Producer ByteString m () -> m Bool
notElem Word8
w8 = (ByteString -> Bool) -> Producer ByteString m () -> m Bool
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Producer a m () -> m Bool
P.all (Word8 -> ByteString -> Bool
BS.notElem Word8
w8)
{-# INLINABLE notElem #-}

-- | Find the first element in the stream that matches the predicate
find
    :: Monad m
    => (Word8 -> Bool) -> Producer ByteString m () -> m (Maybe Word8)
find :: (Word8 -> Bool) -> Producer ByteString m () -> m (Maybe Word8)
find Word8 -> Bool
predicate Producer ByteString m ()
p = Producer ByteString m () -> m (Maybe Word8)
forall (m :: * -> *).
Monad m =>
Producer ByteString m () -> m (Maybe Word8)
head (Producer ByteString m ()
p Producer ByteString m ()
-> Proxy () ByteString () ByteString m ()
-> Producer ByteString m ()
forall (m :: * -> *) a' a b r c' c.
Functor m =>
Proxy a' a () b m r -> Proxy () b c' c m r -> Proxy a' a c' c m r
>-> (Word8 -> Bool) -> Proxy () ByteString () ByteString m ()
forall (m :: * -> *) r.
Monad m =>
(Word8 -> Bool) -> Pipe ByteString ByteString m r
filter Word8 -> Bool
predicate)
{-# INLINABLE find #-}

-- | Index into a byte stream
index
    :: (Monad m, Integral n)
    => n -> Producer ByteString m () -> m (Maybe Word8)
index :: n -> Producer ByteString m () -> m (Maybe Word8)
index n
n Producer ByteString m ()
p = Producer ByteString m () -> m (Maybe Word8)
forall (m :: * -> *).
Monad m =>
Producer ByteString m () -> m (Maybe Word8)
head (n -> Producer ByteString m () -> Producer ByteString m ()
forall (m :: * -> *) n r.
(Monad m, Integral n) =>
n -> Producer ByteString m r -> Producer ByteString m r
drop n
n Producer ByteString m ()
p)
{-# INLINABLE index #-}

-- | Find the index of an element that matches the given 'Word8'
elemIndex
    :: (Monad m, Num n) => Word8 -> Producer ByteString m () -> m (Maybe n)
elemIndex :: Word8 -> Producer ByteString m () -> m (Maybe n)
elemIndex Word8
w8 = (Word8 -> Bool) -> Producer ByteString m () -> m (Maybe n)
forall (m :: * -> *) n.
(Monad m, Num n) =>
(Word8 -> Bool) -> Producer ByteString m () -> m (Maybe n)
findIndex (Word8
w8 Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
==)
{-# INLINABLE elemIndex #-}

-- | Store the first index of an element that satisfies the predicate
findIndex
    :: (Monad m, Num n)
    => (Word8 -> Bool) -> Producer ByteString m () -> m (Maybe n)
findIndex :: (Word8 -> Bool) -> Producer ByteString m () -> m (Maybe n)
findIndex Word8 -> Bool
predicate Producer ByteString m ()
p = Producer n m () -> m (Maybe n)
forall (m :: * -> *) a. Monad m => Producer a m () -> m (Maybe a)
P.head (Producer ByteString m ()
p Producer ByteString m ()
-> Proxy () ByteString () n m () -> Producer n m ()
forall (m :: * -> *) a' a b r c' c.
Functor m =>
Proxy a' a () b m r -> Proxy () b c' c m r -> Proxy a' a c' c m r
>-> (Word8 -> Bool) -> Proxy () ByteString () n m ()
forall (m :: * -> *) n r.
(Monad m, Num n) =>
(Word8 -> Bool) -> Pipe ByteString n m r
findIndices Word8 -> Bool
predicate)
{-# INLINABLE findIndex #-}

-- | Store a tally of how many elements match the given 'Word8'
count :: (Monad m, Num n) => Word8 -> Producer ByteString m () -> m n
count :: Word8 -> Producer ByteString m () -> m n
count Word8
w8 Producer ByteString m ()
p = (n -> n -> n) -> n -> (n -> n) -> Producer n m () -> m n
forall (m :: * -> *) x a b.
Monad m =>
(x -> a -> x) -> x -> (x -> b) -> Producer a m () -> m b
P.fold n -> n -> n
forall a. Num a => a -> a -> a
(+) n
0 n -> n
forall a. a -> a
id (Producer ByteString m ()
p Producer ByteString m ()
-> Proxy () ByteString () n m () -> Producer n m ()
forall (m :: * -> *) a' a b r c' c.
Functor m =>
Proxy a' a () b m r -> Proxy () b c' c m r -> Proxy a' a c' c m r
>-> (ByteString -> n) -> Proxy () ByteString () n m ()
forall (m :: * -> *) a b r. Functor m => (a -> b) -> Pipe a b m r
P.map (Int -> n
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> n) -> (ByteString -> Int) -> ByteString -> n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> ByteString -> Int
BS.count Word8
w8))
{-# INLINABLE count #-}

{-| Consume the first byte from a byte stream

    'next' either fails with a 'Left' if the 'Producer' has no more bytes or
    succeeds with a 'Right' providing the next byte and the remainder of the
    'Producer'.
-}
nextByte
    :: Monad m
    => Producer ByteString m r
    -> m (Either r (Word8, Producer ByteString m r))
nextByte :: Producer ByteString m r
-> m (Either r (Word8, Producer ByteString m r))
nextByte = Producer ByteString m r
-> m (Either r (Word8, Producer ByteString m r))
forall (m :: * -> *) r.
Monad m =>
Producer ByteString m r
-> m (Either r (Word8, Producer ByteString m r))
go
  where
    go :: Producer ByteString m b
-> m (Either b (Word8, Producer ByteString m b))
go Producer ByteString m b
p = do
        Either b (ByteString, Producer ByteString m b)
x <- Producer ByteString m b
-> m (Either b (ByteString, Producer ByteString m b))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next Producer ByteString m b
p
        case Either b (ByteString, Producer ByteString m b)
x of
            Left   b
r       -> Either b (Word8, Producer ByteString m b)
-> m (Either b (Word8, Producer ByteString m b))
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> Either b (Word8, Producer ByteString m b)
forall a b. a -> Either a b
Left b
r)
            Right (ByteString
bs, Producer ByteString m b
p') -> case (ByteString -> Maybe (Word8, ByteString)
BS.uncons ByteString
bs) of
                Maybe (Word8, ByteString)
Nothing        -> Producer ByteString m b
-> m (Either b (Word8, Producer ByteString m b))
go Producer ByteString m b
p'
                Just (Word8
w8, ByteString
bs') -> Either b (Word8, Producer ByteString m b)
-> m (Either b (Word8, Producer ByteString m b))
forall (m :: * -> *) a. Monad m => a -> m a
return ((Word8, Producer ByteString m b)
-> Either b (Word8, Producer ByteString m b)
forall a b. b -> Either a b
Right (Word8
w8, ByteString -> Proxy X () () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
bs' Proxy X () () ByteString m ()
-> Producer ByteString m b -> Producer ByteString m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Producer ByteString m b
p'))
{-# INLINABLE nextByte #-}

{-| Draw one 'Word8' from the underlying 'Producer', returning 'Nothing' if the
    'Producer' is empty
-}
drawByte :: Monad m => Parser ByteString m (Maybe Word8)
drawByte :: Parser ByteString m (Maybe Word8)
drawByte = do
    Maybe ByteString
x <- StateT (Producer ByteString m x) m (Maybe ByteString)
forall (m :: * -> *) a. Monad m => Parser a m (Maybe a)
PP.draw
    case Maybe ByteString
x of
        Maybe ByteString
Nothing -> Maybe Word8 -> StateT (Producer ByteString m x) m (Maybe Word8)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Word8
forall a. Maybe a
Nothing
        Just ByteString
bs -> case (ByteString -> Maybe (Word8, ByteString)
BS.uncons ByteString
bs) of
            Maybe (Word8, ByteString)
Nothing        -> StateT (Producer ByteString m x) m (Maybe Word8)
forall (m :: * -> *). Monad m => Parser ByteString m (Maybe Word8)
drawByte
            Just (Word8
w8, ByteString
bs') -> do
                ByteString -> Parser ByteString m ()
forall (m :: * -> *) a. Monad m => a -> Parser a m ()
PP.unDraw ByteString
bs'
                Maybe Word8 -> StateT (Producer ByteString m x) m (Maybe Word8)
forall (m :: * -> *) a. Monad m => a -> m a
return (Word8 -> Maybe Word8
forall a. a -> Maybe a
Just Word8
w8)
{-# INLINABLE drawByte #-}

-- | Push back a 'Word8' onto the underlying 'Producer'
unDrawByte :: Monad m => Word8 -> Parser ByteString m ()
unDrawByte :: Word8 -> Parser ByteString m ()
unDrawByte Word8
w8 = (Proxy X () () ByteString m x -> Proxy X () () ByteString m x)
-> StateT (Proxy X () () ByteString m x) m ()
forall (m :: * -> *) s. Monad m => (s -> s) -> StateT s m ()
modify (ByteString -> Proxy X () () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield (Word8 -> ByteString
BS.singleton Word8
w8) Proxy X () () ByteString m ()
-> Proxy X () () ByteString m x -> Proxy X () () ByteString m x
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>)
{-# INLINABLE unDrawByte #-}

{-| 'peekByte' checks the first 'Word8' in the stream, but uses 'unDrawByte' to
    push the 'Word8' back

> peekByte = do
>     x <- drawByte
>     case x of
>         Nothing -> return ()
>         Just w8 -> unDrawByte w8
>     return x
-}
peekByte :: Monad m => Parser ByteString m (Maybe Word8)
peekByte :: Parser ByteString m (Maybe Word8)
peekByte = do
    Maybe Word8
x <- StateT (Producer ByteString m x) m (Maybe Word8)
forall (m :: * -> *). Monad m => Parser ByteString m (Maybe Word8)
drawByte
    case Maybe Word8
x of
        Maybe Word8
Nothing -> () -> StateT (Producer ByteString m x) m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        Just Word8
w8 -> Word8 -> Parser ByteString m ()
forall (m :: * -> *). Monad m => Word8 -> Parser ByteString m ()
unDrawByte Word8
w8
    Maybe Word8 -> StateT (Producer ByteString m x) m (Maybe Word8)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Word8
x
{-# INLINABLE peekByte #-}

{-| Check if the underlying 'Producer' has no more bytes

    Note that this will skip over empty 'ByteString' chunks, unlike
    'Pipes.Parse.isEndOfInput' from @pipes-parse@.

> isEndOfBytes = liftM isNothing peekByte
-}
isEndOfBytes :: Monad m => Parser ByteString m Bool
isEndOfBytes :: Parser ByteString m Bool
isEndOfBytes = do
    Maybe Word8
x <- StateT (Producer ByteString m x) m (Maybe Word8)
forall (m :: * -> *). Monad m => Parser ByteString m (Maybe Word8)
peekByte
    Bool -> StateT (Producer ByteString m x) m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (case Maybe Word8
x of
        Maybe Word8
Nothing -> Bool
True
        Just Word8
_  -> Bool
False )
{-# INLINABLE isEndOfBytes #-}

type Lens' a b = forall f . Functor f => (b -> f b) -> (a -> f a)

-- | Improper lens that splits a 'Producer' after the given number of bytes
splitAt
    :: (Monad m, Integral n)
    => n
    -> Lens' (Producer ByteString m x)
             (Producer ByteString m (Producer ByteString m x))
splitAt :: n
-> Lens'
     (Producer ByteString m x)
     (Producer ByteString m (Producer ByteString m x))
splitAt n
n0 Producer ByteString m (Producer ByteString m x)
-> f (Producer ByteString m (Producer ByteString m x))
k Producer ByteString m x
p0 = (Producer ByteString m (Producer ByteString m x)
 -> Producer ByteString m x)
-> f (Producer ByteString m (Producer ByteString m x))
-> f (Producer ByteString m x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Producer ByteString m (Producer ByteString m x)
-> Producer ByteString m x
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Producer ByteString m (Producer ByteString m x)
-> f (Producer ByteString m (Producer ByteString m x))
k (n
-> Producer ByteString m x
-> Producer ByteString m (Producer ByteString m x)
forall t (m :: * -> *) b x' x.
(Monad m, Integral t) =>
t
-> Producer ByteString m b
-> Proxy x' x () ByteString m (Producer ByteString m b)
go n
n0 Producer ByteString m x
p0))
  where
    -- go  :: (Monad m, Integral n)
    --     => n
    --     -> Producer ByteString m r
    --     -> Producer' ByteString m (Producer ByteString m r)
    go :: t
-> Producer ByteString m b
-> Proxy x' x () ByteString m (Producer ByteString m b)
go t
n Producer ByteString m b
p =
        if (t
n t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
0)
        then Producer ByteString m b
-> Proxy x' x () ByteString m (Producer ByteString m b)
forall (m :: * -> *) a. Monad m => a -> m a
return Producer ByteString m b
p
        else do
            Either b (ByteString, Producer ByteString m b)
x <- m (Either b (ByteString, Producer ByteString m b))
-> Proxy
     x'
     x
     ()
     ByteString
     m
     (Either b (ByteString, Producer ByteString m b))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Producer ByteString m b
-> m (Either b (ByteString, Producer ByteString m b))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next Producer ByteString m b
p)
            case Either b (ByteString, Producer ByteString m b)
x of
                Left   b
r       -> Producer ByteString m b
-> Proxy x' x () ByteString m (Producer ByteString m b)
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> Producer ByteString m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
r)
                Right (ByteString
bs, Producer ByteString m b
p') -> do
                    let len :: t
len = Int -> t
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
BS.length ByteString
bs)
                    if (t
len t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
n)
                        then do
                            ByteString -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
bs
                            t
-> Producer ByteString m b
-> Proxy x' x () ByteString m (Producer ByteString m b)
go (t
n t -> t -> t
forall a. Num a => a -> a -> a
- t
len) Producer ByteString m b
p'
                        else do
                            let (ByteString
prefix, ByteString
suffix) =
                                    Int -> ByteString -> (ByteString, ByteString)
BS.splitAt (t -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral t
n) ByteString
bs
                            ByteString -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
prefix
                            Producer ByteString m b
-> Proxy x' x () ByteString m (Producer ByteString m b)
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> Proxy X () () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
suffix Proxy X () () ByteString m ()
-> Producer ByteString m b -> Producer ByteString m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Producer ByteString m b
p')
{-# INLINABLE splitAt #-}

{-| Improper lens that splits after the longest consecutive group of bytes that
    satisfy the given predicate
-}
span
    :: Monad m
    => (Word8 -> Bool)
    -> Lens' (Producer ByteString m x)
             (Producer ByteString m (Producer ByteString m x))
span :: (Word8 -> Bool)
-> Lens'
     (Producer ByteString m x)
     (Producer ByteString m (Producer ByteString m x))
span Word8 -> Bool
predicate Producer ByteString m (Producer ByteString m x)
-> f (Producer ByteString m (Producer ByteString m x))
k Producer ByteString m x
p0 = (Producer ByteString m (Producer ByteString m x)
 -> Producer ByteString m x)
-> f (Producer ByteString m (Producer ByteString m x))
-> f (Producer ByteString m x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Producer ByteString m (Producer ByteString m x)
-> Producer ByteString m x
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Producer ByteString m (Producer ByteString m x)
-> f (Producer ByteString m (Producer ByteString m x))
k (Producer ByteString m x
-> Producer ByteString m (Producer ByteString m x)
forall (m :: * -> *) b x' x.
Monad m =>
Producer ByteString m b
-> Proxy x' x () ByteString m (Producer ByteString m b)
go Producer ByteString m x
p0))
  where
    go :: Producer ByteString m b
-> Proxy x' x () ByteString m (Producer ByteString m b)
go Producer ByteString m b
p = do
        Either b (ByteString, Producer ByteString m b)
x <- m (Either b (ByteString, Producer ByteString m b))
-> Proxy
     x'
     x
     ()
     ByteString
     m
     (Either b (ByteString, Producer ByteString m b))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Producer ByteString m b
-> m (Either b (ByteString, Producer ByteString m b))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next Producer ByteString m b
p)
        case Either b (ByteString, Producer ByteString m b)
x of
            Left   b
r       -> Producer ByteString m b
-> Proxy x' x () ByteString m (Producer ByteString m b)
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> Producer ByteString m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
r)
            Right (ByteString
bs, Producer ByteString m b
p') -> do
                let (ByteString
prefix, ByteString
suffix) = (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
BS.span Word8 -> Bool
predicate ByteString
bs
                if (ByteString -> Bool
BS.null ByteString
suffix)
                    then do
                        ByteString -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
bs
                        Producer ByteString m b
-> Proxy x' x () ByteString m (Producer ByteString m b)
go Producer ByteString m b
p'
                    else do
                        ByteString -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
prefix
                        Producer ByteString m b
-> Proxy x' x () ByteString m (Producer ByteString m b)
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> Proxy X () () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
suffix Proxy X () () ByteString m ()
-> Producer ByteString m b -> Producer ByteString m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Producer ByteString m b
p')
{-# INLINABLE span #-}

{-| Improper lens that splits after the longest consecutive group of bytes that
    fail the given predicate
-}
break
    :: Monad m
    => (Word8 -> Bool)
    -> Lens' (Producer ByteString m x)
             (Producer ByteString m (Producer ByteString m x))
break :: (Word8 -> Bool)
-> Lens'
     (Producer ByteString m x)
     (Producer ByteString m (Producer ByteString m x))
break Word8 -> Bool
predicate = (Word8 -> Bool)
-> Lens'
     (Producer ByteString m x)
     (Producer ByteString m (Producer ByteString m x))
forall (m :: * -> *) x.
Monad m =>
(Word8 -> Bool)
-> Lens'
     (Producer ByteString m x)
     (Producer ByteString m (Producer ByteString m x))
span (Bool -> Bool
not (Bool -> Bool) -> (Word8 -> Bool) -> Word8 -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Bool
predicate)
{-# INLINABLE break #-}

{-| Improper lens that splits at the first occurrence of the pattern.
-}
breakOn
    :: Monad m
    => ByteString
    -> Lens' (Producer ByteString m x)
             (Producer ByteString m (Producer ByteString m x))
breakOn :: ByteString
-> Lens'
     (Producer ByteString m x)
     (Producer ByteString m (Producer ByteString m x))
breakOn ByteString
needle Producer ByteString m (Producer ByteString m x)
-> f (Producer ByteString m (Producer ByteString m x))
k Producer ByteString m x
p0 =
    (Producer ByteString m (Producer ByteString m x)
 -> Producer ByteString m x)
-> f (Producer ByteString m (Producer ByteString m x))
-> f (Producer ByteString m x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Producer ByteString m (Producer ByteString m x)
-> Producer ByteString m x
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Producer ByteString m (Producer ByteString m x)
-> f (Producer ByteString m (Producer ByteString m x))
k (ByteString
-> Producer ByteString m x
-> Producer ByteString m (Producer ByteString m x)
forall (m :: * -> *) b x' x.
Monad m =>
ByteString
-> Producer ByteString m b
-> Proxy x' x () ByteString m (Producer ByteString m b)
go ByteString
forall a. Monoid a => a
mempty Producer ByteString m x
p0))
  where
    len0 :: Int
len0 = ByteString -> Int
BS.length ByteString
needle

    go :: ByteString
-> Producer ByteString m b
-> Proxy x' x () ByteString m (Producer ByteString m b)
go ByteString
leftovers Producer ByteString m b
p =
        if ByteString -> Int
BS.length ByteString
leftovers Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
len0
        then do
            Either b (ByteString, Producer ByteString m b)
x <- m (Either b (ByteString, Producer ByteString m b))
-> Proxy
     x'
     x
     ()
     ByteString
     m
     (Either b (ByteString, Producer ByteString m b))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Producer ByteString m b
-> m (Either b (ByteString, Producer ByteString m b))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next Producer ByteString m b
p)
            case Either b (ByteString, Producer ByteString m b)
x of
                Left   b
r          -> do
                    ByteString -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
leftovers
                    Producer ByteString m b
-> Proxy x' x () ByteString m (Producer ByteString m b)
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> Producer ByteString m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
r)
                Right (ByteString
bytes, Producer ByteString m b
p') -> do
                    ByteString
-> Producer ByteString m b
-> Proxy x' x () ByteString m (Producer ByteString m b)
go (ByteString
leftovers ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
bytes) Producer ByteString m b
p'
        else do
            let (ByteString
prefix, ByteString
suffix) = ByteString -> ByteString -> (ByteString, ByteString)
Data.ByteString.Search.breakOn ByteString
needle ByteString
leftovers
            if ByteString -> Bool
BS.null ByteString
suffix
                then do
                    let len :: Int
len = ByteString -> Int
BS.length ByteString
leftovers
                    let (ByteString
output, ByteString
leftovers') =
                            Int -> ByteString -> (ByteString, ByteString)
BS.splitAt (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
len0) ByteString
leftovers
                    ByteString -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
output
                    ByteString
-> Producer ByteString m b
-> Proxy x' x () ByteString m (Producer ByteString m b)
go ByteString
leftovers' Producer ByteString m b
p
                else do
                    ByteString -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
prefix
                    Producer ByteString m b
-> Proxy x' x () ByteString m (Producer ByteString m b)
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> Proxy X () () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
suffix Proxy X () () ByteString m ()
-> Producer ByteString m b -> Producer ByteString m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Producer ByteString m b
p)
{-# INLINABLE breakOn #-}

{-| Improper lens that splits after the first group of matching bytes, as
    defined by the given equality predicate
-}
groupBy
    :: Monad m
    => (Word8 -> Word8 -> Bool)
    -> Lens' (Producer ByteString m x)
             (Producer ByteString m (Producer ByteString m x))
groupBy :: (Word8 -> Word8 -> Bool)
-> Lens'
     (Producer ByteString m x)
     (Producer ByteString m (Producer ByteString m x))
groupBy Word8 -> Word8 -> Bool
equals Producer ByteString m (Producer ByteString m x)
-> f (Producer ByteString m (Producer ByteString m x))
k Producer ByteString m x
p0 = (Producer ByteString m (Producer ByteString m x)
 -> Producer ByteString m x)
-> f (Producer ByteString m (Producer ByteString m x))
-> f (Producer ByteString m x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Producer ByteString m (Producer ByteString m x)
-> Producer ByteString m x
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Producer ByteString m (Producer ByteString m x)
-> f (Producer ByteString m (Producer ByteString m x))
k (Producer ByteString m x
-> Producer ByteString m (Producer ByteString m x)
forall (m :: * -> *) b.
Monad m =>
Producer ByteString m b
-> Proxy X () () ByteString m (Producer ByteString m b)
_groupBy Producer ByteString m x
p0))
  where
    -- _groupBy
    --     :: Monad m
    --     => Producer ByteString m r
    --     -> Producer ByteString m (Producer ByteString m r)
    _groupBy :: Producer ByteString m b
-> Proxy X () () ByteString m (Producer ByteString m b)
_groupBy Producer ByteString m b
p = do
        Either b (ByteString, Producer ByteString m b)
x <- m (Either b (ByteString, Producer ByteString m b))
-> Proxy
     X
     ()
     ()
     ByteString
     m
     (Either b (ByteString, Producer ByteString m b))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Producer ByteString m b
-> m (Either b (ByteString, Producer ByteString m b))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next Producer ByteString m b
p)
        case Either b (ByteString, Producer ByteString m b)
x of
            Left   b
r       -> Producer ByteString m b
-> Proxy X () () ByteString m (Producer ByteString m b)
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> Producer ByteString m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
r)
            Right (ByteString
bs, Producer ByteString m b
p') -> case (ByteString -> Maybe (Word8, ByteString)
BS.uncons ByteString
bs) of
                Maybe (Word8, ByteString)
Nothing      -> Producer ByteString m b
-> Proxy X () () ByteString m (Producer ByteString m b)
_groupBy Producer ByteString m b
p'
                Just (Word8
w8, ByteString
_) -> (ByteString -> Proxy X () () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
bs Proxy X () () ByteString m ()
-> Producer ByteString m b -> Producer ByteString m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Producer ByteString m b
p')Producer ByteString m b
-> ((Proxy X () () ByteString m (Producer ByteString m b)
     -> Constant
          (Proxy X () () ByteString m (Producer ByteString m b))
          (Proxy X () () ByteString m (Producer ByteString m b)))
    -> Producer ByteString m b
    -> Constant
         (Proxy X () () ByteString m (Producer ByteString m b))
         (Producer ByteString m b))
-> Proxy X () () ByteString m (Producer ByteString m b)
forall a b. a -> ((b -> Constant b b) -> a -> Constant b a) -> b
^.(Word8 -> Bool)
-> Lens'
     (Producer ByteString m b)
     (Proxy X () () ByteString m (Producer ByteString m b))
forall (m :: * -> *) x.
Monad m =>
(Word8 -> Bool)
-> Lens'
     (Producer ByteString m x)
     (Producer ByteString m (Producer ByteString m x))
span (Word8 -> Word8 -> Bool
equals Word8
w8)
{-# INLINABLE groupBy #-}

-- | Like 'groupBy', where the equality predicate is ('==')
group
    :: Monad m
    => Lens' (Producer ByteString m x)
             (Producer ByteString m (Producer ByteString m x))
group :: Lens'
  (Producer ByteString m x)
  (Producer ByteString m (Producer ByteString m x))
group = (Word8 -> Word8 -> Bool)
-> Lens'
     (Producer ByteString m x)
     (Producer ByteString m (Producer ByteString m x))
forall (m :: * -> *) x.
Monad m =>
(Word8 -> Word8 -> Bool)
-> Lens'
     (Producer ByteString m x)
     (Producer ByteString m (Producer ByteString m x))
groupBy Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
(==)
{-# INLINABLE group #-}

{-| Improper lens that splits a 'Producer' after the first word

    Unlike 'words', this does not drop leading whitespace

    Note: This function is purely for demonstration purposes since it assumes a
    particular encoding.  You should prefer the 'Data.Text.Text' equivalent of
    this function from the @pipes-text@ library.
-}
word
    :: Monad m
    => Lens' (Producer ByteString m x)
             (Producer ByteString m (Producer ByteString m x))
word :: Lens'
  (Producer ByteString m x)
  (Producer ByteString m (Producer ByteString m x))
word Producer ByteString m (Producer ByteString m x)
-> f (Producer ByteString m (Producer ByteString m x))
k Producer ByteString m x
p0 = (Producer ByteString m (Producer ByteString m x)
 -> Producer ByteString m x)
-> f (Producer ByteString m (Producer ByteString m x))
-> f (Producer ByteString m x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Producer ByteString m (Producer ByteString m x)
-> Producer ByteString m x
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Producer ByteString m (Producer ByteString m x)
-> f (Producer ByteString m (Producer ByteString m x))
k (Producer ByteString m x
-> Producer ByteString m (Producer ByteString m x)
forall (m :: * -> *) b.
Monad m =>
Producer ByteString m b
-> Proxy X () () ByteString m (Producer ByteString m b)
to Producer ByteString m x
p0))
  where
    -- to
    --     :: Monad m
    --     => Producer ByteString m r
    --     -> Producer ByteString m (Producer ByteString m r)
    to :: Producer ByteString m x
-> Proxy X () () ByteString m (Producer ByteString m x)
to Producer ByteString m x
p = do
        Producer ByteString m x
p' <- Producer ByteString m x
pProducer ByteString m x
-> ((Proxy X () () ByteString m (Producer ByteString m x)
     -> Constant
          (Proxy X () () ByteString m (Producer ByteString m x))
          (Proxy X () () ByteString m (Producer ByteString m x)))
    -> Producer ByteString m x
    -> Constant
         (Proxy X () () ByteString m (Producer ByteString m x))
         (Producer ByteString m x))
-> Proxy X () () ByteString m (Producer ByteString m x)
forall a b. a -> ((b -> Constant b b) -> a -> Constant b a) -> b
^.(Word8 -> Bool)
-> Lens'
     (Producer ByteString m x)
     (Proxy X () () ByteString m (Producer ByteString m x))
forall (m :: * -> *) x.
Monad m =>
(Word8 -> Bool)
-> Lens'
     (Producer ByteString m x)
     (Producer ByteString m (Producer ByteString m x))
span Word8 -> Bool
isSpaceWord8
        Producer ByteString m x
p'Producer ByteString m x
-> ((Proxy X () () ByteString m (Producer ByteString m x)
     -> Constant
          (Proxy X () () ByteString m (Producer ByteString m x))
          (Proxy X () () ByteString m (Producer ByteString m x)))
    -> Producer ByteString m x
    -> Constant
         (Proxy X () () ByteString m (Producer ByteString m x))
         (Producer ByteString m x))
-> Proxy X () () ByteString m (Producer ByteString m x)
forall a b. a -> ((b -> Constant b b) -> a -> Constant b a) -> b
^.(Word8 -> Bool)
-> Lens'
     (Producer ByteString m x)
     (Proxy X () () ByteString m (Producer ByteString m x))
forall (m :: * -> *) x.
Monad m =>
(Word8 -> Bool)
-> Lens'
     (Producer ByteString m x)
     (Producer ByteString m (Producer ByteString m x))
break Word8 -> Bool
isSpaceWord8
{-# INLINABLE word #-}

nl :: Word8
nl :: Word8
nl = Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Char -> Int
ord Char
'\n')

{-| Improper lens that splits a 'Producer' after the first line

    Unlike 'lines', this does not consume the newline marker, which is stored
    within the inner 'Producer'

    Note: This function is purely for demonstration purposes since it assumes a
    particular encoding.  You should prefer the 'Data.Text.Text' equivalent of
    this function from the @pipes-text@ library.
-}
line
    :: Monad m
    => Lens' (Producer ByteString m x)
             (Producer ByteString m (Producer ByteString m x))
line :: Lens'
  (Producer ByteString m x)
  (Producer ByteString m (Producer ByteString m x))
line = (Word8 -> Bool)
-> Lens'
     (Producer ByteString m x)
     (Producer ByteString m (Producer ByteString m x))
forall (m :: * -> *) x.
Monad m =>
(Word8 -> Bool)
-> Lens'
     (Producer ByteString m x)
     (Producer ByteString m (Producer ByteString m x))
break (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
nl)
{-# INLINABLE line #-}

-- | @(drop n)@ drops the first @n@ bytes
drop
    :: (Monad m, Integral n)
    => n -> Producer ByteString m r -> Producer ByteString m r
drop :: n -> Producer ByteString m r -> Producer ByteString m r
drop n
n Producer ByteString m r
p = do
    Producer ByteString m r
p' <- m (Producer ByteString m r)
-> Proxy X () () ByteString m (Producer ByteString m r)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Producer ByteString m r)
 -> Proxy X () () ByteString m (Producer ByteString m r))
-> m (Producer ByteString m r)
-> Proxy X () () ByteString m (Producer ByteString m r)
forall a b. (a -> b) -> a -> b
$ Effect m (Producer ByteString m r) -> m (Producer ByteString m r)
forall (m :: * -> *) r. Monad m => Effect m r -> m r
runEffect (Proxy X () () ByteString m (Producer ByteString m r)
-> (ByteString -> Proxy X () () X m ())
-> Effect m (Producer ByteString m r)
forall (m :: * -> *) x' x b' b a' c' c.
Functor m =>
Proxy x' x b' b m a'
-> (b -> Proxy x' x c' c m b') -> Proxy x' x c' c m a'
for (Producer ByteString m r
p Producer ByteString m r
-> ((Proxy X () () ByteString m (Producer ByteString m r)
     -> Constant
          (Proxy X () () ByteString m (Producer ByteString m r))
          (Proxy X () () ByteString m (Producer ByteString m r)))
    -> Producer ByteString m r
    -> Constant
         (Proxy X () () ByteString m (Producer ByteString m r))
         (Producer ByteString m r))
-> Proxy X () () ByteString m (Producer ByteString m r)
forall a b. a -> ((b -> Constant b b) -> a -> Constant b a) -> b
^. n
-> Lens'
     (Producer ByteString m r)
     (Proxy X () () ByteString m (Producer ByteString m r))
forall (m :: * -> *) n x.
(Monad m, Integral n) =>
n
-> Lens'
     (Producer ByteString m x)
     (Producer ByteString m (Producer ByteString m x))
splitAt n
n) ByteString -> Proxy X () () X m ()
forall (m :: * -> *) a. Monad m => a -> m ()
discard)
    Producer ByteString m r
p'
{-# INLINABLE drop #-}

-- | Drop bytes until they fail the predicate
dropWhile
    :: Monad m
    => (Word8 -> Bool) -> Producer ByteString m r -> Producer ByteString m r
dropWhile :: (Word8 -> Bool)
-> Producer ByteString m r -> Producer ByteString m r
dropWhile Word8 -> Bool
predicate Producer ByteString m r
p = do
    Producer ByteString m r
p' <- m (Producer ByteString m r)
-> Proxy X () () ByteString m (Producer ByteString m r)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Producer ByteString m r)
 -> Proxy X () () ByteString m (Producer ByteString m r))
-> m (Producer ByteString m r)
-> Proxy X () () ByteString m (Producer ByteString m r)
forall a b. (a -> b) -> a -> b
$ Effect m (Producer ByteString m r) -> m (Producer ByteString m r)
forall (m :: * -> *) r. Monad m => Effect m r -> m r
runEffect (Proxy X () () ByteString m (Producer ByteString m r)
-> (ByteString -> Proxy X () () X m ())
-> Effect m (Producer ByteString m r)
forall (m :: * -> *) x' x b' b a' c' c.
Functor m =>
Proxy x' x b' b m a'
-> (b -> Proxy x' x c' c m b') -> Proxy x' x c' c m a'
for (Producer ByteString m r
p Producer ByteString m r
-> ((Proxy X () () ByteString m (Producer ByteString m r)
     -> Constant
          (Proxy X () () ByteString m (Producer ByteString m r))
          (Proxy X () () ByteString m (Producer ByteString m r)))
    -> Producer ByteString m r
    -> Constant
         (Proxy X () () ByteString m (Producer ByteString m r))
         (Producer ByteString m r))
-> Proxy X () () ByteString m (Producer ByteString m r)
forall a b. a -> ((b -> Constant b b) -> a -> Constant b a) -> b
^. (Word8 -> Bool)
-> Lens'
     (Producer ByteString m r)
     (Proxy X () () ByteString m (Producer ByteString m r))
forall (m :: * -> *) x.
Monad m =>
(Word8 -> Bool)
-> Lens'
     (Producer ByteString m x)
     (Producer ByteString m (Producer ByteString m x))
span Word8 -> Bool
predicate) ByteString -> Proxy X () () X m ()
forall (m :: * -> *) a. Monad m => a -> m ()
discard)
    Producer ByteString m r
p'
{-# INLINABLE dropWhile #-}

-- | Intersperse a 'Word8' in between the bytes of the byte stream
intersperse
    :: Monad m => Word8 -> Producer ByteString m r -> Producer ByteString m r
intersperse :: Word8 -> Producer ByteString m r -> Producer ByteString m r
intersperse Word8
w8 = Producer ByteString m r -> Producer ByteString m r
forall (m :: * -> *) b x' x.
Monad m =>
Producer ByteString m b -> Proxy x' x () ByteString m b
go0
  where
    go0 :: Producer ByteString m b -> Proxy x' x () ByteString m b
go0 Producer ByteString m b
p = do
        Either b (ByteString, Producer ByteString m b)
x <- m (Either b (ByteString, Producer ByteString m b))
-> Proxy
     x'
     x
     ()
     ByteString
     m
     (Either b (ByteString, Producer ByteString m b))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Producer ByteString m b
-> m (Either b (ByteString, Producer ByteString m b))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next Producer ByteString m b
p)
        case Either b (ByteString, Producer ByteString m b)
x of
            Left   b
r       -> b -> Proxy x' x () ByteString m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
r
            Right (ByteString
bs, Producer ByteString m b
p') -> do
                ByteString -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield (Word8 -> ByteString -> ByteString
BS.intersperse Word8
w8 ByteString
bs)
                Producer ByteString m b -> Proxy x' x () ByteString m b
forall (m :: * -> *) b x' x.
Monad m =>
Producer ByteString m b -> Proxy x' x () ByteString m b
go1 Producer ByteString m b
p'
    go1 :: Producer ByteString m b -> Proxy x' x () ByteString m b
go1 Producer ByteString m b
p = do
        Either b (ByteString, Producer ByteString m b)
x <- m (Either b (ByteString, Producer ByteString m b))
-> Proxy
     x'
     x
     ()
     ByteString
     m
     (Either b (ByteString, Producer ByteString m b))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Producer ByteString m b
-> m (Either b (ByteString, Producer ByteString m b))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next Producer ByteString m b
p)
        case Either b (ByteString, Producer ByteString m b)
x of
            Left   b
r       -> b -> Proxy x' x () ByteString m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
r
            Right (ByteString
bs, Producer ByteString m b
p') -> do
                ByteString -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield (Word8 -> ByteString
BS.singleton Word8
w8)
                ByteString -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield (Word8 -> ByteString -> ByteString
BS.intersperse Word8
w8 ByteString
bs)
                Producer ByteString m b -> Proxy x' x () ByteString m b
go1 Producer ByteString m b
p'
{-# INLINABLE intersperse #-}

-- | Improper lens from unpacked 'Word8's to packaged 'ByteString's
pack :: Monad m => Lens' (Producer Word8 m x) (Producer ByteString m x)
pack :: Lens' (Producer Word8 m x) (Producer ByteString m x)
pack Producer ByteString m x -> f (Producer ByteString m x)
k Producer Word8 m x
p = (Producer ByteString m x -> Producer Word8 m x)
-> f (Producer ByteString m x) -> f (Producer Word8 m x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Producer ByteString m x -> Producer Word8 m x
forall (m :: * -> *) x.
Monad m =>
Producer ByteString m x -> Producer Word8 m x
_unpack (Producer ByteString m x -> f (Producer ByteString m x)
k (Producer Word8 m x -> Producer ByteString m x
forall (m :: * -> *) x.
Monad m =>
Producer Word8 m x -> Producer ByteString m x
_pack Producer Word8 m x
p))
{-# INLINABLE pack #-}

-- | Improper lens from packed 'ByteString's to unpacked 'Word8's
unpack :: Monad m => Lens' (Producer ByteString m x) (Producer Word8 m x)
unpack :: Lens' (Producer ByteString m x) (Producer Word8 m x)
unpack Producer Word8 m x -> f (Producer Word8 m x)
k Producer ByteString m x
p = (Producer Word8 m x -> Producer ByteString m x)
-> f (Producer Word8 m x) -> f (Producer ByteString m x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Producer Word8 m x -> Producer ByteString m x
forall (m :: * -> *) x.
Monad m =>
Producer Word8 m x -> Producer ByteString m x
_pack (Producer Word8 m x -> f (Producer Word8 m x)
k (Producer ByteString m x -> Producer Word8 m x
forall (m :: * -> *) x.
Monad m =>
Producer ByteString m x -> Producer Word8 m x
_unpack Producer ByteString m x
p))
{-# INLINABLE unpack #-}

_pack :: Monad m => Producer Word8 m x -> Producer ByteString m x
_pack :: Producer Word8 m x -> Producer ByteString m x
_pack Producer Word8 m x
p = (([Word8] -> [Word8]) -> Word8 -> [Word8] -> [Word8])
-> ([Word8] -> [Word8])
-> (([Word8] -> [Word8]) -> ByteString)
-> FreeT (Producer Word8 m) m x
-> Producer ByteString m x
forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x)
-> x -> (x -> b) -> FreeT (Producer a m) m r -> Producer b m r
PG.folds ([Word8] -> [Word8]) -> Word8 -> [Word8] -> [Word8]
forall a c. ([a] -> c) -> a -> [a] -> c
step [Word8] -> [Word8]
forall a. a -> a
id ([Word8] -> [Word8]) -> ByteString
forall a. ([a] -> [Word8]) -> ByteString
done (Producer Word8 m x
pProducer Word8 m x
-> ((FreeT (Producer Word8 m) m x
     -> Constant
          (FreeT (Producer Word8 m) m x) (FreeT (Producer Word8 m) m x))
    -> Producer Word8 m x
    -> Constant (FreeT (Producer Word8 m) m x) (Producer Word8 m x))
-> FreeT (Producer Word8 m) m x
forall a b. a -> ((b -> Constant b b) -> a -> Constant b a) -> b
^.Int
-> Lens
     (Producer Word8 m x)
     (Producer Word8 m x)
     (FreeT (Producer Word8 m) m x)
     (FreeT (Producer Word8 m) m x)
forall (m :: * -> *) a' x a.
Monad m =>
Int
-> Lens
     (Producer a' m x)
     (Producer a m x)
     (FreeT (Producer a' m) m x)
     (FreeT (Producer a m) m x)
PG.chunksOf Int
defaultChunkSize)
  where
    step :: ([a] -> c) -> a -> [a] -> c
step [a] -> c
diffAs a
w8 = [a] -> c
diffAs ([a] -> c) -> ([a] -> [a]) -> [a] -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
w8a -> [a] -> [a]
forall a. a -> [a] -> [a]
:)

    done :: ([a] -> [Word8]) -> ByteString
done [a] -> [Word8]
diffAs = [Word8] -> ByteString
BS.pack ([a] -> [Word8]
diffAs [])
{-# INLINABLE _pack #-}

_unpack :: Monad m => Producer ByteString m x -> Producer Word8 m x
_unpack :: Producer ByteString m x -> Producer Word8 m x
_unpack Producer ByteString m x
p = Producer ByteString m x
-> (ByteString -> Proxy X () () Word8 m ()) -> Producer Word8 m x
forall (m :: * -> *) x' x b' b a' c' c.
Functor m =>
Proxy x' x b' b m a'
-> (b -> Proxy x' x c' c m b') -> Proxy x' x c' c m a'
for Producer ByteString m x
p ([Word8] -> Proxy X () () Word8 m ()
forall (m :: * -> *) (f :: * -> *) a x' x.
(Functor m, Foldable f) =>
f a -> Proxy x' x () a m ()
each ([Word8] -> Proxy X () () Word8 m ())
-> (ByteString -> [Word8])
-> ByteString
-> Proxy X () () Word8 m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [Word8]
BS.unpack)
{-# INLINABLE _unpack #-}

{-| Group byte stream chunks into chunks of fixed length

    Note: This is the /only/ function in this API that concatenates
    'ByteString' chunks, which requires allocating new `ByteString`s
-}
chunksOf'
    :: (Monad m, Integral n)
    => n -> Producer ByteString m r -> Producer ByteString m r
chunksOf' :: n -> Producer ByteString m r -> Producer ByteString m r
chunksOf' n
n Producer ByteString m r
p =
    (([ByteString] -> [ByteString])
 -> ByteString -> [ByteString] -> [ByteString])
-> ([ByteString] -> [ByteString])
-> (([ByteString] -> [ByteString]) -> ByteString)
-> FreeT (Producer ByteString m) m r
-> Producer ByteString m r
forall (m :: * -> *) x a b r.
Monad m =>
(x -> a -> x)
-> x -> (x -> b) -> FreeT (Producer a m) m r -> Producer b m r
PG.folds
        (\[ByteString] -> [ByteString]
diffBs ByteString
bs -> [ByteString] -> [ByteString]
diffBs ([ByteString] -> [ByteString])
-> ([ByteString] -> [ByteString]) -> [ByteString] -> [ByteString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString
bsByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
:))
        [ByteString] -> [ByteString]
forall a. a -> a
id
        (\[ByteString] -> [ByteString]
diffBs -> [ByteString] -> ByteString
BS.concat ([ByteString] -> [ByteString]
diffBs []))
        (Producer ByteString m r
p Producer ByteString m r
-> ((FreeT (Producer ByteString m) m r
     -> Constant
          (FreeT (Producer ByteString m) m r)
          (FreeT (Producer ByteString m) m r))
    -> Producer ByteString m r
    -> Constant
         (FreeT (Producer ByteString m) m r) (Producer ByteString m r))
-> FreeT (Producer ByteString m) m r
forall a b. a -> ((b -> Constant b b) -> a -> Constant b a) -> b
^. n
-> Lens'
     (Producer ByteString m r) (FreeT (Producer ByteString m) m r)
forall (m :: * -> *) n x.
(Monad m, Integral n) =>
n
-> Lens'
     (Producer ByteString m x) (FreeT (Producer ByteString m) m x)
chunksOf n
n)
{-# INLINABLE chunksOf' #-}

-- | Split a byte stream into 'FreeT'-delimited byte streams of fixed size
chunksOf
    :: (Monad m, Integral n)
    => n -> Lens' (Producer ByteString m x) (FreeT (Producer ByteString m) m x)
chunksOf :: n
-> Lens'
     (Producer ByteString m x) (FreeT (Producer ByteString m) m x)
chunksOf n
n FreeT (Producer ByteString m) m x
-> f (FreeT (Producer ByteString m) m x)
k Producer ByteString m x
p0 = (FreeT (Producer ByteString m) m x -> Producer ByteString m x)
-> f (FreeT (Producer ByteString m) m x)
-> f (Producer ByteString m x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap FreeT (Producer ByteString m) m x -> Producer ByteString m x
forall (m :: * -> *) a x.
Monad m =>
FreeT (Producer a m) m x -> Producer a m x
concats (FreeT (Producer ByteString m) m x
-> f (FreeT (Producer ByteString m) m x)
k (Producer ByteString m x -> FreeT (Producer ByteString m) m x
forall (m :: * -> *) a.
Monad m =>
Producer ByteString m a -> FreeT (Proxy X () () ByteString m) m a
go Producer ByteString m x
p0))
  where
    go :: Producer ByteString m a -> FreeT (Proxy X () () ByteString m) m a
go Producer ByteString m a
p = m (FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a))
-> FreeT (Proxy X () () ByteString m) m a
forall (f :: * -> *) (m :: * -> *) a.
m (FreeF f a (FreeT f m a)) -> FreeT f m a
PG.FreeT (m (FreeF
      (Proxy X () () ByteString m)
      a
      (FreeT (Proxy X () () ByteString m) m a))
 -> FreeT (Proxy X () () ByteString m) m a)
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
-> FreeT (Proxy X () () ByteString m) m a
forall a b. (a -> b) -> a -> b
$ do
        Either a (ByteString, Producer ByteString m a)
x <- Producer ByteString m a
-> m (Either a (ByteString, Producer ByteString m a))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next Producer ByteString m a
p
        FreeF
  (Proxy X () () ByteString m)
  a
  (FreeT (Proxy X () () ByteString m) m a)
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
forall (m :: * -> *) a. Monad m => a -> m a
return (FreeF
   (Proxy X () () ByteString m)
   a
   (FreeT (Proxy X () () ByteString m) m a)
 -> m (FreeF
         (Proxy X () () ByteString m)
         a
         (FreeT (Proxy X () () ByteString m) m a)))
-> FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a)
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
forall a b. (a -> b) -> a -> b
$ case Either a (ByteString, Producer ByteString m a)
x of
            Left   a
r       -> a
-> FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a)
forall (f :: * -> *) a b. a -> FreeF f a b
PG.Pure a
r
            Right (ByteString
bs, Producer ByteString m a
p') -> Proxy X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
-> FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a)
forall (f :: * -> *) a b. f b -> FreeF f a b
PG.Free (Proxy
   X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
 -> FreeF
      (Proxy X () () ByteString m)
      a
      (FreeT (Proxy X () () ByteString m) m a))
-> Proxy
     X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
-> FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a)
forall a b. (a -> b) -> a -> b
$ do
                Producer ByteString m a
p'' <- (ByteString -> Proxy X () () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
bs Proxy X () () ByteString m ()
-> Producer ByteString m a -> Producer ByteString m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Producer ByteString m a
p')Producer ByteString m a
-> ((Producer ByteString m (Producer ByteString m a)
     -> Constant
          (Producer ByteString m (Producer ByteString m a))
          (Producer ByteString m (Producer ByteString m a)))
    -> Producer ByteString m a
    -> Constant
         (Producer ByteString m (Producer ByteString m a))
         (Producer ByteString m a))
-> Producer ByteString m (Producer ByteString m a)
forall a b. a -> ((b -> Constant b b) -> a -> Constant b a) -> b
^.n
-> Lens'
     (Producer ByteString m a)
     (Producer ByteString m (Producer ByteString m a))
forall (m :: * -> *) n x.
(Monad m, Integral n) =>
n
-> Lens'
     (Producer ByteString m x)
     (Producer ByteString m (Producer ByteString m x))
splitAt n
n
                FreeT (Proxy X () () ByteString m) m a
-> Proxy
     X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Producer ByteString m a -> FreeT (Proxy X () () ByteString m) m a
go Producer ByteString m a
p'')
{-# INLINABLE chunksOf #-}

{-| Split a byte stream into groups separated by bytes that satisfy the
    predicate
-}
splitsWith
    :: Monad m
    => (Word8 -> Bool)
    -> Producer ByteString m x -> FreeT (Producer ByteString m) m x
splitsWith :: (Word8 -> Bool)
-> Producer ByteString m x -> FreeT (Producer ByteString m) m x
splitsWith Word8 -> Bool
predicate Producer ByteString m x
p0 = m (FreeF
     (Producer ByteString m) x (FreeT (Producer ByteString m) m x))
-> FreeT (Producer ByteString m) m x
forall (f :: * -> *) (m :: * -> *) a.
m (FreeF f a (FreeT f m a)) -> FreeT f m a
PG.FreeT (Producer ByteString m x
-> m (FreeF
        (Producer ByteString m) x (FreeT (Producer ByteString m) m x))
forall (m :: * -> *) a.
Monad m =>
Producer ByteString m a
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
go0 Producer ByteString m x
p0)
  where
    go0 :: Producer ByteString m a
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
go0 Producer ByteString m a
p = do
        Either a (ByteString, Producer ByteString m a)
x <- Producer ByteString m a
-> m (Either a (ByteString, Producer ByteString m a))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next Producer ByteString m a
p
        case Either a (ByteString, Producer ByteString m a)
x of
            Left   a
r       -> FreeF
  (Proxy X () () ByteString m)
  a
  (FreeT (Proxy X () () ByteString m) m a)
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
forall (m :: * -> *) a. Monad m => a -> m a
return (a
-> FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a)
forall (f :: * -> *) a b. a -> FreeF f a b
PG.Pure a
r)
            Right (ByteString
bs, Producer ByteString m a
p') ->
                if (ByteString -> Bool
BS.null ByteString
bs)
                then Producer ByteString m a
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
go0 Producer ByteString m a
p'
                else Producer ByteString m a
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
forall (m :: * -> *) a.
Monad m =>
Producer ByteString m a
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
go1 (ByteString -> Proxy X () () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
bs Proxy X () () ByteString m ()
-> Producer ByteString m a -> Producer ByteString m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Producer ByteString m a
p')
    go1 :: Producer ByteString m a
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
go1 Producer ByteString m a
p = FreeF
  (Proxy X () () ByteString m)
  a
  (FreeT (Proxy X () () ByteString m) m a)
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
forall (m :: * -> *) a. Monad m => a -> m a
return (FreeF
   (Proxy X () () ByteString m)
   a
   (FreeT (Proxy X () () ByteString m) m a)
 -> m (FreeF
         (Proxy X () () ByteString m)
         a
         (FreeT (Proxy X () () ByteString m) m a)))
-> FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a)
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
forall a b. (a -> b) -> a -> b
$ Proxy X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
-> FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a)
forall (f :: * -> *) a b. f b -> FreeF f a b
PG.Free (Proxy
   X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
 -> FreeF
      (Proxy X () () ByteString m)
      a
      (FreeT (Proxy X () () ByteString m) m a))
-> Proxy
     X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
-> FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a)
forall a b. (a -> b) -> a -> b
$ do
        Producer ByteString m a
p' <- Producer ByteString m a
pProducer ByteString m a
-> ((Producer ByteString m (Producer ByteString m a)
     -> Constant
          (Producer ByteString m (Producer ByteString m a))
          (Producer ByteString m (Producer ByteString m a)))
    -> Producer ByteString m a
    -> Constant
         (Producer ByteString m (Producer ByteString m a))
         (Producer ByteString m a))
-> Producer ByteString m (Producer ByteString m a)
forall a b. a -> ((b -> Constant b b) -> a -> Constant b a) -> b
^.(Word8 -> Bool)
-> Lens'
     (Producer ByteString m a)
     (Producer ByteString m (Producer ByteString m a))
forall (m :: * -> *) x.
Monad m =>
(Word8 -> Bool)
-> Lens'
     (Producer ByteString m x)
     (Producer ByteString m (Producer ByteString m x))
break Word8 -> Bool
predicate
        FreeT (Proxy X () () ByteString m) m a
-> Proxy
     X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
forall (m :: * -> *) a. Monad m => a -> m a
return (FreeT (Proxy X () () ByteString m) m a
 -> Proxy
      X () () ByteString m (FreeT (Proxy X () () ByteString m) m a))
-> FreeT (Proxy X () () ByteString m) m a
-> Proxy
     X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
forall a b. (a -> b) -> a -> b
$ m (FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a))
-> FreeT (Proxy X () () ByteString m) m a
forall (f :: * -> *) (m :: * -> *) a.
m (FreeF f a (FreeT f m a)) -> FreeT f m a
PG.FreeT (m (FreeF
      (Proxy X () () ByteString m)
      a
      (FreeT (Proxy X () () ByteString m) m a))
 -> FreeT (Proxy X () () ByteString m) m a)
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
-> FreeT (Proxy X () () ByteString m) m a
forall a b. (a -> b) -> a -> b
$ do
            Either a (Word8, Producer ByteString m a)
x <- Producer ByteString m a
-> m (Either a (Word8, Producer ByteString m a))
forall (m :: * -> *) r.
Monad m =>
Producer ByteString m r
-> m (Either r (Word8, Producer ByteString m r))
nextByte Producer ByteString m a
p'
            case Either a (Word8, Producer ByteString m a)
x of
                Left   a
r       -> FreeF
  (Proxy X () () ByteString m)
  a
  (FreeT (Proxy X () () ByteString m) m a)
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
forall (m :: * -> *) a. Monad m => a -> m a
return (a
-> FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a)
forall (f :: * -> *) a b. a -> FreeF f a b
PG.Pure a
r)
                Right (Word8
_, Producer ByteString m a
p'') -> Producer ByteString m a
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
go1 Producer ByteString m a
p''
{-# INLINABLE splitsWith #-}

-- | Split a byte stream into groups separated by the given byte
splits
    :: Monad m
    => Word8
    -> Lens' (Producer ByteString m x) (FreeT (Producer ByteString m) m x)
splits :: Word8
-> Lens'
     (Producer ByteString m x) (FreeT (Producer ByteString m) m x)
splits Word8
w8 FreeT (Producer ByteString m) m x
-> f (FreeT (Producer ByteString m) m x)
k Producer ByteString m x
p =
    (FreeT (Producer ByteString m) m x -> Producer ByteString m x)
-> f (FreeT (Producer ByteString m) m x)
-> f (Producer ByteString m x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Producer ByteString m ()
-> FreeT (Producer ByteString m) m x -> Producer ByteString m x
forall (m :: * -> *) a x.
Monad m =>
Producer a m () -> FreeT (Producer a m) m x -> Producer a m x
PG.intercalates (ByteString -> Producer ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield (Word8 -> ByteString
BS.singleton Word8
w8))) (FreeT (Producer ByteString m) m x
-> f (FreeT (Producer ByteString m) m x)
k ((Word8 -> Bool)
-> Producer ByteString m x -> FreeT (Producer ByteString m) m x
forall (m :: * -> *) x.
Monad m =>
(Word8 -> Bool)
-> Producer ByteString m x -> FreeT (Producer ByteString m) m x
splitsWith (Word8
w8 Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
==) Producer ByteString m x
p))
{-# INLINABLE splits #-}

-- | Split a byte stream into groups separated by the given `ByteString`
splitOn
    :: Monad m
    => ByteString
    -> Lens' (Producer ByteString m x) (FreeT (Producer ByteString m) m x)
splitOn :: ByteString
-> Lens'
     (Producer ByteString m x) (FreeT (Producer ByteString m) m x)
splitOn ByteString
needle FreeT (Producer ByteString m) m x
-> f (FreeT (Producer ByteString m) m x)
k Producer ByteString m x
p0 =
    (FreeT (Producer ByteString m) m x -> Producer ByteString m x)
-> f (FreeT (Producer ByteString m) m x)
-> f (Producer ByteString m x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
        (Producer ByteString m ()
-> FreeT (Producer ByteString m) m x -> Producer ByteString m x
forall (m :: * -> *) a x.
Monad m =>
Producer a m () -> FreeT (Producer a m) m x -> Producer a m x
PG.intercalates (ByteString -> Producer ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
needle))
        (FreeT (Producer ByteString m) m x
-> f (FreeT (Producer ByteString m) m x)
k (Producer ByteString m x -> FreeT (Producer ByteString m) m x
forall (m :: * -> *) a.
Monad m =>
Producer ByteString m a -> FreeT (Proxy X () () ByteString m) m a
go Producer ByteString m x
p0))
  where
    len0 :: Int
len0 = ByteString -> Int
BS.length ByteString
needle
    go :: Producer ByteString m a -> FreeT (Proxy X () () ByteString m) m a
go Producer ByteString m a
p = m (FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a))
-> FreeT (Proxy X () () ByteString m) m a
forall (f :: * -> *) (m :: * -> *) a.
m (FreeF f a (FreeT f m a)) -> FreeT f m a
PG.FreeT (m (FreeF
      (Proxy X () () ByteString m)
      a
      (FreeT (Proxy X () () ByteString m) m a))
 -> FreeT (Proxy X () () ByteString m) m a)
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
-> FreeT (Proxy X () () ByteString m) m a
forall a b. (a -> b) -> a -> b
$ do
        Either a (ByteString, Producer ByteString m a)
x <- Producer ByteString m a
-> m (Either a (ByteString, Producer ByteString m a))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next Producer ByteString m a
p
        FreeF
  (Proxy X () () ByteString m)
  a
  (FreeT (Proxy X () () ByteString m) m a)
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
forall (m :: * -> *) a. Monad m => a -> m a
return (FreeF
   (Proxy X () () ByteString m)
   a
   (FreeT (Proxy X () () ByteString m) m a)
 -> m (FreeF
         (Proxy X () () ByteString m)
         a
         (FreeT (Proxy X () () ByteString m) m a)))
-> FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a)
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
forall a b. (a -> b) -> a -> b
$ case Either a (ByteString, Producer ByteString m a)
x of
            Left   a
r       -> a
-> FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a)
forall (f :: * -> *) a b. a -> FreeF f a b
PG.Pure a
r
            Right (ByteString
bs, Producer ByteString m a
p') -> Proxy X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
-> FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a)
forall (f :: * -> *) a b. f b -> FreeF f a b
PG.Free (Proxy
   X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
 -> FreeF
      (Proxy X () () ByteString m)
      a
      (FreeT (Proxy X () () ByteString m) m a))
-> Proxy
     X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
-> FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a)
forall a b. (a -> b) -> a -> b
$ do
                Producer ByteString m a
p'' <- (ByteString -> Proxy X () () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
bs Proxy X () () ByteString m ()
-> Producer ByteString m a -> Producer ByteString m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Producer ByteString m a
p')Producer ByteString m a
-> ((Producer ByteString m (Producer ByteString m a)
     -> Constant
          (Producer ByteString m (Producer ByteString m a))
          (Producer ByteString m (Producer ByteString m a)))
    -> Producer ByteString m a
    -> Constant
         (Producer ByteString m (Producer ByteString m a))
         (Producer ByteString m a))
-> Producer ByteString m (Producer ByteString m a)
forall a b. a -> ((b -> Constant b b) -> a -> Constant b a) -> b
^.(ByteString
-> Lens'
     (Producer ByteString m a)
     (Producer ByteString m (Producer ByteString m a))
forall (m :: * -> *) x.
Monad m =>
ByteString
-> Lens'
     (Producer ByteString m x)
     (Producer ByteString m (Producer ByteString m x))
breakOn ByteString
needle)
                FreeT (Proxy X () () ByteString m) m a
-> Proxy
     X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Producer ByteString m a -> FreeT (Proxy X () () ByteString m) m a
go (Int -> Producer ByteString m a -> Producer ByteString m a
forall (m :: * -> *) n r.
(Monad m, Integral n) =>
n -> Producer ByteString m r -> Producer ByteString m r
drop Int
len0 Producer ByteString m a
p''))
{-# INLINABLE splitOn #-}

{-| Isomorphism between a byte stream and groups of identical bytes using the
    supplied equality predicate
-}
groupsBy
    :: Monad m
    => (Word8 -> Word8 -> Bool)
    -> Lens' (Producer ByteString m x) (FreeT (Producer ByteString m) m x)
groupsBy :: (Word8 -> Word8 -> Bool)
-> Lens'
     (Producer ByteString m x) (FreeT (Producer ByteString m) m x)
groupsBy Word8 -> Word8 -> Bool
equals FreeT (Producer ByteString m) m x
-> f (FreeT (Producer ByteString m) m x)
k Producer ByteString m x
p0 = (FreeT (Producer ByteString m) m x -> Producer ByteString m x)
-> f (FreeT (Producer ByteString m) m x)
-> f (Producer ByteString m x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap FreeT (Producer ByteString m) m x -> Producer ByteString m x
forall (m :: * -> *) a x.
Monad m =>
FreeT (Producer a m) m x -> Producer a m x
concats (FreeT (Producer ByteString m) m x
-> f (FreeT (Producer ByteString m) m x)
k (Producer ByteString m x -> FreeT (Producer ByteString m) m x
forall (m :: * -> *) a.
Monad m =>
Producer ByteString m a -> FreeT (Proxy X () () ByteString m) m a
_groupsBy Producer ByteString m x
p0))
  where
    -- _groupsBy
    --     :: Monad m
    --     => (Word8 -> Word8 -> Bool)
    --     -> Producer ByteString m x
    --     -> FreeT (Producer ByteString m) m x
    _groupsBy :: Producer ByteString m a -> FreeT (Proxy X () () ByteString m) m a
_groupsBy Producer ByteString m a
p0' = m (FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a))
-> FreeT (Proxy X () () ByteString m) m a
forall (f :: * -> *) (m :: * -> *) a.
m (FreeF f a (FreeT f m a)) -> FreeT f m a
PG.FreeT (Producer ByteString m a
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
forall (m :: * -> *) a.
Monad m =>
Producer ByteString m a
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
go Producer ByteString m a
p0')
      where
        go :: Producer ByteString m a
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
go Producer ByteString m a
p = do
            Either a (ByteString, Producer ByteString m a)
x <- Producer ByteString m a
-> m (Either a (ByteString, Producer ByteString m a))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next Producer ByteString m a
p
            case Either a (ByteString, Producer ByteString m a)
x of
                Left   a
r       -> FreeF
  (Proxy X () () ByteString m)
  a
  (FreeT (Proxy X () () ByteString m) m a)
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
forall (m :: * -> *) a. Monad m => a -> m a
return (a
-> FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a)
forall (f :: * -> *) a b. a -> FreeF f a b
PG.Pure a
r)
                Right (ByteString
bs, Producer ByteString m a
p') -> case (ByteString -> Maybe (Word8, ByteString)
BS.uncons ByteString
bs) of
                    Maybe (Word8, ByteString)
Nothing      -> Producer ByteString m a
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
go Producer ByteString m a
p'
                    Just (Word8
w8, ByteString
_) -> do
                        FreeF
  (Proxy X () () ByteString m)
  a
  (FreeT (Proxy X () () ByteString m) m a)
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
forall (m :: * -> *) a. Monad m => a -> m a
return (FreeF
   (Proxy X () () ByteString m)
   a
   (FreeT (Proxy X () () ByteString m) m a)
 -> m (FreeF
         (Proxy X () () ByteString m)
         a
         (FreeT (Proxy X () () ByteString m) m a)))
-> FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a)
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
forall a b. (a -> b) -> a -> b
$ Proxy X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
-> FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a)
forall (f :: * -> *) a b. f b -> FreeF f a b
PG.Free (Proxy
   X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
 -> FreeF
      (Proxy X () () ByteString m)
      a
      (FreeT (Proxy X () () ByteString m) m a))
-> Proxy
     X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
-> FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a)
forall a b. (a -> b) -> a -> b
$ do
                            Producer ByteString m a
p'' <- (ByteString -> Proxy X () () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
bs Proxy X () () ByteString m ()
-> Producer ByteString m a -> Producer ByteString m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Producer ByteString m a
p')Producer ByteString m a
-> ((Producer ByteString m (Producer ByteString m a)
     -> Constant
          (Producer ByteString m (Producer ByteString m a))
          (Producer ByteString m (Producer ByteString m a)))
    -> Producer ByteString m a
    -> Constant
         (Producer ByteString m (Producer ByteString m a))
         (Producer ByteString m a))
-> Producer ByteString m (Producer ByteString m a)
forall a b. a -> ((b -> Constant b b) -> a -> Constant b a) -> b
^.(Word8 -> Bool)
-> Lens'
     (Producer ByteString m a)
     (Producer ByteString m (Producer ByteString m a))
forall (m :: * -> *) x.
Monad m =>
(Word8 -> Bool)
-> Lens'
     (Producer ByteString m x)
     (Producer ByteString m (Producer ByteString m x))
span (Word8 -> Word8 -> Bool
equals Word8
w8)
                            FreeT (Proxy X () () ByteString m) m a
-> Proxy
     X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
forall (m :: * -> *) a. Monad m => a -> m a
return (FreeT (Proxy X () () ByteString m) m a
 -> Proxy
      X () () ByteString m (FreeT (Proxy X () () ByteString m) m a))
-> FreeT (Proxy X () () ByteString m) m a
-> Proxy
     X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
forall a b. (a -> b) -> a -> b
$ m (FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a))
-> FreeT (Proxy X () () ByteString m) m a
forall (f :: * -> *) (m :: * -> *) a.
m (FreeF f a (FreeT f m a)) -> FreeT f m a
PG.FreeT (Producer ByteString m a
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
go Producer ByteString m a
p'')
{-# INLINABLE groupsBy #-}

-- | Like 'groupsBy', where the equality predicate is ('==')
groups
    :: Monad m
    => Lens' (Producer ByteString m x) (FreeT (Producer ByteString m) m x)
groups :: Lens' (Producer ByteString m x) (FreeT (Producer ByteString m) m x)
groups = (Word8 -> Word8 -> Bool)
-> Lens'
     (Producer ByteString m x) (FreeT (Producer ByteString m) m x)
forall (m :: * -> *) x.
Monad m =>
(Word8 -> Word8 -> Bool)
-> Lens'
     (Producer ByteString m x) (FreeT (Producer ByteString m) m x)
groupsBy Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
(==)
{-# INLINABLE groups #-}

{-| Improper lens between a bytestream and its lines

    Note: This function is purely for demonstration purposes since it assumes a
    particular encoding.  You should prefer the 'Data.Text.Text' equivalent of
    this function from the @pipes-text@ library.
-}
lines
    :: Monad m
    => Lens' (Producer ByteString m x) (FreeT (Producer ByteString m) m x)
lines :: Lens' (Producer ByteString m x) (FreeT (Producer ByteString m) m x)
lines FreeT (Producer ByteString m) m x
-> f (FreeT (Producer ByteString m) m x)
k Producer ByteString m x
p = (FreeT (Producer ByteString m) m x -> Producer ByteString m x)
-> f (FreeT (Producer ByteString m) m x)
-> f (Producer ByteString m x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap FreeT (Producer ByteString m) m x -> Producer ByteString m x
forall (m :: * -> *) x.
Monad m =>
FreeT (Producer ByteString m) m x -> Producer ByteString m x
_unlines (FreeT (Producer ByteString m) m x
-> f (FreeT (Producer ByteString m) m x)
k (Producer ByteString m x -> FreeT (Producer ByteString m) m x
forall (m :: * -> *) a.
Monad m =>
Producer ByteString m a -> FreeT (Proxy X () () ByteString m) m a
_lines Producer ByteString m x
p))
{-# INLINABLE lines #-}

{-| Improper lens between lines and a bytestream

    Note: This function is purely for demonstration purposes since it assumes a
    particular encoding.  You should prefer the 'Data.Text.Text' equivalent of
    this function from the @pipes-text@ library.
-}
unlines
    :: Monad m
    => Lens' (FreeT (Producer ByteString m) m x) (Producer ByteString m x)
unlines :: Lens' (FreeT (Producer ByteString m) m x) (Producer ByteString m x)
unlines Producer ByteString m x -> f (Producer ByteString m x)
k FreeT (Producer ByteString m) m x
p = (Producer ByteString m x -> FreeT (Producer ByteString m) m x)
-> f (Producer ByteString m x)
-> f (FreeT (Producer ByteString m) m x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Producer ByteString m x -> FreeT (Producer ByteString m) m x
forall (m :: * -> *) a.
Monad m =>
Producer ByteString m a -> FreeT (Proxy X () () ByteString m) m a
_lines (Producer ByteString m x -> f (Producer ByteString m x)
k (FreeT (Producer ByteString m) m x -> Producer ByteString m x
forall (m :: * -> *) x.
Monad m =>
FreeT (Producer ByteString m) m x -> Producer ByteString m x
_unlines FreeT (Producer ByteString m) m x
p))
{-# INLINABLE unlines #-}

_lines
    :: Monad m => Producer ByteString m x -> FreeT (Producer ByteString m) m x
_lines :: Producer ByteString m x -> FreeT (Producer ByteString m) m x
_lines Producer ByteString m x
p0 = m (FreeF
     (Producer ByteString m) x (FreeT (Producer ByteString m) m x))
-> FreeT (Producer ByteString m) m x
forall (f :: * -> *) (m :: * -> *) a.
m (FreeF f a (FreeT f m a)) -> FreeT f m a
PG.FreeT (Producer ByteString m x
-> m (FreeF
        (Producer ByteString m) x (FreeT (Producer ByteString m) m x))
forall (m :: * -> *) a.
Monad m =>
Producer ByteString m a
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
go0 Producer ByteString m x
p0)
  where
    go0 :: Producer ByteString m a
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
go0 Producer ByteString m a
p = do
        Either a (ByteString, Producer ByteString m a)
x <- Producer ByteString m a
-> m (Either a (ByteString, Producer ByteString m a))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next Producer ByteString m a
p
        case Either a (ByteString, Producer ByteString m a)
x of
            Left   a
r       -> FreeF
  (Proxy X () () ByteString m)
  a
  (FreeT (Proxy X () () ByteString m) m a)
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
forall (m :: * -> *) a. Monad m => a -> m a
return (a
-> FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a)
forall (f :: * -> *) a b. a -> FreeF f a b
PG.Pure a
r)
            Right (ByteString
bs, Producer ByteString m a
p') ->
                if (ByteString -> Bool
BS.null ByteString
bs)
                then Producer ByteString m a
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
go0 Producer ByteString m a
p'
                else FreeF
  (Proxy X () () ByteString m)
  a
  (FreeT (Proxy X () () ByteString m) m a)
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
forall (m :: * -> *) a. Monad m => a -> m a
return (FreeF
   (Proxy X () () ByteString m)
   a
   (FreeT (Proxy X () () ByteString m) m a)
 -> m (FreeF
         (Proxy X () () ByteString m)
         a
         (FreeT (Proxy X () () ByteString m) m a)))
-> FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a)
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
forall a b. (a -> b) -> a -> b
$ Proxy X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
-> FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a)
forall (f :: * -> *) a b. f b -> FreeF f a b
PG.Free (Proxy
   X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
 -> FreeF
      (Proxy X () () ByteString m)
      a
      (FreeT (Proxy X () () ByteString m) m a))
-> Proxy
     X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
-> FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a)
forall a b. (a -> b) -> a -> b
$ Producer ByteString m a
-> Proxy
     X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
go1 (ByteString -> Proxy X () () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
bs Proxy X () () ByteString m ()
-> Producer ByteString m a -> Producer ByteString m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Producer ByteString m a
p')
    go1 :: Producer ByteString m a
-> Proxy
     X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
go1 Producer ByteString m a
p = do
        Producer ByteString m a
p' <- Producer ByteString m a
pProducer ByteString m a
-> ((Producer ByteString m (Producer ByteString m a)
     -> Constant
          (Producer ByteString m (Producer ByteString m a))
          (Producer ByteString m (Producer ByteString m a)))
    -> Producer ByteString m a
    -> Constant
         (Producer ByteString m (Producer ByteString m a))
         (Producer ByteString m a))
-> Producer ByteString m (Producer ByteString m a)
forall a b. a -> ((b -> Constant b b) -> a -> Constant b a) -> b
^.(Producer ByteString m (Producer ByteString m a)
 -> Constant
      (Producer ByteString m (Producer ByteString m a))
      (Producer ByteString m (Producer ByteString m a)))
-> Producer ByteString m a
-> Constant
     (Producer ByteString m (Producer ByteString m a))
     (Producer ByteString m a)
forall (m :: * -> *) x.
Monad m =>
Lens'
  (Producer ByteString m x)
  (Producer ByteString m (Producer ByteString m x))
line
        FreeT (Proxy X () () ByteString m) m a
-> Proxy
     X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
forall (m :: * -> *) a. Monad m => a -> m a
return (FreeT (Proxy X () () ByteString m) m a
 -> Proxy
      X () () ByteString m (FreeT (Proxy X () () ByteString m) m a))
-> FreeT (Proxy X () () ByteString m) m a
-> Proxy
     X () () ByteString m (FreeT (Proxy X () () ByteString m) m a)
forall a b. (a -> b) -> a -> b
$ m (FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a))
-> FreeT (Proxy X () () ByteString m) m a
forall (f :: * -> *) (m :: * -> *) a.
m (FreeF f a (FreeT f m a)) -> FreeT f m a
PG.FreeT (m (FreeF
      (Proxy X () () ByteString m)
      a
      (FreeT (Proxy X () () ByteString m) m a))
 -> FreeT (Proxy X () () ByteString m) m a)
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
-> FreeT (Proxy X () () ByteString m) m a
forall a b. (a -> b) -> a -> b
$ do
            Either a (Word8, Producer ByteString m a)
x  <- Producer ByteString m a
-> m (Either a (Word8, Producer ByteString m a))
forall (m :: * -> *) r.
Monad m =>
Producer ByteString m r
-> m (Either r (Word8, Producer ByteString m r))
nextByte Producer ByteString m a
p'
            case Either a (Word8, Producer ByteString m a)
x of
                Left   a
r       -> FreeF
  (Proxy X () () ByteString m)
  a
  (FreeT (Proxy X () () ByteString m) m a)
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
forall (m :: * -> *) a. Monad m => a -> m a
return (a
-> FreeF
     (Proxy X () () ByteString m)
     a
     (FreeT (Proxy X () () ByteString m) m a)
forall (f :: * -> *) a b. a -> FreeF f a b
PG.Pure a
r)
                Right (Word8
_, Producer ByteString m a
p'') -> Producer ByteString m a
-> m (FreeF
        (Proxy X () () ByteString m)
        a
        (FreeT (Proxy X () () ByteString m) m a))
go0 Producer ByteString m a
p''
{-# INLINABLE _lines #-}

_unlines
    :: Monad m => FreeT (Producer ByteString m) m x -> Producer ByteString m x
_unlines :: FreeT (Producer ByteString m) m x -> Producer ByteString m x
_unlines = FreeT (Producer ByteString m) m x -> Producer ByteString m x
forall (m :: * -> *) a x.
Monad m =>
FreeT (Producer a m) m x -> Producer a m x
concats (FreeT (Producer ByteString m) m x -> Producer ByteString m x)
-> (FreeT (Producer ByteString m) m x
    -> FreeT (Producer ByteString m) m x)
-> FreeT (Producer ByteString m) m x
-> Producer ByteString m x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall r. Producer ByteString m r -> Producer ByteString m r)
-> FreeT (Producer ByteString m) m x
-> FreeT (Producer ByteString m) m x
forall (m :: * -> *) (g :: * -> *) (f :: * -> *) x.
(Monad m, Functor g) =>
(forall r. f r -> g r) -> FreeT f m x -> FreeT g m x
PG.maps forall r. Producer ByteString m r -> Producer ByteString m r
forall (m :: * -> *) x' x a.
Functor m =>
Proxy x' x () ByteString m a -> Proxy x' x () ByteString m a
addNewline
  where
    addNewline :: Proxy x' x () ByteString m a -> Proxy x' x () ByteString m a
addNewline Proxy x' x () ByteString m a
p = Proxy x' x () ByteString m a
p Proxy x' x () ByteString m a
-> Proxy x' x () ByteString m () -> Proxy x' x () ByteString m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ByteString -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield (Word8 -> ByteString
BS.singleton Word8
nl)
{-# INLINABLE _unlines #-}

{-| Convert a bytestream to delimited words

    Note: This function is purely for demonstration purposes since it assumes a
    particular encoding.  You should prefer the 'Data.Text.Text' equivalent of
    this function from the @pipes-text@ library.
-}
words :: Monad m => Producer ByteString m x -> FreeT (Producer ByteString m) m x
words :: Producer ByteString m x -> FreeT (Producer ByteString m) m x
words Producer ByteString m x
p = m (FreeF
     (Producer ByteString m) x (FreeT (Producer ByteString m) m x))
-> FreeT (Producer ByteString m) m x
forall (f :: * -> *) (m :: * -> *) a.
m (FreeF f a (FreeT f m a)) -> FreeT f m a
PG.FreeT (m (FreeF
      (Producer ByteString m) x (FreeT (Producer ByteString m) m x))
 -> FreeT (Producer ByteString m) m x)
-> m (FreeF
        (Producer ByteString m) x (FreeT (Producer ByteString m) m x))
-> FreeT (Producer ByteString m) m x
forall a b. (a -> b) -> a -> b
$ do
    Either x (ByteString, Producer ByteString m x)
x <- Producer ByteString m x
-> m (Either x (ByteString, Producer ByteString m x))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next ((Word8 -> Bool)
-> Producer ByteString m x -> Producer ByteString m x
forall (m :: * -> *) r.
Monad m =>
(Word8 -> Bool)
-> Producer ByteString m r -> Producer ByteString m r
dropWhile Word8 -> Bool
isSpaceWord8 Producer ByteString m x
p)
    FreeF (Producer ByteString m) x (FreeT (Producer ByteString m) m x)
-> m (FreeF
        (Producer ByteString m) x (FreeT (Producer ByteString m) m x))
forall (m :: * -> *) a. Monad m => a -> m a
return (FreeF
   (Producer ByteString m) x (FreeT (Producer ByteString m) m x)
 -> m (FreeF
         (Producer ByteString m) x (FreeT (Producer ByteString m) m x)))
-> FreeF
     (Producer ByteString m) x (FreeT (Producer ByteString m) m x)
-> m (FreeF
        (Producer ByteString m) x (FreeT (Producer ByteString m) m x))
forall a b. (a -> b) -> a -> b
$ case Either x (ByteString, Producer ByteString m x)
x of
        Left   x
r       -> x
-> FreeF
     (Producer ByteString m) x (FreeT (Producer ByteString m) m x)
forall (f :: * -> *) a b. a -> FreeF f a b
PG.Pure x
r
        Right (ByteString
bs, Producer ByteString m x
p') -> Proxy X () () ByteString m (FreeT (Producer ByteString m) m x)
-> FreeF
     (Producer ByteString m) x (FreeT (Producer ByteString m) m x)
forall (f :: * -> *) a b. f b -> FreeF f a b
PG.Free (Proxy X () () ByteString m (FreeT (Producer ByteString m) m x)
 -> FreeF
      (Producer ByteString m) x (FreeT (Producer ByteString m) m x))
-> Proxy X () () ByteString m (FreeT (Producer ByteString m) m x)
-> FreeF
     (Producer ByteString m) x (FreeT (Producer ByteString m) m x)
forall a b. (a -> b) -> a -> b
$ do
            Producer ByteString m x
p'' <- (ByteString -> Proxy X () () ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield ByteString
bs Proxy X () () ByteString m ()
-> Producer ByteString m x -> Producer ByteString m x
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Producer ByteString m x
p')Producer ByteString m x
-> ((Producer ByteString m (Producer ByteString m x)
     -> Constant
          (Producer ByteString m (Producer ByteString m x))
          (Producer ByteString m (Producer ByteString m x)))
    -> Producer ByteString m x
    -> Constant
         (Producer ByteString m (Producer ByteString m x))
         (Producer ByteString m x))
-> Producer ByteString m (Producer ByteString m x)
forall a b. a -> ((b -> Constant b b) -> a -> Constant b a) -> b
^.(Word8 -> Bool)
-> Lens'
     (Producer ByteString m x)
     (Producer ByteString m (Producer ByteString m x))
forall (m :: * -> *) x.
Monad m =>
(Word8 -> Bool)
-> Lens'
     (Producer ByteString m x)
     (Producer ByteString m (Producer ByteString m x))
break Word8 -> Bool
isSpaceWord8
            FreeT (Producer ByteString m) m x
-> Proxy X () () ByteString m (FreeT (Producer ByteString m) m x)
forall (m :: * -> *) a. Monad m => a -> m a
return (Producer ByteString m x -> FreeT (Producer ByteString m) m x
forall (m :: * -> *) a.
Monad m =>
Producer ByteString m a -> FreeT (Proxy X () () ByteString m) m a
words Producer ByteString m x
p'')
{-# INLINABLE words #-}

{-| Convert delimited words back to a byte stream

    Note: This function is purely for demonstration purposes since it assumes a
    particular encoding.  You should prefer the 'Data.Text.Text' equivalent of
    this function from the @pipes-text@ library.
-}
unwords
    :: Monad m => FreeT (Producer ByteString m) m x -> Producer ByteString m x
unwords :: FreeT (Producer ByteString m) m x -> Producer ByteString m x
unwords = Producer ByteString m ()
-> FreeT (Producer ByteString m) m x -> Producer ByteString m x
forall (m :: * -> *) a x.
Monad m =>
Producer a m () -> FreeT (Producer a m) m x -> Producer a m x
PG.intercalates (ByteString -> Producer ByteString m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield (ByteString -> Producer ByteString m ())
-> ByteString -> Producer ByteString m ()
forall a b. (a -> b) -> a -> b
$ Word8 -> ByteString
BS.singleton (Word8 -> ByteString) -> Word8 -> ByteString
forall a b. (a -> b) -> a -> b
$ Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Int -> Word8
forall a b. (a -> b) -> a -> b
$ Char -> Int
ord Char
' ')
{-# INLINABLE unwords #-}

{- $parse
    The following parsing utilities are single-byte analogs of the ones found
    in @pipes-parse@.
-}

{- $reexports
    @Data.ByteString@ re-exports the 'ByteString' type.

    @Data.Word@ re-exports the 'Word8' type.

    @Pipes.Parse@ re-exports 'Parser'.

    @Pipes.Group@ re-exports 'concats', 'intercalates', and 'FreeT'
    (the type).
-}