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

Calling Matcher.asBoolean() twice returns different results

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Trivial
    • Resolution: Fixed
    • 3.0.0-alpha-3, 2.5.3
    • 3.0.0-rc-2
    • groovy-runtime

    Description

      There is one non-deterministic use case of using type coercion from java.util.regex.Matcher to a boolean. It was initially reported on Stack Overflow - https://stackoverflow.com/q/52930876/2194470

      And here is an example that shows the problem:

      def pattern = /[a-z]+/
      
      def input = 'abc'
      
      def matcher = input =~ pattern
      
      println matcher as Boolean
      println matcher as Boolean
      

      Output:

      true
      false
      

      Solution:

      This problem can be solved by replacing matcher.find() with matcher.find(0), so casting matcher to a boolean does not modify search index and always starts from the beginning.

      I will create a pull request in couple of minutes.

      Attachments

        Activity

          People

            emilles Eric Milles
            wololock Szymon Stępniak
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 40m
                40m