diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-01-02 21:12:58 +0000 |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-01-02 21:12:58 +0000 |
commit | 5b7139aab41becad7ad736bd9ff2332960bf67f9 (patch) | |
tree | 9d3d3e7da0c0073af1fd2784fa7892d3222f9e88 /Objects/bytearrayobject.c | |
parent | d3e323215c6d9f303bf42875f98e365e2ff1734f (diff) | |
download | cpython-git-5b7139aab41becad7ad736bd9ff2332960bf67f9.tar.gz |
Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
`rindex`, `rsplit` and `rpartition` methods. Patch by Florent Xicluna.
Diffstat (limited to 'Objects/bytearrayobject.c')
-rw-r--r-- | Objects/bytearrayobject.c | 39 |
1 files changed, 9 insertions, 30 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 2262601b93..6157c832a1 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -1111,7 +1111,6 @@ bytearray_dealloc(PyByteArrayObject *self) /* Methods */ #define STRINGLIB_CHAR char -#define STRINGLIB_CMP memcmp #define STRINGLIB_LEN PyByteArray_GET_SIZE #define STRINGLIB_STR PyByteArray_AS_STRING #define STRINGLIB_NEW PyByteArray_FromStringAndSize @@ -2282,14 +2281,11 @@ If maxsplit is given, at most maxsplit splits are done."); static PyObject * bytearray_split(PyByteArrayObject *self, PyObject *args) { - Py_ssize_t len = PyByteArray_GET_SIZE(self), n, i, j; + Py_ssize_t len = PyByteArray_GET_SIZE(self), n, i, j, pos; Py_ssize_t maxsplit = -1, count = 0; const char *s = PyByteArray_AS_STRING(self), *sub; PyObject *list, *str, *subobj = Py_None; Py_buffer vsub; -#ifdef USE_FAST - Py_ssize_t pos; -#endif if (!PyArg_ParseTuple(args, "|On:split", &subobj, &maxsplit)) return NULL; @@ -2321,7 +2317,6 @@ bytearray_split(PyByteArrayObject *self, PyObject *args) return NULL; } -#ifdef USE_FAST i = j = 0; while (maxsplit-- > 0) { pos = fastsearch(s+i, len-i, sub, n, FAST_SEARCH); @@ -2331,18 +2326,6 @@ bytearray_split(PyByteArrayObject *self, PyObject *args) SPLIT_ADD(s, i, j); i = j + n; } -#else - i = j = 0; - while ((j+n <= len) && (maxsplit-- > 0)) { - for (; j+n <= len; j++) { - if (Py_STRING_MATCH(s, j, sub, n)) { - SPLIT_ADD(s, i, j); - i = j = j + n; - break; - } - } - } -#endif SPLIT_ADD(s, i, len); FIX_PREALLOC_SIZE(list); PyBuffer_Release(&vsub); @@ -2520,7 +2503,7 @@ If maxsplit is given, at most maxsplit splits are done."); 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, pos; Py_ssize_t maxsplit = -1, count = 0; const char *s = PyByteArray_AS_STRING(self), *sub; PyObject *list, *str, *subobj = Py_None; @@ -2557,17 +2540,13 @@ bytearray_rsplit(PyByteArrayObject *self, PyObject *args) } j = len; - i = j - n; - - while ( (i >= 0) && (maxsplit-- > 0) ) { - for (; i>=0; i--) { - if (Py_STRING_MATCH(s, i, sub, n)) { - SPLIT_ADD(s, i + n, j); - j = i; - i -= n; - break; - } - } + + while (maxsplit-- > 0) { + pos = fastsearch(s, j, sub, n, FAST_RSEARCH); + if (pos < 0) + break; + SPLIT_ADD(s, pos + n, j); + j = pos; } SPLIT_ADD(s, 0, j); FIX_PREALLOC_SIZE(list); |