Skip to content

Commit 7e7b973

Browse files
committed
clearup
1 parent 646c8ac commit 7e7b973

1 file changed

Lines changed: 163 additions & 138 deletions

File tree

‎src/engine.rs‎

Lines changed: 163 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -252,147 +252,16 @@ impl<S: StrDrive> State<S> {
252252
let flags = SreInfo::from_bits_truncate(ctx.peek_code(req, 2));
253253

254254
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..7 + len];
260-
let overlap = &ctx.pattern(req)[7 + len - 1..7 + len * 2];
261-
262-
if len == 1 {
263-
// pattern starts with a literal character
264-
let c = prefix[0];
265-
266-
// code_position ready for tail match
267-
ctx.skip_code_from(req, 1);
268-
ctx.skip_code(2 * skip);
269-
270-
req.must_advance = false;
271-
272-
while !ctx.at_end(req) {
273-
// find the next matched literal
274-
while ctx.peek_char(req) != c {
275-
ctx.skip_char(req, 1);
276-
if ctx.at_end(req) {
277-
return;
278-
}
279-
}
280-
281-
req.start = ctx.string_position;
282-
self.start = ctx.string_position;
283-
self.string_position = ctx.string_position + skip;
284-
285-
// literal only
286-
if flags.contains(SreInfo::LITERAL) {
287-
self.has_matched = true;
288-
return;
289-
}
290-
291-
let mut next_ctx = ctx;
292-
next_ctx.skip_char(req, skip);
293-
294-
self.context_stack.push(next_ctx);
295-
self._match(req);
296-
297-
if self.has_matched {
298-
return;
299-
}
300-
301-
ctx.skip_char(req, 1);
302-
self.marks.clear();
303-
}
304-
return;
305-
} else if len > 1 {
306-
// code_position ready for tail match
307-
ctx.skip_code_from(req, 1);
308-
ctx.skip_code(2 * skip);
309-
310-
req.must_advance = false;
311-
312-
while !ctx.at_end(req) {
313-
let c = prefix[0];
314-
while ctx.peek_char(req) != c {
315-
ctx.skip_char(req, 1);
316-
if ctx.at_end(req) {
317-
return;
318-
}
319-
}
320-
ctx.skip_char(req, 1);
321-
if ctx.at_end(req) {
322-
return;
323-
}
324-
325-
let mut i = 1;
326-
loop {
327-
if ctx.peek_char(req) == prefix[i] {
328-
i += 1;
329-
if i != len {
330-
ctx.skip_char(req, 1);
331-
if ctx.at_end(req) {
332-
return;
333-
}
334-
continue;
335-
}
336-
337-
req.start = ctx.string_position - (len - 1);
338-
self.start = req.start;
339-
self.string_position = self.start + skip;
340-
341-
if flags.contains(SreInfo::LITERAL) {
342-
self.has_matched = true;
343-
return;
344-
}
345-
346-
let mut next_ctx = ctx;
347-
// next_ctx.skip_char(req, 1);
348-
next_ctx.string_position = self.string_position;
349-
next_ctx.string_offset = req.string.offset(0, self.string_position);
350-
self.context_stack.push(next_ctx);
351-
self._match(req);
352-
if self.has_matched {
353-
return;
354-
}
355-
356-
ctx.skip_char(req, 1);
357-
if ctx.at_end(req) {
358-
return;
359-
}
360-
self.marks.clear();
361-
}
362-
i = overlap[i] as usize;
363-
if i == 0 {
364-
break;
365-
}
366-
}
367-
}
368-
return;
255+
if flags.contains(SreInfo::LITERAL) {
256+
search_info_literal::<true, S>(req, self, ctx);
257+
} else {
258+
search_info_literal::<false, S>(req, self, ctx);
369259
}
260+
return;
370261
} else if flags.contains(SreInfo::CHARSET) {
371-
let set = &ctx.pattern(req)[5..];
372-
ctx.skip_code_from(req, 1);
373-
req.must_advance = false;
374-
loop {
375-
while !ctx.at_end(req) && !charset(set, ctx.peek_char(req)) {
376-
ctx.skip_char(req, 1);
377-
}
378-
if ctx.at_end(req) {
379-
return;
380-
}
381-
req.start = ctx.string_position;
382-
self.start = ctx.string_position;
383-
self.string_position = ctx.string_position;
384-
385-
self.context_stack.push(ctx);
386-
self._match(req);
387-
388-
if self.has_matched {
389-
return;
390-
}
391-
392-
ctx.skip_char(req, 1);
393-
self.marks.clear();
394-
}
262+
return search_info_charset(req, self, ctx);
395263
}
264+
// fallback to general search
396265
}
397266

398267
self.context_stack.push(ctx);
@@ -528,6 +397,162 @@ fn dispatch<S: StrDrive>(
528397
}
529398
}
530399

