{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ViewPatterns #-}
module Streaming.ByteString
(
ByteStream
, ByteString
, empty
, singleton
, pack
, unpack
, fromLazy
, toLazy
, toLazy_
, fromChunks
, toChunks
, fromStrict
, toStrict
, toStrict_
, effects
, copy
, drained
, mwrap
, map
, for
, intercalate
, intersperse
, cons
, cons'
, snoc
, append
, filter
, uncons
, nextByte
, break
, drop
, dropWhile
, group
, groupBy
, span
, splitAt
, splitWith
, take
, takeWhile
, split
, concat
, denull
, toStreamingByteString
, toStreamingByteStringWith
, toBuilder
, concatBuilders
, repeat
, iterate
, cycle
, unfoldM
, unfoldr
, reread
, foldr
, fold
, fold_
, head
, head_
, last
, last_
, length
, length_
, null
, null_
, nulls
, testNull
, count
, count_
, getContents
, stdin
, stdout
, interact
, readFile
, writeFile
, appendFile
, fromHandle
, toHandle
, hGet
, hGetContents
, hGetContentsN
, hGetN
, hGetNonBlocking
, hGetNonBlockingN
, hPut
, unconsChunk
, nextChunk
, chunk
, foldrChunks
, foldlChunks
, chunkFold
, chunkFoldM
, chunkMap
, chunkMapM
, chunkMapM_
, dematerialize
, materialize
, distribute
, zipWithStream
) where
import Prelude hiding
(all, any, appendFile, break, concat, concatMap, cycle, drop, dropWhile,
elem, filter, foldl, foldl1, foldr, foldr1, getContents, getLine, head,
init, interact, iterate, last, length, lines, map, maximum, minimum,
notElem, null, putStr, putStrLn, readFile, repeat, replicate, reverse,
scanl, scanl1, scanr, scanr1, span, splitAt, tail, take, takeWhile,
unlines, unzip, writeFile, zip, zipWith)
import qualified Data.ByteString as P (ByteString)
import qualified Data.ByteString as B
import Data.ByteString.Builder.Internal hiding
(append, defaultChunkSize, empty, hPut)
import qualified Data.ByteString.Internal as B
import qualified Data.ByteString.Lazy.Internal as BI
import qualified Data.ByteString.Unsafe as B
import Streaming hiding (concats, distribute, unfold)
import Streaming.ByteString.Internal
import Streaming.Internal (Stream(..))
import qualified Streaming.Prelude as SP
import Control.Monad (forever)
import Control.Monad.Trans.Resource
import Data.Int (Int64)
import qualified Data.List as L
import Data.Word (Word8)
import Foreign.Ptr
import Foreign.Storable
import System.IO (Handle, IOMode(..), hClose, openBinaryFile)
import qualified System.IO as IO (stdin, stdout)
import System.IO.Error (illegalOperationErrorType, mkIOError)
concat :: Monad m => Stream (ByteStream m) m r -> ByteStream m r
concat :: forall (m :: * -> *) r.
Monad m =>
Stream (ByteStream m) m r -> ByteStream m r
concat Stream (ByteStream m) m r
x = forall (f :: * -> *) (m :: * -> *) r b.
(Functor f, Monad m) =>
Stream f m r -> (f b -> b) -> (m b -> b) -> (r -> b) -> b
destroy Stream (ByteStream m) m r
x forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall (m :: * -> *) r. m (ByteStream m r) -> ByteStream m r
Go forall (m :: * -> *) r. r -> ByteStream m r
Empty
{-# INLINE concat #-}
distribute
:: (Monad m, MonadTrans t, MFunctor t, Monad (t m), Monad (t (ByteStream m)))
=> ByteStream (t m) a -> t (ByteStream m) a
distribute :: forall (m :: * -> *) (t :: (* -> *) -> * -> *) a.
(Monad m, MonadTrans t, MFunctor t, Monad (t m),
Monad (t (ByteStream m))) =>
ByteStream (t m) a -> t (ByteStream m) a
distribute ByteStream (t m) a
ls = forall (m :: * -> *) r.
Monad m =>
ByteStream m r
-> forall x. (r -> x) -> (ByteString -> x -> x) -> (m x -> x) -> x
dematerialize ByteStream (t m) a
ls
forall (m :: * -> *) a. Monad m => a -> m a
return
(\ByteString
bs t (ByteStream m) a
x -> forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall a b. (a -> b) -> a -> b
$ forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
bs (forall (m :: * -> *) r. r -> ByteStream m r
Empty t (ByteStream m) a
x) )
(forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (t :: (* -> *) -> k -> *) (m :: * -> *) (n :: * -> *)
(b :: k).
(MFunctor t, Monad m) =>
(forall a. m a -> n a) -> t m b -> t n b
hoist (forall (m :: * -> *) r. m (ByteStream m r) -> ByteStream m r
Go forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (m :: * -> *) r. r -> ByteStream m r
Empty))
{-# INLINE distribute #-}
effects :: Monad m => ByteStream m r -> m r
effects :: forall (m :: * -> *) r. Monad m => ByteStream m r -> m r
effects ByteStream m r
bs = case ByteStream m r
bs of
Empty r
r -> forall (m :: * -> *) a. Monad m => a -> m a
return r
r
Go m (ByteStream m r)
m -> m (ByteStream m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) r. Monad m => ByteStream m r -> m r
effects
Chunk ByteString
_ ByteStream m r
rest -> forall (m :: * -> *) r. Monad m => ByteStream m r -> m r
effects ByteStream m r
rest
{-# INLINABLE effects #-}
drained :: (Monad m, MonadTrans t, Monad (t m)) => t m (ByteStream m r) -> t m r
drained :: forall (m :: * -> *) (t :: (* -> *) -> * -> *) r.
(Monad m, MonadTrans t, Monad (t m)) =>
t m (ByteStream m r) -> t m r
drained t m (ByteStream m r)
t = t m (ByteStream m r)
t forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) r. Monad m => ByteStream m r -> m r
effects
empty :: ByteStream m ()
empty :: forall (m :: * -> *). ByteStream m ()
empty = forall (m :: * -> *) r. r -> ByteStream m r
Empty ()
{-# INLINE empty #-}
singleton :: Monad m => Word8 -> ByteStream m ()
singleton :: forall (m :: * -> *). Monad m => Word8 -> ByteStream m ()
singleton Word8
w = forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk (Word8 -> ByteString
B.singleton Word8
w) (forall (m :: * -> *) r. r -> ByteStream m r
Empty ())
{-# INLINE singleton #-}
pack :: Monad m => Stream (Of Word8) m r -> ByteStream m r
pack :: forall (m :: * -> *) r.
Monad m =>
Stream (Of Word8) m r -> ByteStream m r
pack = forall (m :: * -> *) r.
Monad m =>
Stream (Of Word8) m r -> ByteStream m r
packBytes
{-# INLINE pack #-}
unpack :: Monad m => ByteStream m r -> Stream (Of Word8) m r
unpack :: forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> Stream (Of Word8) m r
unpack = forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> Stream (Of Word8) m r
unpackBytes
fromChunks :: Monad m => Stream (Of P.ByteString) m r -> ByteStream m r
fromChunks :: forall (m :: * -> *) r.
Monad m =>
Stream (Of ByteString) m r -> ByteStream m r
fromChunks Stream (Of ByteString) m r
cs = forall (f :: * -> *) (m :: * -> *) r b.
(Functor f, Monad m) =>
Stream f m r -> (f b -> b) -> (m b -> b) -> (r -> b) -> b
destroy Stream (Of ByteString) m r
cs (\(ByteString
bs :> ByteStream m r
rest) -> forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
bs ByteStream m r
rest) forall (m :: * -> *) r. m (ByteStream m r) -> ByteStream m r
Go forall (m :: * -> *) a. Monad m => a -> m a
return
{-# INLINE fromChunks #-}
toChunks :: Monad m => ByteStream m r -> Stream (Of P.ByteString) m r
toChunks :: forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> Stream (Of ByteString) m r
toChunks ByteStream m r
bs = forall (m :: * -> *) r.
Monad m =>
ByteStream m r
-> forall x. (r -> x) -> (ByteString -> x -> x) -> (m x -> x) -> x
dematerialize ByteStream m r
bs forall (m :: * -> *) a. Monad m => a -> m a
return (\ByteString
b Stream (Of ByteString) m r
mx -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (ByteString
bforall a b. a -> b -> Of a b
:> Stream (Of ByteString) m r
mx)) forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect
{-# INLINE toChunks #-}
fromStrict :: P.ByteString -> ByteStream m ()
fromStrict :: forall (m :: * -> *). ByteString -> ByteStream m ()
fromStrict ByteString
bs | ByteString -> Bool
B.null ByteString
bs = forall (m :: * -> *) r. r -> ByteStream m r
Empty ()
| Bool
otherwise = forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
bs (forall (m :: * -> *) r. r -> ByteStream m r
Empty ())
{-# INLINE fromStrict #-}
toStrict_ :: Monad m => ByteStream m r -> m B.ByteString
#if MIN_VERSION_streaming (0,2,2)
toStrict_ :: forall (m :: * -> *) r. Monad m => ByteStream m r -> m ByteString
toStrict_ = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [ByteString] -> ByteString
B.concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a r. Monad m => Stream (Of a) m r -> m [a]
SP.toList_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> Stream (Of ByteString) m r
toChunks
#else
toStrict_ = fmap B.concat . SP.toList_ . void . toChunks
#endif
{-# INLINE toStrict_ #-}
toStrict :: Monad m => ByteStream m r -> m (Of B.ByteString r)
toStrict :: forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Of ByteString r)
toStrict ByteStream m r
bs = do
([ByteString]
bss :> r
r) <- forall (m :: * -> *) a r.
Monad m =>
Stream (Of a) m r -> m (Of [a] r)
SP.toList (forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> Stream (Of ByteString) m r
toChunks ByteStream m r
bs)
forall (m :: * -> *) a. Monad m => a -> m a
return ([ByteString] -> ByteString
B.concat [ByteString]
bss forall a b. a -> b -> Of a b
:> r
r)
{-# INLINE toStrict #-}
fromLazy :: Monad m => BI.ByteString -> ByteStream m ()
fromLazy :: forall (m :: * -> *). Monad m => ByteString -> ByteStream m ()
fromLazy = forall a. (ByteString -> a -> a) -> a -> ByteString -> a
BI.foldrChunks forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk (forall (m :: * -> *) r. r -> ByteStream m r
Empty ())
{-# INLINE fromLazy #-}
toLazy_ :: Monad m => ByteStream m r -> m BI.ByteString
toLazy_ :: forall (m :: * -> *) r. Monad m => ByteStream m r -> m ByteString
toLazy_ ByteStream m r
bs = forall (m :: * -> *) r.
Monad m =>
ByteStream m r
-> forall x. (r -> x) -> (ByteString -> x -> x) -> (m x -> x) -> x
dematerialize ByteStream m r
bs (\r
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
BI.Empty) (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString -> ByteString
BI.Chunk) forall (m :: * -> *) a. Monad m => m (m a) -> m a
join
{-# INLINE toLazy_ #-}
toLazy :: Monad m => ByteStream m r -> m (Of BI.ByteString r)
toLazy :: forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Of ByteString r)
toLazy ByteStream m r
bs0 = forall (m :: * -> *) r.
Monad m =>
ByteStream m r
-> forall x. (r -> x) -> (ByteString -> x -> x) -> (m x -> x) -> x
dematerialize ByteStream m r
bs0
(\r
r -> forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString
BI.Empty forall a b. a -> b -> Of a b
:> r
r))
(\ByteString
b m (Of ByteString r)
mx -> do
(ByteString
bs :> r
x) <- m (Of ByteString r)
mx
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString -> ByteString
BI.Chunk ByteString
b ByteString
bs forall a b. a -> b -> Of a b
:> r
x
)
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join
{-# INLINE toLazy #-}
null :: Monad m => ByteStream m r -> m (Of Bool r)
null :: forall (m :: * -> *) r. Monad m => ByteStream m r -> m (Of Bool r)
null (Empty r
r) = forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
True forall a b. a -> b -> Of a b
:> r
r)
null (Go m (ByteStream m r)
m) = m (ByteStream m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) r. Monad m => ByteStream m r -> m (Of Bool r)
null
null (Chunk ByteString
bs ByteStream m r
rest) = if ByteString -> Bool
B.null ByteString
bs
then forall (m :: * -> *) r. Monad m => ByteStream m r -> m (Of Bool r)
null ByteStream m r
rest
else do
r
r <- forall (m :: * -> *) a r. Monad m => Stream (Of a) m r -> m r
SP.effects (forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> Stream (Of ByteString) m r
toChunks ByteStream m r
rest)
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
False forall a b. a -> b -> Of a b
:> r
r)
{-# INLINABLE null #-}
null_ :: Monad m => ByteStream m r -> m Bool
null_ :: forall (m :: * -> *) r. Monad m => ByteStream m r -> m Bool
null_ (Empty r
_) = forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
null_ (Go m (ByteStream m r)
m) = m (ByteStream m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) r. Monad m => ByteStream m r -> m Bool
null_
null_ (Chunk ByteString
bs ByteStream m r
rest) = if ByteString -> Bool
B.null ByteString
bs
then forall (m :: * -> *) r. Monad m => ByteStream m r -> m Bool
null_ ByteStream m r
rest
else forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
{-# INLINABLE null_ #-}
testNull :: Monad m => ByteStream m r -> m (Of Bool (ByteStream m r))
testNull :: forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Of Bool (ByteStream m r))
testNull (Empty r
r) = forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
True forall a b. a -> b -> Of a b
:> forall (m :: * -> *) r. r -> ByteStream m r
Empty r
r)
testNull (Go m (ByteStream m r)
m) = m (ByteStream m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Of Bool (ByteStream m r))
testNull
testNull p :: ByteStream m r
p@(Chunk ByteString
bs ByteStream m r
rest) = if ByteString -> Bool
B.null ByteString
bs
then forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Of Bool (ByteStream m r))
testNull ByteStream m r
rest
else forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
False forall a b. a -> b -> Of a b
:> ByteStream m r
p)
{-# INLINABLE testNull #-}
denull :: Monad m => Stream (ByteStream m) m r -> Stream (ByteStream m) m r
{-# INLINABLE denull #-}
denull :: forall (m :: * -> *) r.
Monad m =>
Stream (ByteStream m) m r -> Stream (ByteStream m) m r
denull = forall {r}.
Either
(ByteStream m (Stream (ByteStream m) m r))
(Stream (ByteStream m) m r)
-> Stream (ByteStream m) m r
loop forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. b -> Either a b
Right
where
loop :: Either
(ByteStream m (Stream (ByteStream m) m r))
(Stream (ByteStream m) m r)
-> Stream (ByteStream m) m r
loop = \ case
Left ByteStream m (Stream (ByteStream m) m r)
mbs -> case ByteStream m (Stream (ByteStream m) m r)
mbs of
Chunk ByteString
c ByteStream m (Stream (ByteStream m) m r)
cs | ByteString -> Int
B.length ByteString
c forall a. Ord a => a -> a -> Bool
> Int
0 -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
c forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Either
(ByteStream m (Stream (ByteStream m) m r))
(Stream (ByteStream m) m r)
-> Stream (ByteStream m) m r
loop forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. b -> Either a b
Right) ByteStream m (Stream (ByteStream m) m r)
cs
| Bool
otherwise -> Either
(ByteStream m (Stream (ByteStream m) m r))
(Stream (ByteStream m) m r)
-> Stream (ByteStream m) m r
loop forall a b. (a -> b) -> a -> b
$ forall a b. a -> Either a b
Left ByteStream m (Stream (ByteStream m) m r)
cs
Go m (ByteStream m (Stream (ByteStream m) m r))
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ Either
(ByteStream m (Stream (ByteStream m) m r))
(Stream (ByteStream m) m r)
-> Stream (ByteStream m) m r
loop forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> Either a b
Left forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (ByteStream m (Stream (ByteStream m) m r))
m
Empty Stream (ByteStream m) m r
r -> Either
(ByteStream m (Stream (ByteStream m) m r))
(Stream (ByteStream m) m r)
-> Stream (ByteStream m) m r
loop forall a b. (a -> b) -> a -> b
$ forall a b. b -> Either a b
Right Stream (ByteStream m) m r
r
Right Stream (ByteStream m) m r
strm -> case Stream (ByteStream m) m r
strm of
Step ByteStream m (Stream (ByteStream m) m r)
mbs -> case ByteStream m (Stream (ByteStream m) m r)
mbs of
Chunk ByteString
c ByteStream m (Stream (ByteStream m) m r)
cs | ByteString -> Int
B.length ByteString
c forall a. Ord a => a -> a -> Bool
> Int
0 -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
c forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Either
(ByteStream m (Stream (ByteStream m) m r))
(Stream (ByteStream m) m r)
-> Stream (ByteStream m) m r
loop forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. b -> Either a b
Right) ByteStream m (Stream (ByteStream m) m r)
cs
| Bool
otherwise -> Either
(ByteStream m (Stream (ByteStream m) m r))
(Stream (ByteStream m) m r)
-> Stream (ByteStream m) m r
loop forall a b. (a -> b) -> a -> b
$ forall a b. a -> Either a b
Left ByteStream m (Stream (ByteStream m) m r)
cs
Go m (ByteStream m (Stream (ByteStream m) m r))
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ Either
(ByteStream m (Stream (ByteStream m) m r))
(Stream (ByteStream m) m r)
-> Stream (ByteStream m) m r
loop forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> Either a b
Left forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (ByteStream m (Stream (ByteStream m) m r))
m
Empty Stream (ByteStream m) m r
r -> Either
(ByteStream m (Stream (ByteStream m) m r))
(Stream (ByteStream m) m r)
-> Stream (ByteStream m) m r
loop forall a b. (a -> b) -> a -> b
$ forall a b. b -> Either a b
Right Stream (ByteStream m) m r
r
Effect m (Stream (ByteStream m) m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Either
(ByteStream m (Stream (ByteStream m) m r))
(Stream (ByteStream m) m r)
-> Stream (ByteStream m) m r
loop forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. b -> Either a b
Right) m (Stream (ByteStream m) m r)
m
r :: Stream (ByteStream m) m r
r@(Return r
_) -> Stream (ByteStream m) m r
r
nulls :: Monad m => ByteStream m r -> m (Sum (ByteStream m) (ByteStream m) r)
nulls :: forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Sum (ByteStream m) (ByteStream m) r)
nulls (Empty r
r) = forall (m :: * -> *) a. Monad m => a -> m a
return (forall {k} (f :: k -> *) (g :: k -> *) (a :: k). g a -> Sum f g a
InR (forall (m :: * -> *) a. Monad m => a -> m a
return r
r))
nulls (Go m (ByteStream m r)
m) = m (ByteStream m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Sum (ByteStream m) (ByteStream m) r)
nulls
nulls (Chunk ByteString
bs ByteStream m r
rest) = if ByteString -> Bool
B.null ByteString
bs
then forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Sum (ByteStream m) (ByteStream m) r)
nulls ByteStream m r
rest
else forall (m :: * -> *) a. Monad m => a -> m a
return (forall {k} (f :: k -> *) (g :: k -> *) (a :: k). f a -> Sum f g a
InL (forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
bs ByteStream m r
rest))
{-# INLINABLE nulls #-}
length_ :: Monad m => ByteStream m r -> m Int
length_ :: forall (m :: * -> *) r. Monad m => ByteStream m r -> m Int
length_ = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Int
n:> r
_) -> Int
n) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a r.
Monad m =>
(a -> ByteString -> a) -> a -> ByteStream m r -> m (Of a r)
foldlChunks (\Int
n ByteString
c -> Int
n forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
B.length ByteString
c)) Int
0
{-# INLINE length_ #-}
length :: Monad m => ByteStream m r -> m (Of Int r)
length :: forall (m :: * -> *) r. Monad m => ByteStream m r -> m (Of Int r)
length = forall (m :: * -> *) a r.
Monad m =>
(a -> ByteString -> a) -> a -> ByteStream m r -> m (Of a r)
foldlChunks (\Int
n ByteString
c -> Int
n forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
B.length ByteString
c)) Int
0
{-# INLINE length #-}
cons :: Monad m => Word8 -> ByteStream m r -> ByteStream m r
cons :: forall (m :: * -> *) r.
Monad m =>
Word8 -> ByteStream m r -> ByteStream m r
cons Word8
c ByteStream m r
cs = forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk (Word8 -> ByteString
B.singleton Word8
c) ByteStream m r
cs
{-# INLINE cons #-}
cons' :: Word8 -> ByteStream m r -> ByteStream m r
cons' :: forall (m :: * -> *) r. Word8 -> ByteStream m r -> ByteStream m r
cons' Word8
w (Chunk ByteString
c ByteStream m r
cs) | ByteString -> Int
B.length ByteString
c forall a. Ord a => a -> a -> Bool
< Int
16 = forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk (Word8 -> ByteString -> ByteString
B.cons Word8
w ByteString
c) ByteStream m r
cs
cons' Word8
w ByteStream m r
cs = forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk (Word8 -> ByteString
B.singleton Word8
w) ByteStream m r
cs
{-# INLINE cons' #-}
snoc :: Monad m => ByteStream m r -> Word8 -> ByteStream m r
snoc :: forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> Word8 -> ByteStream m r
snoc ByteStream m r
cs Word8
w = do
r
r <- ByteStream m r
cs
forall (m :: * -> *). Monad m => Word8 -> ByteStream m ()
singleton Word8
w
forall (m :: * -> *) a. Monad m => a -> m a
return r
r
{-# INLINE snoc #-}
head_ :: Monad m => ByteStream m r -> m Word8
head_ :: forall (m :: * -> *) r. Monad m => ByteStream m r -> m Word8
head_ (Empty r
_) = forall a. HasCallStack => [Char] -> a
error [Char]
"head"
head_ (Chunk ByteString
c ByteStream m r
bs) = if ByteString -> Bool
B.null ByteString
c
then forall (m :: * -> *) r. Monad m => ByteStream m r -> m Word8
head_ ByteStream m r
bs
else forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ ByteString -> Word8
B.unsafeHead ByteString
c
head_ (Go m (ByteStream m r)
m) = m (ByteStream m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) r. Monad m => ByteStream m r -> m Word8
head_
{-# INLINABLE head_ #-}
head :: Monad m => ByteStream m r -> m (Of (Maybe Word8) r)
head :: forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Of (Maybe Word8) r)
head (Empty r
r) = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Maybe a
Nothing forall a b. a -> b -> Of a b
:> r
r)
head (Chunk ByteString
c ByteStream m r
rest) = case ByteString -> Maybe (Word8, ByteString)
B.uncons ByteString
c of
Maybe (Word8, ByteString)
Nothing -> forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Of (Maybe Word8) r)
head ByteStream m r
rest
Just (Word8
w,ByteString
_) -> do
r
r <- forall (m :: * -> *) a r. Monad m => Stream (Of a) m r -> m r
SP.effects forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> Stream (Of ByteString) m r
toChunks ByteStream m r
rest
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! forall a. a -> Maybe a
Just Word8
w forall a b. a -> b -> Of a b
:> r
r
head (Go m (ByteStream m r)
m) = m (ByteStream m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Of (Maybe Word8) r)
head
{-# INLINABLE head #-}
uncons :: Monad m => ByteStream m r -> m (Either r (Word8, ByteStream m r))
uncons :: forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Either r (Word8, ByteStream m r))
uncons (Chunk c :: ByteString
c@(ByteString -> Int
B.length -> Int
len) ByteStream m r
cs)
| Int
len forall a. Ord a => a -> a -> Bool
> Int
0 = let !h :: Word8
h = ByteString -> Word8
B.unsafeHead ByteString
c
!t :: ByteStream m r
t = if Int
len forall a. Ord a => a -> a -> Bool
> Int
1 then forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk (ByteString -> ByteString
B.unsafeTail ByteString
c) ByteStream m r
cs else ByteStream m r
cs
in forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. b -> Either a b
Right (Word8
h, ByteStream m r
t)
| Bool
otherwise = forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Either r (Word8, ByteStream m r))
uncons ByteStream m r
cs
uncons (Go m (ByteStream m r)
m) = m (ByteStream m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Either r (Word8, ByteStream m r))
uncons
uncons (Empty r
r) = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left r
r)
{-# INLINABLE uncons #-}
nextByte :: Monad m => ByteStream m r -> m (Either r (Word8, ByteStream m r))
nextByte :: forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Either r (Word8, ByteStream m r))
nextByte = forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Either r (Word8, ByteStream m r))
uncons
{-# INLINABLE nextByte #-}
{-# DEPRECATED nextByte "Use uncons instead." #-}
unconsChunk :: Monad m => ByteStream m r -> m (Either r (B.ByteString, ByteStream m r))
unconsChunk :: forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Either r (ByteString, ByteStream m r))
unconsChunk (Chunk ByteString
c ByteStream m r
cs)
| ByteString -> Bool
B.null ByteString
c = forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Either r (ByteString, ByteStream m r))
unconsChunk ByteStream m r
cs
| Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right (ByteString
c,ByteStream m r
cs))
unconsChunk (Go m (ByteStream m r)
m) = m (ByteStream m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Either r (ByteString, ByteStream m r))
unconsChunk
unconsChunk (Empty r
r) = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left r
r)
{-# INLINABLE unconsChunk #-}
nextChunk :: Monad m => ByteStream m r -> m (Either r (B.ByteString, ByteStream m r))
nextChunk :: forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Either r (ByteString, ByteStream m r))
nextChunk = forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Either r (ByteString, ByteStream m r))
unconsChunk
{-# INLINABLE nextChunk #-}
{-# DEPRECATED nextChunk "Use unconsChunk instead." #-}
last_ :: Monad m => ByteStream m r -> m Word8
last_ :: forall (m :: * -> *) r. Monad m => ByteStream m r -> m Word8
last_ (Empty r
_) = forall a. HasCallStack => [Char] -> a
error [Char]
"Streaming.ByteString.last: empty string"
last_ (Go m (ByteStream m r)
m) = m (ByteStream m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) r. Monad m => ByteStream m r -> m Word8
last_
last_ (Chunk ByteString
c0 ByteStream m r
cs0) = forall {m :: * -> *} {r}.
Monad m =>
ByteString -> ByteStream m r -> m Word8
go ByteString
c0 ByteStream m r
cs0
where
go :: ByteString -> ByteStream m r -> m Word8
go ByteString
c (Empty r
_) = if ByteString -> Bool
B.null ByteString
c
then forall a. HasCallStack => [Char] -> a
error [Char]
"Streaming.ByteString.last: empty string"
else forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ ByteString -> Word8
unsafeLast ByteString
c
go ByteString
_ (Chunk ByteString
c ByteStream m r
cs) = ByteString -> ByteStream m r -> m Word8
go ByteString
c ByteStream m r
cs
go ByteString
x (Go m (ByteStream m r)
m) = m (ByteStream m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> ByteStream m r -> m Word8
go ByteString
x
{-# INLINABLE last_ #-}
last :: Monad m => ByteStream m r -> m (Of (Maybe Word8) r)
last :: forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Of (Maybe Word8) r)
last (Empty r
r) = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Maybe a
Nothing forall a b. a -> b -> Of a b
:> r
r)
last (Go m (ByteStream m r)
m) = m (ByteStream m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Of (Maybe Word8) r)
last
last (Chunk ByteString
c0 ByteStream m r
cs0) = forall {m :: * -> *} {b}.
Monad m =>
ByteString -> ByteStream m b -> m (Of (Maybe Word8) b)
go ByteString
c0 ByteStream m r
cs0
where
go :: ByteString -> ByteStream m b -> m (Of (Maybe Word8) b)
go ByteString
c (Empty b
r) = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just (ByteString -> Word8
unsafeLast ByteString
c) forall a b. a -> b -> Of a b
:> b
r)
go ByteString
_ (Chunk ByteString
c ByteStream m b
cs) = ByteString -> ByteStream m b -> m (Of (Maybe Word8) b)
go ByteString
c ByteStream m b
cs
go ByteString
x (Go m (ByteStream m b)
m) = m (ByteStream m b)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> ByteStream m b -> m (Of (Maybe Word8) b)
go ByteString
x
{-# INLINABLE last #-}
append :: Monad m => ByteStream m r -> ByteStream m s -> ByteStream m s
append :: forall (m :: * -> *) r s.
Monad m =>
ByteStream m r -> ByteStream m s -> ByteStream m s
append ByteStream m r
xs ByteStream m s
ys = forall (m :: * -> *) r.
Monad m =>
ByteStream m r
-> forall x. (r -> x) -> (ByteString -> x -> x) -> (m x -> x) -> x
dematerialize ByteStream m r
xs (forall a b. a -> b -> a
const ByteStream m s
ys) forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk forall (m :: * -> *) r. m (ByteStream m r) -> ByteStream m r
Go
{-# INLINE append #-}
map :: Monad m => (Word8 -> Word8) -> ByteStream m r -> ByteStream m r
map :: forall (m :: * -> *) r.
Monad m =>
(Word8 -> Word8) -> ByteStream m r -> ByteStream m r
map Word8 -> Word8
f ByteStream m r
z = forall (m :: * -> *) r.
Monad m =>
ByteStream m r
-> forall x. (r -> x) -> (ByteString -> x -> x) -> (m x -> x) -> x
dematerialize ByteStream m r
z forall (m :: * -> *) r. r -> ByteStream m r
Empty (forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word8 -> Word8) -> ByteString -> ByteString
B.map Word8 -> Word8
f) forall (m :: * -> *) r. m (ByteStream m r) -> ByteStream m r
Go
{-# INLINE map #-}
for :: Monad m => ByteStream m r -> (P.ByteString -> ByteStream m x) -> ByteStream m r
for :: forall (m :: * -> *) r x.
Monad m =>
ByteStream m r -> (ByteString -> ByteStream m x) -> ByteStream m r
for ByteStream m r
stream ByteString -> ByteStream m x
f = case ByteStream m r
stream of
Empty r
r -> forall (m :: * -> *) r. r -> ByteStream m r
Empty r
r
Chunk ByteString
bs ByteStream m r
bss -> ByteString -> ByteStream m x
f ByteString
bs forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall (m :: * -> *) r x.
Monad m =>
ByteStream m r -> (ByteString -> ByteStream m x) -> ByteStream m r
for ByteStream m r
bss ByteString -> ByteStream m x
f
Go m (ByteStream m r)
m -> forall (m :: * -> *) r. m (ByteStream m r) -> ByteStream m r
Go ((forall (m :: * -> *) r x.
Monad m =>
ByteStream m r -> (ByteString -> ByteStream m x) -> ByteStream m r
`for` ByteString -> ByteStream m x
f) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (ByteStream m r)
m)
{-# INLINE for #-}
intersperse :: Monad m => Word8 -> ByteStream m r -> ByteStream m r
intersperse :: forall (m :: * -> *) r.
Monad m =>
Word8 -> ByteStream m r -> ByteStream m r
intersperse Word8
_ (Empty r
r) = forall (m :: * -> *) r. r -> ByteStream m r
Empty r
r
intersperse Word8
w (Go m (ByteStream m r)
m) = forall (m :: * -> *) r. m (ByteStream m r) -> ByteStream m r
Go (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (m :: * -> *) r.
Monad m =>
Word8 -> ByteStream m r -> ByteStream m r
intersperse Word8
w) m (ByteStream m r)
m)
intersperse Word8
w (Chunk ByteString
c ByteStream m r
cs) | ByteString -> Bool
B.null ByteString
c = forall (m :: * -> *) r.
Monad m =>
Word8 -> ByteStream m r -> ByteStream m r
intersperse Word8
w ByteStream m r
cs
| Bool
otherwise =
forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk (Word8 -> ByteString -> ByteString
B.intersperse Word8
w ByteString
c)
(forall (m :: * -> *) r.
Monad m =>
ByteStream m r
-> forall x. (r -> x) -> (ByteString -> x -> x) -> (m x -> x) -> x
dematerialize ByteStream m r
cs forall (m :: * -> *) r. r -> ByteStream m r
Empty (forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
intersperse') forall (m :: * -> *) r. m (ByteStream m r) -> ByteStream m r
Go)
where intersperse' :: P.ByteString -> P.ByteString
intersperse' :: ByteString -> ByteString
intersperse' (B.PS ForeignPtr Word8
fp Int
o Int
l)
| Int
l forall a. Ord a => a -> a -> Bool
> Int
0 = Int -> (Ptr Word8 -> IO ()) -> ByteString
B.unsafeCreate (Int
2forall a. Num a => a -> a -> a
*Int
l) forall a b. (a -> b) -> a -> b
$ \Ptr Word8
p' -> forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
unsafeWithForeignPtr ForeignPtr Word8
fp forall a b. (a -> b) -> a -> b
$ \Ptr Word8
p -> do
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr Word8
p' Word8
w
Ptr Word8 -> Ptr Word8 -> CSize -> Word8 -> IO ()
B.c_intersperse (Ptr Word8
p' forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
1) (Ptr Word8
p forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
o) (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
l) Word8
w
| Bool
otherwise = ByteString
B.empty
{-# INLINABLE intersperse #-}
foldr :: Monad m => (Word8 -> a -> a) -> a -> ByteStream m () -> m a
foldr :: forall (m :: * -> *) a.
Monad m =>
(Word8 -> a -> a) -> a -> ByteStream m () -> m a
foldr Word8 -> a -> a
k = forall (m :: * -> *) a r.
Monad m =>
(ByteString -> a -> a) -> a -> ByteStream m r -> m a
foldrChunks (forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall a. (Word8 -> a -> a) -> a -> ByteString -> a
B.foldr Word8 -> a -> a
k))
{-# INLINE foldr #-}
fold_ :: Monad m => (x -> Word8 -> x) -> x -> (x -> b) -> ByteStream m () -> m b
fold_ :: forall (m :: * -> *) x b.
Monad m =>
(x -> Word8 -> x) -> x -> (x -> b) -> ByteStream m () -> m b
fold_ x -> Word8 -> x
step0 x
begin x -> b
finish ByteStream m ()
p0 = ByteStream m () -> x -> m b
loop ByteStream m ()
p0 x
begin
where
loop :: ByteStream m () -> x -> m b
loop ByteStream m ()
p !x
x = case ByteStream m ()
p of
Chunk ByteString
bs ByteStream m ()
bss -> ByteStream m () -> x -> m b
loop ByteStream m ()
bss forall a b. (a -> b) -> a -> b
$! forall a. (a -> Word8 -> a) -> a -> ByteString -> a
B.foldl' x -> Word8 -> x
step0 x
x ByteString
bs
Go m (ByteStream m ())
m -> m (ByteStream m ())
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ByteStream m ()
p' -> ByteStream m () -> x -> m b
loop ByteStream m ()
p' x
x
Empty ()
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return (x -> b
finish x
x)
{-# INLINABLE fold_ #-}
fold :: Monad m => (x -> Word8 -> x) -> x -> (x -> b) -> ByteStream m r -> m (Of b r)
fold :: forall (m :: * -> *) x b r.
Monad m =>
(x -> Word8 -> x) -> x -> (x -> b) -> ByteStream m r -> m (Of b r)
fold x -> Word8 -> x
step0 x
begin x -> b
finish ByteStream m r
p0 = ByteStream m r -> x -> m (Of b r)
loop ByteStream m r
p0 x
begin
where
loop :: ByteStream m r -> x -> m (Of b r)
loop ByteStream m r
p !x
x = case ByteStream m r
p of
Chunk ByteString
bs ByteStream m r
bss -> ByteStream m r -> x -> m (Of b r)
loop ByteStream m r
bss forall a b. (a -> b) -> a -> b
$! forall a. (a -> Word8 -> a) -> a -> ByteString -> a
B.foldl' x -> Word8 -> x
step0 x
x ByteString
bs
Go m (ByteStream m r)
m -> m (ByteStream m r)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ByteStream m r
p' -> ByteStream m r -> x -> m (Of b r)
loop ByteStream m r
p' x
x
Empty r
r -> forall (m :: * -> *) a. Monad m => a -> m a
return (x -> b
finish x
x forall a b. a -> b -> Of a b
:> r
r)
{-# INLINABLE fold #-}
iterate :: (Word8 -> Word8) -> Word8 -> ByteStream m r
iterate :: forall (m :: * -> *) r. (Word8 -> Word8) -> Word8 -> ByteStream m r
iterate Word8 -> Word8
f = forall a r (m :: * -> *).
(a -> Either r (Word8, a)) -> a -> ByteStream m r
unfoldr (\Word8
x -> case Word8 -> Word8
f Word8
x of !Word8
x' -> forall a b. b -> Either a b
Right (Word8
x', Word8
x'))
{-# INLINABLE iterate #-}
repeat :: Word8 -> ByteStream m r
repeat :: forall (m :: * -> *) r. Word8 -> ByteStream m r
repeat Word8
w = ByteStream m r
cs where cs :: ByteStream m r
cs = forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk (Int -> Word8 -> ByteString
B.replicate Int
BI.smallChunkSize Word8
w) ByteStream m r
cs
{-# INLINABLE repeat #-}
cycle :: Monad m => ByteStream m r -> ByteStream m s
cycle :: forall (m :: * -> *) r s.
Monad m =>
ByteStream m r -> ByteStream m s
cycle = forall (f :: * -> *) a b. Applicative f => f a -> f b
forever
{-# INLINE cycle #-}
unfoldM :: Monad m => (a -> Maybe (Word8, a)) -> a -> ByteStream m ()
unfoldM :: forall (m :: * -> *) a.
Monad m =>
(a -> Maybe (Word8, a)) -> a -> ByteStream m ()
unfoldM a -> Maybe (Word8, a)
f a
s0 = Int -> a -> ByteStream m ()
unfoldChunk Int
32 a
s0
where unfoldChunk :: Int -> a -> ByteStream m ()
unfoldChunk Int
n a
s =
case forall a.
Int -> (a -> Maybe (Word8, a)) -> a -> (ByteString, Maybe a)
B.unfoldrN Int
n a -> Maybe (Word8, a)
f a
s of
(ByteString
c, Maybe a
Nothing)
| ByteString -> Bool
B.null ByteString
c -> forall (m :: * -> *) r. r -> ByteStream m r
Empty ()
| Bool
otherwise -> forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
c (forall (m :: * -> *) r. r -> ByteStream m r
Empty ())
(ByteString
c, Just a
s') -> forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
c (Int -> a -> ByteStream m ()
unfoldChunk (Int
nforall a. Num a => a -> a -> a
*Int
2) a
s')
{-# INLINABLE unfoldM #-}
unfoldr :: (a -> Either r (Word8, a)) -> a -> ByteStream m r
unfoldr :: forall a r (m :: * -> *).
(a -> Either r (Word8, a)) -> a -> ByteStream m r
unfoldr a -> Either r (Word8, a)
f a
s0 = Int -> a -> ByteStream m r
unfoldChunk Int
32 a
s0
where unfoldChunk :: Int -> a -> ByteStream m r
unfoldChunk Int
n a
s =
case forall a r.
Int -> (a -> Either r (Word8, a)) -> a -> (ByteString, Either r a)
unfoldrNE Int
n a -> Either r (Word8, a)
f a
s of
(ByteString
c, Left r
r)
| ByteString -> Bool
B.null ByteString
c -> forall (m :: * -> *) r. r -> ByteStream m r
Empty r
r
| Bool
otherwise -> forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
c (forall (m :: * -> *) r. r -> ByteStream m r
Empty r
r)
(ByteString
c, Right a
s') -> forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
c (Int -> a -> ByteStream m r
unfoldChunk (Int
nforall a. Num a => a -> a -> a
*Int
2) a
s')
{-# INLINABLE unfoldr #-}
take :: Monad m => Int64 -> ByteStream m r -> ByteStream m ()
take :: forall (m :: * -> *) r.
Monad m =>
Int64 -> ByteStream m r -> ByteStream m ()
take Int64
i ByteStream m r
_ | Int64
i forall a. Ord a => a -> a -> Bool
<= Int64
0 = forall (m :: * -> *) r. r -> ByteStream m r
Empty ()
take Int64
i ByteStream m r
cs0 = forall {t} {m :: * -> *} {r}.
(Integral t, Functor m) =>
t -> ByteStream m r -> ByteStream m ()
take' Int64
i ByteStream m r
cs0
where take' :: t -> ByteStream m r -> ByteStream m ()
take' t
0 ByteStream m r
_ = forall (m :: * -> *) r. r -> ByteStream m r
Empty ()
take' t
_ (Empty r
_) = forall (m :: * -> *) r. r -> ByteStream m r
Empty ()
take' t
n (Chunk ByteString
c ByteStream m r
cs) =
if t
n forall a. Ord a => a -> a -> Bool
< forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
B.length ByteString
c)
then forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk (Int -> ByteString -> ByteString
B.take (forall a b. (Integral a, Num b) => a -> b
fromIntegral t
n) ByteString
c) (forall (m :: * -> *) r. r -> ByteStream m r
Empty ())
else forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
c (t -> ByteStream m r -> ByteStream m ()
take' (t
n forall a. Num a => a -> a -> a
- forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
B.length ByteString
c)) ByteStream m r
cs)
take' t
n (Go m (ByteStream m r)
m) = forall (m :: * -> *) r. m (ByteStream m r) -> ByteStream m r
Go (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (t -> ByteStream m r -> ByteStream m ()
take' t
n) m (ByteStream m r)
m)
{-# INLINABLE take #-}
drop :: Monad m => Int64 -> ByteStream m r -> ByteStream m r
drop :: forall (m :: * -> *) r.
Monad m =>
Int64 -> ByteStream m r -> ByteStream m r
drop Int64
i ByteStream m r
p | Int64
i forall a. Ord a => a -> a -> Bool
<= Int64
0 = ByteStream m r
p
drop Int64
i ByteStream m r
cs0 = forall {t} {m :: * -> *} {r}.
(Integral t, Functor m) =>
t -> ByteStream m r -> ByteStream m r
drop' Int64
i ByteStream m r
cs0
where drop' :: t -> ByteStream m r -> ByteStream m r
drop' t
0 ByteStream m r
cs = ByteStream m r
cs
drop' t
_ (Empty r
r) = forall (m :: * -> *) r. r -> ByteStream m r
Empty r
r
drop' t
n (Chunk ByteString
c ByteStream m r
cs) =
if t
n forall a. Ord a => a -> a -> Bool
< forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
B.length ByteString
c)
then forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk (Int -> ByteString -> ByteString
B.drop (forall a b. (Integral a, Num b) => a -> b
fromIntegral t
n) ByteString
c) ByteStream m r
cs
else t -> ByteStream m r -> ByteStream m r
drop' (t
n forall a. Num a => a -> a -> a
- forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
B.length ByteString
c)) ByteStream m r
cs
drop' t
n (Go m (ByteStream m r)
m) = forall (m :: * -> *) r. m (ByteStream m r) -> ByteStream m r
Go (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (t -> ByteStream m r -> ByteStream m r
drop' t
n) m (ByteStream m r)
m)
{-# INLINABLE drop #-}
splitAt :: Monad m => Int64 -> ByteStream m r -> ByteStream m (ByteStream m r)
splitAt :: forall (m :: * -> *) r.
Monad m =>
Int64 -> ByteStream m r -> ByteStream m (ByteStream m r)
splitAt Int64
i ByteStream m r
cs0 | Int64
i forall a. Ord a => a -> a -> Bool
<= Int64
0 = forall (m :: * -> *) r. r -> ByteStream m r
Empty ByteStream m r
cs0
splitAt Int64
i ByteStream m r
cs0 = forall {t} {m :: * -> *} {r}.
(Integral t, Functor m) =>
t -> ByteStream m r -> ByteStream m (ByteStream m r)
splitAt' Int64
i ByteStream m r
cs0
where splitAt' :: t -> ByteStream m r -> ByteStream m (ByteStream m r)
splitAt' t
0 ByteStream m r
cs = forall (m :: * -> *) r. r -> ByteStream m r
Empty ByteStream m r
cs
splitAt' t
_ (Empty r
r ) = forall (m :: * -> *) r. r -> ByteStream m r
Empty (forall (m :: * -> *) r. r -> ByteStream m r
Empty r
r)
splitAt' t
n (Chunk ByteString
c ByteStream m r
cs) =
if t
n forall a. Ord a => a -> a -> Bool
< forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
B.length ByteString
c)
then forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk (Int -> ByteString -> ByteString
B.take (forall a b. (Integral a, Num b) => a -> b
fromIntegral t
n) ByteString
c) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) r. r -> ByteStream m r
Empty (forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk (Int -> ByteString -> ByteString
B.drop (forall a b. (Integral a, Num b) => a -> b
fromIntegral t
n) ByteString
c) ByteStream m r
cs)
else forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
c (t -> ByteStream m r -> ByteStream m (ByteStream m r)
splitAt' (t
n forall a. Num a => a -> a -> a
- forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
B.length ByteString
c)) ByteStream m r
cs)
splitAt' t
n (Go m (ByteStream m r)
m) = forall (m :: * -> *) r. m (ByteStream m r) -> ByteStream m r
Go (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (t -> ByteStream m r -> ByteStream m (ByteStream m r)
splitAt' t
n) m (ByteStream m r)
m)
{-# INLINABLE splitAt #-}
takeWhile :: Monad m => (Word8 -> Bool) -> ByteStream m r -> ByteStream m ()
takeWhile :: forall (m :: * -> *) r.
Monad m =>
(Word8 -> Bool) -> ByteStream m r -> ByteStream m ()
takeWhile Word8 -> Bool
f ByteStream m r
cs0 = ByteStream m r -> ByteStream m ()
takeWhile' ByteStream m r
cs0
where
takeWhile' :: ByteStream m r -> ByteStream m ()
takeWhile' (Empty r
_) = forall (m :: * -> *) r. r -> ByteStream m r
Empty ()
takeWhile' (Go m (ByteStream m r)
m) = forall (m :: * -> *) r. m (ByteStream m r) -> ByteStream m r
Go forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteStream m r -> ByteStream m ()
takeWhile' m (ByteStream m r)
m
takeWhile' (Chunk ByteString
c ByteStream m r
cs) =
case (Word8 -> Bool) -> ByteString -> Int
findIndexOrEnd (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Bool
f) ByteString
c of
Int
0 -> forall (m :: * -> *) r. r -> ByteStream m r
Empty ()
Int
n | Int
n forall a. Ord a => a -> a -> Bool
< ByteString -> Int
B.length ByteString
c -> forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk (Int -> ByteString -> ByteString
B.take Int
n ByteString
c) (forall (m :: * -> *) r. r -> ByteStream m r
Empty ())
| Bool
otherwise -> forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
c (ByteStream m r -> ByteStream m ()
takeWhile' ByteStream m r
cs)
{-# INLINABLE takeWhile #-}
dropWhile :: Monad m => (Word8 -> Bool) -> ByteStream m r -> ByteStream m r
dropWhile :: forall (m :: * -> *) r.
Monad m =>
(Word8 -> Bool) -> ByteStream m r -> ByteStream m r
dropWhile Word8 -> Bool
p = ByteStream m r -> ByteStream m r
drop' where
drop' :: ByteStream m r -> ByteStream m r
drop' ByteStream m r
bs = case ByteStream m r
bs of
Empty r
r -> forall (m :: * -> *) r. r -> ByteStream m r
Empty r
r
Go m (ByteStream m r)
m -> forall (m :: * -> *) r. m (ByteStream m r) -> ByteStream m r
Go (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteStream m r -> ByteStream m r
drop' m (ByteStream m r)
m)
Chunk ByteString
c ByteStream m r
cs -> case (Word8 -> Bool) -> ByteString -> Int
findIndexOrEnd (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Bool
p) ByteString
c of
Int
0 -> forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
c ByteStream m r
cs
Int
n | Int
n forall a. Ord a => a -> a -> Bool
< ByteString -> Int
B.length ByteString
c -> forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk (Int -> ByteString -> ByteString
B.drop Int
n ByteString
c) ByteStream m r
cs
| Bool
otherwise -> ByteStream m r -> ByteStream m r
drop' ByteStream m r
cs
{-# INLINABLE dropWhile #-}
break :: Monad m => (Word8 -> Bool) -> ByteStream m r -> ByteStream m (ByteStream m r)
break :: forall (m :: * -> *) r.
Monad m =>
(Word8 -> Bool) -> ByteStream m r -> ByteStream m (ByteStream m r)
break Word8 -> Bool
f ByteStream m r
cs0 = ByteStream m r -> ByteStream m (ByteStream m r)
break' ByteStream m r
cs0
where break' :: ByteStream m r -> ByteStream m (ByteStream m r)
break' (Empty r
r) = forall (m :: * -> *) r. r -> ByteStream m r
Empty (forall (m :: * -> *) r. r -> ByteStream m r
Empty r
r)
break' (Chunk ByteString
c ByteStream m r
cs) =
case (Word8 -> Bool) -> ByteString -> Int
findIndexOrEnd Word8 -> Bool
f ByteString
c of
Int
0 -> forall (m :: * -> *) r. r -> ByteStream m r
Empty (forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
c ByteStream m r
cs)
Int
n | Int
n forall a. Ord a => a -> a -> Bool
< ByteString -> Int
B.length ByteString
c -> forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk (Int -> ByteString -> ByteString
B.take Int
n ByteString
c) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) r. r -> ByteStream m r
Empty (forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk (Int -> ByteString -> ByteString
B.drop Int
n ByteString
c) ByteStream m r
cs)
| Bool
otherwise -> forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
c (ByteStream m r -> ByteStream m (ByteStream m r)
break' ByteStream m r
cs)
break' (Go m (ByteStream m r)
m) = forall (m :: * -> *) r. m (ByteStream m r) -> ByteStream m r
Go (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteStream m r -> ByteStream m (ByteStream m r)
break' m (ByteStream m r)
m)
{-# INLINABLE break #-}
span :: Monad m => (Word8 -> Bool) -> ByteStream m r -> ByteStream m (ByteStream m r)
span :: forall (m :: * -> *) r.
Monad m =>
(Word8 -> Bool) -> ByteStream m r -> ByteStream m (ByteStream m r)
span Word8 -> Bool
p = forall (m :: * -> *) r.
Monad m =>
(Word8 -> Bool) -> ByteStream m r -> ByteStream m (ByteStream m r)
break (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Bool
p)
{-# INLINE span #-}
splitWith :: Monad m => (Word8 -> Bool) -> ByteStream m r -> Stream (ByteStream m) m r
splitWith :: forall (m :: * -> *) r.
Monad m =>
(Word8 -> Bool) -> ByteStream m r -> Stream (ByteStream m) m r
splitWith Word8 -> Bool
_ (Empty r
r) = forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
splitWith Word8 -> Bool
p (Go m (ByteStream m r)
m) = forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (m :: * -> *) r.
Monad m =>
(Word8 -> Bool) -> ByteStream m r -> Stream (ByteStream m) m r
splitWith Word8 -> Bool
p) m (ByteStream m r)
m
splitWith Word8 -> Bool
p (Chunk ByteString
c0 ByteStream m r
cs0) = [ByteString]
-> [ByteString] -> ByteStream m r -> Stream (ByteStream m) m r
comb [] ((Word8 -> Bool) -> ByteString -> [ByteString]
B.splitWith Word8 -> Bool
p ByteString
c0) ByteStream m r
cs0
where
comb :: [ByteString]
-> [ByteString] -> ByteStream m r -> Stream (ByteStream m) m r
comb [ByteString]
acc [ByteString
s] (Empty r
r) = forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
L.foldl' (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk)
(forall (m :: * -> *) r. r -> ByteStream m r
Empty (forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r))
(ByteString
sforall a. a -> [a] -> [a]
:[ByteString]
acc)
comb [ByteString]
acc [ByteString
s] (Chunk ByteString
c ByteStream m r
cs) = [ByteString]
-> [ByteString] -> ByteStream m r -> Stream (ByteStream m) m r
comb (ByteString
sforall a. a -> [a] -> [a]
:[ByteString]
acc) ((Word8 -> Bool) -> ByteString -> [ByteString]
B.splitWith Word8 -> Bool
p ByteString
c) ByteStream m r
cs
comb [ByteString]
acc [ByteString]
b (Go m (ByteStream m r)
m) = forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([ByteString]
-> [ByteString] -> ByteStream m r -> Stream (ByteStream m) m r
comb [ByteString]
acc [ByteString]
b) m (ByteStream m r)
m)
comb [ByteString]
acc (ByteString
s:[ByteString]
ss) ByteStream m r
cs = forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
L.foldl' (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk)
(forall (m :: * -> *) r. r -> ByteStream m r
Empty ([ByteString]
-> [ByteString] -> ByteStream m r -> Stream (ByteStream m) m r
comb [] [ByteString]
ss ByteStream m r
cs))
(ByteString
sforall a. a -> [a] -> [a]
:[ByteString]
acc)
comb [ByteString]
acc [] (Empty r
r) = forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
L.foldl' (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk)
(forall (m :: * -> *) r. r -> ByteStream m r
Empty (forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r))
[ByteString]
acc
comb [ByteString]
acc [] (Chunk ByteString
c ByteStream m r
cs) = [ByteString]
-> [ByteString] -> ByteStream m r -> Stream (ByteStream m) m r
comb [ByteString]
acc ((Word8 -> Bool) -> ByteString -> [ByteString]
B.splitWith Word8 -> Bool
p ByteString
c) ByteStream m r
cs
{-# INLINABLE splitWith #-}
split :: Monad m => Word8 -> ByteStream m r -> Stream (ByteStream m) m r
split :: forall (m :: * -> *) r.
Monad m =>
Word8 -> ByteStream m r -> Stream (ByteStream m) m r
split Word8
w = ByteStream m r -> Stream (ByteStream m) m r
loop
where
loop :: ByteStream m r -> Stream (ByteStream m) m r
loop !ByteStream m r
x = case ByteStream m r
x of
Empty r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Go m (ByteStream m r)
m -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteStream m r -> Stream (ByteStream m) m r
loop m (ByteStream m r)
m
Chunk ByteString
c0 ByteStream m r
cs0 -> [ByteString]
-> [ByteString] -> ByteStream m r -> Stream (ByteStream m) m r
comb [] (Word8 -> ByteString -> [ByteString]
B.split Word8
w ByteString
c0) ByteStream m r
cs0
comb :: [ByteString]
-> [ByteString] -> ByteStream m r -> Stream (ByteStream m) m r
comb ![ByteString]
acc [] (Empty r
r) = forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r.
Monad m =>
[ByteString] -> r -> ByteStream m r
revChunks [ByteString]
acc (forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r)
comb [ByteString]
acc [] (Chunk ByteString
c ByteStream m r
cs) = [ByteString]
-> [ByteString] -> ByteStream m r -> Stream (ByteStream m) m r
comb [ByteString]
acc (Word8 -> ByteString -> [ByteString]
B.split Word8
w ByteString
c) ByteStream m r
cs
comb ![ByteString]
acc [ByteString
s] (Empty r
r) = forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r.
Monad m =>
[ByteString] -> r -> ByteStream m r
revChunks (ByteString
sforall a. a -> [a] -> [a]
:[ByteString]
acc) (forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r)
comb [ByteString]
acc [ByteString
s] (Chunk ByteString
c ByteStream m r
cs) = [ByteString]
-> [ByteString] -> ByteStream m r -> Stream (ByteStream m) m r
comb (ByteString
sforall a. a -> [a] -> [a]
:[ByteString]
acc) (Word8 -> ByteString -> [ByteString]
B.split Word8
w ByteString
c) ByteStream m r
cs
comb [ByteString]
acc [ByteString]
b (Go m (ByteStream m r)
m) = forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([ByteString]
-> [ByteString] -> ByteStream m r -> Stream (ByteStream m) m r
comb [ByteString]
acc [ByteString]
b) m (ByteStream m r)
m)
comb [ByteString]
acc (ByteString
s:[ByteString]
ss) ByteStream m r
cs = forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r.
Monad m =>
[ByteString] -> r -> ByteStream m r
revChunks (ByteString
sforall a. a -> [a] -> [a]
:[ByteString]
acc) ([ByteString]
-> [ByteString] -> ByteStream m r -> Stream (ByteStream m) m r
comb [] [ByteString]
ss ByteStream m r
cs)
{-# INLINABLE split #-}
group :: Monad m => ByteStream m r -> Stream (ByteStream m) m r
group :: forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> Stream (ByteStream m) m r
group = forall {m :: * -> *} {r}.
Functor m =>
ByteStream m r -> Stream (ByteStream m) m r
go
where
go :: ByteStream m r -> Stream (ByteStream m) m r
go (Empty r
r) = forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
go (Go m (ByteStream m r)
m) = forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteStream m r -> Stream (ByteStream m) m r
go m (ByteStream m r)
m
go (Chunk ByteString
c ByteStream m r
cs)
| ByteString -> Int
B.length ByteString
c forall a. Eq a => a -> a -> Bool
== Int
1 = forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b. (a -> b) -> a -> b
$ [ByteString]
-> Word8
-> ByteStream m r
-> ByteStream m (Stream (ByteStream m) m r)
to [ByteString
c] (ByteString -> Word8
B.unsafeHead ByteString
c) ByteStream m r
cs
| Bool
otherwise = forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b. (a -> b) -> a -> b
$ [ByteString]
-> Word8
-> ByteStream m r
-> ByteStream m (Stream (ByteStream m) m r)
to [Int -> ByteString -> ByteString
B.unsafeTake Int
1 ByteString
c] (ByteString -> Word8
B.unsafeHead ByteString
c) (forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk (ByteString -> ByteString
B.unsafeTail ByteString
c) ByteStream m r
cs)
to :: [ByteString]
-> Word8
-> ByteStream m r
-> ByteStream m (Stream (ByteStream m) m r)
to [ByteString]
acc !Word8
_ (Empty r
r) = forall (m :: * -> *) r.
[ByteString] -> ByteStream m r -> ByteStream m r
revNonEmptyChunks [ByteString]
acc (forall (m :: * -> *) r. r -> ByteStream m r
Empty (forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r))
to [ByteString]
acc !Word8
w (Go m (ByteStream m r)
m) = forall (m :: * -> *) r. m (ByteStream m r) -> ByteStream m r
Go forall a b. (a -> b) -> a -> b
$ [ByteString]
-> Word8
-> ByteStream m r
-> ByteStream m (Stream (ByteStream m) m r)
to [ByteString]
acc Word8
w forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (ByteStream m r)
m
to [ByteString]
acc !Word8
w (Chunk ByteString
c ByteStream m r
cs) = case (Word8 -> Bool) -> ByteString -> Int
findIndexOrEnd (forall a. Eq a => a -> a -> Bool
/= Word8
w) ByteString
c of
Int
0 -> forall (m :: * -> *) r.
[ByteString] -> ByteStream m r -> ByteStream m r
revNonEmptyChunks [ByteString]
acc (forall (m :: * -> *) r. r -> ByteStream m r
Empty (ByteStream m r -> Stream (ByteStream m) m r
go (forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
c ByteStream m r
cs)))
Int
n | Int
n forall a. Eq a => a -> a -> Bool
== ByteString -> Int
B.length ByteString
c -> [ByteString]
-> Word8
-> ByteStream m r
-> ByteStream m (Stream (ByteStream m) m r)
to (Int -> ByteString -> ByteString
B.unsafeTake Int
n ByteString
c forall a. a -> [a] -> [a]
: [ByteString]
acc) Word8
w ByteStream m r
cs
| Bool
otherwise -> forall (m :: * -> *) r.
[ByteString] -> ByteStream m r -> ByteStream m r
revNonEmptyChunks (Int -> ByteString -> ByteString
B.unsafeTake Int
n ByteString
c forall a. a -> [a] -> [a]
: [ByteString]
acc) (forall (m :: * -> *) r. r -> ByteStream m r
Empty (ByteStream m r -> Stream (ByteStream m) m r
go (forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk (Int -> ByteString -> ByteString
B.unsafeDrop Int
n ByteString
c) ByteStream m r
cs)))
{-# INLINABLE group #-}
groupBy :: Monad m => (Word8 -> Word8 -> Bool) -> ByteStream m r -> Stream (ByteStream m) m r
groupBy :: forall (m :: * -> *) r.
Monad m =>
(Word8 -> Word8 -> Bool)
-> ByteStream m r -> Stream (ByteStream m) m r
groupBy Word8 -> Word8 -> Bool
rel = ByteStream m r -> Stream (ByteStream m) m r
go
where
go :: ByteStream m r -> Stream (ByteStream m) m r
go (Empty r
r) = forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
go (Go m (ByteStream m r)
m) = forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteStream m r -> Stream (ByteStream m) m r
go m (ByteStream m r)
m
go (Chunk ByteString
c ByteStream m r
cs)
| ByteString -> Int
B.length ByteString
c forall a. Eq a => a -> a -> Bool
== Int
1 = forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b. (a -> b) -> a -> b
$ [ByteString]
-> Word8
-> ByteStream m r
-> ByteStream m (Stream (ByteStream m) m r)
to [ByteString
c] (ByteString -> Word8
B.unsafeHead ByteString
c) ByteStream m r
cs
| Bool
otherwise = forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b. (a -> b) -> a -> b
$ [ByteString]
-> Word8
-> ByteStream m r
-> ByteStream m (Stream (ByteStream m) m r)
to [Int -> ByteString -> ByteString
B.unsafeTake Int
1 ByteString
c] (ByteString -> Word8
B.unsafeHead ByteString
c) (forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk (ByteString -> ByteString
B.unsafeTail ByteString
c) ByteStream m r
cs)
to :: [ByteString]
-> Word8
-> ByteStream m r
-> ByteStream m (Stream (ByteStream m) m r)
to [ByteString]
acc !Word8
_ (Empty r
r) = forall (m :: * -> *) r.
[ByteString] -> ByteStream m r -> ByteStream m r
revNonEmptyChunks [ByteString]
acc (forall (m :: * -> *) r. r -> ByteStream m r
Empty (forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r))
to [ByteString]
acc !Word8
w (Go m (ByteStream m r)
m) = forall (m :: * -> *) r. m (ByteStream m r) -> ByteStream m r
Go forall a b. (a -> b) -> a -> b
$ [ByteString]
-> Word8
-> ByteStream m r
-> ByteStream m (Stream (ByteStream m) m r)
to [ByteString]
acc Word8
w forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (ByteStream m r)
m
to [ByteString]
acc !Word8
w (Chunk ByteString
c ByteStream m r
cs) = case (Word8 -> Bool) -> ByteString -> Int
findIndexOrEnd (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Word8 -> Bool
rel Word8
w) ByteString
c of
Int
0 -> forall (m :: * -> *) r.
[ByteString] -> ByteStream m r -> ByteStream m r
revNonEmptyChunks [ByteString]
acc (forall (m :: * -> *) r. r -> ByteStream m r
Empty (ByteStream m r -> Stream (ByteStream m) m r
go (forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
c ByteStream m r
cs)))
Int
n | Int
n forall a. Eq a => a -> a -> Bool
== ByteString -> Int
B.length ByteString
c -> [ByteString]
-> Word8
-> ByteStream m r
-> ByteStream m (Stream (ByteStream m) m r)
to (Int -> ByteString -> ByteString
B.unsafeTake Int
n ByteString
c forall a. a -> [a] -> [a]
: [ByteString]
acc) Word8
w ByteStream m r
cs
| Bool
otherwise -> forall (m :: * -> *) r.
[ByteString] -> ByteStream m r -> ByteStream m r
revNonEmptyChunks (Int -> ByteString -> ByteString
B.unsafeTake Int
n ByteString
c forall a. a -> [a] -> [a]
: [ByteString]
acc) (forall (m :: * -> *) r. r -> ByteStream m r
Empty (ByteStream m r -> Stream (ByteStream m) m r
go (forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk (Int -> ByteString -> ByteString
B.unsafeDrop Int
n ByteString
c) ByteStream m r
cs)))
{-# INLINABLE groupBy #-}
intercalate :: Monad m => ByteStream m () -> Stream (ByteStream m) m r -> ByteStream m r
intercalate :: forall (m :: * -> *) r.
Monad m =>
ByteStream m () -> Stream (ByteStream m) m r -> ByteStream m r
intercalate ByteStream m ()
s = Stream (ByteStream m) m r -> ByteStream m r
loop
where
loop :: Stream (ByteStream m) m r -> ByteStream m r
loop (Return r
r) = forall (m :: * -> *) r. r -> ByteStream m r
Empty r
r
loop (Effect m (Stream (ByteStream m) m r)
m) = forall (m :: * -> *) r. m (ByteStream m r) -> ByteStream m r
Go forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Stream (ByteStream m) m r -> ByteStream m r
loop m (Stream (ByteStream m) m r)
m
loop (Step ByteStream m (Stream (ByteStream m) m r)
bs) = ByteStream m (Stream (ByteStream m) m r)
bs forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Return r
r -> forall (m :: * -> *) r. r -> ByteStream m r
Empty r
r
Stream (ByteStream m) m r
x -> ByteStream m ()
s forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Stream (ByteStream m) m r -> ByteStream m r
loop Stream (ByteStream m) m r
x
{-# INLINABLE intercalate #-}
count_ :: Monad m => Word8 -> ByteStream m r -> m Int
count_ :: forall (m :: * -> *) r. Monad m => Word8 -> ByteStream m r -> m Int
count_ Word8
w = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Int
n :> r
_) -> Int
n) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a r.
Monad m =>
(a -> ByteString -> a) -> a -> ByteStream m r -> m (Of a r)
foldlChunks (\Int
n ByteString
c -> Int
n forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> ByteString -> Int
B.count Word8
w ByteString
c)) Int
0
{-# INLINE count_ #-}
count :: Monad m => Word8 -> ByteStream m r -> m (Of Int r)
count :: forall (m :: * -> *) r.
Monad m =>
Word8 -> ByteStream m r -> m (Of Int r)
count Word8
w ByteStream m r
cs = forall (m :: * -> *) a r.
Monad m =>
(a -> ByteString -> a) -> a -> ByteStream m r -> m (Of a r)
foldlChunks (\Int
n ByteString
c -> Int
n forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> ByteString -> Int
B.count Word8
w ByteString
c)) Int
0 ByteStream m r
cs
{-# INLINE count #-}
filter :: Monad m => (Word8 -> Bool) -> ByteStream m r -> ByteStream m r
filter :: forall (m :: * -> *) r.
Monad m =>
(Word8 -> Bool) -> ByteStream m r -> ByteStream m r
filter Word8 -> Bool
p ByteStream m r
s = ByteStream m r -> ByteStream m r
go ByteStream m r
s
where
go :: ByteStream m r -> ByteStream m r
go (Empty r
r ) = forall (m :: * -> *) r. r -> ByteStream m r
Empty r
r
go (Chunk ByteString
x ByteStream m r
xs) = forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
consChunk ((Word8 -> Bool) -> ByteString -> ByteString
B.filter Word8 -> Bool
p ByteString
x) (ByteStream m r -> ByteStream m r
go ByteStream m r
xs)
go (Go m (ByteStream m r)
m) = forall (m :: * -> *) r. m (ByteStream m r) -> ByteStream m r
Go (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteStream m r -> ByteStream m r
go m (ByteStream m r)
m)
{-# INLINABLE filter #-}
hGetContentsN :: MonadIO m => Int -> Handle -> ByteStream m ()
hGetContentsN :: forall (m :: * -> *). MonadIO m => Int -> Handle -> ByteStream m ()
hGetContentsN Int
k Handle
h = ByteStream m ()
loop
where
loop :: ByteStream m ()
loop = do
ByteString
c <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> Int -> IO ByteString
B.hGetSome Handle
h Int
k)
if ByteString -> Bool
B.null ByteString
c
then forall (m :: * -> *) r. r -> ByteStream m r
Empty ()
else forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
c ByteStream m ()
loop
{-# INLINABLE hGetContentsN #-}
hGetN :: MonadIO m => Int -> Handle -> Int -> ByteStream m ()
hGetN :: forall (m :: * -> *).
MonadIO m =>
Int -> Handle -> Int -> ByteStream m ()
hGetN Int
k Handle
h Int
n | Int
n forall a. Ord a => a -> a -> Bool
> Int
0 = Int -> ByteStream m ()
readChunks Int
n
where
readChunks :: Int -> ByteStream m ()
readChunks !Int
i = forall (m :: * -> *) r. m (ByteStream m r) -> ByteStream m r
Go forall a b. (a -> b) -> a -> b
$ do
ByteString
c <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Handle -> Int -> IO ByteString
B.hGet Handle
h (forall a. Ord a => a -> a -> a
min Int
k Int
i)
case ByteString -> Int
B.length ByteString
c of
Int
0 -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r. r -> ByteStream m r
Empty ()
Int
m -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
c (Int -> ByteStream m ()
readChunks (Int
i forall a. Num a => a -> a -> a
- Int
m))
hGetN Int
_ Handle
_ Int
0 = forall (m :: * -> *) r. r -> ByteStream m r
Empty ()
hGetN Int
_ Handle
h Int
n = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Handle -> [Char] -> Int -> IO a
illegalBufferSize Handle
h [Char]
"hGet" Int
n
{-# INLINABLE hGetN #-}
hGetNonBlockingN :: MonadIO m => Int -> Handle -> Int -> ByteStream m ()
hGetNonBlockingN :: forall (m :: * -> *).
MonadIO m =>
Int -> Handle -> Int -> ByteStream m ()
hGetNonBlockingN Int
k Handle
h Int
n | Int
n forall a. Ord a => a -> a -> Bool
> Int
0 = Int -> ByteStream m ()
readChunks Int
n
where
readChunks :: Int -> ByteStream m ()
readChunks !Int
i = forall (m :: * -> *) r. m (ByteStream m r) -> ByteStream m r
Go forall a b. (a -> b) -> a -> b
$ do
ByteString
c <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Handle -> Int -> IO ByteString
B.hGetNonBlocking Handle
h (forall a. Ord a => a -> a -> a
min Int
k Int
i)
case ByteString -> Int
B.length ByteString
c of
Int
0 -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall (m :: * -> *) r. r -> ByteStream m r
Empty ())
Int
m -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
c (Int -> ByteStream m ()
readChunks (Int
i forall a. Num a => a -> a -> a
- Int
m)))
hGetNonBlockingN Int
_ Handle
_ Int
0 = forall (m :: * -> *) r. r -> ByteStream m r
Empty ()
hGetNonBlockingN Int
_ Handle
h Int
n = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Handle -> [Char] -> Int -> IO a
illegalBufferSize Handle
h [Char]
"hGetNonBlocking" Int
n
{-# INLINABLE hGetNonBlockingN #-}
illegalBufferSize :: Handle -> String -> Int -> IO a
illegalBufferSize :: forall a. Handle -> [Char] -> Int -> IO a
illegalBufferSize Handle
handle [Char]
fn Int
sz =
forall a. IOError -> IO a
ioError (IOErrorType -> [Char] -> Maybe Handle -> Maybe [Char] -> IOError
mkIOError IOErrorType
illegalOperationErrorType [Char]
msg (forall a. a -> Maybe a
Just Handle
handle) forall a. Maybe a
Nothing)
where
msg :: [Char]
msg = [Char]
fn forall a. [a] -> [a] -> [a]
++ [Char]
": illegal ByteStream size " forall a. [a] -> [a] -> [a]
++ forall a. Show a => Int -> a -> ShowS
showsPrec Int
9 Int
sz []
{-# INLINABLE illegalBufferSize #-}
hGetContents :: MonadIO m => Handle -> ByteStream m ()
hGetContents :: forall (m :: * -> *). MonadIO m => Handle -> ByteStream m ()
hGetContents = forall (m :: * -> *). MonadIO m => Int -> Handle -> ByteStream m ()
hGetContentsN Int
defaultChunkSize
{-# INLINE hGetContents #-}
fromHandle :: MonadIO m => Handle -> ByteStream m ()
fromHandle :: forall (m :: * -> *). MonadIO m => Handle -> ByteStream m ()
fromHandle = forall (m :: * -> *). MonadIO m => Handle -> ByteStream m ()
hGetContents
{-# INLINE fromHandle #-}
stdin :: MonadIO m => ByteStream m ()
stdin :: forall (m :: * -> *). MonadIO m => ByteStream m ()
stdin = forall (m :: * -> *). MonadIO m => Handle -> ByteStream m ()
hGetContents Handle
IO.stdin
{-# INLINE stdin #-}
hGet :: MonadIO m => Handle -> Int -> ByteStream m ()
hGet :: forall (m :: * -> *). MonadIO m => Handle -> Int -> ByteStream m ()
hGet = forall (m :: * -> *).
MonadIO m =>
Int -> Handle -> Int -> ByteStream m ()
hGetN Int
defaultChunkSize
{-# INLINE hGet #-}
hGetNonBlocking :: MonadIO m => Handle -> Int -> ByteStream m ()
hGetNonBlocking :: forall (m :: * -> *). MonadIO m => Handle -> Int -> ByteStream m ()
hGetNonBlocking = forall (m :: * -> *).
MonadIO m =>
Int -> Handle -> Int -> ByteStream m ()
hGetNonBlockingN Int
defaultChunkSize
{-# INLINE hGetNonBlocking #-}
writeFile :: MonadResource m => FilePath -> ByteStream m r -> m r
writeFile :: forall (m :: * -> *) r.
MonadResource m =>
[Char] -> ByteStream m r -> m r
writeFile [Char]
f ByteStream m r
str = do
(ReleaseKey
key, Handle
handle) <- forall (m :: * -> *) a.
MonadResource m =>
IO a -> (a -> IO ()) -> m (ReleaseKey, a)
allocate ([Char] -> IOMode -> IO Handle
openBinaryFile [Char]
f IOMode
WriteMode) Handle -> IO ()
hClose
r
r <- forall (m :: * -> *) r.
MonadIO m =>
Handle -> ByteStream m r -> m r
hPut Handle
handle ByteStream m r
str
forall (m :: * -> *). MonadIO m => ReleaseKey -> m ()
release ReleaseKey
key
forall (m :: * -> *) a. Monad m => a -> m a
return r
r
{-# INLINE writeFile #-}
readFile :: MonadResource m => FilePath -> ByteStream m ()
readFile :: forall (m :: * -> *). MonadResource m => [Char] -> ByteStream m ()
readFile [Char]
f = forall (m :: * -> *) a b.
MonadResource m =>
IO a -> (a -> IO ()) -> (a -> ByteStream m b) -> ByteStream m b
bracketByteString ([Char] -> IOMode -> IO Handle
openBinaryFile [Char]
f IOMode
ReadMode) Handle -> IO ()
hClose forall (m :: * -> *). MonadIO m => Handle -> ByteStream m ()
hGetContents
{-# INLINE readFile #-}
appendFile :: MonadResource m => FilePath -> ByteStream m r -> m r
appendFile :: forall (m :: * -> *) r.
MonadResource m =>
[Char] -> ByteStream m r -> m r
appendFile [Char]
f ByteStream m r
str = do
(ReleaseKey
key, Handle
handle) <- forall (m :: * -> *) a.
MonadResource m =>
IO a -> (a -> IO ()) -> m (ReleaseKey, a)
allocate ([Char] -> IOMode -> IO Handle
openBinaryFile [Char]
f IOMode
AppendMode) Handle -> IO ()
hClose
r
r <- forall (m :: * -> *) r.
MonadIO m =>
Handle -> ByteStream m r -> m r
hPut Handle
handle ByteStream m r
str
forall (m :: * -> *). MonadIO m => ReleaseKey -> m ()
release ReleaseKey
key
forall (m :: * -> *) a. Monad m => a -> m a
return r
r
{-# INLINE appendFile #-}
getContents :: MonadIO m => ByteStream m ()
getContents :: forall (m :: * -> *). MonadIO m => ByteStream m ()
getContents = forall (m :: * -> *). MonadIO m => Handle -> ByteStream m ()
hGetContents Handle
IO.stdin
{-# INLINE getContents #-}
hPut :: MonadIO m => Handle -> ByteStream m r -> m r
hPut :: forall (m :: * -> *) r.
MonadIO m =>
Handle -> ByteStream m r -> m r
hPut Handle
h ByteStream m r
cs = forall (m :: * -> *) r.
Monad m =>
ByteStream m r
-> forall x. (r -> x) -> (ByteString -> x -> x) -> (m x -> x) -> x
dematerialize ByteStream m r
cs forall (m :: * -> *) a. Monad m => a -> m a
return (\ByteString
x m r
y -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> ByteString -> IO ()
B.hPut Handle
h ByteString
x) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> m r
y) (forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. a -> a
id)
{-# INLINE hPut #-}
toHandle :: MonadIO m => Handle -> ByteStream m r -> m r
toHandle :: forall (m :: * -> *) r.
MonadIO m =>
Handle -> ByteStream m r -> m r
toHandle = forall (m :: * -> *) r.
MonadIO m =>
Handle -> ByteStream m r -> m r
hPut
{-# INLINE toHandle #-}
stdout :: MonadIO m => ByteStream m r -> m r
stdout :: forall (m :: * -> *) r. MonadIO m => ByteStream m r -> m r
stdout = forall (m :: * -> *) r.
MonadIO m =>
Handle -> ByteStream m r -> m r
hPut Handle
IO.stdout
{-# INLINE stdout #-}
interact :: (ByteStream IO () -> ByteStream IO r) -> IO r
interact :: forall r. (ByteStream IO () -> ByteStream IO r) -> IO r
interact ByteStream IO () -> ByteStream IO r
f = forall (m :: * -> *) r. MonadIO m => ByteStream m r -> m r
stdout (ByteStream IO () -> ByteStream IO r
f forall (m :: * -> *). MonadIO m => ByteStream m ()
stdin)
{-# INLINE interact #-}
revNonEmptyChunks :: [P.ByteString] -> ByteStream m r -> ByteStream m r
revNonEmptyChunks :: forall (m :: * -> *) r.
[ByteString] -> ByteStream m r -> ByteStream m r
revNonEmptyChunks = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
L.foldl' (\ByteStream m r -> ByteStream m r
f ByteString
bs -> forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
bs forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteStream m r -> ByteStream m r
f) forall a. a -> a
id
{-# INLINE revNonEmptyChunks #-}
revChunks :: Monad m => [P.ByteString] -> r -> ByteStream m r
revChunks :: forall (m :: * -> *) r.
Monad m =>
[ByteString] -> r -> ByteStream m r
revChunks [ByteString]
cs r
r = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
L.foldl' (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk) (forall (m :: * -> *) r. r -> ByteStream m r
Empty r
r) [ByteString]
cs
{-# INLINE revChunks #-}
zipWithStream
:: Monad m
=> (forall x . a -> ByteStream m x -> ByteStream m x)
-> [a]
-> Stream (ByteStream m) m r
-> Stream (ByteStream m) m r
zipWithStream :: forall (m :: * -> *) a r.
Monad m =>
(forall x. a -> ByteStream m x -> ByteStream m x)
-> [a] -> Stream (ByteStream m) m r -> Stream (ByteStream m) m r
zipWithStream forall x. a -> ByteStream m x -> ByteStream m x
op [a]
zs = [a] -> Stream (ByteStream m) m r -> Stream (ByteStream m) m r
loop [a]
zs
where
loop :: [a] -> Stream (ByteStream m) m r -> Stream (ByteStream m) m r
loop [] !Stream (ByteStream m) m r
ls = [a] -> Stream (ByteStream m) m r -> Stream (ByteStream m) m r
loop [a]
zs Stream (ByteStream m) m r
ls
loop a :: [a]
a@(a
x:[a]
xs) Stream (ByteStream m) m r
ls = case Stream (ByteStream m) m r
ls of
Return r
r -> forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return r
r
Step ByteStream m (Stream (ByteStream m) m r)
fls -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([a] -> Stream (ByteStream m) m r -> Stream (ByteStream m) m r
loop [a]
xs) (forall x. a -> ByteStream m x -> ByteStream m x
op a
x ByteStream m (Stream (ByteStream m) m r)
fls)
Effect m (Stream (ByteStream m) m r)
mls -> forall (f :: * -> *) (m :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([a] -> Stream (ByteStream m) m r -> Stream (ByteStream m) m r
loop [a]
a) m (Stream (ByteStream m) m r)
mls
{-# INLINABLE zipWithStream #-}
toStreamingByteString :: MonadIO m => Builder -> ByteStream m ()
toStreamingByteString :: forall (m :: * -> *). MonadIO m => Builder -> ByteStream m ()
toStreamingByteString = forall (m :: * -> *).
MonadIO m =>
AllocationStrategy -> Builder -> ByteStream m ()
toStreamingByteStringWith
(Int -> Int -> AllocationStrategy
safeStrategy Int
BI.smallChunkSize Int
BI.defaultChunkSize)
{-# INLINE toStreamingByteString #-}
toStreamingByteStringWith :: MonadIO m => AllocationStrategy -> Builder -> ByteStream m ()
toStreamingByteStringWith :: forall (m :: * -> *).
MonadIO m =>
AllocationStrategy -> Builder -> ByteStream m ()
toStreamingByteStringWith AllocationStrategy
strategy Builder
builder0 = do
ChunkIOStream ()
cios <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall a. AllocationStrategy -> BuildStep a -> IO (ChunkIOStream a)
buildStepToCIOS AllocationStrategy
strategy (Builder -> BuildStep ()
runBuilder Builder
builder0))
let loop :: ChunkIOStream r -> ByteStream m r
loop ChunkIOStream r
cios0 = case ChunkIOStream r
cios0 of
Yield1 ByteString
bs IO (ChunkIOStream r)
io -> forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
bs forall a b. (a -> b) -> a -> b
$ do
ChunkIOStream r
cios1 <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO (ChunkIOStream r)
io
ChunkIOStream r -> ByteStream m r
loop ChunkIOStream r
cios1
Finished Buffer
buf r
r -> forall {m :: * -> *} {r}.
Buffer -> ByteStream m r -> ByteStream m r
trimmedChunkFromBuffer Buffer
buf (forall (m :: * -> *) r. r -> ByteStream m r
Empty r
r)
trimmedChunkFromBuffer :: Buffer -> ByteStream m r -> ByteStream m r
trimmedChunkFromBuffer Buffer
buffer ByteStream m r
k
| ByteString -> Bool
B.null ByteString
bs = ByteStream m r
k
| Int
2 forall a. Num a => a -> a -> a
* ByteString -> Int
B.length ByteString
bs forall a. Ord a => a -> a -> Bool
< Buffer -> Int
bufferSize Buffer
buffer = forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk (ByteString -> ByteString
B.copy ByteString
bs) ByteStream m r
k
| Bool
otherwise = forall (m :: * -> *) r.
ByteString -> ByteStream m r -> ByteStream m r
Chunk ByteString
bs ByteStream m r
k
where
bs :: ByteString
bs = Buffer -> ByteString
byteStringFromBuffer Buffer
buffer
forall {m :: * -> *} {r}.
MonadIO m =>
ChunkIOStream r -> ByteStream m r
loop ChunkIOStream ()
cios
{-# INLINABLE toStreamingByteStringWith #-}
{-# SPECIALIZE toStreamingByteStringWith :: AllocationStrategy -> Builder -> ByteStream IO () #-}
concatBuilders :: Stream (Of Builder) IO () -> Builder
concatBuilders :: Stream (Of Builder) IO () -> Builder
concatBuilders Stream (Of Builder) IO ()
p = (forall r. BuildStep r -> BuildStep r) -> Builder
builder forall a b. (a -> b) -> a -> b
$ \BuildStep r
bstep BufferRange
r -> do
case Stream (Of Builder) IO ()
p of
Return ()
_ -> forall a. Builder -> BuildStep a -> BuildStep a
runBuilderWith forall a. Monoid a => a
mempty BuildStep r
bstep BufferRange
r
Step (Builder
b :> Stream (Of Builder) IO ()
rest) -> forall a. Builder -> BuildStep a -> BuildStep a
runBuilderWith (Builder
b forall a. Monoid a => a -> a -> a
`mappend` Stream (Of Builder) IO () -> Builder
concatBuilders Stream (Of Builder) IO ()
rest) BuildStep r
bstep BufferRange
r
Effect IO (Stream (Of Builder) IO ())
m -> IO (Stream (Of Builder) IO ())
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Stream (Of Builder) IO ()
p' -> forall a. Builder -> BuildStep a -> BuildStep a
runBuilderWith (Stream (Of Builder) IO () -> Builder
concatBuilders Stream (Of Builder) IO ()
p') BuildStep r
bstep BufferRange
r
{-# INLINABLE concatBuilders #-}
toBuilder :: ByteStream IO () -> Builder
toBuilder :: ByteStream IO () -> Builder
toBuilder = Stream (Of Builder) IO () -> Builder
concatBuilders forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a b r.
Monad m =>
(a -> b) -> Stream (Of a) m r -> Stream (Of b) m r
SP.map ByteString -> Builder
byteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> Stream (Of ByteString) m r
toChunks
{-# INLINABLE toBuilder #-}