aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorinoas <mail@inoas.com>2023-05-29 12:34:28 +0000
committerGitHub <noreply@github.com>2023-05-29 13:34:28 +0100
commitf8945cba4b9ed2d5490cdf13be1c441fcf81d33f (patch)
treec1ebe5990396082d6f4621fb2e5a5c78080a7540
parent940401f74cd2a35f7639ad8116a91d8af06925c0 (diff)
downloadgleam_stdlib-f8945cba4b9ed2d5490cdf13be1c441fcf81d33f.tar.gz
gleam_stdlib-f8945cba4b9ed2d5490cdf13be1c441fcf81d33f.zip
Fix repeated `regex.check()` calls on the same regex on target javascript (#459)
-rw-r--r--CHANGELOG.md2
-rw-r--r--src/gleam_stdlib.mjs1
-rw-r--r--test/gleam/regex_test.gleam24
3 files changed, 26 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3ce51e9..a5ce2ef 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,8 @@
## Unreleased
+- Fixed a bug on target JavaScript where `regex.check` would not correctly execute
+ while using the same regular expression in consecutive calls.
- The `zip` function's second argument in the `list` module gains the `with` label.
- The `strict_zip` function's second argument in the `list` module gains the `with` label.
diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs
index 7c41cdd..1163c6f 100644
--- a/src/gleam_stdlib.mjs
+++ b/src/gleam_stdlib.mjs
@@ -371,6 +371,7 @@ export function utf_codepoint_to_int(utf_codepoint) {
}
export function regex_check(regex, string) {
+ regex.lastIndex = 0;
return regex.test(string);
}
diff --git a/test/gleam/regex_test.gleam b/test/gleam/regex_test.gleam
index eb84013..d97c8fa 100644
--- a/test/gleam/regex_test.gleam
+++ b/test/gleam/regex_test.gleam
@@ -27,7 +27,7 @@ pub fn compile_test() {
regex.check(re, "abc\n123")
|> should.be_true
- // For Erlang: This test will only passes if unicode and ucp flags are set
+ // On target Erlang this test will only pass if unicode and ucp flags are set
let assert Ok(re) = regex.compile("\\s", options)
// Em space == U+2003 == " " == used below
regex.check(re, " ")
@@ -42,6 +42,28 @@ pub fn check_test() {
regex.check(re, "boo")
|> should.be_false
+
+ re
+ |> regex.check(content: "foo")
+ |> should.be_true
+
+ "boo"
+ |> regex.check(with: re)
+ |> should.be_false
+
+ // On target JavaScript internal `RegExp` objects are stateful when they
+ // have the global or sticky flags set (e.g., /foo/g or /foo/y).
+ // These following tests make sure that our implementation circumvents this.
+ let assert Ok(re) = regex.from_string("^-*[0-9]+")
+
+ regex.check(re, "1")
+ |> should.be_true
+
+ regex.check(re, "12")
+ |> should.be_true
+
+ regex.check(re, "123")
+ |> should.be_true
}
pub fn split_test() {