aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authormichaelt <what_is_it_to_do_anything@yahoo.com>2013-11-18 10:11:22 -0500
committermichaelt <what_is_it_to_do_anything@yahoo.com>2013-11-18 10:11:22 -0500
commitcf10d6f11b9a1912e234d449a4eef422e2dd1d66 (patch)
treea6bc1c4ad313f8118b87f655489340b00e1c27ae
parenta02a69adc0a4bbefb93bb80e17ac604f1b89ef78 (diff)
downloadtext-pipes-cf10d6f11b9a1912e234d449a4eef422e2dd1d66.tar.gz
text-pipes-cf10d6f11b9a1912e234d449a4eef422e2dd1d66.tar.zst
text-pipes-cf10d6f11b9a1912e234d449a4eef422e2dd1d66.zip
Gabriel's improved 'words'
-rw-r--r--Pipes/Text.hs28
1 files changed, 10 insertions, 18 deletions
diff --git a/Pipes/Text.hs b/Pipes/Text.hs
index 04c509f..cc08256 100644
--- a/Pipes/Text.hs
+++ b/Pipes/Text.hs
@@ -159,7 +159,7 @@ import Data.Text.Lazy.Internal (foldrChunks, defaultChunkSize)
159import Data.ByteString.Unsafe (unsafeTake, unsafeDrop) 159import Data.ByteString.Unsafe (unsafeTake, unsafeDrop)
160import Data.ByteString (ByteString) 160import Data.ByteString (ByteString)
161import qualified Data.ByteString as B 161import qualified Data.ByteString as B
162import Data.Char (ord) 162import Data.Char (ord, isSpace)
163import Data.Functor.Identity (Identity) 163import Data.Functor.Identity (Identity)
164import qualified Data.List as List 164import qualified Data.List as List
165import Foreign.C.Error (Errno(Errno), ePIPE) 165import Foreign.C.Error (Errno(Errno), ePIPE)
@@ -794,26 +794,18 @@ lines p0 = PP.FreeT (go0 p0)
794-- | Split a text stream into 'FreeT'-delimited words 794-- | Split a text stream into 'FreeT'-delimited words
795words 795words
796 :: (Monad m) => Producer Text m r -> FreeT (Producer Text m) m r 796 :: (Monad m) => Producer Text m r -> FreeT (Producer Text m) m r
797words p0 = removeEmpty (splitWith isSpace p0) 797words = go
798 where 798 where
799 removeEmpty f = PP.FreeT $ do 799 go p = PP.FreeT $ do
800 x <- PP.runFreeT f 800 x <- next (p >-> dropWhile isSpace)
801 case x of 801 return $ case x of
802 PP.Pure r -> return (PP.Pure r) 802 Left r -> PP.Pure r
803 PP.Free p -> loop p 803 Right (bs, p') -> PP.Free $ do
804 loop p = do 804 p'' <- break isSpace (yield bs >> p')
805 y <- next p 805 return (go p'')
806 case y of
807 Left f' -> PP.runFreeT (removeEmpty f')
808 Right (txt, p') ->
809 if T.null txt
810 then loop p'
811 else return $ PP.Free $ do
812 yield txt
813 f' <- p'
814 return (removeEmpty f')
815{-# INLINABLE words #-} 806{-# INLINABLE words #-}
816 807
808
817-- | Intersperse a 'Char' in between the characters of the text stream 809-- | Intersperse a 'Char' in between the characters of the text stream
818intersperse 810intersperse
819 :: (Monad m) => Char -> Producer Text m r -> Producer Text m r 811 :: (Monad m) => Char -> Producer Text m r -> Producer Text m r