@@ -1,6 +1,7 @@
1module BitsPut
1module BitsPut
3 , runBitPut
3 , runBitPut
@@ -12,6 +13,8 @@ module BitsPut
12import qualified Data.Binary.Builder as B
13import qualified Data.Binary.Builder as B
13import Data.Binary.Builder ( Builder )
14import Data.Binary.Builder ( Builder )
14import qualified Data.Binary.Put as Put
15import qualified Data.Binary.Put as Put
16+import Data.Binary.Put ( Put )
15import Data.ByteString
18import Data.ByteString
17import Data.Bits
20import Data.Bits
@@ -74,18 +77,28 @@ putWord64be :: Int -> Word64 -> BitPut ()
74putWord64be n w
77putWord64be n w
75 | n <= 16 = putWord16be n (fromIntegral w)
78 | n <= 16 = putWord16be n (fromIntegral w)
80+joinPut :: Put -> BitPut ()
81+joinPut m = BitPut $ \s0 -> PairS () $
82+ let (S b0 _ _) = flushIncomplete s0
84+ in (S (b0`mappend`b) 0 0)
77flush :: S -> S
86flush :: S -> S
78flush s@(S b w o)
87flush s@(S b w o)
79 | o > 8 = error "flush: offset > 8"
88 | o > 8 = error "flush: offset > 8"
80 | o == 8 = S (b `mappend` B.singleton w) 0 0
89 | o == 8 = S (b `mappend` B.singleton w) 0 0
81 | otherwise = s
90 | otherwise = s
92+flushIncomplete :: S -> S
93+flushIncomplete s@(S b w o)
95+ | otherwise = (S (b `mappend` B.singleton w) 0 0)
83runBitPut :: BitPut () -> Put.Put
97runBitPut :: BitPut () -> Put.Put
84runBitPut m = Put.putBuilder b'
98runBitPut m = Put.putBuilder b'
86− PairS _ (S b t o) = run m (S mempty 0 0)
100+ PairS _ s@(S b t o) = run m (S mempty 0 0)
87− b' | o == 0 = b
101+ (S b' _ _) = flushIncomplete s
88− | otherwise = b `mappend` B.singleton t
90instance Monad BitPut where
103instance Monad BitPut where
91 m >>= k = BitPut $ \s ->
104 m >>= k = BitPut $ \s ->