[Python-checkins] CVS: python/dist/src/Modules _sre.c,2.70,2.71

Fredrik Lundh effbot@users.sourceforge.net
Sun, 21 Oct 2001 14:48:32 -0700


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

Modified Files:
	_sre.c 
Log Message:


fixed character set description in docstring (SRE uses Python
strings, not C strings)

removed USE_PYTHON defines, and related sre.py helpers

skip calling the subx helper if the template is callable.
interestingly enough, this means that

	def callback(m):
	    return literal
	result = pattern.sub(callback, string)

is much faster than

	result = pattern.sub(literal, string)


Index: _sre.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/_sre.c,v
retrieving revision 2.70
retrieving revision 2.71
diff -C2 -d -r2.70 -r2.71
*** _sre.c	2001/10/21 18:04:11	2.70
--- _sre.c	2001/10/21 21:48:30	2.71
***************
*** 77,84 ****
  /* optional features */
  
- /* test: define to use sre.py helpers instead of C code */
- #undef USE_PYTHON_SPLIT
- #undef USE_PYTHON_SUB
- 
  /* prevent run-away recursion (bad patterns on long strings) */
  
--- 77,80 ----
***************
*** 1252,1255 ****
--- 1248,1253 ----
              state->start = ptr;
              state->ptr = ++ptr;
+             if (flags & SRE_INFO_LITERAL)
+                 return 1; /* we got all of it */
              status = SRE_MATCH(state, pattern + 2, 1);
              if (status != 0)
***************
*** 1821,1885 ****
  }
  
- 
- #ifdef USE_PYTHON_SUB
  static PyObject*
- pattern_sub(PatternObject* self, PyObject* args, PyObject* kw)
- {
-     PyObject* template;
-     PyObject* string;
-     PyObject* count = Py_False; /* zero */
-     static char* kwlist[] = { "repl", "string", "count", NULL };
-     if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|O:sub", kwlist,
-                                      &template, &string, &count))
-         return NULL;
- 
-     /* delegate to Python code */
-     return call(
-         SRE_MODULE, "_sub",
-         Py_BuildValue("OOOO", self, template, string, count)
-         );
- }
- #endif
- 
- #ifdef USE_PYTHON_SUB
- static PyObject*
- pattern_subn(PatternObject* self, PyObject* args, PyObject* kw)
- {
-     PyObject* template;
-     PyObject* string;
-     PyObject* count = Py_False; /* zero */
-     static char* kwlist[] = { "repl", "string", "count", NULL };
-     if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|O:subn", kwlist,
-                                      &template, &string, &count))
-         return NULL;
- 
-     /* delegate to Python code */
-     return call(
-         SRE_MODULE, "_subn",
-         Py_BuildValue("OOOO", self, template, string, count)
-         );
- }
- #endif
- 
- #if defined(USE_PYTHON_SPLIT)
- static PyObject*
- pattern_split(PatternObject* self, PyObject* args, PyObject* kw)
- {
-     PyObject* string;
-     PyObject* maxsplit = Py_False; /* zero */
-     static char* kwlist[] = { "source", "maxsplit", NULL };
-     if (!PyArg_ParseTupleAndKeywords(args, kw, "O|O:split", kwlist,
-                                      &string, &maxsplit))
-         return NULL;
- 
-     /* delegate to Python code */
-     return call(
-         SRE_MODULE, "_split",
-         Py_BuildValue("OOO", self, string, maxsplit)
-         );
- }
- #endif
- 
- static PyObject*
  pattern_findall(PatternObject* self, PyObject* args, PyObject* kw)
  {
--- 1819,1823 ----
***************
*** 1981,1985 ****
  }
  
- #if !defined(USE_PYTHON_SPLIT)
  static PyObject*
  pattern_split(PatternObject* self, PyObject* args, PyObject* kw)
--- 1919,1922 ----
***************
*** 2072,2084 ****
  
      /* get segment following last match */
!     item = PySequence_GetSlice(
!         string, STATE_OFFSET(&state, last), state.endpos
!         );
!     if (!item)
!         goto error;
!     status = PyList_Append(list, item);
!     Py_DECREF(item);
!     if (status < 0)
!         goto error;
  
      state_fini(&state);
--- 2009,2022 ----
  
      /* get segment following last match */
!     i = STATE_OFFSET(&state, last);
!     if (i < state.endpos) {
!         item = PySequence_GetSlice(string, i, state.endpos);
!         if (!item)
!             goto error;
!         status = PyList_Append(list, item);
!         Py_DECREF(item);
!         if (status < 0)
!             goto error;
!     }
  
      state_fini(&state);
***************
*** 2091,2097 ****
      
  }
- #endif
  
- #if !defined(USE_PYTHON_SUB)
  static PyObject*
  pattern_subx(PatternObject* self, PyObject* template, PyObject* string,
--- 2029,2033 ----
***************
*** 2109,2121 ****
      int filter_is_callable;
  
!     /* call subx helper to get the filter */
!     filter = call(
!         SRE_MODULE, "_subx",
!         Py_BuildValue("OO", self, template)
!         );
!     if (!filter)
!         return NULL;
! 
!     filter_is_callable = PyCallable_Check(filter);
  
      string = state_init(&state, self, string, 0, INT_MAX);
--- 2045,2064 ----
      int filter_is_callable;
  
!     if (PyCallable_Check(template)) {
!         /* sub/subn takes either a function or a template */
!         filter = template;
!         Py_INCREF(filter);
!         filter_is_callable = 1;
!     } else {
!         /* if not callable, call the template compiler.  it may return
!            either a filter function or a literal string */
!         filter = call(
!             SRE_MODULE, "_subx",
!             Py_BuildValue("OO", self, template)
!             );
!         if (!filter)
!             return NULL;
!         filter_is_callable = PyCallable_Check(filter);
!     }
  
      string = state_init(&state, self, string, 0, INT_MAX);
***************
*** 2170,2174 ****
  
          if (filter_is_callable) {
!             /* filter match */
              match = pattern_new_match(self, &state, 1);
              if (!match)
--- 2113,2117 ----
  
          if (filter_is_callable) {
!             /* pass match object through filter */
              match = pattern_new_match(self, &state, 1);
              if (!match)
***************
*** 2187,2191 ****
              /* filter is literal string */
              item = filter;
!             Py_INCREF(filter);
          }
  
--- 2130,2134 ----
              /* filter is literal string */
              item = filter;
!             Py_INCREF(item);
          }
  
***************
*** 2209,2224 ****
  
      /* get segment following last match */
!     item = PySequence_GetSlice(string, i, state.endpos);
!     if (!item)
!         goto error;
!     status = PyList_Append(list, item);
!     Py_DECREF(item);
!     if (status < 0)
!         goto error;
  
      state_fini(&state);
  
!     /* convert list to single string */
      item = join(list, self->pattern);
      if (!item)
          return NULL;
--- 2152,2170 ----
  
      /* get segment following last match */
!     if (i < state.endpos) {
!         item = PySequence_GetSlice(string, i, state.endpos);
!         if (!item)
!             goto error;
!         status = PyList_Append(list, item);
!         Py_DECREF(item);
!         if (status < 0)
!             goto error;
!     }
  
      state_fini(&state);
  
!     /* convert list to single string (also removes list) */
      item = join(list, self->pattern);
+ 
      if (!item)
          return NULL;
***************
*** 2263,2267 ****
      return pattern_subx(self, template, string, count, 1);
  }
- #endif
  
  static PyObject*
--- 2209,2212 ----