Skip to content
This repository was archived by the owner on Mar 22, 2024. It is now read-only.

Commit 10e51ba

Browse files
committed
improve: use adjust_cursor reduce double calc
1 parent c93ea30 commit 10e51ba

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

src/engine.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::string::{
77

88
use super::{SreAtCode, SreCatCode, SreInfo, SreOpcode, StrDrive, StringCursor, MAXREPEAT};
99
use optional::Optioned;
10-
use std::convert::TryFrom;
10+
use std::{convert::TryFrom, ptr::null};
1111

1212
#[derive(Debug, Clone, Copy)]
1313
pub struct Request<'a, S> {
@@ -126,17 +126,12 @@ impl State {
126126
self.marks.clear();
127127
self.repeat_stack.clear();
128128
self.start = start;
129-
if self.cursor.ptr.is_null() || self.cursor.position > self.start {
130-
self.cursor = req.string.create_cursor(self.start);
131-
} else if self.cursor.position < self.start {
132-
let skip = self.start - self.cursor.position;
133-
S::skip(&mut self.cursor, skip);
134-
}
129+
req.string.adjust_cursor(&mut self.cursor, start);
135130
}
136131

137132
pub fn pymatch<S: StrDrive>(&mut self, req: &Request<S>) -> bool {
138133
self.start = req.start;
139-
self.cursor = req.string.create_cursor(self.start);
134+
req.string.adjust_cursor(&mut self.cursor, self.start);
140135

141136
let ctx = MatchContext {
142137
cursor: self.cursor,
@@ -151,7 +146,7 @@ impl State {
151146

152147
pub fn search<S: StrDrive>(&mut self, mut req: Request<S>) -> bool {
153148
self.start = req.start;
154-
self.cursor = req.string.create_cursor(self.start);
149+
req.string.adjust_cursor(&mut self.cursor, self.start);
155150

156151
if req.start > req.end {
157152
return false;
@@ -215,7 +210,9 @@ impl State {
215210
|| ctx.try_peek_code_as::<SreAtCode, _>(&req, 1).unwrap()
216211
== SreAtCode::BEGINNING_STRING)
217212
{
218-
self.reset(&req, req.end);
213+
self.cursor.position = req.end;
214+
self.cursor.ptr = null();
215+
// self.reset(&req, req.end);
219216
return false;
220217
}
221218

src/string.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ impl Default for StringCursor {
1616
pub trait StrDrive: Copy {
1717
fn count(&self) -> usize;
1818
fn create_cursor(&self, n: usize) -> StringCursor;
19+
fn adjust_cursor(&self, cursor: &mut StringCursor, n: usize);
1920
fn advance(cursor: &mut StringCursor) -> u32;
2021
fn peek(cursor: &StringCursor) -> u32;
2122
fn skip(cursor: &mut StringCursor, n: usize);
@@ -38,6 +39,12 @@ impl<'a> StrDrive for &'a [u8] {
3839
}
3940
}
4041

42+
#[inline]
43+
fn adjust_cursor(&self, cursor: &mut StringCursor, n: usize) {
44+
cursor.position = n;
45+
cursor.ptr = self[n..].as_ptr();
46+
}
47+
4148
#[inline]
4249
fn advance(cursor: &mut StringCursor) -> u32 {
4350
cursor.position += 1;
@@ -83,11 +90,21 @@ impl StrDrive for &str {
8390

8491
#[inline]
8592
fn create_cursor(&self, n: usize) -> StringCursor {
86-
let mut ptr = self.as_ptr();
87-
for _ in 0..n {
88-
unsafe { next_code_point(&mut ptr) };
93+
let mut cursor = StringCursor {
94+
ptr: self.as_ptr(),
95+
position: 0,
96+
};
97+
Self::skip(&mut cursor, n);
98+
cursor
99+
}
100+
101+
#[inline]
102+
fn adjust_cursor(&self, cursor: &mut StringCursor, n: usize) {
103+
if cursor.ptr.is_null() || cursor.position > n {
104+
*cursor = Self::create_cursor(&self, n);
105+
} else if cursor.position < n {
106+
Self::skip(cursor, n - cursor.position);
89107
}
90-
StringCursor { ptr, position: n }
91108
}
92109

93110
#[inline]

0 commit comments

Comments
 (0)