diff options
author | Bram Moolenaar <Bram@vim.org> | 2022-08-30 14:34:52 +0100 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2022-08-30 14:34:52 +0100 |
commit | 976f859763b215050a03248dbc2bb62fa5d0d059 (patch) | |
tree | 36bc5a92c8eb8cf57c0116e21c9a36679993fba4 /src/list.c | |
parent | 0e412be00f8290e0575c7f72ec080725631eff38 (diff) | |
download | vim-git-976f859763b215050a03248dbc2bb62fa5d0d059.tar.gz |
patch 9.0.0327: items() does not work on a listv9.0.0327
Problem: items() does not work on a list. (Sergey Vlasov)
Solution: Make items() work on a list. (closes #11013)
Diffstat (limited to 'src/list.c')
-rw-r--r-- | src/list.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/list.c b/src/list.c index 10a7aacd3..056210d24 100644 --- a/src/list.c +++ b/src/list.c @@ -1053,6 +1053,38 @@ f_flattennew(typval_T *argvars, typval_T *rettv) } /* + * "items(list)" function + * Caller must have already checked that argvars[0] is a List. + */ + void +list2items(typval_T *argvars, typval_T *rettv) +{ + list_T *l = argvars[0].vval.v_list; + listitem_T *li; + varnumber_T idx; + + if (rettv_list_alloc(rettv) == FAIL) + return; + + if (l == NULL) + return; // empty list behaves like an empty list + + // TODO: would be more efficient to not materialize the argument + CHECK_LIST_MATERIALIZE(l); + for (idx = 0, li = l->lv_first; li != NULL; li = li->li_next, ++idx) + { + list_T *l2 = list_alloc(); + + if (l2 == NULL) + break; + if (list_append_list(rettv->vval.v_list, l2) == FAIL + || list_append_number(l2, idx) == FAIL + || list_append_tv(l2, &li->li_tv) == FAIL) + break; + } +} + +/* * Extend "l1" with "l2". "l1" must not be NULL. * If "bef" is NULL append at the end, otherwise insert before this item. * Returns FAIL when out of memory. |