on master help sign in
Simple implementation of isInfixOf

--HG-- extra : convert_revision : 88505a71433f083560ba6e7919751eff3dbe8b15

commitc7c71f7a5ca1c73b09dee0b544c64137db166982 authorBryan O'Sullivan <bos@serpentine.com> dateThu 12 Feb 2009, 07:47 (18 years ago) parent48685b2
2 files changed+10−1
Data/Text.hs+8−1 tests/Properties.hs+2
Data/Text.hs+8−1
@@ -117,7 +117,7 @@ module Data.Text
117117 -- * Predicates
118118 , isPrefixOf
119119 , isSuffixOf
120− -- , isInfixOf
120+ , isInfixOf
121121
122122 -- * Searching
123123 , elem
@@ -861,5 +861,12 @@ isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
861861 | otherwise = Text barr (boff+d) alen
862862{-# INLINE isSuffixOf #-}
863863
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+
864871errorEmptyList :: String -> a
865872errorEmptyList 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)
143143prop_isPrefixOf s = L.isPrefixOf s`eqP` T.isPrefixOf (pack s)
144144prop_isPrefixOfS s = L.isPrefixOf s`eqP` (S.isPrefixOf (stream $ pack s) . stream)
145145prop_isSuffixOf s = L.isSuffixOf s`eqP` T.isSuffixOf (pack s)
146+prop_isInfixOf s = L.isInfixOf s `eqP` T.isInfixOf (pack s)
146147
147148prop_elem c = L.elem c `eqP` T.elem c
148149prop_filter p = L.filter p `eqP` (unpack . T.filter p)
@@ -242,6 +243,7 @@ tests = [
242243 ("prop_isPrefixOf", mytest prop_isPrefixOf),
243244 ("prop_isPrefixOfS", mytest prop_isPrefixOfS),
244245 ("prop_isSuffixOf", mytest prop_isSuffixOf),
246+ ("prop_isInfixOf", mytest prop_isInfixOf),
245247
246248 ("prop_elem", mytest prop_elem),
247249 ("prop_filter", mytest prop_filter),