@@ -98,18 +98,23 @@ type Added = T.Added B.ByteString
9898type Failure r = T.Failure B.ByteString r
9999type Success a r = T.Success B.ByteString a r
100100
101+ensure' :: Int -> Input -> Added -> More -> Failure r -> Success B.ByteString r
102+ -> IResult B.ByteString r
103+ensure' n0 i0 a0 m0 kf0 ks0 =
104+ T.runParser (demandInput >> go n0) i0 a0 m0 kf0 ks0
105+ where
106+ go n = T.Parser $ \i a m kf ks ->
107+ if B.length (unI i) >= n
108+ then ks i a m (unI i)
109+ else T.runParser (demandInput >> go n) i a m kf ks
110+
101111-- | If at least @n@ bytes of input are available, return the current
102112-- input, otherwise fail.
103113ensure :: Int -> Parser B.ByteString
104114ensure !n = T.Parser $ \i0 a0 m0 kf ks ->
105115 if B.length (unI i0) >= n
106116 then inline ks i0 a0 m0 (unI i0)
107− else T.runParser (demandInput >> go n) i0 a0 m0 kf ks
108− where
109− go n' = T.Parser $ \i0 a0 m0 kf ks ->
110− if B.length (unI i0) >= n'
111− then ks i0 a0 m0 (unI i0)
112− else T.runParser (demandInput >> go n') i0 a0 m0 kf ks
117+ else ensure' n i0 a0 m0 kf ks
113118{-# INLINE ensure #-}
114119
115120-- | Ask for input. If we receive any, pass it to a success