Index: Objects/stringlib/partition.h =================================================================== --- Objects/stringlib/partition.h (revision 76970) +++ Objects/stringlib/partition.h (working copy) @@ -58,7 +58,7 @@ ) { PyObject* out; - Py_ssize_t pos, j; + Py_ssize_t pos; if (sep_len == 0) { PyErr_SetString(PyExc_ValueError, "empty separator"); @@ -69,20 +69,14 @@ if (!out) return NULL; - /* XXX - create reversefastsearch helper! */ - pos = -1; - for (j = str_len - sep_len; j >= 0; --j) - if (STRINGLIB_CMP(str+j, sep, sep_len) == 0) { - pos = j; - break; - } + pos = fastsearch(str, str_len, sep, sep_len, FAST_RSEARCH); if (pos < 0) { Py_INCREF(STRINGLIB_EMPTY); PyTuple_SET_ITEM(out, 0, (PyObject*) STRINGLIB_EMPTY); Py_INCREF(STRINGLIB_EMPTY); PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY); - Py_INCREF(str_obj); + Py_INCREF(str_obj); PyTuple_SET_ITEM(out, 2, (PyObject*) str_obj); return out; } Index: Objects/stringlib/find.h =================================================================== --- Objects/stringlib/find.h (revision 76970) +++ Objects/stringlib/find.h (working copy) @@ -32,20 +32,19 @@ const STRINGLIB_CHAR* sub, Py_ssize_t sub_len, Py_ssize_t offset) { - /* XXX - create reversefastsearch helper! */ - if (sub_len == 0) { - if (str_len < 0) - return -1; - return str_len + offset; - } else { - Py_ssize_t j, pos = -1; - for (j = str_len - sub_len; j >= 0; --j) - if (STRINGLIB_CMP(str+j, sub, sub_len) == 0) { - pos = j + offset; - break; - } - return pos; - } + Py_ssize_t pos; + + if (str_len < 0) + return -1; + if (sub_len == 0) + return str_len + offset; + + pos = fastsearch(str, str_len, sub, sub_len, FAST_RSEARCH); + + if (pos >= 0) + pos += offset; + + return pos; } Py_LOCAL_INLINE(Py_ssize_t) @@ -64,10 +63,7 @@ if (end < 0) end = 0; - return stringlib_find( - str + start, end - start, - sub, sub_len, start - ); + return stringlib_find(str + start, end - start, sub, sub_len, start); } Py_LOCAL_INLINE(Py_ssize_t) Index: Objects/stringlib/fastsearch.h =================================================================== --- Objects/stringlib/fastsearch.h (revision 76970) +++ Objects/stringlib/fastsearch.h (working copy) @@ -5,7 +5,7 @@ /* fast search/count implementation, based on a mix between boyer- moore and horspool, with a few more bells and whistles on the top. - for some more background, see: http://effbot.org/stringlib.htm */ + for some more background, see: http://effbot.org/zone/stringlib.htm */ /* note: fastsearch may access s[n], which isn't a problem when using Python's ordinary string types, but may cause problems if you're @@ -16,6 +16,7 @@ #define FAST_COUNT 0 #define FAST_SEARCH 1 +#define FAST_RSEARCH 2 Py_LOCAL_INLINE(Py_ssize_t) fastsearch(const STRINGLIB_CHAR* s, Py_ssize_t n, @@ -41,51 +42,92 @@ if (s[i] == p[0]) count++; return count; - } else { + } else if (mode == FAST_SEARCH) { for (i = 0; i < n; i++) if (s[i] == p[0]) return i; + } else { /* FAST_RSEARCH */ + for (i = n - 1; i > -1; i--) + if (s[i] == p[0]) + return i; } return -1; } mlast = m - 1; - - /* create compressed boyer-moore delta 1 table */ skip = mlast - 1; - /* process pattern[:-1] */ - for (mask = i = 0; i < mlast; i++) { - mask |= (1 << (p[i] & 0x1F)); - if (p[i] == p[mlast]) - skip = mlast - i - 1; - } - /* process pattern[-1] outside the loop */ - mask |= (1 << (p[mlast] & 0x1F)); - for (i = 0; i <= w; i++) { - /* note: using mlast in the skip path slows things down on x86 */ - if (s[i+m-1] == p[m-1]) { - /* candidate match */ - for (j = 0; j < mlast; j++) - if (s[i+j] != p[j]) - break; - if (j == mlast) { - /* got a match! */ - if (mode != FAST_COUNT) + if (mode != FAST_RSEARCH) { + + /* create compressed boyer-moore delta 1 table */ + + /* process pattern[:-1] */ + for (mask = i = 0; i < mlast; i++) { + mask |= (1 << (p[i] & 0x1F)); + if (p[i] == p[mlast]) + skip = mlast - i - 1; + } + /* process pattern[-1] outside the loop */ + mask |= (1 << (p[mlast] & 0x1F)); + + for (i = 0; i <= w; i++) { + /* note: using mlast in the skip path slows things down on x86 */ + if (s[i+m-1] == p[m-1]) { + /* candidate match */ + for (j = 0; j < mlast; j++) + if (s[i+j] != p[j]) + break; + if (j == mlast) { + /* got a match! */ + if (mode != FAST_COUNT) + return i; + count++; + i = i + mlast; + continue; + } + /* miss: check if next character is part of pattern */ + if (!(mask & (1 << (s[i+m] & 0x1F)))) + i = i + m; + else + i = i + skip; + } else { + /* skip: check if next character is part of pattern */ + if (!(mask & (1 << (s[i+m] & 0x1F)))) + i = i + m; + } + } + } else { /* FAST_RSEARCH */ + + /* create compressed boyer-moore delta 1 table */ + + /* process pattern[0] outside the loop */ + mask = (1 << (p[0] & 0x1F)); + /* process pattern[:0:-1] */ + for (i = mlast; i > 0; i--) { + mask |= (1 << (p[i] & 0x1F)); + if (p[i] == p[0]) + skip = i - 1; + } + + for (i = w; i >= 0; i--) { + if (s[i] == p[0]) { + /* candidate match */ + for (j = mlast; j > 0; j--) + if (s[i+j] != p[j]) + break; + if (j == 0) + /* got a match! */ return i; - count++; - i = i + mlast; - continue; + /* miss: check if previous character is part of pattern */ + if (!(mask & (1 << (s[i-1] & 0x1F)))) + i = i - m; + else + i = i - skip; + } else { + /* skip: check if previous character is part of pattern */ + if (!(mask & (1 << (s[i-1] & 0x1F)))) + i = i - m; } - /* miss: check if next character is part of pattern */ - if (!(mask & (1 << (s[i+m] & 0x1F)))) - i = i + m; - else - i = i + skip; - } else { - /* skip: check if next character is part of pattern */ - if (!(mask & (1 << (s[i+m] & 0x1F)))) - i = i + m; } } Index: Objects/stringobject.c =================================================================== --- Objects/stringobject.c (revision 76970) +++ Objects/stringobject.c (working copy) @@ -1580,7 +1580,7 @@ Py_ssize_t maxsplit = -1, count=0; const char *s = PyString_AS_STRING(self), *sub; PyObject *list, *str, *subobj = Py_None; -#ifdef USE_FAST +#if 1 /* USE_FAST */ Py_ssize_t pos; #endif @@ -1612,13 +1612,13 @@ if (list == NULL) return NULL; -#ifdef USE_FAST +#if 1 /* USE_FAST */ i = j = 0; while (maxsplit-- > 0) { pos = fastsearch(s+i, len-i, sub, n, FAST_SEARCH); if (pos < 0) break; - j = i+pos; + j = i + pos; SPLIT_ADD(s, i, j); i = j + n; } @@ -1801,10 +1801,15 @@ static PyObject * string_rsplit(PyStringObject *self, PyObject *args) { - Py_ssize_t len = PyString_GET_SIZE(self), n, i, j; + Py_ssize_t len = PyString_GET_SIZE(self), n, j; Py_ssize_t maxsplit = -1, count=0; - const char *s, *sub; + const char *s = PyString_AS_STRING(self), *sub; PyObject *list, *str, *subobj = Py_None; +#if 1 /* USE_FAST */ + Py_ssize_t pos; +#else + Py_ssize_t i; +#endif if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit)) return NULL; @@ -1835,9 +1840,17 @@ return NULL; j = len; - i = j - n; - s = PyString_AS_STRING(self); +#if 1 /* USE_FAST */ + while (maxsplit-- > 0) { + pos = fastsearch(s, j, sub, n, FAST_RSEARCH); + if (pos < 0) + break; + SPLIT_ADD(s, pos + n, j); + j = pos; + } +#else + i = j - n; while ( (i >= 0) && (maxsplit-- > 0) ) { for (; i>=0; i--) { if (Py_STRING_MATCH(s, i, sub, n)) { @@ -1848,6 +1861,7 @@ } } } +#endif SPLIT_ADD(s, 0, j); FIX_PREALLOC_SIZE(list); if (PyList_Reverse(list) < 0) Index: Objects/bytearrayobject.c =================================================================== --- Objects/bytearrayobject.c (revision 76970) +++ Objects/bytearrayobject.c (working copy) @@ -2287,7 +2287,7 @@ const char *s = PyByteArray_AS_STRING(self), *sub; PyObject *list, *str, *subobj = Py_None; Py_buffer vsub; -#ifdef USE_FAST +#if 1 /* USE_FAST */ Py_ssize_t pos; #endif @@ -2321,7 +2321,7 @@ return NULL; } -#ifdef USE_FAST +#if 1 /* USE_FAST */ i = j = 0; while (maxsplit-- > 0) { pos = fastsearch(s+i, len-i, sub, n, FAST_SEARCH); @@ -2520,11 +2520,16 @@ static PyObject * bytearray_rsplit(PyByteArrayObject *self, PyObject *args) { - Py_ssize_t len = PyByteArray_GET_SIZE(self), n, i, j; + Py_ssize_t len = PyByteArray_GET_SIZE(self), n, j; Py_ssize_t maxsplit = -1, count = 0; const char *s = PyByteArray_AS_STRING(self), *sub; PyObject *list, *str, *subobj = Py_None; Py_buffer vsub; +#if 1 /* USE_FAST */ + Py_ssize_t pos; +#else + Py_ssize_t i; +#endif if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit)) return NULL; @@ -2557,8 +2562,17 @@ } j = len; - i = j - n; +#if 1 /* USE_FAST */ + while (maxsplit-- > 0) { + pos = fastsearch(s, j, sub, n, FAST_RSEARCH); + if (pos < 0) + break; + SPLIT_ADD(s, pos + n, j); + j = pos; + } +#else + i = j - n; while ( (i >= 0) && (maxsplit-- > 0) ) { for (; i>=0; i--) { if (Py_STRING_MATCH(s, i, sub, n)) { @@ -2569,6 +2583,7 @@ } } } +#endif SPLIT_ADD(s, 0, j); FIX_PREALLOC_SIZE(list); if (PyList_Reverse(list) < 0)