diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/eval.c | 3 | ||||
-rw-r--r-- | src/nvim/eval.lua | 7 | ||||
-rw-r--r-- | src/nvim/eval/funcs.c | 54 |
3 files changed, 55 insertions, 9 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c index dab0896d75..b73744f21b 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -108,7 +108,7 @@ 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[] +static const 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"; @@ -5206,7 +5206,6 @@ static void filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap 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, diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index 4dbd63d131..62d97a2931 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -7876,9 +7876,9 @@ M.funcs = { tags = { 'E998' }, desc = [=[ {func} is called for every item in {object}, which can be a - |List| or a |Blob|. {func} is called with two arguments: the - result so far and current item. After processing all items - the result is returned. + |String|, |List| or a |Blob|. {func} is called with two arguments: + the result so far and current item. After processing all + items the result is returned. {initial} is the initial result. When omitted, the first item in {object} is used and {func} is first called for the second @@ -7889,6 +7889,7 @@ M.funcs = { echo reduce([1, 3, 5], { acc, val -> acc + val }) echo reduce(['x', 'y'], { acc, val -> acc .. val }, 'a') echo reduce(0z1122, { acc, val -> 2 * acc + val }) + echo reduce('xyz', { acc, val -> acc .. ',' .. val }) < ]=], name = 'reduce', diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index 0506c08b07..bf11fdf029 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -150,8 +150,12 @@ static const char *e_invalwindow = N_("E957: Invalid window number"); static const char e_invalid_submatch_number_nr[] = N_("E935: Invalid submatch number: %d"); static const char *e_reduceempty = N_("E998: Reduce of an empty %s with no initial value"); +static const char e_string_list_or_blob_required[] + = N_("E1098: String, List or Blob required"); static const char e_missing_function_argument[] = N_("E1132: Missing function argument"); +static const char e_string_expected_for_argument_nr[] + = N_("E1253: String expected for argument %d"); /// Dummy va_list for passing to vim_snprintf /// @@ -6168,11 +6172,16 @@ static void f_reverse(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) } /// "reduce(list, { accumulator, element -> value } [, initial])" function +/// "reduce(blob, { accumulator, element -> value } [, initial])" function +/// "reduce(string, { accumulator, element -> value } [, initial])" function static void f_reduce(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) { - if (argvars[0].v_type != VAR_LIST && argvars[0].v_type != VAR_BLOB) { - emsg(_(e_listblobreq)); - return; + const int called_emsg_start = called_emsg; + + if (argvars[0].v_type != VAR_STRING + && argvars[0].v_type != VAR_LIST + && argvars[0].v_type != VAR_BLOB) { + emsg(_(e_string_list_or_blob_required)); } const char *func_name; @@ -6217,7 +6226,6 @@ static void f_reduce(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) if (l != NULL) { const VarLockStatus prev_locked = tv_list_locked(l); - const int called_emsg_start = called_emsg; tv_list_set_lock(l, VAR_FIXED); // disallow the list changing here for (; li != NULL; li = TV_LIST_ITEM_NEXT(l, li)) { @@ -6232,6 +6240,44 @@ static void f_reduce(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) } tv_list_set_lock(l, prev_locked); } + } else if (argvars[0].v_type == VAR_STRING) { + const char *p = tv_get_string(&argvars[0]); + int len; + + if (argvars[2].v_type == VAR_UNKNOWN) { + if (*p == NUL) { + semsg(_(e_reduceempty), "String"); + return; + } + len = utfc_ptr2len(p); + *rettv = (typval_T){ + .v_type = VAR_STRING, + .v_lock = VAR_UNLOCKED, + .vval.v_string = xstrnsave(p, (size_t)len), + }; + p += len; + } else if (argvars[2].v_type != VAR_STRING) { + semsg(_(e_string_expected_for_argument_nr), 3); + return; + } else { + tv_copy(&argvars[2], rettv); + } + + for (; *p != NUL; p += len) { + argv[0] = *rettv; + len = utfc_ptr2len(p); + argv[1] = (typval_T){ + .v_type = VAR_STRING, + .v_lock = VAR_UNLOCKED, + .vval.v_string = xstrnsave(p, (size_t)len), + }; + const int r = call_func(func_name, -1, rettv, 2, argv, &funcexe); + tv_clear(&argv[0]); + tv_clear(&argv[1]); + if (r == FAIL || called_emsg != called_emsg_start) { + break; + } + } } else { const blob_T *const b = argvars[0].vval.v_blob; int i; |