Skip to content

Truly independent web browser

LibRegex: Try an anchored match before searching for a literal

sideshowbarkerd320e73c49d5
diff --git a/Libraries/LibRegex/Rust/src/vm.rs b/Libraries/LibRegex/Rust/src/vm.rs
index 78d472dadb0..5feec239b39 100644
--- a/Libraries/LibRegex/Rust/src/vm.rs
+++ b/Libraries/LibRegex/Rust/src/vm.rs
@@ -391,6 +391,13 @@ fn matches_ascii_case_insensitive_u16_at<I: Input>(input: I, pos: usize, needle:
#[inline(always)]
fn fails_required_literal_hint<I: Input>(input: I, start: usize, hints: &PatternHints) -> bool {
+ if hints.starts_with_anchor && !hints.anchor_multiline {
+ return false;
+ }
+ missing_required_literal(input, start, hints)
+}
+
+fn missing_required_literal<I: Input>(input: I, start: usize, hints: &PatternHints) -> bool {
let Some(literal) = hints.required_literal.as_ref() else {
return false;
};
@@ -533,12 +540,8 @@ pub fn execute_anchored_into_with_scratch<I: Input>(
if fails_trailing_literal_hint(input, hints) {
return VmResult::NoMatch;
}
- if fails_required_literal_hint(input, start_pos, hints) {
- return VmResult::NoMatch;
- }
-
let mut vm = Vm::new(program, input, start_pos, scratch);
- let result = vm.run();
+ let result = vm.run_at_single_position(hints);
if result == VmResult::Match {
copy_captures_to_out(vm.registers, program.capture_count, out);
}
@@ -573,6 +576,22 @@ pub fn find_all_with_scratch<I: Input>(
let mut count = 0i32;
let mut pos = start_pos;
+ // Anchored non-multiline: only try the requested start position.
+ if hints.starts_with_anchor && !hints.anchor_multiline {
+ match vm.run_at_single_position(hints) {
+ VmResult::Match => {
+ if capacity >= 2 {
+ result_buf[0] = vm.registers[0];
+ result_buf[1] = vm.registers[1];
+ return 1;
+ }
+ return -1;
+ }
+ VmResult::LimitExceeded | VmResult::Interrupted => return -2,
+ VmResult::NoMatch => return 0,
+ }
+ }
+
if let Some(ref start_hint) = hints.start_position_hint
&& !program.unicode
{
@@ -600,23 +619,6 @@ pub fn find_all_with_scratch<I: Input>(
return count;
}
- // Anchored non-multiline: only try the requested start position.
- if hints.starts_with_anchor && !hints.anchor_multiline {
- vm.reset(start_pos);
- match vm.run() {
- VmResult::Match => {
- if capacity >= 2 {
- result_buf[0] = vm.registers[0];
- result_buf[1] = vm.registers[1];
- return 1;
- }
- return -1;
- }
- VmResult::LimitExceeded | VmResult::Interrupted => return -2,
- VmResult::NoMatch => return 0,
- }
- }
-
// Fast path: non-unicode pattern starting with a literal character.
if let Some((ch, false)) = hints.first_char
&& !program.unicode
@@ -740,8 +742,7 @@ fn execute_into_impl<I: Input>(
// Anchored non-multiline: only try the requested start position.
if hints.starts_with_anchor && !hints.anchor_multiline {
- vm.reset(start_pos);
- let result = vm.run();
+ let result = vm.run_at_single_position(hints);
if result == VmResult::Match {
copy_captures_to_out(vm.registers, program.capture_count, out);
}
@@ -1531,6 +1532,27 @@ impl<'a, I: Input> Vm<'a, I> {
None
}
+ fn run_at_single_position(&mut self, hints: &PatternHints) -> VmResult {
+ if hints.required_literal.is_none() {
+ return self.run();
+ }
+
+ // A single pass through the instructions can settle a prefix mismatch without searching the rest of the input.
+ // Keep the literal check for patterns that would otherwise spend their match budget backtracking.
+ let start = self.pos;
+ self.step_limit = self.step_limit.min(self.program.instructions.len() as u64 + 1);
+ self.next_checkpoint = self.next_interrupt_poll.min(self.step_limit);
+ let result = self.run();
+ if result != VmResult::LimitExceeded {
+ return result;
+ }
+ if missing_required_literal(self.input, start, hints) {
+ return VmResult::NoMatch;
+ }
+ self.reset(start);
+ self.run()
+ }
+
fn run(&mut self) -> VmResult {
// Use the fast path for non-Unicode, forward-only patterns.
if !self.program.unicode && !self.backward {
@@ -3800,3 +3822,37 @@ pub(crate) fn match_unicode_property_resolved(
}
match_unicode_property(cp, name, value)
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[derive(Clone, Copy)]
+ struct TemplateText;
+
+ impl Input for TemplateText {
+ fn len(self) -> usize {
+ 128
+ }
+
+ fn code_unit(self, pos: usize) -> u16 {
+ assert_eq!(pos, 0, "a mismatch at the start rules out the match");
+ u16::from(b'<')
+ }
+ }
+
+ #[test]
+ fn anchored_mismatch_does_not_search_the_remaining_text() {
+ let mut program = Program::new();
+ program.instructions = vec![
+ Instruction::Save(0),
+ Instruction::AssertStart { multiline: false },
+ Instruction::Char(u32::from(b'M')),
+ Instruction::Char(u32::from(b'!')),
+ Instruction::Save(1),
+ Instruction::Match,
+ ];
+ let hints = analyze_pattern(&program, None);
+ assert!(!fails_required_literal_hint(TemplateText, 0, &hints));
+ }
+}
diff --git a/Tests/LibJS/Runtime/builtins/RegExp/RegExp.prototype.exec.js b/Tests/LibJS/Runtime/builtins/RegExp/RegExp.prototype.exec.js
index fb52fc364a4..ba806751631 100644
--- a/Tests/LibJS/Runtime/builtins/RegExp/RegExp.prototype.exec.js
+++ b/Tests/LibJS/Runtime/builtins/RegExp/RegExp.prototype.exec.js
@@ -449,3 +449,18 @@ test("lookarounds restore state without outer alternatives", () => {
expect(/^(?=(?i:a))a$/.exec("a")[0]).toBe("a");
expect(/^(?!(?=(?i:a))b)a$/.exec("a")[0]).toBe("a");
});
+
+test("captures after repeated groups at a single start position", () => {
+ const text = "ab".repeat(20) + "cdEND";
+ for (const flags of ["", "u", "y"]) {
+ const regexp = new RegExp("^((ab)|(cd))+END", flags);
+ const match = regexp.exec(text);
+ expect(match[0]).toBe(text);
+ expect(match[1]).toBe("cd");
+ expect(match[2]).toBeUndefined();
+ expect(match[3]).toBe("cd");
+ regexp.lastIndex = 0;
+ expect(regexp.exec(text.slice(0, -3))).toBeNull();
+ }
+ expect(text.match(/^((ab)|(cd))+END/g)).toEqual([text]);
+});