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

Fredrik Lundh effbot@users.sourceforge.net
Sun, 08 Jul 2001 06:26:59 -0700


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

Modified Files:
	sre.py 
Log Message:


map re.sub() to string.replace(), when possible


Index: sre.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/sre.py,v
retrieving revision 1.32
retrieving revision 1.33
diff -C2 -r1.32 -r1.33
*** sre.py	2001/07/06 20:56:10	1.32
--- sre.py	2001/07/08 13:26:57	1.33
***************
*** 160,168 ****
      return sre_parse.expand_template(template, match)
  
! def _sub(pattern, template, string, count=0):
      # internal: pattern.sub implementation hook
!     return _subn(pattern, template, string, count)[0]
  
! def _subn(pattern, template, string, count=0):
      # internal: pattern.subn implementation hook
      if callable(template):
--- 160,168 ----
      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):
***************
*** 170,173 ****
--- 170,178 ----
      else:
          template = _compile_repl(template, pattern)
+         literals = template[1]
+         if (sub and not count and pattern._isliteral() 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)
***************
*** 175,179 ****
      s = []
      append = s.append
!     c = pattern.scanner(string)
      while not count or n < count:
          m = c.search()
--- 180,184 ----
      s = []
      append = s.append
!     c = pattern.scanner(text)
      while not count or n < count:
          m = c.search()
***************
*** 182,193 ****
          b, e = m.span()
          if i < b:
!             append(string[i:b])
          append(filter(m))
          i = e
          n = n + 1
!     append(string[i:])
!     return _join(s, string[:0]), n
  
! def _split(pattern, string, maxsplit=0):
      # internal: pattern.split implementation hook
      n = i = 0
--- 187,198 ----
          b, e = m.span()
          if i < b:
!             append(text[i:b])
          append(filter(m))
          i = e
          n = n + 1
!     append(text[i:])
!     return _join(s, text[:0]), n
  
! def _split(pattern, text, maxsplit=0):
      # internal: pattern.split implementation hook
      n = i = 0
***************
*** 195,199 ****
      append = s.append
      extend = s.extend
!     c = pattern.scanner(string)
      g = pattern.groups
      while not maxsplit or n < maxsplit:
--- 200,204 ----
      append = s.append
      extend = s.extend
!     c = pattern.scanner(text)
      g = pattern.groups
      while not maxsplit or n < maxsplit:
***************
*** 203,215 ****
          b, e = m.span()
          if b == e:
!             if i >= len(string):
                  break
              continue
!         append(string[i:b])
          if g and b != e:
              extend(list(m.groups()))
          i = e
          n = n + 1
!     append(string[i:])
      return s
  
--- 208,220 ----
          b, e = m.span()
          if b == e:
!             if i >= len(text):
                  break
              continue
!         append(text[i:b])
          if g and b != e:
              extend(list(m.groups()))
          i = e
          n = n + 1
!     append(text[i:])
      return s