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
a00905e5
Commit
a00905e5
authored
Nov 26, 2009
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
support len(char*) efficiently by calling strlen() instead
parent
054e4d13
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
94 additions
and
0 deletions
+94
-0
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+29
-0
tests/run/charptr_len.pyx
tests/run/charptr_len.pyx
+65
-0
No files found.
Cython/Compiler/Optimize.py
View file @
a00905e5
...
@@ -1045,6 +1045,28 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
...
@@ -1045,6 +1045,28 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
)
)
return
node
return
node
Pyx_strlen_func_type
=
PyrexTypes
.
CFuncType
(
PyrexTypes
.
c_size_t_type
,
[
PyrexTypes
.
CFuncTypeArg
(
"bytes"
,
PyrexTypes
.
c_char_ptr_type
,
None
)
])
def
_handle_simple_function_len
(
self
,
node
,
pos_args
):
if
len
(
pos_args
)
!=
1
:
self
.
_error_wrong_arg_count
(
'len'
,
node
,
pos_args
,
1
)
return
node
arg
=
pos_args
[
0
]
if
isinstance
(
arg
,
ExprNodes
.
CoerceToPyTypeNode
):
arg
=
arg
.
arg
if
not
arg
.
type
.
is_string
:
return
node
node
=
ExprNodes
.
PythonCapiCallNode
(
node
.
pos
,
"strlen"
,
self
.
Pyx_strlen_func_type
,
args
=
[
arg
],
is_temp
=
node
.
is_temp
,
utility_code
=
include_string_h_utility_code
,
)
return
node
### special methods
### special methods
Pyx_tp_new_func_type
=
PyrexTypes
.
CFuncType
(
Pyx_tp_new_func_type
=
PyrexTypes
.
CFuncType
(
...
@@ -1483,6 +1505,13 @@ static INLINE PyObject* __Pyx_Type(PyObject* o) {
...
@@ -1483,6 +1505,13 @@ static INLINE PyObject* __Pyx_Type(PyObject* o) {
)
)
include_string_h_utility_code
=
UtilityCode
(
proto
=
"""
#include <string.h>
"""
)
tpnew_utility_code
=
UtilityCode
(
tpnew_utility_code
=
UtilityCode
(
proto
=
"""
proto
=
"""
static INLINE PyObject* __Pyx_tp_new(PyObject* type_obj) {
static INLINE PyObject* __Pyx_tp_new(PyObject* type_obj) {
...
...
tests/run/charptr_len.pyx
0 → 100644
View file @
a00905e5
__doc__
=
"""
>>> lentest_char()
7
>>> lentest_char_c()
7
>>> lentest_uchar()
7
>>> lentest_uchar_c()
7
>>> lentest_py()
7
>>> lentest_py_c()
7
"""
cimport
cython
cdef
char
*
s
=
b"abcdefg"
cdef
unsigned
char
*
us
=
b"abcdefg"
cdef
bytes
pystr
=
b"abcdefg"
@
cython
.
test_assert_path_exists
(
"//PythonCapiCallNode"
,
)
def
lentest_char
():
return
len
(
s
)
@
cython
.
test_assert_path_exists
(
"//PythonCapiCallNode"
,
)
def
lentest_char_c
():
cdef
Py_ssize_t
l
=
len
(
s
)
return
l
@
cython
.
test_assert_path_exists
(
"//PythonCapiCallNode"
,
)
def
lentest_uchar
():
return
len
(
us
)
@
cython
.
test_assert_path_exists
(
"//PythonCapiCallNode"
,
)
def
lentest_uchar_c
():
cdef
Py_ssize_t
l
=
len
(
us
)
return
l
@
cython
.
test_assert_path_exists
(
"//SimpleCallNode"
,
)
def
lentest_py
():
return
len
(
pystr
)
@
cython
.
test_assert_path_exists
(
"//SimpleCallNode"
,
)
def
lentest_py_c
():
cdef
Py_ssize_t
l
=
len
(
pystr
)
return
l
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