@@ -1138,21 +1138,17 @@ elemIndices :: Char -> Text -> [Int]
1138elemIndices c t = S.elemIndices c (stream t)
1138elemIndices c t = S.elemIndices c (stream t)
1139{-# INLINE elemIndices #-}
1139{-# INLINE elemIndices #-}
1141−-- | /O(n*m)/ The 'count' function returns the number of times the
1141+-- | /O(n+m)/ The 'count' function returns the number of times the
1142-- query string appears in the given 'Text'. An empty query string is
1142-- query string appears in the given 'Text'. An empty query string is
1143-- invalid, and will cause an error to be raised.
1143-- invalid, and will cause an error to be raised.
1145+-- In (unlikely) bad cases, this function's time complexity
1146+-- degenerates towards /O(n*m)/.
1144count :: Text -> Text -> Int
1147count :: Text -> Text -> Int
1145−count pat src0
1148+count pat src
1146− | null pat = emptyError "count"
1149+ | null pat = emptyError "count"
1147− | l == 1 = countChar (unsafeHead pat) src0
1150+ | isSingleton pat = countChar (unsafeHead pat) src
1148− | otherwise = go 0 src0
1151+ | otherwise = L.length (indices pat src)
1151− go !n src = search src
1153− search s | null s = n
1154− | pat `isPrefixOf` s = go (n+1) (drop l s)
1155− | otherwise = search (unsafeTail s)
1156{-# INLINE [1] count #-}
1152{-# INLINE [1] count #-}
1158{-# RULES
1154{-# RULES
@@ -1268,13 +1264,15 @@ isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
1268 | otherwise = Text barr (boff+d) alen
1264 | otherwise = Text barr (boff+d) alen
1269{-# INLINE isSuffixOf #-}
1265{-# INLINE isSuffixOf #-}
1271−-- | /O(n)/ The 'isInfixOf' function takes two 'Text's and returns
1267+-- | /O(n+m)/ The 'isInfixOf' function takes two 'Text's and returns
1272-- 'True' iff the first is contained, wholly and intact, anywhere
1268-- 'True' iff the first is contained, wholly and intact, anywhere
1273-- within the second.
1269-- within the second.
1271+-- In (unlikely) bad cases, this function's time complexity
1272+-- degenerates towards /O(n*m)/.
1274isInfixOf :: Text -> Text -> Bool
1273isInfixOf :: Text -> Text -> Bool
1275−isInfixOf needle haystack = L.any (isPrefixOf needle) (tails haystack)
1274+isInfixOf pat src = null pat || (not . L.null $ indices pat src)
1276{-# INLINE isInfixOf #-}
1275{-# INLINE isInfixOf #-}
1277−-- TODO: a better implementation
1279emptyError :: String -> a
1277emptyError :: String -> a
1280emptyError fun = P.error ("Data.Text." ++ fun ++ ": empty input")
1278emptyError fun = P.error ("Data.Text." ++ fun ++ ": empty input")