]> granicus.if.org Git - vim/commitdiff
patch 9.0.0327: items() does not work on a list v9.0.0327
authorBram Moolenaar <Bram@vim.org>
Tue, 30 Aug 2022 13:34:52 +0000 (14:34 +0100)
committerBram Moolenaar <Bram@vim.org>
Tue, 30 Aug 2022 13:34:52 +0000 (14:34 +0100)
Problem:    items() does not work on a list. (Sergey Vlasov)
Solution:   Make items() work on a list. (closes #11013)

src/dict.c
src/evalfunc.c
src/list.c
src/proto/list.pro
src/testdir/test_listdict.vim
src/testdir/test_vim9_builtin.vim
src/version.c

index 29608bd88ca5d62fa0dceb36b60a49feab1f55be..6bac4d5416aa683d8b5547cc9405d414dad026a6 100644 (file)
@@ -1440,14 +1440,17 @@ dict_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
     dictitem_remove(d, di);
 }
 
+typedef enum {
+    DICT2LIST_KEYS,
+    DICT2LIST_VALUES,
+    DICT2LIST_ITEMS,
+} dict2list_T;
+
 /*
- * Turn a dict into a list:
- * "what" == 0: list of keys
- * "what" == 1: list of values
- * "what" == 2: list of items
+ * Turn a dict into a list.
  */
     static void
