[Python-checkins] python/dist/src/Modules _sre.c,2.87,2.88 sre_constants.h,2.13,2.14

gvanrossum@users.sourceforge.net gvanrossum@users.sourceforge.net
Mon, 14 Apr 2003 10:59:38 -0700


Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1:/tmp/cvs-serv12812/Modules

Modified Files:
	_sre.c sre_constants.h 
Log Message:
SF patch #720991 by Gary Herron:
A small fix for bug #545855 and Greg Chapman's
addition of op code SRE_OP_MIN_REPEAT_ONE for
eliminating recursion on simple uses of pattern '*?' on a
long string.


Index: _sre.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/_sre.c,v
retrieving revision 2.87
retrieving revision 2.88
diff -C2 -d -r2.87 -r2.88
*** _sre.c	22 Nov 2002 12:46:35 -0000	2.87
--- _sre.c	14 Apr 2003 17:59:34 -0000	2.88
***************
*** 994,997 ****
--- 994,1057 ----
              return 0;
  
+         case SRE_OP_MIN_REPEAT_ONE:
+             /* match repeated sequence (minimizing regexp) */
+ 
+             /* this operator only works if the repeated item is
+                exactly one character wide, and we're not already
+                collecting backtracking points.  for other cases,
+                use the MIN_REPEAT operator */
+ 
+             /* <MIN_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */
+ 
+             TRACE(("|%p|%p|MIN_REPEAT_ONE %d %d\n", pattern, ptr,
+                    pattern[1], pattern[2]));
+ 
+             if (ptr + pattern[1] > end)
+                 return 0; /* cannot match */
+ 
+             state->ptr = ptr;
+ 
+             if (pattern[1] == 0)
+                 count = 0;
+             else {
+                 /* count using pattern min as the maximum */
+                 count = SRE_COUNT(state, pattern + 3, pattern[1], level + 1);
+ 
+                 if (count < 0)
+                     return count;   /* exception */
+                 if (count < (int) pattern[1])
+                     return 0;       /* did not match minimum number of times */ 
+                 ptr += count;       /* advance past minimum matches of repeat */
+             }
+ 
+             if (pattern[pattern[0]] == SRE_OP_SUCCESS) {
+                 /* tail is empty.  we're finished */
+                 state->ptr = ptr;
+                 return 1;
+ 
+             } else {
+                 /* general case */
+                 int matchmax = ((int)pattern[2] == 65535);
+                 int c;
+                 lastmark = state->lastmark;
+                 while (matchmax || count <= (int) pattern[2]) {
+                     state->ptr = ptr;
+                     i = SRE_MATCH(state, pattern + pattern[0], level + 1);
+                     if (i)
+                         return i;
+                     state->ptr = ptr;
+                     c = SRE_COUNT(state, pattern+3, 1, level+1);
+                     if (c < 0)
+                         return c;
+                     if (c == 0)
+                         break;
+                     assert(c == 1);
+                     ptr++;
+                     count++;
+                 }
+                 lastmark_restore(state, lastmark);
+             }
+             return 0;
+ 
          case SRE_OP_REPEAT:
              /* create repeat context.  all the hard work is done

Index: sre_constants.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/sre_constants.h,v
retrieving revision 2.13
retrieving revision 2.14
diff -C2 -d -r2.13 -r2.14
*** sre_constants.h	2 Jul 2001 16:58:38 -0000	2.13
--- sre_constants.h	14 Apr 2003 17:59:34 -0000	2.14
***************
*** 43,46 ****
--- 43,47 ----
  #define SRE_OP_REPEAT_ONE 28
  #define SRE_OP_SUBPATTERN 29
+ #define SRE_OP_MIN_REPEAT_ONE 30
  #define SRE_AT_BEGINNING 0
  #define SRE_AT_BEGINNING_LINE 1