diff options
| author | Serhiy Storchaka <storchaka@gmail.com> | 2015-03-24 19:55:47 (GMT) |
|---|---|---|
| committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-03-24 19:55:47 (GMT) |
| commit | d9d769fcdd573ab12b628798288c02dceba53505 (patch) | |
| tree | b61e4767ddb9a89e4402a041f381584662363584 /Objects/bytesobject.c | |
| parent | f7ef47583e743ebecaae8c93dcb92e7a5792884f (diff) | |
| download | cpython-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/bytesobject.c')
| -rw-r--r-- | Objects/bytesobject.c | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 5a9dfbd..ae3c289 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -1914,7 +1914,7 @@ bytes_find_internal(PyBytesObject *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; @@ -1933,15 +1933,30 @@ bytes_find_internal(PyBytesObject *self, PyObject *args, int dir) sub = &byte; sub_len = 1; } + len = PyBytes_GET_SIZE(self); - if (dir > 0) - res = stringlib_find_slice( - PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), - sub, sub_len, start, end); - else - res = stringlib_rfind_slice( - PyBytes_AS_STRING(self), PyBytes_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( + PyBytes_AS_STRING(self) + start, end - start, + needle, needle, mode); + if (res >= 0) + res += start; + } + else { + if (dir > 0) + res = stringlib_find_slice( + PyBytes_AS_STRING(self), len, + sub, sub_len, start, end); + else + res = stringlib_rfind_slice( + PyBytes_AS_STRING(self), len, + sub, sub_len, start, end); + } if (subobj) PyBuffer_Release(&subbuf); |
