Commits

Bryan O'Sullivan committed 6d43331

Simple implementation of isInfixOf

  • Participants
  • Parent commits f1161ce

Comments (0)

Files changed (2)

File Data/Text.hs

     -- * Predicates
     , isPrefixOf
     , isSuffixOf
-    -- , isInfixOf
+    , isInfixOf
 
     -- * Searching
     , elem
            | otherwise = Text barr (boff+d) alen
 {-# INLINE isSuffixOf #-}
 
+-- | /O(n)/ The 'isInfixOf' function takes two 'Text's and returns
+-- 'True' iff the first is contained, wholly and intact, anywhere
+-- within the second.
+isInfixOf :: Text -> Text -> Bool
+isInfixOf needle haystack = L.any (isPrefixOf needle) (tails haystack)
+{-# INLINE isInfixOf #-}
+
 errorEmptyList :: String -> a
 errorEmptyList fun = error ("Data.Text." ++ fun ++ ": empty list")

File tests/Properties.hs

 prop_isPrefixOf s    = L.isPrefixOf s`eqP` T.isPrefixOf (pack s)
 prop_isPrefixOfS s   = L.isPrefixOf s`eqP` (S.isPrefixOf (stream $ pack s) . stream)
 prop_isSuffixOf s    = L.isSuffixOf s`eqP` T.isSuffixOf (pack s)
+prop_isInfixOf s     = L.isInfixOf s `eqP` T.isInfixOf (pack s)
 
 prop_elem c          = L.elem c      `eqP` T.elem c
 prop_filter p        = L.filter p    `eqP` (unpack . T.filter p)
   ("prop_isPrefixOf", mytest prop_isPrefixOf),
   ("prop_isPrefixOfS", mytest prop_isPrefixOfS),
   ("prop_isSuffixOf", mytest prop_isSuffixOf),
+  ("prop_isInfixOf", mytest prop_isInfixOf),
 
   ("prop_elem", mytest prop_elem),
   ("prop_filter", mytest prop_filter),