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
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
cython
Commits
57773d75
Commit
57773d75
authored
May 30, 2011
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
clean up PyStr_Tailmatch() implementation
parent
d74d4bd0
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
56 deletions
+28
-56
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+28
-56
No files found.
Cython/Compiler/Optimize.py
View file @
57773d75
...
...
@@ -2413,9 +2413,9 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
node
,
"PyUnicode_Split"
,
self
.
PyUnicode_Split_func_type
,
'split'
,
is_unbound_method
,
args
)
Py
Unicode
_Tailmatch_func_type
=
PyrexTypes
.
CFuncType
(
Py
String
_Tailmatch_func_type
=
PyrexTypes
.
CFuncType
(
PyrexTypes
.
c_bint_type
,
[
PyrexTypes
.
CFuncTypeArg
(
"str"
,
Builtin
.
unicode_type
,
None
),
PyrexTypes
.
CFuncTypeArg
(
"str"
,
PyrexTypes
.
py_object_type
,
None
),
# bytes/str/unicode
PyrexTypes
.
CFuncTypeArg
(
"substring"
,
PyrexTypes
.
py_object_type
,
None
),
PyrexTypes
.
CFuncTypeArg
(
"start"
,
PyrexTypes
.
c_py_ssize_t_type
,
None
),
PyrexTypes
.
CFuncTypeArg
(
"end"
,
PyrexTypes
.
c_py_ssize_t_type
,
None
),
...
...
@@ -2426,15 +2426,15 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
def
_handle_simple_method_unicode_endswith
(
self
,
node
,
args
,
is_unbound_method
):
return
self
.
_inject_tailmatch
(
node
,
args
,
is_unbound_method
,
'unicode'
,
'endswith'
,
self
.
PyUnicode_Tailmatch_func_type
,
unicode_tailmatch_utility_code
,
+
1
)
unicode_tailmatch_utility_code
,
+
1
)
def
_handle_simple_method_unicode_startswith
(
self
,
node
,
args
,
is_unbound_method
):
return
self
.
_inject_tailmatch
(
node
,
args
,
is_unbound_method
,
'unicode'
,
'startswith'
,
self
.
PyUnicode_Tailmatch_func_type
,
unicode_tailmatch_utility_code
,
-
1
)
unicode_tailmatch_utility_code
,
-
1
)
def
_inject_tailmatch
(
self
,
node
,
args
,
is_unbound_method
,
type_name
,
method_name
,
func_type
,
utility_code
,
direction
):
method_name
,
utility_code
,
direction
):
"""Replace unicode.startswith(...) and unicode.endswith(...)
by a direct call to the corresponding C-API function.
"""
...
...
@@ -2449,7 +2449,8 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
node
.
pos
,
value
=
str
(
direction
),
type
=
PyrexTypes
.
c_int_type
))
method_call
=
self
.
_substitute_method_call
(
node
,
"__Pyx_Py%s_Tailmatch"
%
type_name
.
capitalize
(),
func_type
,
node
,
"__Pyx_Py%s_Tailmatch"
%
type_name
.
capitalize
(),
self
.
PyString_Tailmatch_func_type
,
method_name
,
is_unbound_method
,
args
,
utility_code
=
utility_code
)
return
method_call
.
coerce_to
(
Builtin
.
bool_type
,
self
.
current_env
())
...
...
@@ -2779,46 +2780,25 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
return
(
encoding
,
encoding_node
,
error_handling
,
error_handling_node
)
PyStr_Tailmatch_func_type
=
PyrexTypes
.
CFuncType
(
PyrexTypes
.
c_bint_type
,
[
PyrexTypes
.
CFuncTypeArg
(
"str"
,
Builtin
.
str_type
,
None
),
PyrexTypes
.
CFuncTypeArg
(
"substring"
,
PyrexTypes
.
py_object_type
,
None
),
PyrexTypes
.
CFuncTypeArg
(
"start"
,
PyrexTypes
.
c_py_ssize_t_type
,
None
),
PyrexTypes
.
CFuncTypeArg
(
"end"
,
PyrexTypes
.
c_py_ssize_t_type
,
None
),
PyrexTypes
.
CFuncTypeArg
(
"direction"
,
PyrexTypes
.
c_int_type
,
None
),
],
exception_value
=
'-1'
)
def
_handle_simple_method_str_endswith
(
self
,
node
,
args
,
is_unbound_method
):
return
self
.
_inject_tailmatch
(
node
,
args
,
is_unbound_method
,
'str'
,
'endswith'
,
s
elf
.
PyStr_Tailmatch_func_type
,
s
tr_tailmatch_utility_code
,
+
1
)
str_tailmatch_utility_code
,
+
1
)
def
_handle_simple_method_str_startswith
(
self
,
node
,
args
,
is_unbound_method
):
return
self
.
_inject_tailmatch
(
node
,
args
,
is_unbound_method
,
'str'
,
'startswith'
,
self
.
PyStr_Tailmatch_func_type
,
str_tailmatch_utility_code
,
-
1
)
PyBytes_Tailmatch_func_type
=
PyrexTypes
.
CFuncType
(
PyrexTypes
.
c_bint_type
,
[
PyrexTypes
.
CFuncTypeArg
(
"str"
,
Builtin
.
bytes_type
,
None
),
PyrexTypes
.
CFuncTypeArg
(
"substring"
,
PyrexTypes
.
py_object_type
,
None
),
PyrexTypes
.
CFuncTypeArg
(
"start"
,
PyrexTypes
.
c_py_ssize_t_type
,
None
),
PyrexTypes
.
CFuncTypeArg
(
"end"
,
PyrexTypes
.
c_py_ssize_t_type
,
None
),
PyrexTypes
.
CFuncTypeArg
(
"direction"
,
PyrexTypes
.
c_int_type
,
None
),
],
exception_value
=
'-1'
)
str_tailmatch_utility_code
,
-
1
)
def
_handle_simple_method_bytes_endswith
(
self
,
node
,
args
,
is_unbound_method
):
return
self
.
_inject_tailmatch
(
node
,
args
,
is_unbound_method
,
'bytes'
,
'endswith'
,
self
.
PyBytes_Tailmatch_func_type
,
bytes_tailmatch_utility_code
,
+
1
)
bytes_tailmatch_utility_code
,
+
1
)
def
_handle_simple_method_bytes_startswith
(
self
,
node
,
args
,
is_unbound_method
):
return
self
.
_inject_tailmatch
(
node
,
args
,
is_unbound_method
,
'bytes'
,
'startswith'
,
self
.
PyBytes_Tailmatch_func_type
,
bytes_tailmatch_utility_code
,
-
1
)
bytes_tailmatch_utility_code
,
-
1
)
### helpers
...
...
@@ -2929,9 +2909,7 @@ static int __Pyx_PyBytes_SingleTailmatch(PyObject* self, PyObject* arg, Py_ssize
int retval;
#if PY_VERSION_HEX >= 0x02060000
PyBufferProcs *pb = NULL;
Py_buffer view;
view.obj = NULL;
#endif
...
...
@@ -2947,19 +2925,11 @@ static int __Pyx_PyBytes_SingleTailmatch(PyObject* self, PyObject* arg, Py_ssize
#endif
else {
#if PY_VERSION_HEX < 0x02060000
if (
PyObject_AsCharBuffer(arg, &sub_ptr, &sub_len
))
if (
unlikely(PyObject_AsCharBuffer(arg, &sub_ptr, &sub_len)
))
return -1;
#else
pb = Py_TYPE(self)->tp_as_buffer;
if (pb == NULL || pb->bf_getbuffer == NULL) {
PyErr_SetString(PyExc_TypeError,
"expected an object with the buffer interface");
return -1;
}
if ((*pb->bf_getbuffer)(self, &view, PyBUF_SIMPLE)) {
if (unlikely(PyObject_GetBuffer(self, &view, PyBUF_SIMPLE) == -1))
return -1;
}
sub_ptr = (const char*) view.buf;
sub_len = view.len;
#endif
...
...
@@ -2988,9 +2958,8 @@ static int __Pyx_PyBytes_SingleTailmatch(PyObject* self, PyObject* arg, Py_ssize
retval = 0;
#if PY_VERSION_HEX >= 0x02060000
if (pb != NULL && pb->bf_releasebuffer != NULL)
(*pb->bf_releasebuffer)(self, &view);
Py_XDECREF(view.obj);
if (view.obj)
PyBuffer_Release(&view);
#endif
return retval;
...
...
@@ -3019,18 +2988,21 @@ static int __Pyx_PyBytes_Tailmatch(PyObject* self, PyObject* substr, Py_ssize_t
str_tailmatch_utility_code
=
UtilityCode
(
proto
=
'''
static int __Pyx_PyStr_Tailmatch(PyObject* self, PyObject* arg, Py_ssize_t start,
static
CYTHON_INLINE
int __Pyx_PyStr_Tailmatch(PyObject* self, PyObject* arg, Py_ssize_t start,
Py_ssize_t end, int direction);
'''
,
# We do not use a C compiler macro here to avoid "unused function"
# warnings for the *_Tailmatch() function that is not being used in
# the specific CPython version. The C compiler will generate the same
# code anyway, and will usually just remove the unused function.
impl
=
'''
static int __Pyx_PyStr_Tailmatch(PyObject* self, PyObject* arg, Py_ssize_t start,
static
CYTHON_INLINE
int __Pyx_PyStr_Tailmatch(PyObject* self, PyObject* arg, Py_ssize_t start,
Py_ssize_t end, int direction)
{
#if PY_MAJOR_VERSION < 3
if (PY_MAJOR_VERSION < 3)
return __Pyx_PyBytes_Tailmatch(self, arg, start, end, direction);
#
else
else
return __Pyx_PyUnicode_Tailmatch(self, arg, start, end, direction);
#endif
}
'''
,
requires
=
[
unicode_tailmatch_utility_code
,
bytes_tailmatch_utility_code
]
...
...
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