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

Calling Matcher.asBoolean() twice returns different results

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Trivial
    • Resolution: Fixed
    • Affects Version/s: 3.0.0-alpha-3, 2.5.3
    • Fix Version/s: 3.0.0-rc-2
    • Component/s: groovy-runtime
    • Labels:

      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

            • Assignee:
              emilles Eric Milles
              Reporter:
              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