py/objstr.c: Add support for bytes.find(int).

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
pull/11631/head
Jim Mussared 2023-05-26 17:53:00 +10:00
rodzic ed7a3b11d9
commit a64ea602fd
1 zmienionych plików z 22 dodań i 4 usunięć

Wyświetl plik

@ -743,11 +743,29 @@ STATIC mp_obj_t str_finder(size_t n_args, const mp_obj_t *args, int direction, b
const mp_obj_type_t *self_type = mp_obj_get_type(args[0]);
check_is_str_or_bytes(args[0]);
// check argument type
str_check_arg_type(self_type, args[1]);
GET_STR_DATA_LEN(args[0], haystack, haystack_len);
GET_STR_DATA_LEN(args[1], needle, needle_len);
mp_int_t val;
byte needle_data;
const byte *needle;
size_t needle_len;
if (self_type != &mp_type_str && mp_obj_get_int_maybe(args[1], &val)) {
// Allow {bytes/bytearray}.{find,index}(int).
#if MICROPY_FULL_CHECKS
if (val < 0 || val > 255) {
mp_raise_ValueError(MP_ERROR_TEXT("bytes value out of range"));
}
#endif
needle_data = val;
needle = &needle_data;
needle_len = 1;
} else {
// check argument type
str_check_arg_type(self_type, args[1]);
GET_STR_DATA_LEN(args[1], needle_tmp, needle_len_tmp);
needle = needle_tmp;
needle_len = needle_len_tmp;
}
const byte *start = haystack;
const byte *end = haystack + haystack_len;