1
0
forked from aniani/vim

patch 9.0.0331: cannot use items() on a string

Problem:    Cannot use items() on a string.
Solution:   Make items() work on a string. (closes #11016)
This commit is contained in:
Bram Moolenaar
2022-08-30 17:45:33 +01:00
parent f92cfb1acc
commit 3e518a8ec7
7 changed files with 65 additions and 24 deletions

View File

@@ -1456,7 +1456,6 @@ dict2list(typval_T *argvars, typval_T *rettv, dict2list_T what)
dictitem_T *di;
hashitem_T *hi;
listitem_T *li;
listitem_T *li2;
dict_T *d;
int todo;
@@ -1464,7 +1463,7 @@ dict2list(typval_T *argvars, typval_T *rettv, dict2list_T what)
return;
if ((what == DICT2LIST_ITEMS
? check_for_list_or_dict_arg(argvars, 0)
? check_for_string_or_list_or_dict_arg(argvars, 0)
: check_for_dict_arg(argvars, 0)) == FAIL)
return;
@@ -1509,19 +1508,9 @@ dict2list(typval_T *argvars, typval_T *rettv, dict2list_T what)
break;
++l2->lv_refcount;
li2 = listitem_alloc();
if (li2 == NULL)
if (list_append_string(l2, di->di_key, -1) == FAIL
|| list_append_tv(l2, &di->di_tv) == FAIL)
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);
}
}
}
@@ -1533,7 +1522,9 @@ dict2list(typval_T *argvars, typval_T *rettv, dict2list_T what)
void
f_items(typval_T *argvars, typval_T *rettv)
{
if (argvars[0].v_type == VAR_LIST)
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);

View File

@@ -902,7 +902,7 @@ arg_slice1(type_T *type, type_T *decl_type UNUSED, argcontext_T *context)
* or any)
*/
static int
arg_count1(type_T *type, type_T *decl_type UNUSED, argcontext_T *context)
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,7 +911,8 @@ arg_count1(type_T *type, type_T *decl_type UNUSED, argcontext_T *context)
|| type->tt_type == VAR_DICT)
return OK;
arg_type_mismatch(&t_string, type, context->arg_idx + 1);
semsg(_(e_string_list_or_dict_required_for_argument_nr),
context->arg_idx + 1);
return FAIL;
}
@@ -950,6 +951,7 @@ static argcheck_T arg1_list_number[] = {arg_list_number};
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,7 +1030,7 @@ static argcheck_T arg34_assert_inrange[] = {arg_float_or_nr, arg_float_or_nr, ar
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 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,7 +2031,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_list_or_dict,
{"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)},

View File

@@ -1065,9 +1065,8 @@ list2items(typval_T *argvars, typval_T *rettv)
if (rettv_list_alloc(rettv) == FAIL)
return;
if (l == NULL)
return; // empty list behaves like an empty list
return; // null list behaves like an empty list
// TODO: would be more efficient to not materialize the argument
CHECK_LIST_MATERIALIZE(l);
@@ -1084,6 +1083,39 @@ list2items(typval_T *argvars, typval_T *rettv)
}
}
/*
* "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.

View File

@@ -36,6 +36,7 @@ int list_assign_range(list_T *dest, list_T *src, long idx1_arg, long idx2, int e
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);

View File

@@ -206,7 +206,16 @@ func Test_list_items()
endfor
call assert_equal([[0, 'a'], [1, 'b'], [2, 'c']], r)
call assert_fails('call items(3)', 'E1227:')
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

View File

@@ -799,7 +799,7 @@ enddef
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(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,7 +2244,7 @@ def Test_islocked()
enddef
def Test_items()
v9.CheckDefFailure(['"x"->items()'], 'E1013: Argument 1: type mismatch, expected list<any> but got string')
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,6 +2252,10 @@ def Test_items()
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()

View File

@@ -707,6 +707,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
331,
/**/
330,
/**/