summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2018-05-24 13:07:42 +1000
committerDamien George <damien.p.george@gmail.com>2018-07-02 14:53:30 +1000
commit1f864609106cc293da3748ee8f93e40c4a28a495 (patch)
tree9d1be7220d1f59f52312b7fa835b409d9693cc4d
parentab02abe96dc2ccdb2556c894dc04de11674e3476 (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.
-rw-r--r--extmod/modure.c20
-rw-r--r--py/mpconfig.h4
-rw-r--r--tests/extmod/ure_groups.py33
3 files changed, 57 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);
diff --git a/py/mpconfig.h b/py/mpconfig.h
index 8f202380d..a979a9579 100644
--- a/py/mpconfig.h
+++ b/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
diff --git a/tests/extmod/ure_groups.py b/tests/extmod/ure_groups.py
new file mode 100644
index 000000000..4fac896d7
--- /dev/null
+++ b/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())