]> git.immae.eu Git - github/fretlink/text-pipes.git/blobdiff - Pipes/Text/Encoding.hs
Merge pull request #18 from sid-kap/text_lines
[github/fretlink/text-pipes.git] / Pipes / Text / Encoding.hs
index 991000f57dcaeef6621b284665c8cbb2dd6293a2..e24241132b3ae13f43a608922a461e8287386617 100644 (file)
@@ -1,19 +1,25 @@
 {-# LANGUAGE RankNTypes, BangPatterns #-}
 
--- | This module uses the stream decoding functions from Michael Snoyman's new
---  <http://hackage.haskell.org/package/text-stream-decode text-stream-decode
+-- | This module uses the stream decoding functions from
+--  <http://hackage.haskell.org/package/streaming-commons streaming-commons
 --  package to define decoding functions and lenses.  The exported names
---  conflict with names in @Data.Text.Encoding@ but the module can otherwise be 
---  imported unqualified. 
+--  conflict with names in @Data.Text.Encoding@ but not with the @Prelude@ 
 
 module Pipes.Text.Encoding
     ( 
-    -- * The Lens or Codec type
+    -- * Decoding ByteStrings and Encoding Texts
+    -- ** Simple usage
+    -- $usage
+    
+    -- ** Lens usage
     -- $lenses
+  
+    
+    -- * Basic lens operations
     Codec
     , decode
-    -- * \'Viewing\' the Text in a byte stream
-    -- $codecs
+    , eof
+    -- * Decoding lenses
     , utf8
     , utf8Pure
     , utf16LE
@@ -41,88 +47,121 @@ module Pipes.Text.Encoding
     , decodeAscii
     , encodeIso8859_1
     , decodeIso8859_1
-    , Lens'_
-    , Iso'_
     ) 
     where
 
 import Data.Functor.Constant (Constant(..))
-import Data.Profunctor (Profunctor)
 import Data.Char (ord)
 import Data.ByteString as B 
-import Data.ByteString (ByteString)
 import Data.ByteString.Char8 as B8
 import Data.Text (Text)
 import qualified Data.Text as T 
 import qualified Data.Text.Encoding as TE 
 import qualified Data.Streaming.Text as Stream
 import Data.Streaming.Text (DecodeResult(..))
-import Control.Monad (join)
-import Data.Word (Word8)
+import Control.Monad (join, liftM)
 import Pipes
 
-type Lens'_ a b = forall f . Functor f => (b -> f b) -> (a -> f a)
-type Iso'_ a b = forall f p . (Functor f, Profunctor p) => p b (f b) -> p a (f a)
 
-{- $lenses
-    The 'Codec' type is a simple specializion of 
-    the @Lens'_@ type synonymn used by the standard lens libraries, 
-    <http://hackage.haskell.org/package/lens lens> and 
-    <http://hackage.haskell.org/package/lens-family lens-family>. That type, 
-    
->   type Lens'_ a b = forall f . Functor f => (b -> f b) -> (a -> f a)
 
-    is just an alias for a Prelude type. Thus you use any particular codec with
-    the @view@ / @(^.)@ , @zoom@ and @over@ functions from either of those libraries;
-    we presuppose neither since we already have access to the types they require.
+{- $usage
+    Encoding is of course simple. Given 
 
-    -}
+>   text :: Producer Text IO ()
 
-type Codec
-    =  forall m r
-    .  Monad m
-    => Lens'_ (Producer ByteString m r)
-             (Producer Text m (Producer ByteString m r))
+    we can encode it with @Data.Text.Encoding.encodeUtf8@ 
 
-{- | 'decode' is just the ordinary @view@ or @(^.)@ of the lens libraries;
-      exported here under a name appropriate to the material. All of these are
-      the same: 
+>   TE.encodeUtf8 :: Text -> ByteString
 
->    decode utf8 p = decodeUtf8 p = view utf8 p = p ^. utf8
+    and ordinary pipe operations:
 
--}
+>   text >-> P.map TE.encodeUtf8 :: Producer.ByteString IO ()
 
-decode :: ((b -> Constant b b) -> (a -> Constant b a)) -> a -> b
-decode codec a = getConstant (codec Constant a)
+    or, equivalently
+
+>   for text (yield . TE.encodeUtf8)
+
+    But, using this module, we might use
+
+>   encodeUtf8 :: Text -> Producer ByteString m ()
+
+    to write
+
+>   for text encodeUtf8 :: Producer.ByteString IO ()
+
+    All of the above come to the same. 
 
 
-{- $codecs
+    Given
+
+>   bytes :: Producer ByteString IO ()
+
+    we can apply a decoding function from this module:
+
+>   decodeUtf8 bytes :: Producer Text IO (Producer ByteString IO ())
+
+    The Text producer ends wherever decoding first fails. The un-decoded
+    material is returned. If we are confident it is of no interest, we can
+    write: 
+
+>   void $ decodeUtf8 bytes :: Producer Text IO ()
+
+    Thus we can re-encode
+    as uft8 as much of our byte stream as is decodeUtf16BE decodable, with, e.g.
+
+>   for (decodeUtf16BE bytes) encodeUtf8 :: Producer ByteString IO (Producer ByteString IO ())
     
-    Each Codec-lens looks into a byte stream that is supposed to contain text.
-    The particular \'Codec\' lenses are named in accordance with the expected 
-    encoding, 'utf8', 'utf16LE' etc. To turn a Codec into an ordinary function, 
-    use @view@ / @(^.)@ -- here also called 'decode':
+    The bytestring producer that is returned begins with where utf16BE decoding
+    failed; if it didn't fail the producer is empty. 
+
+-}
+
+{- $lenses
+    We get a bit more flexibility, particularly in the use of pipes-style "parsers", 
+    if we use a lens like @utf8@ or @utf16BE@ 
+    that focusses on the text in an appropriately encoded byte stream.
+
+>   type Lens' a b = forall f . Functor f => (b -> f b) -> (a -> f a)
+
+    is just an alias for a Prelude type.  We abbreviate this further, for our use case, as
+
+>   type Codec
+>     =  forall m r .  Monad m => Lens' (Producer ByteString m r) (Producer Text m (Producer ByteString m r))
+
+    and call the decoding lenses @utf8@, @utf16BE@ \"codecs\", since they can 
+    re-encode what they have decoded.  Thus you use any particular codec with
+    the @view@ / @(^.)@ , @zoom@ and @over@ functions from the standard lens libraries;
+    <http://hackage.haskell.org/package/lens lens>,
+    <http://hackage.haskell.org/package/lens-family lens-family>,
+    <http://hackage.haskell.org/package/lens-simple lens-simple>, or one of the
+    and <http://hackage.haskell.org/package/microlens microlens> packages will all work
+    the same, since we already have access to the types they require.      
+
+    Each decoding lens looks into a byte stream that is supposed to contain text.
+    The particular lenses are named in accordance with the expected 
+    encoding, 'utf8', 'utf16LE' etc. To turn a such a lens or @Codec@ 
+    into an ordinary function, use @view@ / @(^.)@ -- here also called 'decode':
 
 >   view utf8 :: Producer ByteString m r -> Producer Text m (Producer ByteString m r)
 >   decode utf8 Byte.stdin :: Producer Text IO (Producer ByteString IO r)
 >   Bytes.stdin ^. utf8 ::  Producer Text IO (Producer ByteString IO r)
 
-    Uses of a codec with @view@ or @(^.)@ or 'decode' can always be replaced by the specialized 
-    decoding functions exported here, e.g. 
+    Of course, we could always do this with the specialized decoding functions, e.g. 
 
 >   decodeUtf8 ::  Producer ByteString m r -> Producer Text m (Producer ByteString m r)
 >   decodeUtf8 Byte.stdin :: Producer Text IO (Producer ByteString IO r)
 
-    The stream of text that a @Codec@ \'sees\' in the stream of bytes begins at its head. 
+    As with these functions, the stream of text that a @Codec@ \'sees\' 
+    in the stream of bytes begins at its head. 
     At any point of decoding failure, the stream of text ends and reverts to (returns) 
     the original byte stream. Thus if the first bytes are already
     un-decodable, the whole ByteString producer will be returned, i.e.
 
->   view utf8 bytestream 
+>   view utf8 bad_bytestream 
 
     will just come to the same as 
 
->   return bytestream
+>   return bad_bytestream
 
     Where there is no decoding failure, the return value of the text stream will be
     an empty byte stream followed by its own return value.  In all cases you must
@@ -130,8 +169,22 @@ decode codec a = getConstant (codec Constant a)
     it can be thrown away with @Control.Monad.void@
 
 >   void (Bytes.stdin ^. utf8) :: Producer Text IO ()
+
+    The @eof@ lens permits you to pattern match: if there is a Right value,
+    it is the leftover bytestring producer, if there is a Right value, it 
+    is the return value of the original bytestring producer:
+
+>   Bytes.stdin ^. utf8 . eof :: Producer Text IO (Either (Producer ByteString IO IO) ())
     
-    @zoom@ converts a Text parser into a ByteString parser:
+    Thus for the stream of un-decodable bytes mentioned above,
+
+>   view (utf8 . eof) bad_bytestream
+
+    will be the same as 
+
+>   return (Left bad_bytestream)
+
+    @zoom utf8@ converts a Text parser into a ByteString parser:
 
 >   zoom utf8 drawChar :: Monad m => StateT (Producer ByteString m r) m (Maybe Char)
 
@@ -139,24 +192,81 @@ decode codec a = getConstant (codec Constant a)
     
 >   zoom utf8 drawChar :: Monad m => Parser ByteString m (Maybe Char)
 
-    Thus we can define a ByteString parser like this:
+    Thus we can define a ByteString parser (in the pipes-parse sense) like this:
     
->   withNextByte :: Parser ByteString m (Maybe Char, Maybe Word8))) 
->   withNextByte = do char_ <- zoom utf8 Text.drawChar
+>   charPlusByte :: Parser ByteString m (Maybe Char, Maybe Word8))) 
+>   charPlusByte = do char_ <- zoom utf8 Text.drawChar
 >                     byte_ <- Bytes.peekByte
 >                     return (char_, byte_)
 
