@@ -359,7 +359,10 @@ _replace(io, repl, str, r, pattern) = print(io, repl)
359359_replace (io, repl:: Function , str, r, pattern) =
360360 print (io, repl (SubString (str, first (r), last (r))))
361361
362- function replace (str:: String , pattern, repl, limit:: Integer )
362+ # TODO : rename to `replace` when `replace` is removed from deprecated.jl
363+ function replace_new (str:: String , pattern, repl, count:: Integer )
364+ count == 0 && return str
365+ count < 0 && throw (DomainError ())
363366 n = 1
364367 e = endof (str)
365368 i = a = start (str)
@@ -384,25 +387,29 @@ function replace(str::String, pattern, repl, limit::Integer)
384387 end
385388 r = search (str,pattern,k)
386389 j, k = first (r), last (r)
387- n == limit && break
390+ n == count && break
388391 n += 1
389392 end
390393 write (out, SubString (str,i))
391394 String (take! (out))
392395end
393396
394397"""
395- replace(string ::AbstractString, pat, r[, n ::Integer=0 ])
398+ replace(s ::AbstractString, pat, r, [count ::Integer])
396399
397- Search for the given pattern `pat`, and replace each occurrence with `r`. If `n` is
398- provided, replace at most `n` occurrences. As with search, the second argument may be a
400+ Search for the given pattern `pat` in `s`, and replace each occurrence with `r`.
401+ If `count` is provided, replace at most `count` occurrences.
402+ As with [`search`](@ref), the second argument may be a
399403single character, a vector or a set of characters, a string, or a regular expression. If `r`
400404is a function, each occurrence is replaced with `r(s)` where `s` is the matched substring.
401405If `pat` is a regular expression and `r` is a `SubstitutionString`, then capture group
402406references in `r` are replaced with the corresponding matched text.
403407"""
404- replace (s:: AbstractString , pat, f, n:: Integer ) = replace (String (s), pat, f, n)
405- replace (s:: AbstractString , pat, r) = replace (s, pat, r, 0 )
408+ replace (s:: AbstractString , pat, f) = replace_new (String (s), pat, f, typemax (Int))
409+ # TODO : change this to the following when `replace` is removed from deprecated.jl:
410+ # replace(s::AbstractString, pat, f, count::Integer=typemax(Int)) =
411+ # replace(String(s), pat, f, count)
412+
406413
407414# hex <-> bytes conversion
408415
0 commit comments