@@ -104,8 +104,8 @@ module Data.Text
104104 , tails
105105
106106 -- ** Breaking into many substrings
107− -- , split
108− -- , splitWith
107+ , split
108+ , splitWith
109109 -- , breakSubstring
110110
111111 -- ** Breaking into lines and words
@@ -701,6 +701,43 @@ tails :: Text -> [Text]
701701tails t | null t = [empty]
702702 | otherwise = t : tails (tail t)
703703
704+-- | /O(n)/ Break a 'Text' into pieces separated by the 'Char'
705+-- argument, consuming the delimiter. I.e.
706+--
707+-- > split '\n' "a\nb\nd\ne" == ["a","b","d","e"]
708+-- > split 'a' "aXaXaXa" == ["","X","X","X",""]
709+-- > split 'x' "x" == ["",""]
710+--
711+-- and
712+--
713+-- > intercalate (singleton c) . split c == id
714+-- > split == splitWith . (==)
715+--
716+-- As for all splitting functions in this library, this function does
717+-- not copy the substrings, it just constructs new 'Text's that are
718+-- slices of the original.
719+split :: Char -> Text -> [Text]
720+split c = splitWith (==c)
721+{-# INLINE split #-}
722+
723+-- | /O(n)/ Splits a 'Text' into components delimited by separators,
724+-- where the predicate returns True for a separator element. The
725+-- resulting components do not contain the separators. Two adjacent
726+-- separators result in an empty component in the output. eg.
727+--
728+-- > splitWith (=='a') "aabbaca" == ["","","bb","c",""]
729+-- > splitWith (=='a') [] == []
730+splitWith :: (Char -> Bool) -> Text -> [Text]
731+splitWith p = loop
732+ where loop t@(Text arr off len)
733+ | null t = [empty]
734+ | otherwise = text arr off n : rest
735+ where n = findAIndexOrEnd p t
736+ m = n + iter_ t n
737+ rest | n >= len = []
738+ | otherwise = loop (text arr (off+m) (len-m))
739+{-# INLINE splitWith #-}
740+
704741-- ----------------------------------------------------------------------------
705742-- * Searching
706743