diff options
author | michaelt <what_is_it_to_do_anything@yahoo.com> | 2013-11-08 14:43:21 -0500 |
---|---|---|
committer | michaelt <what_is_it_to_do_anything@yahoo.com> | 2013-11-08 14:43:21 -0500 |
commit | a02a69adc0a4bbefb93bb80e17ac604f1b89ef78 (patch) | |
tree | d0929aff11fef7404eaf9b3ce83fa2ce6b307ca4 /Pipes | |
parent | 63ea9ffd3b32d1b4816e5b1e183d942df3d0de33 (diff) | |
download | text-pipes-a02a69adc0a4bbefb93bb80e17ac604f1b89ef78.tar.gz text-pipes-a02a69adc0a4bbefb93bb80e17ac604f1b89ef78.tar.zst text-pipes-a02a69adc0a4bbefb93bb80e17ac604f1b89ef78.zip |
decoding pipes added
Diffstat (limited to 'Pipes')
-rw-r--r-- | Pipes/Text.hs | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/Pipes/Text.hs b/Pipes/Text.hs index bdd706a..04c509f 100644 --- a/Pipes/Text.hs +++ b/Pipes/Text.hs | |||
@@ -81,6 +81,10 @@ module Pipes.Text ( | |||
81 | filter, | 81 | filter, |
82 | scan, | 82 | scan, |
83 | encodeUtf8, | 83 | encodeUtf8, |
84 | #if MIN_VERSION_text(0,11,4) | ||
85 | pipeDecodeUtf8, | ||
86 | pipeDecodeUtf8With, | ||
87 | #endif | ||
84 | pack, | 88 | pack, |
85 | unpack, | 89 | unpack, |
86 | toCaseFold, | 90 | toCaseFold, |
@@ -318,7 +322,8 @@ concatMap f = P.map (T.concatMap f) | |||
318 | 322 | ||
319 | 323 | ||
320 | -- | Transform a Pipe of 'Text' into a Pipe of 'ByteString's using UTF-8 | 324 | -- | Transform a Pipe of 'Text' into a Pipe of 'ByteString's using UTF-8 |
321 | -- encoding | 325 | -- encoding; @encodeUtf8 = Pipes.Prelude.map TE.encodeUtf8@ so more complex |
326 | -- encoding pipes can easily be constructed with the functions in @Data.Text.Encoding@ | ||
322 | encodeUtf8 :: Monad m => Pipe Text ByteString m r | 327 | encodeUtf8 :: Monad m => Pipe Text ByteString m r |
323 | encodeUtf8 = P.map TE.encodeUtf8 | 328 | encodeUtf8 = P.map TE.encodeUtf8 |
324 | {-# INLINEABLE encodeUtf8 #-} | 329 | {-# INLINEABLE encodeUtf8 #-} |
@@ -594,6 +599,29 @@ decodeUtf8With onErr = go (TE.streamDecodeUtf8With onErr) | |||
594 | yield l | 599 | yield l |
595 | p' | 600 | p' |
596 | {-# INLINEABLE decodeUtf8With #-} | 601 | {-# INLINEABLE decodeUtf8With #-} |
602 | |||
603 | -- | A simple pipe from 'ByteString' to 'Text'; a decoding error will arise | ||
604 | -- with any chunk that contains a sequence of bytes that is unreadable. Otherwise | ||
605 | -- only few bytes will only be moved from one chunk to the next before decoding. | ||
606 | pipeDecodeUtf8 :: Monad m => Pipe ByteString Text m r | ||
607 | pipeDecodeUtf8 = go TE.streamDecodeUtf8 | ||
608 | where go dec = do chunk <- await | ||
609 | case dec chunk of | ||
610 | TE.Some text l dec' -> do yield text | ||
611 | go dec' | ||
612 | {-# INLINEABLE pipeDecodeUtf8 #-} | ||
613 | |||
614 | -- | A simple pipe from 'ByteString' to 'Text' using a replacement function. | ||
615 | pipeDecodeUtf8With | ||
616 | :: Monad m | ||
617 | => TE.OnDecodeError | ||
618 | -> Pipe ByteString Text m r | ||
619 | pipeDecodeUtf8With onErr = go (TE.streamDecodeUtf8With onErr) | ||
620 | where go dec = do chunk <- await | ||
621 | case dec chunk of | ||
622 | TE.Some text l dec' -> do yield text | ||
623 | go dec' | ||
624 | {-# INLINEABLE pipeDecodeUtf8With #-} | ||
597 | #endif | 625 | #endif |
598 | 626 | ||
599 | -- | Splits a 'Producer' after the given number of characters | 627 | -- | Splits a 'Producer' after the given number of characters |