@@ -222,7 +222,9 @@ PyPcre_compile(self, args)
222222 {
223223 PyMem_DEL (rv );
224224 if (!PyErr_Occurred ())
225- PyErr_SetObject (ErrorObject , Py_BuildValue ("si" , error , erroroffset ));
225+ {
226+ PyErr_SetObject (ErrorObject , Py_BuildValue ("si" , error , erroroffset ));
227+ }
226228 return NULL ;
227229 }
228230 rv -> regex_extra = pcre_study (rv -> regex , 0 , & error );
@@ -243,47 +245,57 @@ PyPcre_compile(self, args)
243245}
244246
245247static PyObject *
246- PyPcre_expand_escape (self , args )
247- PyObject * self ;
248- PyObject * args ;
248+ PyPcre_expand_escape (pattern , pattern_len , indexptr , typeptr )
249+ unsigned char * pattern ;
250+ int pattern_len , * indexptr , * typeptr ;
249251{
250- unsigned char c , * pattern ;
251- int index , pattern_len ;
252- const int context = REPLACEMENT ;
253-
254- if (!PyArg_ParseTuple (args , "s#i" , & pattern , & pattern_len , & index ))
255- return NULL ;
252+ unsigned char c ;
253+ int index = * indexptr ;
254+
256255 if (pattern_len <=index )
257256 {
258257 PyErr_SetString (ErrorObject , "escape ends too soon" );
259258 return NULL ;
260259 }
261260 c = pattern [index ]; index ++ ;
261+ * typeptr = CHAR ;
262+
262263 switch (c )
263264 {
264265 case ('t' ):
265- return Py_BuildValue ("ici" , CHAR , (char )9 , index );
266+ * indexptr = index ;
267+ return Py_BuildValue ("c" , (char )9 );
266268 break ;
267269 case ('n' ):
268- return Py_BuildValue ("ici" , CHAR , (char )10 , index );
270+ * indexptr = index ;
271+ return Py_BuildValue ("c" , (char )10 );
269272 break ;
270273 case ('v' ):
271- return Py_BuildValue ("ici" , CHAR , (char )11 , index );
274+ * indexptr = index ;
275+ return Py_BuildValue ("c" , (char )11 );
272276 break ;
273277 case ('r' ):
274- return Py_BuildValue ("ici" , CHAR , (char )13 , index );
278+ * indexptr = index ;
279+ return Py_BuildValue ("c" , (char )13 );
275280 break ;
276281 case ('f' ):
277- return Py_BuildValue ("ici" , CHAR , (char )12 , index );
282+ * indexptr = index ;
283+ return Py_BuildValue ("c" , (char )12 );
278284 break ;
279285 case ('a' ):
280- return Py_BuildValue ("ici" , CHAR , (char )7 , index );
286+ * indexptr = index ;
287+ return Py_BuildValue ("c" , (char )7 );
281288 break ;
289+ case ('b' ):
290+ * indexptr = index ;
291+ return Py_BuildValue ("c" , (char )8 );
292+ break ;
293+
282294 case ('x' ):
283295 {
284296 int end , length ;
285297 unsigned char * string ;
286- PyObject * v , * result ;
298+ PyObject * v ;
287299
288300 end = index ;
289301 while (end < pattern_len &&
@@ -312,52 +324,11 @@ PyPcre_expand_escape(self, args)
312324 free (string );
313325 /* The evaluation raised an exception */
314326 if (v == NULL ) return NULL ;
315- result = Py_BuildValue ("iOi" , CHAR , v , end );
316- Py_DECREF (v );
317- return result ;
327+ * indexptr = end ;
328+ return v ;
318329 }
319330 break ;
320331
321- case ('b' ):
322- if (context != NORMAL )
323- return Py_BuildValue ("ici" , CHAR , (char )8 , index );
324- else
325- {
326- unsigned char empty_string [1 ];
327- empty_string [0 ]= '\0' ;
328- return Py_BuildValue ("isi" , WORD_BOUNDARY , empty_string , index );
329- }
330- break ;
331- case ('B' ):
332- if (context != NORMAL )
333- return Py_BuildValue ("ici" , CHAR , 'B' , index );
334- else
335- {
336- unsigned char empty_string [1 ];
337- empty_string [0 ]= '\0' ;
338- return Py_BuildValue ("isi" , NOT_WORD_BOUNDARY , empty_string , index );
339- }
340- break ;
341- case ('A' ):
342- if (context != NORMAL )
343- return Py_BuildValue ("ici" , CHAR , 'A' , index );
344- else
345- {
346- unsigned char empty_string [1 ];
347- empty_string [0 ]= '\0' ;
348- return Py_BuildValue ("isi" , BEGINNING_OF_BUFFER , empty_string , index );
349- }
350- break ;
351- case ('Z' ):
352- if (context != NORMAL )
353- return Py_BuildValue ("ici" , CHAR , 'Z' , index );
354- else
355- {
356- unsigned char empty_string [1 ];
357- empty_string [0 ]= '\0' ;
358- return Py_BuildValue ("isi" , END_OF_BUFFER , empty_string , index );
359- }
360- break ;
361332 case ('E' ): case ('G' ): case ('L' ): case ('Q' ):
362333 case ('U' ): case ('l' ): case ('u' ):
363334 {
@@ -367,26 +338,6 @@ PyPcre_expand_escape(self, args)
367338 return NULL ;
368339 }
369340
370- case ('w' ):
371- return Py_BuildValue ("ici" , CHAR , 'w' , index );
372- break ;
373- case ('W' ):
374- return Py_BuildValue ("ici" , CHAR , 'W' , index );
375- break ;
376- case ('s' ):
377- return Py_BuildValue ("ici" , CHAR , 's' , index );
378- break ;
379- case ('S' ):
380- return Py_BuildValue ("ici" , CHAR , 'S' , index );
381- break ;
382-
383- case ('d' ):
384- return Py_BuildValue ("ici" , CHAR , 'd' , index );
385- break ;
386- case ('D' ):
387- return Py_BuildValue ("ici" , CHAR , 'D' , index );
388- break ;
389-
390341 case ('g' ):
391342 {
392343 int end , valid , i ;
@@ -427,9 +378,9 @@ PyPcre_expand_escape(self, args)
427378 return NULL ;
428379 }
429380
430- return Py_BuildValue ( "is#i" , MEMORY_REFERENCE ,
431- pattern + index , end - index ,
432- end + 1 );
381+ * typeptr = MEMORY_REFERENCE ;
382+ * indexptr = end + 1 ;
383+ return Py_BuildValue ( "s#" , pattern + index , end - index );
433384 }
434385 break ;
435386
@@ -451,7 +402,8 @@ PyPcre_expand_escape(self, args)
451402 PyErr_SetString (ErrorObject , "octal value out of range" );
452403 return NULL ;
453404 }
454- return Py_BuildValue ("ici" , CHAR , (unsigned char )octval , i );
405+ * indexptr = i ;
406+ return Py_BuildValue ("c" , (unsigned char )octval );
455407 }
456408 break ;
457409 case ('1' ): case ('2' ): case ('3' ): case ('4' ):
@@ -483,45 +435,37 @@ PyPcre_expand_escape(self, args)
483435 PyErr_SetString (ErrorObject , "octal value out of range" );
484436 return NULL ;
485437 }
486- return Py_BuildValue ("ici" , CHAR , (unsigned char )value , index + 3 );
438+ * indexptr = index + 3 ;
439+ return Py_BuildValue ("c" , (unsigned char )value );
487440 }
488441 else
489442 {
490443 /* 2-digit form, so it's a memory reference */
491- if (context == CHARCLASS )
492- {
493- PyErr_SetString (ErrorObject , "cannot reference a register "
494- "from inside a character class" );
495- return NULL ;
496- }
497444 value = 10 * (pattern [index ]- '0' ) +
498445 (pattern [index + 1 ]- '0' );
499446 if (value < 1 || EXTRACT_MAX <=value )
500447 {
501448 PyErr_SetString (ErrorObject , "memory reference out of range" );
502449 return NULL ;
503450 }
504- return Py_BuildValue ("iii" , MEMORY_REFERENCE ,
505- value , index + 2 );
451+ * typeptr = MEMORY_REFERENCE ;
452+ * indexptr = index + 2 ;
453+ return Py_BuildValue ("i" , value );
506454 }
507455 }
508456 else
509457 {
510458 /* Single-digit form, like \2, so it's a memory reference */
511- if (context == CHARCLASS )
512- {
513- PyErr_SetString (ErrorObject , "cannot reference a register "
514- "from inside a character class" );
515- return NULL ;
516- }
517- return Py_BuildValue ("iii" , MEMORY_REFERENCE ,
518- pattern [index ]- '0' , index + 1 );
459+ * typeptr = MEMORY_REFERENCE ;
460+ * indexptr = index + 1 ;
461+ return Py_BuildValue ("i" , pattern [index ]- '0' );
519462 }
520463 }
521464 break ;
522465
523466 default :
524- return Py_BuildValue ("ici" , CHAR , c , index );
467+ * indexptr = index ;
468+ return Py_BuildValue ("c" , c );
525469 break ;
526470 }
527471}
@@ -547,7 +491,7 @@ PyPcre_expand(self, args)
547491 {
548492 if (repl [i ]== '\\' )
549493 {
550- PyObject * args , * t , * value ;
494+ PyObject * value ;
551495 int escape_type ;
552496
553497 if (start != i )
@@ -557,18 +501,14 @@ PyPcre_expand(self, args)
557501 total_len += i - start ;
558502 }
559503 i ++ ;
560- args = Py_BuildValue ("Oi" , repl_obj , i );
561- t = PyPcre_expand_escape (NULL , args );
562- Py_DECREF (args );
563- if (t == NULL )
504+ value = PyPcre_expand_escape (repl , size , & i , & escape_type );
505+ if (value == NULL )
564506 {
565507 /* PyPcre_expand_escape triggered an exception of some sort,
566508 so just return */
567509 Py_DECREF (results );
568510 return NULL ;
569511 }
570- value = PyTuple_GetItem (t , 1 );
571- escape_type = PyInt_AsLong (PyTuple_GetItem (t , 0 ));
572512 switch (escape_type )
573513 {
574514 case (CHAR ):
@@ -599,7 +539,6 @@ PyPcre_expand(self, args)
599539 PyErr_SetString (ErrorObject ,
600540 message );
601541 Py_DECREF (result );
602- Py_DECREF (t );
603542 Py_DECREF (results );
604543 return NULL ;
605544 }
@@ -610,15 +549,13 @@ PyPcre_expand(self, args)
610549 }
611550 break ;
612551 default :
613- Py_DECREF (t );
614552 Py_DECREF (results );
615553 PyErr_SetString (ErrorObject ,
616554 "bad escape in replacement" );
617555 return NULL ;
618556 }
619- i = start = PyInt_AsLong ( PyTuple_GetItem ( t , 2 )) ;
557+ start = i ;
620558 i -- ; /* Decrement now, because the 'for' loop will increment it */
621- Py_DECREF (t );
622559 }
623560 } /* endif repl[i]!='\\' */
624561
690627initpcre ()
691628{
692629 PyObject * m , * d ;
693- int a ;
694630
695631 /* Create the module and add the functions */
696632 m = Py_InitModule ("pcre" , pcre_methods );
0 commit comments