2014-09-12 11:48:07 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-09-12 11:48:07 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Paul Sokolovsky
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-01-01 15:35:21 -05:00
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/binary.h"
|
2017-06-30 18:25:23 -04:00
|
|
|
#include "py/objstr.h"
|
2017-10-02 17:12:06 -04:00
|
|
|
#include "py/stackctrl.h"
|
2014-09-12 11:48:07 -04:00
|
|
|
|
2022-09-05 08:58:04 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_STR_UNICODE
|
|
|
|
#include "py/unicode.h"
|
|
|
|
#endif
|
|
|
|
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_RE
|
2014-09-12 11:48:07 -04:00
|
|
|
|
2017-10-02 17:12:06 -04:00
|
|
|
#define re1_5_stack_chk() MP_STACK_CHECK()
|
|
|
|
|
2021-07-12 02:34:27 -04:00
|
|
|
#include "lib/re1.5/re1.5.h"
|
2014-09-12 11:48:07 -04:00
|
|
|
|
|
|
|
#define FLAG_DEBUG 0x1000
|
|
|
|
|
|
|
|
typedef struct _mp_obj_re_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
ByteProg re;
|
|
|
|
} mp_obj_re_t;
|
|
|
|
|
|
|
|
typedef struct _mp_obj_match_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
int num_matches;
|
|
|
|
mp_obj_t str;
|
|
|
|
const char *caps[0];
|
|
|
|
} mp_obj_match_t;
|
|
|
|
|
2020-06-03 04:18:49 -04:00
|
|
|
STATIC mp_obj_t mod_re_compile(size_t n_args, const mp_obj_t *args);
|
|
|
|
#if !MICROPY_ENABLE_DYNRUNTIME
|
|
|
|
STATIC const mp_obj_type_t re_type;
|
|
|
|
#endif
|
2014-09-12 11:48:07 -04:00
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
STATIC void match_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)kind;
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_match_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_printf(print, "<match num=%d>", self->num_matches);
|
2014-09-12 11:48:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t match_group(mp_obj_t self_in, mp_obj_t no_in) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_match_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-05-12 18:05:53 -04:00
|
|
|
mp_int_t no = mp_obj_get_int(no_in);
|
2015-02-18 09:47:14 -05:00
|
|
|
if (no < 0 || no >= self->num_matches) {
|
2021-07-14 08:56:52 -04:00
|
|
|
mp_raise_type_arg(&mp_type_IndexError, no_in);
|
2014-09-12 11:48:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *start = self->caps[no * 2];
|
2015-03-04 08:51:32 -05:00
|
|
|
if (start == NULL) {
|
|
|
|
// no match for this group
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-06-30 18:25:23 -04:00
|
|
|
return mp_obj_new_str_of_type(mp_obj_get_type(self->str),
|
|
|
|
(const byte *)start, self->caps[no * 2 + 1] - start);
|
2014-09-12 11:48:07 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(match_group_obj, match_group);
|
|
|
|
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_RE_MATCH_GROUPS
|
2018-05-23 23:07:42 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_RE_MATCH_SPAN_START_END
|
2018-05-23 23:08:15 -04:00
|
|
|
|
|
|
|
STATIC void match_span_helper(size_t n_args, const mp_obj_t *args, mp_obj_t span[2]) {
|
|
|
|
mp_obj_match_t *self = MP_OBJ_TO_PTR(args[0]);
|
|
|
|
|
|
|
|
mp_int_t no = 0;
|
|
|
|
if (n_args == 2) {
|
|
|
|
no = mp_obj_get_int(args[1]);
|
|
|
|
if (no < 0 || no >= self->num_matches) {
|
2021-07-14 08:56:52 -04:00
|
|
|
mp_raise_type_arg(&mp_type_IndexError, args[1]);
|
2018-05-23 23:08:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_int_t s = -1;
|
|
|
|
mp_int_t e = -1;
|
|
|
|
const char *start = self->caps[no * 2];
|
|
|
|
if (start != NULL) {
|
|
|
|
// have a match for this group
|
|
|
|
const char *begin = mp_obj_str_get_str(self->str);
|
|
|
|
s = start - begin;
|
|
|
|
e = self->caps[no * 2 + 1] - begin;
|
|
|
|
}
|
|
|
|
|
2022-09-05 08:58:04 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_STR_UNICODE
|
|
|
|
if (mp_obj_get_type(self->str) == &mp_type_str) {
|
|
|
|
const byte *begin = (const byte *)mp_obj_str_get_str(self->str);
|
|
|
|
if (s != -1) {
|
|
|
|
s = utf8_ptr_to_index(begin, begin + s);
|
|
|
|
}
|
|
|
|
if (e != -1) {
|
|
|
|
e = utf8_ptr_to_index(begin, begin + e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-05-23 23:08:15 -04:00
|
|
|
span[0] = mp_obj_new_int(s);
|
|
|
|
span[1] = mp_obj_new_int(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t match_span(size_t n_args, const mp_obj_t *args) {
|
|
|
|
mp_obj_t span[2];
|
|
|
|
match_span_helper(n_args, args, span);
|
|
|
|
return mp_obj_new_tuple(2, span);
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(match_span_obj, 1, 2, match_span);
|
|
|
|
|
|
|
|
STATIC mp_obj_t match_start(size_t n_args, const mp_obj_t *args) {
|
|
|
|
mp_obj_t span[2];
|
|
|
|
match_span_helper(n_args, args, span);
|
|
|
|
return span[0];
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(match_start_obj, 1, 2, match_start);
|
|
|
|
|
|
|
|
STATIC mp_obj_t match_end(size_t n_args, const mp_obj_t *args) {
|
|
|
|
mp_obj_t span[2];
|
|
|
|
match_span_helper(n_args, args, span);
|
|
|
|
return span[1];
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(match_end_obj, 1, 2, match_end);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-11-04 19:38:01 -05:00
|
|
|
#if !MICROPY_ENABLE_DYNRUNTIME
|
2015-11-27 08:38:15 -05:00
|
|
|
STATIC const mp_rom_map_elem_t match_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_group), MP_ROM_PTR(&match_group_obj) },
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_RE_MATCH_GROUPS
|
2018-05-23 23:07:42 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_groups), MP_ROM_PTR(&match_groups_obj) },
|
|
|
|
#endif
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_RE_MATCH_SPAN_START_END
|
2018-05-23 23:08:15 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_span), MP_ROM_PTR(&match_span_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_start), MP_ROM_PTR(&match_start_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_end), MP_ROM_PTR(&match_end_obj) },
|
|
|
|
#endif
|
2014-09-12 11:48:07 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(match_locals_dict, match_locals_dict_table);
|
|
|
|
|
2021-07-14 00:38:38 -04:00
|
|
|
STATIC MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
match_type,
|
|
|
|
MP_QSTR_match,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
|
|
|
print, match_print,
|
2022-06-24 02:27:46 -04:00
|
|
|
locals_dict, &match_locals_dict
|
2021-07-14 00:38:38 -04:00
|
|
|
);
|
2019-11-04 19:38:01 -05:00
|
|
|
#endif
|
2014-09-12 11:48:07 -04:00
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
STATIC void re_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)kind;
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_re_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_printf(print, "<re %p>", self);
|
2014-09-12 11:48:07 -04:00
|
|
|
}
|
|
|
|
|
2022-08-18 00:49:57 -04:00
|
|
|
STATIC mp_obj_t re_exec(bool is_anchored, uint n_args, const mp_obj_t *args) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)n_args;
|
2020-06-03 04:18:49 -04:00
|
|
|
mp_obj_re_t *self;
|
2022-04-21 23:05:56 -04:00
|
|
|
if (mp_obj_is_type(args[0], (mp_obj_type_t *)&re_type)) {
|
2020-06-03 04:18:49 -04:00
|
|
|
self = MP_OBJ_TO_PTR(args[0]);
|
|
|
|
} else {
|
|
|
|
self = MP_OBJ_TO_PTR(mod_re_compile(1, args));
|
|
|
|
}
|
2014-09-12 11:48:07 -04:00
|
|
|
Subject subj;
|
2017-03-25 04:48:44 -04:00
|
|
|
size_t len;
|
2022-03-09 19:51:42 -05:00
|
|
|
subj.begin_line = subj.begin = mp_obj_str_get_data(args[1], &len);
|
2014-09-12 11:48:07 -04:00
|
|
|
subj.end = subj.begin + len;
|
|
|
|
int caps_num = (self->re.sub + 1) * 2;
|
|
|
|
mp_obj_match_t *match = m_new_obj_var(mp_obj_match_t, char *, caps_num);
|
2015-03-16 04:27:46 -04:00
|
|
|
// 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
|
|
|
|
memset((char *)match->caps, 0, caps_num * sizeof(char *));
|
2014-09-12 11:48:07 -04:00
|
|
|
int res = re1_5_recursiveloopprog(&self->re, &subj, match->caps, caps_num, is_anchored);
|
|
|
|
if (res == 0) {
|
|
|
|
m_del_var(mp_obj_match_t, char *, caps_num, match);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2022-04-21 23:05:56 -04:00
|
|
|
match->base.type = (mp_obj_type_t *)&match_type;
|
2015-02-18 09:47:14 -05:00
|
|
|
match->num_matches = caps_num / 2; // caps_num counts start and end pointers
|
2014-09-12 11:48:07 -04:00
|
|
|
match->str = args[1];
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(match);
|
2014-09-12 11:48:07 -04:00
|
|
|
}
|
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t re_match(size_t n_args, const mp_obj_t *args) {
|
2022-08-18 00:49:57 -04:00
|
|
|
return re_exec(true, n_args, args);
|
2014-09-12 11:48:07 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(re_match_obj, 2, 4, re_match);
|
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t re_search(size_t n_args, const mp_obj_t *args) {
|
2022-08-18 00:49:57 -04:00
|
|
|
return re_exec(false, n_args, args);
|
2014-09-12 11:48:07 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(re_search_obj, 2, 4, re_search);
|
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t re_split(size_t n_args, const mp_obj_t *args) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_re_t *self = MP_OBJ_TO_PTR(args[0]);
|
2014-09-12 11:48:07 -04:00
|
|
|
Subject subj;
|
2017-03-25 04:48:44 -04:00
|
|
|
size_t len;
|
2017-06-30 18:25:23 -04:00
|
|
|
const mp_obj_type_t *str_type = mp_obj_get_type(args[1]);
|
2022-03-09 19:51:42 -05:00
|
|
|
subj.begin_line = subj.begin = mp_obj_str_get_data(args[1], &len);
|
2014-09-12 11:48:07 -04:00
|
|
|
subj.end = subj.begin + len;
|
|
|
|
int caps_num = (self->re.sub + 1) * 2;
|
|
|
|
|
|
|
|
int maxsplit = 0;
|
|
|
|
if (n_args > 2) {
|
2015-05-12 18:05:53 -04:00
|
|
|
maxsplit = mp_obj_get_int(args[2]);
|
2014-09-12 11:48:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t retval = mp_obj_new_list(0, NULL);
|
2017-11-26 07:39:25 -05:00
|
|
|
const char **caps = mp_local_alloc(caps_num * sizeof(char *));
|
2014-09-12 11:48:07 -04:00
|
|
|
while (true) {
|
2015-03-16 04:27:46 -04:00
|
|
|
// 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
|
|
|
|
memset((char **)caps, 0, caps_num * sizeof(char *));
|
2014-09-12 11:48:07 -04:00
|
|
|
int res = re1_5_recursiveloopprog(&self->re, &subj, caps, caps_num, false);
|
|
|
|
|
|
|
|
// if we didn't have a match, or had an empty match, it's time to stop
|
|
|
|
if (!res || caps[0] == caps[1]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-06-30 18:25:23 -04:00
|
|
|
mp_obj_t s = mp_obj_new_str_of_type(str_type, (const byte *)subj.begin, caps[0] - subj.begin);
|
2014-09-12 11:48:07 -04:00
|
|
|
mp_obj_list_append(retval, s);
|
|
|
|
if (self->re.sub > 0) {
|
2020-04-09 03:22:25 -04:00
|
|
|
mp_raise_NotImplementedError(MP_ERROR_TEXT("splitting with sub-captures"));
|
2014-09-12 11:48:07 -04:00
|
|
|
}
|
|
|
|
subj.begin = caps[1];
|
|
|
|
if (maxsplit > 0 && --maxsplit == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-12-13 06:22:57 -05:00
|
|
|
// cast is a workaround for a bug in msvc (see above)
|
|
|
|
mp_local_free((char **)caps);
|
2014-09-12 11:48:07 -04:00
|
|
|
|
2017-06-30 18:25:23 -04:00
|
|
|
mp_obj_t s = mp_obj_new_str_of_type(str_type, (const byte *)subj.begin, subj.end - subj.begin);
|
2014-09-12 11:48:07 -04:00
|
|
|
mp_obj_list_append(retval, s);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(re_split_obj, 2, 3, re_split);
|
|
|
|
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_RE_SUB
|
2018-05-23 23:08:51 -04:00
|
|
|
|
2020-06-03 04:18:49 -04:00
|
|
|
STATIC mp_obj_t re_sub_helper(size_t n_args, const mp_obj_t *args) {
|
|
|
|
mp_obj_re_t *self;
|
2022-04-21 23:05:56 -04:00
|
|
|
if (mp_obj_is_type(args[0], (mp_obj_type_t *)&re_type)) {
|
2020-06-03 04:18:49 -04:00
|
|
|
self = MP_OBJ_TO_PTR(args[0]);
|
|
|
|
} else {
|
|
|
|
self = MP_OBJ_TO_PTR(mod_re_compile(1, args));
|
|
|
|
}
|
2018-05-23 23:08:51 -04:00
|
|
|
mp_obj_t replace = args[1];
|
|
|
|
mp_obj_t where = args[2];
|
|
|
|
mp_int_t count = 0;
|
|
|
|
if (n_args > 3) {
|
|
|
|
count = mp_obj_get_int(args[3]);
|
|
|
|
// Note: flags are currently ignored
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t where_len;
|
|
|
|
const char *where_str = mp_obj_str_get_data(where, &where_len);
|
|
|
|
Subject subj;
|
2022-03-09 19:51:42 -05:00
|
|
|
subj.begin_line = subj.begin = where_str;
|
2018-05-23 23:08:51 -04:00
|
|
|
subj.end = subj.begin + where_len;
|
|
|
|
int caps_num = (self->re.sub + 1) * 2;
|
|
|
|
|
|
|
|
vstr_t vstr_return;
|
|
|
|
vstr_return.buf = NULL; // We'll init the vstr after the first match
|
|
|
|
mp_obj_match_t *match = mp_local_alloc(sizeof(mp_obj_match_t) + caps_num * sizeof(char *));
|
2022-04-21 23:05:56 -04:00
|
|
|
match->base.type = (mp_obj_type_t *)&match_type;
|
2018-05-23 23:08:51 -04:00
|
|
|
match->num_matches = caps_num / 2; // caps_num counts start and end pointers
|
|
|
|
match->str = where;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
// 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
|
|
|
|
memset((char *)match->caps, 0, caps_num * sizeof(char *));
|
|
|
|
int res = re1_5_recursiveloopprog(&self->re, &subj, match->caps, caps_num, false);
|
|
|
|
|
|
|
|
// If we didn't have a match, or had an empty match, it's time to stop
|
|
|
|
if (!res || match->caps[0] == match->caps[1]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialise the vstr if it's not already
|
|
|
|
if (vstr_return.buf == NULL) {
|
|
|
|
vstr_init(&vstr_return, match->caps[0] - subj.begin);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add pre-match string
|
|
|
|
vstr_add_strn(&vstr_return, subj.begin, match->caps[0] - subj.begin);
|
|
|
|
|
|
|
|
// Get replacement string
|
|
|
|
const char *repl = mp_obj_str_get_str((mp_obj_is_callable(replace) ? mp_call_function_1(replace, MP_OBJ_FROM_PTR(match)) : replace));
|
|
|
|
|
|
|
|
// Append replacement string to result, substituting any regex groups
|
|
|
|
while (*repl != '\0') {
|
|
|
|
if (*repl == '\\') {
|
|
|
|
++repl;
|
|
|
|
bool is_g_format = false;
|
|
|
|
if (*repl == 'g' && repl[1] == '<') {
|
|
|
|
// Group specified with syntax "\g<number>"
|
|
|
|
repl += 2;
|
|
|
|
is_g_format = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('0' <= *repl && *repl <= '9') {
|
|
|
|
// Group specified with syntax "\g<number>" or "\number"
|
|
|
|
unsigned int match_no = 0;
|
|
|
|
do {
|
|
|
|
match_no = match_no * 10 + (*repl++ - '0');
|
|
|
|
} while ('0' <= *repl && *repl <= '9');
|
|
|
|
if (is_g_format && *repl == '>') {
|
|
|
|
++repl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match_no >= (unsigned int)match->num_matches) {
|
2021-07-14 08:56:52 -04:00
|
|
|
mp_raise_type_arg(&mp_type_IndexError, MP_OBJ_NEW_SMALL_INT(match_no));
|
2018-05-23 23:08:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *start_match = match->caps[match_no * 2];
|
|
|
|
if (start_match != NULL) {
|
|
|
|
// Add the substring matched by group
|
|
|
|
const char *end_match = match->caps[match_no * 2 + 1];
|
|
|
|
vstr_add_strn(&vstr_return, start_match, end_match - start_match);
|
|
|
|
}
|
2020-09-24 22:27:02 -04:00
|
|
|
} else if (*repl == '\\') {
|
|
|
|
// Add the \ character
|
|
|
|
vstr_add_byte(&vstr_return, *repl++);
|
2018-05-23 23:08:51 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Just add the current byte from the replacement string
|
|
|
|
vstr_add_byte(&vstr_return, *repl++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move start pointer to end of last match
|
|
|
|
subj.begin = match->caps[1];
|
|
|
|
|
|
|
|
// Stop substitutions if count was given and gets to 0
|
|
|
|
if (count > 0 && --count == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_local_free(match);
|
|
|
|
|
|
|
|
if (vstr_return.buf == NULL) {
|
|
|
|
// Optimisation for case of no substitutions
|
|
|
|
return where;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add post-match string
|
|
|
|
vstr_add_strn(&vstr_return, subj.begin, subj.end - subj.begin);
|
|
|
|
|
2022-08-22 03:08:05 -04:00
|
|
|
if (mp_obj_get_type(where) == &mp_type_str) {
|
2022-08-23 04:35:37 -04:00
|
|
|
return mp_obj_new_str_from_utf8_vstr(&vstr_return);
|
2022-08-22 03:08:05 -04:00
|
|
|
} else {
|
|
|
|
return mp_obj_new_bytes_from_vstr(&vstr_return);
|
|
|
|
}
|
2018-05-23 23:08:51 -04:00
|
|
|
}
|
|
|
|
|
2020-06-03 04:18:49 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(re_sub_obj, 3, 5, re_sub_helper);
|
2018-05-23 23:08:51 -04:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-11-04 19:38:01 -05:00
|
|
|
#if !MICROPY_ENABLE_DYNRUNTIME
|
2015-11-27 08:38:15 -05:00
|
|
|
STATIC const mp_rom_map_elem_t re_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_match), MP_ROM_PTR(&re_match_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_search), MP_ROM_PTR(&re_search_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_split), MP_ROM_PTR(&re_split_obj) },
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_RE_SUB
|
2018-05-23 23:08:51 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_sub), MP_ROM_PTR(&re_sub_obj) },
|
|
|
|
#endif
|
2014-09-12 11:48:07 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(re_locals_dict, re_locals_dict_table);
|
|
|
|
|
2021-07-14 00:38:38 -04:00
|
|
|
STATIC MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
re_type,
|
2022-08-17 02:18:31 -04:00
|
|
|
MP_QSTR_re,
|
2021-07-14 00:38:38 -04:00
|
|
|
MP_TYPE_FLAG_NONE,
|
|
|
|
print, re_print,
|
2022-06-24 02:27:46 -04:00
|
|
|
locals_dict, &re_locals_dict
|
2021-07-14 00:38:38 -04:00
|
|
|
);
|
2019-11-04 19:38:01 -05:00
|
|
|
#endif
|
2014-09-12 11:48:07 -04:00
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t mod_re_compile(size_t n_args, const mp_obj_t *args) {
|
2019-08-17 09:50:19 -04:00
|
|
|
(void)n_args;
|
2014-09-12 11:48:07 -04:00
|
|
|
const char *re_str = mp_obj_str_get_str(args[0]);
|
|
|
|
int size = re1_5_sizecode(re_str);
|
2015-10-31 17:38:12 -04:00
|
|
|
if (size == -1) {
|
|
|
|
goto error;
|
|
|
|
}
|
2022-04-21 23:05:56 -04:00
|
|
|
mp_obj_re_t *o = mp_obj_malloc_var(mp_obj_re_t, char, size, (mp_obj_type_t *)&re_type);
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_RE_DEBUG
|
2014-09-12 11:48:07 -04:00
|
|
|
int flags = 0;
|
|
|
|
if (n_args > 1) {
|
|
|
|
flags = mp_obj_get_int(args[1]);
|
|
|
|
}
|
2019-08-17 09:50:19 -04:00
|
|
|
#endif
|
2014-09-12 11:48:07 -04:00
|
|
|
int error = re1_5_compilecode(&o->re, re_str);
|
|
|
|
if (error != 0) {
|
2015-10-31 17:38:12 -04:00
|
|
|
error:
|
2020-04-09 03:22:25 -04:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("error in regex"));
|
2014-09-12 11:48:07 -04:00
|
|
|
}
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_RE_DEBUG
|
2014-09-12 11:48:07 -04:00
|
|
|
if (flags & FLAG_DEBUG) {
|
|
|
|
re1_5_dumpcode(&o->re);
|
|
|
|
}
|
2019-08-17 09:50:19 -04:00
|
|
|
#endif
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2014-09-12 11:48:07 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_re_compile_obj, 1, 2, mod_re_compile);
|
|
|
|
|
2019-11-04 19:38:01 -05:00
|
|
|
#if !MICROPY_ENABLE_DYNRUNTIME
|
2015-11-27 08:38:15 -05:00
|
|
|
STATIC const mp_rom_map_elem_t mp_module_re_globals_table[] = {
|
2022-08-17 02:18:31 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_re) },
|
2015-11-27 08:38:15 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_compile), MP_ROM_PTR(&mod_re_compile_obj) },
|
2020-06-03 04:18:49 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_match), MP_ROM_PTR(&re_match_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_search), MP_ROM_PTR(&re_search_obj) },
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_RE_SUB
|
2020-06-03 04:18:49 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_sub), MP_ROM_PTR(&re_sub_obj) },
|
2018-05-23 23:08:51 -04:00
|
|
|
#endif
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_RE_DEBUG
|
2015-11-27 08:38:15 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_DEBUG), MP_ROM_INT(FLAG_DEBUG) },
|
2019-08-17 09:50:19 -04:00
|
|
|
#endif
|
2014-09-12 11:48:07 -04:00
|
|
|
};
|
|
|
|
|
2014-11-29 09:39:27 -05:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_re_globals, mp_module_re_globals_table);
|
2014-09-12 11:48:07 -04:00
|
|
|
|
2022-08-18 00:47:56 -04:00
|
|
|
const mp_obj_module_t mp_module_re = {
|
2014-09-12 11:48:07 -04:00
|
|
|
.base = { &mp_type_module },
|
|
|
|
.globals = (mp_obj_dict_t *)&mp_module_re_globals,
|
|
|
|
};
|
2022-04-20 02:06:22 -04:00
|
|
|
|
2023-06-01 22:33:25 -04:00
|
|
|
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_re, mp_module_re);
|
2019-11-04 19:38:01 -05:00
|
|
|
#endif
|
2014-09-12 11:48:07 -04:00
|
|
|
|
2014-10-11 20:12:58 -04:00
|
|
|
// Source files #include'd here to make sure they're compiled in
|
|
|
|
// only if module is enabled by config setting.
|
|
|
|
|
|
|
|
#define re1_5_fatal(x) assert(!x)
|
2021-12-08 20:52:27 -05:00
|
|
|
|
2021-07-12 02:34:27 -04:00
|
|
|
#include "lib/re1.5/compilecode.c"
|
2021-12-08 20:52:27 -05:00
|
|
|
#include "lib/re1.5/recursiveloop.c"
|
|
|
|
#include "lib/re1.5/charclass.c"
|
|
|
|
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_RE_DEBUG
|
2021-12-08 20:52:27 -05:00
|
|
|
// Make sure the output print statements go to the same output as other Python output.
|
|
|
|
#define printf(...) mp_printf(&mp_plat_print, __VA_ARGS__)
|
2021-07-12 02:34:27 -04:00
|
|
|
#include "lib/re1.5/dumpcode.c"
|
2021-12-08 20:52:27 -05:00
|
|
|
#undef printf
|
2019-08-17 09:50:19 -04:00
|
|
|
#endif
|
2014-10-11 20:12:58 -04:00
|
|
|
|
2022-08-18 01:01:26 -04:00
|
|
|
#endif // MICROPY_PY_RE
|