-     Though @withNextByte@ is partly defined with a Text parser 'drawChar'; 
+     Though @charPlusByte@ is partly defined with a Text parser 'drawChar'; 
      but it is a ByteString parser; it will return the first valid utf8-encoded 
-     Char in a ByteString, whatever its length
-     and the first byte of the next character, if they exist. Because 
+     Char in a ByteString, /whatever its byte-length/
+     and the first byte following, if both exist. Because 
      we \'draw\' one and \'peek\' at the other, the parser as a whole only 
      advances one Char's length along the bytestring, whatever that length may be.
      See the slightly more complex example \'decode.hs\' in the 
-     <http://www.haskellforall.com/2014/02/pipes-parse-30-lens-based-parsing.html#batteries-included haskellforall> 
-     discussion of this type of byte stream parsing.
+     <http://www.haskellforall.com/2014/02/pipes-parse-30-lens-based-parsing.html#batteries-included haskellforall blog
+     discussion of this type of byte stream parsing. 
     -}
 
+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@ is just the ordinary @view@ or @(^.)@ of the lens libraries;
+   exported here under a name appropriate to the material. Thus
+
+>    decode utf8 bytes :: Producer Text IO (Producer ByteString IO ())
+
+    All of these are thus the same:
+
+>    decode utf8 bytes = view utf8 bytes = bytes ^. utf8 = decodeUtf8 bytes
+
+
+-}
+
+decode :: ((b -> Constant b b) -> (a -> Constant b a)) -> a -> b
+decode codec a = getConstant (codec Constant a)
+
+{- | @eof@ tells you explicitly when decoding stops due to bad bytes or 
+    instead reaches end-of-file happily. (Without it one just makes an explicit 
+    test for emptiness of the resulting bytestring production using next) Thus
+
+>    decode (utf8 . eof) bytes :: Producer T.Text IO (Either (Producer B.ByteString IO ()) ())
+
+    If we hit undecodable bytes, the remaining bytestring producer will be 
+    returned as a Left value; in the happy case, a Right value is returned 
+    with the anticipated return value for the original bytestring producer.
+
+    Again, all of these are the same
+
+>    decode (utf8 . eof) bytes = view (utf8 . eof) p = p^.utf8.eof
+
+-}
+
+eof :: (Monad m, Monad (t m), MonadTrans t) => Lens' (t m (Producer ByteString m r))
+                       (t m (Either (Producer ByteString m r) r))
+eof k p0 = fmap fromEither (k (toEither p0)) where
+
+ fromEither = liftM (either id return)
+
+ toEither pp = do p <- pp
+                  check p
+
+ check p = do e <- lift (next p)
+              case e of 
+                Left r -> return (Right r)
+                Right (bs,pb) ->  if B.null bs 
+                                    then check pb
+                                    else return (Left (do yield bs
+                                                          pb))
+
 utf8 :: Codec
 utf8 = mkCodec decodeUtf8 TE.encodeUtf8
 
@@ -181,15 +291,17 @@ decodeStream :: Monad m
 decodeStream = loop where
   loop dec0 p = 
     do x <- lift (next p) 
-       case x of Left r -> return (return r)
-                 Right (chunk, p') -> case dec0 chunk of 
-                    DecodeResultSuccess text dec -> do yield text
-                                                       loop dec p'
-                    DecodeResultFailure text bs -> do yield text 
-                                                      return (do yield bs 
-                                                                 p')
+       case x of 
+         Left r -> return (return r)
+         Right (chunk, p') -> case dec0 chunk of 
+           DecodeResultSuccess text dec -> do yield text
+                                              loop dec p'
+           DecodeResultFailure text bs -> do yield text 
+                                             return (do yield bs 
+                                                        p')
 {-# INLINABLE decodeStream#-}
 
+
 {- $decoders
    These are functions with the simple type: