@@ -6,7 +6,7 @@ import Data.Char (chr, ord)
66import Data.Int (Int64)
77import Data.Word (Word8)
88import Prelude hiding (take, takeWhile)
9−import QC.Common ()
9+import QC.Common (parseBS, toStrictBS)
1010import Test.Framework (Test)
1111import Test.Framework.Providers.QuickCheck2 (testProperty)
1212import Test.QuickCheck
@@ -19,22 +19,19 @@ import qualified Data.ByteString.Lazy.Char8 as L8
1919
2020-- Basic byte-level combinators.
2121
22−maybeP :: PL.Parser r -> L.ByteString -> Maybe r
23−maybeP p = PL.maybeResult . PL.parse p
24−
2522satisfy :: Word8 -> L.ByteString -> Bool
26−satisfy w s = maybeP (P.satisfy (<=w)) (L.cons w s) == Just w
23+satisfy w s = parseBS (P.satisfy (<=w)) (L.cons w s) == Just w
2724
2825satisfyWith :: Char -> L.ByteString -> Bool
29−satisfyWith c s = maybeP (P.satisfyWith (chr . fromIntegral) (<=c))
26+satisfyWith c s = parseBS (P.satisfyWith (chr . fromIntegral) (<=c))
3027 (L.cons (fromIntegral (ord c)) s) == Just c
3128
3229word8 :: Word8 -> L.ByteString -> Bool
33−word8 w s = maybeP (P.word8 w) (L.cons w s) == Just w
30+word8 w s = parseBS (P.word8 w) (L.cons w s) == Just w
3431
3532skip :: Word8 -> L.ByteString -> Bool
3633skip w s =
37− case (maybeP (P.skip (<w)) s, L.uncons s) of
34+ case (parseBS (P.skip (<w)) s, L.uncons s) of
3835 (Nothing, mcs) -> maybe True (not . it) mcs
3936 (Just _, mcs) -> maybe False it mcs
4037 where it cs = fst cs < w
@@ -43,10 +40,10 @@ anyWord8 :: L.ByteString -> Bool
4340anyWord8 s
4441 | L.null s = p == Nothing
4542 | otherwise = p == Just (L.head s)
46− where p = maybeP P.anyWord8 s
43+ where p = parseBS P.anyWord8 s
4744
4845notWord8 :: Word8 -> NonEmptyList Word8 -> Bool
49−notWord8 w (NonEmpty s) = maybeP (P.notWord8 w) bs == if v == w
46+notWord8 w (NonEmpty s) = parseBS (P.notWord8 w) bs == if v == w
5047 then Nothing
5148 else Just v
5249 where v = L.head bs
@@ -56,17 +53,14 @@ peekWord8 :: L.ByteString -> Bool
5653peekWord8 s
5754 | L.null s = p == Just (Nothing, s)
5855 | otherwise = p == Just (Just (L.head s), s)
59− where p = maybeP ((,) <$> P.peekWord8 <*> P.takeLazyByteString) s
56+ where p = parseBS ((,) <$> P.peekWord8 <*> P.takeLazyByteString) s
6057
6158peekWord8' :: L.ByteString -> Bool
62−peekWord8' s = maybeP P.peekWord8' s == (fst <$> L.uncons s)
59+peekWord8' s = parseBS P.peekWord8' s == (fst <$> L.uncons s)
6360
6461string :: L.ByteString -> L.ByteString -> Bool
65−string s t = maybeP (P.string s') (s `L.append` t) == Just s'
66− where s' = toStrict s
67−
68−toStrict :: L.ByteString -> B.ByteString
69−toStrict = B.concat . L.toChunks
62+string s t = parseBS (P.string s') (s `L.append` t) == Just s'
63+ where s' = toStrictBS s
7064
7165skipWhile :: Word8 -> L.ByteString -> Bool
7266skipWhile w s =
@@ -77,7 +71,7 @@ skipWhile w s =
7771
7872takeCount :: Positive Int -> L.ByteString -> Bool
7973takeCount (Positive k) s =
80− case maybeP (P.take k) s of
74+ case parseBS (P.take k) s of
8175 Nothing -> fromIntegral k > L.length s
8276 Just _s -> fromIntegral k <= L.length s
8377
@@ -85,51 +79,51 @@ takeWhile :: Word8 -> L.ByteString -> Bool
8579takeWhile w s =
8680 let (h,t) = L.span (==w) s
8781 in case PL.parse (P.takeWhile (==w)) s of
88− PL.Done t' h' -> t == t' && toStrict h == h'
82+ PL.Done t' h' -> t == t' && toStrictBS h == h'
8983 _ -> False
9084
9185take :: Int -> L.ByteString -> Bool
92−take n s = maybe (L.length s < fromIntegral n) (== B.take n (toStrict s)) $
93− maybeP (P.take n) s
86+take n s = maybe (L.length s < fromIntegral n) (== B.take n (toStrictBS s)) $
87+ parseBS (P.take n) s
9488
9589takeByteString :: L.ByteString -> Bool
96−takeByteString s = maybe False (== toStrict s) . maybeP P.takeByteString $ s
90+takeByteString s = maybe False (== toStrictBS s) . parseBS P.takeByteString $ s
9791
9892takeLazyByteString :: L.ByteString -> Bool
99−takeLazyByteString s = maybe False (== s) . maybeP P.takeLazyByteString $ s
93+takeLazyByteString s = maybe False (== s) . parseBS P.takeLazyByteString $ s
10094
10195takeWhile1 :: Word8 -> L.ByteString -> Bool
10296takeWhile1 w s =
10397 let s' = L.cons w s
10498 (h,t) = L.span (<=w) s'
10599 in case PL.parse (P.takeWhile1 (<=w)) s' of
106− PL.Done t' h' -> t == t' && toStrict h == h'
100+ PL.Done t' h' -> t == t' && toStrictBS h == h'
107101 _ -> False
108102
109103takeTill :: Word8 -> L.ByteString -> Bool
110104takeTill w s =
111105 let (h,t) = L.break (==w) s
112106 in case PL.parse (P.takeTill (==w)) s of
113− PL.Done t' h' -> t == t' && toStrict h == h'
107+ PL.Done t' h' -> t == t' && toStrictBS h == h'
114108 _ -> False
115109
116110takeWhile1_empty :: Bool
117−takeWhile1_empty = maybeP (P.takeWhile1 undefined) L.empty == Nothing
111+takeWhile1_empty = parseBS (P.takeWhile1 undefined) L.empty == Nothing
118112
119113endOfInput :: L.ByteString -> Bool
120−endOfInput s = maybeP P.endOfInput s == if L.null s
114+endOfInput s = parseBS P.endOfInput s == if L.null s
121115 then Just ()
122116 else Nothing
123117
124118endOfLine :: L.ByteString -> Bool
125119endOfLine s =
126− case (maybeP P8.endOfLine s, L8.uncons s) of
120+ case (parseBS P8.endOfLine s, L8.uncons s) of
127121 (Nothing, mcs) -> maybe True (not . eol) mcs
128122 (Just _, mcs) -> maybe False eol mcs
129123 where eol (c,s') = c == '\n' || (c, fst <$> L8.uncons s') == ('\r', Just '\n')
130124
131125scan :: L.ByteString -> Positive Int64 -> Bool
132−scan s (Positive k) = maybeP p s == (Just $ toStrict $ L.take k s)
126+scan s (Positive k) = parseBS p s == (Just $ toStrictBS $ L.take k s)
133127 where p = P.scan k $ \ n _ ->
134128 if n > 0 then let !n' = n - 1 in Just n' else Nothing
135129