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

@@ -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.