@@ -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@.