{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeSynonymInstances #-}

-- |Utilities to represent and display bit sequences
module Flat.Bits (
    Bits,
    toBools,
    fromBools,
    bits,
    paddedBits,
    asBytes,
    asBits,
) where

import Data.Bits (FiniteBits (finiteBitSize), testBit)
import qualified Data.ByteString as B
import qualified Data.Vector.Unboxed as V
import Data.Word (Word8)
import Flat.Class (Flat)
import Flat.Decoder (Decoded)
import Flat.Filler (PostAligned (PostAligned), fillerLength)
import Flat.Run (flat, unflatRaw)
import Text.PrettyPrint.HughesPJClass (
    Doc,
    Pretty (pPrint),
    hsep,
    text,
 )

-- |A sequence of bits
type Bits = V.Vector Bool

toBools :: Bits -> [Bool]
toBools :: Bits -> [Bool]
toBools = Bits -> [Bool]
forall a. Unbox a => Vector a -> [a]
V.toList

fromBools :: [Bool] -> Bits
fromBools :: [Bool] -> Bits
fromBools = [Bool] -> Bits
forall a. Unbox a => [a] -> Vector a
V.fromList

{- $setup
>>> import Data.Word
>>> import Flat.Instances.Base
>>> import Flat.Instances.Test(tst)
-}

{- |The sequence of bits corresponding to the serialization of the passed value (without any final byte padding)

>>> bits True
[True]
-}
bits :: forall a. Flat a => a -> Bits
bits :: a -> Bits
bits a
v =
    let lbs :: ByteString
lbs = a -> ByteString
forall a. Flat a => a -> ByteString
flat a
v
        Right (PostAligned a
_ Filler
f) = ByteString -> Either DecodeException (PostAligned a)
forall a b. (Flat a, AsByteString b) => b -> Decoded a
unflatRaw ByteString
lbs :: Decoded (PostAligned a)
     in Int -> ByteString -> Bits
takeBits (Int
8 Int -> Int -> Int
forall a. Num a => a -> a -> a
* ByteString -> Int
B.length ByteString
lbs Int -> Int -> Int
forall a. Num a => a -> a -> a
- Filler -> Int
forall a. Num a => Filler -> a
fillerLength Filler
f) ByteString
lbs

{- |The sequence of bits corresponding to the byte-padded serialization of the passed value

>>> paddedBits True
[True,False,False,False,False,False,False,True]
-}
paddedBits :: forall a. Flat a => a -> Bits
paddedBits :: a -> Bits
paddedBits a
v = let lbs :: ByteString
lbs = a -> ByteString
forall a. Flat a => a -> ByteString
flat a
v in Int -> ByteString -> Bits
takeBits (Int
8 Int -> Int -> Int
forall a. Num a => a -> a -> a
* ByteString -> Int
B.length ByteString
lbs) ByteString
lbs

takeBits :: Int -> B.ByteString -> Bits
takeBits :: Int -> ByteString -> Bits
takeBits Int
numBits ByteString
lbs =
    Int -> (Int -> Bool) -> Bits
forall a. Unbox a => Int -> (Int -> a) -> Vector a
V.generate
        (Int -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
numBits)
        ( \Int
n ->
            let (Int
bb, Int
b) = Int
n Int -> Int -> (Int, Int)
forall a. Integral a => a -> a -> (a, a)
`divMod` Int
8
             in Word8 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit (ByteString -> Int -> Word8
B.index ByteString
lbs (Int -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
bb)) (Int
7 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
b)
        )

{- |Convert an integral value to its equivalent bit representation

>>> asBits (5::Word8)
[False,False,False,False,False,True,False,True]
-}
asBits :: FiniteBits a => a -> Bits
asBits :: a -> Bits
asBits a
w = let s :: Int
s = a -> Int
forall b. FiniteBits b => b -> Int
finiteBitSize a
w in Int -> (Int -> Bool) -> Bits
forall a. Unbox a => Int -> (Int -> a) -> Vector a
V.generate Int
s (a -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit a
w (Int -> Bool) -> (Int -> Int) -> Int -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int
s Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
-))

