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

use Char replacement function for Charpattern #25815

merged 5 commits into from
Feb 22, 2018

Conversation

KlausC
Copy link
Contributor

@KlausC KlausC commented Jan 30, 2018

According to a proposal of @nalimilan in #25396 .
If the replacement pattern is a function, it should be a function of Char, if the pattern is a Char, a collection of Chars, or a predicate function (which acts on a single Char), and a function of the matched substring otherwise.

Copy link
Member

@nalimilan nalimilan left a comment

Choose a reason for hiding this comment

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

Thanks!

@@ -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.

@@ -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)]?

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`).?

Copy link
Member

@nalimilan nalimilan left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks!

@stevengj stevengj added the breaking This change will break code label Jan 31, 2018
@stevengj
Copy link
Member

This is a breaking change, yes? I can't think of any way to do a better deprecation process. Should be listed under breaking changes in the NEWS.

@stevengj stevengj added the needs news A NEWS entry is required for this change label Jan 31, 2018
NEWS.md Outdated
@@ -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).

@nalimilan nalimilan removed the needs news A NEWS entry is required for this change label Feb 3, 2018
@nalimilan
Copy link
Member

@stevengj Merge if that's OK for you?

NEWS.md Outdated
@@ -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
`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.

@stevengj
Copy link
Member

stevengj commented Feb 4, 2018

Looks okay to merge.

We might want to implement a faster replace(str::String, pat_repl::Pair{Function}) method that just loops over the characters in str, but that can be done in a later PR.

@stevengj stevengj merged commit c6708ed into JuliaLang:master Feb 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking This change will break code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants