]> git.immae.eu Git - github/fretlink/text-pipes.git/blame - examples/decode.hs
version bump
[github/fretlink/text-pipes.git] / examples / decode.hs
CommitLineData
955edd33 1-- http://www.haskellforall.com/2014/02/pipes-parse-30-lens-based-parsing.html
2
3import Data.ByteString (ByteString)
4import Data.Text (Text)
5import Lens.Family.State.Strict (zoom)
8197d6e0 6
955edd33 7import Pipes
8import Pipes.Parse
9import qualified Pipes.ByteString as ByteString
8197d6e0 10import qualified Pipes.Text as Text
11import qualified Pipes.Text.Encoding as Text
955edd33 12
77c32261 13-- Retrieve utf8-encode `Text` chunk(s) up to 10 characters
14-- from the bytestring (this can have various byte lengths)
955edd33 15parser :: Monad m => Parser ByteString m [Text]
8197d6e0 16parser = zoom (Text.utf8 . Text.splitAt 10) drawAll
955edd33 17
18main = do
19 (textChunks, leftovers) <- runStateT parser ByteString.stdin
20 print textChunks
21
22 -- Now print the remaining `ByteString` chunks
23 byteChunks <- evalStateT drawAll leftovers
24 print byteChunks
25{-
26$ ./decode
27Hello, 世界!!!<Enter>
28["Hello, \19990\30028!"]
29abcdefg<Enter>
30<Ctrl-D>
31["!!\n","abcdefg\n"]
32
33-}