on master help sign in
Implement naive way to putByteString
commit52aa5f17b117b9a9b2dc887368c294fd0e1e8a55 authorLennart Kolmodin <kolmodin@gmail.com> dateSun 5 Jun 2011, 14:56 (15 years ago) parentf723509
1 file changed+10
Data/Binary/Bits/Put.hs+10
Data/Binary/Bits/Put.hs+10
@@ -8,6 +8,7 @@ module Data.Binary.Bits.Put
88 , putWord16be
99 , putWord32be
1010 , putWord64be
11+ , putByteString
1112 )
1213 where
1314
@@ -84,6 +85,15 @@ putWord64be n w
8485 putWord64be (n-32) (w`shiftR`32)
8586 putWord64be 32 (w .&. 0xffffffff)
8687
88+putByteString :: ByteString -> BitPut ()
89+putByteString bs = do
90+ offset <- hasOffset
91+ if offset
92+ then mapM_ (putWord8 8) (unpack bs) -- naive
93+ else joinPut (Put.putByteString bs)
94+ where
95+ hasOffset = BitPut $ \ s@(S _ _ o) -> PairS (o /= 0) s
96+
8797joinPut :: Put -> BitPut ()
8898joinPut m = BitPut $ \s0 -> PairS () $
8999 let (S b0 _ _) = flushIncomplete s0