@@ -1142,7 +1142,7 @@ bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir)
11421142 char byte ;
11431143 Py_buffer subbuf ;
11441144 const char * sub ;
1145- Py_ssize_t sub_len ;
1145+ Py_ssize_t len , sub_len ;
11461146 Py_ssize_t start = 0 , end = PY_SSIZE_T_MAX ;
11471147 Py_ssize_t res ;
11481148
@@ -1161,15 +1161,30 @@ bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir)
11611161 sub = & byte ;
11621162 sub_len = 1 ;
11631163 }
1164+ len = PyByteArray_GET_SIZE (self );
11641165
1165- if (dir > 0 )
1166- res = stringlib_find_slice (
1167- PyByteArray_AS_STRING (self ), PyByteArray_GET_SIZE (self ),
1168- sub , sub_len , start , end );
1169- else
1170- res = stringlib_rfind_slice (
1171- PyByteArray_AS_STRING (self ), PyByteArray_GET_SIZE (self ),
1172- sub , sub_len , start , end );
1166+ ADJUST_INDICES (start , end , len );
1167+ if (end - start < sub_len )
1168+ res = -1 ;
1169+ else if (sub_len == 1 ) {
1170+ unsigned char needle = * sub ;
1171+ int mode = (dir > 0 ) ? FAST_SEARCH : FAST_RSEARCH ;
1172+ res = stringlib_fastsearch_memchr_1char (
1173+ PyByteArray_AS_STRING (self ) + start , end - start ,
1174+ needle , needle , mode );
1175+ if (res >= 0 )
1176+ res += start ;
1177+ }
1178+ else {
1179+ if (dir > 0 )
1180+ res = stringlib_find_slice (
1181+ PyByteArray_AS_STRING (self ), len ,
1182+ sub , sub_len , start , end );
1183+ else
1184+ res = stringlib_rfind_slice (
1185+ PyByteArray_AS_STRING (self ), len ,
1186+ sub , sub_len , start , end );
1187+ }
11731188
11741189 if (subobj )
11751190 PyBuffer_Release (& subbuf );
0 commit comments