Skip to content

Commit 99ed744

Browse files
committed
impl atomic group & possessive repeat
1 parent a9ed7de commit 99ed744

2 files changed

Lines changed: 136 additions & 4 deletions

File tree

‎src/engine.rs‎

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,11 @@ enum Jump {
267267
RepeatOne2,
268268
MinRepeatOne1,
269269
MinRepeatOne2,
270+
AtomicGroup1,
271+
PossessiveRepeat1,
272+
PossessiveRepeat2,
273+
PossessiveRepeat3,
274+
PossessiveRepeat4,
270275
}
271276

272277
fn _match<S: StrDrive>(req: &Request<S>, state: &mut State, ctx: MatchContext) -> bool {
@@ -445,6 +450,73 @@ fn _match<S: StrDrive>(req: &Request<S>, state: &mut State, ctx: MatchContext) -
445450
ctx.jump = Jump::MinRepeatOne1;
446451
continue 'context;
447452
}
453+
Jump::AtomicGroup1 => {
454+
if popped_result {
455+
ctx.skip_code_from(req, 1);
456+
ctx.string_position = state.string_position;
457+
ctx.string_offset = req.string.offset(0, state.string_position);
458+
// dispatch opcode
459+
} else {
460+
state.string_position = ctx.string_position;
461+
break 'result false;
462+
}
463+
}
464+
Jump::PossessiveRepeat1 => {
465+
let min_count = ctx.peek_code(req, 2) as isize;
466+
if ctx.count < min_count {
467+
break 'context ctx.next_offset(4, Jump::PossessiveRepeat2);
468+
}
469+
// zero match protection
470+
ctx.string_position = usize::MAX;
471+
ctx.jump = Jump::PossessiveRepeat3;
472+
continue 'context;
473+
}
474+
Jump::PossessiveRepeat2 => {
475+
if popped_result {
476+
ctx.count += 1;
477+
ctx.jump = Jump::PossessiveRepeat1;
478+
continue 'context;
479+
} else {
480+
state.string_position = ctx.string_position;
481+
break 'result false;
482+
}
483+
}
484+
Jump::PossessiveRepeat3 => {
485+
let max_count = ctx.peek_code(req, 3) as usize;
486+
if ((ctx.count as usize) < max_count || max_count == MAXREPEAT)
487+
&& ctx.string_position != state.string_position
488+
{
489+
state.marks.push();
490+
ctx.string_position = state.string_position;
491+
ctx.string_offset = req.string.offset(0, state.string_position);
492+
break 'context ctx.next_offset(4, Jump::PossessiveRepeat4);
493+
}
494+
ctx.string_position = state.string_position;
495+
ctx.string_offset = req.string.offset(0, state.string_position);
496+
// popped_result = false;
497+
// ctx.jump = Jump::PossessiveRepeat4;
498+
// continue 'context;
499+
ctx.skip_code_from(req, 1);
500+
ctx.skip_code(1);
501+
// if ctx.remaining_codes(req) > 1 && ctx.toplevel {
502+
// ctx.skip_code(1);
503+
// }
504+
}
505+
Jump::PossessiveRepeat4 => {
506+
if popped_result {
507+
state.marks.pop_discard();
508+
ctx.count += 1;
509+
ctx.jump = Jump::PossessiveRepeat3;
510+
continue 'context;
511+
}
512+
state.marks.pop();
513+
state.string_position = ctx.string_position;
514+
ctx.skip_code_from(req, 1);
515+
ctx.skip_code(1);
516+
// if ctx.remaining_codes(req) > 1 && ctx.toplevel {
517+
// ctx.skip_code(1);
518+
// }
519+
}
448520
}
449521
ctx.jump = Jump::OpCode;
450522

