on master help sign in
Allow compareLengthI to shortcircuit if the maximum length is less than the target length.
a66cbb7Edward Kmett
Data/Text/Internal/Fusion/Common.hs+6−3
@@ -315,12 +315,15 @@ lengthI (Stream next s0 _len) = loop_length 0 s0
315--
315--
316-- This function gives the same answer as comparing against the result
316-- This function gives the same answer as comparing against the result
317-- of 'lengthI', but can short circuit if the count of characters is
317-- of 'lengthI', but can short circuit if the count of characters is
318+-- greater than the number or if the stream can't possibly be as long
318−-- greater than the number, and hence be more efficient.
319+-- as the number supplied, and hence be more efficient.
319compareLengthI :: Integral a => Stream Char -> a -> Ordering
320compareLengthI :: Integral a => Stream Char -> a -> Ordering
320compareLengthI (Stream next s0 len) n =
321compareLengthI (Stream next s0 len) n =
321 case exactly len of
322 case exactly len of
323+ Nothing
324+ | upperBound (fromIntegral n) len < (fromIntegral n) -> LT
322− Nothing -> loop_cmp 0 s0
325+ | otherwise -> loop_cmp 0 s0
323− Just i -> compare (fromIntegral i) n
326+ Just i -> compare (fromIntegral i) n
324 where
327 where
325 loop_cmp !z s = case next s of
328 loop_cmp !z s = case next s of
326 Done -> compare z n
329 Done -> compare z n