on master help sign in
Remove the old version of replace

This provides a nice performance boost!

commit1aa063b7c45cb7f3fcef1e2316bdfd665ad1ebc6 authorBryan O'Sullivan <bos@serpentine.com> dateTue 3 Dec 2013, 22:26 (13 years ago) parent3cf8564
3 files changed+7−21
Data/Text.hs+7−15 benchmarks/haskell/Benchmarks/Replace.hs−1 tests/Tests/Properties.hs−5
Data/Text.hs+7−15
@@ -73,7 +73,6 @@ module Data.Text
7373 , transpose
7474 , reverse
7575 , replace
76− , replace2
7776
7877 -- ** Case conversion
7978 -- $case
@@ -617,19 +616,12 @@ reverse t = S.reverse (stream t)
617616--
618617-- In (unlikely) bad cases, this function's time complexity degrades
619618-- towards /O(n*m)/.
620−replace :: Text -- ^ Text to search for
621− -> Text -- ^ Replacement text
622− -> Text -- ^ Input text
623− -> Text
624−replace s d = intercalate d . splitOn s
625−{-# INLINE replace #-}
626−
627−replace2 :: Text -> Text -> Text -> Text
628−replace2 needle@(Text _ _ neeLen)
629− r@(Text repArr repOff repLen)
630− haystack@(Text hayArr hayOff hayLen)
631− | neeLen == 0 = emptyError "replace2"
632− | len < 0 = overflowError "replace2"
619+replace :: Text -> Text -> Text -> Text
620+replace needle@(Text _ _ neeLen)
621+ (Text repArr repOff repLen)
622+ haystack@(Text hayArr hayOff hayLen)
623+ | neeLen == 0 = emptyError "replace"
624+ | len < 0 = overflowError "replace"
633625 | len == 0 = empty
634626 | L.null ixs = haystack
635627 | otherwise = Text (A.run x) 0 len
@@ -639,7 +631,7 @@ replace2 needle@(Text _ _ neeLen)
639631 len = hayLen - (neeLen - repLen) * cnt
640632 x = do
641633 marr <- A.new len
642− let loop is0@(i:is) o d = do
634+ let loop (i:is) o d = do
643635 let d0 = d + i - o
644636 d1 = d0 + repLen
645637 A.copyI marr d hayArr (hayOff+o) d0
benchmarks/haskell/Benchmarks/Replace.hs−1
@@ -28,7 +28,6 @@ benchmark fp pat sub = do
2828 !b = T.encodeUtf8 t
2929 return $ bgroup "Replace" [
3030 bench "Text" $ nf (T.length . T.replace tpat tsub) t
31− , bench "Text2" $ nf (T.length . T.replace2 tpat tsub) t
3231 , bench "ByteString" $ nf (BL.length . B.replace bpat bsub) b
3332 , bench "LazyText" $ nf (TL.length . TL.replace tlpat tlsub) tl
3433 , bench "LazyByteString" $ nf (BL.length . BL.replace blpat blsub) bl
tests/Tests/Properties.hs−5
@@ -234,10 +234,6 @@ t_reverse_short n = L.reverse `eqP` (unpackS . S.reverse . shorten n . S.stream)
234234
235235t_replace s d = (L.intercalate d . splitOn s) `eqP`
236236 (unpackS . T.replace (T.pack s) (T.pack d))
237−t_replace2 (NotEmpty s0) (NotEmpty d0) (NotEmpty w0) =
238− T.replace s d w =^= T.replace2 s d w
239− where s = f s0; d = f d0; w = f w0
240− f = T.tail . T.pack
241237tl_replace s d = (L.intercalate d . splitOn s) `eqP`
242238 (unpackS . TL.replace (TL.pack s) (TL.pack d))
243239
@@ -900,7 +896,6 @@ tests =
900896 testProperty "tl_reverse" tl_reverse,
901897 testProperty "t_reverse_short" t_reverse_short,
902898 testProperty "t_replace" t_replace,
903− testProperty "t_replace2" t_replace2,
904899 testProperty "tl_replace" tl_replace,
905900
906901 testGroup "case conversion" [