@@ -759,15 +831,43 @@ fn _match<S: StrDrive>(req: &Request<S>, state: &mut State, ctx: MatchContext) -
759831
ctx.skip_code_from(req, 2)
760832
}
761833
}
834+
/* <ATOMIC_GROUP> <skip> pattern <SUCCESS> tail */
835+
SreOpcode::ATOMIC_GROUP => {
836+
state.string_position = ctx.string_position;
837+
break 'context ctx.next_offset(2, Jump::AtomicGroup1);
838+
}
839+
/* <POSSESSIVE_REPEAT> <skip> <1=min> <2=max> pattern
840+
<SUCCESS> tail */
841+
SreOpcode::POSSESSIVE_REPEAT => {
842+
state.string_position = ctx.string_position;
843+
ctx.count = 0;
844+
ctx.jump = Jump::PossessiveRepeat1;
845+
continue 'context;
846+
}
847+
/* <POSSESSIVE_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS>
848+
tail */
849+
SreOpcode::POSSESSIVE_REPEAT_ONE => {
850+
let min_count = ctx.peek_code(req, 2) as usize;
851+
let max_count = ctx.peek_code(req, 3) as usize;
852+
if ctx.remaining_chars(req) < min_count {
853+
break 'result false;
854+
}
855+
state.string_position = ctx.string_position;
856+
let mut count_ctx = ctx;
857+
count_ctx.skip_code(4);
858+
let count = _count(req, state, count_ctx, max_count);
859+
if count < min_count {
860+
break 'result false;
861+
}
862+
ctx.skip_char(req, count);
863+
ctx.skip_code_from(req, 1);
864+
}
762865
SreOpcode::CHARSET => todo!(),
763866
SreOpcode::BIGCHARSET => todo!(),
764867
SreOpcode::NEGATE => todo!(),
765868
SreOpcode::RANGE => todo!(),
766869
SreOpcode::RANGE_UNI_IGNORE => todo!(),
767870
SreOpcode::SUBPATTERN => todo!(),
768-
SreOpcode::ATOMIC_GROUP => todo!(),
769-
SreOpcode::POSSESSIVE_REPEAT => todo!(),
770-
SreOpcode::POSSESSIVE_REPEAT_ONE => todo!(),
771871
}
772872
}
773873
};
@@ -1906,7 +2006,7 @@ fn _count<S: StrDrive>(
19062006

19072007
while count < max_count {
19082008
let sub_ctx = MatchContext {
1909-
toplevel: true,
2009+
toplevel: false,
19102010
jump: Jump::OpCode,
19112011
repeat_ctx_id: usize::MAX,
19122012
count: -1,

‎tests/tests.rs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,36 @@ fn test_repeat_in_assertions() {
130130
// END GENERATED
131131
let (req, mut state) = p.state("abc");
132132
assert!(state.search(req));
133+
}
134+
135+
#[test]
136+
fn test_possessive_quantifier() {
137+
// pattern p = re.compile('e++a')
138+
// START GENERATED by generate_tests.py
139+
#[rustfmt::skip] let p = Pattern { code: &[14, 4, 0, 2, 4294967295, 29, 6, 1, 4294967295, 16, 101, 1, 16, 97, 1] };
140+
// END GENERATED
141+
let (req, mut state) = p.state("eeea");
142+
assert!(state.pymatch(&req));
143+
}
144+
145+
#[test]
146+
fn test_possessive_atomic_group() {
147+
// pattern p = re.compile('(?>x)++x')
148+
// START GENERATED by generate_tests.py
149+
#[rustfmt::skip] let p = Pattern { code: &[14, 4, 0, 2, 4294967295, 28, 8, 1, 4294967295, 27, 4, 16, 120, 1, 1, 16, 120, 1] };
150+
// END GENERATED
151+
let (req, mut state) = p.state("xxx");
152+
assert!(!state.pymatch(&req));
153+
}
154+
155+
#[test]
156+
fn test_bug_20998() {
157+
// pattern p = re.compile('[a-c]+', re.I)
158+
// START GENERATED by generate_tests.py
159+
#[rustfmt::skip] let p = Pattern { code: &[14, 4, 0, 1, 4294967295, 24, 10, 1, 4294967295, 39, 5, 22, 97, 99, 0, 1, 1] };
160+
// END GENERATED
161+
let (mut req, mut state) = p.state("ABC");
162+
req.match_all = true;
163+
assert!(state.pymatch(&req));
164+
assert_eq!(state.string_position, 3);
133165
}

0 commit comments

Comments
 (0)