mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
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:
parent
b1d4ef2b42
commit
17d616037d
@ -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)
|
static void f_str2float(typval_T *argvars, typval_T *rettv, FunPtr fptr)
|
||||||
{
|
{
|
||||||
char_u *p = skipwhite((const char_u *)tv_get_string(&argvars[0]));
|
char_u *p = skipwhite((const char_u *)tv_get_string(&argvars[0]));
|
||||||
|
bool isneg = (*p == '-');
|
||||||
|
|
||||||
if (*p == '+') {
|
if (*p == '+' || *p == '-') {
|
||||||
p = skipwhite(p + 1);
|
p = skipwhite(p + 1);
|
||||||
}
|
}
|
||||||
(void)string2float((char *)p, &rettv->vval.v_float);
|
(void)string2float((char *)p, &rettv->vval.v_float);
|
||||||
|
if (isneg) {
|
||||||
|
rettv->vval.v_float *= -1;
|
||||||
|
}
|
||||||
rettv->v_type = VAR_FLOAT;
|
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]));
|
char_u *p = skipwhite((const char_u *)tv_get_string(&argvars[0]));
|
||||||
if (*p == '+') {
|
bool isneg = (*p == '-');
|
||||||
|
if (*p == '+' || *p == '-') {
|
||||||
p = skipwhite(p + 1);
|
p = skipwhite(p + 1);
|
||||||
}
|
}
|
||||||
switch (base) {
|
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);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -11,6 +11,7 @@ source test_feedkeys.vim
|
|||||||
source test_filter_cmd.vim
|
source test_filter_cmd.vim
|
||||||
source test_filter_map.vim
|
source test_filter_map.vim
|
||||||
source test_float_func.vim
|
source test_float_func.vim
|
||||||
|
source test_functions.vim
|
||||||
source test_goto.vim
|
source test_goto.vim
|
||||||
source test_jumps.vim
|
source test_jumps.vim
|
||||||
source test_lambda.vim
|
source test_lambda.vim
|
||||||
|
@ -165,9 +165,22 @@ endfunc
|
|||||||
|
|
||||||
func Test_str2float()
|
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 ')))
|
||||||
|
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.23')))
|
||||||
call assert_equal('1.23', string(str2float('1.23abc')))
|
call assert_equal('1.23', string(str2float('1.23abc')))
|
||||||
call assert_equal('1.0e40', string(str2float('1e40')))
|
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('1e1000')))
|
||||||
call assert_equal("str2float('inf')", string(str2float('inf')))
|
call assert_equal("str2float('inf')", string(str2float('inf')))
|
||||||
call assert_equal("-str2float('inf')", string(str2float('-inf')))
|
call assert_equal("-str2float('inf')", string(str2float('-inf')))
|
||||||
|
@ -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()
|
func Test_setbufvar_options()
|
||||||
" This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
|
" This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
|
||||||
" window layout.
|
" window layout.
|
||||||
|
Loading…
Reference in New Issue
Block a user