Problem: str2nr() and str2float() do not always work with negative values.
Solution: Be more flexible about handling signs. (LemonBoy, closes #1332)
Add more tests.
test_delete \
test_diffmode \
test_digraph \
+ test_functions \
test_display \
test_ex_undo \
test_execute_func \
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
char_u *p;
varnumber_T n;
int what;
+ int isneg;
if (argvars[1].v_type != VAR_UNKNOWN)
{
}
p = skipwhite(get_tv_string(&argvars[0]));
- if (*p == '+')
+ isneg = (*p == '-');
+ if (*p == '+' || *p == '-')
p = skipwhite(p + 1);
switch (base)
{
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
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
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')))
--- /dev/null
+" 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
static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 167,
/**/
166,
/**/