Commits

Bryan O'Sullivan committed ad15bb6

Remove the old version of replace

This provides a nice performance boost!

  • Participants
  • Parent commits 98e61f1

Comments (0)

Files changed (3)

     , transpose
     , reverse
     , replace
-    , replace2
 
     -- ** Case conversion
     -- $case
 --
 -- In (unlikely) bad cases, this function's time complexity degrades
 -- towards /O(n*m)/.
-replace :: Text                 -- ^ Text to search for
-        -> Text                 -- ^ Replacement text
-        -> Text                 -- ^ Input text
-        -> Text
-replace s d = intercalate d . splitOn s
-{-# INLINE replace #-}
-
-replace2 :: Text -> Text -> Text -> Text
-replace2 needle@(Text _      _      neeLen)
-              r@(Text repArr repOff repLen)
-       haystack@(Text hayArr hayOff hayLen)
-  | neeLen == 0 = emptyError "replace2"
-  | len < 0     = overflowError "replace2"
+replace :: Text -> Text -> Text -> Text
+replace needle@(Text _      _      neeLen)
+               (Text repArr repOff repLen)
+      haystack@(Text hayArr hayOff hayLen)
+  | neeLen == 0 = emptyError "replace"
+  | len < 0     = overflowError "replace"
   | len == 0    = empty
   | L.null ixs  = haystack
   | otherwise   = Text (A.run x) 0 len
     len = hayLen - (neeLen - repLen) * cnt
     x = do
       marr <- A.new len
-      let loop is0@(i:is) o d = do
+      let loop (i:is) o d = do
             let d0 = d + i - o
                 d1 = d0 + repLen
             A.copyI marr d  hayArr (hayOff+o) d0

benchmarks/haskell/Benchmarks/Replace.hs

         !b = T.encodeUtf8 t
     return $ bgroup "Replace" [
           bench "Text"           $ nf (T.length . T.replace tpat tsub) t
-        , bench "Text2"          $ nf (T.length . T.replace2 tpat tsub) t
         , bench "ByteString"     $ nf (BL.length . B.replace bpat bsub) b
         , bench "LazyText"       $ nf (TL.length . TL.replace tlpat tlsub) tl
         , bench "LazyByteString" $ nf (BL.length . BL.replace blpat blsub) bl

tests/Tests/Properties.hs

 
 t_replace s d     = (L.intercalate d . splitOn s) `eqP`
                     (unpackS . T.replace (T.pack s) (T.pack d))
-t_replace2 (NotEmpty s0) (NotEmpty d0) (NotEmpty w0) =
-    T.replace s d w =^= T.replace2 s d w
-  where s = f s0; d = f d0; w = f w0
-        f = T.tail . T.pack
 tl_replace s d     = (L.intercalate d . splitOn s) `eqP`
                      (unpackS . TL.replace (TL.pack s) (TL.pack d))
 
       testProperty "tl_reverse" tl_reverse,
       testProperty "t_reverse_short" t_reverse_short,
       testProperty "t_replace" t_replace,
-      testProperty "t_replace2" t_replace2,
       testProperty "tl_replace" tl_replace,
 
       testGroup "case conversion" [