@@ -114,6 +114,7 @@ module Data.Text
115 -- ** Breaking into many substrings
115 -- ** Breaking into many substrings
117 , splitChar
118 , splitChar
118 , splitWith
119 , splitWith
119 , breakSubstring
120 , breakSubstring
@@ -860,6 +861,39 @@ split pat src0
860 | otherwise = search (n+1) (unsafeTail s)
861 | otherwise = search (n+1) (unsafeTail s)
861{-# INLINE split #-}
862{-# INLINE split #-}
864+-- | /O(m)*O(n)/ Break a 'Text' into pieces at most @k@ times,
865+-- treating the first 'Text' argument as the delimiter to break on,
866+-- and consuming the delimiter. The last element of the list contains
867+-- the remaining text after the number of times to split has been
868+-- reached. A value of zero or less for @k@ causes no splitting to
873+-- > splitTimes 0 "//" "a//b//c" == ["a//b//c"]
874+-- > splitTimes 2 ":" "a:b:c:d:e" == ["a","b","c:d:e"]
875+-- > splitTimes 100 "???" "a????b" == ["a","?b"]
879+-- > intercalate s . splitTimes k s == id
880+splitTimes :: Int -- ^ Maximum number of times to split
881+ -> Text -- ^ Text to split on
882+ -> Text -- ^ Input text
884+splitTimes k pat src0
885+ | k <= 0 || l == 0 = [src0]
886+ | otherwise = go k src0
889+ go !i src = search 0 src
892+ | i == 0 || null s = [src] -- not found or limit reached
893+ | pat `isPrefixOf` s = take n src : go (i-1) (drop l s)
894+ | otherwise = search (n+1) (unsafeTail s)
895+{-# INLINE splitTimes #-}
863-- | /O(n)/ Splits a 'Text' into components delimited by separators,
897-- | /O(n)/ Splits a 'Text' into components delimited by separators,
864-- where the predicate returns True for a separator element. The
898-- where the predicate returns True for a separator element. The
865-- resulting components do not contain the separators. Two adjacent
899-- resulting components do not contain the separators. Two adjacent