Skip to content

Commit bf83397

Browse files
authored
make replace with count=0 a no-op (#22325)
1 parent c4f2737 commit bf83397

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ Deprecated or removed
127127
Ternaries must now include some amount of whitespace, e.g. `x ? a : b` rather than
128128
`x? a : b` ([#22523]).
129129

130+
* The method `replace(s::AbstractString, pat, r, count)` with `count <= 0` is deprecated
131+
in favor of `replace(s::AbstractString, pat, r, typemax(Int))` ([#22325]).
132+
130133

131134
Julia v0.6.0 Release Notes
132135
==========================

base/deprecated.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,22 @@ function bkfact!(A::StridedMatrix, uplo::Symbol, symmetric::Bool = issymmetric(A
15031503
return bkfact!(symmetric ? Symmetric(A, uplo) : Hermitian(A, uplo), rook)
15041504
end
15051505

1506+
# PR #22325
1507+
# TODO: when this replace is removed from deprecated.jl:
1508+
# 1) rename the function replace_new from strings/util.jl to replace
1509+
# 2) update the replace(s::AbstractString, pat, f) method, below replace_new
1510+
# (see instructions there)
1511+
function replace(s::AbstractString, pat, f, n::Integer)
1512+
if n <= 0
1513+
depwarn(string("`replace(s, pat, r, count)` with `count <= 0` is deprecated, use ",
1514+
"`replace(s, pat, r, typemax(Int))` or `replace(s, pat, r)` instead"),
1515+
:replace)
1516+
replace(s, pat, f)
1517+
else
1518+
replace_new(String(s), pat, f, n)
1519+
end
1520+
end
1521+
15061522
# END 0.7 deprecations
15071523

15081524
# BEGIN 1.0 deprecations

base/strings/util.jl

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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))
392395
end
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
399403
single character, a vector or a set of characters, a string, or a regular expression. If `r`
400404
is a function, each occurrence is replaced with `r(s)` where `s` is the matched substring.
401405
If `pat` is a regular expression and `r` is a `SubstitutionString`, then capture group
402406
references 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

test/strings/util.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,18 @@ end
208208
# Issue 13332
209209
@test replace("abc", 'b', 2.1) == "a2.1c"
210210

211+
# test replace with a count for String and GenericString
212+
# check that replace is a no-op if count==0
213+
for s in ["aaa", Base.Test.GenericString("aaa")]
214+
# @test replace("aaa", 'a', 'z', 0) == "aaa" # enable when undeprecated
215+
@test replace(s, 'a', 'z', 1) == "zaa"
216+
@test replace(s, 'a', 'z', 2) == "zza"
217+
@test replace(s, 'a', 'z', 3) == "zzz"
218+
@test replace(s, 'a', 'z', 4) == "zzz"
219+
@test replace(s, 'a', 'z', typemax(Int)) == "zzz"
220+
@test replace(s, 'a', 'z') == "zzz"
221+
end
222+
211223
# chomp/chop
212224
@test chomp("foo\n") == "foo"
213225
@test chop("fooε") == "foo"

0 commit comments

Comments
 (0)