Commits

Bryan O'Sullivan committed fe35c8e

Write a Text version of dropWhile

  • Participants
  • Parent commits 792d566

Comments (0)

Files changed (2)

File Data/Text.hs

   #-}
 
 -- | /O(n)/ 'dropWhile' @p@ @xs@ returns the suffix remaining after
--- 'takeWhile' @p@ @xs@.
+-- 'takeWhile' @p@ @xs@. This function is subject to array fusion.
 dropWhile :: (Char -> Bool) -> Text -> Text
-dropWhile p t = unstream (S.dropWhile p (stream t))
-{-# INLINE dropWhile #-}
+dropWhile p (Text arr off len) = loop off 0
+  where loop !i !l | l >= len  = empty
+                   | p c       = loop (i+d) (l+d)
+                   | otherwise = Text arr i (len-l)
+            where (c,d)        = iter arr i
+{-# INLINE [1] dropWhile #-}
+
+{-# RULES
+"TEXT dropWhile -> fused" [~1] forall p t.
+    dropWhile p t = unstream (S.dropWhile p (stream t))
+"TEXT dropWhile -> unfused" [1] forall p t.
+    unstream (S.dropWhile p (stream t)) = dropWhile p t
+  #-}
 
 -- | /O(n)/ Return all initial segments of the given 'Text', shortest
 -- first.

File tests/Properties.hs

 prop_takeWhile p     = L.takeWhile p `eqP` (unpack . T.takeWhile p)
 prop_takeWhileS p    = L.takeWhile p `eqP` (unpack . unstream . S.takeWhile p . stream)
 prop_dropWhile p     = L.dropWhile p `eqP` (unpack . T.dropWhile p)
+prop_dropWhileS p    = L.dropWhile p `eqP` (unpack . unstream . S.dropWhile p . stream)
 prop_inits           = L.inits       `eqP` (map unpack . T.inits)
 prop_tails           = L.tails       `eqP` (map unpack . T.tails)
 
   ("prop_takeWhile", mytest prop_takeWhile),
   ("prop_takeWhileS", mytest prop_takeWhileS),
   ("prop_dropWhile", mytest prop_dropWhile),
+  ("prop_dropWhileS", mytest prop_dropWhileS),
   ("prop_inits", mytest prop_inits),
   ("prop_tails", mytest prop_tails),