git.iko.soy/typeable/attoparsec
on master help sign in
Refactor peekWord8 and peekChar.

We now check whether there's input before we care whether we've hit EOF.

commit02c7230bc41fe7b982a7cfcee9d08468e8620dc3 authorBryan O'Sullivan <bos@serpentine.com> dateTue 14 Aug 2012, 16:25 (14 years ago) parentb26a317
2 files changed+20−16
Data/Attoparsec/ByteString/Internal.hs+10−8 Data/Attoparsec/Text/Internal.hs+10−8
Data/Attoparsec/ByteString/Internal.hs+10−8
@@ -407,14 +407,16 @@ notWord8 c = satisfy (/= c) <?> "not " ++ show c
407407-- combinators such as 'many', because such parsers loop until a
408408-- failure occurs. Careless use will thus result in an infinite loop.
409409peekWord8 :: Parser (Maybe Word8)
410−peekWord8 = T.Parser $ \i0 a0 m0 _kf ks -> case m0 of
411− Complete -> ks i0 a0 m0 Nothing
412− Incomplete -> let ks' i a m = let w = B.unsafeHead (unI i)
413− in w `seq` ks i a m (Just w)
414− kf' i a m = ks i a m Nothing
415− in if B.null (unI i0)
416− then prompt i0 a0 m0 kf' ks'
417− else ks' i0 a0 m0
410+peekWord8 = T.Parser $ \i0 a0 m0 _kf ks ->
411+ if B.null (unI i0)
412+ then if m0 == Complete
413+ then ks i0 a0 m0 Nothing
414+ else let ks' i a m = let !w = B.unsafeHead (unI i)
415+ in ks i a m (Just w)
416+ kf' i a m = ks i a m Nothing
417+ in prompt i0 a0 m0 kf' ks'
418+ else let !w = B.unsafeHead (unI i0)
419+ in ks i0 a0 m0 (Just w)
418420{-# INLINE peekWord8 #-}
419421
420422-- | Match only if all input has been consumed.
Data/Attoparsec/Text/Internal.hs+10−8
@@ -410,14 +410,16 @@ notChar c = satisfy (/= c) <?> "not " ++ show c
410410-- combinators such as 'many', because such parsers loop until a
411411-- failure occurs. Careless use will thus result in an infinite loop.
412412peekChar :: Parser (Maybe Char)
413−peekChar = T.Parser $ \i0 a0 m0 _kf ks -> case m0 of
414− Complete -> ks i0 a0 m0 Nothing
415− Incomplete -> let ks' i a m = let w = unsafeHead (unI i)
416− in w `seq` ks i a m (Just w)
417− kf' i a m = ks i a m Nothing
418− in if T.null (unI i0)
419− then prompt i0 a0 m0 kf' ks'
420− else ks' i0 a0 m0
413+peekChar = T.Parser $ \i0 a0 m0 _kf ks ->
414+ if T.null (unI i0)
415+ then if m0 == Complete
416+ then ks i0 a0 m0 Nothing
417+ else let ks' i a m = let !c = unsafeHead (unI i)
418+ in ks i a m (Just c)
419+ kf' i a m = ks i a m Nothing
420+ in prompt i0 a0 m0 kf' ks'
421+ else let !c = unsafeHead (unI i0)
422+ in ks i0 a0 m0 (Just c)
421423{-# INLINE peekChar #-}
422424
423425-- | Match only if all input has been consumed.