[ 
https://issues.apache.org/jira/browse/GROOVY-7540?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14659569#comment-14659569
 ] 

Jochen Kemnade edited comment on GROOVY-7540 at 8/6/15 6:39 AM:
----------------------------------------------------------------

{quote}
There are questions to answer though, like should the method be regex aware 
{quote}
I'm not sure. I don't need it to be regex aware, but I can see how that would 
be a nice feature. I'd like to avoid the overhead of compiling a Pattern for 
simple strings though. Also, if we allow regexes, people will want to pass in a 
pattern. We cannot provide two methods ({{Map<String, String>}} and 
{{Map<Pattern, String>}}) because of type erasure, but we could allow 
{{Map<Object, String>}} and check the keys's types. Allowing regexes will 
definitely make things more complicated.
{quote}
and as per your earlier comment, are the transformations sequential in nature 
(the 'bar' and 'baz') alternatives
{quote}
I'd say no.
{code}
"foo".collectReplacements(["foo":"bar", "bar":"baz"])                        // 
should become "bar", if I want "baz", I can call
"foo".collectReplacements(["foo":"bar"]).collectReplacements(["bar":"baz"])  // 
but if the replacements are done sequentially, 
                                                                             // 
there's no way to do the "faabor" -> "foobor" example.
{code}
But I've got another one: What happens here?
{code}
"foo".collectReplacements(["f":"g", "foo":"bar"])    // goo? bar? Exception?
{code}
I'd go for exception, but those cases can be hard to detect, if not impossible, 
especially if we do allow regexes. StringUtils does this:
{code}
StringUtils.replaceEach("foo", new String[] { "f", "foo" }, new String[] { "g", 
"bar" }) // => goo
StringUtils.replaceEach("foo", new String[] { "foo", "f" }, new String[] { 
"bar", "g" }) // => bar
{code}


was (Author: jkemnade):
{quote}
There are questions to answer though, like should the method be regex aware 
{quote}
I'm not sure. I don't need it to be regex aware, but I can see how that would 
be a nice feature. I'd like to avoid the overhead of compiling a Pattern for 
simple strings though. Also, if we allow regexes, people will want to pass in a 
pattern. We cannot provide two methods ({{Map<String, String>}} and 
{{Map<Pattern, String>}}) because of type erasure, but we could allow 
{{Map<Object, String>}} and check the keys's types. Allowing regexes will 
definitely make things more complicated.
{quote}
and as per your earlier comment, are the transformations sequential in nature 
(the 'bar' and 'baz') alternatives
{quote}
I'd say no.
{code}
"foo".collectReplacements(["foo":"bar", "bar":"baz"])                        // 
should become "bar", if I want "baz", I can call
"foo".collectReplacements(["foo":"bar"]).collectReplacements(["bar":"baz"])  // 
but if the replacements are done sequentially, 
                                                                             // 
there's no way to do the "faabor" -> "foobor" example.
{code}
But I've got another one: What happens here?
{code}
"foo".collectReplacements(["f":"g", "foo":"bar"])    // goo? bar? Exception?
{code}
I'd go for exception, but those cases can be hard to detect, if not impossible, 
especially if we do allow regexes. StringUtils does this:
{code}


> Add StringGroovyMethods.collectReplacements(String, Map)
> --------------------------------------------------------
>
>                 Key: GROOVY-7540
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7540
>             Project: Groovy
>          Issue Type: Improvement
>            Reporter: Jochen Kemnade
>            Priority: Minor
>
> It should be possible to use a map with {{collectReplacements}}, like in
> {code}
> "f006ar".collectReplacements(["0":"o", "6":"b"])
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to