From d9e7dad13945ce1f4110c37d5388b5c532dec0d3 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 17 Aug 2023 12:37:52 +0800 Subject: [PATCH 1/4] vim-patch:8.2.3818: cannot filter or map characters in a string Problem: Cannot filter or map characters in a string. Solution: Make filter() and map() work on a string. (Naruhiko Nishino, closes vim/vim#9327) https://github.com/vim/vim/commit/c479ce032f5d4d14bab9e479acbf42d758879893 Co-authored-by: rbtnn --- runtime/doc/builtin.txt | 47 +++++++++++--------- runtime/lua/vim/_meta/vimfn.lua | 47 +++++++++++--------- src/nvim/eval.c | 52 +++++++++++++++++++++- src/nvim/eval.lua | 47 +++++++++++--------- test/old/testdir/test_filter_map.vim | 66 +++++++++++++++++++++++++++- 5 files changed, 196 insertions(+), 63 deletions(-) diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt index a51f14aab0..0b24ed8d85 100644 --- a/runtime/doc/builtin.txt +++ b/runtime/doc/builtin.txt @@ -1623,10 +1623,11 @@ filewritable({file}) *filewritable()* directory, and we can write to it, the result is 2. filter({expr1}, {expr2}) *filter()* - {expr1} must be a |List|, |Blob|, or a |Dictionary|. + {expr1} must be a |List|, |String|, |Blob| or |Dictionary|. For each item in {expr1} evaluate {expr2} and when the result - is zero remove the item from the |List| or |Dictionary|. For a - |Blob| each byte is removed. + is zero or false remove the item from the |List| or + |Dictionary|. Similarly for each byte in a |Blob| and each + character in a |String|. {expr2} must be a |string| or |Funcref|. @@ -1662,14 +1663,16 @@ filter({expr1}, {expr2}) *filter()* < If you do not use "val" you can leave it out: >vim call filter(myList, {idx -> idx % 2 == 1}) < - The operation is done in-place. If you want a |List| or - |Dictionary| to remain unmodified make a copy first: >vim + For a |List| and a |Dictionary| the operation is done + in-place. If you want it to remain unmodified make a copy + first: >vim let l = filter(copy(mylist), 'v:val =~ "KEEP"') -< Returns {expr1}, the |List|, |Blob| or |Dictionary| that was - filtered. When an error is encountered while evaluating - {expr2} no further items in {expr1} are processed. When - {expr2} is a Funcref errors inside a function are ignored, +< Returns {expr1}, the |List| or |Dictionary| that was filtered, + or a new |Blob| or |String|. + When an error is encountered while evaluating {expr2} no + further items in {expr1} are processed. + When {expr2} is a Funcref errors inside a function are ignored, unless it was defined with the "abort" flag. finddir({name} [, {path} [, {count}]]) *finddir()* @@ -4062,15 +4065,18 @@ luaeval({expr} [, {expr}]) *luaeval()* to Vim data structures. See |lua-eval| for more details. map({expr1}, {expr2}) *map()* - {expr1} must be a |List|, |Blob| or |Dictionary|. - Replace each item in {expr1} with the result of evaluating - {expr2}. For a |Blob| each byte is replaced. + {expr1} must be a |List|, |String|, |Blob| or |Dictionary|. + When {expr1} is a |List|| or |Dictionary|, replace each + item in {expr1} with the result of evaluating {expr2}. + For a |Blob| each byte is replaced. + For a |String|, each character, including composing + characters, is replaced. If the item type changes you may want to use |mapnew()| to create a new List or Dictionary. - {expr2} must be a |string| or |Funcref|. + {expr2} must be a |String| or |Funcref|. - If {expr2} is a |string|, inside {expr2} |v:val| has the value + If {expr2} is a |String|, inside {expr2} |v:val| has the value of the current item. For a |Dictionary| |v:key| has the key of the current item and for a |List| |v:key| has the index of the current item. For a |Blob| |v:key| has the index of the @@ -4100,14 +4106,15 @@ map({expr1}, {expr2}) *map()* < If you do not use "key" you can use a short name: >vim call map(myDict, {_, val -> 'item: ' .. val}) < - The operation is done in-place. If you want a |List| or - |Dictionary| to remain unmodified make a copy first: >vim + The operation is done in-place for a |List| and |Dictionary|. + If you want it to remain unmodified make a copy first: >vim let tlist = map(copy(mylist), ' v:val .. "\t"') -< Returns {expr1}, the |List|, |Blob| or |Dictionary| that was - filtered. When an error is encountered while evaluating - {expr2} no further items in {expr1} are processed. When - {expr2} is a Funcref errors inside a function are ignored, +< Returns {expr1}, the |List| or |Dictionary| that was filtered, + or a new |Blob| or |String|. + When an error is encountered while evaluating {expr2} no + further items in {expr1} are processed. + When {expr2} is a Funcref errors inside a function are ignored, unless it was defined with the "abort" flag. maparg({name} [, {mode} [, {abbr} [, {dict}]]]) *maparg()* diff --git a/runtime/lua/vim/_meta/vimfn.lua b/runtime/lua/vim/_meta/vimfn.lua index b48e2beb8d..4b90a9e3c0 100644 --- a/runtime/lua/vim/_meta/vimfn.lua +++ b/runtime/lua/vim/_meta/vimfn.lua @@ -2005,10 +2005,11 @@ function vim.fn.filereadable(file) end --- @return 0|1 function vim.fn.filewritable(file) end ---- {expr1} must be a |List|, |Blob|, or a |Dictionary|. +--- {expr1} must be a |List|, |String|, |Blob| or |Dictionary|. --- For each item in {expr1} evaluate {expr2} and when the result ---- is zero remove the item from the |List| or |Dictionary|. For a ---- |Blob| each byte is removed. +--- is zero or false remove the item from the |List| or +--- |Dictionary|. Similarly for each byte in a |Blob| and each +--- character in a |String|. --- --- {expr2} must be a |string| or |Funcref|. --- @@ -2044,14 +2045,16 @@ function vim.fn.filewritable(file) end --- vim --- call filter(myList, {idx -> idx % 2 == 1}) --- < ---- The operation is done in-place. If you want a |List| or ---- |Dictionary| to remain unmodified make a copy first: >vim +--- For a |List| and a |Dictionary| the operation is done +--- in-place. If you want it to remain unmodified make a copy +--- first: >vim --- let l = filter(copy(mylist), 'v:val =~ "KEEP"') --- ---- vim --- call map(myDict, {_, val -> 'item: ' .. val}) --- < ---- The operation is done in-place. If you want a |List| or ---- |Dictionary| to remain unmodified make a copy first: >vim +--- The operation is done in-place for a |List| and |Dictionary|. +--- If you want it to remain unmodified make a copy first: >vim --- let tlist = map(copy(mylist), ' v:val .. "\t"') --- ---- dv_lock, arg_errmsg, TV_TRANSLATE))) { return; } + } else if (argvars[0].v_type == VAR_STRING) { + rettv->v_type = VAR_STRING; + rettv->vval.v_string = NULL; } else { - semsg(_(e_listdictblobarg), ermsg); + semsg(_(e_argument_of_str_must_be_list_string_dictionary_or_blob), ermsg); return; } @@ -5193,6 +5198,51 @@ static void filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap } idx++; } + } else if (argvars[0].v_type == VAR_STRING) { + vimvars[VV_KEY].vv_type = VAR_NUMBER; + + garray_T ga; + ga_init(&ga, (int)sizeof(char), 80); + int len; + for (const char *p = tv_get_string(&argvars[0]); *p != NUL; p += len) { + len = utfc_ptr2len(p); + + typval_T tv = { + .v_type = VAR_STRING, + .v_lock = VAR_UNLOCKED, + .vval.v_string = xstrnsave(p, (size_t)len), + }; + + vimvars[VV_KEY].vv_nr = idx; + typval_T newtv; + if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL + || did_emsg) { + break; + } + if (did_emsg) { + tv_clear(&newtv); + tv_clear(&tv); + break; + } else if (filtermap != FILTERMAP_FILTER) { + if (newtv.v_type != VAR_STRING) { + tv_clear(&newtv); + tv_clear(&tv); + emsg(_(e_stringreq)); + break; + } else { + ga_concat(&ga, newtv.vval.v_string); + } + } else if (!rem) { + ga_concat(&ga, tv.vval.v_string); + } + + tv_clear(&newtv); + tv_clear(&tv); + + idx++; + } + ga_append(&ga, NUL); + rettv->vval.v_string = ga.ga_data; } else { assert(argvars[0].v_type == VAR_LIST); diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index 97b10a46c0..4dbd63d131 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -2539,10 +2539,11 @@ M.funcs = { args = 2, base = 1, desc = [=[ - {expr1} must be a |List|, |Blob|, or a |Dictionary|. + {expr1} must be a |List|, |String|, |Blob| or |Dictionary|. For each item in {expr1} evaluate {expr2} and when the result - is zero remove the item from the |List| or |Dictionary|. For a - |Blob| each byte is removed. + is zero or false remove the item from the |List| or + |Dictionary|. Similarly for each byte in a |Blob| and each + character in a |String|. {expr2} must be a |string| or |Funcref|. @@ -2578,14 +2579,16 @@ M.funcs = { vim call filter(myList, {idx -> idx % 2 == 1}) < - The operation is done in-place. If you want a |List| or - |Dictionary| to remain unmodified make a copy first: >vim + For a |List| and a |Dictionary| the operation is done + in-place. If you want it to remain unmodified make a copy + first: >vim let l = filter(copy(mylist), 'v:val =~ "KEEP"') - vim call map(myDict, {_, val -> 'item: ' .. val}) < - The operation is done in-place. If you want a |List| or - |Dictionary| to remain unmodified make a copy first: >vim + The operation is done in-place for a |List| and |Dictionary|. + If you want it to remain unmodified make a copy first: >vim let tlist = map(copy(mylist), ' v:val .. "\t"') - \" . v:val')", 'E896:') - call assert_fails("let l = filter('abc', '\"> \" . v:val')", 'E896:') call assert_fails("let l = filter([1, 2, 3], '{}')", 'E728:') call assert_fails("let l = filter({'k' : 10}, '{}')", 'E728:') call assert_fails("let l = filter([1, 2], {})", 'E731:') @@ -138,4 +136,68 @@ func Test_mapnew_blob() call assert_equal(0z129956, bout) endfunc +func Test_filter_map_string() + let s = "abc" + + " filter() + call filter(s, '"b" != v:val') + call assert_equal(s, s) + call assert_equal('ac', filter('abc', '"b" != v:val')) + call assert_equal('あいうえお', filter('あxいxうxえxお', '"x" != v:val')) + call assert_equal('あa😊💕💕b💕', filter('あxax😊x💕💕b💕x', '"x" != v:val')) + call assert_equal('xxxx', filter('あxax😊x💕💕b💕x', '"x" == v:val')) + let t = "%),:;>?]}’”†‡…‰,‱‼⁇⁈⁉℃℉,、。〉》」,』】〕〗〙〛,!),.:,;?,]}" + let u = "%):;>?]}’”†‡…‰‱‼⁇⁈⁉℃℉、。〉》」』】〕〗〙〛!),.:;?]}" + call assert_equal(u, filter(t, '"," != v:val')) + call assert_equal('', filter('abc', '0')) + call assert_equal('ac', filter('abc', { i, x -> "b" != x })) + call assert_equal('あいうえお', filter('あxいxうxえxお', { i, x -> "x" != x })) + call assert_equal('', filter('abc', { i, x -> v:false })) + + " map() + call map(s, 'nr2char(char2nr(v:val) + 2)') + call assert_equal(s, s) + call assert_equal('cde', map('abc', 'nr2char(char2nr(v:val) + 2)')) + call assert_equal('[あ][i][う][え][お]', map('あiうえお', '"[" .. v:val .. "]"')) + call assert_equal('[あ][a][😊][,][‱][‼][⁇][⁈][⁉][💕][b][💕][c][💕]', map('あa😊,‱‼⁇⁈⁉💕b💕c💕', '"[" .. v:val .. "]"')) + call assert_equal('', map('abc', '""')) + call assert_equal('cde', map('abc', { i, x -> nr2char(char2nr(x) + 2) })) + call assert_equal('[あ][i][う][え][お]', map('あiうえお', { i, x -> '[' .. x .. ']' })) + call assert_equal('', map('abc', { i, x -> '' })) + + " mapnew() + call mapnew(s, 'nr2char(char2nr(v:val) + 2)') + call assert_equal(s, s) + call assert_equal('cde', mapnew('abc', 'nr2char(char2nr(v:val) + 2)')) + call assert_equal('[あ][i][う][え][お]', mapnew('あiうえお', '"[" .. v:val .. "]"')) + call assert_equal('[あ][a][😊][,][‱][‼][⁇][⁈][⁉][💕][b][💕][c][💕]', mapnew('あa😊,‱‼⁇⁈⁉💕b💕c💕', '"[" .. v:val .. "]"')) + call assert_equal('', mapnew('abc', '""')) + call assert_equal('cde', mapnew('abc', { i, x -> nr2char(char2nr(x) + 2) })) + call assert_equal('[あ][i][う][え][お]', mapnew('あiうえお', { i, x -> '[' .. x .. ']' })) + call assert_equal('', mapnew('abc', { i, x -> '' })) + + " map() and filter() + call assert_equal('[あ][⁈][a][😊][⁉][💕][💕][b][💕]', map(filter('あx⁈ax😊x⁉💕💕b💕x', '"x" != v:val'), '"[" .. v:val .. "]"')) + + " patterns-composing(\Z) + call assert_equal('ॠॠ', filter('ऊॠॡ,ऊॠॡ', {i,x -> x =~ '\Z' .. nr2char(0x0960) })) + call assert_equal('àà', filter('càt,càt', {i,x -> x =~ '\Za' })) + call assert_equal('ÅÅ', filter('Åström,Åström', {i,x -> x =~ '\Z' .. nr2char(0xc5) })) + call assert_equal('öö', filter('Åström,Åström', {i,x -> x =~ '\Z' .. nr2char(0xf6) })) + call assert_equal('ऊ@ॡ', map('ऊॠॡ', {i,x -> x =~ '\Z' .. nr2char(0x0960) ? '@' : x })) + call assert_equal('c@t', map('càt', {i,x -> x =~ '\Za' ? '@' : x })) + call assert_equal('@ström', map('Åström', {i,x -> x =~ '\Z' .. nr2char(0xc5) ? '@' : x })) + call assert_equal('Åstr@m', map('Åström', {i,x -> x =~ '\Z' .. nr2char(0xf6) ? '@' : x })) + + " patterns-composing(\%C) + call assert_equal('ॠॠ', filter('ऊॠॡ,ऊॠॡ', {i,x -> x =~ nr2char(0x0960) .. '\%C' })) + call assert_equal('àà', filter('càt,càt', {i,x -> x =~ 'a' .. '\%C' })) + call assert_equal('ÅÅ', filter('Åström,Åström', {i,x -> x =~ nr2char(0xc5) .. '\%C' })) + call assert_equal('öö', filter('Åström,Åström', {i,x -> x =~ nr2char(0xf6) .. '\%C' })) + call assert_equal('ऊ@ॡ', map('ऊॠॡ', {i,x -> x =~ nr2char(0x0960) .. '\%C' ? '@' : x })) + call assert_equal('c@t', map('càt', {i,x -> x =~ 'a' .. '\%C' ? '@' : x })) + call assert_equal('@ström', map('Åström', {i,x -> x =~ nr2char(0xc5) .. '\%C' ? '@' : x })) + call assert_equal('Åstr@m', map('Åström', {i,x -> x =~ nr2char(0xf6) .. '\%C' ? '@' : x })) +endfunc + " vim: shiftwidth=2 sts=2 expandtab From faef577e90cf17699bd7bc206b0fb64af65334d2 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 17 Aug 2023 12:59:16 +0800 Subject: [PATCH 2/4] vim-patch:8.2.3822: leaking memory in map() and filter(), no string in Vim9 Problem: Leaking memory in map() and filter(), cannot use a string argument in Vim9 script. Solution: Fix the leak, adjust the argument check, also run the tests as Vim9 script. (Yegappan Lakshmanan, closes vim/vim#9354) https://github.com/vim/vim/commit/2d877599ee1cede063ef4abe3a2272e67c116238 Co-authored-by: Bram Moolenaar --- src/nvim/eval.c | 2 +- test/old/testdir/test_filter_map.vim | 128 ++++++++++++++++----------- 2 files changed, 76 insertions(+), 54 deletions(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 4e3638b93f..50f5fa578c 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -5042,7 +5042,7 @@ static void filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap : N_("filter() argument"))); // map() and filter() return the first argument, also on failure. - if (filtermap != FILTERMAP_MAPNEW) { + if (filtermap != FILTERMAP_MAPNEW && argvars[0].v_type != VAR_STRING) { tv_copy(&argvars[0], rettv); } diff --git a/test/old/testdir/test_filter_map.vim b/test/old/testdir/test_filter_map.vim index c86e772d1f..e67243b072 100644 --- a/test/old/testdir/test_filter_map.vim +++ b/test/old/testdir/test_filter_map.vim @@ -1,5 +1,7 @@ " Test filter() and map() +source vim9.vim + " list with expression string func Test_filter_map_list_expr_string() " filter() @@ -136,68 +138,88 @@ func Test_mapnew_blob() call assert_equal(0z129956, bout) endfunc +" Test for using map(), filter() and mapnew() with a string func Test_filter_map_string() - let s = "abc" - " filter() - call filter(s, '"b" != v:val') - call assert_equal(s, s) - call assert_equal('ac', filter('abc', '"b" != v:val')) - call assert_equal('あいうえお', filter('あxいxうxえxお', '"x" != v:val')) - call assert_equal('あa😊💕💕b💕', filter('あxax😊x💕💕b💕x', '"x" != v:val')) - call assert_equal('xxxx', filter('あxax😊x💕💕b💕x', '"x" == v:val')) - let t = "%),:;>?]}’”†‡…‰,‱‼⁇⁈⁉℃℉,、。〉》」,』】〕〗〙〛,!),.:,;?,]}" - let u = "%):;>?]}’”†‡…‰‱‼⁇⁈⁉℃℉、。〉》」』】〕〗〙〛!),.:;?]}" - call assert_equal(u, filter(t, '"," != v:val')) - call assert_equal('', filter('abc', '0')) - call assert_equal('ac', filter('abc', { i, x -> "b" != x })) - call assert_equal('あいうえお', filter('あxいxうxえxお', { i, x -> "x" != x })) - call assert_equal('', filter('abc', { i, x -> v:false })) + let lines =<< trim END + VAR s = "abc" + call filter(s, '"b" != v:val') + call assert_equal(s, s) + call assert_equal('ac', filter('abc', '"b" != v:val')) + call assert_equal('あいうえお', filter('あxいxうxえxお', '"x" != v:val')) + call assert_equal('あa😊💕💕b💕', filter('あxax😊x💕💕b💕x', '"x" != v:val')) + call assert_equal('xxxx', filter('あxax😊x💕💕b💕x', '"x" == v:val')) + VAR t = "%),:;>?]}’”†‡…‰,‱‼⁇⁈⁉℃℉,、。〉》」,』】〕〗〙〛,!),.:,;?,]}" + VAR u = "%):;>?]}’”†‡…‰‱‼⁇⁈⁉℃℉、。〉》」』】〕〗〙〛!),.:;?]}" + call assert_equal(u, filter(t, '"," != v:val')) + call assert_equal('', filter('abc', '0')) + call assert_equal('ac', filter('abc', LSTART i, x LMIDDLE "b" != x LEND)) + call assert_equal('あいうえお', filter('あxいxうxえxお', LSTART i, x LMIDDLE "x" != x LEND)) + call assert_equal('', filter('abc', LSTART i, x LMIDDLE v:false LEND)) + call assert_equal('', filter('', "v:val == 'a'")) + call assert_equal('', filter(v:_null_string, "v:val == 'a'")) + END + call CheckLegacyAndVim9Success(lines) " map() - call map(s, 'nr2char(char2nr(v:val) + 2)') - call assert_equal(s, s) - call assert_equal('cde', map('abc', 'nr2char(char2nr(v:val) + 2)')) - call assert_equal('[あ][i][う][え][お]', map('あiうえお', '"[" .. v:val .. "]"')) - call assert_equal('[あ][a][😊][,][‱][‼][⁇][⁈][⁉][💕][b][💕][c][💕]', map('あa😊,‱‼⁇⁈⁉💕b💕c💕', '"[" .. v:val .. "]"')) - call assert_equal('', map('abc', '""')) - call assert_equal('cde', map('abc', { i, x -> nr2char(char2nr(x) + 2) })) - call assert_equal('[あ][i][う][え][お]', map('あiうえお', { i, x -> '[' .. x .. ']' })) - call assert_equal('', map('abc', { i, x -> '' })) + let lines =<< trim END + VAR s = "abc" + call map(s, 'nr2char(char2nr(v:val) + 2)') + call assert_equal(s, s) + call assert_equal('cde', map('abc', 'nr2char(char2nr(v:val) + 2)')) + call assert_equal('[あ][i][う][え][お]', map('あiうえお', '"[" .. v:val .. "]"')) + call assert_equal('[あ][a][😊][,][‱][‼][⁇][⁈][⁉][💕][b][💕][c][💕]', map('あa😊,‱‼⁇⁈⁉💕b💕c💕', '"[" .. v:val .. "]"')) + call assert_equal('', map('abc', '""')) + call assert_equal('cde', map('abc', LSTART i, x LMIDDLE nr2char(char2nr(x) + 2) LEND)) + call assert_equal('[あ][i][う][え][お]', map('あiうえお', LSTART i, x LMIDDLE '[' .. x .. ']' LEND)) + call assert_equal('', map('abc', LSTART i, x LMIDDLE '' LEND)) + call assert_equal('', map('', "v:val == 'a'")) + call assert_equal('', map(v:_null_string, "v:val == 'a'")) + END + call CheckLegacyAndVim9Success(lines) " mapnew() - call mapnew(s, 'nr2char(char2nr(v:val) + 2)') - call assert_equal(s, s) - call assert_equal('cde', mapnew('abc', 'nr2char(char2nr(v:val) + 2)')) - call assert_equal('[あ][i][う][え][お]', mapnew('あiうえお', '"[" .. v:val .. "]"')) - call assert_equal('[あ][a][😊][,][‱][‼][⁇][⁈][⁉][💕][b][💕][c][💕]', mapnew('あa😊,‱‼⁇⁈⁉💕b💕c💕', '"[" .. v:val .. "]"')) - call assert_equal('', mapnew('abc', '""')) - call assert_equal('cde', mapnew('abc', { i, x -> nr2char(char2nr(x) + 2) })) - call assert_equal('[あ][i][う][え][お]', mapnew('あiうえお', { i, x -> '[' .. x .. ']' })) - call assert_equal('', mapnew('abc', { i, x -> '' })) + let lines =<< trim END + VAR s = "abc" + call mapnew(s, 'nr2char(char2nr(v:val) + 2)') + call assert_equal(s, s) + call assert_equal('cde', mapnew('abc', 'nr2char(char2nr(v:val) + 2)')) + call assert_equal('[あ][i][う][え][お]', mapnew('あiうえお', '"[" .. v:val .. "]"')) + call assert_equal('[あ][a][😊][,][‱][‼][⁇][⁈][⁉][💕][b][💕][c][💕]', mapnew('あa😊,‱‼⁇⁈⁉💕b💕c💕', '"[" .. v:val .. "]"')) + call assert_equal('', mapnew('abc', '""')) + call assert_equal('cde', mapnew('abc', LSTART i, x LMIDDLE nr2char(char2nr(x) + 2) LEND)) + call assert_equal('[あ][i][う][え][お]', mapnew('あiうえお', LSTART i, x LMIDDLE '[' .. x .. ']' LEND)) + call assert_equal('', mapnew('abc', LSTART i, x LMIDDLE '' LEND)) + call assert_equal('', mapnew('', "v:val == 'a'")) + call assert_equal('', mapnew(v:_null_string, "v:val == 'a'")) + END + call CheckLegacyAndVim9Success(lines) - " map() and filter() - call assert_equal('[あ][⁈][a][😊][⁉][💕][💕][b][💕]', map(filter('あx⁈ax😊x⁉💕💕b💕x', '"x" != v:val'), '"[" .. v:val .. "]"')) + let lines =<< trim END + #" map() and filter() + call assert_equal('[あ][⁈][a][😊][⁉][💕][💕][b][💕]', map(filter('あx⁈ax😊x⁉💕💕b💕x', '"x" != v:val'), '"[" .. v:val .. "]"')) - " patterns-composing(\Z) - call assert_equal('ॠॠ', filter('ऊॠॡ,ऊॠॡ', {i,x -> x =~ '\Z' .. nr2char(0x0960) })) - call assert_equal('àà', filter('càt,càt', {i,x -> x =~ '\Za' })) - call assert_equal('ÅÅ', filter('Åström,Åström', {i,x -> x =~ '\Z' .. nr2char(0xc5) })) - call assert_equal('öö', filter('Åström,Åström', {i,x -> x =~ '\Z' .. nr2char(0xf6) })) - call assert_equal('ऊ@ॡ', map('ऊॠॡ', {i,x -> x =~ '\Z' .. nr2char(0x0960) ? '@' : x })) - call assert_equal('c@t', map('càt', {i,x -> x =~ '\Za' ? '@' : x })) - call assert_equal('@ström', map('Åström', {i,x -> x =~ '\Z' .. nr2char(0xc5) ? '@' : x })) - call assert_equal('Åstr@m', map('Åström', {i,x -> x =~ '\Z' .. nr2char(0xf6) ? '@' : x })) + #" patterns-composing(\Z) + call assert_equal('ॠॠ', filter('ऊॠॡ,ऊॠॡ', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0x0960) LEND)) + call assert_equal('àà', filter('càt,càt', LSTART i, x LMIDDLE x =~ '\Za' LEND)) + call assert_equal('ÅÅ', filter('Åström,Åström', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0xc5) LEND)) + call assert_equal('öö', filter('Åström,Åström', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0xf6) LEND)) + call assert_equal('ऊ@ॡ', map('ऊॠॡ', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0x0960) ? '@' : x LEND)) + call assert_equal('c@t', map('càt', LSTART i, x LMIDDLE x =~ '\Za' ? '@' : x LEND)) + call assert_equal('@ström', map('Åström', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0xc5) ? '@' : x LEND)) + call assert_equal('Åstr@m', map('Åström', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0xf6) ? '@' : x LEND)) - " patterns-composing(\%C) - call assert_equal('ॠॠ', filter('ऊॠॡ,ऊॠॡ', {i,x -> x =~ nr2char(0x0960) .. '\%C' })) - call assert_equal('àà', filter('càt,càt', {i,x -> x =~ 'a' .. '\%C' })) - call assert_equal('ÅÅ', filter('Åström,Åström', {i,x -> x =~ nr2char(0xc5) .. '\%C' })) - call assert_equal('öö', filter('Åström,Åström', {i,x -> x =~ nr2char(0xf6) .. '\%C' })) - call assert_equal('ऊ@ॡ', map('ऊॠॡ', {i,x -> x =~ nr2char(0x0960) .. '\%C' ? '@' : x })) - call assert_equal('c@t', map('càt', {i,x -> x =~ 'a' .. '\%C' ? '@' : x })) - call assert_equal('@ström', map('Åström', {i,x -> x =~ nr2char(0xc5) .. '\%C' ? '@' : x })) - call assert_equal('Åstr@m', map('Åström', {i,x -> x =~ nr2char(0xf6) .. '\%C' ? '@' : x })) + #" patterns-composing(\%C) + call assert_equal('ॠॠ', filter('ऊॠॡ,ऊॠॡ', LSTART i, x LMIDDLE x =~ nr2char(0x0960) .. '\%C' LEND)) + call assert_equal('àà', filter('càt,càt', LSTART i, x LMIDDLE x =~ 'a' .. '\%C' LEND)) + call assert_equal('ÅÅ', filter('Åström,Åström', LSTART i, x LMIDDLE x =~ nr2char(0xc5) .. '\%C' LEND)) + call assert_equal('öö', filter('Åström,Åström', LSTART i, x LMIDDLE x =~ nr2char(0xf6) .. '\%C' LEND)) + call assert_equal('ऊ@ॡ', map('ऊॠॡ', LSTART i, x LMIDDLE x =~ nr2char(0x0960) .. '\%C' ? '@' : x LEND)) + call assert_equal('c@t', map('càt', LSTART i, x LMIDDLE x =~ 'a' .. '\%C' ? '@' : x LEND)) + call assert_equal('@ström', map('Åström', LSTART i, x LMIDDLE x =~ nr2char(0xc5) .. '\%C' ? '@' : x LEND)) + call assert_equal('Åstr@m', map('Åström', LSTART i, x LMIDDLE x =~ nr2char(0xf6) .. '\%C' ? '@' : x LEND)) + END + call CheckLegacyAndVim9Success(lines) endfunc " vim: shiftwidth=2 sts=2 expandtab From db64280be5ff866b80ffa80569838688bfd7caa5 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 17 Aug 2023 13:01:45 +0800 Subject: [PATCH 3/4] vim-patch:8.2.3908: cannot use a script-local function for 'foldtext' Problem: Cannot use a script-local function for 'foldtext'. Solution: Expand "s:" and "". (Yegappan Lakshmanan, closes vim/vim#9411) https://github.com/vim/vim/commit/27708e6c7b6f444fd599f3dc5015336b002b874d Cherry-pick test_filter_map.vim change from patch 8.2.3871. Co-authored-by: Yegappan Lakshmanan --- src/nvim/eval.c | 3 --- test/old/testdir/test_blob.vim | 14 ++++++++++++++ test/old/testdir/test_expr.vim | 1 + test/old/testdir/test_filter_map.vim | 2 ++ test/old/testdir/test_listdict.vim | 1 + 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 50f5fa578c..dab0896d75 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -5217,9 +5217,6 @@ static void filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap typval_T newtv; if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL || did_emsg) { - break; - } - if (did_emsg) { tv_clear(&newtv); tv_clear(&tv); break; diff --git a/test/old/testdir/test_blob.vim b/test/old/testdir/test_blob.vim index 920ceb826d..2dae19bcfb 100644 --- a/test/old/testdir/test_blob.vim +++ b/test/old/testdir/test_blob.vim @@ -38,6 +38,7 @@ func Test_blob_create() call assert_fails('VAR b = 0z001122.') call assert_fails('call get("", 1)', 'E896:') call assert_equal(0, len(v:_null_blob)) + call assert_equal(0z, copy(v:_null_blob)) END call CheckLegacyAndVim9Success(lines) endfunc @@ -370,6 +371,14 @@ func Test_blob_add() add(v:_null_blob, 0x22) END call CheckDefExecAndScriptFailure(lines, 'E1131:') + + let lines =<< trim END + let b = 0zDEADBEEF + lockvar b + call add(b, 0) + unlockvar b + END + call CheckScriptFailure(lines, 'E741:') endfunc func Test_blob_empty() @@ -464,6 +473,9 @@ func Test_blob_func_remove() remove(b, 0) END call CheckScriptFailure(lines, 'E741:') + + call assert_fails('echo remove(0z1020, [])', 'E745:') + call assert_fails('echo remove(0z1020, 0, [])', 'E745:') endfunc func Test_blob_read_write() @@ -524,6 +536,7 @@ func Test_blob_filter() call assert_equal(0zADEF, filter(0zDEADBEEF, 'v:key % 2')) END call CheckLegacyAndVim9Success(lines) + call assert_fails('echo filter(0z10, "a10")', 'E121:') endfunc " map() item in blob @@ -539,6 +552,7 @@ func Test_blob_map() call map(0z00, '[9]') END call CheckLegacyAndVim9Failure(lines, 'E978:') + call assert_fails('echo map(0z10, "a10")', 'E121:') endfunc func Test_blob_index() diff --git a/test/old/testdir/test_expr.vim b/test/old/testdir/test_expr.vim index bf1ba240eb..d316e63818 100644 --- a/test/old/testdir/test_expr.vim +++ b/test/old/testdir/test_expr.vim @@ -421,6 +421,7 @@ func Test_printf_misc() call assert_equal('[00000あiう]', printf('[%010.7S]', 'あiう')) call assert_equal('1%', printf('%d%%', 1)) + call assert_notequal('', printf('%p', "abc")) END call CheckLegacyAndVim9Success(lines) diff --git a/test/old/testdir/test_filter_map.vim b/test/old/testdir/test_filter_map.vim index e67243b072..9b1dc93fd4 100644 --- a/test/old/testdir/test_filter_map.vim +++ b/test/old/testdir/test_filter_map.vim @@ -175,6 +175,8 @@ func Test_filter_map_string() call assert_equal('', map('abc', LSTART i, x LMIDDLE '' LEND)) call assert_equal('', map('', "v:val == 'a'")) call assert_equal('', map(v:_null_string, "v:val == 'a'")) + call assert_fails('echo map("abc", "10")', 'E928:') + call assert_fails('echo map("abc", "a10")', 'E121:') END call CheckLegacyAndVim9Success(lines) diff --git a/test/old/testdir/test_listdict.vim b/test/old/testdir/test_listdict.vim index dc155ce0fc..d6309bad1e 100644 --- a/test/old/testdir/test_listdict.vim +++ b/test/old/testdir/test_listdict.vim @@ -1079,6 +1079,7 @@ func Test_listdict_extend() let d = {'a': 'A', 'b': 'B'} call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 'error')", 'E737:') + call assert_fails("call extend(d, {'b': 0}, [])", 'E730:') call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 'xxx')", 'E475:') if has('float') call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 1.2)", 'E475:') From acac1e19d364c90ded6ded8c874f886d9ebbc393 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 17 Aug 2023 13:10:01 +0800 Subject: [PATCH 4/4] vim-patch:8.2.3982: some lines of code not covered by tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: Some lines of code not covered by tests. Solution: Add a few more test cases. (Dominique Pellé, closes vim/vim#9453) https://github.com/vim/vim/commit/8bfa0eb863357c1013024233ebb2e95a0a848002 Co-authored-by: Dominique Pelle --- test/old/testdir/test_filter_map.vim | 6 +++--- test/old/testdir/test_highlight.vim | 3 +++ test/old/testdir/test_search.vim | 2 ++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/test/old/testdir/test_filter_map.vim b/test/old/testdir/test_filter_map.vim index 9b1dc93fd4..3040b7b0d0 100644 --- a/test/old/testdir/test_filter_map.vim +++ b/test/old/testdir/test_filter_map.vim @@ -144,7 +144,7 @@ func Test_filter_map_string() let lines =<< trim END VAR s = "abc" call filter(s, '"b" != v:val') - call assert_equal(s, s) + call assert_equal('abc', s) call assert_equal('ac', filter('abc', '"b" != v:val')) call assert_equal('あいうえお', filter('あxいxうxえxお', '"x" != v:val')) call assert_equal('あa😊💕💕b💕', filter('あxax😊x💕💕b💕x', '"x" != v:val')) @@ -165,7 +165,7 @@ func Test_filter_map_string() let lines =<< trim END VAR s = "abc" call map(s, 'nr2char(char2nr(v:val) + 2)') - call assert_equal(s, s) + call assert_equal('abc', s) call assert_equal('cde', map('abc', 'nr2char(char2nr(v:val) + 2)')) call assert_equal('[あ][i][う][え][お]', map('あiうえお', '"[" .. v:val .. "]"')) call assert_equal('[あ][a][😊][,][‱][‼][⁇][⁈][⁉][💕][b][💕][c][💕]', map('あa😊,‱‼⁇⁈⁉💕b💕c💕', '"[" .. v:val .. "]"')) @@ -184,7 +184,7 @@ func Test_filter_map_string() let lines =<< trim END VAR s = "abc" call mapnew(s, 'nr2char(char2nr(v:val) + 2)') - call assert_equal(s, s) + call assert_equal('abc', s) call assert_equal('cde', mapnew('abc', 'nr2char(char2nr(v:val) + 2)')) call assert_equal('[あ][i][う][え][お]', mapnew('あiうえお', '"[" .. v:val .. "]"')) call assert_equal('[あ][a][😊][,][‱][‼][⁇][⁈][⁉][💕][b][💕][c][💕]', mapnew('あa😊,‱‼⁇⁈⁉💕b💕c💕', '"[" .. v:val .. "]"')) diff --git a/test/old/testdir/test_highlight.vim b/test/old/testdir/test_highlight.vim index 8ed03732c2..dcfd1a350a 100644 --- a/test/old/testdir/test_highlight.vim +++ b/test/old/testdir/test_highlight.vim @@ -864,17 +864,20 @@ func Test_highlight_default_colorscheme_restores_links() let hlTestHiPre = HighlightArgs('TestHi') " Test colorscheme + call assert_equal("\ndefault", execute('colorscheme')) hi clear if exists('syntax_on') syntax reset endif let g:colors_name = 'test' + call assert_equal("\ntest", execute('colorscheme')) hi link TestLink ErrorMsg hi TestHi ctermbg=green " Restore default highlighting colorscheme default " 'default' should work no matter if highlight group was cleared + call assert_equal("\ndefault", execute('colorscheme')) hi def link TestLink Identifier hi def TestHi ctermbg=red let hlTestLinkPost = HighlightArgs('TestLink') diff --git a/test/old/testdir/test_search.vim b/test/old/testdir/test_search.vim index 4a92ae34e4..c8152af4f3 100644 --- a/test/old/testdir/test_search.vim +++ b/test/old/testdir/test_search.vim @@ -1762,6 +1762,8 @@ func Test_invalid_regexp() call assert_fails("call search('\\(')", 'E54:') call assert_fails("call search('\\)')", 'E55:') call assert_fails("call search('\\z\\(\\)')", 'E66:') + call assert_fails("call search('\\z2')", 'E67:') + call assert_fails("call search('\\zx')", 'E867:') call assert_fails("call search('\\%[ab')", 'E69:') call assert_fails("call search('\\%[]')", 'E70:') call assert_fails("call search('\\%9999999999999999999999999999v')", 'E951:')