@@ -438,7 +438,7 @@ STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
438438 return res ;
439439}
440440
441- STATIC mp_obj_t str_finder (uint n_args , const mp_obj_t * args , machine_int_t direction ) {
441+ STATIC mp_obj_t str_finder (uint n_args , const mp_obj_t * args , machine_int_t direction , bool is_index ) {
442442 assert (2 <= n_args && n_args <= 4 );
443443 assert (MP_OBJ_IS_STR (args [0 ]));
444444 assert (MP_OBJ_IS_STR (args [1 ]));
@@ -458,19 +458,31 @@ STATIC mp_obj_t str_finder(uint n_args, const mp_obj_t *args, machine_int_t dire
458458 const byte * p = find_subbytes (haystack + start , end - start , needle , needle_len , direction );
459459 if (p == NULL ) {
460460 // not found
461- return MP_OBJ_NEW_SMALL_INT (-1 );
461+ if (is_index ) {
462+ nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , "substring not found" ));
463+ } else {
464+ return MP_OBJ_NEW_SMALL_INT (-1 );
465+ }
462466 } else {
463467 // found
464468 return MP_OBJ_NEW_SMALL_INT (p - haystack );
465469 }
466470}
467471
468472STATIC mp_obj_t str_find (uint n_args , const mp_obj_t * args ) {
469- return str_finder (n_args , args , 1 );
473+ return str_finder (n_args , args , 1 , false );
470474}
471475
472476STATIC mp_obj_t str_rfind (uint n_args , const mp_obj_t * args ) {
473- return str_finder (n_args , args , -1 );
477+ return str_finder (n_args , args , -1 , false);
478+ }
479+
480+ STATIC mp_obj_t str_index (uint n_args , const mp_obj_t * args ) {
481+ return str_finder (n_args , args , 1 , true);
482+ }
483+
484+ STATIC mp_obj_t str_rindex (uint n_args , const mp_obj_t * args ) {
485+ return str_finder (n_args , args , -1 , true);
474486}
475487
476488// TODO: (Much) more variety in args
@@ -1125,7 +1137,7 @@ STATIC mp_obj_t str_replace(uint n_args, const mp_obj_t *args) {
11251137 }
11261138 }
11271139
1128- // if max_rep is still 0 by this point we will need to do all possible replacements
1140+ // if max_rep is still -1 by this point we will need to do all possible replacements
11291141
11301142 // check argument types
11311143
@@ -1304,6 +1316,8 @@ STATIC machine_int_t str_get_buffer(mp_obj_t self_in, buffer_info_t *bufinfo, in
13041316
13051317STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_find_obj , 2 , 4 , str_find );
13061318STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_rfind_obj , 2 , 4 , str_rfind );
1319+ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_index_obj , 2 , 4 , str_index );
1320+ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_rindex_obj , 2 , 4 , str_rindex );
13071321STATIC MP_DEFINE_CONST_FUN_OBJ_2 (str_join_obj , str_join );
13081322STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_split_obj , 1 , 3 , str_split );
13091323STATIC MP_DEFINE_CONST_FUN_OBJ_2 (str_startswith_obj , str_startswith );
@@ -1317,6 +1331,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_rpartition_obj, str_rpartition);
13171331STATIC const mp_map_elem_t str_locals_dict_table [] = {
13181332 { MP_OBJ_NEW_QSTR (MP_QSTR_find ), (mp_obj_t )& str_find_obj },
13191333 { MP_OBJ_NEW_QSTR (MP_QSTR_rfind ), (mp_obj_t )& str_rfind_obj },
1334+ { MP_OBJ_NEW_QSTR (MP_QSTR_index ), (mp_obj_t )& str_index_obj },
1335+ { MP_OBJ_NEW_QSTR (MP_QSTR_rindex ), (mp_obj_t )& str_rindex_obj },
13201336 { MP_OBJ_NEW_QSTR (MP_QSTR_join ), (mp_obj_t )& str_join_obj },
13211337 { MP_OBJ_NEW_QSTR (MP_QSTR_split ), (mp_obj_t )& str_split_obj },
13221338 { MP_OBJ_NEW_QSTR (MP_QSTR_startswith ), (mp_obj_t )& str_startswith_obj },
0 commit comments