Skip to content

Commit

Permalink
Fusion.Common: Fix take/drop's treatment of negative counts
Browse files Browse the repository at this point in the history
This fixes #227.
  • Loading branch information
bgamari committed Aug 22, 2018
1 parent 76a4999 commit 8b514d4
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Data/Text/Internal/Fusion/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 8b514d4

Please sign in to comment.