on master help sign in
Rewrite various loops using iter_ and iter

--HG-- extra : convert_revision : c7f89390dc6112f14417174c52f69817310d5f25

commita6a1162c07c8e3a43800e66c7ed541e36103071e authorBryan O'Sullivan <bos@serpentine.com> dateFri 6 Feb 2009, 07:52 (18 years ago) parenta0411e4
1 file changed+17−35
Data/Text.hs+17−35
Data/Text.hs+17−35
@@ -297,14 +297,8 @@ iter_ arr i | m < 0xD800 || m > 0xDBFF = 1
297297uncons :: Text -> Maybe (Char, Text)
298298uncons (Text arr off len)
299299 | len <= 0 = Nothing
300− | otherwise = Just (c, Text arr (off+k) (len-k))
301− where c | single = unsafeChr m
302− | otherwise = U16.chr2 m n
303− k | single = 1
304− | otherwise = 2
305− single = m < 0xD800 || m > 0xDBFF
306− m = A.unsafeIndex arr off
307− n = A.unsafeIndex arr (off+1)
300+ | otherwise = Just (c, Text arr (off+d) (len-d))
301+ where (c,d) = iter arr off
308302{-# INLINE uncons #-}
309303
310304second :: (b -> c) -> (a,b) -> (a,c)
@@ -324,9 +318,8 @@ last (Text arr off len)
324318 | len <= 0 = errorEmptyList "last"
325319 | n < 0xDC00 || n > 0xDFFF = unsafeChr n
326320 | otherwise = U16.chr2 n0 n
327− where
328− n = A.unsafeIndex arr (off+len-1)
329− n0 = A.unsafeIndex arr (off+len-2)
321+ where n = A.unsafeIndex arr (off+len-1)
322+ n0 = A.unsafeIndex arr (off+len-2)
330323{-# INLINE [1] last #-}
331324
332325{-# RULES
@@ -341,11 +334,9 @@ last (Text arr off len)
341334-- must be non-empty. Subject to array fusion.
342335tail :: Text -> Text
343336tail (Text arr off len)
344− | len <= 0 = errorEmptyList "tail"
345− | n >= 0xD800 && n <= 0xDBFF = Text arr (off+2) (len-2)
346− | otherwise = Text arr (off+1) (len-1)
347− where
348− n = A.unsafeIndex arr off
337+ | len <= 0 = errorEmptyList "tail"
338+ | otherwise = Text arr (off+d) (len-d)
339+ where d = iter_ arr off
349340{-# INLINE [1] tail #-}
350341
351342
@@ -571,13 +562,11 @@ take n t@(Text arr off len)
571562 | n >= len = t
572563 | otherwise = Text arr off (loop off 0)
573564 where
574− end = off+len
565+ end = off + len
575566 loop !i !count
576567 | i >= end || count >= n = i - off
577− | c < 0xD800 || c > 0xDBFF = loop (i+1) (count+1)
578− | otherwise = loop (i+2) (count+1)
579− where
580− c = arr `A.unsafeIndex` i
568+ | otherwise = loop (i+d) (count+1)
569+ where d = iter_ arr i
581570{-# INLINE [1] take #-}
582571
583572{-# RULES
@@ -594,16 +583,12 @@ drop :: Int -> Text -> Text
594583drop n t@(Text arr off len)
595584 | n <= 0 = t
596585 | n >= len = empty
597− | otherwise = Text arr newOff newLen
598− where
599− (newOff, newLen) = loop off 0 len
600− end = off + len
601− loop !i !count !l
602− | i >= end || count >= n = (i,l)
603− | c < 0xD800 || c > 0xDBFF = loop (i+1) (count+1) (l-1)
604− | otherwise = loop (i+2) (count+1) (l-2)
605− where
606− c = arr `A.unsafeIndex` i
586+ | otherwise = loop off 0 len
587+ where end = off + len
588+ loop !i !count !l
589+ | i >= end || count >= n = Text arr i l
590+ | otherwise = loop (i+d) (count+1) (l-d)
591+ where d = iter_ arr i
607592{-# INLINE [1] drop #-}
608593
609594{-# RULES
@@ -630,10 +615,7 @@ dropWhile p t = unstream (S.dropWhile p (stream t))
630615inits :: Text -> [Text]
631616inits t@(Text arr off len) = loop off
632617 where loop i | i >= len = [t]
633− | otherwise = Text arr off i : loop j
634− where j | n < 0xD800 || n > 0xDBFF = i + 1
635− | otherwise = i + 2
636− n = A.unsafeIndex arr i
618+ | otherwise = Text arr off i : loop (i + iter_ arr i)
637619
638620-- | /O(n)/ Return all final segments of the given 'Text', longest
639621-- first.