Skip to content

Commit 174c932

Browse files
authored
Merge pull request #1 from qingshi163/master
add tests; fix OpAssert panic
2 parents 2592067 + 2473c3e commit 174c932

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

src/engine.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -369,20 +369,18 @@ fn once<F: FnOnce(&mut StackDrive)>(f: F) -> Box<OpOnce<F>> {
369369
Box::new(OpOnce { f: Some(f) })
370370
}
371371

372-
// F1 F2 are same identical, but workaround for closure
373372
struct OpTwice<F1, F2> {
374373
f1: Option<F1>,
375374
f2: Option<F2>,
376375
}
377376
impl<F1, F2> OpcodeExecutor for OpTwice<F1, F2>
378377
where
379-
F1: FnOnce(&mut StackDrive),
378+
F1: FnOnce(&mut StackDrive) -> Option<()>,
380379
F2: FnOnce(&mut StackDrive),
381380
{
382381
fn next(&mut self, drive: &mut StackDrive) -> Option<()> {
383382
if let Some(f1) = self.f1.take() {
384-
f1(drive);
385-
Some(())
383+
f1(drive)
386384
} else if let Some(f2) = self.f2.take() {
387385
f2(drive);
388386
None
@@ -393,7 +391,7 @@ where
393391
}
394392
fn twice<F1, F2>(f1: F1, f2: F2) -> Box<OpTwice<F1, F2>>
395393
where
396-
F1: FnOnce(&mut StackDrive),
394+
F1: FnOnce(&mut StackDrive) -> Option<()>,
397395
F2: FnOnce(&mut StackDrive),
398396
{
399397
Box::new(OpTwice {
@@ -483,11 +481,12 @@ impl OpcodeDispatcher {
483481
let passed = drive.ctx().string_position - drive.state.start;
484482
if passed < back {
485483
drive.ctx_mut().has_matched = Some(false);
486-
return;
484+
return None;
487485
}
488486
drive.state.string_position = drive.ctx().string_position - back;
489487
drive.push_new_context(3);
490488
drive.state.context_stack.last_mut().unwrap().toplevel = false;
489+
Some(())
491490
},
492491
|drive| {
493492
let child_ctx = drive.state.popped_context.unwrap();
@@ -504,11 +503,12 @@ impl OpcodeDispatcher {
504503
let passed = drive.ctx().string_position - drive.state.start;
505504
if passed < back {
506505
drive.skip_code(drive.peek_code(1) as usize + 1);
507-
return;
506+
return None;
508507
}
509508
drive.state.string_position = drive.ctx().string_position - back;
510509
drive.push_new_context(3);
511510
drive.state.context_stack.last_mut().unwrap().toplevel = false;
511+
Some(())
512512
},
513513
|drive| {
514514
let child_ctx = drive.state.popped_context.unwrap();
@@ -598,6 +598,7 @@ impl OpcodeDispatcher {
598598
drive.state.string_position = drive.ctx().string_position;
599599
// execute UNTIL operator
600600
drive.push_new_context(drive.peek_code(1) as usize + 1);
601+
Some(())
601602
},
602603
|drive| {
603604
drive.state.repeat_stack.pop();

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
pub mod constants;
22
pub mod engine;
3+
#[cfg(test)]
4+
mod tests;
35

46
pub const CODESIZE: usize = 4;
57

src/tests.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use engine::{State, StrDrive};
2+
3+
use super::*;
4+
5+
#[test]
6+
fn test_2427() {
7+
let str_drive = StrDrive::Str("x");
8+
// r'(?<!\.)x\b'
9+
let code: Vec<u32> = vec![15, 4, 0, 1, 1, 5, 5, 1, 17, 46, 1, 17, 120, 6, 10, 1];
10+
let mut state = State::new(
11+
str_drive,
12+
0,
13+
std::usize::MAX,
14+
constants::SreFlag::UNICODE,
15+
&code,
16+
);
17+
state = state.pymatch();
18+
assert!(state.has_matched == Some(true));
19+
}

0 commit comments

Comments
 (0)