0
0
mirror of https://github.com/vim/vim.git synced 2025-09-23 03:43:49 -04:00

patch 8.2.3315: cannot use single quote in a float number for readability

Problem:    Cannot use single quote in a float number for readability.
Solution:   Support single quotes like in numbers. (closes #8713)
This commit is contained in:
Bram Moolenaar
2021-08-08 15:43:34 +02:00
parent 267359902c
commit 2950065e18
7 changed files with 72 additions and 10 deletions

View File

@@ -29,7 +29,8 @@
int
string2float(
char_u *text,
float_T *value) // result stored here
float_T *value, // result stored here
int skip_quotes)
{
char *s = (char *)text;
float_T f;
@@ -50,6 +51,32 @@ string2float(
*value = NAN;
return 3;
}
if (skip_quotes && vim_strchr((char_u *)s, '\'') != NULL)
{
char_u buf[100];
char_u *p = buf;
int quotes = 0;
vim_strncpy(buf, (char_u *)s, 99);
p = buf;
for (;;)
{
// remove single quotes between digits, not in the exponent
if (*p == '\'')
{
++quotes;
mch_memmove(p, p + 1, STRLEN(p));
}
if (!vim_isdigit(*p))
break;
p = skipdigits(p);
}
s = (char *)buf;
f = strtod(s, &s);
*value = f;
return (int)((char_u *)s - buf) + quotes;
}
f = strtod(s, &s);
*value = f;
return (int)((char_u *)s - text);
@@ -488,16 +515,19 @@ f_str2float(typval_T *argvars, typval_T *rettv)
{
char_u *p;
int isneg;
int skip_quotes;
if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
return;
skip_quotes = argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]);
p = skipwhite(tv_get_string_strict(&argvars[0]));
isneg = (*p == '-');
if (*p == '+' || *p == '-')
p = skipwhite(p + 1);
(void)string2float(p, &rettv->vval.v_float);
(void)string2float(p, &rettv->vval.v_float, skip_quotes);
if (isneg)
rettv->vval.v_float *= -1;
rettv->v_type = VAR_FLOAT;

View File

@@ -791,12 +791,13 @@ json_decode_item(js_read_T *reader, typval_T *res, int options)
{
float_T f;
len = string2float(p, &f);
len = string2float(p, &f, FALSE);
}
else
{
cur_item->v_type = VAR_FLOAT;
len = string2float(p, &cur_item->vval.v_float);
len = string2float(p, &cur_item->vval.v_float,
FALSE);
}
}
else

View File

@@ -1,5 +1,5 @@
/* float.c */
int string2float(char_u *text, float_T *value);
int string2float(char_u *text, float_T *value, int skip_quotes);
void f_abs(typval_T *argvars, typval_T *rettv);
void f_acos(typval_T *argvars, typval_T *rettv);
void f_asin(typval_T *argvars, typval_T *rettv);

View File

@@ -239,13 +239,28 @@ func Test_str2float()
call assert_equal('nan', string(str2float('NaN')))
call assert_equal('nan', string(str2float(' nan ')))
call assert_equal(1.2, str2float(1.2))
call assert_equal('123456.789', string(str2float("123'456.789", 1)))
call assert_equal('123456.789', string(str2float("12'34'56.789", 1)))
call assert_equal('123456.789', string(str2float("1'2'3'4'5'6.789", 1)))
call assert_equal('1.0', string(str2float("1''2.3", 1)))
call assert_equal('123456.7', string(str2float("123'456.7'89", 1)))
call assert_equal(1.2, str2float(1.2, 0))
call CheckDefAndScriptFailure2(['str2float(1.2)'], 'E1013: Argument 1: type mismatch, expected string but got float', 'E1174: String required for argument 1')
call assert_fails("call str2float([])", 'E730:')
call assert_fails("call str2float({})", 'E731:')
call assert_fails("call str2float(function('string'))", 'E729:')
endfunc
def Test_float_quotes()
call assert_equal('123456.789', string(123'456.789))
call assert_equal('123456.789', string(12'34'56.789))
call assert_equal('123456.789', string(1'2'3'4'5'6.789))
call assert_fails("echo string(1''2.3)", 'E116:')
call assert_fails("echo string(123'456.7'89)", 'E116:')
enddef
func Test_float2nr()
call assert_equal(1, float2nr(1.234))
call assert_equal(123, float2nr(1.234e2))

View File

@@ -1704,6 +1704,7 @@ eval_number(
int want_string UNUSED)
{
int len;
int skip_quotes = current_sctx.sc_version >= 4;
#ifdef FEAT_FLOAT
char_u *p;
int get_float = FALSE;
@@ -1718,7 +1719,20 @@ eval_number(
if (**arg == '.')
p = *arg;
else
p = skipdigits(*arg + 1);
{
p = *arg + 1;
if (skip_quotes)
for (;;)
{
if (*p == '\'')
++p;
if (!vim_isdigit(*p))
break;
p = skipdigits(p);
}
else
p = skipdigits(p);
}
if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
{
get_float = TRUE;
@@ -1740,7 +1754,7 @@ eval_number(
{
float_T f;
*arg += string2float(*arg, &f);
*arg += string2float(*arg, &f, skip_quotes);
if (evaluate)
{
rettv->v_type = VAR_FLOAT;
@@ -1784,7 +1798,7 @@ eval_number(
varnumber_T n;
// decimal, hex or octal number
vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
vim_str2nr(*arg, NULL, &len, skip_quotes
? STR2NR_NO_OCT + STR2NR_QUOTE
: STR2NR_ALL, &n, NULL, 0, TRUE);
if (len == 0)

View File

@@ -755,6 +755,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
3315,
/**/
3314,
/**/

View File

@@ -1247,7 +1247,7 @@ read_viminfo_varlist(vir_T *virp, int writing)
(int)(tab - virp->vir_line + 1), TRUE);
#ifdef FEAT_FLOAT
else if (type == VAR_FLOAT)
(void)string2float(tab + 1, &tv.vval.v_float);
(void)string2float(tab + 1, &tv.vval.v_float, FALSE);
#endif
else
{