Skip to content

Commit

Permalink
cleanups and portability fixes in optimised string methods
Browse files Browse the repository at this point in the history
  • Loading branch information
scoder committed Aug 23, 2012
1 parent 550a70e commit f5bfa12
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions Cython/Utility/Optimize.c
Expand Up @@ -119,11 +119,18 @@ static CYTHON_INLINE int __Pyx_Py_UNICODE_ISTITLE(Py_UCS4 uchar)
static int __Pyx_PyUnicode_Tailmatch(PyObject* s, PyObject* substr,
Py_ssize_t start, Py_ssize_t end, int direction) {
if (unlikely(PyTuple_Check(substr))) {
int result;
Py_ssize_t i;
for (i = 0; i < PyTuple_GET_SIZE(substr); i++) {
Py_ssize_t i, count = PyTuple_GET_SIZE(substr);
for (i = 0; i < count; i++) {
int result;
#if CYTHON_COMPILING_IN_CPYTHON
result = PyUnicode_Tailmatch(s, PyTuple_GET_ITEM(substr, i),
start, end, direction);
#else
PyObject* sub = PySequence_GetItem(substr, i);
if (unlikely(!sub)) return -1;
result = PyUnicode_Tailmatch(s, sub, start, end, direction);
Py_DECREF(sub);
#endif
if (result) {
return result;
}
Expand Down Expand Up @@ -206,11 +213,18 @@ static int __Pyx_PyBytes_Tailmatch(PyObject* self, PyObject* substr, Py_ssize_t
Py_ssize_t end, int direction)
{
if (unlikely(PyTuple_Check(substr))) {
int result;
Py_ssize_t i;
for (i = 0; i < PyTuple_GET_SIZE(substr); i++) {
Py_ssize_t i, count = PyTuple_GET_SIZE(substr);
for (i = 0; i < count; i++) {
int result;
#if CYTHON_COMPILING_IN_CPYTHON
result = __Pyx_PyBytes_SingleTailmatch(self, PyTuple_GET_ITEM(substr, i),
start, end, direction);
#else
PyObject* sub = PySequence_GetItem(substr, i);
if (unlikely(!sub)) return -1;
result = __Pyx_PyBytes_SingleTailmatch(s, sub, start, end, direction);
Py_DECREF(sub);
#endif
if (result) {
return result;
}
Expand All @@ -226,8 +240,8 @@ static int __Pyx_PyBytes_Tailmatch(PyObject* self, PyObject* substr, Py_ssize_t

static CYTHON_INLINE char __Pyx_PyBytes_GetItemInt(PyObject* bytes, Py_ssize_t index, int check_bounds) {
if (check_bounds) {
if (unlikely(index >= PyBytes_GET_SIZE(bytes)) |
((index < 0) & unlikely(index < -PyBytes_GET_SIZE(bytes)))) {
Py_ssize_t size = PyBytes_GET_SIZE(bytes);
if (unlikely(index >= size) | ((index < 0) & unlikely(index < -size))) {
PyErr_Format(PyExc_IndexError, "string index out of range");
return -1;
}
Expand Down

0 comments on commit f5bfa12

Please sign in to comment.