on master help sign in
Implement and test lazy isInfixOf
33fb4b3Bryan O'Sullivan
Data/Text.hs+1
@@ -960,6 +960,7 @@ isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
960isInfixOf :: Text -> Text -> Bool
960isInfixOf :: Text -> Text -> Bool
961isInfixOf needle haystack = L.any (isPrefixOf needle) (tails haystack)
961isInfixOf needle haystack = L.any (isPrefixOf needle) (tails haystack)
962{-# INLINE isInfixOf #-}
962{-# INLINE isInfixOf #-}
963+-- TODO: a better implementation
963
964
964emptyError :: String -> a
965emptyError :: String -> a
965emptyError fun = P.error ("Data.Text." ++ fun ++ ": empty input")
966emptyError fun = P.error ("Data.Text." ++ fun ++ ": empty input")
Data/Text/Lazy.hs+9−1
@@ -122,7 +122,7 @@ module Data.Text.Lazy
122 -- * Predicates
122 -- * Predicates
123 , isPrefixOf
123 , isPrefixOf
124 , isSuffixOf
124 , isSuffixOf
125− -- , isInfixOf
125+ , isInfixOf
126
126
127 -- * Searching
127 -- * Searching
128 -- , elem
128 -- , elem
@@ -773,6 +773,14 @@ isSuffixOf x y = reverse x `isPrefixOf` reverse y
773{-# INLINE isSuffixOf #-}
773{-# INLINE isSuffixOf #-}
774-- TODO: a better implementation
774-- TODO: a better implementation
775
775
776+-- | /O(n)/ The 'isInfixOf' function takes two 'Text's and returns
777+-- 'True' iff the first is contained, wholly and intact, anywhere
778+-- within the second.
779+isInfixOf :: Text -> Text -> Bool
780+isInfixOf needle haystack = L.any (isPrefixOf needle) (tails haystack)
781+{-# INLINE isInfixOf #-}
782+-- TODO: a better implementation
783+
776revChunks :: [T.Text] -> Text
784revChunks :: [T.Text] -> Text
777revChunks = L.foldl' (flip chunk) Empty
785revChunks = L.foldl' (flip chunk) Empty
778
786
tests/Properties.hs+2
@@ -306,6 +306,7 @@ prop_TL_isPrefixOf s = L.isPrefixOf s`eqP` TL.isPrefixOf (packT s)
306prop_T_isSuffixOf s = L.isSuffixOf s`eqP` T.isSuffixOf (packT s)
306prop_T_isSuffixOf s = L.isSuffixOf s`eqP` T.isSuffixOf (packT s)
307prop_TL_isSuffixOf s = L.isSuffixOf s`eqP` TL.isSuffixOf (packT s)
307prop_TL_isSuffixOf s = L.isSuffixOf s`eqP` TL.isSuffixOf (packT s)
308prop_T_isInfixOf s = L.isInfixOf s `eqP` T.isInfixOf (packT s)
308prop_T_isInfixOf s = L.isInfixOf s `eqP` T.isInfixOf (packT s)
309+prop_TL_isInfixOf s = L.isInfixOf s `eqP` TL.isInfixOf (packT s)
309
310
310prop_T_elem c = L.elem c `eqP` T.elem c
311prop_T_elem c = L.elem c `eqP` T.elem c
311prop_T_filter p = L.filter p `eqP` (unpackT . T.filter p)
312prop_T_filter p = L.filter p `eqP` (unpackT . T.filter p)
@@ -504,6 +505,7 @@ tests = [
504 ("prop_T_isSuffixOf", mytest prop_T_isSuffixOf),
505 ("prop_T_isSuffixOf", mytest prop_T_isSuffixOf),
505 ("prop_TL_isSuffixOf", mytest prop_TL_isSuffixOf),
506 ("prop_TL_isSuffixOf", mytest prop_TL_isSuffixOf),
506 ("prop_T_isInfixOf", mytest prop_T_isInfixOf),
507 ("prop_T_isInfixOf", mytest prop_T_isInfixOf),
508+ ("prop_TL_isInfixOf", mytest prop_TL_isInfixOf),
507
509
508 ("prop_T_elem", mytest prop_T_elem),
510 ("prop_T_elem", mytest prop_T_elem),
509 ("prop_T_filter", mytest prop_T_filter),
511 ("prop_T_filter", mytest prop_T_filter),