patch 9.0.0196: finding value in list may require a for loop
Problem: Finding value in list may require a for loop.
Solution: Add indexof(). (Yegappan Lakshmanan, closes #10903)
diff --git a/src/testdir/test_blob.vim b/src/testdir/test_blob.vim
index ff4186a..46f2d61 100644
--- a/src/testdir/test_blob.vim
+++ b/src/testdir/test_blob.vim
@@ -764,4 +764,30 @@
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 1a6a1d0..2c4e5c7 100644
--- a/src/testdir/test_listdict.vim
+++ b/src/testdir/test_listdict.vim
@@ -1446,4 +1446,49 @@
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 b8e2ca7..212e9f1 100644
--- a/src/testdir/test_vim9_builtin.vim
+++ b/src/testdir/test_vim9_builtin.vim
@@ -2066,6 +2066,13 @@
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'])