summaryrefslogtreecommitdiffstats
path: root/Objects/bytearrayobject.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-03-24 19:55:47 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-03-24 19:55:47 (GMT)
commitd9d769fcdd573ab12b628798288c02dceba53505 (patch)
treeb61e4767ddb9a89e4402a041f381584662363584 /Objects/bytearrayobject.c
parentf7ef47583e743ebecaae8c93dcb92e7a5792884f (diff)
downloadcpython-d9d769fcdd573ab12b628798288c02dceba53505.zip
cpython-d9d769fcdd573ab12b628798288c02dceba53505.tar.gz
cpython-d9d769fcdd573ab12b628798288c02dceba53505.tar.bz2
Issue #23573: Increased performance of string search operations (str.find,
str.index, str.count, the in operator, str.split, str.partition) with arguments of different kinds (UCS1, UCS2, UCS4).
Diffstat (limited to 'Objects/bytearrayobject.c')
-rw-r--r--Objects/bytearrayobject.c33
1 files changed, 24 insertions, 9 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index 8b3267e..195c79f 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -1142,7 +1142,7 @@ bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir)
char byte;
Py_buffer subbuf;
const char *sub;
- Py_ssize_t sub_len;
+ Py_ssize_t len, sub_len;
Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
Py_ssize_t res;
@@ -1161,15 +1161,30 @@ bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir)
sub = &byte;
sub_len = 1;
}
+ len = PyByteArray_GET_SIZE(self);
- if (dir > 0)
- res = stringlib_find_slice(
- PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
- sub, sub_len, start, end);
- else
- res = stringlib_rfind_slice(
- PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
- sub, sub_len, start, end);
+ ADJUST_INDICES(start, end, len);
+ if (end - start < sub_len)
+ res = -1;
+ else if (sub_len == 1) {
+ unsigned char needle = *sub;
+ int mode = (dir > 0) ? FAST_SEARCH : FAST_RSEARCH;
+ res = stringlib_fastsearch_memchr_1char(
+ PyByteArray_AS_STRING(self) + start, end - start,
+ needle, needle, mode);
+ if (res >= 0)
+ res += start;
+ }
+ else {
+ if (dir > 0)
+ res = stringlib_find_slice(
+ PyByteArray_AS_STRING(self), len,
+ sub, sub_len, start, end);
+ else
+ res = stringlib_rfind_slice(
+ PyByteArray_AS_STRING(self), len,
+ sub, sub_len, start, end);
+ }
if (subobj)
PyBuffer_Release(&subbuf);