@@ -278,6 +278,16 @@ static mp_obj_t str_find(uint n_args, const mp_obj_t *args) {
278278 }
279279}
280280
281+ // TODO: (Much) more variety in args
282+ static mp_obj_t str_startswith (mp_obj_t self_in , mp_obj_t arg ) {
283+ GET_STR_DATA_LEN (self_in , str , str_len );
284+ GET_STR_DATA_LEN (arg , prefix , prefix_len );
285+ if (prefix_len > str_len ) {
286+ return mp_const_false ;
287+ }
288+ return MP_BOOL (memcmp (str , prefix , prefix_len ) == 0 );
289+ }
290+
281291static bool chr_in_str (const byte * const str , const size_t str_len , int c ) {
282292 for (size_t i = 0 ; i < str_len ; i ++ ) {
283293 if (str [i ] == c ) {
@@ -364,13 +374,15 @@ mp_obj_t str_format(uint n_args, const mp_obj_t *args) {
364374static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_find_obj , 2 , 4 , str_find ) ;
365375static MP_DEFINE_CONST_FUN_OBJ_2 (str_join_obj , str_join ) ;
366376static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_split_obj , 1 , 3 , str_split ) ;
377+ static MP_DEFINE_CONST_FUN_OBJ_2 (str_startswith_obj , str_startswith ) ;
367378static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (str_strip_obj , 1 , 2 , str_strip ) ;
368379static MP_DEFINE_CONST_FUN_OBJ_VAR (str_format_obj , 1 , str_format ) ;
369380
370381static const mp_method_t str_type_methods [] = {
371382 { "find" , & str_find_obj },
372383 { "join" , & str_join_obj },
373384 { "split" , & str_split_obj },
385+ { "startswith" , & str_startswith_obj },
374386 { "strip" , & str_strip_obj },
375387 { "format" , & str_format_obj },
376388 { NULL , NULL }, // end-of-list sentinel
0 commit comments