on master help sign in
Implement and test lazy isInfixOf

--HG-- extra : convert_revision : 89d236cb95fa4d9cd603c5dcb9919b184fb0dd8a

commit33fb4b3f8a392a3897cd102f7a26e0f6f3d45d99 authorBryan O'Sullivan <bos@serpentine.com> dateSun 8 Mar 2009, 03:26 (18 years ago) parent29f046b
3 files changed+12−1
Data/Text.hs+1 Data/Text/Lazy.hs+9−1 tests/Properties.hs+2
Data/Text.hs+1
@@ -960,6 +960,7 @@ isSuffixOf a@(Text _aarr _aoff alen) b@(Text barr boff blen) =
960960isInfixOf :: Text -> Text -> Bool
961961isInfixOf needle haystack = L.any (isPrefixOf needle) (tails haystack)
962962{-# INLINE isInfixOf #-}
963+-- TODO: a better implementation
963964
964965emptyError :: String -> a
965966emptyError fun = P.error ("Data.Text." ++ fun ++ ": empty input")
Data/Text/Lazy.hs+9−1
@@ -122,7 +122,7 @@ module Data.Text.Lazy
122122 -- * Predicates
123123 , isPrefixOf
124124 , isSuffixOf
125− -- , isInfixOf
125+ , isInfixOf
126126
127127 -- * Searching
128128 -- , elem
@@ -773,6 +773,14 @@ isSuffixOf x y = reverse x `isPrefixOf` reverse y
773773{-# INLINE isSuffixOf #-}
774774-- TODO: a better implementation
775775
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+
776784revChunks :: [T.Text] -> Text
777785revChunks = L.foldl' (flip chunk) Empty
778786
tests/Properties.hs+2
@@ -306,6 +306,7 @@ prop_TL_isPrefixOf s = L.isPrefixOf s`eqP` TL.isPrefixOf (packT s)
306306prop_T_isSuffixOf s = L.isSuffixOf s`eqP` T.isSuffixOf (packT s)
307307prop_TL_isSuffixOf s = L.isSuffixOf s`eqP` TL.isSuffixOf (packT s)
308308prop_T_isInfixOf s = L.isInfixOf s `eqP` T.isInfixOf (packT s)
309+prop_TL_isInfixOf s = L.isInfixOf s `eqP` TL.isInfixOf (packT s)
309310
310311prop_T_elem c = L.elem c `eqP` T.elem c
311312prop_T_filter p = L.filter p `eqP` (unpackT . T.filter p)
@@ -504,6 +505,7 @@ tests = [
504505 ("prop_T_isSuffixOf", mytest prop_T_isSuffixOf),
505506 ("prop_TL_isSuffixOf", mytest prop_TL_isSuffixOf),
506507 ("prop_T_isInfixOf", mytest prop_T_isInfixOf),
508+ ("prop_TL_isInfixOf", mytest prop_TL_isInfixOf),
507509
508510 ("prop_T_elem", mytest prop_T_elem),
509511 ("prop_T_filter", mytest prop_T_filter),