mirror of
https://github.com/vim/vim.git
synced 2025-07-26 11:04:33 -04:00
patch 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 #10908)
This commit is contained in:
parent
9113c2cd19
commit
3fbf6cd355
@ -4733,7 +4733,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
|
||||||
@ -4759,15 +4759,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|.
|
||||||
|
|
||||||
@ -4783,16 +4785,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)
|
||||||
|
144
src/evalfunc.c
144
src/evalfunc.c
@ -6818,17 +6818,90 @@ 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 int
|
||||||
|
indexof_blob(blob_T *b, long startidx, typval_T *expr)
|
||||||
|
{
|
||||||
|
long idx = 0;
|
||||||
|
|
||||||
|
if (b == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (startidx < 0)
|
||||||
|
{
|
||||||
|
// negative index: index from the last byte
|
||||||
|
startidx = blob_len(b) + startidx;
|
||||||
|
if (startidx < 0)
|
||||||
|
startidx = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
set_vim_var_type(VV_KEY, VAR_NUMBER);
|
||||||
|
set_vim_var_type(VV_VAL, VAR_NUMBER);
|
||||||
|
|
||||||
|
for (idx = startidx; idx < blob_len(b); ++idx)
|
||||||
|
{
|
||||||
|
set_vim_var_nr(VV_KEY, idx);
|
||||||
|
set_vim_var_nr(VV_VAL, blob_get(b, 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 int
|
||||||
|
indexof_list(list_T *l, long startidx, typval_T *expr)
|
||||||
|
{
|
||||||
|
listitem_T *item;
|
||||||
|
long idx = 0;
|
||||||
|
|
||||||
|
if (l == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
CHECK_LIST_MATERIALIZE(l);
|
||||||
|
|
||||||
|
if (startidx == 0)
|
||||||
|
item = l->lv_first;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Start at specified item. Use the cached index that list_find()
|
||||||
|
// sets, so that a negative number also works.
|
||||||
|
item = list_find(l, startidx);
|
||||||
|
if (item != NULL)
|
||||||
|
idx = l->lv_u.mat.lv_idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
set_vim_var_type(VV_KEY, VAR_NUMBER);
|
||||||
|
|
||||||
|
for ( ; item != NULL; item = item->li_next, ++idx)
|
||||||
|
{
|
||||||
|
set_vim_var_nr(VV_KEY, idx);
|
||||||
|
copy_tv(&item->li_tv, get_vim_var_tv(VV_VAL));
|
||||||
|
|
||||||
|
if (indexof_eval_expr(expr))
|
||||||
|
return idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* "indexof()" function
|
* "indexof()" function
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
f_indexof(typval_T *argvars, typval_T *rettv)
|
f_indexof(typval_T *argvars, typval_T *rettv)
|
||||||
{
|
{
|
||||||
list_T *l;
|
|
||||||
listitem_T *item;
|
|
||||||
blob_T *b;
|
|
||||||
long startidx = 0;
|
long startidx = 0;
|
||||||
long idx = 0;
|
|
||||||
typval_T save_val;
|
typval_T save_val;
|
||||||
typval_T save_key;
|
typval_T save_key;
|
||||||
int save_did_emsg;
|
int save_did_emsg;
|
||||||
@ -6857,67 +6930,12 @@ f_indexof(typval_T *argvars, typval_T *rettv)
|
|||||||
did_emsg = FALSE;
|
did_emsg = FALSE;
|
||||||
|
|
||||||
if (argvars[0].v_type == VAR_BLOB)
|
if (argvars[0].v_type == VAR_BLOB)
|
||||||
{
|
rettv->vval.v_number = indexof_blob(argvars[0].vval.v_blob, startidx,
|
||||||
b = argvars[0].vval.v_blob;
|
&argvars[1]);
|
||||||
if (b == NULL)
|
|
||||||
goto theend;
|
|
||||||
if (startidx < 0)
|
|
||||||
{
|
|
||||||
startidx = blob_len(b) + startidx;
|
|
||||||
if (startidx < 0)
|
|
||||||
startidx = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
set_vim_var_type(VV_KEY, VAR_NUMBER);
|
|
||||||
set_vim_var_type(VV_VAL, VAR_NUMBER);
|
|
||||||
|
|
||||||
for (idx = startidx; idx < blob_len(b); ++idx)
|
|
||||||
{
|
|
||||||
set_vim_var_nr(VV_KEY, idx);
|
|
||||||
set_vim_var_nr(VV_VAL, blob_get(b, idx));
|
|
||||||
|
|
||||||
if (indexof_eval_expr(&argvars[1]))
|
|
||||||
{
|
|
||||||
rettv->vval.v_number = idx;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
rettv->vval.v_number = indexof_list(argvars[0].vval.v_list, startidx,
|
||||||
l = argvars[0].vval.v_list;
|
&argvars[1]);
|
||||||
if (l == NULL)
|
|
||||||
goto theend;
|
|
||||||
|
|
||||||
CHECK_LIST_MATERIALIZE(l);
|
|
||||||
|
|
||||||
if (startidx == 0)
|
|
||||||
item = l->lv_first;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Start at specified item. Use the cached index that list_find()
|
|
||||||
// sets, so that a negative number also works.
|
|
||||||
item = list_find(l, startidx);
|
|
||||||
if (item != NULL)
|
|
||||||
idx = l->lv_u.mat.lv_idx;
|
|
||||||
}
|
|
||||||
|
|
||||||
set_vim_var_type(VV_KEY, VAR_NUMBER);
|
|
||||||
|
|
||||||
for ( ; item != NULL; item = item->li_next, ++idx)
|
|
||||||
{
|
|
||||||
set_vim_var_nr(VV_KEY, idx);
|
|
||||||
copy_tv(&item->li_tv, 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;
|
||||||
|
@ -2067,10 +2067,17 @@ def Test_index()
|
|||||||
enddef
|
enddef
|
||||||
|
|
||||||
def Test_indexof()
|
def Test_indexof()
|
||||||
var l = [{color: 'red'}, {color: 'blue'}, {color: 'green'}]
|
var l = [{color: 'red'}, {color: 'blue'}, {color: 'green'}, {color: 'blue'}]
|
||||||
indexof(l, (i, v) => v.color == 'green')->assert_equal(2)
|
indexof(l, (i, v) => v.color == 'blue')->assert_equal(1)
|
||||||
|
indexof(l, (i, v) => v.color == 'blue', {startidx: 1})->assert_equal(1)
|
||||||
|
indexof(l, (i, v) => v.color == 'blue', {startidx: 2})->assert_equal(3)
|
||||||
var b = 0zdeadbeef
|
var b = 0zdeadbeef
|
||||||
indexof(b, "v:val == 0xef")->assert_equal(3)
|
indexof(b, "v:val == 0xef")->assert_equal(3)
|
||||||
|
|
||||||
|
def TestIdx(k: number, v: dict<any>): bool
|
||||||
|
return v.color == 'blue'
|
||||||
|
enddef
|
||||||
|
indexof(l, TestIdx)->assert_equal(1)
|
||||||
enddef
|
enddef
|
||||||
|
|
||||||
def Test_input()
|
def Test_input()
|
||||||
|
@ -735,6 +735,8 @@ static char *(features[]) =
|
|||||||
|
|
||||||
static int included_patches[] =
|
static int included_patches[] =
|
||||||
{ /* Add new patch number below this line */
|
{ /* Add new patch number below this line */
|
||||||
|
/**/
|
||||||
|
202,
|
||||||
/**/
|
/**/
|
||||||
201,
|
201,
|
||||||
/**/
|
/**/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user