@@ -159,58 +159,63 @@ def _expand(pattern, match, template):
159159 template = sre_parse .parse_template (template , pattern )
160160 return sre_parse .expand_template (template , match )
161161
162- def _sub (pattern , template , string , count = 0 ):
162+ def _sub (pattern , template , text , count = 0 ):
163163 # internal: pattern.sub implementation hook
164- return _subn (pattern , template , string , count )[0 ]
164+ return _subn (pattern , template , text , count , 1 )[0 ]
165165
166- def _subn (pattern , template , string , count = 0 ):
166+ def _subn (pattern , template , text , count = 0 , sub = 0 ):
167167 # internal: pattern.subn implementation hook
168168 if callable (template ):
169169 filter = template
170170 else :
171171 template = _compile_repl (template , pattern )
172+ literals = template [1 ]
173+ if (sub and not count and pattern ._isliteral () and
174+ len (literals ) == 1 and literals [0 ]):
175+ # shortcut: both pattern and string are literals
176+ return string .replace (text , pattern .pattern , literals [0 ]), 0
172177 def filter (match , template = template ):
173178 return sre_parse .expand_template (template , match )
174179 n = i = 0
175180 s = []
176181 append = s .append
177- c = pattern .scanner (string )
182+ c = pattern .scanner (text )
178183 while not count or n < count :
179184 m = c .search ()
180185 if not m :
181186 break
182187 b , e = m .span ()
183188 if i < b :
184- append (string [i :b ])
189+ append (text [i :b ])
185190 append (filter (m ))
186191 i = e
187192 n = n + 1
188- append (string [i :])
189- return _join (s , string [:0 ]), n
193+ append (text [i :])
194+ return _join (s , text [:0 ]), n
190195
191- def _split (pattern , string , maxsplit = 0 ):
196+ def _split (pattern , text , maxsplit = 0 ):
192197 # internal: pattern.split implementation hook
193198 n = i = 0
194199 s = []
195200 append = s .append
196201 extend = s .extend
197- c = pattern .scanner (string )
202+ c = pattern .scanner (text )
198203 g = pattern .groups
199204 while not maxsplit or n < maxsplit :
200205 m = c .search ()
201206 if not m :
202207 break
203208 b , e = m .span ()
204209 if b == e :
205- if i >= len (string ):
210+ if i >= len (text ):
206211 break
207212 continue
208- append (string [i :b ])
213+ append (text [i :b ])
209214 if g and b != e :
210215 extend (list (m .groups ()))
211216 i = e
212217 n = n + 1
213- append (string [i :])
218+ append (text [i :])
214219 return s
215220
216221# register myself for pickling
0 commit comments