on master help sign in
Make the behaviour of splitMany similar to that of parseMany
f261f02Adithya Kumar
src/Streamly/Internal/Data/Parser/ParserD/Type.hs+1
@@ -646,6 +646,7 @@ splitMany (Parser step1 initial1 extract1) (Fold fstep finitial fextract) =
646 xs <- fextract fs
646 xs <- fextract fs
647 return $ Done cnt1 xs
647 return $ Done cnt1 xs
648
648
649+ extract (Tuple3' _ 0 fs) = fextract fs
649 -- XXX The "try" may impact performance if this parser is used as a scan
650 -- XXX The "try" may impact performance if this parser is used as a scan
650 extract (Tuple3' s _ fs) = do
651 extract (Tuple3' s _ fs) = do
651 r <- try $ extract1 s
652 r <- try $ extract1 s
test/Streamly/Test/Data/Parser.hs+16−1
@@ -403,7 +403,7 @@ wordBy =
403 parser = P.many (P.wordBy predicate FL.toList) FL.toList
403 parser = P.many (P.wordBy predicate FL.toList) FL.toList
404 words' lst =
404 words' lst =
405 let wrds = words lst
405 let wrds = words lst
406− in if wrds == [] then [""] else wrds
406+ in if wrds == [] && length lst > 0 then [""] else wrds
407
407
408-- splitWithPass :: Property
408-- splitWithPass :: Property
409-- splitWithPass =
409-- splitWithPass =
@@ -666,6 +666,20 @@ parseMany2Events =
666 }
666 }
667 in listEquals (==) xs (replicate 2 ev)
667 in listEquals (==) xs (replicate 2 ev)
668
668
669+manyEqParseMany :: Property
670+manyEqParseMany =
671+ forAll (listOf (chooseInt (0, 100))) $ \lst ->
672+ forAll (chooseInt (1, 100)) $ \i ->
673+ monadicIO $ do
674+ let strm = S.fromList lst
675+ r1 <- run $ S.parse (P.many (split i) FL.toList) strm
676+ r2 <- run $ S.toList $ S.parseMany (split i) strm
677+ assert $ r1 == r2
678+
679+ where
680+
681+ split i = P.fromFold (FL.take i FL.toList)
682+
669-------------------------------------------------------------------------------
683-------------------------------------------------------------------------------
670-- Main
684-- Main
671-------------------------------------------------------------------------------
685-------------------------------------------------------------------------------
@@ -741,4 +755,5 @@ main =
741 prop ("P.some concatFold $ P.takeEndBy_ (== 1) FL.toList ="
755 prop ("P.some concatFold $ P.takeEndBy_ (== 1) FL.toList ="
742 ++ "Prelude.filter (== 0)") some
756 ++ "Prelude.filter (== 0)") some
743 -- prop "fail due to parser being die" someFail
757 -- prop "fail due to parser being die" someFail
758+ prop "P.many == S.parseMany" manyEqParseMany
744 takeProperties
759 takeProperties
test/Streamly/Test/Data/Parser/ParserD.hs+1−1
@@ -380,7 +380,7 @@ wordBy =
380 parser = P.many (P.wordBy predicate FL.toList) FL.toList
380 parser = P.many (P.wordBy predicate FL.toList) FL.toList
381 words' lst =
381 words' lst =
382 let wrds = words lst
382 let wrds = words lst
383− in if wrds == [] then [""] else wrds
383+ in if wrds == [] && length lst > 0 then [""] else wrds
384
384
385
385
386serialWith :: Property
386serialWith :: Property