2 {-# LANGUAGE RankNTypes, BangPatterns #-}
5 -- This module uses the stream decoding functions from the text-stream-decoding package
6 -- to define pipes decoding functions and lenses.
8 module Pipes.Text.Encoding
29 import Data.Char (ord)
30 import Data.ByteString as B
31 import Data.ByteString (ByteString)
32 import Data.ByteString.Char8 as B8
33 import Data.Text (Text)
34 import qualified Data.Text as T
35 import qualified Data.Text.Encoding as TE
36 import Data.Text.StreamDecoding
37 import Control.Monad (join)
38 import Data.Word (Word8)
41 type Lens' a b = forall f . Functor f => (b -> f b) -> (a -> f a)
43 {- | A 'Codec' is just an improper lens into a byte stream that is expected to contain text.
44 They are named in accordance with the expected encoding, 'utf8', 'utf16LE' etc.
45 The stream of text they 'see' in a bytestream ends by returning the original byte stream
46 beginning at the point of failure, or the empty bytestream with its return value.
51 => Lens' (Producer ByteString m r)
52 (Producer Text m (Producer ByteString m r))
54 decodeStream :: Monad m
55 => (B.ByteString -> DecodeResult)
56 -> Producer ByteString m r -> Producer Text m (Producer ByteString m r)
57 decodeStream = loop where
60 case x of Left r -> return (return r)
61 Right (chunk, p') -> case dec0 chunk of
62 DecodeResultSuccess text dec -> do yield text
64 DecodeResultFailure text bs -> do yield text
67 {-# INLINABLE decodeStream#-}
69 decodeUtf8 :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
70 decodeUtf8 = decodeStream streamUtf8
71 {-# INLINE decodeUtf8 #-}
73 decodeUtf8Pure :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
74 decodeUtf8Pure = decodeStream streamUtf8Pure
75 {-# INLINE decodeUtf8Pure #-}
77 decodeUtf16LE :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
78 decodeUtf16LE = decodeStream streamUtf16LE
79 {-# INLINE decodeUtf16LE #-}
81 decodeUtf16BE :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
82 decodeUtf16BE = decodeStream streamUtf16BE
83 {-# INLINE decodeUtf16BE #-}
85 decodeUtf32LE :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
86 decodeUtf32LE = decodeStream streamUtf32LE
87 {-# INLINE decodeUtf32LE #-}
89 decodeUtf32BE :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
90 decodeUtf32BE = decodeStream streamUtf32BE
91 {-# INLINE decodeUtf32BE #-}
93 mkCodec :: (forall r m . Monad m =>
94 Producer ByteString m r -> Producer Text m (Producer ByteString m r ))
95 -> (Text -> ByteString)
97 mkCodec dec enc = \k p0 -> fmap (\p -> join (for p (yield . enc))) (k (dec p0))
100 {- | An improper lens into a byte stream expected to be UTF-8 encoded; the associated
101 text stream ends by returning the original bytestream beginning at the point of failure,
102 or the empty bytestring for a well-encoded text.
106 utf8 = mkCodec decodeUtf8 TE.encodeUtf8
109 utf8Pure = mkCodec decodeUtf8Pure TE.encodeUtf8
112 utf16LE = mkCodec decodeUtf16LE TE.encodeUtf16LE
115 utf16BE = mkCodec decodeUtf16BE TE.encodeUtf16BE
118 utf32LE = mkCodec decodeUtf32LE TE.encodeUtf32LE
121 utf32BE = mkCodec decodeUtf32BE TE.encodeUtf32BE
124 {- | ascii and latin encodings only use a small number of the characters 'Text'
125 recognizes; thus we cannot use the pipes 'Lens' style to work with them.
126 Rather we simply define functions each way.
128 'encodeAscii' : Reduce as much of your stream of 'Text' actually is ascii to a byte stream,
129 returning the rest of the 'Text' at the first non-ascii 'Char'
132 encodeAscii :: Monad m => Producer Text m r -> Producer ByteString m (Producer Text m r)
133 encodeAscii = go where
134 go p = do e <- lift (next p)
136 Left r -> return (return r)
140 else let (safe, unsafe) = T.span (\c -> ord c <= 0x7F) chunk
141 in do yield (B8.pack (T.unpack safe))
144 else return $ do yield unsafe
147 {- | Reduce as much of your stream of 'Text' actually is iso8859 or latin1 to a byte stream,
148 returning the rest of the 'Text' upon hitting any non-latin 'Char'
150 encodeIso8859_1 :: Monad m => Producer Text m r -> Producer ByteString m (Producer Text m r)
151 encodeIso8859_1 = go where
152 go p = do e <- lift (next p)
154 Left r -> return (return r)
158 else let (safe, unsafe) = T.span (\c -> ord c <= 0xFF) txt
159 in do yield (B8.pack (T.unpack safe))
162 else return $ do yield unsafe
165 {- | Reduce a byte stream to a corresponding stream of ascii chars, returning the
166 unused 'ByteString' upon hitting an un-ascii byte.
168 decodeAscii :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
169 decodeAscii = go where
170 go p = do e <- lift (next p)
172 Left r -> return (return r)
176 else let (safe, unsafe) = B.span (<= 0x7F) chunk
177 in do yield (T.pack (B8.unpack safe))
180 else return (do yield unsafe
183 {- | Reduce a byte stream to a corresponding stream of ascii chars, returning the
184 unused 'ByteString' upon hitting the rare un-latinizable byte.
186 decodeIso8859_1 :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
187 decodeIso8859_1 = go where
188 go p = do e <- lift (next p)
190 Left r -> return (return r)
194 else do let (safe, unsafe) = B.span (<= 0xFF) chunk
195 yield (T.pack (B8.unpack safe))
198 else return (do yield unsafe