-dict_list(typval_T *argvars, typval_T *rettv, int what)
+dict2list(typval_T *argvars, typval_T *rettv, dict2list_T what)
 {
     list_T     *l2;
     dictitem_T *di;
@@ -1460,16 +1463,13 @@ dict_list(typval_T *argvars, typval_T *rettv, int what)
     if (rettv_list_alloc(rettv) == FAIL)
        return;
 
-    if (in_vim9script() && check_for_dict_arg(argvars, 0) == FAIL)
-       return;
-
-    if (argvars[0].v_type != VAR_DICT)
-    {
-       emsg(_(e_dictionary_required));
+    if ((what == DICT2LIST_ITEMS
+               ? check_for_list_or_dict_arg(argvars, 0)
+               : check_for_dict_arg(argvars, 0)) == FAIL)
        return;
-    }
 
-    if ((d = argvars[0].vval.v_dict) == NULL)
+    d = argvars[0].vval.v_dict;
+    if (d == NULL)
        // empty dict behaves like an empty dict
        return;
 
@@ -1486,14 +1486,14 @@ dict_list(typval_T *argvars, typval_T *rettv, int what)
                break;
            list_append(rettv->vval.v_list, li);
 
-           if (what == 0)
+           if (what == DICT2LIST_KEYS)
            {
                // keys()
                li->li_tv.v_type = VAR_STRING;
                li->li_tv.v_lock = 0;
                li->li_tv.vval.v_string = vim_strsave(di->di_key);
            }
-           else if (what == 1)
+           else if (what == DICT2LIST_VALUES)
            {
                // values()
                copy_tv(&di->di_tv, &li->li_tv);
@@ -1533,7 +1533,10 @@ dict_list(typval_T *argvars, typval_T *rettv, int what)
     void
 f_items(typval_T *argvars, typval_T *rettv)
 {
-    dict_list(argvars, rettv, 2);
+    if (argvars[0].v_type == VAR_LIST)
+       list2items(argvars, rettv);
+    else
+       dict2list(argvars, rettv, DICT2LIST_ITEMS);
 }
 
 /*
@@ -1542,7 +1545,7 @@ f_items(typval_T *argvars, typval_T *rettv)
     void
 f_keys(typval_T *argvars, typval_T *rettv)
 {
-    dict_list(argvars, rettv, 0);
+    dict2list(argvars, rettv, DICT2LIST_KEYS);
 }
 
 /*
@@ -1551,7 +1554,7 @@ f_keys(typval_T *argvars, typval_T *rettv)
     void
 f_values(typval_T *argvars, typval_T *rettv)
 {
-    dict_list(argvars, rettv, 1);
+    dict2list(argvars, rettv, DICT2LIST_VALUES);
 }
 
 /*
index 1b629ff8435718a6cf538daa86903416702cf31d..c43db65b95874b4feb437912ce9af8b19e8feea9 100644 (file)
@@ -2029,7 +2029,7 @@ static funcentry_T global_functions[] =
                        ret_number_bool,    f_islocked},
     {"isnan",          1, 1, FEARG_1,      arg1_float_or_nr,
                        ret_number_bool,    MATH_FUNC(f_isnan)},
-    {"items",          1, 1, FEARG_1,      arg1_dict_any,
+    {"items",          1, 1, FEARG_1,      arg1_list_or_dict,
                        ret_list_items,     f_items},
     {"job_getchannel", 1, 1, FEARG_1,      arg1_job,
                        ret_channel,        JOB_FUNC(f_job_getchannel)},
index 10a7aacd355f7d64af22ff1cc17849fbdf4d0fd7..056210d243194944180d5e2f7eeae788ee3240ce 100644 (file)
@@ -1052,6 +1052,38 @@ f_flattennew(typval_T *argvars, typval_T *rettv)
     flatten_common(argvars, rettv, TRUE);
 }
 
+/*
+ * "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.
index 3f77f04d15d3832b54ddf8aed5b0ba033052da7a..611aeac40fc871ed1b49fdaec29dcf063d3d99aa 100644 (file)
@@ -35,6 +35,7 @@ int check_range_index_two(list_T *l, long *n1, listitem_T *li1, long *n2, int qu
 int list_assign_range(list_T *dest, list_T *src, long idx1_arg, long idx2, int empty_idx2, char_u *op, char_u *varname);
 void f_flatten(typval_T *argvars, typval_T *rettv);
 void f_flattennew(typval_T *argvars, typval_T *rettv);
+void list2items(typval_T *argvars, typval_T *rettv);
 int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
 int list_concat(list_T *l1, list_T *l2, typval_T *tv);
 list_T *list_slice(list_T *ol, long n1, long n2);
index bba4e3e1e1744d1c37d7b37e0574e4a456c10984..c87d701a362bde573aa37c4c35f5cb18e3d45a3f 100644 (file)
@@ -198,6 +198,17 @@ func Test_list_range_assign()
   call v9.CheckDefAndScriptFailure(lines, 'E1012:', 2)
 endfunc
 
+func Test_list_items()
+  let r = []
+  let l = ['a', 'b', 'c']
+  for [idx, val] in items(l)
+    call extend(r, [[idx, val]])
+  endfor
+  call assert_equal([[0, 'a'], [1, 'b'], [2, 'c']], r)
+
+  call assert_fails('call items(3)', 'E1227:')
+endfunc
+
 " Test removing items in list
 func Test_list_func_remove()
   let lines =<< trim END
index d68ddce7aac0a0591efdf1c6dcb9bd878c1530be..5eb64adefa6fbd27df3b2105abbb83ee862bc1e3 100644 (file)
@@ -2244,10 +2244,14 @@ def Test_islocked()
 enddef
 
 def Test_items()
-  v9.CheckDefFailure(['[]->items()'], 'E1013: Argument 1: type mismatch, expected dict<any> but got list<unknown>')
+  v9.CheckDefFailure(['"x"->items()'], 'E1013: Argument 1: type mismatch, expected list<any> but got string')
   assert_equal([['a', 10], ['b', 20]], {'a': 10, 'b': 20}->items())
   assert_equal([], {}->items())
   assert_equal(['x', 'x'], {'a': 10, 'b': 20}->items()->map((_, _) => 'x'))
+
+  assert_equal([[0, 'a'], [1, 'b']], ['a', 'b']->items())
+  assert_equal([], []->items())
+  assert_equal([], test_null_list()->items())
 enddef
 
 def Test_job_getchannel()
index 505fe4fbef5a226214cf23a8edea35847f44ff88..92a9f3ad98131bfa6ab78412bdb3c360c20faf27 100644 (file)
@@ -707,6 +707,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    327,
 /**/
     326,
 /**/