Skip to content

Commit 2fe1292

Browse files
committed
improve ctx in _match lazy create stack vec
1 parent 17e1152 commit 2fe1292

2 files changed

Lines changed: 26 additions & 9 deletions

File tree

‎src/constants.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use bitflags::bitflags;
1515

1616
pub const SRE_MAGIC: usize = 20221023;
17-
#[derive(num_enum::TryFromPrimitive, Debug)]
17+
#[derive(num_enum::TryFromPrimitive, Debug, PartialEq, Eq)]
1818
#[repr(u32)]
1919
#[allow(non_camel_case_types, clippy::upper_case_acronyms)]
2020
pub enum SreOpcode {
@@ -62,7 +62,7 @@ pub enum SreOpcode {
6262
NOT_LITERAL_UNI_IGNORE = 41,
6363
RANGE_UNI_IGNORE = 42,
6464
}
65-
#[derive(num_enum::TryFromPrimitive, Debug)]
65+
#[derive(num_enum::TryFromPrimitive, Debug, PartialEq, Eq)]
6666
#[repr(u32)]
6767
#[allow(non_camel_case_types, clippy::upper_case_acronyms)]
6868
pub enum SreAtCode {

‎src/engine.rs‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,15 @@ impl State {
207207
return true;
208208
}
209209

210+
if ctx.try_peek_code_as::<SreOpcode, _>(&req, 1).unwrap() == SreOpcode::AT
211+
&& (ctx.try_peek_code_as::<SreAtCode, _>(&req, 2).unwrap() == SreAtCode::BEGINNING
212+
|| ctx.try_peek_code_as::<SreAtCode, _>(&req, 2).unwrap()
213+
== SreAtCode::BEGINNING_STRING)
214+
{
215+
self.reset(req.end);
216+
return false;
217+
}
218+
210219
req.must_advance = false;
211220
ctx.toplevel = false;
212221
while req.start < end {
@@ -272,15 +281,11 @@ enum Jump {
272281
PossessiveRepeat4,
273282
}
274283

275-
fn _match<S: StrDrive>(req: &Request<S>, state: &mut State, ctx: MatchContext) -> bool {
276-
let mut context_stack = vec![ctx];
284+
fn _match<S: StrDrive>(req: &Request<S>, state: &mut State, mut ctx: MatchContext) -> bool {
285+
let mut context_stack = vec![];
277286
let mut popped_result = false;
278287

279288
'coro: loop {
280-
let Some(mut ctx) = context_stack.pop() else {
281-
break;
282-
};
283-
284289
popped_result = 'result: loop {
285290
let yielded = 'context: loop {
286291
match ctx.jump {
@@ -864,9 +869,14 @@ fn _match<S: StrDrive>(req: &Request<S>, state: &mut State, ctx: MatchContext) -
864869
}
865870
};
866871
context_stack.push(ctx);
867-
context_stack.push(yielded);
872+
ctx = yielded;
868873
continue 'coro;
869874
};
875+
if let Some(popped_ctx) = context_stack.pop() {
876+
ctx = popped_ctx;
877+
} else {
878+
break;
879+
}
870880
}
871881
popped_result
872882
}
@@ -1148,6 +1158,13 @@ impl MatchContext {
11481158
req.pattern_codes[self.code_position + peek]
11491159
}
11501160

1161+
fn try_peek_code_as<T, S>(&self, req: &Request<S>, peek: usize) -> Result<T, T::Error>
1162+
where
1163+
T: TryFrom<u32>,
1164+
{
1165+
self.peek_code(req, peek).try_into()
1166+
}
1167+
11511168
fn skip_code(&mut self, skip: usize) {
11521169
self.code_position += skip;
11531170
}

0 commit comments

Comments
 (0)