Skip to content

Commit c4f10ed

Browse files
committed
impl opinfo single literal
1 parent e42df1d commit c4f10ed

3 files changed

Lines changed: 139 additions & 109 deletions

File tree

‎benches/benches.rs‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,29 +92,29 @@ fn benchmarks(b: &mut Bencher) {
9292

9393
b.iter(move || {
9494
for (p, s) in &tests {
95-
let (mut req, mut state) = p.state(s.clone());
96-
state.search(&mut req);
95+
let (req, mut state) = p.state(s.clone());
96+
state.search(req);
9797
assert!(state.has_matched);
98-
let (mut req, mut state) = p.state(s.clone());
99-
state.pymatch(&mut req);
98+
let (req, mut state) = p.state(s.clone());
99+
state.pymatch(req);
100100
assert!(state.has_matched);
101101
let (mut req, mut state) = p.state(s.clone());
102102
req.match_all = true;
103-
state.pymatch(&mut req);
103+
state.pymatch(req);
104104
assert!(state.has_matched);
105105
let s2 = format!("{}{}{}", " ".repeat(10000), s, " ".repeat(10000));
106-
let (mut req, mut state) = p.state_range(s2.as_str(), 0..usize::MAX);
107-
state.search(&mut req);
106+
let (req, mut state) = p.state_range(s2.as_str(), 0..usize::MAX);
107+
state.search(req);
108108
assert!(state.has_matched);
109-
let (mut req, mut state) = p.state_range(s2.as_str(), 10000..usize::MAX);
110-
state.pymatch(&mut req);
109+
let (req, mut state) = p.state_range(s2.as_str(), 10000..usize::MAX);
110+
state.pymatch(req);
111111
assert!(state.has_matched);
112-
let (mut req, mut state) = p.state_range(s2.as_str(), 10000..10000 + s.len());
113-
state.pymatch(&mut req);
112+
let (req, mut state) = p.state_range(s2.as_str(), 10000..10000 + s.len());
113+
state.pymatch(req);
114114
assert!(state.has_matched);
115115
let (mut req, mut state) = p.state_range(s2.as_str(), 10000..10000 + s.len());
116116
req.match_all = true;
117-
state.pymatch(&mut req);
117+
state.pymatch(req);
118118
assert!(state.has_matched);
119119
}
120120
})

‎src/engine.rs‎

Lines changed: 90 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// good luck to those that follow; here be dragons
22

3+
use crate::constants::SreInfo;
4+
35
use super::constants::{SreAtCode, SreCatCode, SreOpcode};
46
use super::MAXREPEAT;
57
use optional::Optioned;
@@ -10,6 +12,7 @@ const fn is_py_ascii_whitespace(b: u8) -> bool {
1012
matches!(b, b'\t' | b'\n' | b'\x0C' | b'\r' | b' ' | b'\x0B')
1113
}
1214

15+
#[derive(Debug, Clone, Copy)]
1316
pub struct Request<'a, S: StrDrive> {
1417
pub string: S,
1518
pub start: usize,
@@ -180,7 +183,7 @@ impl<S: StrDrive> State<S> {
180183
self.has_matched = self.popped_has_matched;
181184
}
182185

183-
pub fn pymatch(&mut self, req: &mut Request<S>) {
186+
pub fn pymatch(&mut self, mut req: Request<S>) {
184187
self.start = req.start;
185188
self.string_position = req.start;
186189

@@ -196,10 +199,10 @@ impl<S: StrDrive> State<S> {
196199
};
197200
self.context_stack.push(ctx);
198201

199-
self._match(req);
202+
self._match(&mut req);
200203
}
201204

202-
pub fn search(&mut self, req: &mut Request<S>) {
205+
pub fn search(&mut self, mut req: Request<S>) {
203206
self.start = req.start;
204207
self.string_position = req.start;
205208

@@ -208,12 +211,11 @@ impl<S: StrDrive> State<S> {
208211
return;
209212
}
210213

211-
// let start = self.start;
212-
// let end = self.end;
214+
let mut end = req.end;
213215

214216
let mut start_offset = req.string.offset(0, req.start);
215217

216-
let ctx = MatchContext {
218+
let mut ctx = MatchContext {
217219
string_position: req.start,
218220
string_offset: start_offset,
219221
code_position: 0,
@@ -224,35 +226,97 @@ impl<S: StrDrive> State<S> {
224226
count: -1,
225227
};
226228

227-
// if ctx.peek_code(self, 0) == SreOpcode::INFO as u32 {
228-
// search_op_info(self, &mut ctx);
229-
// if let Some(has_matched) = ctx.has_matched {
230-
// self.has_matched = has_matched;
231-
// return;
232-
// }
233-
// }
229+
if ctx.peek_code(&req, 0) == SreOpcode::INFO as u32 {
230+
/* optimization info block */
231+
/* <INFO> <1=skip> <2=flags> <3=min> <4=max> <5=prefix info> */
232+
let req = &mut req;
233+
let min = ctx.peek_code(req, 3) as usize;
234+
235+
if ctx.remaining_chars(req) < min {
236+
return;
237+
}
238+
239+
if min > 1 {
240+
/* adjust end point (but make sure we leave at least one
241+
character in there, so literal search will work) */
242+
// no overflow can happen as remaining chars >= min
243+
end -= min - 1;
244+
245+
// adjust ctx position
246+
if end < ctx.string_position {
247+
ctx.string_position = end;
248+
ctx.string_offset = req.string.offset(0, ctx.string_position);
249+
}
250+
}
251+
252+
let flags = SreInfo::from_bits_truncate(ctx.peek_code(req, 2));
253+
254+
if flags.contains(SreInfo::PREFIX) {
255+
/* pattern starts with a known prefix */
256+
/* <length> <skip> <prefix data> <overlap data> */
257+
let len = ctx.peek_code(req, 5) as usize;
258+
let skip = ctx.peek_code(req, 6) as usize;
259+
let prefix = &ctx.pattern(req)[7..];
260+
let overlap = &prefix[len - 1..];
261+
262+
if len == 1 {
263+
// pattern starts with a literal character
264+
ctx.skip_code_from(req, 1);
265+
let c = prefix[0];
266+
req.must_advance = false;
267+
268+
while !ctx.at_end(req) {
269+
// find the next matched literal
270+
while ctx.peek_char(req) != c {
271+
ctx.skip_char(req, 1);
272+
if ctx.at_end(req) {
273+
return;
274+
}
275+
}
276+
277+
req.start = ctx.string_position;
278+
self.reset(req.start);
279+
// self.start = ctx.string_position;
280+
self.string_position += skip;
281+
282+
// literal only
283+
if flags.contains(SreInfo::LITERAL) {
284+
self.has_matched = true;
285+
return;
286+
}
287+
288+
let mut next_ctx = ctx;
289+
next_ctx.skip_char(req, skip);
290+
next_ctx.skip_code(2 * skip);
291+
292+
self.context_stack.push(next_ctx);
293+
self._match(req);
294+
295+
if self.has_matched {
296+
return;
297+
}
298+
299+
ctx.skip_char(req, 1);
300+
}
301+
return;
302+
}
303+
}
304+
}
234305

235306
self.context_stack.push(ctx);
236-
self._match(req);
307+
self._match(&mut req);
237308

238309
req.must_advance = false;
239-
while !self.has_matched && req.start < req.end {
310+
ctx.toplevel = false;
311+
while !self.has_matched && req.start < end {
240312
req.start += 1;
241313
start_offset = req.string.offset(start_offset, 1);
242314
self.reset(req.start);
315+
ctx.string_position = req.start;
316+
ctx.string_offset = start_offset;
243317

244-
let ctx = MatchContext {
245-
string_position: req.start,
246-
string_offset: start_offset,
247-
code_position: 0,
248-
has_matched: None,
249-
toplevel: false,
250-
handler: None,
251-
repeat_ctx_id: usize::MAX,
252-
count: -1,
253-
};
254318
self.context_stack.push(ctx);
255-
self._match(req);
319+
self._match(&mut req);
256320
}
257321
}
258322
}
@@ -372,63 +436,6 @@ fn dispatch<S: StrDrive>(
372436
}
373437
}
374438

375-
/* optimization info block */
376-
/* <INFO> <1=skip> <2=flags> <3=min> <4=max> <5=prefix info> */
377-
// fn search_op_info<'a, S: StrDrive>(state: &mut State<'a, S>, ctx: &mut MatchContext<'a, S>) {
378-
// let min = ctx.peek_code(state, 3) as usize;
379-
380-
// if ctx.remaining_chars(state) < min {
381-
// return ctx.failure();
382-
// }
383-
384-
// if min > 1 {
385-
// /* adjust end point (but make sure we leave at least one
386-
// character in there, so literal search will work) */
387-
// // no overflow can happen as remaining chars >= min
388-
// state.end -= min - 1;
389-
390-
// // adjust ctx position
391-
// if state.end < ctx.string_position {
392-
// ctx.string_position = state.end;
393-
// ctx.string_offset = state.string.offset(0, ctx.string_position);
394-
// }
395-
// }
396-
397-
// let flags = SreInfo::from_bits_truncate(ctx.peek_code(state, 2));
398-
399-
// if flags.contains(SreInfo::PREFIX) {
400-
// /* pattern starts with a known prefix */
401-
// /* <length> <skip> <prefix data> <overlap data> */
402-
// let len = ctx.peek_code(state, 5) as usize;
403-
// let skip = ctx.peek_code(state, 6) as usize;
404-
// let prefix = &ctx.pattern(state)[7..];
405-
// let overlap = &prefix[len - 1..];
406-
407-
// ctx.skip_code_from(state, 1);
408-
409-
// if len == 1 {
410-
// // pattern starts with a literal character
411-
// let c = prefix[0];
412-
// let end = state.end;
413-
414-
// while (!ctx.at_end(state)) {
415-
// // find the next matched literal
416-
// while (ctx.peek_char(state) != c) {
417-
// ctx.skip_char(state, 1);
418-
// if (ctx.at_end(state)) {
419-
// return ctx.failure();
420-
// }
421-
// }
422-
423-
// // literal only
424-
// if flags.contains(SreInfo::LITERAL) {
425-
// return ctx.success();
426-
// }
427-
// }
428-
// }
429-
// }
430-
// }
431-
432439
/* assert subpattern */
433440
/* <ASSERT> <skip> <back> <pattern> */
434441
fn op_assert<S: StrDrive>(req: &Request<S>, state: &mut State<S>, ctx: &mut MatchContext<S>) {

‎tests/tests.rs‎

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ fn test_2427() {
2121
// START GENERATED by generate_tests.py
2222
#[rustfmt::skip] let lookbehind = Pattern { code: &[15, 4, 0, 1, 1, 5, 5, 1, 17, 46, 1, 17, 120, 6, 10, 1] };
2323
// END GENERATED
24-
let (mut req, mut state) = lookbehind.state("x");
25-
state.pymatch(&mut req);
24+
let (req, mut state) = lookbehind.state("x");
25+
state.pymatch(req);
2626
assert!(state.has_matched);
2727
}
2828

@@ -32,8 +32,8 @@ fn test_assert() {
3232
// START GENERATED by generate_tests.py
3333
#[rustfmt::skip] let positive_lookbehind = Pattern { code: &[15, 4, 0, 3, 3, 4, 9, 3, 17, 97, 17, 98, 17, 99, 1, 17, 100, 17, 101, 17, 102, 1] };
3434
// END GENERATED
35-
let (mut req, mut state) = positive_lookbehind.state("abcdef");
36-
state.search(&mut req);
35+
let (req, mut state) = positive_lookbehind.state("abcdef");
36+
state.search(req);
3737
assert!(state.has_matched);
3838
}
3939

@@ -43,8 +43,8 @@ fn test_string_boundaries() {
4343
// START GENERATED by generate_tests.py
4444
#[rustfmt::skip] let big_b = Pattern { code: &[15, 4, 0, 0, 0, 6, 11, 1] };
4545
// END GENERATED
46-
let (mut req, mut state) = big_b.state("");
47-
state.search(&mut req);
46+
let (req, mut state) = big_b.state("");
47+
state.search(req);
4848
assert!(!state.has_matched);
4949
}
5050

@@ -56,8 +56,8 @@ fn test_zerowidth() {
5656
// END GENERATED
5757
let (mut req, mut state) = p.state("a:");
5858
req.must_advance = true;
59-
state.search(&mut req);
60-
assert!(state.string_position == 1);
59+
state.search(req);
60+
assert_eq!(state.string_position, 1);
6161
}
6262

6363
#[test]
@@ -67,9 +67,9 @@ fn test_repeat_context_panic() {
6767
// START GENERATED by generate_tests.py
6868
#[rustfmt::skip] let p = Pattern { code: &[15, 4, 0, 0, 4294967295, 24, 25, 0, 4294967295, 27, 6, 0, 4294967295, 17, 97, 1, 24, 11, 0, 1, 18, 0, 17, 120, 17, 120, 18, 1, 20, 17, 122, 19, 1] };
6969
// END GENERATED
70-
let (mut req, mut state) = p.state("axxzaz");
71-
state.pymatch(&mut req);
72-
assert!(*state.marks == vec![Optioned::some(1), Optioned::some(3)]);
70+
let (req, mut state) = p.state("axxzaz");
71+
state.pymatch(req);
72+
assert_eq!(*state.marks, vec![Optioned::some(1), Optioned::some(3)]);
7373
}
7474

7575
#[test]
@@ -78,7 +78,30 @@ fn test_double_max_until() {
7878
// START GENERATED by generate_tests.py
7979
#[rustfmt::skip] let p = Pattern { code: &[15, 4, 0, 0, 4294967295, 24, 18, 0, 4294967295, 18, 0, 24, 9, 0, 1, 18, 2, 17, 49, 18, 3, 19, 18, 1, 19, 1] };
8080
// END GENERATED
81-
let (mut req, mut state) = p.state("1111");
82-
state.pymatch(&mut req);
83-
assert!(state.string_position == 4);
81+
let (req, mut state) = p.state("1111");
82+
state.pymatch(req);
83+
assert_eq!(state.string_position, 4);
84+
}
85+
86+
#[test]
87+
fn test_info_single() {
88+
// pattern p = re.compile(r'aa*')
89+
// START GENERATED by generate_tests.py
90+
#[rustfmt::skip] let p = Pattern { code: &[15, 8, 1, 1, 4294967295, 1, 1, 97, 0, 17, 97, 25, 6, 0, 4294967295, 17, 97, 1, 1] };
91+
// END GENERATED
92+
let (req, mut state) = p.state("baaaa");
93+
state.search(req);
94+
assert_eq!(state.start, 1);
95+
assert_eq!(state.string_position, 5);
96+
}
97+
98+
#[test]
99+
fn test_info_single2() {
100+
// pattern p = re.compile(r'Python|Perl')
101+
// START GENERATED by generate_tests.py
102+
#[rustfmt::skip] let p = Pattern { code: &[15, 8, 1, 4, 6, 1, 1, 80, 0, 17, 80, 7, 13, 17, 121, 17, 116, 17, 104, 17, 111, 17, 110, 16, 11, 9, 17, 101, 17, 114, 17, 108, 16, 2, 0, 1] };
103+
// END GENERATED
104+
let (req, mut state) = p.state("Perl");
105+
state.search(req);
106+
assert!(state.has_matched);
84107
}

0 commit comments

Comments
 (0)