mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
Merge pull request #22386 from zeertzjq/vim-8.2.2449
vim-patch:8.2.{2449,4627,4629,4632}: flattennew(), flatten() fixes
This commit is contained in:
commit
306e673583
@ -148,6 +148,8 @@ finddir({name} [, {path} [, {count}]])
|
|||||||
findfile({name} [, {path} [, {count}]])
|
findfile({name} [, {path} [, {count}]])
|
||||||
String find file {name} in {path}
|
String find file {name} in {path}
|
||||||
flatten({list} [, {maxdepth}]) List flatten {list} up to {maxdepth} levels
|
flatten({list} [, {maxdepth}]) List flatten {list} up to {maxdepth} levels
|
||||||
|
flattennew({list} [, {maxdepth}])
|
||||||
|
List flatten a copy of {list}
|
||||||
float2nr({expr}) Number convert Float {expr} to a Number
|
float2nr({expr}) Number convert Float {expr} to a Number
|
||||||
floor({expr}) Float round {expr} down
|
floor({expr}) Float round {expr} down
|
||||||
fmod({expr1}, {expr2}) Float remainder of {expr1} / {expr2}
|
fmod({expr1}, {expr2}) Float remainder of {expr1} / {expr2}
|
||||||
@ -2277,7 +2279,7 @@ flatten({list} [, {maxdepth}]) *flatten()*
|
|||||||
Flatten {list} up to {maxdepth} levels. Without {maxdepth}
|
Flatten {list} up to {maxdepth} levels. Without {maxdepth}
|
||||||
the result is a |List| without nesting, as if {maxdepth} is
|
the result is a |List| without nesting, as if {maxdepth} is
|
||||||
a very large number.
|
a very large number.
|
||||||
The {list} is changed in place, make a copy first if you do
|
The {list} is changed in place, use |flattennew()| if you do
|
||||||
not want that.
|
not want that.
|
||||||
*E900*
|
*E900*
|
||||||
{maxdepth} means how deep in nested lists changes are made.
|
{maxdepth} means how deep in nested lists changes are made.
|
||||||
@ -2295,6 +2297,10 @@ flatten({list} [, {maxdepth}]) *flatten()*
|
|||||||
Can also be used as a |method|: >
|
Can also be used as a |method|: >
|
||||||
mylist->flatten()
|
mylist->flatten()
|
||||||
<
|
<
|
||||||
|
flattennew({list} [, {maxdepth}]) *flattennew()*
|
||||||
|
Like |flatten()| but first make a copy of {list}.
|
||||||
|
|
||||||
|
|
||||||
float2nr({expr}) *float2nr()*
|
float2nr({expr}) *float2nr()*
|
||||||
Convert {expr} to a Number by omitting the part after the
|
Convert {expr} to a Number by omitting the part after the
|
||||||
decimal point.
|
decimal point.
|
||||||
|
@ -670,6 +670,7 @@ List manipulation: *list-functions*
|
|||||||
count() count number of times a value appears in a List
|
count() count number of times a value appears in a List
|
||||||
repeat() repeat a List multiple times
|
repeat() repeat a List multiple times
|
||||||
flatten() flatten a List
|
flatten() flatten a List
|
||||||
|
flattennew() flatten a copy of a List
|
||||||
|
|
||||||
Dictionary manipulation: *dict-functions*
|
Dictionary manipulation: *dict-functions*
|
||||||
get() get an entry without an error for a wrong key
|
get() get an entry without an error for a wrong key
|
||||||
|
@ -128,6 +128,7 @@ return {
|
|||||||
finddir={args={1, 3}, base=1},
|
finddir={args={1, 3}, base=1},
|
||||||
findfile={args={1, 3}, base=1},
|
findfile={args={1, 3}, base=1},
|
||||||
flatten={args={1, 2}, base=1},
|
flatten={args={1, 2}, base=1},
|
||||||
|
flattennew={args={1, 2}, base=1},
|
||||||
float2nr={args=1, base=1},
|
float2nr={args=1, base=1},
|
||||||
floor={args=1, base=1, float_func="floor"},
|
floor={args=1, base=1, float_func="floor"},
|
||||||
fmod={args=2, base=1},
|
fmod={args=2, base=1},
|
||||||
|
@ -1870,8 +1870,8 @@ static void f_expandcmd(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
|
|||||||
rettv->vval.v_string = cmdstr;
|
rettv->vval.v_string = cmdstr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// "flatten(list[, {maxdepth}])" function
|
/// "flatten()" and "flattennew()" functions
|
||||||
static void f_flatten(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
|
static void flatten_common(typval_T *argvars, typval_T *rettv, bool make_copy)
|
||||||
{
|
{
|
||||||
bool error = false;
|
bool error = false;
|
||||||
|
|
||||||
@ -1895,13 +1895,38 @@ static void f_flatten(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
|
|||||||
}
|
}
|
||||||
|
|
||||||
list_T *list = argvars[0].vval.v_list;
|
list_T *list = argvars[0].vval.v_list;
|
||||||
if (list != NULL
|
rettv->v_type = VAR_LIST;
|
||||||
&& !value_check_lock(tv_list_locked(list),
|
rettv->vval.v_list = list;
|
||||||
N_("flatten() argument"),
|
if (list == NULL) {
|
||||||
TV_TRANSLATE)
|
return;
|
||||||
&& tv_list_flatten(list, maxdepth) == OK) {
|
|
||||||
tv_copy(&argvars[0], rettv);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (make_copy) {
|
||||||
|
list = tv_list_copy(NULL, list, false, get_copyID());
|
||||||
|
rettv->vval.v_list = list;
|
||||||
|
if (list == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (value_check_lock(tv_list_locked(list), N_("flatten() argument"), TV_TRANSLATE)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
tv_list_ref(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
tv_list_flatten(list, NULL, tv_list_len(list), maxdepth);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// "flatten(list[, {maxdepth}])" function
|
||||||
|
static void f_flatten(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
|
||||||
|
{
|
||||||
|
flatten_common(argvars, rettv, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// "flattennew(list[, {maxdepth}])" function
|
||||||
|
static void f_flattennew(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
|
||||||
|
{
|
||||||
|
flatten_common(argvars, rettv, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// "extend(list, list [, idx])" function
|
/// "extend(list, list [, idx])" function
|
||||||
|
@ -647,55 +647,54 @@ tv_list_copy_error:
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Flatten "list" in place to depth "maxdepth".
|
/// Flatten up to "maxitems" in "list", starting at "first" to depth "maxdepth".
|
||||||
|
/// When "first" is NULL use the first item.
|
||||||
/// Does nothing if "maxdepth" is 0.
|
/// Does nothing if "maxdepth" is 0.
|
||||||
///
|
///
|
||||||
/// @param[in,out] list List to flatten
|
/// @param[in,out] list List to flatten
|
||||||
/// @param[in] maxdepth Maximum depth that will be flattened
|
/// @param[in] maxdepth Maximum depth that will be flattened
|
||||||
///
|
///
|
||||||
/// @return OK or FAIL
|
/// @return OK or FAIL
|
||||||
int tv_list_flatten(list_T *list, long maxdepth)
|
void tv_list_flatten(list_T *list, listitem_T *first, long maxitems, long maxdepth)
|
||||||
FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_WARN_UNUSED_RESULT
|
FUNC_ATTR_NONNULL_ARG(1)
|
||||||
{
|
{
|
||||||
listitem_T *item;
|
listitem_T *item;
|
||||||
listitem_T *to_free;
|
int done = 0;
|
||||||
int n;
|
|
||||||
if (maxdepth == 0) {
|
if (maxdepth == 0) {
|
||||||
return OK;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
n = 0;
|
if (first == NULL) {
|
||||||
item = list->lv_first;
|
item = list->lv_first;
|
||||||
while (item != NULL) {
|
} else {
|
||||||
|
item = first;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (item != NULL && done < maxitems) {
|
||||||
|
listitem_T *next = item->li_next;
|
||||||
|
|
||||||
fast_breakcheck();
|
fast_breakcheck();
|
||||||
if (got_int) {
|
if (got_int) {
|
||||||
return FAIL;
|
return;
|
||||||
}
|
}
|
||||||
if (item->li_tv.v_type == VAR_LIST) {
|
if (item->li_tv.v_type == VAR_LIST) {
|
||||||
listitem_T *next = item->li_next;
|
list_T *itemlist = item->li_tv.vval.v_list;
|
||||||
|
|
||||||
tv_list_drop_items(list, item, item);
|
tv_list_drop_items(list, item, item);
|
||||||
tv_list_extend(list, item->li_tv.vval.v_list, next);
|
tv_list_extend(list, itemlist, next);
|
||||||
|
|
||||||
|
if (maxdepth > 0) {
|
||||||
|
tv_list_flatten(list,
|
||||||
|
item->li_prev == NULL ? list->lv_first : item->li_prev->li_next,
|
||||||
|
itemlist->lv_len, maxdepth - 1);
|
||||||
|
}
|
||||||
tv_clear(&item->li_tv);
|
tv_clear(&item->li_tv);
|
||||||
to_free = item;
|
xfree(item);
|
||||||
|
|
||||||
if (item->li_prev == NULL) {
|
|
||||||
item = list->lv_first;
|
|
||||||
} else {
|
|
||||||
item = item->li_prev->li_next;
|
|
||||||
}
|
|
||||||
xfree(to_free);
|
|
||||||
|
|
||||||
if (++n >= maxdepth) {
|
|
||||||
n = 0;
|
|
||||||
item = next;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
n = 0;
|
|
||||||
item = item->li_next;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
done++;
|
||||||
|
item = next;
|
||||||
}
|
}
|
||||||
return OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extend first list with the second
|
/// Extend first list with the second
|
||||||
|
@ -78,4 +78,31 @@ func Test_flatten()
|
|||||||
call add(y, x) " l:y = [2, [1, [...]]]
|
call add(y, x) " l:y = [2, [1, [...]]]
|
||||||
call assert_equal([1, 2, 1, 2], flatten(l:x, 2))
|
call assert_equal([1, 2, 1, 2], flatten(l:x, 2))
|
||||||
call assert_equal([2, l:x], l:y)
|
call assert_equal([2, l:x], l:y)
|
||||||
|
|
||||||
|
let l4 = [ 1, [ 11, [ 101, [ 1001 ] ] ] ]
|
||||||
|
call assert_equal(l4, flatten(deepcopy(l4), 0))
|
||||||
|
call assert_equal([1, 11, [101, [1001]]], flatten(deepcopy(l4), 1))
|
||||||
|
call assert_equal([1, 11, 101, [1001]], flatten(deepcopy(l4), 2))
|
||||||
|
call assert_equal([1, 11, 101, 1001], flatten(deepcopy(l4), 3))
|
||||||
|
call assert_equal([1, 11, 101, 1001], flatten(deepcopy(l4), 4))
|
||||||
|
call assert_equal([1, 11, 101, 1001], flatten(deepcopy(l4)))
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
|
func Test_flattennew()
|
||||||
|
let l = [1, [2, [3, 4]], 5]
|
||||||
|
call assert_equal([1, 2, 3, 4, 5], flattennew(l))
|
||||||
|
call assert_equal([1, [2, [3, 4]], 5], l)
|
||||||
|
|
||||||
|
call assert_equal([1, 2, [3, 4], 5], flattennew(l, 1))
|
||||||
|
call assert_equal([1, [2, [3, 4]], 5], l)
|
||||||
|
|
||||||
|
let l4 = [ 1, [ 11, [ 101, [ 1001 ] ] ] ]
|
||||||
|
call assert_equal(l4, flatten(deepcopy(l4), 0))
|
||||||
|
call assert_equal([1, 11, [101, [1001]]], flattennew(l4, 1))
|
||||||
|
call assert_equal([1, 11, 101, [1001]], flattennew(l4, 2))
|
||||||
|
call assert_equal([1, 11, 101, 1001], flattennew(l4, 3))
|
||||||
|
call assert_equal([1, 11, 101, 1001], flattennew(l4, 4))
|
||||||
|
call assert_equal([1, 11, 101, 1001], flattennew(l4))
|
||||||
|
endfunc
|
||||||
|
|
||||||
|
" vim: shiftwidth=2 sts=2 expandtab
|
||||||
|
Loading…
Reference in New Issue
Block a user