From d23519908d7dfa8a70fe265340ef3a55ae28bc1c Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Wed, 22 Aug 2018 12:16:36 -0400 Subject: [PATCH] Fusion.Common: Fix take/drop's treatment of negative counts This fixes #227. --- Data/Text/Internal/Fusion/Common.hs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Data/Text/Internal/Fusion/Common.hs b/Data/Text/Internal/Fusion/Common.hs index 15fbab0d4..f00569b86 100644 --- a/Data/Text/Internal/Fusion/Common.hs +++ b/Data/Text/Internal/Fusion/Common.hs @@ -735,8 +735,10 @@ unfoldrNI n f s0 | n < 0 = empty -- length of the stream. take :: Integral a => a -> Stream Char -> Stream Char take n0 (Stream next0 s0 len) = - Stream next (n0 :*: s0) (smaller len (codePointsSize $ fromIntegral n0)) + Stream next (n0' :*: s0) (smaller len (codePointsSize $ fromIntegral n0')) where + n0' = max n0 0 + {-# INLINE next #-} next (n :*: s) | n <= 0 = Done | otherwise = case next0 s of @@ -753,8 +755,10 @@ data Drop a s = NS !s -- is greater than the length of the stream. drop :: Integral a => a -> Stream Char -> Stream Char drop n0 (Stream next0 s0 len) = - Stream next (JS n0 s0) (len - codePointsSize (fromIntegral n0)) + Stream next (JS n0' s0) (len - codePointsSize (fromIntegral n0')) where + n0' = max n0 0 + {-# INLINE next #-} next (JS n s) | n <= 0 = Skip (NS s)