Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
c733640d
Commit
c733640d
authored
Mar 21, 2010
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
optimise unicode.replace()
parent
c919b6a4
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
0 deletions
+57
-0
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+26
-0
tests/run/unicodemethods.pyx
tests/run/unicodemethods.pyx
+31
-0
No files found.
Cython/Compiler/Optimize.py
View file @
c733640d
...
@@ -1629,6 +1629,32 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
...
@@ -1629,6 +1629,32 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
return
ExprNodes
.
CoerceToPyTypeNode
(
return
ExprNodes
.
CoerceToPyTypeNode
(
method_call
,
self
.
env_stack
[
-
1
],
PyrexTypes
.
py_object_type
)
method_call
,
self
.
env_stack
[
-
1
],
PyrexTypes
.
py_object_type
)
PyUnicode_Replace_func_type
=
PyrexTypes
.
CFuncType
(
Builtin
.
unicode_type
,
[
PyrexTypes
.
CFuncTypeArg
(
"str"
,
Builtin
.
unicode_type
,
None
),
PyrexTypes
.
CFuncTypeArg
(
"substring"
,
PyrexTypes
.
py_object_type
,
None
),
PyrexTypes
.
CFuncTypeArg
(
"replstr"
,
PyrexTypes
.
py_object_type
,
None
),
PyrexTypes
.
CFuncTypeArg
(
"maxcount"
,
PyrexTypes
.
c_py_ssize_t_type
,
None
),
])
def
_handle_simple_method_unicode_replace
(
self
,
node
,
args
,
is_unbound_method
):
"""Replace unicode.replace(...) by a direct call to the
corresponding C-API function.
"""
if
len
(
args
)
not
in
(
3
,
4
):
self
.
_error_wrong_arg_count
(
'unicode.replace'
,
node
,
args
,
"3-4"
)
return
node
if
len
(
args
)
<
4
:
args
.
append
(
ExprNodes
.
IntNode
(
node
.
pos
,
value
=
"-1"
,
type
=
PyrexTypes
.
c_py_ssize_t_type
))
else
:
args
[
3
]
=
args
[
3
].
coerce_to
(
PyrexTypes
.
c_py_ssize_t_type
,
self
.
env_stack
[
-
1
])
return
self
.
_substitute_method_call
(
node
,
"PyUnicode_Replace"
,
self
.
PyUnicode_Replace_func_type
,
'replace'
,
is_unbound_method
,
args
)
PyUnicode_AsEncodedString_func_type
=
PyrexTypes
.
CFuncType
(
PyUnicode_AsEncodedString_func_type
=
PyrexTypes
.
CFuncType
(
Builtin
.
bytes_type
,
[
Builtin
.
bytes_type
,
[
PyrexTypes
.
CFuncTypeArg
(
"obj"
,
Builtin
.
unicode_type
,
None
),
PyrexTypes
.
CFuncTypeArg
(
"obj"
,
Builtin
.
unicode_type
,
None
),
...
...
tests/run/unicodemethods.pyx
View file @
c733640d
...
@@ -434,3 +434,34 @@ def count_start_end(unicode s, substring, start, end):
...
@@ -434,3 +434,34 @@ def count_start_end(unicode s, substring, start, end):
"""
"""
cdef
Py_ssize_t
pos
=
s
.
count
(
substring
,
start
,
end
)
cdef
Py_ssize_t
pos
=
s
.
count
(
substring
,
start
,
end
)
return
pos
return
pos
# unicode.replace(s, sub, repl, [maxcount])
@
cython
.
test_fail_if_path_exists
(
"//CoerceFromPyTypeNode"
,
"//CastNode"
,
"//TypecastNode"
)
@
cython
.
test_assert_path_exists
(
"//PythonCapiCallNode"
)
def
replace
(
unicode
s
,
substring
,
repl
):
"""
>>> print( text.replace('sa', 'SA') )
ab jd sdflk as SA SAdas asdas fsdf
>>> print( replace(text, 'sa', 'SA') )
ab jd sdflk as SA SAdas asdas fsdf
"""
return
s
.
replace
(
substring
,
repl
)
@
cython
.
test_fail_if_path_exists
(
"//CastNode"
,
"//TypecastNode"
)
@
cython
.
test_assert_path_exists
(
"//CoerceFromPyTypeNode"
,
"//PythonCapiCallNode"
)
def
replace_maxcount
(
unicode
s
,
substring
,
repl
,
maxcount
):
"""
>>> print( text.replace('sa', 'SA', 1) )
ab jd sdflk as SA sadas asdas fsdf
>>> print( replace_maxcount(text, 'sa', 'SA', 1) )
ab jd sdflk as SA sadas asdas fsdf
"""
return
s
.
replace
(
substring
,
repl
,
maxcount
)
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