@@ -99,8 +99,8 @@ module Data.Text.Lazy
9999 , takeWhile
100100 , dropWhile
101101 , splitAt
102− -- , span
103− -- , break
102+ , span
103+ , break
104104 -- , group
105105 -- , groupBy
106106 -- , inits
@@ -150,7 +150,7 @@ import Prelude (Char, Bool(..), Functor(..), Int, Maybe(..), String,
150150 Eq(..), Ord(..), (++),
151151 Read(..), Show(..),
152152 (&&), (||), (+), (-), (.), ($),
153− fromIntegral, not, return, otherwise)
153+ flip, fromIntegral, not, return, otherwise)
154154import qualified Prelude as P
155155import Data.Int (Int64)
156156import qualified Data.List as L
@@ -586,7 +586,31 @@ splitAt = loop
586586 in (Chunk ts' Empty, Chunk ts'' Empty)
587587 | otherwise = let (ts',ts'') = loop (n - len) ts
588588 in (Chunk t ts', ts'')
589− where len = T.length t
589+ where len = fromIntegral (T.length t)
590+
591+-- | /O(n)/ 'break' is like 'span', but the prefix returned is over
592+-- elements that fail the predicate @p@.
593+break :: (Char -> Bool) -> Text -> (Text, Text)
594+break p t0 = break' t0
595+ where break' Empty = (empty, empty)
596+ break' c@(Chunk t ts) =
597+ case T.findIndex p t of
598+ Nothing -> let (ts', ts'') = break' ts
599+ in (Chunk t ts', ts'')
600+ Just n | n == 0 -> (Empty, c)
601+ | otherwise -> let (a,b) = T.splitAt n t
602+ in (Chunk a Empty, Chunk b ts)
603+
604+-- | /O(n)/ 'span', applied to a predicate @p@ and text @t@, returns a
605+-- pair whose first element is the longest prefix (possibly empty) of
606+-- @t@ of elements that satisfy @p@, and whose second is the remainder
607+-- of the list.
608+span :: (Char -> Bool) -> Text -> (Text, Text)
609+span p = break (not . p)
610+{-# INLINE span #-}
611+
612+revChunks :: [T.Text] -> Text
613+revChunks = L.foldl' (flip chunk) Empty
590614
591615emptyError :: String -> a
592616emptyError fun = P.error ("Data.Text.Lazy." ++ fun ++ ": empty input")