-- 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 =
- Nothing -> loop_cmp 0 s0
- Just i -> compare (fromIntegral i) n
+ | 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