Commits

Bryan O'Sullivan  committed f5b8d1a

Get rid of the elem function.

  • Participants
  • Parent commits ad26fd2

Comments (0)

Files changed (3)

File Data/Text.hs

     , isInfixOf
 
     -- * Searching
-    , elem
     , filter
     , find
     , findBy
 -- * Searching
 
 -------------------------------------------------------------------------------
--- ** Searching by equality
-
--- | /O(n)/ 'elem' is the 'Text' membership predicate.
-elem :: Char -> Text -> Bool
-elem c t = S.elem c (stream t)
-{-# INLINE elem #-}
-
--------------------------------------------------------------------------------
 -- ** Searching with a predicate
 
 -- | /O(n)/ The 'findBy' function takes a predicate and a 'Text', and
 -- In (unlikely) bad cases, this function's time complexity degrades
 -- towards /O(n*m)/.
 isInfixOf :: Text -> Text -> Bool
-isInfixOf pat src = null pat || (not . L.null $ indices pat src)
-{-# INLINE isInfixOf #-}
+isInfixOf needle haystack
+    | null needle        = True
+    | isSingleton needle = S.elem (unsafeHead needle) . S.stream $ haystack
+    | otherwise          = not . L.null . indices needle $ haystack
+{-# INLINE [1] isInfixOf #-}
+
+{-# RULES
+"TEXT isInfixOf/singleton -> S.elem/S.stream" [~1] forall n h.
+    isInfixOf (singleton n) h = S.elem n (S.stream h)
+  #-}
 
 emptyError :: String -> a
 emptyError fun = P.error ("Data.Text." ++ fun ++ ": empty input")

File Data/Text/Lazy.hs

     , isInfixOf
 
     -- * Searching
-    , elem
     , filter
     , findBy
     , partitionBy
 {-# INLINE isInfixOf #-}
 -- TODO: a better implementation
 
--- | /O(n)/ 'elem' is the 'Text' membership predicate.
-elem :: Char -> Text -> Bool
-elem c t = S.elem c (stream t)
-{-# INLINE elem #-}
-
 -- | /O(n)/ 'filter', applied to a predicate and a 'Text',
 -- returns a 'Text' containing those characters that satisfy the
 -- predicate.

File tests/Properties.hs

 tl_isInfixOf s    = L.isInfixOf s `eqP` TL.isInfixOf (packS s)
 
 sf_elem p c       = (L.elem c . L.filter p) `eqP` (S.elem c . S.filter p)
-t_elem c          = L.elem c      `eqP` T.elem c
-tl_elem c         = L.elem c      `eqP` TL.elem c
 sf_filter q p     = (L.filter p . L.filter q) `eqP` (unpackS . S.filter p . S.filter q)
 t_filter p        = L.filter p    `eqP` (unpackS . T.filter p)
 tl_filter p       = L.filter p    `eqP` (unpackS . TL.filter p)
 
   testGroup "searching" [
     testProperty "sf_elem" sf_elem,
-    testProperty "t_elem" t_elem,
-    testProperty "tl_elem" tl_elem,
     testProperty "sf_filter" sf_filter,
     testProperty "t_filter" t_filter,
     testProperty "tl_filter" tl_filter,