400+
fn search_info_literal<const LITERAL: bool, S: StrDrive>(
401+
req: &mut Request<S>,
402+
state: &mut State<S>,
403+
mut ctx: MatchContext<S>,
404+
) {
405+
/* pattern starts with a known prefix */
406+
/* <length> <skip> <prefix data> <overlap data> */
407+
let len = ctx.peek_code(req, 5) as usize;
408+
let skip = ctx.peek_code(req, 6) as usize;
409+
let prefix = &ctx.pattern(req)[7..7 + len];
410+
let overlap = &ctx.pattern(req)[7 + len - 1..7 + len * 2];
411+
412+
// code_position ready for tail match
413+
ctx.skip_code_from(req, 1);
414+
ctx.skip_code(2 * skip);
415+
416+
req.must_advance = false;
417+
418+
if len == 1 {
419+
// pattern starts with a literal character
420+
let c = prefix[0];
421+
422+
while !ctx.at_end(req) {
423+
// find the next matched literal
424+
while ctx.peek_char(req) != c {
425+
ctx.skip_char(req, 1);
426+
if ctx.at_end(req) {
427+
return;
428+
}
429+
}
430+
431+
req.start = ctx.string_position;
432+
state.start = ctx.string_position;
433+
state.string_position = ctx.string_position + skip;
434+
435+
// literal only
436+
if LITERAL {
437+
state.has_matched = true;
438+
return;
439+
}
440+
441+
let mut next_ctx = ctx;
442+
next_ctx.skip_char(req, skip);
443+
444+
state.context_stack.push(next_ctx);
445+
state._match(req);
446+
447+
if state.has_matched {
448+
return;
449+
}
450+
451+
ctx.skip_char(req, 1);
452+
state.marks.clear();
453+
}
454+
} else {
455+
while !ctx.at_end(req) {
456+
let c = prefix[0];
457+
while ctx.peek_char(req) != c {
458+
ctx.skip_char(req, 1);
459+
if ctx.at_end(req) {
460+
return;
461+
}
462+
}
463+
ctx.skip_char(req, 1);
464+
if ctx.at_end(req) {
465+
return;
466+
}
467+
468+
let mut i = 1;
469+
loop {
470+
if ctx.peek_char(req) == prefix[i] {
471+
i += 1;
472+
if i != len {
473+
ctx.skip_char(req, 1);
474+
if ctx.at_end(req) {
475+
return;
476+
}
477+
continue;
478+
}
479+
480+
req.start = ctx.string_position - (len - 1);
481+
state.start = req.start;
482+
state.string_position = state.start + skip;
483+
484+
// literal only
485+
if LITERAL {
486+
state.has_matched = true;
487+
return;
488+
}
489+
490+
let mut next_ctx = ctx;
491+
if skip != 0 {
492+
next_ctx.skip_char(req, 1);
493+
} else {
494+
next_ctx.string_position = state.string_position;
495+
next_ctx.string_offset = req.string.offset(0, state.string_position);
496+
}
497+
498+
state.context_stack.push(next_ctx);
499+
state._match(req);
500+
501+
if state.has_matched {
502+
return;
503+
}
504+
505+
ctx.skip_char(req, 1);
506+
if ctx.at_end(req) {
507+
return;
508+
}
509+
state.marks.clear();
510+
}
511+
512+
i = overlap[i] as usize;
513+
if i == 0 {
514+
break;
515+
}
516+
}
517+
}
518+
}
519+
}
520+
521+
fn search_info_charset<S: StrDrive>(
522+
req: &mut Request<S>,
523+
state: &mut State<S>,
524+
mut ctx: MatchContext<S>,
525+
) {
526+
let set = &ctx.pattern(req)[5..];
527+
528+
ctx.skip_code_from(req, 1);
529+
530+
req.must_advance = false;
531+
532+
loop {
533+
while !ctx.at_end(req) && !charset(set, ctx.peek_char(req)) {
534+
ctx.skip_char(req, 1);
535+
}
536+
if ctx.at_end(req) {
537+
return;
538+
}
539+
540+
req.start = ctx.string_position;
541+
state.start = ctx.string_position;
542+
state.string_position = ctx.string_position;
543+
544+
state.context_stack.push(ctx);
545+
state._match(req);
546+
547+
if state.has_matched {
548+
return;
549+
}
550+
551+
ctx.skip_char(req, 1);
552+
state.marks.clear();
553+
}
554+
}
555+
531556
/* assert subpattern */
532557
/* <ASSERT> <skip> <back> <pattern> */
533558
fn op_assert<S: StrDrive>(req: &Request<S>, state: &mut State<S>, ctx: &mut MatchContext<S>) {

0 commit comments

Comments
 (0)