on master help sign in
Make the behaviour of splitMany similar to that of parseMany
commitf261f02d15671426f5773ad3c5bd0f910dbeebb6 authorAdithya Kumar <adithya@composewell.com> dateFri 12 Mar 2021, 04:21 (6 years ago) parentdc17317
3 files changed+18−2
src/Streamly/Internal/Data/Parser/ParserD/Type.hs+1 test/Streamly/Test/Data/Parser.hs+16−1 test/Streamly/Test/Data/Parser/ParserD.hs+1−1
src/Streamly/Internal/Data/Parser/ParserD/Type.hs+1
@@ -646,6 +646,7 @@ splitMany (Parser step1 initial1 extract1) (Fold fstep finitial fextract) =
646646 xs <- fextract fs
647647 return $ Done cnt1 xs
648648
649+ extract (Tuple3' _ 0 fs) = fextract fs
649650 -- XXX The "try" may impact performance if this parser is used as a scan
650651 extract (Tuple3' s _ fs) = do
651652 r <- try $ extract1 s
test/Streamly/Test/Data/Parser.hs+16−1
@@ -403,7 +403,7 @@ wordBy =
403403 parser = P.many (P.wordBy predicate FL.toList) FL.toList
404404 words' lst =
405405 let wrds = words lst
406− in if wrds == [] then [""] else wrds
406+ in if wrds == [] && length lst > 0 then [""] else wrds
407407
408408-- splitWithPass :: Property
409409-- splitWithPass =
@@ -666,6 +666,20 @@ parseMany2Events =
666666 }
667667 in listEquals (==) xs (replicate 2 ev)
668668
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+
669683-------------------------------------------------------------------------------
670684-- Main
671685-------------------------------------------------------------------------------
@@ -741,4 +755,5 @@ main =
741755 prop ("P.some concatFold $ P.takeEndBy_ (== 1) FL.toList ="
742756 ++ "Prelude.filter (== 0)") some
743757 -- prop "fail due to parser being die" someFail
758+ prop "P.many == S.parseMany" manyEqParseMany
744759 takeProperties
test/Streamly/Test/Data/Parser/ParserD.hs+1−1
@@ -380,7 +380,7 @@ wordBy =
380380 parser = P.many (P.wordBy predicate FL.toList) FL.toList
381381 words' lst =
382382 let wrds = words lst
383− in if wrds == [] then [""] else wrds
383+ in if wrds == [] && length lst > 0 then [""] else wrds
384384
385385
386386serialWith :: Property