1 {-# LANGUAGE RankNTypes, BangPatterns #-}
3 -- | This module uses the stream decoding functions from Michael Snoyman's new
4 -- <http://hackage.haskell.org/package/text-stream-decode text-stream-decode>
5 -- package to define decoding functions and lenses. The exported names
6 -- conflict with names in @Data.Text.Encoding@ but the module can otherwise be
7 -- imported unqualified.
9 module Pipes.Text.Encoding
11 -- * The Lens or Codec type
15 -- * \'Viewing\' the Text in a byte stream
23 -- * Non-lens decoding functions
31 -- * Re-encoding functions
38 -- * Functions for latin and ascii text
49 import Data.Functor.Constant (Constant(..))
50 import Data.Profunctor (Profunctor)
51 import Data.Char (ord)
52 import Data.ByteString as B
53 import Data.ByteString (ByteString)
54 import Data.ByteString.Char8 as B8
55 import Data.Text (Text)
56 import qualified Data.Text as T
57 import qualified Data.Text.Encoding as TE
58 import qualified Data.Streaming.Text as Stream
59 import Data.Streaming.Text (DecodeResult(..))
60 import Control.Monad (join)
61 import Data.Word (Word8)
64 type Lens'_ a b = forall f . Functor f => (b -> f b) -> (a -> f a)
65 type Iso'_ a b = forall f p . (Functor f, Profunctor p) => p b (f b) -> p a (f a)
68 The 'Codec' type is a simple specializion of
69 the @Lens'_@ type synonymn used by the standard lens libraries,
70 <http://hackage.haskell.org/package/lens lens> and
71 <http://hackage.haskell.org/package/lens-family lens-family>. That type,
73 > type Lens'_ a b = forall f . Functor f => (b -> f b) -> (a -> f a)
75 is just an alias for a Prelude type. Thus you use any particular codec with
76 the @view@ / @(^.)@ , @zoom@ and @over@ functions from either of those libraries;
77 we presuppose neither since we already have access to the types they require.
84 => Lens'_ (Producer ByteString m r)
85 (Producer Text m (Producer ByteString m r))
87 {- | 'decode' is just the ordinary @view@ or @(^.)@ of the lens libraries;
88 exported here under a name appropriate to the material. All of these are
91 > decode utf8 p = decodeUtf8 p = view utf8 p = p ^. utf8
95 decode :: ((b -> Constant b b) -> (a -> Constant b a)) -> a -> b
96 decode codec a = getConstant (codec Constant a)
101 Each Codec-lens looks into a byte stream that is supposed to contain text.
102 The particular \'Codec\' lenses are named in accordance with the expected
103 encoding, 'utf8', 'utf16LE' etc. To turn a Codec into an ordinary function,
104 use @view@ / @(^.)@ -- here also called 'decode':
106 > view utf8 :: Producer ByteString m r -> Producer Text m (Producer ByteString m r)
107 > decode utf8 Byte.stdin :: Producer Text IO (Producer ByteString IO r)
108 > Bytes.stdin ^. utf8 :: Producer Text IO (Producer ByteString IO r)
110 Uses of a codec with @view@ or @(^.)@ or 'decode' can always be replaced by the specialized
111 decoding functions exported here, e.g.
113 > decodeUtf8 :: Producer ByteString m r -> Producer Text m (Producer ByteString m r)
114 > decodeUtf8 Byte.stdin :: Producer Text IO (Producer ByteString IO r)
116 The stream of text that a @Codec@ \'sees\' in the stream of bytes begins at its head.
117 At any point of decoding failure, the stream of text ends and reverts to (returns)
118 the original byte stream. Thus if the first bytes are already
119 un-decodable, the whole ByteString producer will be returned, i.e.
121 > view utf8 bytestream
123 will just come to the same as
127 Where there is no decoding failure, the return value of the text stream will be
128 an empty byte stream followed by its own return value. In all cases you must
129 deal with the fact that it is a /ByteString producer/ that is returned, even if
130 it can be thrown away with @Control.Monad.void@
132 > void (Bytes.stdin ^. utf8) :: Producer Text IO ()
134 @zoom@ converts a Text parser into a ByteString parser:
136 > zoom utf8 drawChar :: Monad m => StateT (Producer ByteString m r) m (Maybe Char)
138 or, using the type synonymn from @Pipes.Parse@:
140 > zoom utf8 drawChar :: Monad m => Parser ByteString m (Maybe Char)
142 Thus we can define a ByteString parser like this:
144 > withNextByte :: Parser ByteString m (Maybe Char, Maybe Word8)))
145 > withNextByte = do char_ <- zoom utf8 Text.drawChar
146 > byte_ <- Bytes.peekByte
147 > return (char_, byte_)
149 Though @withNextByte@ is partly defined with a Text parser 'drawChar';
150 but it is a ByteString parser; it will return the first valid utf8-encoded
151 Char in a ByteString, whatever its length,
152 and the first byte of the next character, if they exist. Because
153 we \'draw\' one and \'peek\' at the other, the parser as a whole only
154 advances one Char's length along the bytestring, whatever that length may be.
155 See the slightly more complex example \'decode.hs\' in the
156 <http://www.haskellforall.com/2014/02/pipes-parse-30-lens-based-parsing.html#batteries-included haskellforall>
157 discussion of this type of byte stream parsing.
161 utf8 = mkCodec decodeUtf8 TE.encodeUtf8
164 utf8Pure = mkCodec decodeUtf8Pure TE.encodeUtf8
167 utf16LE = mkCodec decodeUtf16LE TE.encodeUtf16LE
170 utf16BE = mkCodec decodeUtf16BE TE.encodeUtf16BE
173 utf32LE = mkCodec decodeUtf32LE TE.encodeUtf32LE
176 utf32BE = mkCodec decodeUtf32BE TE.encodeUtf32BE
178 decodeStream :: Monad m
179 => (B.ByteString -> DecodeResult)
180 -> Producer ByteString m r -> Producer Text m (Producer ByteString m r)
181 decodeStream = loop where
183 do x <- lift (next p)
184 case x of Left r -> return (return r)
185 Right (chunk, p') -> case dec0 chunk of
186 DecodeResultSuccess text dec -> do yield text
188 DecodeResultFailure text bs -> do yield text
191 {-# INLINABLE decodeStream#-}
194 These are functions with the simple type:
196 > decodeUtf8 :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
200 > decodeUtf8 = view utf8
201 > decodeUtf16LE = view utf16LE
203 and so forth, but these forms
204 may be more convenient (and give better type errors!) where lenses are
209 decodeUtf8 :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
210 decodeUtf8 = decodeStream Stream.decodeUtf8
211 {-# INLINE decodeUtf8 #-}
213 decodeUtf8Pure :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
214 decodeUtf8Pure = decodeStream Stream.decodeUtf8Pure
215 {-# INLINE decodeUtf8Pure #-}
217 decodeUtf16LE :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
218 decodeUtf16LE = decodeStream Stream.decodeUtf16LE
219 {-# INLINE decodeUtf16LE #-}
221 decodeUtf16BE :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
222 decodeUtf16BE = decodeStream Stream.decodeUtf16BE
223 {-# INLINE decodeUtf16BE #-}
225 decodeUtf32LE :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
226 decodeUtf32LE = decodeStream Stream.decodeUtf32LE
227 {-# INLINE decodeUtf32LE #-}
229 decodeUtf32BE :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
230 decodeUtf32BE = decodeStream Stream.decodeUtf32BE
231 {-# INLINE decodeUtf32BE #-}
235 These are simply defined
237 > encodeUtf8 = yield . TE.encodeUtf8
239 They are intended for use with 'for'
241 > for Text.stdin encodeUtf8 :: Producer ByteString IO ()
243 which would have the effect of
245 > Text.stdin >-> Pipes.Prelude.map (TE.encodeUtf8)
247 using the encoding functions from Data.Text.Encoding
250 encodeUtf8 :: Monad m => Text -> Producer' ByteString m ()
251 encodeUtf8 = yield . TE.encodeUtf8
252 encodeUtf16LE :: Monad m => Text -> Producer' ByteString m ()
253 encodeUtf16LE = yield . TE.encodeUtf16LE
254 encodeUtf16BE :: Monad m => Text -> Producer' ByteString m ()
255 encodeUtf16BE = yield . TE.encodeUtf16BE
256 encodeUtf32LE :: Monad m => Text -> Producer' ByteString m ()
257 encodeUtf32LE = yield . TE.encodeUtf32LE
258 encodeUtf32BE :: Monad m => Text -> Producer' ByteString m ()
259 encodeUtf32BE = yield . TE.encodeUtf32BE
261 mkCodec :: (forall r m . Monad m =>
262 Producer ByteString m r -> Producer Text m (Producer ByteString m r ))
263 -> (Text -> ByteString)
265 mkCodec dec enc = \k p0 -> fmap (\p -> join (for p (yield . enc))) (k (dec p0))
270 ascii and latin encodings only use a small number of the characters 'Text'
271 recognizes; thus we cannot use the pipes @Lens@ style to work with them.
272 Rather we simply define functions each way.
276 -- | 'encodeAscii' reduces as much of your stream of 'Text' actually is ascii to a byte stream,
277 -- returning the rest of the 'Text' at the first non-ascii 'Char'
279 encodeAscii :: Monad m => Producer Text m r -> Producer ByteString m (Producer Text m r)
280 encodeAscii = go where
281 go p = do e <- lift (next p)
283 Left r -> return (return r)
287 else let (safe, unsafe) = T.span (\c -> ord c <= 0x7F) chunk
288 in do yield (B8.pack (T.unpack safe))
291 else return $ do yield unsafe
294 {- | Reduce as much of your stream of 'Text' actually is iso8859 or latin1 to a byte stream,
295 returning the rest of the 'Text' upon hitting any non-latin 'Char'
297 encodeIso8859_1 :: Monad m => Producer Text m r -> Producer ByteString m (Producer Text m r)
298 encodeIso8859_1 = go where
299 go p = do e <- lift (next p)
301 Left r -> return (return r)
305 else let (safe, unsafe) = T.span (\c -> ord c <= 0xFF) txt
306 in do yield (B8.pack (T.unpack safe))
309 else return $ do yield unsafe
312 {- | Reduce a byte stream to a corresponding stream of ascii chars, returning the
313 unused 'ByteString' upon hitting an un-ascii byte.
315 decodeAscii :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
316 decodeAscii = go where
317 go p = do e <- lift (next p)
319 Left r -> return (return r)
323 else let (safe, unsafe) = B.span (<= 0x7F) chunk
324 in do yield (T.pack (B8.unpack safe))
327 else return (do yield unsafe
330 {- | Reduce a byte stream to a corresponding stream of ascii chars, returning the
331 unused 'ByteString' upon hitting the rare un-latinizable byte.
333 decodeIso8859_1 :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r)
334 decodeIso8859_1 = go where
335 go p = do e <- lift (next p)
337 Left r -> return (return r)
341 else do let (safe, unsafe) = B.span (<= 0xFF) chunk
342 yield (T.pack (B8.unpack safe))
345 else return (do yield unsafe