mirror of
https://github.com/ilyakooo0/binary-bits.git
synced 2024-09-11 06:35:26 +03:00
Fix BitOrder naming coherence
This commit is contained in:
parent
858e615896
commit
1bfa8cbbdf
@ -19,9 +19,9 @@ where
|
||||
--
|
||||
-- E.g. two words of 5 bits: ABCDE, VWXYZ
|
||||
-- - BB: ABCDEVWX YZxxxxxx
|
||||
-- - LB: XYZABCDE xxxxxxVW
|
||||
-- - LL: XYZABCDE xxxxxxVW
|
||||
-- - BL: EDCBAZYX WVxxxxxx
|
||||
-- - LL: XWVEDCBA xxxxxxZY
|
||||
-- - LB: XWVEDCBA xxxxxxZY
|
||||
data BitOrder
|
||||
= BB -- ^ Big-endian bytes and bits
|
||||
| LB -- ^ Little-endian bytes, big-endian bits
|
||||
|
@ -179,8 +179,8 @@ readBool :: S -> Bool
|
||||
readBool (S bs o bo) = case bo of
|
||||
BB -> testBit (unsafeHead bs) (7-o)
|
||||
BL -> testBit (unsafeHead bs) (7-o)
|
||||
LB -> testBit (unsafeHead bs) o
|
||||
LL -> testBit (unsafeHead bs) o
|
||||
LB -> testBit (unsafeHead bs) o
|
||||
|
||||
-- | Extract a range of bits from (ws :: ByteString)
|
||||
--
|
||||
@ -203,17 +203,17 @@ extract bo bs o n
|
||||
|
||||
-- shift offset depending on the byte position (0..B.length-1)
|
||||
off i = case bo of
|
||||
LB -> 8*i - o
|
||||
LL -> 8*i - o
|
||||
LB -> 8*i - o
|
||||
BB -> (B.length bs -1 - i) * 8 - r
|
||||
BL -> (B.length bs -1 - i) * 8 - r
|
||||
|
||||
-- reverse bits if necessary
|
||||
rev = case bo of
|
||||
LL -> reverseBits n
|
||||
LB -> reverseBits n
|
||||
BL -> reverseBits n
|
||||
BB -> id
|
||||
LB -> id
|
||||
LL -> id
|
||||
|
||||
|
||||
-- | Generic readWord
|
||||
@ -234,9 +234,9 @@ readWordChecked m n s
|
||||
--
|
||||
-- Examples:
|
||||
-- BB: xxxABCDE FGHIJKLM NOPxxxxx -> ABCDEFGH IJKLMNOP
|
||||
-- LB: LMNOPxxx DEFGHIJK xxxxxABC -> ABCDEFGH IJKLMNOP
|
||||
-- LL: LMNOPxxx DEFGHIJK xxxxxABC -> ABCDEFGH IJKLMNOP
|
||||
-- BL: xxxPONML KJIHGFED CBAxxxxx -> ABCDEFGH IJKLMNOP
|
||||
-- LL: EDCBAxxx MLKJIHGF xxxxxPON -> ABCDEFGH IJKLMNOP
|
||||
-- LB: EDCBAxxx MLKJIHGF xxxxxPON -> ABCDEFGH IJKLMNOP
|
||||
readByteString :: Int -> S -> ByteString
|
||||
readByteString n (S bs o bo) =
|
||||
let
|
||||
@ -245,12 +245,12 @@ readByteString n (S bs o bo) =
|
||||
rev = B.map (reverseBits 8)
|
||||
in case (o,bo) of
|
||||
(0,BB) -> bs''
|
||||
(0,LB) -> B.reverse bs''
|
||||
(0,LL) -> rev bs''
|
||||
(0,LL) -> B.reverse bs''
|
||||
(0,LB) -> rev bs''
|
||||
(0,BL) -> rev . B.reverse $ bs''
|
||||
(_,LB) -> readByteString n (S (B.reverse bs') (8-o) BB)
|
||||
(_,LL) -> readByteString n (S (B.reverse bs') (8-o) BB)
|
||||
(_,BL) -> rev . B.reverse $ readByteString n (S bs' o BB)
|
||||
(_,LL) -> rev . B.reverse $ readByteString n (S bs' o LB)
|
||||
(_,LB) -> rev . B.reverse $ readByteString n (S bs' o LL)
|
||||
(_,BB) -> unsafePerformIO $ do
|
||||
let len = n+1
|
||||
ptr <- mallocBytes len
|
||||
|
@ -85,8 +85,8 @@ putWordS n w s@(S builder b o bo) = s'
|
||||
w' = case bo of
|
||||
BB -> w
|
||||
BL -> w `fastShiftR` cn
|
||||
LB -> w `fastShiftR` cn
|
||||
LL -> w
|
||||
LL -> w `fastShiftR` cn
|
||||
LB -> w
|
||||
|
||||
-- Select bits to store in the current byte.
|
||||
-- Put them in the correct order and return them in the least-significant
|
||||
@ -94,8 +94,8 @@ putWordS n w s@(S builder b o bo) = s'
|
||||
selectBits :: (Num a, FastBits a, Integral a) => a -> Word8
|
||||
selectBits x = fromIntegral $ case bo of
|
||||
BB -> mask cn $ x `fastShiftR` (n-cn)
|
||||
LL -> reverseBits cn $ mask cn $ x `fastShiftR` (n-cn)
|
||||
LB -> mask cn x
|
||||
LB -> reverseBits cn $ mask cn $ x `fastShiftR` (n-cn)
|
||||
LL -> mask cn x
|
||||
BL -> reverseBits cn $ mask cn x
|
||||
|
||||
-- shift left at the correct position
|
||||
@ -103,8 +103,8 @@ putWordS n w s@(S builder b o bo) = s'
|
||||
shl x = case bo of
|
||||
BB -> x `fastShiftL` (8-o-cn)
|
||||
BL -> x `fastShiftL` (8-o-cn)
|
||||
LB -> x `fastShiftL` o
|
||||
LL -> x `fastShiftL` o
|
||||
LB -> x `fastShiftL` o
|
||||
|
||||
flush s2@(S b2 w2 o2 bo2)
|
||||
| o2 == 8 = S (b2 `mappend` B.singleton w2) 0 0 bo2
|
||||
@ -143,9 +143,9 @@ putWord64be = putWord
|
||||
--
|
||||
-- Examples: 3 bits are already written in the current byte
|
||||
-- BB: ABCDEFGH IJKLMNOP -> xxxABCDE FGHIJKLM NOPxxxxx
|
||||
-- LB: ABCDEFGH IJKLMNOP -> LMNOPxxx DEFGHIJK xxxxxABC
|
||||
-- LL: ABCDEFGH IJKLMNOP -> LMNOPxxx DEFGHIJK xxxxxABC
|
||||
-- BL: ABCDEFGH IJKLMNOP -> xxxPONML KJIHGFED CBAxxxxx
|
||||
-- LL: ABCDEFGH IJKLMNOP -> EDCBAxxx MLKJIHGF xxxxxPON
|
||||
-- LB: ABCDEFGH IJKLMNOP -> EDCBAxxx MLKJIHGF xxxxxPON
|
||||
putByteString :: ByteString -> BitPut ()
|
||||
putByteString bs = BitPut $ \s -> PairS () (putByteStringS bs s)
|
||||
|
||||
@ -154,13 +154,13 @@ putByteStringS bs s
|
||||
| BS.null bs = s
|
||||
| otherwise = case s of
|
||||
(S builder b 0 BB) -> S (builder `mappend` B.fromByteString bs) b 0 BB
|
||||
(S builder b 0 LB) -> S (builder `mappend` B.fromByteString (BS.reverse bs)) b 0 LB
|
||||
(S builder b 0 LL) -> S (builder `mappend` B.fromByteString (rev bs)) b 0 LL
|
||||
(S builder b 0 LL) -> S (builder `mappend` B.fromByteString (BS.reverse bs)) b 0 LL
|
||||
(S builder b 0 LB) -> S (builder `mappend` B.fromByteString (rev bs)) b 0 LB
|
||||
(S builder b 0 BL) -> S (builder `mappend` B.fromByteString (rev (BS.reverse bs))) b 0 BL
|
||||
(S _ _ _ BB) -> putByteStringS (BS.unsafeTail bs) (putWordS 8 (BS.unsafeHead bs) s)
|
||||
(S _ _ _ LB) -> putByteStringS (BS.unsafeInit bs) (putWordS 8 (BS.unsafeLast bs) s)
|
||||
(S _ _ _ LL) -> putByteStringS (BS.unsafeInit bs) (putWordS 8 (BS.unsafeLast bs) s)
|
||||
(S _ _ _ BL) -> putByteStringS (BS.unsafeInit bs) (putWordS 8 (BS.unsafeLast bs) s)
|
||||
(S _ _ _ LL) -> putByteStringS (BS.unsafeTail bs) (putWordS 8 (BS.unsafeHead bs) s)
|
||||
(S _ _ _ LB) -> putByteStringS (BS.unsafeTail bs) (putWordS 8 (BS.unsafeHead bs) s)
|
||||
where
|
||||
rev = BS.map (reverseBits 8)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user