Skip to content

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

Merged
merged 1 commit into from
Mar 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Data/String/Regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ exports.flags = function (r) {

exports.test = function (r) {
return function (s) {
return r.test(s);
var lastIndex = r.lastIndex;
var result = r.test(s);
r.lastIndex = lastIndex;
return result;
};
};

Expand Down
4 changes: 3 additions & 1 deletion src/Data/String/Regex.purs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ parseFlags s =
, unicode: contains "u" s
}

-- | Returns `true` if the `Regex` matches the string.
-- | Returns `true` if the `Regex` matches the string. In contrast to
-- | `RegExp.prototype.test()` in JavaScript, `test` does not affect
-- | the `lastIndex` property of the Regex.
foreign import test :: Regex -> String -> Boolean

foreign import _match :: (forall r. r -> Maybe r)
Expand Down
7 changes: 7 additions & 0 deletions test/Test/Data/String/Regex.purs
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,10 @@ testStringRegex = do
assert $ split (regex' "" noFlags) "abc" == ["a", "b", "c"]
assert $ split (regex' "b" noFlags) "" == [""]
assert $ split (regex' "b" noFlags) "abc" == ["a", "c"]

log "test"
-- Ensure that we have referential transparency for calls to 'test'. No
-- global state should be maintained between these two calls:
let pattern = regex' "a" (parseFlags "g")
assert $ test pattern "a"
assert $ test pattern "a"