@@ -107,8 +107,8 @@ module Data.Text.Lazy
107107 , tails
108108
109109 -- ** Breaking into many substrings
110− -- , split
111− -- , splitWith
110+ , split
111+ , splitWith
112112 -- , breakSubstring
113113
114114 -- ** Breaking into lines and words
@@ -646,6 +646,40 @@ tails ts@(Chunk t ts')
646646 | T.length t == 1 = ts : tails ts'
647647 | otherwise = ts : tails (Chunk (T.unsafeTail t) ts')
648648
649+-- | /O(n)/ Break a 'Text' into pieces separated by the byte
650+-- argument, consuming the delimiter. I.e.
651+--
652+-- > split '\n' "a\nb\nd\ne" == ["a","b","d","e"]
653+-- > split 'a' "aXaXaXa" == ["","X","X","X",""]
654+-- > split 'x' "x" == ["",""]
655+--
656+-- and
657+--
658+-- > intercalate [c] . split c == id
659+-- > split == splitWith . (==)
660+--
661+-- As for all splitting functions in this library, this function does
662+-- not copy the substrings, it just constructs new 'Text's that are
663+-- slices of the original.
664+split :: Char -> Text -> [Text]
665+split c = splitWith (==c)
666+{-# INLINE split #-}
667+
668+-- | /O(n)/ Splits a 'Text' into components delimited by separators,
669+-- where the predicate returns True for a separator element. The
670+-- resulting components do not contain the separators. Two adjacent
671+-- separators result in an empty component in the output. eg.
672+--
673+-- > splitWith (=='a') "aabbaca" == ["","","bb","c",""]
674+-- > splitWith (=='a') [] == []
675+splitWith :: (Char -> Bool) -> Text -> [Text]
676+splitWith _ Empty = []
677+splitWith p (Chunk t0 ts0) = comb [] (T.splitWith p t0) ts0
678+ where comb acc (s:[]) Empty = revChunks (s:acc) : []
679+ comb acc (s:[]) (Chunk t ts) = comb (s:acc) (T.splitWith p t) ts
680+ comb acc (s:ss) ts = revChunks (s:acc) : comb [] ss ts
681+{-# INLINE splitWith #-}
682+
649683revChunks :: [T.Text] -> Text
650684revChunks = L.foldl' (flip chunk) Empty
651685