diff options
author | Damien George <damien.p.george@gmail.com> | 2018-05-24 13:07:42 +1000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2018-07-02 14:53:30 +1000 |
commit | 1f864609106cc293da3748ee8f93e40c4a28a495 (patch) | |
tree | 9d1be7220d1f59f52312b7fa835b409d9693cc4d /extmod/modure.c | |
parent | ab02abe96dc2ccdb2556c894dc04de11674e3476 (diff) |
extmod/modure: Add match.groups() method, and tests.
This feature is controlled at compile time by MICROPY_PY_URE_MATCH_GROUPS,
disabled by default.
Thanks to @dmazzella for the original patch for this feature; see #3770.
Diffstat (limited to 'extmod/modure.c')
-rw-r--r-- | extmod/modure.c | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/extmod/modure.c b/extmod/modure.c index 31c2b9864..aec7350b2 100644 --- a/extmod/modure.c +++ b/extmod/modure.c @@ -77,8 +77,28 @@ STATIC mp_obj_t match_group(mp_obj_t self_in, mp_obj_t no_in) { } MP_DEFINE_CONST_FUN_OBJ_2(match_group_obj, match_group); +#if MICROPY_PY_URE_MATCH_GROUPS + +STATIC mp_obj_t match_groups(mp_obj_t self_in) { + mp_obj_match_t *self = MP_OBJ_TO_PTR(self_in); + if (self->num_matches <= 1) { + return mp_const_empty_tuple; + } + mp_obj_tuple_t *groups = MP_OBJ_TO_PTR(mp_obj_new_tuple(self->num_matches - 1, NULL)); + for (int i = 1; i < self->num_matches; ++i) { + groups->items[i - 1] = match_group(self_in, MP_OBJ_NEW_SMALL_INT(i)); + } + return MP_OBJ_FROM_PTR(groups); +} +MP_DEFINE_CONST_FUN_OBJ_1(match_groups_obj, match_groups); + +#endif + STATIC const mp_rom_map_elem_t match_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_group), MP_ROM_PTR(&match_group_obj) }, + #if MICROPY_PY_URE_MATCH_GROUPS + { MP_ROM_QSTR(MP_QSTR_groups), MP_ROM_PTR(&match_groups_obj) }, + #endif }; STATIC MP_DEFINE_CONST_DICT(match_locals_dict, match_locals_dict_table); |