on master help sign in
Add skipBits and alignByte
08842adSylvain HENRY
Data/Binary/Bits/Get.hs+19
@@ -64,6 +64,10 @@ module Data.Binary.Bits.Get
64 , getWord32be
64 , getWord32be
65 , getWord64be
65 , getWord64be
66
66
67+ -- ** Skip bits
68+ , skipBits
69+ , alignByte
70+
67 -- * Blocks
71 -- * Blocks
68
72
69 -- $blocks
73 -- $blocks
@@ -93,6 +97,7 @@ import Data.ByteString.Unsafe
93import Data.Bits
97import Data.Bits
94import Data.Word
98import Data.Word
95import Control.Applicative
99import Control.Applicative
100+import Control.Monad (when)
96
101
97import Prelude as P
102import Prelude as P
98
103
@@ -392,6 +397,20 @@ ensureBits n = do
392 put B.empty
397 put B.empty
393 return (S (bs`append`bs') o, ())
398 return (S (bs`append`bs') o, ())
394
399
400+-- | Skip the given number of bits
401+skipBits :: Int -> BitGet ()
402+skipBits n = do
403+ ensureBits n
404+ s <- getState
405+ putState $! (incS n s)
406+
407+-- | Skip bits if necessary to align on a byte
408+alignByte :: BitGet ()
409+alignByte = do
410+ (S _ n) <- getState
411+ when (n /= 0) $
412+ skipBits (8-n)
413+
395-- | Get 1 bit as a 'Bool'.
414-- | Get 1 bit as a 'Bool'.
396getBool :: BitGet Bool
415getBool :: BitGet Bool
397getBool = block bool
416getBool = block bool