Skip to content

Commit 3fbf6cd

Browse files
yegappanbrammool
authored andcommitted
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)
1 parent 9113c2c commit 3fbf6cd

File tree

4 files changed

+109
-79
lines changed

4 files changed

+109
-79
lines changed

runtime/doc/builtin.txt

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4733,7 +4733,7 @@ indent({lnum}) The result is a Number, which is indent of line {lnum} in the
47334733
47344734
index({object}, {expr} [, {start} [, {ic}]]) *index()*
47354735
Find {expr} in {object} and return its index. See
4736-
|filterof()| for using a lambda to select the item.
4736+
|indexof()| for using a lambda to select the item.
47374737

47384738
If {object} is a |List| return the lowest index where the item
47394739
has a value equal to {expr}. There is no automatic
@@ -4759,15 +4759,17 @@ index({object}, {expr} [, {start} [, {ic}]]) *index()*
47594759
< Can also be used as a |method|: >
47604760
GetObject()->index(what)
47614761
4762-
indexof({object}, {expr} [, {opt}]) *indexof()*
4763-
{object} must be a |List| or a |Blob|.
4762+
indexof({object}, {expr} [, {opts}]) *indexof()*
4763+
Returns the index of an item in {object} where {expr} is
4764+
v:true. {object} must be a |List| or a |Blob|.
4765+
47644766
If {object} is a |List|, evaluate {expr} for each item in the
4765-
List until the expression returns v:true and return the index
4766-
of this item.
4767+
List until the expression is v:true and return the index of
4768+
this item.
47674769

47684770
If {object} is a |Blob| evaluate {expr} for each byte in the
4769-
Blob until the expression returns v:true and return the index
4770-
of this byte.
4771+
Blob until the expression is v:true and return the index of
4772+
this byte.
47714773

47724774
{expr} must be a |string| or |Funcref|.
47734775

@@ -4783,16 +4785,17 @@ indexof({object}, {expr} [, {opt}]) *indexof()*
47834785
The function must return |TRUE| if the item is found and the
47844786
search should stop.
47854787

