on master help sign in
Add an unstreamed version of takeWhile

--HG-- extra : convert_revision : df24f520aef6aeef0900adab32289ca2d8b7e9a6

commit9b61673757d0591da1b68acb30819c2af9af4398 authorBryan O'Sullivan <bos@serpentine.com> dateFri 6 Feb 2009, 07:52 (18 years ago) parenta6a1162
2 files changed+18−4
Data/Text.hs+16−4 tests/Properties.hs+2
Data/Text.hs+16−4
@@ -598,11 +598,23 @@ drop n t@(Text arr off len)
598598 unstream (S.drop n (stream t)) = drop n t
599599 #-}
600600
601−-- | /O(n)/ 'takeWhile', applied to a predicate @p@ and a 'Text', returns the
602−-- longest prefix (possibly empty) of elements that satisfy @p@.
601+-- | /O(n)/ 'takeWhile', applied to a predicate @p@ and a 'Text', returns
602+-- the longest prefix (possibly empty) of elements that satisfy @p@.
603+-- This function is subject to array fusion.
603604takeWhile :: (Char -> Bool) -> Text -> Text
604−takeWhile p t = unstream (S.takeWhile p (stream t))
605−{-# INLINE takeWhile #-}
605+takeWhile p t@(Text arr off len) = loop off 0
606+ where loop !i !l | l >= len = t
607+ | p c = loop (i+d) (l+d)
608+ | otherwise = Text arr off l
609+ where (c,d) = iter arr i
610+{-# INLINE [1] takeWhile #-}
611+
612+{-# RULES
613+"TEXT takeWhile -> fused" [~1] forall p t.
614+ takeWhile p t = unstream (S.takeWhile p (stream t))
615+"TEXT takeWhile -> unfused" [1] forall p t.
616+ unstream (S.takeWhile p (stream t)) = takeWhile p t
617+ #-}
606618
607619-- | /O(n)/ 'dropWhile' @p@ @xs@ returns the suffix remaining after
608620-- 'takeWhile' @p@ @xs@.
tests/Properties.hs+2
@@ -89,6 +89,7 @@ prop_unfoldrN n m = (L.take n . L.unfoldr f) `eq` (unpack . T.unfoldrN n f)
8989prop_take n = L.take n `eqP` (unpack . T.take n)
9090prop_drop n = L.drop n `eqP` (unpack . T.drop n)
9191prop_takeWhile p = L.takeWhile p `eqP` (unpack . T.takeWhile p)
92+prop_takeWhileS p = L.takeWhile p `eqP` (unpack . unstream . S.takeWhile p . stream)
9293prop_dropWhile p = L.dropWhile p `eqP` (unpack . T.dropWhile p)
9394prop_inits = L.inits `eqP` (map unpack . T.inits)
9495prop_tails = L.tails `eqP` (map unpack . T.tails)
@@ -174,6 +175,7 @@ tests = [
174175 ("prop_take", mytest prop_take),
175176 ("prop_drop", mytest prop_drop),
176177 ("prop_takeWhile", mytest prop_takeWhile),
178+ ("prop_takeWhileS", mytest prop_takeWhileS),
177179 ("prop_dropWhile", mytest prop_dropWhile),
178180 ("prop_inits", mytest prop_inits),
179181 ("prop_tails", mytest prop_tails),