{-# LANGUAGE RankNTypes #-}
module Pipes.Text.Encoding
(
Codec,
decode,
eof,
utf8,
utf8Pure,
utf16LE,
utf16BE,
utf32LE,
utf32BE,
decodeUtf8,
decodeUtf8Pure,
decodeUtf16LE,
decodeUtf16BE,
decodeUtf32LE,
decodeUtf32BE,
encodeUtf8,
encodeUtf16LE,
encodeUtf16BE,
encodeUtf32LE,
encodeUtf32BE,
encodeAscii,
decodeAscii,
encodeIso8859_1,
decodeIso8859_1,
)
where
import Control.Monad (join)
import Data.ByteString as B
import Data.ByteString.Char8 as B8
import Data.Char (ord)
import Data.Functor.Constant (Constant (..))
import Data.Streaming.Text (DecodeResult (..))
import qualified Data.Streaming.Text as Stream
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import Pipes
type Lens' a b = forall f. Functor f => (b -> f b) -> (a -> f a)
type Codec =
forall m r.
Monad m =>
Lens'
(Producer ByteString m r)
(Producer Text m (Producer ByteString m r))
decode :: ((b -> Constant b b) -> (a -> Constant b a)) -> a -> b
decode :: ((b -> Constant b b) -> a -> Constant b a) -> a -> b
decode (b -> Constant b b) -> a -> Constant b a
codec a
a = Constant b a -> b
forall a k (b :: k). Constant a b -> a
getConstant ((b -> Constant b b) -> a -> Constant b a
codec b -> Constant b b
forall k a (b :: k). a -> Constant a b
Constant a
a)
eof ::
(Monad m, Monad (t m), MonadTrans t) =>
Lens'
(t m (Producer ByteString m r))
(t m (Either (Producer ByteString m r) r))
eof :: Lens'
(t m (Producer ByteString m r))
(t m (Either (Producer ByteString m r) r))
eof t m (Either (Producer ByteString m r) r)
-> f (t m (Either (Producer ByteString m r) r))
k t m (Producer ByteString m r)
p0 = (t m (Either (Producer ByteString m r) r)
-> t m (Producer ByteString m r))
-> f (t m (Either (Producer ByteString m r) r))
-> f (t m (Producer ByteString m r))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap t m (Either (Producer ByteString m r) r)
-> t m (Producer ByteString m r)
forall a.
t m (Either (Proxy X () () ByteString m a) a)
-> t m (Proxy X () () ByteString m a)
fromEither (t m (Either (Producer ByteString m r) r)
-> f (t m (Either (Producer ByteString m r) r))
k (t m (Producer ByteString m r)
-> t m (Either (Producer ByteString m r) r)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) b.
(MonadTrans t, Monad m, Monad (t m)) =>
t m (Producer ByteString m b)
-> t m (Either (Producer ByteString m b) b)
toEither t m (Producer ByteString m r)
p0))
where
fromEither :: t m (Either (Proxy X () () ByteString m a) a)
-> t m (Proxy X () () ByteString m a)
fromEither = (Either (Proxy X () () ByteString m a) a
-> Proxy X () () ByteString m a)
-> t m (Either (Proxy X () () ByteString m a) a)
-> t m (Proxy X () () ByteString m a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Proxy X () () ByteString m a -> Proxy X () () ByteString m a)
-> (a -> Proxy X () () ByteString m a)
-> Either (Proxy X () () ByteString m a) a
-> Proxy X () () ByteString m a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Proxy X () () ByteString m a -> Proxy X () () ByteString m a
forall a. a -> a
id a -> Proxy X () () ByteString m a
forall (m :: * -> *) a. Monad m => a -> m a
return)
toEither :: t m (Producer ByteString m b)
-> t m (Either (Producer ByteString m b) b)
toEither t m (Producer ByteString m b)
pp = do
Producer ByteString m b
p <- t m (Producer ByteString m b)
pp
Producer ByteString m b -> t m (Either (Producer ByteString m b) b)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) b.
(MonadTrans t, Monad m, Monad (t m)) =>
Producer ByteString m b -> t m (Either (Producer ByteString m b) b)
check Producer ByteString m b
p
check :: Producer ByteString m b -> t m (Either (Producer ByteString m b) b)
check Producer ByteString m b
p = do
Either b (ByteString, Producer ByteString m b)
e <- m (Either b (ByteString, Producer ByteString m b))
-> t 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)
e of
Left b
r -> Either (Producer ByteString m b) b
-> t m (Either (Producer ByteString m b) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> Either (Producer ByteString m b) b
forall a b. b -> Either a b
Right b
r)
Right (ByteString
bs, Producer ByteString m b
pb) ->
if ByteString -> Bool
B.null ByteString
bs
then Producer ByteString m b -> t m (Either (Producer ByteString m b) b)
check Producer ByteString m b
pb
else
Either (Producer ByteString m b) b
-> t m (Either (Producer ByteString m b) b)
forall (m :: * -> *) a. Monad m => a -> m a
return
( Producer ByteString m b -> Either (Producer ByteString m b) b
forall a b. a -> Either a b
Left
( do
ByteString -> Producer' ByteString m ()
forall (m :: * -> *) a. Functor m => a -> Producer' a m ()
yield ByteString
bs
Producer ByteString m b
pb
)
)
utf8 :: Codec
utf8 :: Lens'
(Producer ByteString m r)
(Producer Text m (Producer ByteString m r))
utf8 = (forall r (m :: * -> *).
Monad m =>
Producer ByteString m r
-> Producer Text m (Producer ByteString m r))
-> (Text -> ByteString) -> Codec
mkCodec forall r (m :: * -> *).
Monad m =>
Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
forall (m :: * -> *) r.
Monad m =>
Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
decodeUtf8 Text -> ByteString
TE.encodeUtf8
utf8Pure :: Codec
utf8Pure :: Lens'
(Producer ByteString m r)
(Producer Text m (Producer ByteString m r))
utf8Pure = (forall r (m :: * -> *).
Monad m =>
Producer ByteString m r
-> Producer Text m (Producer ByteString m r))
-> (Text -> ByteString) -> Codec
mkCodec forall r (m :: * -> *).
Monad m =>
Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
forall (m :: * -> *) r.
Monad m =>
Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
decodeUtf8Pure Text -> ByteString
TE.encodeUtf8
utf16LE :: Codec
utf16LE :: Lens'
(Producer ByteString m r)
(Producer Text m (Producer ByteString m r))
utf16LE = (forall r (m :: * -> *).
Monad m =>
Producer ByteString m r
-> Producer Text m (Producer ByteString m r))
-> (Text -> ByteString) -> Codec
mkCodec forall r (m :: * -> *).
Monad m =>
Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
forall (m :: * -> *) r.
Monad m =>
Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
decodeUtf16LE Text -> ByteString
TE.encodeUtf16LE
utf16BE :: Codec
utf16BE :: Lens'
(Producer ByteString m r)
(Producer Text m (Producer ByteString m r))
utf16BE = (forall r (m :: * -> *).
Monad m =>
Producer ByteString m r
-> Producer Text m (Producer ByteString m r))
-> (Text -> ByteString) -> Codec
mkCodec forall r (m :: * -> *).
Monad m =>
Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
forall (m :: * -> *) r.
Monad m =>
Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
decodeUtf16BE Text -> ByteString
TE.encodeUtf16BE
utf32LE :: Codec
utf32LE :: Lens'
(Producer ByteString m r)
(Producer Text m (Producer ByteString m r))
utf32LE = (forall r (m :: * -> *).
Monad m =>
Producer ByteString m r
-> Producer Text m (Producer ByteString m r))
-> (Text -> ByteString) -> Codec
mkCodec forall r (m :: * -> *).
Monad m =>
Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
forall (m :: * -> *) r.
Monad m =>
Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
decodeUtf32LE Text -> ByteString
TE.encodeUtf32LE
utf32BE :: Codec
utf32BE :: Lens'
(Producer ByteString m r)
(Producer Text m (Producer ByteString m r))
utf32BE = (forall r (m :: * -> *).
Monad m =>
Producer ByteString m r
-> Producer Text m (Producer ByteString m r))
-> (Text -> ByteString) -> Codec
mkCodec forall r (m :: * -> *).
Monad m =>
Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
forall (m :: * -> *) r.
Monad m =>
Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
decodeUtf32BE Text -> ByteString
TE.encodeUtf32BE
decodeStream ::
Monad m =>
(B.ByteString -> DecodeResult) ->
Producer ByteString m r ->
Producer Text m (Producer ByteString m r)
decodeStream :: (ByteString -> DecodeResult)
-> Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
decodeStream = (ByteString -> DecodeResult)
-> Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
forall (m :: * -> *) b x' x.
Monad m =>
(ByteString -> DecodeResult)
-> Producer ByteString m b
-> Proxy x' x () Text m (Producer ByteString m b)
loop
where
loop :: (ByteString -> DecodeResult)
-> Producer ByteString m b
-> Proxy x' x () Text m (Producer ByteString m b)
loop ByteString -> DecodeResult
dec0 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 () Text 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 () Text 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
chunk, Producer ByteString m b
p') -> case ByteString -> DecodeResult
dec0 ByteString
chunk of
DecodeResultSuccess Text
text ByteString -> DecodeResult
dec -> do
Text -> Producer' Text m ()
forall (m :: * -> *) a. Functor m => a -> Producer' a m ()
yield Text
text
(ByteString -> DecodeResult)
-> Producer ByteString m b
-> Proxy x' x () Text m (Producer ByteString m b)
loop ByteString -> DecodeResult
dec Producer ByteString m b
p'
DecodeResultFailure Text
text ByteString
bs -> do
Text -> Producer' Text m ()
forall (m :: * -> *) a. Functor m => a -> Producer' a m ()
yield Text
text
Producer ByteString m b
-> Proxy x' x () Text m (Producer ByteString m b)
forall (m :: * -> *) a. Monad m => a -> m a
return
( do
ByteString -> Producer' ByteString m ()
forall (m :: * -> *) a. Functor m => a -> Producer' a m ()
yield ByteString
bs
Producer ByteString m b
p'
)
{-# INLINEABLE decodeStream #-}
decodeUtf8 :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
decodeUtf8 :: Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
decodeUtf8 = (ByteString -> DecodeResult)
-> Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
forall (m :: * -> *) r.
Monad m =>
(ByteString -> DecodeResult)
-> Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
decodeStream ByteString -> DecodeResult
Stream.decodeUtf8
{-# INLINE decodeUtf8 #-}
decodeUtf8Pure :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
decodeUtf8Pure :: Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
decodeUtf8Pure = (ByteString -> DecodeResult)
-> Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
forall (m :: * -> *) r.
Monad m =>
(ByteString -> DecodeResult)
-> Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
decodeStream ByteString -> DecodeResult
Stream.decodeUtf8Pure
{-# INLINE decodeUtf8Pure #-}
decodeUtf16LE :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
decodeUtf16LE :: Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
decodeUtf16LE = (ByteString -> DecodeResult)
-> Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
forall (m :: * -> *) r.
Monad m =>
(ByteString -> DecodeResult)
-> Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
decodeStream ByteString -> DecodeResult
Stream.decodeUtf16LE
{-# INLINE decodeUtf16LE #-}
decodeUtf16BE :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
decodeUtf16BE :: Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
decodeUtf16BE = (ByteString -> DecodeResult)
-> Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
forall (m :: * -> *) r.
Monad m =>
(ByteString -> DecodeResult)
-> Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
decodeStream ByteString -> DecodeResult
Stream.decodeUtf16BE
{-# INLINE decodeUtf16BE #-}
decodeUtf32LE :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
decodeUtf32LE :: Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
decodeUtf32LE = (ByteString -> DecodeResult)
-> Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
forall (m :: * -> *) r.
Monad m =>
(ByteString -> DecodeResult)
-> Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
decodeStream ByteString -> DecodeResult
Stream.decodeUtf32LE
{-# INLINE decodeUtf32LE #-}
decodeUtf32BE :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
decodeUtf32BE :: Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
decodeUtf32BE = (ByteString -> DecodeResult)
-> Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
forall (m :: * -> *) r.
Monad m =>
(ByteString -> DecodeResult)
-> Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
decodeStream ByteString -> DecodeResult
Stream.decodeUtf32BE
{-# INLINE decodeUtf32BE #-}
encodeUtf8 :: Monad m => Text -> Producer' ByteString m ()
encodeUtf8 :: Text -> Producer' ByteString m ()
encodeUtf8 = ByteString -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a. Functor m => a -> Producer' a m ()
yield (ByteString -> Proxy x' x () ByteString m ())
-> (Text -> ByteString) -> Text -> Proxy x' x () ByteString m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
TE.encodeUtf8
encodeUtf16LE :: Monad m => Text -> Producer' ByteString m ()
encodeUtf16LE :: Text -> Producer' ByteString m ()
encodeUtf16LE = ByteString -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a. Functor m => a -> Producer' a m ()
yield (ByteString -> Proxy x' x () ByteString m ())
-> (Text -> ByteString) -> Text -> Proxy x' x () ByteString m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
TE.encodeUtf16LE
encodeUtf16BE :: Monad m => Text -> Producer' ByteString m ()
encodeUtf16BE :: Text -> Producer' ByteString m ()
encodeUtf16BE = ByteString -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a. Functor m => a -> Producer' a m ()
yield (ByteString -> Proxy x' x () ByteString m ())
-> (Text -> ByteString) -> Text -> Proxy x' x () ByteString m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
TE.encodeUtf16BE
encodeUtf32LE :: Monad m => Text -> Producer' ByteString m ()
encodeUtf32LE :: Text -> Producer' ByteString m ()
encodeUtf32LE = ByteString -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a. Functor m => a -> Producer' a m ()
yield (ByteString -> Proxy x' x () ByteString m ())
-> (Text -> ByteString) -> Text -> Proxy x' x () ByteString m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
TE.encodeUtf32LE
encodeUtf32BE :: Monad m => Text -> Producer' ByteString m ()
encodeUtf32BE :: Text -> Producer' ByteString m ()
encodeUtf32BE = ByteString -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a. Functor m => a -> Producer' a m ()
yield (ByteString -> Proxy x' x () ByteString m ())
-> (Text -> ByteString) -> Text -> Proxy x' x () ByteString m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
TE.encodeUtf32BE
mkCodec ::
( forall r m.
Monad m =>
Producer ByteString m r ->
Producer Text m (Producer ByteString m r)
) ->
(Text -> ByteString) ->
Codec
mkCodec :: (forall r (m :: * -> *).
Monad m =>
Producer ByteString m r
-> Producer Text m (Producer ByteString m r))
-> (Text -> ByteString) -> Codec
mkCodec forall r (m :: * -> *).
Monad m =>
Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
dec Text -> ByteString
enc Producer Text m (Producer ByteString m r)
-> f (Producer Text m (Producer ByteString m r))
k Producer ByteString m r
p0 = (Producer Text m (Producer ByteString m r)
-> Producer ByteString m r)
-> f (Producer Text m (Producer ByteString m r))
-> f (Producer ByteString m r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Producer Text m (Producer ByteString m r)
p -> Proxy X () () ByteString m (Producer ByteString m r)
-> Producer ByteString m r
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Producer Text m (Producer ByteString m r)
-> (Text -> Proxy X () () ByteString m ())
-> Proxy X () () ByteString 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 Text m (Producer ByteString m r)
p (ByteString -> Proxy X () () ByteString m ()
forall (m :: * -> *) a. Functor m => a -> Producer' a m ()
yield (ByteString -> Proxy X () () ByteString m ())
-> (Text -> ByteString) -> Text -> Proxy X () () ByteString m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
enc))) (Producer Text m (Producer ByteString m r)
-> f (Producer Text m (Producer ByteString m r))
k (Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
forall r (m :: * -> *).
Monad m =>
Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
dec Producer ByteString m r
p0))
encodeAscii :: Monad m => Producer Text m r -> Producer ByteString m (Producer Text m r)
encodeAscii :: Producer Text m r -> Producer ByteString m (Producer Text m r)
encodeAscii = Producer Text m r -> Producer ByteString m (Producer Text m r)
forall (m :: * -> *) b x' x.
Monad m =>
Producer Text m b -> Proxy x' x () ByteString m (Producer Text m b)
go
where
go :: Producer Text m b -> Proxy x' x () ByteString m (Producer Text m b)
go Producer Text m b
p = do
Either b (Text, Producer Text m b)
e <- m (Either b (Text, Producer Text m b))
-> Proxy x' x () ByteString m (Either b (Text, Producer Text m b))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Producer Text m b -> m (Either b (Text, Producer Text m b))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next Producer Text m b
p)
case Either b (Text, Producer Text m b)
e of
Left b
r -> Producer Text m b -> Proxy x' x () ByteString m (Producer Text m b)
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> Producer Text m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
r)
Right (Text
chunk, Producer Text m b
p') ->
if Text -> Bool
T.null Text
chunk
then Producer Text m b -> Proxy x' x () ByteString m (Producer Text m b)
go Producer Text m b
p'
else
let (Text
safe, Text
unsafe) = (Char -> Bool) -> Text -> (Text, Text)
T.span (\Char
c -> Char -> Int
ord Char
c Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0x7F) Text
chunk
in do
ByteString -> Producer' ByteString m ()
forall (m :: * -> *) a. Functor m => a -> Producer' a m ()
yield (String -> ByteString
B8.pack (Text -> String
T.unpack Text
safe))
if Text -> Bool
T.null Text
unsafe
then Producer Text m b -> Proxy x' x () ByteString m (Producer Text m b)
go Producer Text m b
p'
else Producer Text m b -> Proxy x' x () ByteString m (Producer Text m b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Producer Text m b
-> Proxy x' x () ByteString m (Producer Text m b))
-> Producer Text m b
-> Proxy x' x () ByteString m (Producer Text m b)
forall a b. (a -> b) -> a -> b
$ do
Text -> Producer' Text m ()
forall (m :: * -> *) a. Functor m => a -> Producer' a m ()
yield Text
unsafe
Producer Text m b
p'
encodeIso8859_1 :: Monad m => Producer Text m r -> Producer ByteString m (Producer Text m r)
encodeIso8859_1 :: Producer Text m r -> Producer ByteString m (Producer Text m r)
encodeIso8859_1 = Producer Text m r -> Producer ByteString m (Producer Text m r)
forall (m :: * -> *) b x' x.
Monad m =>
Producer Text m b -> Proxy x' x () ByteString m (Producer Text m b)
go
where
go :: Producer Text m b -> Proxy x' x () ByteString m (Producer Text m b)
go Producer Text m b
p = do
Either b (Text, Producer Text m b)
e <- m (Either b (Text, Producer Text m b))
-> Proxy x' x () ByteString m (Either b (Text, Producer Text m b))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Producer Text m b -> m (Either b (Text, Producer Text m b))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next Producer Text m b
p)
case Either b (Text, Producer Text m b)
e of
Left b
r -> Producer Text m b -> Proxy x' x () ByteString m (Producer Text m b)
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> Producer Text m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
r)
Right (Text
txt, Producer Text m b
p') ->
if Text -> Bool
T.null Text
txt
then Producer Text m b -> Proxy x' x () ByteString m (Producer Text m b)
go Producer Text m b
p'
else
let (Text
safe, Text
unsafe) = (Char -> Bool) -> Text -> (Text, Text)
T.span (\Char
c -> Char -> Int
ord Char
c Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0xFF) Text
txt
in do
ByteString -> Producer' ByteString m ()
forall (m :: * -> *) a. Functor m => a -> Producer' a m ()
yield (String -> ByteString
B8.pack (Text -> String
T.unpack Text
safe))
if Text -> Bool
T.null Text
unsafe
then Producer Text m b -> Proxy x' x () ByteString m (Producer Text m b)
go Producer Text m b
p'
else Producer Text m b -> Proxy x' x () ByteString m (Producer Text m b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Producer Text m b
-> Proxy x' x () ByteString m (Producer Text m b))
-> Producer Text m b
-> Proxy x' x () ByteString m (Producer Text m b)
forall a b. (a -> b) -> a -> b
$ do
Text -> Producer' Text m ()
forall (m :: * -> *) a. Functor m => a -> Producer' a m ()
yield Text
unsafe
Producer Text m b
p'
decodeAscii :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
decodeAscii :: Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
decodeAscii = Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
forall (m :: * -> *) b x' x.
Monad m =>
Producer ByteString m b
-> Proxy x' x () Text m (Producer ByteString m b)
go
where
go :: Producer ByteString m b
-> Proxy x' x () Text m (Producer ByteString m b)
go Producer ByteString m b
p = do
Either b (ByteString, Producer ByteString m b)
e <- m (Either b (ByteString, Producer ByteString m b))
-> Proxy
x' x () Text 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)
e of
Left b
r -> Producer ByteString m b
-> Proxy x' x () Text 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
chunk, Producer ByteString m b
p') ->
if ByteString -> Bool
B.null ByteString
chunk
then Producer ByteString m b
-> Proxy x' x () Text m (Producer ByteString m b)
go Producer ByteString m b
p'
else
let (ByteString
safe, ByteString
unsafe) = (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
B.span (Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word8
0x7F) ByteString
chunk
in do
Text -> Producer' Text m ()
forall (m :: * -> *) a. Functor m => a -> Producer' a m ()
yield (String -> Text
T.pack (ByteString -> String
B8.unpack ByteString
safe))
if ByteString -> Bool
B.null ByteString
unsafe
then Producer ByteString m b
-> Proxy x' x () Text m (Producer ByteString m b)
go Producer ByteString m b
p'
else
Producer ByteString m b
-> Proxy x' x () Text m (Producer ByteString m b)
forall (m :: * -> *) a. Monad m => a -> m a
return
( do
ByteString -> Producer' ByteString m ()
forall (m :: * -> *) a. Functor m => a -> Producer' a m ()
yield ByteString
unsafe
Producer ByteString m b
p'
)
decodeIso8859_1 :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
decodeIso8859_1 :: Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
decodeIso8859_1 = Producer ByteString m r
-> Producer Text m (Producer ByteString m r)
forall (m :: * -> *) b x' x.
Monad m =>
Producer ByteString m b
-> Proxy x' x () Text m (Producer ByteString m b)
go
where
go :: Producer ByteString m b
-> Proxy x' x () Text m (Producer ByteString m b)
go Producer ByteString m b
p = do
Either b (ByteString, Producer ByteString m b)
e <- m (Either b (ByteString, Producer ByteString m b))
-> Proxy
x' x () Text 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)
e of
Left b
r -> Producer ByteString m b
-> Proxy x' x () Text 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
chunk, Producer ByteString m b
p') ->
if ByteString -> Bool
B.null ByteString
chunk
then Producer ByteString m b
-> Proxy x' x () Text m (Producer ByteString m b)
go Producer ByteString m b
p'
else do
let (ByteString
safe, ByteString
unsafe) = (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
B.span (Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word8
0xFF) ByteString
chunk
Text -> Producer' Text m ()
forall (m :: * -> *) a. Functor m => a -> Producer' a m ()
yield (String -> Text
T.pack (ByteString -> String
B8.unpack ByteString
safe))
if ByteString -> Bool
B.null ByteString
unsafe
then Producer ByteString m b
-> Proxy x' x () Text m (Producer ByteString m b)
go Producer ByteString m b
p'
else
Producer ByteString m b
-> Proxy x' x () Text m (Producer ByteString m b)
forall (m :: * -> *) a. Monad m => a -> m a
return
( do
ByteString -> Producer' ByteString m ()
forall (m :: * -> *) a. Functor m => a -> Producer' a m ()
yield ByteString
unsafe
Producer ByteString m b
p'
)