You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Szymon Stępniak (JIRA)" <ji...@apache.org> on 2018/10/23 10:07:00 UTC
[jira] [Created] (GROOVY-8855) Calling Matcher.asBoolean() twice
returns different results
Szymon Stępniak created GROOVY-8855:
---------------------------------------
Summary: Calling Matcher.asBoolean() twice returns different results
Key: GROOVY-8855
URL: https://issues.apache.org/jira/browse/GROOVY-8855
Project: Groovy
Issue Type: Bug
Components: groovy-runtime
Affects Versions: 2.5.3, 3.0.0-alpha-3
Reporter: Szymon Stępniak
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:
{code:groovy}
def pattern = /[a-z]+/
def input = 'abc'
def matcher = input =~ pattern
println matcher as Boolean
println matcher as Boolean
{code}
Output:
{code:bash}
true
false
{code}
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.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)