on master help sign in
Simple implementation of isInfixOf
c7c71f7Bryan O'Sullivan
Data/Text.hs+8−1
@@ -117,7 +117,7 @@ module Data.Text
117 -- * Predicates
117 -- * Predicates
118 , isPrefixOf
118 , isPrefixOf
119 , isSuffixOf
119 , isSuffixOf
120− -- , isInfixOf
120+ , isInfixOf
121
121
122 -- * Searching
122 -- * Searching
123 , elem
123 , elem
@@ -861,5 +861,12 @@ isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
861 | otherwise = Text barr (boff+d) alen
861 | otherwise = Text barr (boff+d) alen
862{-# INLINE isSuffixOf #-}
862{-# INLINE isSuffixOf #-}
863
863
864+-- | /O(n)/ The 'isInfixOf' function takes two 'Text's and returns
865+-- 'True' iff the first is contained, wholly and intact, anywhere
866+-- within the second.
867+isInfixOf :: Text -> Text -> Bool
868+isInfixOf needle haystack = L.any (isPrefixOf needle) (tails haystack)
869+{-# INLINE isInfixOf #-}
870+
864errorEmptyList :: String -> a
871errorEmptyList :: String -> a
865errorEmptyList fun = error ("Data.Text." ++ fun ++ ": empty list")
872errorEmptyList fun = error ("Data.Text." ++ fun ++ ": empty list")
tests/Properties.hs+2
@@ -143,6 +143,7 @@ prop_unwords = L.unwords `eq` (unpack . T.unwords . map pack)
143prop_isPrefixOf s = L.isPrefixOf s`eqP` T.isPrefixOf (pack s)
143prop_isPrefixOf s = L.isPrefixOf s`eqP` T.isPrefixOf (pack s)
144prop_isPrefixOfS s = L.isPrefixOf s`eqP` (S.isPrefixOf (stream $ pack s) . stream)
144prop_isPrefixOfS s = L.isPrefixOf s`eqP` (S.isPrefixOf (stream $ pack s) . stream)
145prop_isSuffixOf s = L.isSuffixOf s`eqP` T.isSuffixOf (pack s)
145prop_isSuffixOf s = L.isSuffixOf s`eqP` T.isSuffixOf (pack s)
146+prop_isInfixOf s = L.isInfixOf s `eqP` T.isInfixOf (pack s)
146
147
147prop_elem c = L.elem c `eqP` T.elem c
148prop_elem c = L.elem c `eqP` T.elem c
148prop_filter p = L.filter p `eqP` (unpack . T.filter p)
149prop_filter p = L.filter p `eqP` (unpack . T.filter p)
@@ -242,6 +243,7 @@ tests = [
242 ("prop_isPrefixOf", mytest prop_isPrefixOf),
243 ("prop_isPrefixOf", mytest prop_isPrefixOf),
243 ("prop_isPrefixOfS", mytest prop_isPrefixOfS),
244 ("prop_isPrefixOfS", mytest prop_isPrefixOfS),
244 ("prop_isSuffixOf", mytest prop_isSuffixOf),
245 ("prop_isSuffixOf", mytest prop_isSuffixOf),
246+ ("prop_isInfixOf", mytest prop_isInfixOf),
245
247
246 ("prop_elem", mytest prop_elem),
248 ("prop_elem", mytest prop_elem),
247 ("prop_filter", mytest prop_filter),
249 ("prop_filter", mytest prop_filter),