summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorYegappan Lakshmanan <yegappan@yahoo.com>2022-08-13 13:09:20 +0100
committerBram Moolenaar <Bram@vim.org>2022-08-13 13:09:20 +0100
commitb218655d5a485f5b193fb18d7240837d42b89812 (patch)
tree63cd2df4a3e2f3bbd0cac7bbe6ab637c56c6b6c5 /src
parent9032b9ceb6073288d75386dbcbd9d1982fa24080 (diff)
patch 9.0.0196: finding value in list may require a for loopv9.0.0196
Problem: Finding value in list may require a for loop. Solution: Add indexof(). (Yegappan Lakshmanan, closes #10903)
Diffstat (limited to 'src')
-rw-r--r--src/evalfunc.c134
-rw-r--r--src/testdir/test_blob.vim26
-rw-r--r--src/testdir/test_listdict.vim45
-rw-r--r--src/testdir/test_vim9_builtin.vim7
-rw-r--r--src/version.c2
5 files changed, 214 insertions, 0 deletions
diff --git a/src/evalfunc.c b/src/evalfunc.c
index da2f35e5cf..ce981bfe54 100644
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -79,6 +79,7 @@ static void f_hlID(typval_T *argvars, typval_T *rettv);
static void f_hlexists(typval_T *argvars, typval_T *rettv);
static void f_hostname(typval_T *argvars, typval_T *rettv);
static void f_index(typval_T *argvars, typval_T *rettv);
+static void f_indexof(typval_T *argvars, typval_T *rettv);
static void f_input(typval_T *argvars, typval_T *rettv);
static void f_inputdialog(typval_T *argvars, typval_T *rettv);
static void f_inputlist(typval_T *argvars, typval_T *rettv);
@@ -1037,6 +1038,7 @@ static argcheck_T arg23_get[] = {arg_get1, arg_string_or_nr, NULL};
static argcheck_T arg14_glob[] = {arg_string, arg_bool, arg_bool, arg_bool};
static argcheck_T arg25_globpath[] = {arg_string, arg_string, arg_bool, arg_bool, arg_bool};
static argcheck_T arg24_index[] = {arg_list_or_blob, arg_item_of_prev, arg_number, arg_bool};
+static argcheck_T arg23_index[] = {arg_list_or_blob, arg_filter_func, arg_dict_any};
static argcheck_T arg23_insert[] = {arg_list_or_blob, arg_item_of_prev, arg_number};
static argcheck_T arg1_len[] = {arg_len1};
static argcheck_T arg3_libcall[] = {arg_string, arg_string, arg_string_or_nr};
@@ -1995,6 +1997,8 @@ static funcentry_T global_functions[] =
ret_number, f_indent},
{"index", 2, 4, FEARG_1, arg24_index,
ret_number, f_index},
+ {"indexof", 2, 3, FEARG_1, arg23_index,
+ ret_number, f_indexof},
{"input", 1, 3, FEARG_1, arg3_string,
ret_string, f_input},
{"inputdialog", 1, 3, FEARG_1, arg3_string,
@@ -6789,6 +6793,136 @@ f_index(typval_T *argvars, typval_T *rettv)
}
}
+/*
+ * Evaluate 'expr' with the v:key and v:val arguments and return the result.
+ * The expression is expected to return a boolean value. The caller should set
+ * the VV_KEY and VV_VAL vim variables before calling this function.
+ */
+ static int
+indexof_eval_expr(typval_T *expr)
+{
+ typval_T argv[3];
+ typval_T newtv;
+ varnumber_T found;
+ int error = FALSE;
+
+ argv[0] = *get_vim_var_tv(VV_KEY);
+ argv[1] = *get_vim_var_tv(VV_VAL);
+ newtv.v_type = VAR_UNKNOWN;
+
+ if (eval_expr_typval(expr, argv, 2, &newtv) == FAIL)
+ return FALSE;
+
+ found = tv_get_bool_chk(&newtv, &error);
+
+ return error ? FALSE : found;
+}
+
+/*
+ * "indexof()" function
+ */
+ static void
+f_indexof(typval_T *argvars, typval_T *rettv)
+{
+ list_T *l;
+ listitem_T *item;
+ blob_T *b;
+ long startidx = 0;
+ long idx = 0;
+ typval_T save_val;
+ typval_T save_key;
+ int save_did_emsg;
+
+ rettv->vval.v_number = -1;
+
+ if (check_for_list_or_blob_arg(argvars, 0) == FAIL
+ || check_for_string_or_func_arg(argvars, 1) == FAIL
+ || check_for_opt_dict_arg(argvars, 2) == FAIL)
+ return;
+
+ if ((argvars[1].v_type == VAR_STRING && argvars[1].vval.v_string == NULL)
+ || (argvars[1].v_type == VAR_FUNC
+ && argvars[1].vval.v_partial == NULL))
+ return;
+
+ if (argvars[2].v_type == VAR_DICT)
+ startidx = dict_get_number_def(argvars[2].vval.v_dict, "startidx", 0);
+
+ prepare_vimvar(VV_VAL, &save_val);
+ prepare_vimvar(VV_KEY, &save_key);
+
+ // We reset "did_emsg" to be able to detect whether an error occurred
+ // during evaluation of the expression.
+ save_did_emsg = did_emsg;
+ did_emsg = FALSE;
+
+ if (argvars[0].v_type == VAR_BLOB)
+ {
+ b = argvars[0].vval.v_blob;
+ 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
+ {
+ l = argvars[0].vval.v_list;
+ 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_VAL, &save_val);
+ did_emsg |= save_did_emsg;
+}
+
static int inputsecret_flag = 0;
/*
diff --git a/src/testdir/test_blob.vim b/src/testdir/test_blob.vim
index ff4186a730..46f2d61ad3 100644
--- a/src/testdir/test_blob.vim
+++ b/src/testdir/test_blob.vim
@@ -764,4 +764,30 @@ func Test_blob_alloc_failure()
call assert_equal(0, x)
endfunc
+" Test for the indexof() function
+func Test_indexof()
+ let b = 0zdeadbeef
+ call assert_equal(0, indexof(b, {i, v -> v == 0xde}))
+ call assert_equal(3, indexof(b, {i, v -> v == 0xef}))
+ call assert_equal(-1, indexof(b, {i, v -> v == 0x1}))
+ call assert_equal(1, indexof(b, "v:val == 0xad"))
+ call assert_equal(-1, indexof(b, "v:val == 0xff"))
+
+ call assert_equal(-1, indexof(0z, "v:val == 0x0"))
+ call assert_equal(-1, indexof(test_null_blob(), "v:val == 0xde"))
+ call assert_equal(-1, indexof(b, test_null_string()))
+ call assert_equal(-1, indexof(b, test_null_function()))
+
+ let b = 0z01020102
+ call assert_equal(1, indexof(b, "v:val == 0x02", #{startidx: 0}))
+ call assert_equal(2, indexof(b, "v:val == 0x01", #{startidx: -2}))
+ call assert_equal(-1, indexof(b, "v:val == 0x01", #{startidx: 5}))
+ call assert_equal(0, indexof(b, "v:val == 0x01", #{startidx: -5}))
+ call assert_equal(0, indexof(b, "v:val == 0x01", test_null_dict()))
+
+ " failure cases
+ call assert_fails('let i = indexof(b, "val == 0xde")', 'E121:')
+ call assert_fails('let i = indexof(b, {})', 'E1256:')
+endfunc
+
" vim: shiftwidth=2 sts=2 expandtab
diff --git a/src/testdir/test_listdict.vim b/src/testdir/test_listdict.vim
index 1a6a1d045b..2c4e5c722d 100644
--- a/src/testdir/test_listdict.vim
+++ b/src/testdir/test_listdict.vim
@@ -1446,4 +1446,49 @@ func Test_null_dict()
unlockvar d
endfunc
+" Test for the indexof() function
+func Test_indexof()
+ let l = [#{color: 'red'}, #{color: 'blue'}, #{color: 'green'}]
+ call assert_equal(0, indexof(l, {i, v -> v.color == 'red'}))
+ call assert_equal(2, indexof(l, {i, v -> v.color == 'green'}))
+ call assert_equal(-1, indexof(l, {i, v -> v.color == 'grey'}))
+ call assert_equal(1, indexof(l, "v:val.color == 'blue'"))
+ call assert_equal(-1, indexof(l, "v:val.color == 'cyan'"))
+
+ let l = [#{n: 10}, #{n: 10}, #{n: 20}]
+ call assert_equal(0, indexof(l, "v:val.n == 10", #{startidx: 0}))
+ call assert_equal(1, indexof(l, "v:val.n == 10", #{startidx: -2}))
+ call assert_equal(-1, indexof(l, "v:val.n == 10", #{startidx: 4}))
+ call assert_equal(-1, indexof(l, "v:val.n == 10", #{startidx: -4}))
+ call assert_equal(0, indexof(l, "v:val.n == 10", test_null_dict()))
+
+ call assert_equal(-1, indexof([], {i, v -> v == 'a'}))
+ call assert_equal(-1, indexof(test_null_list(), {i, v -> v == 'a'}))
+ call assert_equal(-1, indexof(l, test_null_string()))
+ call assert_equal(-1, indexof(l, test_null_function()))
+
+ " failure cases
+ call assert_fails('let i = indexof(l, "v:val == ''cyan''")', 'E735:')
+ call assert_fails('let i = indexof(l, "color == ''cyan''")', 'E121:')
+ call assert_fails('let i = indexof(l, {})', 'E1256:')
+ call assert_fails('let i = indexof({}, "v:val == 2")', 'E1226:')
+ call assert_fails('let i = indexof([], "v:val == 2", [])', 'E1206:')
+
+ func TestIdx(k, v)
+ return a:v.n == 20
+ endfunc
+ call assert_equal(2, indexof(l, function("TestIdx")))
+ delfunc TestIdx
+ func TestIdx(k, v)
+ return {}
+ endfunc
+ call assert_fails('let i = indexof(l, function("TestIdx"))', 'E728:')
+ delfunc TestIdx
+ func TestIdx(k, v)
+ throw "IdxError"
+ endfunc
+ call assert_fails('let i = indexof(l, function("TestIdx"))', 'E605:')
+ delfunc TestIdx
+endfunc
+
" vim: shiftwidth=2 sts=2 expandtab
diff --git a/src/testdir/test_vim9_builtin.vim b/src/testdir/test_vim9_builtin.vim
index b8e2ca7e38..212e9f11b7 100644
--- a/src/testdir/test_vim9_builtin.vim
+++ b/src/testdir/test_vim9_builtin.vim
@@ -2066,6 +2066,13 @@ def Test_index()
v9.CheckDefAndScriptFailure(['index(0z1020, 10, 1, 2)'], ['E1013: Argument 4: type mismatch, expected bool but got number', 'E1212: Bool required for argument 4'])
enddef
+def Test_indexof()
+ var l = [{color: 'red'}, {color: 'blue'}, {color: 'green'}]
+ indexof(l, (i, v) => v.color == 'green')->assert_equal(2)
+ var b = 0zdeadbeef
+ indexof(b, "v:val == 0xef")->assert_equal(3)
+enddef
+
def Test_input()
v9.CheckDefAndScriptFailure(['input(5)'], ['E1013: Argument 1: type mismatch, expected string but got number', 'E1174: String required for argument 1'])
v9.CheckDefAndScriptFailure(['input(["a"])'], ['E1013: Argument 1: type mismatch, expected string but got list<string>', 'E1174: String required for argument 1'])
diff --git a/src/version.c b/src/version.c
index 72890e46b3..65bbcf7392 100644
--- a/src/version.c
+++ b/src/version.c
@@ -736,6 +736,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 196,
+/**/
195,
/**/
194,