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

use Char replacement function for Charpattern #25815

Merged
merged 5 commits into from
Feb 22, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
7 changes: 6 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ Breaking changes

This section lists changes that do not have deprecation warnings.

* `replace(s::AbstractString, pat=>repl)` if repl is a function, now calls
Copy link
Member

Choose a reason for hiding this comment

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

This isn't clear about what the change was. Maybe:

`replace(s::AbstractString, pat=>repl)` for function `repl` arguments formerly passed a substring
to `repl` in all cases.  It now passes substrings for string patterns `pat`, but a `Char`
for character patterns (when `pat` is a `Char`, collection of `Char`, or a character predicate).

`repl(s[pos]::Char)`, when pat is a `Char` or collection of `Char` or predicate,
Copy link
Member

Choose a reason for hiding this comment

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

Seems easier to just say now calls repl(c) for each matched character c::Char, instead of a substring as it did previously. No need to define pos, and it would be clearer to say what the old behavior was.

to determine the replacement of a single character. `pos` is the index of the
matched character.

* `readuntil` now does *not* include the delimiter in its result, matching the
behavior of `readline`. Pass `keep=true` to get the old behavior ([#25633]).

Expand Down Expand Up @@ -1260,4 +1265,4 @@ Command-line option changes
[#25622]: https://github.com/JuliaLang/julia/issues/25622
[#25634]: https://github.com/JuliaLang/julia/issues/25634
[#25654]: https://github.com/JuliaLang/julia/issues/25654
[#25655]: https://github.com/JuliaLang/julia/issues/25655
[#25655]: https://github.com/JuliaLang/julia/issues/25655
8 changes: 6 additions & 2 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@ end
_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))))
_replace(io, repl::Function, str, r, pattern::Function) =
print(io, repl(str[first(r)]))

replace(str::String, pat_repl::Pair{Char}; count::Integer=typemax(Int)) =
replace(str, equalto(first(pat_repl)) => last(pat_repl); count=count)
Expand Down Expand Up @@ -438,8 +440,10 @@ end
Search for the given pattern `pat` in `s`, and replace each occurrence with `r`.
If `count` is provided, replace at most `count` occurrences.
`pat` may be a 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.
or a regular expression.
If `r` is a function, each occurrence is replaced with `r(s)`
where `s` is the matched substring (when `pat`is a `Regex` or `AbstractString`) or
character (when `pat` is a `Char` or a collection of `Char`).
If `pat` is a regular expression and `r` is a `SubstitutionString`, then capture group
references in `r` are replaced with the corresponding matched text.
To remove instances of `pat` from `string`, set `r` to the empty `String` (`""`).
Expand Down
8 changes: 8 additions & 0 deletions test/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,14 @@ end

# Issue 25741
@test replace("abc", ['a', 'd'] => 'A') == "Abc"

# for Char pattern call Char replacement function
@test replace("a", "a" => typeof) == "SubString{String}"
@test replace("a", r"a" => typeof) == "SubString{String}"
@test replace("a", 'a' => typeof) == "Char"
@test replace("a", occursin("a") => typeof) == "Char"
@test replace("a", ['a'] => typeof) == "Char"

end

@testset "chomp/chop" begin
Expand Down