Patch 9.0.0331

4 views
Skip to first unread message

Bram Moolenaar

unread,
Aug 30, 2022, 12:46:47 PM8/30/22
to vim...@googlegroups.com

Patch 9.0.0331
Problem: Cannot use items() on a string.
Solution: Make items() work on a string. (closes #11016)
Files: src/dict.c, src/list.c, src/proto/list.pro, src/evalfunc.c
src/testdir/test_listdict.vim, src/testdir/test_vim9_builtin.vim


*** ../vim-9.0.0330/src/dict.c 2022-08-30 14:34:48.481138929 +0100
--- src/dict.c 2022-08-30 17:21:05.595676697 +0100
***************
*** 1456,1462 ****
dictitem_T *di;
hashitem_T *hi;
listitem_T *li;
- listitem_T *li2;
dict_T *d;
int todo;

--- 1456,1461 ----
***************
*** 1464,1470 ****
return;

if ((what == DICT2LIST_ITEMS
! ? check_for_list_or_dict_arg(argvars, 0)
: check_for_dict_arg(argvars, 0)) == FAIL)
return;

--- 1463,1469 ----
return;

if ((what == DICT2LIST_ITEMS
! ? check_for_string_or_list_or_dict_arg(argvars, 0)
: check_for_dict_arg(argvars, 0)) == FAIL)
return;

***************
*** 1509,1527 ****
break;
++l2->lv_refcount;

! li2 = listitem_alloc();
! if (li2 == NULL)
break;
- list_append(l2, li2);
- li2->li_tv.v_type = VAR_STRING;
- li2->li_tv.v_lock = 0;
- li2->li_tv.vval.v_string = vim_strsave(di->di_key);
-
- li2 = listitem_alloc();
- if (li2 == NULL)
- break;
- list_append(l2, li2);
- copy_tv(&di->di_tv, &li2->li_tv);
}
}
}
--- 1508,1516 ----
break;
++l2->lv_refcount;

! if (list_append_string(l2, di->di_key, -1) == FAIL
! || list_append_tv(l2, &di->di_tv) == FAIL)
break;
}
}
}
***************
*** 1533,1539 ****
void
f_items(typval_T *argvars, typval_T *rettv)
{
! if (argvars[0].v_type == VAR_LIST)
list2items(argvars, rettv);
else
dict2list(argvars, rettv, DICT2LIST_ITEMS);
--- 1522,1530 ----
void
f_items(typval_T *argvars, typval_T *rettv)
{
! if (argvars[0].v_type == VAR_STRING)
! string2items(argvars, rettv);
! else if (argvars[0].v_type == VAR_LIST)
list2items(argvars, rettv);
else
dict2list(argvars, rettv, DICT2LIST_ITEMS);
*** ../vim-9.0.0330/src/list.c 2022-08-30 14:34:48.481138929 +0100
--- src/list.c 2022-08-30 17:19:42.663841923 +0100
***************
*** 1065,1073 ****

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);
--- 1065,1072 ----

if (rettv_list_alloc(rettv) == FAIL)
return;
if (l == NULL)
! return; // null list behaves like an empty list

// TODO: would be more efficient to not materialize the argument
CHECK_LIST_MATERIALIZE(l);
***************
*** 1084,1089 ****
--- 1083,1121 ----
}
}

