aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/eval.c
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2023-08-17 12:37:52 +0800
committerzeertzjq <zeertzjq@outlook.com>2023-08-17 13:14:32 +0800
commitd9e7dad13945ce1f4110c37d5388b5c532dec0d3 (patch)
tree0cf385ae282fa00e739b1eb0926b86262b3b555a /src/nvim/eval.c
parent8861f2af72481b1759a6935afa6dce2aae512359 (diff)
downloadrneovim-d9e7dad13945ce1f4110c37d5388b5c532dec0d3.tar.gz
rneovim-d9e7dad13945ce1f4110c37d5388b5c532dec0d3.tar.bz2
rneovim-d9e7dad13945ce1f4110c37d5388b5c532dec0d3.zip
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 <naru123456789@gmail.com>
Diffstat (limited to 'src/nvim/eval.c')
-rw-r--r--src/nvim/eval.c52
1 files changed, 51 insertions, 1 deletions
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);