vim-patch:8.0.0167

Problem:    str2nr() and str2float() do not always work with negative values.
Solution:   Be more flexible about handling signs. (LemonBoy, closes vim/vim#1332)
            Add more tests.

08243d26d2
This commit is contained in:
James McCoy 2017-06-05 22:39:09 -04:00
parent b1d4ef2b42
commit 17d616037d
No known key found for this signature in database
GPG Key ID: DFE691AE331BA3DB
4 changed files with 45 additions and 3 deletions

View File

@ -15698,11 +15698,15 @@ static void f_split(typval_T *argvars, typval_T *rettv, FunPtr fptr)
static void f_str2float(typval_T *argvars, typval_T *rettv, FunPtr fptr)
{
char_u *p = skipwhite((const char_u *)tv_get_string(&argvars[0]));
bool isneg = (*p == '-');
if (*p == '+') {
if (*p == '+' || *p == '-') {
p = skipwhite(p + 1);
}
(void)string2float((char *)p, &rettv->vval.v_float);
if (isneg) {
rettv->vval.v_float *= -1;
}
rettv->v_type = VAR_FLOAT;
}
@ -15722,7 +15726,8 @@ static void f_str2nr(typval_T *argvars, typval_T *rettv, FunPtr fptr)
}
char_u *p = skipwhite((const char_u *)tv_get_string(&argvars[0]));
if (*p == '+') {
bool isneg = (*p == '-');
if (*p == '+' || *p == '-') {
p = skipwhite(p + 1);
}
switch (base) {
@ -15743,7 +15748,11 @@ static void f_str2nr(typval_T *argvars, typval_T *rettv, FunPtr fptr)
}
}
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;
}
}
/*

View File

@ -11,6 +11,7 @@ source test_feedkeys.vim
source test_filter_cmd.vim
source test_filter_map.vim
source test_float_func.vim
source test_functions.vim
source test_goto.vim
source test_jumps.vim
source test_lambda.vim

View 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("str2float('inf')", string(str2float('1e1000')))
call assert_equal("str2float('inf')", string(str2float('inf')))
call assert_equal("-str2float('inf')", string(str2float('-inf')))

View File

@ -1,3 +1,22 @@
" 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
func Test_setbufvar_options()
" This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
" window layout.