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.
This commit is contained in:
Damien George 2018-05-24 13:07:42 +10:00 committed by Noralf Trønnes
parent 379258112a
commit a24fabbb6f
3 changed files with 57 additions and 0 deletions

View File

@ -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);

View File

@ -1164,6 +1164,10 @@ typedef double mp_float_t;
#define MICROPY_PY_URE (0)
#endif
#ifndef MICROPY_PY_URE_MATCH_GROUPS
#define MICROPY_PY_URE_MATCH_GROUPS (0)
#endif
#ifndef MICROPY_PY_UHEAPQ
#define MICROPY_PY_UHEAPQ (0)
#endif

View File

@ -0,0 +1,33 @@
# test match.groups()
try:
import ure as re
except ImportError:
try:
import re
except ImportError:
print("SKIP")
raise SystemExit
try:
m = re.match(".", "a")
m.groups
except AttributeError:
print('SKIP')
raise SystemExit
m = re.match(r'(([0-9]*)([a-z]*)[0-9]*)','1234hello567')
print(m.groups())
m = re.match(r'([0-9]*)(([a-z]*)([0-9]*))','1234hello567')
print(m.groups())
# optional group that matches
print(re.match(r'(a)?b(c)', 'abc').groups())
# optional group that doesn't match
print(re.match(r'(a)?b(c)', 'bc').groups())
# only a single match
print(re.match(r'abc', 'abc').groups())