2 {-# LANGUAGE RankNTypes, BangPatterns #-}
4 -- Copyright: 2014 Michael Thompson
6 -- This module uses the stream decoding functions from the text-stream-decoding package
7 -- to define pipes decoding functions and lenses.
9 module Pipes.Text.Encoding
31 import Data.Char (ord)
32 import Data.ByteString as B
33 import Data.ByteString (ByteString)
34 import Data.ByteString.Char8 as B8
35 import Data.Text (Text)
36 import qualified Data.Text as T
37 import qualified Data.Text.Encoding as TE
38 import Data.Text.StreamDecoding
39 import Control.Monad (join)
44 {- | A 'Codec' is just an improper lens into a byte stream that is expected to contain text.
45 They are named in accordance with the expected encoding, 'utf8', 'utf16LE' etc.
46 The stream of text they 'see' in a bytestream ends by returning the original byte stream
47 beginning at the point of failure, or the empty bytestream with its return value.
49 type Codec = forall f m r . (Functor f , Monad m ) =>
50 (Producer Text m (Producer ByteString m r) -> f (Producer Text m (Producer ByteString m r)))
51 -> Producer ByteString m r -> f (Producer ByteString m r )
53 decodeStream :: Monad m
54 => (B.ByteString -> DecodeResult)
55 -> Producer ByteString m r -> Producer Text m (Producer ByteString m r)
56 decodeStream = loop where
59 case x of Left r -> return (return r)
60 Right (chunk, p') -> case dec0 chunk of
61 DecodeResultSuccess text dec -> do yield text
63 DecodeResultFailure text bs -> do yield text
66 {-# INLINABLE decodeStream#-}
68 decodeUtf8 :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
69 decodeUtf8 = decodeStream streamUtf8
70 {-# INLINE decodeUtf8 #-}
72 decodeUtf8Pure :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
73 decodeUtf8Pure = decodeStream streamUtf8Pure
74 {-# INLINE decodeUtf8Pure #-}
76 decodeUtf16LE :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
77 decodeUtf16LE = decodeStream streamUtf16LE
78 {-# INLINE decodeUtf16LE #-}
80 decodeUtf16BE :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
81 decodeUtf16BE = decodeStream streamUtf16BE
82 {-# INLINE decodeUtf16BE #-}
84 decodeUtf32LE :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
85 decodeUtf32LE = decodeStream streamUtf32LE
86 {-# INLINE decodeUtf32LE #-}
88 decodeUtf32BE :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
89 decodeUtf32BE = decodeStream streamUtf32BE
90 {-# INLINE decodeUtf32BE #-}
92 mkCodec :: (forall r m . Monad m =>
93 Producer ByteString m r -> Producer Text m (Producer ByteString m r ))
94 -> (Text -> ByteString)
96 mkCodec dec enc = \k p0 -> fmap (\p -> join (for p (yield . enc))) (k (dec p0))
99 {- | An improper lens into a byte stream expected to be UTF-8 encoded; the associated
100 text stream ends by returning the original bytestream beginning at the point of failure,
101 or the empty bytestring for a well-encoded text.
105 utf8 = mkCodec decodeUtf8 TE.encodeUtf8
108 utf8Pure = mkCodec decodeUtf8Pure TE.encodeUtf8
111 utf16LE = mkCodec decodeUtf16LE TE.encodeUtf16LE
114 utf16BE = mkCodec decodeUtf16BE TE.encodeUtf16BE
117 utf32LE = mkCodec decodeUtf32LE TE.encodeUtf32LE
120 utf32BE = mkCodec decodeUtf32BE TE.encodeUtf32BE
123 {- | ascii and latin encodings only use a small number of the characters 'Text'
124 recognizes; thus we cannot use the pipes 'Lens' style to work with them.
125 Rather we simply define functions each way.
127 'encodeAscii' : Reduce as much of your stream of 'Text' actually is ascii to a byte stream,
128 returning the rest of the 'Text' at the first non-ascii 'Char'
131 encodeAscii :: Monad m => Producer Text m r -> Producer ByteString m (Producer Text m r)
132 encodeAscii = go where
133 go p = do e <- lift (next p)
135 Left r -> return (return r)
139 else let (safe, unsafe) = T.span (\c -> ord c <= 0x7F) chunk
140 in do yield (B8.pack (T.unpack safe))
143 else return $ do yield unsafe
146 {- | Reduce as much of your stream of 'Text' actually is iso8859 or latin1 to a byte stream,
147 returning the rest of the 'Text' upon hitting any non-latin 'Char'
149 encodeIso8859_1 :: Monad m => Producer Text m r -> Producer ByteString m (Producer Text m r)
150 encodeIso8859_1 = go where
151 go p = do e <- lift (next p)
153 Left r -> return (return r)
157 else let (safe, unsafe) = T.span (\c -> ord c <= 0xFF) txt
158 in do yield (B8.pack (T.unpack safe))
161 else return $ do yield unsafe
164 {- | Reduce a byte stream to a corresponding stream of ascii chars, returning the
165 unused 'ByteString' upon hitting an un-ascii byte.
167 decodeAscii :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
168 decodeAscii = go where
169 go p = do e <- lift (next p)
171 Left r -> return (return r)
175 else let (safe, unsafe) = B.span (<= 0x7F) chunk
176 in do yield (T.pack (B8.unpack safe))
179 else return (do yield unsafe
182 {- | Reduce a byte stream to a corresponding stream of ascii chars, returning the
183 unused 'ByteString' upon hitting the rare un-latinizable byte.
185 decodeIso8859_1 :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
186 decodeIso8859_1 = go where
187 go p = do e <- lift (next p)
189 Left r -> return (return r)
193 else do let (safe, unsafe) = B.span (<= 0xFF) chunk
194 yield (T.pack (B8.unpack safe))
197 else return (do yield unsafe