@@ -107,8 +107,8 @@ module Data.Text.Lazy
109 -- ** Breaking into many substrings
109 -- ** Breaking into many substrings
110− -- , split
110+ , split
111− -- , splitWith
111+ , splitWith
112 -- , breakSubstring
112 -- , breakSubstring
114 -- ** Breaking into lines and words
114 -- ** Breaking into lines and words
@@ -646,6 +646,40 @@ tails ts@(Chunk t ts')
646 | T.length t == 1 = ts : tails ts'
646 | T.length t == 1 = ts : tails ts'
647 | otherwise = ts : tails (Chunk (T.unsafeTail t) ts')
647 | otherwise = ts : tails (Chunk (T.unsafeTail t) ts')
649+-- | /O(n)/ Break a 'Text' into pieces separated by the byte
650+-- argument, consuming the delimiter. I.e.
652+-- > split '\n' "a\nb\nd\ne" == ["a","b","d","e"]
653+-- > split 'a' "aXaXaXa" == ["","X","X","X",""]
654+-- > split 'x' "x" == ["",""]
658+-- > intercalate [c] . split c == id
659+-- > split == splitWith . (==)
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)
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.
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 #-}
649revChunks :: [T.Text] -> Text
683revChunks :: [T.Text] -> Text
650revChunks = L.foldl' (flip chunk) Empty
684revChunks = L.foldl' (flip chunk) Empty