on master help sign in
Initialize parser only if the stream is not empty

In parseMany on the next parse if we find that the stream has ended then the parse may fail on extraction, so we initialize the parser only if we see at least one element in the stream.

commit6a6be9dfca925949615387bb77586e9cc7357fcd authorHarendra Kumar <harendra@composewell.com> dateWed 2 Sep 2020, 13:17 (6 years ago) parentfbb1c84
1 file changed+9−4
src/Streamly/Internal/Data/Stream/StreamD.hs+9−4
src/Streamly/Internal/Data/Stream/StreamD.hs+9−4
@@ -1038,9 +1038,14 @@ parseMany (PRD.Parser pstep initial extract) (Stream step state) =
10381038 where
10391039
10401040 {-# INLINE_LATE stepOuter #-}
1041− -- Buffer is empty, go to stream processing loop
1042− stepOuter _ (ParseChunksInit [] st) = do
1043− initial >>= return . Skip . ParseChunksStream st []
1041+ -- Buffer is empty, get the first element from the stream, initialize the
1042+ -- fold and then go to stream processing loop.
1043+ stepOuter gst (ParseChunksInit [] st) = do
1044+ r <- step (adaptState gst) st
1045+ case r of
1046+ Yield x s -> initial >>= return . Skip . ParseChunksBuf [x] s []
1047+ Skip s -> return $ Skip $ ParseChunksInit [] s
1048+ Stop -> return Stop
10441049
10451050 -- Buffer is not empty, go to buffered processing loop
10461051 stepOuter _ (ParseChunksInit src st) = do
@@ -1049,7 +1054,7 @@ parseMany (PRD.Parser pstep initial extract) (Stream step state) =
10491054 -- XXX we just discard any leftover input at the end
10501055 stepOuter _ (ParseChunksInitLeftOver _) = return Stop
10511056
1052− -- Buffer is empty process elements from the stream
1057+ -- Buffer is empty, process elements from the stream
10531058 stepOuter gst (ParseChunksStream st buf pst) = do
10541059 r <- step (adaptState gst) st
10551060 case r of