+ /*
+ * "items(string)" function
+ * Caller must have already checked that argvars[0] is a String.
+ */
+ void
+ string2items(typval_T *argvars, typval_T *rettv)
+ {
+ char_u *p = argvars[0].vval.v_string;
+ varnumber_T idx;
+
+ if (rettv_list_alloc(rettv) == FAIL)
+ return;
+ if (p == NULL)
+ return; // null string behaves like an empty string
+
+ for (idx = 0; *p != NUL; ++idx)
+ {
+ int len = mb_ptr2len(p);
+ list_T *l2;
+
+ if (len == 0)
+ break;
+ 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_string(l2, p, len) == FAIL)
+ break;
+ p += len;
+ }
+ }
+
/*
* Extend "l1" with "l2". "l1" must not be NULL.
* If "bef" is NULL append at the end, otherwise insert before this item.
*** ../vim-9.0.0330/src/proto/list.pro 2022-08-30 14:34:48.481138929 +0100
--- src/proto/list.pro 2022-08-30 17:20:05.379796161 +0100
***************
*** 36,41 ****
--- 36,42 ----
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);
+ void string2items(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);
*** ../vim-9.0.0330/src/evalfunc.c 2022-08-30 14:34:48.481138929 +0100
--- src/evalfunc.c 2022-08-30 17:27:11.586994171 +0100
***************
*** 902,908 ****
* or any)
*/
static int
! arg_count1(type_T *type, type_T *decl_type UNUSED, argcontext_T *context)
{
if (type->tt_type == VAR_ANY
|| type->tt_type == VAR_UNKNOWN
--- 902,908 ----
* or any)
*/
static int
! arg_string_or_list_or_dict(type_T *type, type_T *decl_type UNUSED, argcontext_T *context)
{
if (type->tt_type == VAR_ANY
|| type->tt_type == VAR_UNKNOWN
***************
*** 911,917 ****
|| type->tt_type == VAR_DICT)
return OK;

! arg_type_mismatch(&t_string, type, context->arg_idx + 1);
return FAIL;
}

--- 911,918 ----
|| type->tt_type == VAR_DICT)
return OK;

! semsg(_(e_string_list_or_dict_required_for_argument_nr),
! context->arg_idx + 1);
return FAIL;
}

***************
*** 950,955 ****
--- 951,957 ----
static argcheck_T arg1_list_or_blob[] = {arg_list_or_blob};
static argcheck_T arg1_list_or_dict[] = {arg_list_or_dict};
static argcheck_T arg1_list_string[] = {arg_list_string};
+ static argcheck_T arg1_string_or_list_or_dict[] = {arg_string_or_list_or_dict};
static argcheck_T arg1_lnum[] = {arg_lnum};
static argcheck_T arg1_number[] = {arg_number};
static argcheck_T arg1_string[] = {arg_string};
***************
*** 1028,1034 ****
static argcheck_T arg4_browse[] = {arg_bool, arg_string, arg_string, arg_string};
static argcheck_T arg23_chanexpr[] = {arg_chan_or_job, NULL, arg_dict_any};
static argcheck_T arg23_chanraw[] = {arg_chan_or_job, arg_string_or_blob, arg_dict_any};
! static argcheck_T arg24_count[] = {arg_count1, NULL, arg_bool, arg_number};
static argcheck_T arg13_cursor[] = {arg_cursor1, arg_number, arg_number};
static argcheck_T arg12_deepcopy[] = {NULL, arg_bool};
static argcheck_T arg12_execute[] = {arg_string_or_list_string, arg_string};
--- 1030,1036 ----
static argcheck_T arg4_browse[] = {arg_bool, arg_string, arg_string, arg_string};
static argcheck_T arg23_chanexpr[] = {arg_chan_or_job, NULL, arg_dict_any};
static argcheck_T arg23_chanraw[] = {arg_chan_or_job, arg_string_or_blob, arg_dict_any};
! static argcheck_T arg24_count[] = {arg_string_or_list_or_dict, NULL, arg_bool, arg_number};
static argcheck_T arg13_cursor[] = {arg_cursor1, arg_number, arg_number};
static argcheck_T arg12_deepcopy[] = {NULL, arg_bool};
static argcheck_T arg12_execute[] = {arg_string_or_list_string, arg_string};
***************
*** 2029,2035 ****
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_list_or_dict,
ret_list_items, f_items},
{"job_getchannel", 1, 1, FEARG_1, arg1_job,
ret_channel, JOB_FUNC(f_job_getchannel)},
--- 2031,2037 ----
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_string_or_list_or_dict,
ret_list_items, f_items},
{"job_getchannel", 1, 1, FEARG_1, arg1_job,
ret_channel, JOB_FUNC(f_job_getchannel)},
*** ../vim-9.0.0330/src/testdir/test_listdict.vim 2022-08-30 14:34:48.481138929 +0100
--- src/testdir/test_listdict.vim 2022-08-30 17:29:44.766723844 +0100
***************
*** 206,212 ****
endfor
call assert_equal([[0, 'a'], [1, 'b'], [2, 'c']], r)

