Skip to content

Commit 5e65441

Browse files
shilangyuMultimodcrafter
authored andcommitted
Fix anchors in lookarounds
1 parent ee95174 commit 5e65441

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

regex-automata/src/nfa/thompson/compiler.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,6 +2158,29 @@ mod tests {
21582158
);
21592159
}
21602160

2161+
#[test]
2162+
fn compile_yes_unanchored_prefix_with_start_anchor_in_lookaround() {
2163+
let nfa = NFA::compiler()
2164+
.configure(NFA::config().which_captures(WhichCaptures::None))
2165+
.build(r"(?<=^)a")
2166+
.unwrap();
2167+
assert_eq!(
2168+
nfa.states(),
2169+
&[
2170+
s_bin_union(2, 1),
2171+
s_range(0, 255, 0),
2172+
s_bin_union(3, 6),
2173+
s_bin_union(5, 4),
2174+
s_range(0, 255, 3),
2175+
s_look(Look::Start, 7),
2176+
s_check_lookaround(0, true, 8),
2177+
s_write_lookaround(0),
2178+
s_byte(b'a', 9),
2179+
s_match(0)
2180+
]
2181+
);
2182+
}
2183+
21612184
#[test]
21622185
fn compile_empty() {
21632186
assert_eq!(build("").states(), &[s_match(0),]);

regex-syntax/src/hir/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2589,6 +2589,8 @@ impl Properties {
25892589
literal: false,
25902590
alternation_literal: false,
25912591
contains_lookaround_expr: true,
2592+
look_set_prefix: LookSet::empty(),
2593+
look_set_suffix: LookSet::empty(),
25922594
..*sub_p.0.clone()
25932595
};
25942596
Properties(Box::new(inner))

testdata/lookaround.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ regex = "(?<=c+)a|(?<=d+)a"
5858
haystack = "aabacadaccaddaea"
5959
matches = [[5, 6], [7, 8], [10, 11], [13, 14]]
6060

61+
[[test]]
62+
name = "lookbehind with anchor"
63+
regex = "(?<=^c)a"
64+
haystack = "cacacaasdacabasdqwe"
65+
matches = [[1, 2]]
66+
6167
[[test]]
6268
name = "lookbehind next to capture group"
6369
regex = "(?<=c)(a|b)(b|a)"

0 commit comments

Comments
 (0)