Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
2d96f11d
Commit
2d96f11d
authored
Jul 08, 2001
by
Fredrik Lundh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
map re.sub() to string.replace(), when possible
parent
44835d8e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
12 deletions
+40
-12
Lib/sre.py
Lib/sre.py
+17
-12
Modules/_sre.c
Modules/_sre.c
+23
-0
No files found.
Lib/sre.py
View file @
2d96f11d
...
...
@@ -159,42 +159,47 @@ def _expand(pattern, match, template):
template
=
sre_parse
.
parse_template
(
template
,
pattern
)
return
sre_parse
.
expand_template
(
template
,
match
)
def
_sub
(
pattern
,
template
,
string
,
count
=
0
):
def
_sub
(
pattern
,
template
,
text
,
count
=
0
):
# internal: pattern.sub implementation hook
return
_subn
(
pattern
,
template
,
string
,
count
)[
0
]
return
_subn
(
pattern
,
template
,
text
,
count
,
1
)[
0
]
def
_subn
(
pattern
,
template
,
string
,
count
=
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
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
)
n
=
i
=
0
s
=
[]
append
=
s
.
append
c
=
pattern
.
scanner
(
string
)
c
=
pattern
.
scanner
(
text
)
while
not
count
or
n
<
count
:
m
=
c
.
search
()
if
not
m
:
break
b
,
e
=
m
.
span
()
if
i
<
b
:
append
(
string
[
i
:
b
])
append
(
text
[
i
:
b
])
append
(
filter
(
m
))
i
=
e
n
=
n
+
1
append
(
string
[
i
:])
return
_join
(
s
,
string
[:
0
]),
n
append
(
text
[
i
:])
return
_join
(
s
,
text
[:
0
]),
n
def
_split
(
pattern
,
string
,
maxsplit
=
0
):
def
_split
(
pattern
,
text
,
maxsplit
=
0
):
# internal: pattern.split implementation hook
n
=
i
=
0
s
=
[]
append
=
s
.
append
extend
=
s
.
extend
c
=
pattern
.
scanner
(
string
)
c
=
pattern
.
scanner
(
text
)
g
=
pattern
.
groups
while
not
maxsplit
or
n
<
maxsplit
:
m
=
c
.
search
()
...
...
@@ -202,15 +207,15 @@ def _split(pattern, string, maxsplit=0):
break
b
,
e
=
m
.
span
()
if
b
==
e
:
if
i
>=
len
(
string
):
if
i
>=
len
(
text
):
break
continue
append
(
string
[
i
:
b
])
append
(
text
[
i
:
b
])
if
g
and
b
!=
e
:
extend
(
list
(
m
.
groups
()))
i
=
e
n
=
n
+
1
append
(
string
[
i
:])
append
(
text
[
i
:])
return
s
# register myself for pickling
...
...
Modules/_sre.c
View file @
2d96f11d
...
...
@@ -1955,6 +1955,28 @@ pattern_deepcopy(PatternObject* self, PyObject* args)
#endif
}
static
PyObject
*
pattern_isliteral
(
PatternObject
*
self
,
PyObject
*
args
)
{
/* internal: return true if pattern consists of literal text only */
SRE_CODE
*
code
;
PyObject
*
isliteral
;
if
(
!
PyArg_ParseTuple
(
args
,
":_isliteral"
))
return
NULL
;
code
=
PatternObject_GetCode
(
self
);
if
(
code
[
0
]
==
SRE_OP_INFO
&&
code
[
2
]
&
SRE_INFO_LITERAL
)
isliteral
=
Py_True
;
else
isliteral
=
Py_False
;
Py_INCREF
(
isliteral
);
return
isliteral
;
}
static
PyMethodDef
pattern_methods
[]
=
{
{
"match"
,
(
PyCFunction
)
pattern_match
,
METH_VARARGS
|
METH_KEYWORDS
},
{
"search"
,
(
PyCFunction
)
pattern_search
,
METH_VARARGS
|
METH_KEYWORDS
},
...
...
@@ -1965,6 +1987,7 @@ static PyMethodDef pattern_methods[] = {
{
"scanner"
,
(
PyCFunction
)
pattern_scanner
,
METH_VARARGS
},
{
"__copy__"
,
(
PyCFunction
)
pattern_copy
,
METH_VARARGS
},
{
"__deepcopy__"
,
(
PyCFunction
)
pattern_deepcopy
,
METH_VARARGS
},
{
"_isliteral"
,
(
PyCFunction
)
pattern_isliteral
,
METH_VARARGS
},
{
NULL
,
NULL
}
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment