on master help sign in
Write a Text version of dropWhile

--HG-- extra : convert_revision : 7a9da6b6060e742498ba2cf937ac2efe8935259e

commitbbee3945257fe427c1830e4d59f8926634860603 authorBryan O'Sullivan <bos@serpentine.com> dateFri 6 Feb 2009, 20:49 (18 years ago) parent9b61673
2 files changed+16−3
Data/Text.hs+14−3 tests/Properties.hs+2
Data/Text.hs+14−3
@@ -617,10 +617,21 @@ takeWhile p t@(Text arr off len) = loop off 0
617617 #-}
618618
619619-- | /O(n)/ 'dropWhile' @p@ @xs@ returns the suffix remaining after
620−-- 'takeWhile' @p@ @xs@.
620+-- 'takeWhile' @p@ @xs@. This function is subject to array fusion.
621621dropWhile :: (Char -> Bool) -> Text -> Text
622−dropWhile p t = unstream (S.dropWhile p (stream t))
623−{-# INLINE dropWhile #-}
622+dropWhile p (Text arr off len) = loop off 0
623+ where loop !i !l | l >= len = empty
624+ | p c = loop (i+d) (l+d)
625+ | otherwise = Text arr i (len-l)
626+ where (c,d) = iter arr i
627+{-# INLINE [1] dropWhile #-}
628+
629+{-# RULES
630+"TEXT dropWhile -> fused" [~1] forall p t.
631+ dropWhile p t = unstream (S.dropWhile p (stream t))
632+"TEXT dropWhile -> unfused" [1] forall p t.
633+ unstream (S.dropWhile p (stream t)) = dropWhile p t
634+ #-}
624635
625636-- | /O(n)/ Return all initial segments of the given 'Text', shortest
626637-- first.
tests/Properties.hs+2
@@ -91,6 +91,7 @@ prop_drop n = L.drop n `eqP` (unpack . T.drop n)
9191prop_takeWhile p = L.takeWhile p `eqP` (unpack . T.takeWhile p)
9292prop_takeWhileS p = L.takeWhile p `eqP` (unpack . unstream . S.takeWhile p . stream)
9393prop_dropWhile p = L.dropWhile p `eqP` (unpack . T.dropWhile p)
94+prop_dropWhileS p = L.dropWhile p `eqP` (unpack . unstream . S.dropWhile p . stream)
9495prop_inits = L.inits `eqP` (map unpack . T.inits)
9596prop_tails = L.tails `eqP` (map unpack . T.tails)
9697
@@ -177,6 +178,7 @@ tests = [
177178 ("prop_takeWhile", mytest prop_takeWhile),
178179 ("prop_takeWhileS", mytest prop_takeWhileS),
179180 ("prop_dropWhile", mytest prop_dropWhile),
181+ ("prop_dropWhileS", mytest prop_dropWhileS),
180182 ("prop_inits", mytest prop_inits),
181183 ("prop_tails", mytest prop_tails),
182184