From 24473b79838412e7be0c65e6dfae45146cf15a6b Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Sat, 27 Feb 2021 21:13:55 +0100 Subject: Separate out mp_obj_list_insert for use in display.Group Note that for some reason this makes the binary 500 bytes larger! --- py/objlist.c | 20 +++++++++++--------- py/objlist.h | 1 + 2 files changed, 12 insertions(+), 9 deletions(-) (limited to 'py') diff --git a/py/objlist.c b/py/objlist.c index 309e0e542..7aa4ee89c 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -270,7 +270,7 @@ STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in) { return mp_const_none; // return None, as per CPython } -mp_obj_t mp_obj_list_pop(mp_obj_list_t *self, size_t index) { +inline mp_obj_t mp_obj_list_pop(mp_obj_list_t *self, size_t index) { if (self->len == 0) { mp_raise_IndexError_varg(translate("pop from empty %q"), MP_QSTR_list); } @@ -375,6 +375,15 @@ STATIC mp_obj_t list_index(size_t n_args, const mp_obj_t *args) { return mp_seq_index_obj(self->items, self->len, n_args, args); } +inline void mp_obj_list_insert(mp_obj_list_t *self, size_t index, mp_obj_t obj) { + mp_obj_list_append(MP_OBJ_FROM_PTR(self), mp_const_none); + + for (size_t i = self->len - 1; i > index; --i) { + self->items[i] = self->items[i - 1]; + } + self->items[index] = obj; +} + STATIC mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) { mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list)); mp_obj_list_t *self = mp_instance_cast_to_native_base(self_in, &mp_type_list); @@ -389,14 +398,7 @@ STATIC mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) { if ((size_t)index > self->len) { index = self->len; } - - mp_obj_list_append(self_in, mp_const_none); - - for (mp_int_t i = self->len-1; i > index; i--) { - self->items[i] = self->items[i-1]; - } - self->items[index] = obj; - + mp_obj_list_insert(self, index, obj); return mp_const_none; } diff --git a/py/objlist.h b/py/objlist.h index cec23e06c..eb005e81c 100644 --- a/py/objlist.h +++ b/py/objlist.h @@ -37,5 +37,6 @@ typedef struct _mp_obj_list_t { void mp_obj_list_init(mp_obj_list_t *o, size_t n); mp_obj_t mp_obj_list_pop(mp_obj_list_t *self, size_t index); +void mp_obj_list_insert(mp_obj_list_t *self, size_t index, mp_obj_t obj); #endif // MICROPY_INCLUDED_PY_OBJLIST_H -- cgit v1.2.3