aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2023-05-04 15:09:50 +0800
committerGitHub <noreply@github.com>2023-05-04 15:09:50 +0800
commitb9e34571f95b1480607c50d5303b4c63c3370374 (patch)
tree015dbc091376d4eda280d2a5fc3c6d2fcb0ef4ac
parent24d4a825693db5f8454a389259a76f71f8697832 (diff)
parentf4043e290e10007e51596a489a5ef444e28e5de8 (diff)
downloadrneovim-b9e34571f95b1480607c50d5303b4c63c3370374.tar.gz
rneovim-b9e34571f95b1480607c50d5303b4c63c3370374.tar.bz2
rneovim-b9e34571f95b1480607c50d5303b4c63c3370374.zip
Merge pull request #23467 from zeertzjq/vim-8.2.0987
vim-patch:8.2.{0987,1463}
-rw-r--r--src/nvim/eval.c48
-rw-r--r--src/nvim/eval/typval.c55
2 files changed, 64 insertions, 39 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index 1dfa853fd3..e72287031d 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -3470,7 +3470,7 @@ static int eval_index(char **arg, typval_T *rettv, evalarg_T *const evalarg, boo
bool empty1 = false;
bool empty2 = false;
ptrdiff_t len = -1;
- int range = false;
+ bool range = false;
const char *key = NULL;
switch (rettv->v_type) {
@@ -3667,45 +3667,15 @@ static int eval_index(char **arg, typval_T *rettv, evalarg_T *const evalarg, boo
}
break;
case VAR_LIST:
- len = tv_list_len(rettv->vval.v_list);
- if (n1 < 0) {
- n1 = (int)len + n1;
- }
- if (!empty1 && (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 = (int)len;
+ if (empty1) {
+ n1 = 0;
}
- if (range) {
- list_T *l;
- listitem_T *item;
-
- if (n2 < 0) {
- n2 = (int)len + n2;
- } else if (n2 >= len) {
- n2 = (int)len - 1;
- }
- if (!empty2 && (n2 < 0 || n2 + 1 < n1)) {
- n2 = -1;
- }
- l = tv_list_alloc(n2 - n1 + 1);
- item = tv_list_find(rettv->vval.v_list, n1);
- while (n1++ <= n2) {
- tv_list_append_tv(l, TV_LIST_ITEM_TV(item));
- item = TV_LIST_ITEM_NEXT(rettv->vval.v_list, item);
- }
- tv_clear(rettv);
- tv_list_set_ret(rettv, l);
- } else {
- tv_copy(TV_LIST_ITEM_TV(tv_list_find(rettv->vval.v_list, (int)n1)), &var1);
- tv_clear(rettv);
- *rettv = var1;
+ if (empty2) {
+ n2 = -1;
+ }
+ if (tv_list_slice_or_index(rettv->vval.v_list,
+ range, n1, n2, rettv, verbose) == FAIL) {
+ return FAIL;
}
break;
case VAR_DICT: {
diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c
index cf355a22fa..ba1d60959a 100644
--- a/src/nvim/eval/typval.c
+++ b/src/nvim/eval/typval.c
@@ -765,6 +765,61 @@ int tv_list_concat(list_T *const l1, list_T *const l2, typval_T *const tv)
return OK;
}
+static list_T *tv_list_slice(list_T *ol, int n1, int n2)
+{
+ list_T *l = tv_list_alloc(n2 - n1 + 1);
+ listitem_T *item = tv_list_find(ol, n1);
+ for (; n1 <= n2; n1++) {
+ tv_list_append_tv(l, TV_LIST_ITEM_TV(item));
+ item = TV_LIST_ITEM_NEXT(rettv->vval.v_list, item);
+ }
+ 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 {
char *s;
char *tofree;