Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Minor
-
Resolution: Fixed
-
Affects Version/s: 1.0-RC-2
-
Fix Version/s: 1.6-beta-1
-
Component/s: regular expressions
-
Labels:None
-
Environment:Groovy RC2 on jre 1.6.0 rc1 on WinXP prof edn SP2
-
Number of attachments :
Description
(1) each(Matcher,Closure) in DGM doesn't work for patterns with groups
m= 'a name is just a game' =~ /a name is (.*)/
m.each{ println it }
//Caught: groovy.lang.MissingMethodException:
//No signature of method Regex.doCall() is applicable for argument types: (java.lang.String, java.lang.String)
//values: {"a name is just a game", "just a game"}
To fix, replace line:
closure.call((Object[]) groups.toArray())
with these from nearby "eachMatch" method:
if( groups.size()==1 || closure.getMaximumNumberOfParameters()<groups.size() ){ // not enough parameters there to give each group part its own parameter, // so try a closure with one parameter and give it all groups as a array closure.call( (Object)groups.toArray() ) }else{ closure.call( (Object[])groups.toArray() ) }
(2) each(Matcher,Closure) in DGM requires reset()
m= 'coffee' =~ /ee/
m.each{ println it } //prints: ee
m.each{ println it } //prints nothing
m.reset(); m.each{ println it } //prints: ee
reset() is called in these DGM regex methods: getCount, size, replaceAll, setIndex, & getAt
Why not in each() ?
To fix, before the m.find() call, put:
m.reset()
(3) MatcherIterator in DGM also requires reset():
m= 'reek coffee' =~ /ee/
println m.collect{ it }.join(',') //ee,ee
println m.collect{ it }.join(',') //prints blank
m.reset(); println m.collect{ it }.join(',') //ee,ee
To fix (I think), in MatcherIterator constructor:
MatcherIterator( Matcher m ){ m.reset(); matcher= m }
Issue Links
- is related to
-
GROOVY-1132
match vs match[] handling inconsistent for eachMatch vs. =~
-
I must remember to use html when filling out this form. Missing portion of above description is:
if( groups.size()==1 || closure.getMaximumNumberOfParameters() < groups.size() ){
// not enough parameters there to give each group part its own parameter,
// so try a closure with one parameter and give it all groups as a array
closure.call( (Object)groups.toArray() )
}else{
closure.call( (Object[])groups.toArray() )
}