diff options
author | michaelt <what_is_it_to_do_anything@yahoo.com> | 2013-11-18 10:11:22 -0500 |
---|---|---|
committer | michaelt <what_is_it_to_do_anything@yahoo.com> | 2013-11-18 10:11:22 -0500 |
commit | cf10d6f11b9a1912e234d449a4eef422e2dd1d66 (patch) | |
tree | a6bc1c4ad313f8118b87f655489340b00e1c27ae | |
parent | a02a69adc0a4bbefb93bb80e17ac604f1b89ef78 (diff) | |
download | text-pipes-cf10d6f11b9a1912e234d449a4eef422e2dd1d66.tar.gz text-pipes-cf10d6f11b9a1912e234d449a4eef422e2dd1d66.tar.zst text-pipes-cf10d6f11b9a1912e234d449a4eef422e2dd1d66.zip |
Gabriel's improved 'words'
-rw-r--r-- | Pipes/Text.hs | 28 |
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) | |||
159 | import Data.ByteString.Unsafe (unsafeTake, unsafeDrop) | 159 | import Data.ByteString.Unsafe (unsafeTake, unsafeDrop) |
160 | import Data.ByteString (ByteString) | 160 | import Data.ByteString (ByteString) |
161 | import qualified Data.ByteString as B | 161 | import qualified Data.ByteString as B |
162 | import Data.Char (ord) | 162 | import Data.Char (ord, isSpace) |
163 | import Data.Functor.Identity (Identity) | 163 | import Data.Functor.Identity (Identity) |
164 | import qualified Data.List as List | 164 | import qualified Data.List as List |
165 | import Foreign.C.Error (Errno(Errno), ePIPE) | 165 | import 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 |
795 | words | 795 | words |
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 |
797 | words p0 = removeEmpty (splitWith isSpace p0) | 797 | words = 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 |
818 | intersperse | 810 | intersperse |
819 | :: (Monad m) => Char -> Producer Text m r -> Producer Text m r | 811 | :: (Monad m) => Char -> Producer Text m r -> Producer Text m r |