mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
vim-patch:partial:9.0.0202: code and help for indexof() is not ideal
Problem: Code and help for indexof() is not ideal.
Solution: Refactor the code, improve the help. (Yegappan Lakshmanan,
closes vim/vim#10908)
3fbf6cd355
Skip CHECK_LIST_MATERIALIZE and set_vim_var_type().
Use tv_list_uidx() instead of lv_idx.
Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
This commit is contained in:
parent
0972d7a124
commit
13da3d469a
@ -4087,7 +4087,7 @@ indent({lnum}) The result is a Number, which is indent of line {lnum} in the
|
|||||||
|
|
||||||
index({object}, {expr} [, {start} [, {ic}]]) *index()*
|
index({object}, {expr} [, {start} [, {ic}]]) *index()*
|
||||||
Find {expr} in {object} and return its index. See
|
Find {expr} in {object} and return its index. See
|
||||||
|filterof()| for using a lambda to select the item.
|
|indexof()| for using a lambda to select the item.
|
||||||
|
|
||||||
If {object} is a |List| return the lowest index where the item
|
If {object} is a |List| return the lowest index where the item
|
||||||
has a value equal to {expr}. There is no automatic
|
has a value equal to {expr}. There is no automatic
|
||||||
@ -4113,15 +4113,17 @@ index({object}, {expr} [, {start} [, {ic}]]) *index()*
|
|||||||
< Can also be used as a |method|: >
|
< Can also be used as a |method|: >
|
||||||
GetObject()->index(what)
|
GetObject()->index(what)
|
||||||
|
|
||||||
indexof({object}, {expr} [, {opt}]) *indexof()*
|
indexof({object}, {expr} [, {opts}]) *indexof()*
|
||||||
{object} must be a |List| or a |Blob|.
|
Returns the index of an item in {object} where {expr} is
|
||||||
|
v:true. {object} must be a |List| or a |Blob|.
|
||||||
|
|
||||||
If {object} is a |List|, evaluate {expr} for each item in the
|
If {object} is a |List|, evaluate {expr} for each item in the
|
||||||
List until the expression returns v:true and return the index
|
List until the expression is v:true and return the index of
|
||||||
of this item.
|
this item.
|
||||||
|
|
||||||
If {object} is a |Blob| evaluate {expr} for each byte in the
|
If {object} is a |Blob| evaluate {expr} for each byte in the
|
||||||
Blob until the expression returns v:true and return the index
|
Blob until the expression is v:true and return the index of
|
||||||
of this byte.
|
this byte.
|
||||||
|
|
||||||
{expr} must be a |string| or |Funcref|.
|
{expr} must be a |string| or |Funcref|.
|
||||||
|
|
||||||
@ -4137,16 +4139,17 @@ indexof({object}, {expr} [, {opt}]) *indexof()*
|
|||||||
The function must return |TRUE| if the item is found and the
|
The function must return |TRUE| if the item is found and the
|
||||||
search should stop.
|
search should stop.
|
||||||
|
|
||||||
The optional argument {opt} is a Dict and supports the
|
The optional argument {opts} is a Dict and supports the
|
||||||
following items:
|
following items:
|
||||||
start start evaluating {expr} at the item with index
|
startidx start evaluating {expr} at the item with this
|
||||||
{start} (may be negative for an item relative
|
index; may be negative for an item relative to
|
||||||
to the end).
|
the end
|
||||||
Returns -1 when {expr} evaluates to v:false for all the items.
|
Returns -1 when {expr} evaluates to v:false for all the items.
|
||||||
Example: >
|
Example: >
|
||||||
:let l = [#{n: 10}, #{n: 20}, #{n: 30]]
|
:let l = [#{n: 10}, #{n: 20}, #{n: 30}]
|
||||||
:let idx = indexof(l, "v:val.n == 20")
|
:echo indexof(l, "v:val.n == 20")
|
||||||
:let idx = indexof(l, {i, v -> v.n == 30})
|
:echo indexof(l, {i, v -> v.n == 30})
|
||||||
|
:echo indexof(l, "v:val.n == 20", #{startidx: 1})
|
||||||
|
|
||||||
< Can also be used as a |method|: >
|
< Can also be used as a |method|: >
|
||||||
mylist->indexof(expr)
|
mylist->indexof(expr)
|
||||||
|
@ -3509,6 +3509,71 @@ static varnumber_T indexof_eval_expr(typval_T *expr)
|
|||||||
return error ? false : found;
|
return error ? false : found;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Evaluate "expr" for each byte in the Blob "b" starting with the byte at
|
||||||
|
/// "startidx" and return the index of the byte where "expr" is TRUE. Returns
|
||||||
|
/// -1 if "expr" doesn't evaluate to TRUE for any of the bytes.
|
||||||
|
static varnumber_T indexof_blob(blob_T *b, varnumber_T startidx, typval_T *expr)
|
||||||
|
{
|
||||||
|
if (b == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (startidx < 0) {
|
||||||
|
// negative index: index from the last byte
|
||||||
|
startidx = tv_blob_len(b) + startidx;
|
||||||
|
if (startidx < 0) {
|
||||||
|
startidx = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (varnumber_T idx = startidx; idx < tv_blob_len(b); idx++) {
|
||||||
|
set_vim_var_nr(VV_KEY, idx);
|
||||||
|
set_vim_var_nr(VV_VAL, tv_blob_get(b, (int)idx));
|
||||||
|
|
||||||
|
if (indexof_eval_expr(expr)) {
|
||||||
|
return idx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Evaluate "expr" for each item in the List "l" starting with the item at
|
||||||
|
/// "startidx" and return the index of the item where "expr" is TRUE. Returns
|
||||||
|
/// -1 if "expr" doesn't evaluate to TRUE for any of the items.
|
||||||
|
static varnumber_T indexof_list(list_T *l, varnumber_T startidx, typval_T *expr)
|
||||||
|
{
|
||||||
|
if (l == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
listitem_T *item;
|
||||||
|
varnumber_T idx = 0;
|
||||||
|
if (startidx == 0) {
|
||||||
|
item = tv_list_first(l);
|
||||||
|
} else {
|
||||||
|
// Start at specified item.
|
||||||
|
idx = tv_list_uidx(l, (int)startidx);
|
||||||
|
if (idx == -1) {
|
||||||
|
item = NULL;
|
||||||
|
} else {
|
||||||
|
item = tv_list_find(l, (int)idx);
|
||||||
|
assert(item != NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (; item != NULL; item = TV_LIST_ITEM_NEXT(l, item), idx++) {
|
||||||
|
set_vim_var_nr(VV_KEY, idx);
|
||||||
|
tv_copy(TV_LIST_ITEM_TV(item), get_vim_var_tv(VV_VAL));
|
||||||
|
|
||||||
|
if (indexof_eval_expr(expr)) {
|
||||||
|
return idx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
/// "indexof()" function
|
/// "indexof()" function
|
||||||
static void f_indexof(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
|
static void f_indexof(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
|
||||||
{
|
{
|
||||||
@ -3526,7 +3591,6 @@ static void f_indexof(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
|
|||||||
}
|
}
|
||||||
|
|
||||||
varnumber_T startidx = 0;
|
varnumber_T startidx = 0;
|
||||||
varnumber_T idx = 0;
|
|
||||||
if (argvars[2].v_type == VAR_DICT) {
|
if (argvars[2].v_type == VAR_DICT) {
|
||||||
startidx = tv_dict_get_number_def(argvars[2].vval.v_dict, "startidx", 0);
|
startidx = tv_dict_get_number_def(argvars[2].vval.v_dict, "startidx", 0);
|
||||||
}
|
}
|
||||||
@ -3542,56 +3606,11 @@ static void f_indexof(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
|
|||||||
did_emsg = false;
|
did_emsg = false;
|
||||||
|
|
||||||
if (argvars[0].v_type == VAR_BLOB) {
|
if (argvars[0].v_type == VAR_BLOB) {
|
||||||
blob_T *const b = argvars[0].vval.v_blob;
|
rettv->vval.v_number = indexof_blob(argvars[0].vval.v_blob, startidx, &argvars[1]);
|
||||||
if (b == NULL) {
|
|
||||||
goto theend;
|
|
||||||
}
|
|
||||||
if (startidx < 0) {
|
|
||||||
startidx = tv_blob_len(b) + startidx;
|
|
||||||
if (startidx < 0) {
|
|
||||||
startidx = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (idx = startidx; idx < tv_blob_len(b); idx++) {
|
|
||||||
set_vim_var_nr(VV_KEY, idx);
|
|
||||||
set_vim_var_nr(VV_VAL, tv_blob_get(b, (int)idx));
|
|
||||||
|
|
||||||
if (indexof_eval_expr(&argvars[1])) {
|
|
||||||
rettv->vval.v_number = idx;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
list_T *const l = argvars[0].vval.v_list;
|
rettv->vval.v_number = indexof_list(argvars[0].vval.v_list, startidx, &argvars[1]);
|
||||||
if (l == NULL) {
|
|
||||||
goto theend;
|
|
||||||
}
|
|
||||||
|
|
||||||
listitem_T *item;
|
|
||||||
if (startidx == 0) {
|
|
||||||
item = tv_list_first(l);
|
|
||||||
} else {
|
|
||||||
// Start at specified item. Use the cached index that list_find()
|
|
||||||
// sets, so that a negative number also works.
|
|
||||||
item = tv_list_find(l, (int)startidx);
|
|
||||||
if (item != NULL) {
|
|
||||||
idx = l->lv_idx;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (; item != NULL; item = TV_LIST_ITEM_NEXT(l, item), idx++) {
|
|
||||||
set_vim_var_nr(VV_KEY, idx);
|
|
||||||
tv_copy(TV_LIST_ITEM_TV(item), get_vim_var_tv(VV_VAL));
|
|
||||||
|
|
||||||
if (indexof_eval_expr(&argvars[1])) {
|
|
||||||
rettv->vval.v_number = idx;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
theend:
|
|
||||||
restore_vimvar(VV_KEY, &save_key);
|
restore_vimvar(VV_KEY, &save_key);
|
||||||
restore_vimvar(VV_VAL, &save_val);
|
restore_vimvar(VV_VAL, &save_val);
|
||||||
did_emsg |= save_did_emsg;
|
did_emsg |= save_did_emsg;
|
||||||
|
Loading…
Reference in New Issue
Block a user