@@ -43,7 +43,7 @@ module Streamly.Internal.Data.Parser.ParserD
43 -- takeWhileBetween cond m n p = takeWhile cond (takeBetween m n p)
43 -- takeWhileBetween cond m n p = takeWhile cond (takeBetween m n p)
45 -- Grab a sequence of input elements without inspecting them
45 -- Grab a sequence of input elements without inspecting them
46− -- , takeBetween
46+ , takeBetween
47 -- , takeLE -- take -- takeBetween 0 n
47 -- , takeLE -- take -- takeBetween 0 n
48 -- , takeLE1 -- take1 -- takeBetween 1 n
48 -- , takeLE1 -- take1 -- takeBetween 1 n
49 , takeEQ -- takeBetween n n
49 , takeEQ -- takeBetween n n
@@ -281,6 +281,59 @@ either parser = Parser step initial extract
281-- Taking elements
281-- Taking elements
282-------------------------------------------------------------------------------
282-------------------------------------------------------------------------------
284+-- | See 'Streamly.Internal.Data.Parser.takeBetween'.
288+{-# INLINE takeBetween #-}
289+takeBetween :: MonadCatch m => Int -> Int -> Fold m a b -> Parser m a b
290+takeBetween low high (Fold fstep finitial fextract) =
292+ Parser step initial extract
296+ initial = Tuple' 0 <$> finitial
299+ | low > high = throwM $ ParseError
300+ $ "takeBetween: lower bound - " ++ show low
301+ ++ " is greater than higher bound - " ++ show high
302+ | high <= 0 = Done 1 <$> fextract s
307+ FL.Partial s' -> Continue 0 $ Tuple' i1 s'
310+ $ "takeBetween: the collecting fold terminated after"
311+ ++ " consuming" ++ show i1 ++ " elements"
317+ then Done 0 <$> fextract s'
318+ else return $ Partial 0 $ Tuple' i1 s'
319+ FL.Done b -> return $ Done 0 b
325+ extract (Tuple' i s)
326+ | i >= low && i <= high = fextract s
327+ | otherwise = throwM $ ParseError err
332+ "takeBetween: Expecting alteast " ++ show low
333+ ++ " elements, got " ++ show i
284-- | See 'Streamly.Internal.Data.Parser.takeEQ'.
337-- | See 'Streamly.Internal.Data.Parser.takeEQ'.
286-- /Internal/
339-- /Internal/