diff options
| author | Noralf Trønnes <noralf@tronnes.org> | 2019-02-12 22:42:59 +0100 |
|---|---|---|
| committer | Noralf Trønnes <noralf@tronnes.org> | 2019-02-14 15:42:28 +0100 |
| commit | 0865c9d38193b61898d631081161cac4d3365437 (patch) | |
| tree | cc8d25044ba20631cae49b2c8525c030a0916fce /extmod/modure.c | |
| parent | 20a787adb4140002acb8e5ea21536069e184ffaf (diff) | |
extmod/ure: Support search/match() pos and endpos parameters
MICROPY_PY_URE_MATCH_SPAN_START_END is used to enable the functionality
since it's similar.
Diffstat (limited to 'extmod/modure.c')
| -rw-r--r-- | extmod/modure.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/extmod/modure.c b/extmod/modure.c index 847801966..a368ee8fa 100644 --- a/extmod/modure.c +++ b/extmod/modure.c @@ -178,6 +178,35 @@ STATIC mp_obj_t ure_exec(bool is_anchored, uint n_args, const mp_obj_t *args) { size_t len; subj.begin = mp_obj_str_get_data(args[1], &len); subj.end = subj.begin + len; +#if MICROPY_PY_URE_MATCH_SPAN_START_END + if (n_args > 2) { + const mp_obj_type_t *self_type = mp_obj_get_type(args[1]); + mp_int_t str_len = MP_OBJ_SMALL_INT_VALUE(mp_obj_len_maybe(args[1])); + const byte *begin = (const byte *)subj.begin; + + int pos = mp_obj_get_int(args[2]); + if (pos >= str_len) { + return mp_const_none; + } + if (pos < 0) { + pos = 0; + } + const byte *pos_ptr = str_index_to_ptr(self_type, begin, len, MP_OBJ_NEW_SMALL_INT(pos), true); + + const byte *endpos_ptr = (const byte *)subj.end; + if (n_args > 3) { + int endpos = mp_obj_get_int(args[3]); + if (endpos <= pos) { + return mp_const_none; + } + // Will cap to length + endpos_ptr = str_index_to_ptr(self_type, begin, len, args[3], true); + } + + subj.begin = (const char *)pos_ptr; + subj.end = (const char *)endpos_ptr; + } +#endif int caps_num = (self->re.sub + 1) * 2; mp_obj_match_t *match = m_new_obj_var(mp_obj_match_t, char*, caps_num); // cast is a workaround for a bug in msvc: it treats const char** as a const pointer instead of a pointer to pointer to const char |
