@@ -53,6 +53,10 @@ typedef struct _mp_obj_match_t {
5353 const char * caps [0 ];
5454} mp_obj_match_t ;
5555
56+ STATIC mp_obj_t mod_re_compile (size_t n_args , const mp_obj_t * args );
57+ #if !MICROPY_ENABLE_DYNRUNTIME
58+ STATIC const mp_obj_type_t re_type ;
59+ #endif
5660
5761STATIC void match_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
5862 (void )kind ;
@@ -175,7 +179,12 @@ STATIC void re_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t
175179
176180STATIC mp_obj_t ure_exec (bool is_anchored , uint n_args , const mp_obj_t * args ) {
177181 (void )n_args ;
178- mp_obj_re_t * self = MP_OBJ_TO_PTR (args [0 ]);
182+ mp_obj_re_t * self ;
183+ if (mp_obj_is_type (args [0 ], & re_type )) {
184+ self = MP_OBJ_TO_PTR (args [0 ]);
185+ } else {
186+ self = MP_OBJ_TO_PTR (mod_re_compile (1 , args ));
187+ }
179188 Subject subj ;
180189 size_t len ;
181190 subj .begin = mp_obj_str_get_data (args [1 ], & len );
@@ -253,8 +262,13 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(re_split_obj, 2, 3, re_split);
253262
254263#if MICROPY_PY_URE_SUB
255264
256- STATIC mp_obj_t re_sub_helper (mp_obj_t self_in , size_t n_args , const mp_obj_t * args ) {
257- mp_obj_re_t * self = MP_OBJ_TO_PTR (self_in );
265+ STATIC mp_obj_t re_sub_helper (size_t n_args , const mp_obj_t * args ) {
266+ mp_obj_re_t * self ;
267+ if (mp_obj_is_type (args [0 ], & re_type )) {
268+ self = MP_OBJ_TO_PTR (args [0 ]);
269+ } else {
270+ self = MP_OBJ_TO_PTR (mod_re_compile (1 , args ));
271+ }
258272 mp_obj_t replace = args [1 ];
259273 mp_obj_t where = args [2 ];
260274 mp_int_t count = 0 ;
@@ -358,10 +372,7 @@ STATIC mp_obj_t re_sub_helper(mp_obj_t self_in, size_t n_args, const mp_obj_t *a
358372 return mp_obj_new_str_from_vstr (mp_obj_get_type (where ), & vstr_return );
359373}
360374
361- STATIC mp_obj_t re_sub (size_t n_args , const mp_obj_t * args ) {
362- return re_sub_helper (args [0 ], n_args , args );
363- }
364- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (re_sub_obj , 3 , 5 , re_sub );
375+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (re_sub_obj , 3 , 5 , re_sub_helper );
365376
366377#endif
367378
@@ -414,41 +425,14 @@ STATIC mp_obj_t mod_re_compile(size_t n_args, const mp_obj_t *args) {
414425}
415426MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (mod_re_compile_obj , 1 , 2 , mod_re_compile );
416427
417- STATIC mp_obj_t mod_re_exec (bool is_anchored , uint n_args , const mp_obj_t * args ) {
418- (void )n_args ;
419- mp_obj_t self = mod_re_compile (1 , args );
420-
421- const mp_obj_t args2 [] = {self , args [1 ]};
422- mp_obj_t match = ure_exec (is_anchored , 2 , args2 );
423- return match ;
424- }
425-
426- STATIC mp_obj_t mod_re_match (size_t n_args , const mp_obj_t * args ) {
427- return mod_re_exec (true, n_args , args );
428- }
429- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (mod_re_match_obj , 2 , 4 , mod_re_match );
430-
431- STATIC mp_obj_t mod_re_search (size_t n_args , const mp_obj_t * args ) {
432- return mod_re_exec (false, n_args , args );
433- }
434- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (mod_re_search_obj , 2 , 4 , mod_re_search );
435-
436- #if MICROPY_PY_URE_SUB
437- STATIC mp_obj_t mod_re_sub (size_t n_args , const mp_obj_t * args ) {
438- mp_obj_t self = mod_re_compile (1 , args );
439- return re_sub_helper (self , n_args , args );
440- }
441- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (mod_re_sub_obj , 3 , 5 , mod_re_sub );
442- #endif
443-
444428#if !MICROPY_ENABLE_DYNRUNTIME
445429STATIC const mp_rom_map_elem_t mp_module_re_globals_table [] = {
446430 { MP_ROM_QSTR (MP_QSTR___name__ ), MP_ROM_QSTR (MP_QSTR_ure ) },
447431 { MP_ROM_QSTR (MP_QSTR_compile ), MP_ROM_PTR (& mod_re_compile_obj ) },
448- { MP_ROM_QSTR (MP_QSTR_match ), MP_ROM_PTR (& mod_re_match_obj ) },
449- { MP_ROM_QSTR (MP_QSTR_search ), MP_ROM_PTR (& mod_re_search_obj ) },
432+ { MP_ROM_QSTR (MP_QSTR_match ), MP_ROM_PTR (& re_match_obj ) },
433+ { MP_ROM_QSTR (MP_QSTR_search ), MP_ROM_PTR (& re_search_obj ) },
450434 #if MICROPY_PY_URE_SUB
451- { MP_ROM_QSTR (MP_QSTR_sub ), MP_ROM_PTR (& mod_re_sub_obj ) },
435+ { MP_ROM_QSTR (MP_QSTR_sub ), MP_ROM_PTR (& re_sub_obj ) },
452436 #endif
453437 #if MICROPY_PY_URE_DEBUG
454438 { MP_ROM_QSTR (MP_QSTR_DEBUG ), MP_ROM_INT (FLAG_DEBUG ) },
0 commit comments