Skip to content
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

Optimize Regex match check #3779

Merged
merged 5 commits into from
Apr 2, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Apply review
HalidOdat committed Apr 2, 2024
commit 3d108246ee39ce508ee71e9f92e3d92f91d90464
49 changes: 18 additions & 31 deletions core/engine/src/builtins/regexp/mod.rs
Original file line number Diff line number Diff line change
@@ -944,10 +944,6 @@ impl RegExp {
// 12. NOTE: Each element of input is considered to be a character.

// TODO: Comment spec deviation
// TODO: It would be better to put this in an enum.
// enum Matches { Utf16(..), Ucs2(..) }
let mut utf16_matches = matcher.find_from_utf16(input, last_index as usize);
let mut ucs2_matches = matcher.find_from_ucs2(input, last_index as usize);

// 10. Let matchSucceeded be false.
// 13. Repeat, while matchSucceeded is false,
@@ -963,42 +959,33 @@ impl RegExp {
return Ok(None);
}

// b. Let inputIndex be the index into input of the character that was obtained from element lastIndex of S.
// c. Let r be matcher(input, inputIndex).
// b. Let inputIndex be the index into input of the character that was obtained from element lastIndex of S.
// c. Let r be matcher(input, inputIndex).
let r: Option<regress::Match> = if full_unicode {
utf16_matches.next()
matcher.find_from_utf16(input, last_index as usize).next()
} else {
ucs2_matches.next()
matcher.find_from_ucs2(input, last_index as usize).next()
};

let match_value = match r {
let Some(match_value) = r else {
// d. If r is failure, then
None => {
// i. If global is true or sticky is true, then
if global || sticky {
// 1. Perform ? Set(R, "lastIndex", +0𝔽, true).
this.set(utf16!("lastIndex"), 0, true, context)?;
}

return Ok(None);
// i. If global is true or sticky is true, then
if global || sticky {
// 1. Perform ? Set(R, "lastIndex", +0𝔽, true).
this.set(utf16!("lastIndex"), 0, true, context)?;
}
// i. Assert: r is a State.
Some(m) => {
if sticky && m.start() != last_index as usize {
// 1. Perform ? Set(R, "lastIndex", +0𝔽, true).
this.set(utf16!("lastIndex"), 0, true, context)?;

// 2. Return null.
return Ok(None);
}

// FIXME: Fix unicode regex

// ii. Set matchSucceeded to true.
m
}
return Ok(None);
};

if sticky && match_value.start() != last_index as usize {
// 1. Perform ? Set(R, "lastIndex", +0𝔽, true).
this.set(utf16!("lastIndex"), 0, true, context)?;

// 2. Return null.
return Ok(None);
}

// 14. Let e be r's endIndex value.
let e = match_value.end();
last_index = match_value.start() as u64;