aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/decode.hs
blob: d5fe558238f72e1916874b519a35473ff016b494 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
-- http://www.haskellforall.com/2014/02/pipes-parse-30-lens-based-parsing.html

import Data.ByteString (ByteString)
import Data.Text       (Text)
import Lens.Family.State.Strict (zoom)

import Pipes
import Pipes.Parse
import qualified Pipes.ByteString as ByteString
import qualified Pipes.Text as Text
import qualified Pipes.Text.Encoding as Text

-- Retrieve utf8-encode `Text` chunk(s) up to 10 characters
-- from the bytestring (this can have various byte lengths)
parser :: Monad m => Parser ByteString m [Text]
parser = zoom (Text.utf8 . Text.splitAt 10) drawAll

main = do
    (textChunks, leftovers) <- runStateT parser ByteString.stdin
    print textChunks

    -- Now print the remaining `ByteString` chunks
    byteChunks <- evalStateT drawAll leftovers
    print byteChunks
{-
$ ./decode
Hello, 世界!!!<Enter>
["Hello, \19990\30028!"]
abcdefg<Enter>
<Ctrl-D>
["!!\n","abcdefg\n"]

-}