{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes        #-}

module HaskellWorks.Data.Json.Conduit
  ( blankedJsonToInterestBits
  , byteStringToBits
  , blankedJsonToBalancedParens
  , blankedJsonToBalancedParens2
  , compressWordAsBit
  , interestingWord8s
  ) where

import           Control.Monad
import           Data.Array.Unboxed                   as A
import qualified Data.Bits                            as BITS
import           Data.ByteString                      as BS
import           Data.Conduit
import           Data.Int
import           Data.Word
import           Data.Word8
import           HaskellWorks.Data.Bits.BitWise
import           Prelude                              as P

interestingWord8s :: A.UArray Word8 Word8
interestingWord8s :: UArray Word8 Word8
interestingWord8s = (Word8, Word8) -> [(Word8, Word8)] -> UArray Word8 Word8
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
(i, i) -> [(i, e)] -> a i e
A.array (Word8
0, Word8
255) [
  (Word8
w, if Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_bracketleft Bool -> Bool -> Bool
|| Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_braceleft Bool -> Bool -> Bool
|| Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_parenleft Bool -> Bool -> Bool
|| Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_t Bool -> Bool -> Bool
|| Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_f Bool -> Bool -> Bool
|| Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_n Bool -> Bool -> Bool
|| Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_1
    then Word8
1
    else Word8
0)
  | Word8
w <- [Word8
0 .. Word8
255]]

blankedJsonToInterestBits :: Monad m => Conduit BS.ByteString m BS.ByteString
blankedJsonToInterestBits :: Conduit ByteString m ByteString
blankedJsonToInterestBits = ByteString -> Conduit ByteString m ByteString
forall (m :: * -> *).
Monad m =>
ByteString -> Conduit ByteString m ByteString
blankedJsonToInterestBits' ByteString
""

padRight :: Word8 -> Int -> BS.ByteString -> BS.ByteString
padRight :: Word8 -> Int -> ByteString -> ByteString
padRight Word8
w Int
n ByteString
bs = if ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
n then ByteString
bs else (ByteString, Maybe ByteString) -> ByteString
forall a b. (a, b) -> a
fst (Int
-> (ByteString -> Maybe (Word8, ByteString))
-> ByteString
-> (ByteString, Maybe ByteString)
forall a.
Int -> (a -> Maybe (Word8, a)) -> a -> (ByteString, Maybe a)
BS.unfoldrN Int
n ByteString -> Maybe (Word8, ByteString)
gen ByteString
bs)
  where gen :: ByteString -> Maybe (Word8, ByteString)
        gen :: ByteString -> Maybe (Word8, ByteString)
gen ByteString
cs = case ByteString -> Maybe (Word8, ByteString)
BS.uncons ByteString
cs of
          Just (Word8
c, ByteString
ds) -> (Word8, ByteString) -> Maybe (Word8, ByteString)
forall a. a -> Maybe a
Just (Word8
c, ByteString
ds)
          Maybe (Word8, ByteString)
Nothing      -> (Word8, ByteString) -> Maybe (Word8, ByteString)
forall a. a -> Maybe a
Just (Word8
w, ByteString
BS.empty)

blankedJsonToInterestBits' :: Monad m => BS.ByteString -> Conduit BS.ByteString m BS.ByteString
blankedJsonToInterestBits' :: ByteString -> Conduit ByteString m ByteString
blankedJsonToInterestBits' ByteString
rs = do
  Maybe ByteString
mbs <- ConduitT ByteString ByteString m (Maybe ByteString)
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await
  case Maybe ByteString
mbs of
    Just ByteString
bs -> do
      let cs :: ByteString
cs = if ByteString -> Int
BS.length ByteString
rs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0 then [ByteString] -> ByteString
BS.concat [ByteString
rs, ByteString
bs] else ByteString
bs
      let lencs :: Int
lencs = ByteString -> Int
BS.length ByteString
cs
      let q :: Int
q = Int
lencs Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
7 Int -> Int -> Int
forall a. Integral a => a -> a -> a
`quot` Int
8
      let (ByteString
ds, ByteString
es) = Int -> ByteString -> (ByteString, ByteString)
BS.splitAt (Int
q Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
8) ByteString
cs
      let (ByteString
fs, Maybe ByteString
_) = Int
-> (ByteString -> Maybe (Word8, ByteString))
-> ByteString
-> (ByteString, Maybe ByteString)
forall a.
Int -> (a -> Maybe (Word8, a)) -> a -> (ByteString, Maybe a)
BS.unfoldrN Int
q ByteString -> Maybe (Word8, ByteString)
gen ByteString
ds
      ByteString -> Conduit ByteString m ByteString
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield ByteString
fs
      ByteString -> Conduit ByteString m ByteString
forall (m :: * -> *).
Monad m =>
ByteString -> Conduit ByteString m ByteString
blankedJsonToInterestBits' ByteString
es
    Maybe ByteString
Nothing -> () -> Conduit ByteString m ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  where gen :: ByteString -> Maybe (Word8, ByteString)
        gen :: ByteString -> Maybe (Word8, ByteString)
gen ByteString
as = if ByteString -> Int
BS.length ByteString
as Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
          then Maybe (Word8, ByteString)
forall a. Maybe a
Nothing
          else (Word8, ByteString) -> Maybe (Word8, ByteString)
forall a. a -> Maybe a
Just ( (Word8 -> Word8 -> Word8) -> Word8 -> ByteString -> Word8
forall a. (Word8 -> a -> a) -> a -> ByteString -> a
BS.foldr (\Word8
b Word8
m -> (UArray Word8 Word8
interestingWord8s UArray Word8 Word8 -> Word8 -> Word8
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! Word8
b) Word8 -> Word8 -> Word8
forall a. BitWise a => a -> a -> a
.|. (Word8
m Word8 -> Count -> Word8
forall a. Shift a => a -> Count -> a
.<. Count
1)) Word8
0 (Word8 -> Int -> ByteString -> ByteString
padRight Word8
0 Int
8 (Int -> ByteString -> ByteString
BS.take Int
8 ByteString
as))
                    , Int -> ByteString -> ByteString
BS.drop Int
8 ByteString
as
                    )

blankedJsonToBalancedParens :: Monad m => Conduit BS.ByteString m Bool
blankedJsonToBalancedParens :: Conduit ByteString m Bool
blankedJsonToBalancedParens = do
  Maybe ByteString
mbs <- ConduitT ByteString Bool m (Maybe ByteString)
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await
  case Maybe ByteString
mbs of
    Just ByteString
bs -> ByteString -> Conduit ByteString m Bool
forall (m :: * -> *).
Monad m =>
ByteString -> Conduit ByteString m Bool
blankedJsonToBalancedParens' ByteString
bs
    Maybe ByteString
Nothing -> () -> Conduit ByteString m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return ()

blankedJsonToBalancedParens' :: Monad m => BS.ByteString -> Conduit BS.ByteString m Bool
blankedJsonToBalancedParens' :: ByteString -> Conduit ByteString m Bool
blankedJsonToBalancedParens' ByteString
bs = case ByteString -> Maybe (Word8, ByteString)
BS.uncons ByteString
bs of
  Just (Word8
c, ByteString
cs) -> do
    case Word8
c of
      Word8
d | Word8
d Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_braceleft     -> Bool -> Conduit ByteString m Bool
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield Bool
True
      Word8
d | Word8
d Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_braceright    -> Bool -> Conduit ByteString m Bool
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield Bool
False
      Word8
d | Word8
d Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_bracketleft   -> Bool -> Conduit ByteString m Bool
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield Bool
True
      Word8
d | Word8
d Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_bracketright  -> Bool -> Conduit ByteString m Bool
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield Bool
False
      Word8
d | Word8
d Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_parenleft     -> Bool -> Conduit ByteString m Bool
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield Bool
True
      Word8
d | Word8
d Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_parenright    -> Bool -> Conduit ByteString m Bool
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield Bool
False
      Word8
d | Word8
d Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_t             -> Bool -> Conduit ByteString m Bool
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield Bool
True Conduit ByteString m Bool
-> Conduit ByteString m Bool -> Conduit ByteString m Bool
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> Conduit ByteString m Bool
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield Bool
False
      Word8
d | Word8
d Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_f             -> Bool -> Conduit ByteString m Bool
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield Bool
True Conduit ByteString m Bool
-> Conduit ByteString m Bool -> Conduit ByteString m Bool
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> Conduit ByteString m Bool
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield Bool
False
      Word8
d | Word8
d Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_1             -> Bool -> Conduit ByteString m Bool
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield Bool
True Conduit ByteString m Bool
-> Conduit ByteString m Bool -> Conduit ByteString m Bool
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> Conduit ByteString m Bool
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield Bool
False
      Word8
d | Word8
d Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_n             -> Bool -> Conduit ByteString m Bool
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield Bool
True Conduit ByteString m Bool
-> Conduit ByteString m Bool -> Conduit ByteString m Bool
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> Conduit ByteString m Bool
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield Bool
False
      Word8
_                       -> () -> Conduit ByteString m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    ByteString -> Conduit ByteString m Bool
forall (m :: * -> *).
Monad m =>
ByteString -> Conduit ByteString m Bool
blankedJsonToBalancedParens' ByteString
cs
  Maybe (Word8, ByteString)
Nothing -> () -> Conduit ByteString m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return ()

compressWordAsBit :: Monad m => Conduit BS.ByteString m BS.ByteString
compressWordAsBit :: Conduit ByteString m ByteString
compressWordAsBit = do
  Maybe ByteString
mbs <- ConduitT ByteString ByteString m (Maybe ByteString)
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await
  case Maybe ByteString
mbs of
    Just ByteString
bs -> do
      let (ByteString
cs, Maybe ByteString
_) = Int
-> (ByteString -> Maybe (Word8, ByteString))
-> ByteString
-> (ByteString, Maybe ByteString)
forall a.
Int -> (a -> Maybe (Word8, a)) -> a -> (ByteString, Maybe a)
BS.unfoldrN (ByteString -> Int
BS.length ByteString
bs Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
7 Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
8) ByteString -> Maybe (Word8, ByteString)
gen ByteString
bs
      ByteString -> Conduit ByteString m ByteString
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield ByteString
cs
    Maybe ByteString
Nothing -> () -> Conduit ByteString m ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  where gen :: ByteString -> Maybe (Word8, ByteString)
        gen :: ByteString -> Maybe (Word8, ByteString)
gen ByteString
xs = if ByteString -> Int
BS.length ByteString
xs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
          then Maybe (Word8, ByteString)
forall a. Maybe a
Nothing
          else (Word8, ByteString) -> Maybe (Word8, ByteString)
forall a. a -> Maybe a
Just ( (Word8 -> Word8 -> Word8) -> Word8 -> ByteString -> Word8
forall a. (Word8 -> a -> a) -> a -> ByteString -> a
BS.foldr (\Word8
b Word8
m -> ((Word8
b Word8 -> Word8 -> Word8
forall a. BitWise a => a -> a -> a
.&. Word8
1) Word8 -> Word8 -> Word8
forall a. BitWise a => a -> a -> a
.|. (Word8
m Word8 -> Count -> Word8
forall a. Shift a => a -> Count -> a
.<. Count
1))) Word8
0 (Word8 -> Int -> ByteString -> ByteString
padRight Word8
0 Int
8 (Int -> ByteString -> ByteString
BS.take Int
8 ByteString
xs))
                    , Int -> ByteString -> ByteString
BS.drop Int
8 ByteString
xs
                    )

blankedJsonToBalancedParens2 :: Monad m => Conduit BS.ByteString m BS.ByteString
blankedJsonToBalancedParens2 :: Conduit ByteString m ByteString
blankedJsonToBalancedParens2 = do
  Maybe ByteString
mbs <- ConduitT ByteString ByteString m (Maybe ByteString)
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await
  case Maybe ByteString
mbs of
    Just ByteString
bs -> do
      let (ByteString
cs, Maybe (Maybe Bool, ByteString)
_) = Int
-> ((Maybe Bool, ByteString)
    -> Maybe (Word8, (Maybe Bool, ByteString)))
-> (Maybe Bool, ByteString)
-> (ByteString, Maybe (Maybe Bool, ByteString))
forall a.
Int -> (a -> Maybe (Word8, a)) -> a -> (ByteString, Maybe a)
BS.unfoldrN (ByteString -> Int
BS.length ByteString
bs Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2) (Maybe Bool, ByteString) -> Maybe (Word8, (Maybe Bool, ByteString))
gen (Maybe Bool
forall a. Maybe a
Nothing, ByteString
bs)
      ByteString -> Conduit ByteString m ByteString
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield ByteString
cs
    Maybe ByteString
Nothing -> () -> Conduit ByteString m ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  where gen :: (Maybe Bool, ByteString) -> Maybe (Word8, (Maybe Bool, ByteString))
        gen :: (Maybe Bool, ByteString) -> Maybe (Word8, (Maybe Bool, ByteString))
gen (Just Bool
True  , ByteString
bs) = (Word8, (Maybe Bool, ByteString))
-> Maybe (Word8, (Maybe Bool, ByteString))
forall a. a -> Maybe a
Just (Word8
0xFF, (Maybe Bool
forall a. Maybe a
Nothing, ByteString
bs))
        gen (Just Bool
False , ByteString
bs) = (Word8, (Maybe Bool, ByteString))
-> Maybe (Word8, (Maybe Bool, ByteString))
forall a. a -> Maybe a
Just (Word8
0x00, (Maybe Bool
forall a. Maybe a
Nothing, ByteString
bs))
        gen (Maybe Bool
Nothing    , ByteString
bs) = case ByteString -> Maybe (Word8, ByteString)
BS.uncons ByteString
bs of
          Just (Word8
c, ByteString
cs) -> case Word8 -> MiniBP
balancedParensOf Word8
c of
            MiniBP
MiniN   -> (Maybe Bool, ByteString) -> Maybe (Word8, (Maybe Bool, ByteString))
gen        (Maybe Bool
forall a. Maybe a
Nothing    , ByteString
cs)
            MiniBP
MiniT   -> (Word8, (Maybe Bool, ByteString))
-> Maybe (Word8, (Maybe Bool, ByteString))
forall a. a -> Maybe a
Just (Word8
0xFF, (Maybe Bool
forall a. Maybe a
Nothing    , ByteString
cs))
            MiniBP
MiniF   -> (Word8, (Maybe Bool, ByteString))
-> Maybe (Word8, (Maybe Bool, ByteString))
forall a. a -> Maybe a
Just (Word8
0x00, (Maybe Bool
forall a. Maybe a
Nothing    , ByteString
cs))
            MiniBP
MiniTF  -> (Word8, (Maybe Bool, ByteString))
-> Maybe (Word8, (Maybe Bool, ByteString))
forall a. a -> Maybe a
Just (Word8
0xFF, (Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False , ByteString
cs))
          Maybe (Word8, ByteString)
Nothing   -> Maybe (Word8, (Maybe Bool, ByteString))
forall a. Maybe a
Nothing

data MiniBP = MiniN | MiniT | MiniF | MiniTF

balancedParensOf :: Word8 -> MiniBP
balancedParensOf :: Word8 -> MiniBP
balancedParensOf Word8
c = case Word8
c of
    Word8
d | Word8
d Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_braceleft     -> MiniBP
MiniT
    Word8
d | Word8
d Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_braceright    -> MiniBP
MiniF
    Word8
d | Word8
d Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_bracketleft   -> MiniBP
MiniT
    Word8
d | Word8
d Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_bracketright  -> MiniBP
MiniF
    Word8
d | Word8
d Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_parenleft     -> MiniBP
MiniT
    Word8
d | Word8
d Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_parenright    -> MiniBP
MiniF
    Word8
d | Word8
d Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_t             -> MiniBP
MiniTF
    Word8
d | Word8
d Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_f             -> MiniBP
MiniTF
    Word8
d | Word8
d Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_1             -> MiniBP
MiniTF
    Word8
d | Word8
d Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_n             -> MiniBP
MiniTF
    Word8
_                       -> MiniBP
MiniN

yieldBitsOfWord8 :: Monad m => Word8 -> Conduit BS.ByteString m Bool
yieldBitsOfWord8 :: Word8 -> Conduit ByteString m Bool
yieldBitsOfWord8 Word8
w = do
  Bool -> Conduit ByteString m Bool
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield ((Word8
w Word8 -> Word8 -> Word8
forall a. BitWise a => a -> a -> a
.&. Int -> Word8
forall a. Bits a => Int -> a
BITS.bit Int
0) Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
0)
  Bool -> Conduit ByteString m Bool
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield ((Word8
w Word8 -> Word8 -> Word8
forall a. BitWise a => a -> a -> a
.&. Int -> Word8
forall a. Bits a => Int -> a
BITS.bit Int
1) Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
0)
  Bool -> Conduit ByteString m Bool
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield ((Word8
w Word8 -> Word8 -> Word8
forall a. BitWise a => a -> a -> a
.&. Int -> Word8
forall a. Bits a => Int -> a
BITS.bit Int
2) Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
0)
  Bool -> Conduit ByteString m Bool
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield ((Word8
w Word8 -> Word8 -> Word8
forall a. BitWise a => a -> a -> a
.&. Int -> Word8
forall a. Bits a => Int -> a
BITS.bit Int
3) Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
0)
  Bool -> Conduit ByteString m Bool
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield ((Word8
w Word8 -> Word8 -> Word8
forall a. BitWise a => a -> a -> a
.&. Int -> Word8
forall a. Bits a => Int -> a
BITS.bit Int
4) Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
0)
  Bool -> Conduit ByteString m Bool
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield ((Word8
w Word8 -> Word8 -> Word8
forall a. BitWise a => a -> a -> a
.&. Int -> Word8
forall a. Bits a => Int -> a
BITS.bit Int
5) Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
0)
  Bool -> Conduit ByteString m Bool
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield ((Word8
w Word8 -> Word8 -> Word8
forall a. BitWise a => a -> a -> a
.&. Int -> Word8
forall a. Bits a => Int -> a
BITS.bit Int
6) Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
0)
  Bool -> Conduit ByteString m Bool
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield ((Word8
w Word8 -> Word8 -> Word8
forall a. BitWise a => a -> a -> a
.&. Int -> Word8
forall a. Bits a => Int -> a
BITS.bit Int
7) Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
0)

yieldBitsofWord8s :: Monad m => [Word8] -> Conduit BS.ByteString m Bool
yieldBitsofWord8s :: [Word8] -> Conduit ByteString m Bool
yieldBitsofWord8s = (Word8 -> Conduit ByteString m Bool -> Conduit ByteString m Bool)
-> Conduit ByteString m Bool
-> [Word8]
-> Conduit ByteString m Bool
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
P.foldr (Conduit ByteString m Bool
-> Conduit ByteString m Bool -> Conduit ByteString m Bool
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
(>>) (Conduit ByteString m Bool
 -> Conduit ByteString m Bool -> Conduit ByteString m Bool)
-> (Word8 -> Conduit ByteString m Bool)
-> Word8
-> Conduit ByteString m Bool
-> Conduit ByteString m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Conduit ByteString m Bool
forall (m :: * -> *). Monad m => Word8 -> Conduit ByteString m Bool
yieldBitsOfWord8) (() -> Conduit ByteString m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return ())

byteStringToBits :: Monad m => Conduit BS.ByteString m Bool
byteStringToBits :: Conduit ByteString m Bool
byteStringToBits = do
  Maybe ByteString
mbs <- ConduitT ByteString Bool m (Maybe ByteString)
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await
  case Maybe ByteString
mbs of
    Just ByteString
bs -> [Word8] -> Conduit ByteString m Bool
forall (m :: * -> *).
Monad m =>
[Word8] -> Conduit ByteString m Bool
yieldBitsofWord8s (ByteString -> [Word8]
BS.unpack ByteString
bs) Conduit ByteString m Bool
-> Conduit ByteString m Bool -> Conduit ByteString m Bool
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Conduit ByteString m Bool
forall (m :: * -> *). Monad m => Conduit ByteString m Bool
byteStringToBits
    Maybe ByteString
Nothing -> () -> Conduit ByteString m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return ()