{- |Convert a sequence of bits to the corresponding list of bytes

>>> asBytes $ asBits (256+3::Word16)
[1,3]
-}
asBytes :: Bits -> [Word8]
asBytes :: Bits -> [Word8]
asBytes = ([Bool] -> Word8) -> [[Bool]] -> [Word8]
forall a b. (a -> b) -> [a] -> [b]
map [Bool] -> Word8
byteVal ([[Bool]] -> [Word8]) -> (Bits -> [[Bool]]) -> Bits -> [Word8]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Bool] -> [[Bool]]
forall t. [t] -> [[t]]
bytes ([Bool] -> [[Bool]]) -> (Bits -> [Bool]) -> Bits -> [[Bool]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bits -> [Bool]
forall a. Unbox a => Vector a -> [a]
V.toList

-- |Convert to the corresponding value (most significant bit first)
byteVal :: [Bool] -> Word8
byteVal :: [Bool] -> Word8
byteVal =
    [Word8] -> Word8
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([Word8] -> Word8) -> ([Bool] -> [Word8]) -> [Bool] -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Word8, Bool) -> Word8) -> [(Word8, Bool)] -> [Word8]
forall a b. (a -> b) -> [a] -> [b]
map (\(Word8
e, Bool
b) -> if Bool
b then Word8
e else Word8
0)
        ([(Word8, Bool)] -> [Word8])
-> ([Bool] -> [(Word8, Bool)]) -> [Bool] -> [Word8]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Word8] -> [Bool] -> [(Word8, Bool)]
forall a b. [a] -> [b] -> [(a, b)]
zip
            [Word8
2 Word8 -> Int -> Word8
forall a b. (Num a, Integral b) => a -> b -> a
^ Int
n | Int
n <- [Int
7 :: Int, Int
6 .. Int
0]]

-- |Split a list in groups of 8 elements or less
bytes :: [t] -> [[t]]
bytes :: [t] -> [[t]]
bytes [] = []
bytes [t]
l = let ([t]
w, [t]
r) = Int -> [t] -> ([t], [t])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
8 [t]
l in [t]
w [t] -> [[t]] -> [[t]]
forall a. a -> [a] -> [a]
: [t] -> [[t]]
forall t. [t] -> [[t]]
bytes [t]
r

{- |
>>> prettyShow $ asBits (256+3::Word16)
"00000001 00000011"
-}
instance Pretty Bits where
    pPrint :: Bits -> Doc
pPrint = [Doc] -> Doc
hsep ([Doc] -> Doc) -> (Bits -> [Doc]) -> Bits -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Bool] -> Doc) -> [[Bool]] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map [Bool] -> Doc
forall (t :: * -> *). Foldable t => t Bool -> Doc
prettyBits ([[Bool]] -> [Doc]) -> (Bits -> [[Bool]]) -> Bits -> [Doc]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Bool] -> [[Bool]]
forall t. [t] -> [[t]]
bytes ([Bool] -> [[Bool]]) -> (Bits -> [Bool]) -> Bits -> [[Bool]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bits -> [Bool]
forall a. Unbox a => Vector a -> [a]
V.toList

prettyBits :: Foldable t => t Bool -> Doc
prettyBits :: t Bool -> Doc
prettyBits t Bool
l =
    String -> Doc
text (String -> Doc) -> (t Bool -> String) -> t Bool -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String -> String
forall a. Int -> [a] -> [a]
take (t Bool -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length t Bool
l) (String -> String) -> (t Bool -> String) -> t Bool -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Bool -> String) -> t Bool -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\Bool
b -> if Bool
b then String
"1" else String
"0") (t Bool -> Doc) -> t Bool -> Doc
forall a b. (a -> b) -> a -> b
$ t Bool
l