on master help sign in
Write a Text version of dropWhile
bbee394Bryan O'Sullivan
Data/Text.hs+14−3
@@ -617,10 +617,21 @@ takeWhile p t@(Text arr off len) = loop off 0
617 #-}
617 #-}
618
618
619-- | /O(n)/ 'dropWhile' @p@ @xs@ returns the suffix remaining after
619-- | /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.
621dropWhile :: (Char -> Bool) -> Text -> Text
621dropWhile :: (Char -> Bool) -> Text -> Text
622−dropWhile p t = unstream (S.dropWhile p (stream t))
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
623−{-# INLINE dropWhile #-}
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+ #-}
624
635
625-- | /O(n)/ Return all initial segments of the given 'Text', shortest
636-- | /O(n)/ Return all initial segments of the given 'Text', shortest
626-- first.
637-- first.
tests/Properties.hs+2
@@ -91,6 +91,7 @@ prop_drop n = L.drop n `eqP` (unpack . T.drop n)
91prop_takeWhile p = L.takeWhile p `eqP` (unpack . T.takeWhile p)
91prop_takeWhile p = L.takeWhile p `eqP` (unpack . T.takeWhile p)
92prop_takeWhileS p = L.takeWhile p `eqP` (unpack . unstream . S.takeWhile p . stream)
92prop_takeWhileS p = L.takeWhile p `eqP` (unpack . unstream . S.takeWhile p . stream)
93prop_dropWhile p = L.dropWhile p `eqP` (unpack . T.dropWhile p)
93prop_dropWhile p = L.dropWhile p `eqP` (unpack . T.dropWhile p)
94+prop_dropWhileS p = L.dropWhile p `eqP` (unpack . unstream . S.dropWhile p . stream)
94prop_inits = L.inits `eqP` (map unpack . T.inits)
95prop_inits = L.inits `eqP` (map unpack . T.inits)
95prop_tails = L.tails `eqP` (map unpack . T.tails)
96prop_tails = L.tails `eqP` (map unpack . T.tails)
96
97
@@ -177,6 +178,7 @@ tests = [
177 ("prop_takeWhile", mytest prop_takeWhile),
178 ("prop_takeWhile", mytest prop_takeWhile),
178 ("prop_takeWhileS", mytest prop_takeWhileS),
179 ("prop_takeWhileS", mytest prop_takeWhileS),
179 ("prop_dropWhile", mytest prop_dropWhile),
180 ("prop_dropWhile", mytest prop_dropWhile),
181+ ("prop_dropWhileS", mytest prop_dropWhileS),
180 ("prop_inits", mytest prop_inits),
182 ("prop_inits", mytest prop_inits),
181 ("prop_tails", mytest prop_tails),
183 ("prop_tails", mytest prop_tails),
182
184