! call assert_fails('call items(3)', 'E1227:')
endfunc

" Test removing items in list
--- 206,221 ----
endfor
call assert_equal([[0, 'a'], [1, 'b'], [2, 'c']], r)

! call assert_fails('call items(3)', 'E1225:')
! endfunc
!
! func Test_string_items()
! let r = []
! let s = 'ábツ'
! for [idx, val] in items(s)
! call extend(r, [[idx, val]])
! endfor
! call assert_equal([[0, 'á'], [1, 'b'], [2, 'ツ']], r)
endfunc

" Test removing items in list
*** ../vim-9.0.0330/src/testdir/test_vim9_builtin.vim 2022-08-30 14:34:48.481138929 +0100
--- src/testdir/test_vim9_builtin.vim 2022-08-30 17:43:10.940211724 +0100
***************
*** 799,805 ****
def Test_count()
count('ABC ABC ABC', 'b', true)->assert_equal(3)
count('ABC ABC ABC', 'b', false)->assert_equal(0)
! v9.CheckDefAndScriptFailure(['count(10, 1)'], ['E1013: Argument 1: type mismatch, expected string but got number', 'E1225: String, List or Dictionary required for argument 1'])
v9.CheckDefAndScriptFailure(['count("a", [1], 2)'], ['E1013: Argument 3: type mismatch, expected bool but got number', 'E1212: Bool required for argument 3'])
v9.CheckDefAndScriptFailure(['count("a", [1], 0, "b")'], ['E1013: Argument 4: type mismatch, expected number but got string', 'E1210: Number required for argument 4'])
count([1, 2, 2, 3], 2)->assert_equal(2)
--- 799,805 ----
def Test_count()
count('ABC ABC ABC', 'b', true)->assert_equal(3)
count('ABC ABC ABC', 'b', false)->assert_equal(0)
! v9.CheckDefAndScriptFailure(['count(10, 1)'], 'E1225: String, List or Dictionary required for argument 1')
v9.CheckDefAndScriptFailure(['count("a", [1], 2)'], ['E1013: Argument 3: type mismatch, expected bool but got number', 'E1212: Bool required for argument 3'])
v9.CheckDefAndScriptFailure(['count("a", [1], 0, "b")'], ['E1013: Argument 4: type mismatch, expected number but got string', 'E1210: Number required for argument 4'])
count([1, 2, 2, 3], 2)->assert_equal(2)
***************
*** 2244,2250 ****
enddef

def Test_items()
! 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'))
--- 2244,2250 ----
enddef

def Test_items()
! v9.CheckDefFailure(['123->items()'], 'E1225:')
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'))
***************
*** 2252,2257 ****
--- 2252,2261 ----
assert_equal([[0, 'a'], [1, 'b']], ['a', 'b']->items())
assert_equal([], []->items())
assert_equal([], test_null_list()->items())
+
+ assert_equal([[0, 'a'], [1, '웃'], [2, 'ć']], 'a웃ć'->items())
+ assert_equal([], ''->items())
+ assert_equal([], test_null_string()->items())
enddef

def Test_job_getchannel()
*** ../vim-9.0.0330/src/version.c 2022-08-30 16:40:42.060873946 +0100
--- src/version.c 2022-08-30 17:44:15.087919891 +0100
***************
*** 709,710 ****
--- 709,712 ----
{ /* Add new patch number below this line */
+ /**/
+ 331,
/**/

--
THEOREM: VI is perfect.
PROOF: VI in roman numerals is 6. The natural numbers < 6 which divide 6 are
1, 2, and 3. 1+2+3 = 6. So 6 is a perfect number. Therefore, VI is perfect.
QED
-- Arthur Tateishi

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
Reply all
Reply to author
Forward
0 new messages