@@ -22,7 +22,7 @@ mod _sre {
2222 use num_traits:: ToPrimitive ;
2323 use sre_engine:: {
2424 constants:: SreFlag ,
25- engine:: { lower_ascii, lower_unicode, upper_unicode, State , StrDrive } ,
25+ engine:: { lower_ascii, lower_unicode, upper_unicode, Request , SearchIter , State , StrDrive } ,
2626 } ;
2727
2828 #[ pyattr]
@@ -53,8 +53,8 @@ mod _sre {
5353 trait SreStr : StrDrive {
5454 fn slice ( & self , start : usize , end : usize , vm : & VirtualMachine ) -> PyObjectRef ;
5555
56- fn create_state ( self , pattern : & Pattern , start : usize , end : usize ) -> State < Self > {
57- State :: new ( self , start, end, pattern . flags , & pattern. code )
56+ fn create_request ( self , pattern : & Pattern , start : usize , end : usize ) -> Request < Self > {
57+ Request :: new ( self , start, end, & pattern. code , false )
5858 }
5959 }
6060
@@ -181,8 +181,9 @@ mod _sre {
181181 endpos,
182182 } = string_args;
183183 with_sre_str ! ( zelf, & string. clone( ) , vm, |x| {
184- let mut state = x. create_state( & zelf, pos, endpos) ;
185- state. pymatch( ) ;
184+ let req = x. create_request( & zelf, pos, endpos) ;
185+ let mut state = State :: default ( ) ;
186+ state. pymatch( req) ;
186187 Ok ( state
187188 . has_matched
188189 . then( || Match :: new( & state, zelf. clone( ) , string) . into_ref( vm) ) )
@@ -196,9 +197,10 @@ mod _sre {
196197 vm : & VirtualMachine ,
197198 ) -> PyResult < Option < PyRef < Match > > > {
198199 with_sre_str ! ( zelf, & string_args. string. clone( ) , vm, |x| {
199- let mut state = x. create_state( & zelf, string_args. pos, string_args. endpos) ;
200- state. match_all = true ;
201- state. pymatch( ) ;
200+ let mut req = x. create_request( & zelf, string_args. pos, string_args. endpos) ;
201+ req. match_all = true ;
202+ let mut state = State :: default ( ) ;
203+ state. pymatch( req) ;
202204 Ok ( state
203205 . has_matched
204206 . then( || Match :: new( & state, zelf. clone( ) , string_args. string) . into_ref( vm) ) )
@@ -212,8 +214,9 @@ mod _sre {
212214 vm : & VirtualMachine ,
213215 ) -> PyResult < Option < PyRef < Match > > > {
214216 with_sre_str ! ( zelf, & string_args. string. clone( ) , vm, |x| {
215- let mut state = x. create_state( & zelf, string_args. pos, string_args. endpos) ;
216- state. search( ) ;
217+ let req = x. create_request( & zelf, string_args. pos, string_args. endpos) ;
218+ let mut state = State :: default ( ) ;
219+ state. search( req) ;
217220 Ok ( state
218221 . has_matched
219222 . then( || Match :: new( & state, zelf. clone( ) , string_args. string) . into_ref( vm) ) )
@@ -226,31 +229,26 @@ mod _sre {
226229 string_args : StringArgs ,
227230 vm : & VirtualMachine ,
228231 ) -> PyResult < Vec < PyObjectRef > > {
229- with_sre_str ! ( zelf, & string_args. string, vm, |x| {
230- let mut state = x. create_state( & zelf, string_args. pos, string_args. endpos) ;
232+ with_sre_str ! ( zelf, & string_args. string, vm, |s| {
233+ let req = s. create_request( & zelf, string_args. pos, string_args. endpos) ;
234+ let state = State :: default ( ) ;
231235 let mut matchlist: Vec <PyObjectRef > = Vec :: new( ) ;
232- while state. start <= state. end {
233- state. search( ) ;
234- if !state. has_matched {
235- break ;
236- }
236+ let mut iter = SearchIter { req, state } ;
237237
238- let m = Match :: new( & state, zelf. clone( ) , string_args. string. clone( ) ) ;
238+ while iter. next( ) . is_some( ) {
239+ let m = Match :: new( & iter. state, zelf. clone( ) , string_args. string. clone( ) ) ;
239240
240241 let item = if zelf. groups == 0 || zelf. groups == 1 {
241- m. get_slice( zelf. groups, state . string , vm)
242+ m. get_slice( zelf. groups, s , vm)
242243 . unwrap_or_else( || vm. ctx. none( ) )
243244 } else {
244245 m. groups( OptionalArg :: Present ( vm. ctx. new_str( ascii!( "" ) ) . into( ) ) , vm) ?
245246 . into( )
246247 } ;
247248
248249 matchlist. push( item) ;
249-
250- state. must_advance = state. string_position == state. start;
251- state. start = state. string_position;
252- state. reset( ) ;
253250 }
251+
254252 Ok ( matchlist)
255253 } )
256254 }
@@ -306,40 +304,32 @@ mod _sre {
306304 split_args : SplitArgs ,
307305 vm : & VirtualMachine ,
308306 ) -> PyResult < Vec < PyObjectRef > > {
309- with_sre_str ! ( zelf, & split_args. string, vm, |x| {
310- let mut state = x. create_state( & zelf, 0 , usize :: MAX ) ;
307+ with_sre_str ! ( zelf, & split_args. string, vm, |s| {
308+ let req = s. create_request( & zelf, 0 , usize :: MAX ) ;
309+ let state = State :: default ( ) ;
311310 let mut splitlist: Vec <PyObjectRef > = Vec :: new( ) ;
312-
311+ let mut iter = SearchIter { req , state } ;
313312 let mut n = 0 ;
314313 let mut last = 0 ;
315- while split_args. maxsplit == 0 || n < split_args. maxsplit {
316- state. search( ) ;
317- if !state. has_matched {
318- break ;
319- }
320314
315+ while ( split_args. maxsplit == 0 || n < split_args. maxsplit) && iter. next( ) . is_some( )
316+ {
321317 /* get segment before this match */
322- splitlist. push( state . string . slice( last, state. start, vm) ) ;
318+ splitlist. push( s . slice( last, iter . state. start, vm) ) ;
323319
324- let m = Match :: new( & state, zelf. clone( ) , split_args. string. clone( ) ) ;
320+ let m = Match :: new( & iter . state, zelf. clone( ) , split_args. string. clone( ) ) ;
325321
326322 // add groups (if any)
327323 for i in 1 ..=zelf. groups {
328- splitlist. push(
329- m. get_slice( i, state. string, vm)
330- . unwrap_or_else( || vm. ctx. none( ) ) ,
331- ) ;
324+ splitlist. push( m. get_slice( i, s, vm) . unwrap_or_else( || vm. ctx. none( ) ) ) ;
332325 }
333326
334327 n += 1 ;
335- state. must_advance = state. string_position == state. start;
336- last = state. string_position;
337- state. start = state. string_position;
338- state. reset( ) ;
328+ last = iter. state. string_position;
339329 }
340330
341331 // get segment following last match (even if empty)
342- splitlist. push( state . string. slice( last, state . string . count( ) , vm) ) ;
332+ splitlist. push( req . string. slice( last, s . count( ) , vm) ) ;
343333
344334 Ok ( splitlist)
345335 } )
@@ -438,39 +428,33 @@ mod _sre {
438428 } ;
439429
440430 with_sre_str ! ( zelf, & string, vm, |s| {
441- let mut state = s. create_state( & zelf, 0 , usize :: MAX ) ;
431+ let req = s. create_request( & zelf, 0 , usize :: MAX ) ;
432+ let state = State :: default ( ) ;
442433 let mut sublist: Vec <PyObjectRef > = Vec :: new( ) ;
434+ let mut iter = SearchIter { req, state } ;
443435 let mut n = 0 ;
444436 let mut last_pos = 0 ;
445- while count == 0 || n < count {
446- state. search( ) ;
447- if !state. has_matched {
448- break ;
449- }
450437
451- if last_pos < state. start {
438+ while ( count == 0 || n < count) && iter. next( ) . is_some( ) {
439+ if last_pos < iter. state. start {
452440 /* get segment before this match */
453- sublist. push( state . string . slice( last_pos, state. start, vm) ) ;
441+ sublist. push( s . slice( last_pos, iter . state. start, vm) ) ;
454442 }
455443
456444 if is_callable {
457- let m = Match :: new( & state, zelf. clone( ) , string. clone( ) ) ;
445+ let m = Match :: new( & iter . state, zelf. clone( ) , string. clone( ) ) ;
458446 let ret = vm. invoke( & filter, ( m. into_ref( vm) , ) ) ?;
459447 sublist. push( ret) ;
460448 } else {
461449 sublist. push( filter. clone( ) ) ;
462450 }
463451
464- last_pos = state. string_position;
452+ last_pos = iter . state. string_position;
465453 n += 1 ;
466-
467- state. must_advance = state. string_position == state. start;
468- state. start = state. string_position;
469- state. reset( ) ;
470454 }
471455
472456 /* get segment following last match */
473- sublist. push( state . string . slice( last_pos, state . end, vm) ) ;
457+ sublist. push( s . slice( last_pos, iter . req . end, vm) ) ;
474458
475459 let list = PyList :: from( sublist) . into_pyobject( vm) ;
476460
@@ -550,10 +534,10 @@ mod _sre {
550534 for group in 0 ..pattern. groups {
551535 let mark_index = 2 * group;
552536 if mark_index + 1 < state. marks . len ( ) {
553- if let ( Some ( start) , Some ( end ) ) =
554- ( state. marks [ mark_index] , state . marks [ mark_index + 1 ] )
555- {
556- regs. push ( ( start as isize , end as isize ) ) ;
537+ let start = state . marks [ mark_index ] ;
538+ let end = state. marks [ mark_index + 1 ] ;
539+ if start . is_some ( ) && end . is_some ( ) {
540+ regs. push ( ( start. unpack ( ) as isize , end. unpack ( ) as isize ) ) ;
557541 continue ;
558542 }
559543 }
@@ -563,8 +547,8 @@ mod _sre {
563547 string,
564548 pattern,
565549 pos : state. start ,
566- endpos : state. end ,
567- lastindex : state. lastindex ,
550+ endpos : state. string_position ,
551+ lastindex : state. marks . last_index ( ) ,
568552 regs,
569553 }
570554 }
@@ -802,12 +786,13 @@ mod _sre {
802786 #[ pymethod( name = "match" ) ]
803787 fn pymatch ( & self , vm : & VirtualMachine ) -> PyResult < Option < PyRef < Match > > > {
804788 with_sre_str ! ( self . pattern, & self . string. clone( ) , vm, |s| {
805- let mut state = s. create_state( & self . pattern, self . start. load( ) , self . end) ;
806- state. must_advance = self . must_advance. load( ) ;
807- state. pymatch( ) ;
789+ let mut req = s. create_request( & self . pattern, self . start. load( ) , self . end) ;
790+ let mut state = State :: default ( ) ;
791+ req. must_advance = self . must_advance. load( ) ;
792+ state. pymatch( req) ;
808793
809794 self . must_advance
810- . store( state. start == state. string_position ) ;
795+ . store( state. string_position == state. start ) ;
811796 self . start. store( state. string_position) ;
812797
813798 Ok ( state. has_matched. then( || {
@@ -822,10 +807,11 @@ mod _sre {
822807 return Ok ( None ) ;
823808 }
824809 with_sre_str ! ( self . pattern, & self . string. clone( ) , vm, |s| {
825- let mut state = s. create_state( & self . pattern, self . start. load( ) , self . end) ;
826- state. must_advance = self . must_advance. load( ) ;
810+ let mut req = s. create_request( & self . pattern, self . start. load( ) , self . end) ;
811+ let mut state = State :: default ( ) ;
812+ req. must_advance = self . must_advance. load( ) ;
827813
828- state. search( ) ;
814+ state. search( req ) ;
829815
830816 self . must_advance
831817 . store( state. string_position == state. start) ;
0 commit comments