Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2d96f11

Browse files
author
Fredrik Lundh
committed
map re.sub() to string.replace(), when possible
1 parent 44835d8 commit 2d96f11

2 files changed

Lines changed: 40 additions & 12 deletions

File tree

Lib/sre.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Modules/_sre.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,6 +1955,28 @@ pattern_deepcopy(PatternObject* self, PyObject* args)
19551955
#endif
19561956
}
19571957

1958+
static PyObject*
1959+
pattern_isliteral(PatternObject* self, PyObject* args)
1960+
{
1961+
/* internal: return true if pattern consists of literal text only */
1962+
1963+
SRE_CODE* code;
1964+
PyObject* isliteral;
1965+
1966+
if (!PyArg_ParseTuple(args, ":_isliteral"))
1967+
return NULL;
1968+
1969+
code = PatternObject_GetCode(self);
1970+
1971+
if (code[0] == SRE_OP_INFO && code[2] & SRE_INFO_LITERAL)
1972+
isliteral = Py_True;
1973+
else
1974+
isliteral = Py_False;
1975+
1976+
Py_INCREF(isliteral);
1977+
return isliteral;
1978+
}
1979+
19581980
static PyMethodDef pattern_methods[] = {
19591981
{"match", (PyCFunction) pattern_match, METH_VARARGS|METH_KEYWORDS},
19601982
{"search", (PyCFunction) pattern_search, METH_VARARGS|METH_KEYWORDS},
@@ -1965,6 +1987,7 @@ static PyMethodDef pattern_methods[] = {
19651987
{"scanner", (PyCFunction) pattern_scanner, METH_VARARGS},
19661988
{"__copy__", (PyCFunction) pattern_copy, METH_VARARGS},
19671989
{"__deepcopy__", (PyCFunction) pattern_deepcopy, METH_VARARGS},
1990+
{"_isliteral", (PyCFunction) pattern_isliteral, METH_VARARGS},
19681991
{NULL, NULL}
19691992
};
19701993

0 commit comments

Comments
 (0)