To: vim_dev@googlegroups.com Subject: Patch 8.2.3818 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.3818 Problem: Cannot filter or map characters in a string. Solution: Make filter() and map() work on a string. (Naruhiko Nishino, closes #9327) Files: runtime/doc/eval.txt, src/errors.h, src/list.c, src/testdir/test_filter_map.vim *** ../vim-8.2.3817/runtime/doc/eval.txt 2021-11-20 11:13:53.201671127 +0000 --- runtime/doc/eval.txt 2021-12-15 18:50:35.926868570 +0000 *************** *** 2611,2617 **** feedkeys({string} [, {mode}]) Number add key sequence to typeahead buffer filereadable({file}) Number |TRUE| if {file} is a readable file filewritable({file}) Number |TRUE| if {file} is a writable file ! filter({expr1}, {expr2}) List/Dict remove items from {expr1} where {expr2} is 0 finddir({name} [, {path} [, {count}]]) String find directory {name} in {path} --- 2628,2635 ---- feedkeys({string} [, {mode}]) Number add key sequence to typeahead buffer filereadable({file}) Number |TRUE| if {file} is a readable file filewritable({file}) Number |TRUE| if {file} is a writable file ! filter({expr1}, {expr2}) List/Dict/Blob/String ! remove items from {expr1} where {expr2} is 0 finddir({name} [, {path} [, {count}]]) String find directory {name} in {path} *************** *** 2769,2782 **** log({expr}) Float natural logarithm (base e) of {expr} log10({expr}) Float logarithm of Float {expr} to base 10 luaeval({expr} [, {expr}]) any evaluate |Lua| expression ! map({expr1}, {expr2}) List/Dict change each item in {expr1} to {expr} maparg({name} [, {mode} [, {abbr} [, {dict}]]]) String or Dict rhs of mapping {name} in mode {mode} mapcheck({name} [, {mode} [, {abbr}]]) String check for mappings matching {name} ! mapnew({expr1}, {expr2}) List/Dict like |map()| but creates a new List ! or Dictionary mapset({mode}, {abbr}, {dict}) none restore mapping from |maparg()| result match({expr}, {pat} [, {start} [, {count}]]) Number position where {pat} matches in {expr} --- 2787,2802 ---- log({expr}) Float natural logarithm (base e) of {expr} log10({expr}) Float logarithm of Float {expr} to base 10 luaeval({expr} [, {expr}]) any evaluate |Lua| expression ! map({expr1}, {expr2}) List/Dict/Blob/String ! change each item in {expr1} to {expr2} maparg({name} [, {mode} [, {abbr} [, {dict}]]]) String or Dict rhs of mapping {name} in mode {mode} mapcheck({name} [, {mode} [, {abbr}]]) String check for mappings matching {name} ! mapnew({expr1}, {expr2}) List/Dict/Blob/String ! like |map()| but creates a new List or ! Dictionary mapset({mode}, {abbr}, {dict}) none restore mapping from |maparg()| result match({expr}, {pat} [, {start} [, {count}]]) Number position where {pat} matches in {expr} *************** *** 4848,4862 **** filter({expr1}, {expr2}) *filter()* ! {expr1} must be a |List| or a |Dictionary|. For each item in {expr1} evaluate {expr2} and when the result ! is zero remove the item from the |List| or |Dictionary|. {expr2} must be a |string| or |Funcref|. 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. Examples: > call filter(mylist, 'v:val !~ "OLD"') < Removes the items where "OLD" appears. > --- 4881,4899 ---- filter({expr1}, {expr2}) *filter()* ! {expr1} must be a |List|, |String|, |Blob| or |Dictionary|. For each item in {expr1} evaluate {expr2} and when the result ! is zero or false remove the item from the |List| or ! |Dictionary|. Similarly for each byte in a |Blob| and each ! charactor in a |String|. ! {expr2} must be a |string| or |Funcref|. 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 ! current byte. Examples: > call filter(mylist, 'v:val !~ "OLD"') < Removes the items where "OLD" appears. > *************** *** 4883,4897 **** < If you do not use "val" you can leave it out: > 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: > :let l = filter(copy(mylist), 'v:val =~ "KEEP"') ! < Returns {expr1}, the |List| 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, unless it was ! defined with the "abort" flag. Can also be used as a |method|: > mylist->filter(expr2) --- 4920,4936 ---- < If you do not use "val" you can leave it out: > call filter(myList, {idx -> idx % 2 == 1}) < ! For a |List| and a |Dictionary| the operation is done ! in-place. If you want it to remain unmodified make a copy ! first: > :let l = filter(copy(mylist), 'v:val =~ "KEEP"') ! < 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. Can also be used as a |method|: > mylist->filter(expr2) *************** *** 7496,7514 **** < {only available when compiled with the |+lua| feature} 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. If the item type changes you may want to use |mapnew()| to create a new List or Dictionary. This is required when using Vim9 script. ! {expr2} must be a |string| or |Funcref|. ! 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. Example: > :call map(mylist, '"> " . v:val . " <"') < This puts "> " before and " <" after each item in "mylist". --- 7572,7594 ---- < {only available when compiled with the |+lua| feature} map({expr1}, {expr2}) *map()* ! {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. This is required when using Vim9 script. ! {expr2} must be a |String| or |Funcref|. ! 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 ! current byte. Example: > :call map(mylist, '"> " . v:val . " <"') < This puts "> " before and " <" after each item in "mylist". *************** *** 7534,7547 **** < If you do not use "key" you can use a short name: > 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: > :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, unless it was defined with the "abort" flag. Can also be used as a |method|: > --- 7614,7628 ---- < If you do not use "key" you can use a short name: > call map(myDict, {_, val -> 'item: ' . val}) < ! The operation is done in-place for a |List| and |Dictionary|. ! If you want it to remain unmodified make a copy first: > :let tlist = map(copy(mylist), ' v:val . "\t"') ! < 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. Can also be used as a |method|: > *** ../vim-8.2.3817/src/errors.h 2021-12-07 21:29:13.934441498 +0000 --- src/errors.h 2021-12-15 18:52:18.882725568 +0000 *************** *** 758,760 **** --- 758,762 ---- INIT(= N_("E1248: Closure called from invalid context")); EXTERN char e_highlight_group_name_too_long[] INIT(= N_("E1249: Highlight group name too long")); + EXTERN char e_argument_of_str_must_be_list_string_dictionary_or_blob[] + INIT(= N_("E1250: Argument of %s must be a List, String, Dictionary or Blob")); *** ../vim-8.2.3817/src/list.c 2021-12-13 14:26:40.992627753 +0000 --- src/list.c 2021-12-15 18:53:24.718637830 +0000 *************** *** 2329,2337 **** && value_check_lock(d->dv_lock, arg_errmsg, TRUE))) goto theend; } else { ! semsg(_(e_listdictblobarg), func_name); goto theend; } --- 2329,2343 ---- && value_check_lock(d->dv_lock, arg_errmsg, TRUE))) goto theend; } + else if (argvars[0].v_type == VAR_STRING) + { + rettv->v_type = VAR_STRING; + rettv->vval.v_string = NULL; + } else { ! semsg(_(e_argument_of_str_must_be_list_string_dictionary_or_blob), ! func_name); goto theend; } *************** *** 2480,2485 **** --- 2486,2551 ---- ++idx; } } + else if (argvars[0].v_type == VAR_STRING) + { + char_u *p; + typval_T tv; + garray_T ga; + char_u buf[MB_MAXBYTES + 1]; + int len; + + // set_vim_var_nr() doesn't set the type + set_vim_var_type(VV_KEY, VAR_NUMBER); + + ga_init2(&ga, (int)sizeof(char), 80); + for (p = tv_get_string(&argvars[0]); *p != NUL; p += len) + { + typval_T newtv; + + if (has_mbyte) + len = mb_ptr2len(p); + else + len = 1; + + STRNCPY(buf, p, len); + buf[len] = NUL; + + tv.v_type = VAR_STRING; + tv.vval.v_string = vim_strsave(buf); + + set_vim_var_nr(VV_KEY, idx); + if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL + || did_emsg) + break; + if (did_emsg) + { + clear_tv(&newtv); + clear_tv(&tv); + break; + } + else if (filtermap != FILTERMAP_FILTER) + { + if (newtv.v_type != VAR_STRING) + { + clear_tv(&newtv); + clear_tv(&tv); + emsg(_(e_stringreq)); + break; + } + else + ga_concat(&ga, newtv.vval.v_string); + } + else if (!rem) + ga_concat(&ga, tv.vval.v_string); + + clear_tv(&newtv); + clear_tv(&tv); + + ++idx; + } + ga_append(&ga, NUL); + rettv->vval.v_string = ga.ga_data; + } else // argvars[0].v_type == VAR_LIST { int prev_lock = l->lv_lock; *** ../vim-8.2.3817/src/testdir/test_filter_map.vim 2020-12-01 20:08:01.969599029 +0000 --- src/testdir/test_filter_map.vim 2021-12-15 18:35:45.816194855 +0000 *************** *** 93,100 **** func Test_map_filter_fails() call assert_fails('call map([1], "42 +")', 'E15:') call assert_fails('call filter([1], "42 +")', 'E15:') - call assert_fails("let l = map('abc', '\"> \" . 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:') --- 93,98 ---- *************** *** 145,148 **** --- 143,210 ---- 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 *** ../vim-8.2.3817/src/version.c 2021-12-15 17:53:36.364946447 +0000 --- src/version.c 2021-12-15 18:41:57.155771325 +0000 *************** *** 751,752 **** --- 751,754 ---- { /* Add new patch number below this line */ + /**/ + 3818, /**/ -- Seen it all, done it all, can't remember most of it. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///