From d9e7dad13945ce1f4110c37d5388b5c532dec0d3 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 17 Aug 2023 12:37:52 +0800 Subject: 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 --- src/nvim/eval.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- src/nvim/eval.lua | 47 +++++++++++++++++++++++++++-------------------- 2 files changed, 78 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 23ff012a9c..4e3638b93f 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -108,6 +108,8 @@ static const char e_dot_can_only_be_used_on_dictionary_str[] = N_("E1203: Dot can only be used on a dictionary: %s"); static const char e_empty_function_name[] = N_("E1192: Empty function name"); +static char e_argument_of_str_must_be_list_string_dictionary_or_blob[] + = N_("E1250: Argument of %s must be a List, String, Dictionary or Blob"); static char * const namespace_char = "abglstvw"; @@ -5072,8 +5074,11 @@ static void filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap && value_check_lock(d->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"') - Date: Thu, 17 Aug 2023 12:59:16 +0800 Subject: 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 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') 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); } -- cgit From db64280be5ff866b80ffa80569838688bfd7caa5 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 17 Aug 2023 13:01:45 +0800 Subject: 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 --- 1 file changed, 3 deletions(-) (limited to 'src') 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; -- cgit