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 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
7 changes: 6 additions & 1 deletion 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(getindex(str, first(r))))
Copy link
Member

Choose a reason for hiding this comment

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

Why not 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,11 @@ 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`
or a regular expression.
If `pat` is a `Regex` or `AbstractString` and `r`
is a function, each occurrence is replaced with `r(s)` where `s` is the matched substring.
If `pat` is a `Char`, a collection of `Char` or a predicate function and `r`
Copy link
Member

Choose a reason for hiding this comment

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

Instead of repeating the full sentence, maybe just change where `s` is the matched substring. to where `x` is the matched substring (when `pat` is a `Regex` or `AbstractString`) or character (when `pat` is a `Char` or a collection of `Char`).?

is a function, each occurrence is replaced with `r(s)` where `s` is the matched character.
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
9 changes: 9 additions & 0 deletions test/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,15 @@ end

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

# for Char pattern call Char replacement function
@test replace("a", "a" => sizeof) == "1"
Copy link
Member

Choose a reason for hiding this comment

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

Rather than sizeof, better use typeof.

@test replace("a", r"a" => sizeof) == "1"
@test replace("a", 'a' => sizeof) == "4"
@test replace("a", occursin("a") => sizeof) == "4"
@test replace("a", ['a'] => sizeof) == "4"


end

@testset "chomp/chop" begin
Expand Down