Skip to content

Commit

Permalink
Merge pull request #26 from haskell-works/bytestring-support
Browse files Browse the repository at this point in the history
Add bytestring support
  • Loading branch information
newhoggy authored Nov 4, 2018
2 parents 163b0ad + 0c59ef3 commit 1f34489
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/HaskellWorks/Data/Streams/ByteString.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{-# LANGUAGE ScopedTypeVariables #-}

module HaskellWorks.Data.Streams.ByteString
( streamChunks
, stream
) where

import Data.Word
import HaskellWorks.Data.Streams.Size
import HaskellWorks.Data.Streams.Stream (Step (..), Stream (..))
import Prelude hiding (foldl, map, sum, zipWith)

import qualified Data.ByteString as BS

streamChunks :: [BS.ByteString] -> Stream Word8
streamChunks ass = Stream step (ass, 0) Unknown
where step (bss, i) = case bss of
cs:css -> if i < BS.length cs
then Yield (BS.index cs i) (bss, i + 1)
else Skip (css, 0)
[] -> Done
{-# INLINE [1] streamChunks #-}

stream :: BS.ByteString -> Stream Word8
stream bs = Stream step (bs, 0) Unknown
where step (cs, i) = if i < BS.length cs
then Yield (BS.index cs i) (cs, i + 1)
else Done
{-# INLINE [1] stream #-}
12 changes: 12 additions & 0 deletions src/HaskellWorks/Data/Streams/ByteString/Lazy.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module HaskellWorks.Data.Streams.ByteString.Lazy
( stream
) where

import Data.Word
import HaskellWorks.Data.Streams.Stream (Stream (..))

import qualified Data.ByteString.Lazy as LBS
import qualified HaskellWorks.Data.Streams.ByteString as BS

stream :: LBS.ByteString -> Stream Word8
stream = BS.streamChunks . LBS.toChunks

0 comments on commit 1f34489

Please sign in to comment.