Commits

Bryan O'Sullivan  committed 7e67231

Refactor peekWord8 and peekChar.

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

  • Participants
  • Parent commits fa28b1f

Comments (0)

Files changed (2)

File Data/Attoparsec/ByteString/Internal.hs

 -- combinators such as 'many', because such parsers loop until a
 -- failure occurs.  Careless use will thus result in an infinite loop.
 peekWord8 :: Parser (Maybe Word8)
-peekWord8 = T.Parser $ \i0 a0 m0 _kf ks -> case m0 of
-    Complete   -> ks i0 a0 m0 Nothing
-    Incomplete -> let ks' i a m = let w = B.unsafeHead (unI i)
-                                  in w `seq` ks i a m (Just w)
-                      kf' i a m = ks i a m Nothing
-                  in if B.null (unI i0)
-                     then prompt i0 a0 m0 kf' ks'
-                     else ks' i0 a0 m0
+peekWord8 = T.Parser $ \i0 a0 m0 _kf ks ->
+            if B.null (unI i0)
+            then if m0 == Complete
+                 then ks i0 a0 m0 Nothing
+                 else let ks' i a m = let !w = B.unsafeHead (unI i)
+                                      in ks i a m (Just w)
+                          kf' i a m = ks i a m Nothing
+                      in prompt i0 a0 m0 kf' ks'
+            else let !w = B.unsafeHead (unI i0)
+                 in ks i0 a0 m0 (Just w)
 {-# INLINE peekWord8 #-}
 
 -- | Match only if all input has been consumed.

File Data/Attoparsec/Text/Internal.hs

 -- combinators such as 'many', because such parsers loop until a
 -- failure occurs.  Careless use will thus result in an infinite loop.
 peekChar :: Parser (Maybe Char)
-peekChar = T.Parser $ \i0 a0 m0 _kf ks -> case m0 of
-    Complete   -> ks i0 a0 m0 Nothing
-    Incomplete -> let ks' i a m = let w = unsafeHead (unI i)
-                                  in w `seq` ks i a m (Just w)
-                      kf' i a m = ks i a m Nothing
-                  in if T.null (unI i0)
-                     then prompt i0 a0 m0 kf' ks'
-                     else ks' i0 a0 m0
+peekChar = T.Parser $ \i0 a0 m0 _kf ks ->
+           if T.null (unI i0)
+           then if m0 == Complete
+                then ks i0 a0 m0 Nothing
+                else let ks' i a m = let !c = unsafeHead (unI i)
+                                     in ks i a m (Just c)
+                         kf' i a m = ks i a m Nothing
+                     in prompt i0 a0 m0 kf' ks'
+           else let !c = unsafeHead (unI i0)
+                in ks i0 a0 m0 (Just c)
 {-# INLINE peekChar #-}
 
 -- | Match only if all input has been consumed.