Commits

Bryan O'Sullivan committed 5f1e3a1

Implement and test lazy isInfixOf

  • Participants
  • Parent commits c734f61

Comments (0)

Files changed (3)

File Data/Text.hs

 isInfixOf :: Text -> Text -> Bool
 isInfixOf needle haystack = L.any (isPrefixOf needle) (tails haystack)
 {-# INLINE isInfixOf #-}
+-- TODO: a better implementation
 
 emptyError :: String -> a
 emptyError fun = P.error ("Data.Text." ++ fun ++ ": empty input")

File Data/Text/Lazy.hs

     -- * Predicates
     , isPrefixOf
     , isSuffixOf
-    -- , isInfixOf
+    , isInfixOf
 
     -- * Searching
     -- , elem
 {-# INLINE isSuffixOf #-}
 -- TODO: a better implementation
 
+-- | /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 #-}
+-- TODO: a better implementation
+
 revChunks :: [T.Text] -> Text
 revChunks = L.foldl' (flip chunk) Empty
 

File tests/Properties.hs

 prop_T_isSuffixOf s    = L.isSuffixOf s`eqP` T.isSuffixOf (packT s)
 prop_TL_isSuffixOf s   = L.isSuffixOf s`eqP` TL.isSuffixOf (packT s)
 prop_T_isInfixOf s     = L.isInfixOf s `eqP` T.isInfixOf (packT s)
+prop_TL_isInfixOf s    = L.isInfixOf s `eqP` TL.isInfixOf (packT s)
 
 prop_T_elem c          = L.elem c      `eqP` T.elem c
 prop_T_filter p        = L.filter p    `eqP` (unpackT . T.filter p)
   ("prop_T_isSuffixOf", mytest prop_T_isSuffixOf),
   ("prop_TL_isSuffixOf", mytest prop_TL_isSuffixOf),
   ("prop_T_isInfixOf", mytest prop_T_isInfixOf),
+  ("prop_TL_isInfixOf", mytest prop_TL_isInfixOf),
 
   ("prop_T_elem", mytest prop_T_elem),
   ("prop_T_filter", mytest prop_T_filter),