]> granicus.if.org Git - vim/commitdiff
patch 8.2.2948: substitute() accepts a number but not a float expression v8.2.2948
authorBram Moolenaar <Bram@vim.org>
Sun, 6 Jun 2021 10:33:49 +0000 (12:33 +0200)
committerBram Moolenaar <Bram@vim.org>
Sun, 6 Jun 2021 10:33:49 +0000 (12:33 +0200)
Problem:    Substitute() accepts a number but not a float expression.
Solution:   Also accept a float. (closes #8331)

src/testdir/test_substitute.vim
src/typval.c
src/version.c

index 4c99f818706953912b378a04e62f576dff3caeff..0c367485b865e16ae27910afbb514ddfcc20a7d6 100644 (file)
@@ -1,6 +1,7 @@
 " Tests for multi-line regexps with ":s".
 
 source shared.vim
+source check.vim
 
 func Test_multiline_subst()
   enew!
@@ -453,6 +454,13 @@ func Test_substitute_partial()
    call assert_fails("call substitute('123', '2', Replacer, 'g')", 'E118:')
 endfunc
 
+func Test_substitute_float()
+  CheckFeature float
+
+  call assert_equal('number 1.23', substitute('number ', '$', { -> 1.23 }, ''))
+  vim9 assert_equal('number 1.23', substitute('number ', '$', () => 1.23, ''))
+endfunc
+
 " Tests for *sub-replace-special* and *sub-replace-expression* on :substitute.
 
 " Execute a list of :substitute command tests
index f08d1aa42151d791faaac099a903f87c549bc57d..a17dbef7efaa8cc40f4af334dc461f195564df9b 100644 (file)
@@ -462,8 +462,13 @@ tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
            break;
        case VAR_FLOAT:
 #ifdef FEAT_FLOAT
-           emsg(_(e_float_as_string));
-           break;
+           if (strict)
+           {
+               emsg(_(e_float_as_string));
+               break;
+           }
+           vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
+           return buf;
 #endif
        case VAR_STRING:
            if (varp->vval.v_string != NULL)
index 7bc2afcec872e303d77a83506bb9a94fd3a004a1..b9df68801611a09138fdb09aac567fada774ca2e 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2948,
 /**/
     2947,
 /**/