Browse Source

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.
pull/3903/merge
Damien George 7 years ago
parent
commit
1f86460910
  1. 20
      extmod/modure.c
  2. 4
      py/mpconfig.h
  3. 33
      tests/extmod/ure_groups.py

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

4
py/mpconfig.h

@ -1142,6 +1142,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

33
tests/extmod/ure_groups.py

@ -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())
Loading…
Cancel
Save