Edward Kmett  committed 3cfc794

Allow compareLengthI to shortcircuit if the maximum length is less than the target length.

  • Participants
  • Parent commits 1b88f4b
  • Branches default

Comments (0)

Files changed (1)

File Data/Text/Internal/Fusion/Common.hs

 -- This function gives the same answer as comparing against the result
 -- of 'lengthI', but can short circuit if the count of characters is
--- greater than the number, and hence be more efficient.
+-- greater than the number or if the stream can't possibly be as long
+-- as the number supplied, and hence be more efficient.
 compareLengthI :: Integral a => Stream Char -> a -> Ordering
 compareLengthI (Stream next s0 len) n =
     case exactly len of
-      Nothing -> loop_cmp 0 s0
-      Just i  -> compare (fromIntegral i) n
+      Nothing
+        | upperBound (fromIntegral n) len < (fromIntegral n) -> LT
+        | otherwise -> loop_cmp 0 s0
+      Just i -> compare (fromIntegral i) n
       loop_cmp !z s  = case next s of
                          Done       -> compare z n