on master help sign in
Allow compareLengthI to shortcircuit if the maximum length is less than the target length.
commita66cbb79dce25e20a02916dd33805ee32b68393d authorEdward Kmett <ekmett@gmail.com> dateFri 4 Apr 2014, 07:14 (13 years ago) parent260f3ab
1 file changed+6−3
Data/Text/Internal/Fusion/Common.hs+6−3
Data/Text/Internal/Fusion/Common.hs+6−3
@@ -315,12 +315,15 @@ lengthI (Stream next s0 _len) = loop_length 0 s0
315315--
316316-- This function gives the same answer as comparing against the result
317317-- of 'lengthI', but can short circuit if the count of characters is
318−-- greater than the number, and hence be more efficient.
318+-- greater than the number or if the stream can't possibly be as long
319+-- as the number supplied, and hence be more efficient.
319320compareLengthI :: Integral a => Stream Char -> a -> Ordering
320321compareLengthI (Stream next s0 len) n =
321322 case exactly len of
322− Nothing -> loop_cmp 0 s0
323− Just i -> compare (fromIntegral i) n
323+ Nothing
324+ | upperBound (fromIntegral n) len < (fromIntegral n) -> LT
325+ | otherwise -> loop_cmp 0 s0
326+ Just i -> compare (fromIntegral i) n
324327 where
325328 loop_cmp !z s = case next s of
326329 Done -> compare z n