on master help sign in
Extract the loop from dropEnd
commitb696eb03eb4f9149acd569c65c9c031059fccf28 authorBryan O'Sullivan <bos@serpentine.com> dateFri 7 Mar 2014, 09:16 (13 years ago) parent6207edf
1 file changed+9−6
Data/Text.hs+9−6
Data/Text.hs+9−6
@@ -1018,6 +1018,14 @@ take n t@(Text arr off len)
10181018 unstream (S.take n (stream t)) = take n t
10191019 #-}
10201020
1021+iterNEnd :: Int -> Text -> Int
1022+iterNEnd n t@(Text _arr _off len) = loop (len-1) n
1023+ where loop i !m
1024+ | i <= 0 = 0
1025+ | m <= 1 = i
1026+ | otherwise = loop (i+d) (m-1)
1027+ where d = reverseIter_ t i
1028+
10211029-- | /O(n)/ 'drop' @n@, applied to a 'Text', returns the suffix of the
10221030-- 'Text' after the first @n@ characters, or the empty 'Text' if @n@
10231031-- is greater than the length of the 'Text'. Subject to fusion.
@@ -1049,12 +1057,7 @@ dropEnd :: Int -> Text -> Text
10491057dropEnd n t@(Text arr off len)
10501058 | n <= 0 = t
10511059 | n >= len = empty
1052− | otherwise = loop (len-1) n
1053− where loop i !m
1054− | i <= 0 = empty
1055− | m <= 1 = Text arr off i
1056− | otherwise = loop (i+d) (m-1)
1057− where d = reverseIter_ t i
1060+ | otherwise = text arr off (iterNEnd n t)
10581061
10591062-- | /O(n)/ 'takeWhile', applied to a predicate @p@ and a 'Text',
10601063-- returns the longest prefix (possibly empty) of elements that