From 93c0cfbd370d40ec08cc14062b38ba88c28951f0 Mon Sep 17 00:00:00 2001 From: jf-home Date: Tue, 29 Apr 2025 22:55:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0list=E7=9A=84slice=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/list.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/list.c b/src/list.c index 5f211e2..db759ad 100644 --- a/src/list.c +++ b/src/list.c @@ -191,6 +191,12 @@ static void list_print(struct _list* self) struct _list* list_slice(struct _list *self, int start, int end, int step) { + assert(self != NULL); + if(step == 0) + { + return NULL; + } + if(start < 0) { start += self->size(self); @@ -201,6 +207,44 @@ struct _list* list_slice(struct _list *self, int start, int end, int step) end += self->size(self); } + list_t list = list_new2(self->_obj_size, end - start); + if(list == NULL) + { + return NULL; + } + + if(step > 0) + { + if(start >= end) + { + return NULL; + } + } + else + { + uint32_t temp = 0; + if(start <= end) + { + return NULL; + } + // start ^= end; + // end ^= start; + // start ^= end; + temp = start; + start = end; + end = temp; + step = -step; + } + + for(int i = start; i < end; i += step) + { + if(i >= self->size(self)) + { + break; + } + list->append(list, (char*)self->obj + i * self->_obj_size); + } + return NULL; }