diff options
author | michaelt <what_is_it_to_do_anything@yahoo.com> | 2014-11-11 22:02:24 -0500 |
---|---|---|
committer | michaelt <what_is_it_to_do_anything@yahoo.com> | 2014-11-11 22:02:24 -0500 |
commit | 6c2fffdc8bc84879e103e6838e4f9fc762d50a2d (patch) | |
tree | a1cf38735800adf9757b1b609e9a6f75bf0b5941 /Pipes | |
parent | 87f0d4d06a74aa59047a0bcb54eaf10acbfdf06a (diff) | |
download | text-pipes-6c2fffdc8bc84879e103e6838e4f9fc762d50a2d.tar.gz text-pipes-6c2fffdc8bc84879e103e6838e4f9fc762d50a2d.tar.zst text-pipes-6c2fffdc8bc84879e103e6838e4f9fc762d50a2d.zip |
added eoflens to discriminate whether decoding was completed
Diffstat (limited to 'Pipes')
-rw-r--r-- | Pipes/Text/Encoding.hs | 35 | ||||
-rw-r--r-- | Pipes/Text/Tutorial.hs | 4 |
2 files changed, 37 insertions, 2 deletions
diff --git a/Pipes/Text/Encoding.hs b/Pipes/Text/Encoding.hs index e00cd43..f26f168 100644 --- a/Pipes/Text/Encoding.hs +++ b/Pipes/Text/Encoding.hs | |||
@@ -12,6 +12,7 @@ module Pipes.Text.Encoding | |||
12 | -- $lenses | 12 | -- $lenses |
13 | Codec | 13 | Codec |
14 | , decode | 14 | , decode |
15 | , eof | ||
15 | -- * \'Viewing\' the Text in a byte stream | 16 | -- * \'Viewing\' the Text in a byte stream |
16 | -- $codecs | 17 | -- $codecs |
17 | , utf8 | 18 | , utf8 |
@@ -88,9 +89,42 @@ type Codec | |||
88 | 89 | ||
89 | -} | 90 | -} |
90 | 91 | ||
92 | |||
91 | decode :: ((b -> Constant b b) -> (a -> Constant b a)) -> a -> b | 93 | decode :: ((b -> Constant b b) -> (a -> Constant b a)) -> a -> b |
92 | decode codec a = getConstant (codec Constant a) | 94 | decode codec a = getConstant (codec Constant a) |
93 | 95 | ||
96 | {- | 'eof' tells you explicitly when decoding stops due to bad bytes or instead | ||
97 | reaches end-of-file happily. (Without it one just makes an explicit test | ||
98 | for emptiness of the resulting bytestring production using 'next') | ||
99 | Thus | ||
100 | |||
101 | > decode (utf8 . eof) p = view (utf8 . eof) p = p^.utf8.eof | ||
102 | |||
103 | will be a text producer. If we hit undecodable bytes, the remaining | ||
104 | bytestring producer will be returned as a 'Left' value; | ||
105 | in the happy case, a 'Right' value is returned with the anticipated | ||
106 | return value for the original bytestring producer. | ||
107 | ) | ||
108 | |||
109 | -} | ||
110 | |||
111 | eof :: Monad m => Lens' (Producer Text m (Producer ByteString m r)) | ||
112 | (Producer Text m (Either (Producer ByteString m r) r)) | ||
113 | eof k p = fmap fromEither (k (toEither p)) where | ||
114 | |||
115 | fromEither = liftM (either id return) | ||
116 | |||
117 | toEither pp = do p <- pp | ||
118 | check p | ||
119 | |||
120 | check p = do e <- lift (next p) | ||
121 | case e of | ||
122 | Left r -> return (Right r) | ||
123 | Right (bs,pb) -> if B.null bs | ||
124 | then check pb | ||
125 | else return (Left (do yield bs | ||
126 | pb)) | ||
127 | |||
94 | 128 | ||
95 | {- $codecs | 129 | {- $codecs |
96 | 130 | ||
@@ -186,6 +220,7 @@ decodeStream = loop where | |||
186 | p') | 220 | p') |
187 | {-# INLINABLE decodeStream#-} | 221 | {-# INLINABLE decodeStream#-} |
188 | 222 | ||
223 | |||
189 | {- $decoders | 224 | {- $decoders |
190 | These are functions with the simple type: | 225 | These are functions with the simple type: |
191 | 226 | ||
diff --git a/Pipes/Text/Tutorial.hs b/Pipes/Text/Tutorial.hs index 07b8751..25f9e41 100644 --- a/Pipes/Text/Tutorial.hs +++ b/Pipes/Text/Tutorial.hs | |||
@@ -24,12 +24,12 @@ module Pipes.Text.Tutorial ( | |||
24 | -- * Special types: @Producer Text m (Producer Text m r)@ and @FreeT (Producer Text m) m r@ | 24 | -- * Special types: @Producer Text m (Producer Text m r)@ and @FreeT (Producer Text m) m r@ |
25 | -- $special | 25 | -- $special |
26 | ) where | 26 | ) where |
27 | 27 | ||
28 | import Pipes | 28 | import Pipes |
29 | import Pipes.Text | 29 | import Pipes.Text |
30 | import Pipes.Text.IO | 30 | import Pipes.Text.IO |
31 | import Pipes.Text.Encoding | 31 | import Pipes.Text.Encoding |
32 | 32 | ||
33 | {- $intro | 33 | {- $intro |
34 | This package provides @pipes@ utilities for /character streams/, | 34 | This package provides @pipes@ utilities for /character streams/, |
35 | realized as streams of 'Text' chunks. The individual chunks are uniformly /strict/, | 35 | realized as streams of 'Text' chunks. The individual chunks are uniformly /strict/, |