-
Notifications
You must be signed in to change notification settings - Fork 74
Reset Regex state after calling 'test', resolves #7 #54
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
Conversation
@@ -29,7 +29,9 @@ exports.flags = function (r) { | |||
|
|||
exports.test = function (r) { | |||
return function (s) { | |||
return r.test(s); | |||
var result = r.test(s); | |||
r.lastIndex = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we restore the previous value of r.lastIndex
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the Purescript Regex API, there is no other function that affects lastIndex
(if I'm correct), so I thought resetting to 0
would be fine. But your solution is the safer alternative (there might be foreign code which affects the lastIndex
property). Thanks.
It's a shame it works this way, but I can see why it might be useful in code where performance is a must. Perhaps we should find a way to represent things like |
ec74012
to
ac55b2b
Compare
LGTM |
I agree that - with these changes - the global flag in a Regex is useless when using I don't really have a strong opinion on this, but using testFromIndex :: Regex -> Int -> String -> { match :: Boolean, newIndex :: Int } which could be used in a fold/loop. [*] Note that |
Could you rebase this one please? |
ac55b2b
to
f5d9f35
Compare
Previously, the second `assert` in the following code would fail: ``` purs let pattern = regex "a" (parseFlags "g") assert $ test pattern "a" assert $ test pattern "a" ``` because the RegExp object in JavaScript maintains a global state (for details, see the discussion in purescript#7). This commit resets the `lastIndex` property on the RegExp object, such that both calls to `test` return the same result.
f5d9f35
to
26941a9
Compare
Thanks! |
Previously, the second
assert
in the following code would fail:The reason is that the RegExp object in JavaScript maintains a global state (for details, see the discussion in #7).
This commit resets the
lastIndex
property on the RegExp object, such that both calls totest
return the same result.