Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make replace with count=0 a no-op #22325

Merged
merged 13 commits into from
Jul 4, 2017
Merged
Prev Previous commit
Next Next commit
use typemax(Int) instead of -1 for unlimited (nalimilan)
  • Loading branch information
rfourquet committed Jul 4, 2017
commit d387b316fb2141b39eabbc0a3986737073d73ffd
13 changes: 7 additions & 6 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,9 @@ _replace(io, repl, str, r, pattern) = print(io, repl)
_replace(io, repl::Function, str, r, pattern) =
print(io, repl(SubString(str, first(r), last(r))))

function replace(str::String, pattern, repl, limit::Integer)
function replace(str::String, pattern, repl, limit::Int)
limit == 0 && return str
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you're at it, maybe rename the argument so that it's consistent with the docstring (or change the docstring)?

limit < 0 && throw(DomainError())
n = 1
e = endof(str)
i = a = start(str)
Expand Down Expand Up @@ -393,18 +394,18 @@ function replace(str::String, pattern, repl, limit::Integer)
end

"""
replace(string::AbstractString, pat, r, [n::Integer])
replace(string::AbstractString, pat, r, [count::Integer])

Search for the given pattern `pat`, and replace each occurrence with `r`.
If `n` is provided, replace at most `n` occurrences (if `n < 0`, replace
all occurrences). As with search, the second argument may be a
If `count` is provided, replace at most `count` occurrences.
As with search, the second argument may be a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

search would be good as a cross reference

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

single character, a vector or a set of characters, a string, or a regular expression. If `r`
is a function, each occurrence is replaced with `r(s)` where `s` is the matched substring.
If `pat` is a regular expression and `r` is a `SubstitutionString`, then capture group
references in `r` are replaced with the corresponding matched text.
"""
replace(s::AbstractString, pat, f, n::Integer) = replace(String(s), pat, f, n)
replace(s::AbstractString, pat, r) = replace(s, pat, r, -1)
replace(s::AbstractString, pat, f, n::Integer=typemax(Int)) =
replace(String(s), pat, f, Int(n))

# hex <-> bytes conversion

Expand Down