Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-1630

each(Matcher,Closure) doesn't work for patterns with groups; plus other regex issues [minor breaking change]

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.0-RC-2
    • 1.6-beta-1
    • regular expressions
    • None
    • Groovy RC2 on jre 1.6.0 rc1 on WinXP prof edn SP2

    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 }
      

      Attachments

        Issue Links

          Activity

            People

              paulk Paul King
              gavingrover Gavin Grover
              Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: