Problem: Cannot use items() on a string.
Solution: Make items() work on a string. (closes #11016)
dictitem_T *di;
hashitem_T *hi;
listitem_T *li;
- listitem_T *li2;
dict_T *d;
int todo;
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;
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);
}
}
}
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);
* 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
|| 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;
}
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};
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};
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)},
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);
}
}
+/*
+ * "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.
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);
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
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)
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'))
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()
static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 331,
/**/
330,
/**/