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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions NEWS.md
Expand Up @@ -203,6 +203,11 @@ Breaking changes

This section lists changes that do not have deprecation warnings.

* `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) ([#25815]).

* `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
8 changes: 6 additions & 2 deletions base/strings/util.jl
Expand Up @@ -365,6 +365,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 @@ -413,8 +415,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
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