@@ -768,23 +768,11 @@ static mp_obj_t str_finder(size_t n_args, const mp_obj_t *args, int direction, b
768768
769769 GET_STR_DATA_LEN (args [0 ], haystack , haystack_len );
770770 GET_STR_DATA_LEN (args [1 ], needle , needle_len );
771+ size_t sub_len ;
772+ const byte * start = get_substring_data (args [0 ], n_args - 2 , args + 2 , & sub_len );
771773
772- const byte * start = haystack ;
773- const byte * end = haystack + haystack_len ;
774- if (n_args >= 3 && args [2 ] != mp_const_none ) {
775- start = str_index_to_ptr (self_type , haystack , haystack_len , args [2 ], true);
776- }
777- if (n_args >= 4 && args [3 ] != mp_const_none ) {
778- end = str_index_to_ptr (self_type , haystack , haystack_len , args [3 ], true);
779- }
780-
781- if (end < start ) {
782- goto out_error ;
783- }
784-
785- const byte * p = find_subbytes (start , end - start , needle , needle_len , direction );
774+ const byte * p = find_subbytes (start , sub_len , needle , needle_len , direction );
786775 if (p == NULL ) {
787- out_error :
788776 // not found
789777 if (is_index ) {
790778 mp_raise_ValueError (MP_ERROR_TEXT ("substring not found" ));
@@ -1779,32 +1767,24 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace);
17791767static mp_obj_t str_count (size_t n_args , const mp_obj_t * args ) {
17801768 const mp_obj_type_t * self_type = mp_obj_get_type (args [0 ]);
17811769 check_is_str_or_bytes (args [0 ]);
1782-
17831770 // check argument type
17841771 str_check_arg_type (self_type , args [1 ]);
17851772
1786- GET_STR_DATA_LEN (args [0 ], haystack , haystack_len );
1787- GET_STR_DATA_LEN (args [1 ], needle , needle_len );
1788-
1789- const byte * start = haystack ;
1773+ size_t haystack_len ;
1774+ const byte * haystack = get_substring_data (args [0 ], n_args - 2 , args + 2 , & haystack_len );
17901775 const byte * end = haystack + haystack_len ;
1791- if (n_args >= 3 && args [2 ] != mp_const_none ) {
1792- start = str_index_to_ptr (self_type , haystack , haystack_len , args [2 ], true);
1793- }
1794- if (n_args >= 4 && args [3 ] != mp_const_none ) {
1795- end = str_index_to_ptr (self_type , haystack , haystack_len , args [3 ], true);
1796- }
1776+ GET_STR_DATA_LEN (args [1 ], needle , needle_len );
17971777
17981778 // if needle_len is zero then we count each gap between characters as an occurrence
17991779 if (needle_len == 0 ) {
1800- return MP_OBJ_NEW_SMALL_INT (utf8_charlen (start , end - start ) + 1 );
1780+ return MP_OBJ_NEW_SMALL_INT (utf8_charlen (haystack , haystack_len ) + 1 );
18011781 }
18021782
18031783 bool is_str = self_type == & mp_type_str ;
18041784
18051785 // count the occurrences
18061786 mp_int_t num_occurrences = 0 ;
1807- for (const byte * haystack_ptr = start ; haystack_ptr + needle_len <= end ;) {
1787+ for (const byte * haystack_ptr = haystack ; haystack_ptr + needle_len <= end ;) {
18081788 if (memcmp (haystack_ptr , needle , needle_len ) == 0 ) {
18091789 num_occurrences ++ ;
18101790 haystack_ptr += needle_len ;
0 commit comments