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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
replace with count=0 is a no-op
  • Loading branch information
rfourquet committed Jul 4, 2017
commit 76885174d39c1a6003c2cb356247bf99d0be76f0
3 changes: 2 additions & 1 deletion base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ _replace(io, repl::Function, str, r, pattern) =
print(io, repl(SubString(str, first(r), last(r))))

function replace(str::String, pattern, repl, limit::Integer)
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)?

n = 1
e = endof(str)
i = a = start(str)
Expand Down Expand Up @@ -402,7 +403,7 @@ If `pat` is a regular expression and `r` is a `SubstitutionString`, then capture
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, 0)
replace(s::AbstractString, pat, r) = replace(s, pat, r, -1)
Copy link
Contributor

Choose a reason for hiding this comment

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

should probably mention this in the docstring

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok (Note that it's not mentioned in Python's docstring either)

Copy link
Contributor

@tkelman tkelman Jun 10, 2017

Choose a reason for hiding this comment

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

Why is python relevant? The docstring above says "If n is provided, replace at most n occurrences." So if there's a special meaning to negative inputs, best to describe that. We don't do as many sign-based behavior changes as Python does, like negative indexing.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, that was not so relevant, only as a mature language which doesn't mention the behavior for negative inputs, which is same as here, i.e. replace all. Will add that.


# hex <-> bytes conversion

Expand Down
7 changes: 7 additions & 0 deletions test/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ end
# Issue 13332
@test replace("abc", 'b', 2.1) == "a2.1c"

# test replace with a count, check that replace is a no-op if count==0
@test replace("aaa", 'a', 'z', 0) == "aaa"
@test replace("aaa", 'a', 'z', 1) == "zaa"
@test replace("aaa", 'a', 'z', 2) == "zza"
@test replace("aaa", 'a', 'z', 3) == "zzz"
@test replace("aaa", 'a', 'z', 4) == "zzz"

# chomp/chop
@test chomp("foo\n") == "foo"
@test chop("fooε") == "foo"
Expand Down