@@ -368,6 +368,12 @@ null (Text _arr _off len) = assert (len >= 0) $ len <= 0
368368 S.null (stream t) = null t
369369 #-}
370370
371+-- | /O(1)/ Tests whether a 'Text' contains exactly one character.
372+-- Subject to fusion.
373+isSingleton :: Text -> Bool
374+isSingleton = S.isSingleton . stream
375+{-# INLINE isSingleton #-}
376+
371377-- | /O(n)/ Returns the number of characters in a 'Text'.
372378-- Subject to fusion.
373379length :: Text -> Int
@@ -916,12 +922,11 @@ tails t | null t = [empty]
916922-- In (unlikely) bad cases, this function's time complexity
917923-- degenerates towards /O(n*m)/.
918924split :: Text -> Text -> [Text]
919−split pat src@(Text arr off len)
920− | null pat = emptyError "split"
921− | l == 1 = splitWith (== unsafeHead pat) src
922− | otherwise = go 0 (indices pat src)
925+split pat@(Text _ _ l) src@(Text arr off len)
926+ | l <= 0 = emptyError "split"
927+ | isSingleton pat = splitWith (== unsafeHead pat) src
928+ | otherwise = go 0 (indices pat src)
923929 where
924− l = length pat
925930 go !s (x:xs) = textP arr (s+off) (x-s) : go (x+l) xs
926931 go s _ = [textP arr (s+off) (len-s)]
927932{-# INLINE [1] split #-}
@@ -955,14 +960,13 @@ splitTimes :: Int -- ^ Maximum number of times to split
955960 -> Text -- ^ Text to split on
956961 -> Text -- ^ Input text
957962 -> [Text]
958−splitTimes k pat src@(Text arr off len)
959− | null pat = emptyError "splitTimes"
963+splitTimes k pat@(Text _ _ l) src@(Text arr off len)
964+ | l <= 0 = emptyError "splitTimes"
960965 | otherwise = go 0 0 (indices pat src)
961966 where
962967 go !s !i _ | i >= k = [textP arr (s+off) (len-s)]
963968 go !s _ [] = [textP arr (s+off) (len-s)]
964969 go !s !i (x:xs) = textP arr (s+off) (x-s) : go (x+l) (i+1) xs
965− l = length pat
966970{-# INLINE splitTimes #-}
967971
968972-- | /O(m+n)/ Break a 'Text' into pieces at most @k@ times, like