on master help sign in
Further improve docs for replace
commitd6375a1ec4fbc58a18cc6b5c6ce9e13a9977a4a6 authorBryan O'Sullivan <bos@serpentine.com> dateWed 3 Dec 2014, 09:33 (12 years ago) parent52e38da
2 files changed+35−6
Data/Text.hs+15−5 Data/Text/Lazy.hs+20−1
Data/Text.hs+15−5
@@ -666,21 +666,31 @@ reverse :: Text -> Text
666666reverse t = S.reverse (stream t)
667667{-# INLINE reverse #-}
668668
669−-- | /O(m+n)/ Replace every occurrence of @needle@ in @haystack@ with @repl@.
669+-- | /O(m+n)/ Replace every non-overlapping occurrence of @needle@ in
670+-- @haystack@ with @replacement@.
670671--
671−-- If @needle@ occurs in @repl@, it won't be replaced twice:
672+-- This function behaves as though it was defined as follows:
673+--
674+--@
675+-- replace needle replacement haystack =
676+-- 'intercalate' replacement ('splitOn' needle haystack)
677+--@
678+--
679+-- As this suggests, each occurrence is replaced exactly once. So if
680+-- @needle@ occurs in @replacement@, that occurrence will /not/ itself
681+-- be replaced recursively:
672682--
673683-- > replace "oo" "foo" "oo" == "foo"
674684--
675−-- If several instances of @needle@ overlap, only the first one will be
676−-- replaced:
685+-- In cases where several instances of @needle@ overlap, only the
686+-- first one will be replaced:
677687--
678688-- > replace "ofo" "bar" "ofofo" == "barfo"
679689--
680690-- In (unlikely) bad cases, this function's time complexity degrades
681691-- towards /O(n*m)/.
682692replace :: Text -- ^ @needle@ to search for
683− -> Text -- ^ @repl@ to replace @needle@ with
693+ -> Text -- ^ @replacement@ to replace @needle@ with
684694 -> Text -- ^ @haystack@ in which to search
685695 -> Text
686696replace needle@(Text _ _ neeLen)
Data/Text/Lazy.hs+20−1
@@ -663,7 +663,26 @@ reverse = rev Empty
663663 where rev a Empty = a
664664 rev a (Chunk t ts) = rev (Chunk (T.reverse t) a) ts
665665
666−-- | /O(m+n)/ Replace every occurrence of one substring with another.
666+-- | /O(m+n)/ Replace every non-overlapping occurrence of @needle@ in
667+-- @haystack@ with @replacement@.
668+--
669+-- This function behaves as though it was defined as follows:
670+--
671+--@
672+-- replace needle replacement haystack =
673+-- 'intercalate' replacement ('splitOn' needle haystack)
674+--@
675+--
676+-- As this suggests, each occurrence is replaced exactly once. So if
677+-- @needle@ occurs in @replacement@, that occurrence will /not/ itself
678+-- be replaced recursively:
679+--
680+-- > replace "oo" "foo" "oo" == "foo"
681+--
682+-- In cases where several instances of @needle@ overlap, only the
683+-- first one will be replaced:
684+--
685+-- > replace "ofo" "bar" "ofofo" == "barfo"
667686--
668687-- In (unlikely) bad cases, this function's time complexity degrades
669688-- towards /O(n*m)/.