4786-
The optional argument {opt} is a Dict and supports the
4788+
The optional argument {opts} is a Dict and supports the
47874789
following items:
4788-
start start evaluating {expr} at the item with index
4789-
{start} (may be negative for an item relative
4790-
to the end).
4790+
startidx start evaluating {expr} at the item with this
4791+
index; may be negative for an item relative to
4792+
the end
47914793
Returns -1 when {expr} evaluates to v:false for all the items.
47924794
Example: >
4793-
:let l = [#{n: 10}, #{n: 20}, #{n: 30]]
4794-
:let idx = indexof(l, "v:val.n == 20")
4795-
:let idx = indexof(l, {i, v -> v.n == 30})
4795+
:let l = [#{n: 10}, #{n: 20}, #{n: 30}]
4796+
:echo indexof(l, "v:val.n == 20")
4797+
:echo indexof(l, {i, v -> v.n == 30})
4798+
:echo indexof(l, "v:val.n == 20", #{startidx: 1})
47964799
47974800
< Can also be used as a |method|: >
47984801
mylist->indexof(expr)

src/evalfunc.c

Lines changed: 81 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6818,17 +6818,90 @@ indexof_eval_expr(typval_T *expr)
68186818
return error ? FALSE : found;
68196819
}
68206820

6821+
/*
6822+
* Evaluate 'expr' for each byte in the Blob 'b' starting with the byte at
6823+
* 'startidx' and return the index of the byte where 'expr' is TRUE. Returns
6824+
* -1 if 'expr' doesn't evaluate to TRUE for any of the bytes.
6825+
*/
6826+
static int
6827+
indexof_blob(blob_T *b, long startidx, typval_T *expr)
6828+
{
6829+
long idx = 0;
6830+
6831+
if (b == NULL)
6832+
return -1;
6833+
6834+
if (startidx < 0)
6835+
{
6836+
// negative index: index from the last byte
6837+
startidx = blob_len(b) + startidx;
6838+
if (startidx < 0)
6839+
startidx = 0;
6840+
}
6841+
6842+
set_vim_var_type(VV_KEY, VAR_NUMBER);
6843+
set_vim_var_type(VV_VAL, VAR_NUMBER);
6844+
6845+
for (idx = startidx; idx < blob_len(b); ++idx)
6846+
{
6847+
set_vim_var_nr(VV_KEY, idx);
6848+
set_vim_var_nr(VV_VAL, blob_get(b, idx));
6849+
6850+
if (indexof_eval_expr(expr))
6851+
return idx;
6852+
}
6853+
6854+
return -1;
6855+
}
6856+
6857+
/*
6858+
* Evaluate 'expr' for each item in the List 'l' starting with the item at
6859+
* 'startidx' and return the index of the item where 'expr' is TRUE. Returns
6860+
* -1 if 'expr' doesn't evaluate to TRUE for any of the items.
6861+
*/
6862+
static int
6863+
indexof_list(list_T *l, long startidx, typval_T *expr)
6864+
{
6865+
listitem_T *item;
6866+
long idx = 0;
6867+
6868+
if (l == NULL)
6869+
return -1;
6870+
6871+
CHECK_LIST_MATERIALIZE(l);
6872+
6873+
if (startidx == 0)
6874+
item = l->lv_first;
6875+
else
6876+
{
6877+
// Start at specified item. Use the cached index that list_find()
6878+
// sets, so that a negative number also works.
6879+
item = list_find(l, startidx);
6880+
if (item != NULL)
6881+
idx = l->lv_u.mat.lv_idx;
6882+
}
6883+
6884+
set_vim_var_type(VV_KEY, VAR_NUMBER);
6885+
6886+
for ( ; item != NULL; item = item->li_next, ++idx)
6887+
{
6888+
set_vim_var_nr(VV_KEY, idx);
6889+
copy_tv(&item->li_tv, get_vim_var_tv(VV_VAL));
6890+
6891+
if (indexof_eval_expr(expr))
6892+
return idx;
6893+
}
6894+
6895+
return -1;
6896+
}
6897+
68216898
/*
68226899
* "indexof()" function
68236900
*/
68246901
static void
68256902
f_indexof(typval_T *argvars, typval_T *rettv)
68266903
{
6827-
list_T *l;
6828-
listitem_T *item;
6829-
blob_T *b;
68306904
long startidx = 0;
6831-
long idx = 0;
68326905
typval_T save_val;
68336906
typval_T save_key;
68346907
int save_did_emsg;
@@ -6857,67 +6930,12 @@ f_indexof(typval_T *argvars, typval_T *rettv)
68576930
did_emsg = FALSE;
68586931

68596932
if (argvars[0].v_type == VAR_BLOB)
6860-
{
6861-
b = argvars[0].vval.v_blob;
6862-
if (b == NULL)
6863-
goto theend;
6864-
if (startidx < 0)
6865-
{
6866-
startidx = blob_len(b) + startidx;
6867-
if (startidx < 0)
6868-
startidx = 0;
6869-
}
6870-
6871-
set_vim_var_type(VV_KEY, VAR_NUMBER);
6872-
set_vim_var_type(VV_VAL, VAR_NUMBER);
6873-
6874-
for (idx = startidx; idx < blob_len(b); ++idx)
6875-
{
6876-
set_vim_var_nr(VV_KEY, idx);
6877-
set_vim_var_nr(VV_VAL, blob_get(b, idx));
6878-
6879-
if (indexof_eval_expr(&argvars[1]))
6880-
{
6881-
rettv->vval.v_number = idx;
6882-
break;
6883-
}
6884-
}
6885-
}
6933+
rettv->vval.v_number = indexof_blob(argvars[0].vval.v_blob, startidx,
6934+
&argvars[1]);
68866935
else
6887-
{
6888-
l = argvars[0].vval.v_list;
6889-
if (l == NULL)
6890-
goto theend;
6891-
6892-
CHECK_LIST_MATERIALIZE(l);
6893-
6894-
if (startidx == 0)
6895-
item = l->lv_first;
6896-
else
6897-
{
6898-
// Start at specified item. Use the cached index that list_find()
6899-
// sets, so that a negative number also works.
6900-
item = list_find(l, startidx);
6901-
if (item != NULL)
6902-
idx = l->lv_u.mat.lv_idx;
6903-
}
6904-
6905-
set_vim_var_type(VV_KEY, VAR_NUMBER);
6936+
rettv->vval.v_number = indexof_list(argvars[0].vval.v_list, startidx,
6937+
&argvars[1]);
69066938

6907-
for ( ; item != NULL; item = item->li_next, ++idx)
6908-
{
6909-
set_vim_var_nr(VV_KEY, idx);
6910-
copy_tv(&item->li_tv, get_vim_var_tv(VV_VAL));
6911-
6912-
if (indexof_eval_expr(&argvars[1]))
6913-
{
6914-
rettv->vval.v_number = idx;
6915-
break;
6916-
}
6917-
}
6918-
}
6919-
6920-
theend:
69216939
restore_vimvar(VV_KEY, &save_key);
69226940
restore_vimvar(VV_VAL, &save_val);
69236941
did_emsg |= save_did_emsg;

src/testdir/test_vim9_builtin.vim

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2067,10 +2067,17 @@ def Test_index()
20672067
enddef
20682068

20692069
def Test_indexof()
2070-
var l = [{color: 'red'}, {color: 'blue'}, {color: 'green'}]
2071-
indexof(l, (i, v) => v.color == 'green')->assert_equal(2)
2070+
var l = [{color: 'red'}, {color: 'blue'}, {color: 'green'}, {color: 'blue'}]
2071+
indexof(l, (i, v) => v.color == 'blue')->assert_equal(1)
2072+
indexof(l, (i, v) => v.color == 'blue', {startidx: 1})->assert_equal(1)
2073+
indexof(l, (i, v) => v.color == 'blue', {startidx: 2})->assert_equal(3)
20722074
var b = 0zdeadbeef
20732075
indexof(b, "v:val == 0xef")->assert_equal(3)
2076+
2077+
def TestIdx(k: number, v: dict<any>): bool
2078+
return v.color == 'blue'
2079+
enddef
2080+
indexof(l, TestIdx)->assert_equal(1)
20742081
enddef
20752082

20762083
def Test_input()

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,8 @@ static char *(features[]) =
735735

736736
static int included_patches[] =
737737
{ /* Add new patch number below this line */
738+
/**/
739+
202,
738740
/**/
739741
201,
740742
/**/

0 commit comments

Comments
 (0)