Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
`rindex`, `rsplit` and `rpartition` methods. Patch by Florent Xicluna.
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index 2262601..6157c83 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -1111,7 +1111,6 @@
/* 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 @@
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 @@
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 @@
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 @@
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 @@
}
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);