on master help sign in
Make getLazyByteString read from current byte
commitdc3e9095a49bd8ecad250a8fea96ff2975c46f88 authorAlexander Polakov <plhk@sdf.org> dateFri 29 Mar 2013, 00:15 (14 years ago) parent50e2ce0
2 files changed+27−4
BitsQC.hs+18 Data/Binary/Bits/Get.hs+9−4
BitsQC.hs+18
@@ -43,6 +43,13 @@ tests =
4343 , testGroup "getByteString"
4444 [ testProperty "prop_getByteString_negative" prop_getByteString_negative ]
4545
46+ , testGroup "getLazyByteString"
47+ [ testProperty "getLazyByteString == getByteString"
48+ prop_getLazyByteString_equal_to_ByteString
49+ , testProperty "getLazyByteString == getByteString (with shift)"
50+ prop_getLazyByteString_equal_to_ByteString2
51+ ]
52+
4653 , testGroup "Fail"
4754 [ testProperty "monadic fail" prop_fail ]
4855
@@ -110,6 +117,17 @@ tests =
110117 ]
111118 ]
112119
120+prop_getLazyByteString_equal_to_ByteString :: L.ByteString -> Int -> Property
121+prop_getLazyByteString_equal_to_ByteString bs n =
122+ (fromIntegral n) <= L.length bs ==>
123+ runGet (runBitGet (getLazyByteString (fromIntegral n))) bs ==
124+ (L.fromChunks . (:[]) $ runGet (runBitGet (getByteString n)) bs)
125+
126+prop_getLazyByteString_equal_to_ByteString2 :: L.ByteString -> Int -> Property
127+prop_getLazyByteString_equal_to_ByteString2 bs n =
128+ (L.length bs > 1) && (fromIntegral n) < L.length bs ==>
129+ runGet (runBitGet (getWord8 2 >> getLazyByteString (fromIntegral n))) bs ==
130+ (L.fromChunks . (:[]) $ runGet (runBitGet (getWord8 2 >> getByteString n)) bs)
113131
114132prop_getByteString_negative :: Int -> Property
115133prop_getByteString_negative n =
Data/Binary/Bits/Get.hs+9−4
@@ -416,11 +416,16 @@ getWord64be n = block (word64be n)
416416getByteString :: Int -> BitGet ByteString
417417getByteString n = block (byteString n)
418418
419+-- | Get @n@ bytes as a lazy ByteString.
419420getLazyByteString :: Int -> BitGet L.ByteString
420−getLazyByteString m = B $ \ (S n bs) -> do
421− putBackState n bs
422− lbs <- B.getLazyByteString (fromIntegral m)
423− return (S B.empty 0, lbs)
421+getLazyByteString n = do
422+ (S _ o) <- getState
423+ case o of
424+ 0 -> B $ \ (S bs o') -> do
425+ putBackState bs o'
426+ lbs <- B.getLazyByteString (fromIntegral n)
427+ return (S B.empty 0, lbs)
428+ _ -> L.fromChunks . (:[]) <$> Data.Binary.Bits.Get.getByteString n
424429
425430-- | Read a 1 bit 'Bool'.
426431bool :: Block Bool