diff options
author | Bram Moolenaar <Bram@vim.org> | 2022-05-10 18:11:43 +0100 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2022-05-10 18:11:43 +0100 |
commit | 70c41241c2701f26a99085e433925a206ca265a3 (patch) | |
tree | 1afeaeb49dfc47199313bf397955519a5f598572 | |
parent | df6e0e46c55c9c6d788f94482a8858c0f31391f4 (diff) |
patch 8.2.4934: string interpolation fails when not evaluatingv8.2.4934
Problem: String interpolation fails when not evaluating.
Solution: Skip the expression when not evaluating. (closes #10398)
-rw-r--r-- | src/evalvars.c | 22 | ||||
-rw-r--r-- | src/proto/evalvars.pro | 2 | ||||
-rw-r--r-- | src/testdir/test_vim9_expr.vim | 7 | ||||
-rw-r--r-- | src/typval.c | 2 | ||||
-rw-r--r-- | src/version.c | 2 |
5 files changed, 24 insertions, 11 deletions
diff --git a/src/evalvars.c b/src/evalvars.c index d8ee216084..e83c50dff2 100644 --- a/src/evalvars.c +++ b/src/evalvars.c @@ -605,10 +605,11 @@ list_script_vars(int *first) /* * Evaluate one Vim expression {expr} in string "p" and append the * resulting string to "gap". "p" points to the opening "{". + * When "evaluate" is FALSE only skip over the expression. * Return a pointer to the character after "}", NULL for an error. */ char_u * -eval_one_expr_in_str(char_u *p, garray_T *gap) +eval_one_expr_in_str(char_u *p, garray_T *gap, int evaluate) { char_u *block_start = skipwhite(p + 1); // skip the opening { char_u *block_end = block_start; @@ -627,13 +628,16 @@ eval_one_expr_in_str(char_u *p, garray_T *gap) semsg(_(e_missing_close_curly_str), p); return NULL; } - *block_end = NUL; - expr_val = eval_to_string(block_start, TRUE); - *block_end = '}'; - if (expr_val == NULL) - return NULL; - ga_concat(gap, expr_val); - vim_free(expr_val); + if (evaluate) + { + *block_end = NUL; + expr_val = eval_to_string(block_start, TRUE); + *block_end = '}'; + if (expr_val == NULL) + return NULL; + ga_concat(gap, expr_val); + vim_free(expr_val); + } return block_end + 1; } @@ -691,7 +695,7 @@ eval_all_expr_in_str(char_u *str) } // Evaluate the expression and append the result. - p = eval_one_expr_in_str(p, &ga); + p = eval_one_expr_in_str(p, &ga, TRUE); if (p == NULL) { ga_clear(&ga); diff --git a/src/proto/evalvars.pro b/src/proto/evalvars.pro index ce794351dd..4683c15ffa 100644 --- a/src/proto/evalvars.pro +++ b/src/proto/evalvars.pro @@ -13,7 +13,7 @@ list_T *eval_spell_expr(char_u *badword, char_u *expr); int get_spellword(list_T *list, char_u **pp); void prepare_vimvar(int idx, typval_T *save_tv); void restore_vimvar(int idx, typval_T *save_tv); -char_u *eval_one_expr_in_str(char_u *p, garray_T *gap); +char_u *eval_one_expr_in_str(char_u *p, garray_T *gap, int evaluate); char_u *eval_all_expr_in_str(char_u *str); list_T *heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile); void ex_var(exarg_T *eap); diff --git a/src/testdir/test_vim9_expr.vim b/src/testdir/test_vim9_expr.vim index 202bb58e69..bbe39d1b4d 100644 --- a/src/testdir/test_vim9_expr.vim +++ b/src/testdir/test_vim9_expr.vim @@ -2156,6 +2156,13 @@ def Test_expr8_string() ->split($'x{x}x') ->map((_, v: string) => v =~ 'bar') assert_equal([false, true, false], vl) + + # interpolated string in a lambda + lines =<< trim END + assert_equal(['gnome-256color', 'xterm-256color'], ['gnome', 'xterm'] + ->map((_, term: string) => $'{term}-256color')) + END + v9.CheckDefAndScriptSuccess(lines) enddef def Test_expr8_vimvar() diff --git a/src/typval.c b/src/typval.c index 09dc474935..a266330622 100644 --- a/src/typval.c +++ b/src/typval.c @@ -2363,7 +2363,7 @@ eval_interp_string(char_u **arg, typval_T *rettv, int evaluate) ++*arg; break; } - p = eval_one_expr_in_str(*arg, &ga); + p = eval_one_expr_in_str(*arg, &ga, evaluate); if (p == NULL) { ret = FAIL; diff --git a/src/version.c b/src/version.c index cffc703577..80f631bdc3 100644 --- a/src/version.c +++ b/src/version.c @@ -747,6 +747,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 4934, +/**/ 4933, /**/ 4932, |