[Python-checkins] CVS: python/dist/src/Lib sre.py,1.39,1.40

Fredrik Lundh effbot@users.sourceforge.net
Sun, 21 Oct 2001 09:47:59 -0700


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

Modified Files:
	sre.py 
Log Message:


rewrote the pattern.sub and pattern.subn methods in C

removed (conceptually flawed) getliteral helper; the new sub/subn code
uses a faster code path for literal replacement strings, but doesn't
(yet) look for literal patterns.

added STATE_OFFSET macro, and use it to convert state.start/ptr to
char indexes


Index: sre.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/sre.py,v
retrieving revision 1.39
retrieving revision 1.40
diff -C2 -d -r1.39 -r1.40
*** sre.py	2001/10/18 19:30:15	1.39
--- sre.py	2001/10/21 16:47:57	1.40
***************
*** 105,109 ****
      "UNICODE", "error" ]
  
! __version__ = "2.2.0"
  
  # this module works under 1.5.2 and later.  don't use string methods
--- 105,109 ----
      "UNICODE", "error" ]
  
! __version__ = "2.2.1"
  
  # this module works under 1.5.2 and later.  don't use string methods
***************
*** 244,268 ****
      template = sre_parse.parse_template(template, pattern)
      return sre_parse.expand_template(template, match)
- 
- def _sub(pattern, template, text, count=0):
-     # internal: pattern.sub implementation hook
-     return _subn(pattern, template, text, count, 1)[0]
  
! def _subn(pattern, template, text, count=0, sub=0):
!     # internal: pattern.subn implementation hook
      if callable(template):
          filter = template
      else:
          template = _compile_repl(template, pattern)
!         literals = template[1]
!         if sub and not count:
!             literal = pattern._getliteral()
!             if literal and "\\" in literal:
!                 literal = None # may contain untranslated escapes
!             if literal is not None and len(literals) == 1 and literals[0]:
!                 # shortcut: both pattern and string are literals
!                 return string.replace(text, pattern.pattern, literals[0]), 0
!         def filter(match, template=template):
!             return sre_parse.expand_template(template, match)
      n = i = 0
      s = []
--- 244,275 ----
      template = sre_parse.parse_template(template, pattern)
      return sre_parse.expand_template(template, match)
  
! def _subx(pattern, template):
!     # internal: pattern.sub/subn implementation helper
      if callable(template):
          filter = template
      else:
          template = _compile_repl(template, pattern)
!         if not template[0] and len(template[1]) == 1:
!             # literal replacement
!             filter = template[1][0]
!         else:
!             def filter(match, template=template):
!                 return sre_parse.expand_template(template, match)
!     return filter
! 
! def _sub(pattern, template, text, count=0):
!     # internal: pattern.sub implementation hook
!     # FIXME: not used in SRE 2.2.1 and later; will be removed soon
!     return _subn(pattern, template, text, count)[0]
! 
! def _subn(pattern, template, text, count=0):
!     # internal: pattern.subn implementation hook
!     # FIXME: not used in SRE 2.2.1 and later; will be removed soon
!     filter = _subx(pattern, template)
!     if not callable(filter):
!         # literal replacement
!         def filter(match, literal=filter):
!             return literal
      n = i = 0
      s = []
***************
*** 287,290 ****
--- 294,298 ----
  def _split(pattern, text, maxsplit=0):
      # internal: pattern.split implementation hook
+     # FIXME: not used in SRE 2.2.1 and later; will be removed soon
      n = i = 0
      s = []