New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regexes are stateful #188

Closed
j201 opened this Issue Mar 5, 2015 · 3 comments

Comments

Projects
None yet
3 participants
@j201

j201 commented Mar 5, 2015

With Elm 0.14.1, running find on a regex changes its state, possibly causing future calls to find to act differently. It looks like this is because find uses JavaScript's re.exec, which sets the lastIndex property on the regex and makes it ignore matches before that index.

Example:

let re = regex "\\d"
    matchStr = foldl String.append "" << map .match
in [matchStr <| find (AtMost 1) re "1",
    matchStr <| find (AtMost 1) re "1",
    toString <| contains re "1",
    matchStr <| find (AtMost 1) re "1"]

Produces
["1","","True","1"]

The first find probably sets lastIndex to 1, making it ignore the match in the second find. contains calls str.match(re), which clears the regex state, making the last find work.

@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

evancz Mar 6, 2015

Member

I think there was a PR that fixes this. Can you take a look at the history of Regex.js to see?

Member

evancz commented Mar 6, 2015

I think there was a PR that fixes this. Can you take a look at the history of Regex.js to see?

@nathan

This comment has been minimized.

Show comment
Hide comment
@nathan

nathan Mar 6, 2015

Contributor

It looks like https://github.com/elm-lang/core/pull/156 fixed this.

Contributor

nathan commented Mar 6, 2015

It looks like https://github.com/elm-lang/core/pull/156 fixed this.

@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

evancz Mar 6, 2015

Member

Cool, thanks for finding it @nathan! Let's close this until it can be reproduced with the latest version of the Regex library.

Member

evancz commented Mar 6, 2015

Cool, thanks for finding it @nathan! Let's close this until it can be reproduced with the latest version of the Regex library.

@evancz evancz closed this Mar 6, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment