vim-patch:8.2.1463: Vim9: list slice not supported yet

Problem:    Vim9: list slice not supported yet.
Solution:   Add support for list slicing.

ed5918771f

Co-authored-by: Bram Moolenaar <Bram@vim.org>
This commit is contained in:
zeertzjq 2023-05-04 14:03:32 +08:00
parent 1659cd15be
commit f4043e290e
2 changed files with 53 additions and 31 deletions

View File

@ -3470,7 +3470,7 @@ static int eval_index(char **arg, typval_T *rettv, evalarg_T *const evalarg, boo
bool empty1 = false; bool empty1 = false;
bool empty2 = false; bool empty2 = false;
ptrdiff_t len = -1; ptrdiff_t len = -1;
int range = false; bool range = false;
const char *key = NULL; const char *key = NULL;
switch (rettv->v_type) { switch (rettv->v_type) {
@ -3667,37 +3667,15 @@ static int eval_index(char **arg, typval_T *rettv, evalarg_T *const evalarg, boo
} }
break; break;
case VAR_LIST: case VAR_LIST:
len = tv_list_len(rettv->vval.v_list); if (empty1) {
if (n1 < 0) { n1 = 0;
n1 = (int)len + n1;
} }
if (!empty1 && (n1 < 0 || n1 >= len)) { if (empty2) {
// For a range we allow invalid values and return an empty
// list. A list index out of range is an error.
if (!range) {
if (verbose) {
semsg(_(e_listidx), (int64_t)n1);
}
return FAIL;
}
n1 = (int)len;
}
if (range) {
if (n2 < 0) {
n2 = (int)len + n2;
} else if (n2 >= len) {
n2 = (int)len - 1;
}
if (!empty2 && (n2 < 0 || n2 + 1 < n1)) {
n2 = -1; n2 = -1;
} }
list_T *l = tv_list_slice(rettv->vval.v_list, n1, n2); if (tv_list_slice_or_index(rettv->vval.v_list,
tv_clear(rettv); range, n1, n2, rettv, verbose) == FAIL) {
tv_list_set_ret(rettv, l); return FAIL;
} else {
tv_copy(TV_LIST_ITEM_TV(tv_list_find(rettv->vval.v_list, (int)n1)), &var1);
tv_clear(rettv);
*rettv = var1;
} }
break; break;
case VAR_DICT: { case VAR_DICT: {

View File

@ -765,7 +765,7 @@ int tv_list_concat(list_T *const l1, list_T *const l2, typval_T *const tv)
return OK; return OK;
} }
list_T *tv_list_slice(list_T *ol, int n1, int n2) static list_T *tv_list_slice(list_T *ol, int n1, int n2)
{ {
list_T *l = tv_list_alloc(n2 - n1 + 1); list_T *l = tv_list_alloc(n2 - n1 + 1);
listitem_T *item = tv_list_find(ol, n1); listitem_T *item = tv_list_find(ol, n1);
@ -776,6 +776,50 @@ list_T *tv_list_slice(list_T *ol, int n1, int n2)
return l; return l;
} }
int tv_list_slice_or_index(list_T *list, bool range, int n1_arg, int n2_arg, typval_T *rettv,
bool verbose)
{
int len = tv_list_len(rettv->vval.v_list);
int n1 = n1_arg;
int n2 = n2_arg;
if (n1 < 0) {
n1 = len + n1;
}
if (n1 < 0 || n1 >= len) {
// For a range we allow invalid values and return an empty
// list. A list index out of range is an error.
if (!range) {
if (verbose) {
semsg(_(e_listidx), (int64_t)n1);
}
return FAIL;
}
n1 = len;
}
if (range) {
if (n2 < 0) {
n2 = len + n2;
} else if (n2 >= len) {
n2 = len - 1;
}
if (n2 < 0 || n2 + 1 < n1) {
n2 = -1;
}
list_T *l = tv_list_slice(rettv->vval.v_list, n1, n2);
tv_clear(rettv);
tv_list_set_ret(rettv, l);
} else {
// copy the item to "var1" to avoid that freeing the list makes it
// invalid.
typval_T var1;
tv_copy(TV_LIST_ITEM_TV(tv_list_find(rettv->vval.v_list, (int)n1)), &var1);
tv_clear(rettv);
*rettv = var1;
}
return OK;
}
typedef struct { typedef struct {
char *s; char *s;
char *tofree; char *tofree;