]> granicus.if.org Git - vim/commitdiff
patch 8.0.0167: str2nr()/str2float() fail with negative values v8.0.0167
authorBram Moolenaar <Bram@vim.org>
Tue, 10 Jan 2017 15:12:29 +0000 (16:12 +0100)
committerBram Moolenaar <Bram@vim.org>
Tue, 10 Jan 2017 15:12:29 +0000 (16:12 +0100)
Problem:    str2nr() and str2float() do not always work with negative values.
Solution:   Be more flexible about handling signs. (LemonBoy, closes #1332)
            Add more tests.

src/Makefile
src/evalfunc.c
src/testdir/test_alot.vim
src/testdir/test_float_func.vim
src/testdir/test_functions.vim [new file with mode: 0644]
src/version.c

index 7ae3fcbb2345af0c5e959b924e6177fd119fa032..db706728645d2d08c1e196cf34621978f6267128 100644 (file)
@@ -2091,6 +2091,7 @@ test_arglist \
        test_delete \
        test_diffmode \
        test_digraph \
+       test_functions \
        test_display \
        test_ex_undo \
        test_execute_func \
index 74676c2062ec7059a77d451a92403394426b89d1..efa07b1114fb4578eb64f40d2ca0ffaa7f102aa1 100644 (file)
@@ -11066,10 +11066,13 @@ f_sqrt(typval_T *argvars, typval_T *rettv)
 f_str2float(typval_T *argvars, typval_T *rettv)
 {
     char_u *p = skipwhite(get_tv_string(&argvars[0]));
+    int     isneg = (*p == '-');
 
-    if (*p == '+')
+    if (*p == '+' || *p == '-')
        p = skipwhite(p + 1);
     (void)string2float(p, &rettv->vval.v_float);
+    if (isneg)
+       rettv->vval.v_float *= -1;
     rettv->v_type = VAR_FLOAT;
 }
 #endif
@@ -11084,6 +11087,7 @@ f_str2nr(typval_T *argvars, typval_T *rettv)
     char_u     *p;
     varnumber_T        n;
     int                what;
+    int                isneg;
 
     if (argvars[1].v_type != VAR_UNKNOWN)
     {
@@ -11096,7 +11100,8 @@ f_str2nr(typval_T *argvars, typval_T *rettv)
     }
 
     p = skipwhite(get_tv_string(&argvars[0]));
-    if (*p == '+')
+    isneg = (*p == '-');
+    if (*p == '+' || *p == '-')
        p = skipwhite(p + 1);
     switch (base)
     {
@@ -11106,7 +11111,11 @@ f_str2nr(typval_T *argvars, typval_T *rettv)
        default: what = 0;
     }
     vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
-    rettv->vval.v_number = n;
+    if (isneg)
+       rettv->vval.v_number = -n;
+    else
+       rettv->vval.v_number = n;
+
 }
 
 #ifdef HAVE_STRFTIME
index 6e989f6203fa204989c953339ffcab7c0b97840e..d29bc3a58befcac6175a521c2df4c2a941f1cc10 100644 (file)
@@ -18,6 +18,7 @@ source test_filter_cmd.vim
 source test_filter_map.vim
 source test_float_func.vim
 source test_fnamemodify.vim
+source test_functions.vim
 source test_glob2regpat.vim
 source test_goto.vim
 source test_help_tagjump.vim
index e51b83eff2d823dd60d469c99dc1d0361b979ee0..981d821b0705c7640398037f2ba4978be4f5f637 100644 (file)
@@ -165,9 +165,22 @@ endfunc
 
 func Test_str2float()
   call assert_equal('1.0', string(str2float('1')))
+  call assert_equal('1.0', string(str2float(' 1 ')))
+  call assert_equal('1.0', string(str2float(' 1.0 ')))
   call assert_equal('1.23', string(str2float('1.23')))
   call assert_equal('1.23', string(str2float('1.23abc')))
   call assert_equal('1.0e40', string(str2float('1e40')))
+
+  call assert_equal('1.0', string(str2float('+1')))
+  call assert_equal('1.0', string(str2float('+1')))
+  call assert_equal('1.0', string(str2float(' +1 ')))
+  call assert_equal('1.0', string(str2float(' + 1 ')))
+
+  call assert_equal('-1.0', string(str2float('-1')))
+  call assert_equal('-1.0', string(str2float('-1')))
+  call assert_equal('-1.0', string(str2float(' -1 ')))
+  call assert_equal('-1.0', string(str2float(' - 1 ')))
+
   call assert_equal('inf', string(str2float('1e1000')))
   call assert_equal('inf', string(str2float('inf')))
   call assert_equal('-inf', string(str2float('-inf')))
diff --git a/src/testdir/test_functions.vim b/src/testdir/test_functions.vim
new file mode 100644 (file)
index 0000000..ec816d1
--- /dev/null
@@ -0,0 +1,18 @@
+" Tests for various functions.
+
+func Test_str2nr()
+  call assert_equal(0, str2nr(''))
+  call assert_equal(1, str2nr('1'))
+  call assert_equal(1, str2nr(' 1 '))
+
+  call assert_equal(1, str2nr('+1'))
+  call assert_equal(1, str2nr('+ 1'))
+  call assert_equal(1, str2nr(' + 1 '))
+
+  call assert_equal(-1, str2nr('-1'))
+  call assert_equal(-1, str2nr('- 1'))
+  call assert_equal(-1, str2nr(' - 1 '))
+
+  call assert_equal(123456789, str2nr('123456789'))
+  call assert_equal(-123456789, str2nr('-123456789'))
+endfunc
index f5327cc34851e6be3d3239362bb368c51c8574ce..c74052d7881cfea0930d4dffc32c34b84ed4a74b 100644 (file)
@@ -764,6 +764,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    167,
 /**/
     166,
 /**/