# HG changeset patch # User Christian Brabandt # Date 1484061304 -3600 # Node ID fff45421182b322d5307f691fba34fc42112506c # Parent 9dc3c9de419823c351906d50bdbc8ab9a8109467 patch 8.0.0167: str2nr()/str2float() fail with negative values commit https://github.com/vim/vim/commit/08243d26d22ad44a857d02c90071578577b8a55d Author: Bram Moolenaar Date: Tue Jan 10 16:12:29 2017 +0100 patch 8.0.0167: str2nr()/str2float() fail with negative values Problem: str2nr() and str2float() do not always work with negative values. Solution: Be more flexible about handling signs. (LemonBoy, closes https://github.com/vim/vim/issues/1332) Add more tests. diff --git a/src/Makefile b/src/Makefile --- a/src/Makefile +++ b/src/Makefile @@ -2091,6 +2091,7 @@ test_arglist \ test_delete \ test_diffmode \ test_digraph \ + test_functions \ test_display \ test_ex_undo \ test_execute_func \ diff --git a/src/evalfunc.c b/src/evalfunc.c --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -11066,10 +11066,13 @@ f_sqrt(typval_T *argvars, typval_T *rett f_str2float(typval_T *argvars, typval_T *rettv) { char_u *p = skipwhite(get_tv_string(&argvars[0])); - - if (*p == '+') + int isneg = (*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 *re 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 *re } 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 *re 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 diff --git a/src/testdir/test_alot.vim b/src/testdir/test_alot.vim --- a/src/testdir/test_alot.vim +++ b/src/testdir/test_alot.vim @@ -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 diff --git a/src/testdir/test_float_func.vim b/src/testdir/test_float_func.vim --- a/src/testdir/test_float_func.vim +++ b/src/testdir/test_float_func.vim @@ -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 --- /dev/null +++ b/src/testdir/test_functions.vim @@ -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 diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -765,6 +765,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 167, +/**/ 166, /**/ 165,