Skip to content

Commit 51fd6c9

Browse files
committed
extmod/ure: Use single function for match/search/sub.
Saves about 500 bytes on unix x64 and enables CPython-conform usage of passing a re object to these functions.
1 parent bd06c69 commit 51fd6c9

4 files changed

Lines changed: 43 additions & 39 deletions

File tree

examples/natmod/ure/ure.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a
7171
re_type.locals_dict = (void*)&re_locals_dict;
7272

7373
mp_store_global(MP_QSTR_compile, MP_OBJ_FROM_PTR(&mod_re_compile_obj));
74-
mp_store_global(MP_QSTR_match, MP_OBJ_FROM_PTR(&mod_re_match_obj));
75-
mp_store_global(MP_QSTR_search, MP_OBJ_FROM_PTR(&mod_re_search_obj));
74+
mp_store_global(MP_QSTR_match, MP_OBJ_FROM_PTR(&re_match_obj));
75+
mp_store_global(MP_QSTR_search, MP_OBJ_FROM_PTR(&re_search_obj));
7676

7777
MP_DYNRUNTIME_INIT_EXIT
7878
}

extmod/modure.c

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5761
STATIC 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

176180
STATIC 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
}
415426
MP_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
445429
STATIC 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) },

tests/extmod/ure1.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,14 @@
125125
print(re.compile(r"[a\-x]").split("foo-bar"))
126126
print(re.compile(r"[\-ax]").split("foo-bar"))
127127
print("===")
128+
129+
# Module functions take str/bytes/re.
130+
for f in (re.match, re.search):
131+
print(f(".", "foo").group(0))
132+
print(f(b".", b"foo").group(0))
133+
print(f(re.compile("."), "foo").group(0))
134+
try:
135+
f(123, "a")
136+
except TypeError:
137+
print("TypeError")
138+
print("===")

tests/extmod/ure_sub.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,12 @@ def A():
6060
re.sub("(a)", "b\\199999999999999999999999999999999999999", "a")
6161
except:
6262
print("invalid group")
63+
64+
# Module function takes str/bytes/re.
65+
print(re.sub("a", "a", "a"))
66+
print(re.sub(b".", b"a", b"a"))
67+
print(re.sub(re.compile("a"), "a", "a"))
68+
try:
69+
re.sub(123, "a", "a")
70+
except TypeError:
71+
print("TypeError")

0 commit comments

Comments
 (0)