@@ -919,8 +919,8 @@ tails ts@(Chunk t ts')
919919-- copies to create substrings; they just construct new 'Text's that
920920-- are slices of the original.
921921
922−-- | /O(m)*O(n)/ Break a 'Text' into pieces separated by the first
923−-- 'Text' argument, consuming the delimiter. An empty delimiter is
922+-- | /O(m+n)/ Break a 'Text' into pieces separated by the first
923+-- 'Text' argument, consuming the delimiter. An empty delimiter is
924924-- invalid, and will cause an error to be raised.
925925--
926926-- Examples:
@@ -933,21 +933,20 @@ tails ts@(Chunk t ts')
933933--
934934-- > intercalate s . split s == id
935935-- > split (singleton c) == splitBy (==c)
936+--
937+-- In (unlikely) bad cases, this function's time complexity degrades
938+-- towards /O(n*m)/.
936939split :: Text -- ^ Text to split on
937940 -> Text -- ^ Input text
938941 -> [Text]
939−split pat src0
940− | l == 0 = emptyError "split"
941− | l == 1 = splitBy (== (head pat)) src0
942− | otherwise = go src0
942+split pat src
943+ | null pat = emptyError "split"
944+ | isSingleton pat = splitBy (== head pat) src
945+ | otherwise = go 0 (indices pat src) src
943946 where
944− l = length pat
945− go src = search 0 src
946− where
947− search !n !s
948− | null s = [src] -- not found
949− | pat `isPrefixOf` s = take n src : go (drop l s)
950− | otherwise = search (n+1) (tail s)
947+ go _ [] cs = [cs]
948+ go !s (x:xs) cs = let h :*: t = splitAtWord (x-s) cs
949+ in h : go (s+x) xs t
951950{-# INLINE [1] split #-}
952951
953952{-# RULES