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

patch 8.1.0731: JS encoding does not handle negative infinity

Problem:    JS encoding does not handle negative infinity.
Solution:   Add support for negative infinity for JS encoding. (Dominique
            Pelle, closes #3792)
This commit is contained in:
Bram Moolenaar
2019-01-12 14:24:27 +01:00
parent ec9d3001cf
commit 5f6b379ff3
4 changed files with 35 additions and 9 deletions

View File

@@ -5726,7 +5726,8 @@ json_decode({string}) *json_decode()*
"[1, 2, ]" is the same as "[1, 2]". "[1, 2, ]" is the same as "[1, 2]".
- More floating point numbers are recognized, e.g. "1." for - More floating point numbers are recognized, e.g. "1." for
"1.0", or "001.2" for "1.2". Special floating point values "1.0", or "001.2" for "1.2". Special floating point values
"Infinity" and "NaN" (capitalization ignored) are accepted. "Infinity", "-Infinity" and "NaN" (capitalization ignored)
are accepted.
- Leading zeroes in integer numbers are ignored, e.g. "012" - Leading zeroes in integer numbers are ignored, e.g. "012"
for "12" or "-012" for "-12". for "12" or "-012" for "-12".
- Capitalization is ignored in literal names null, true or - Capitalization is ignored in literal names null, true or
@@ -5755,6 +5756,7 @@ json_encode({expr}) *json_encode()*
Float floating point number Float floating point number
Float nan "NaN" Float nan "NaN"
Float inf "Infinity" Float inf "Infinity"
Float -inf "-Infinity"
String in double quotes (possibly null) String in double quotes (possibly null)
Funcref not possible, error Funcref not possible, error
List as an array (possibly null); when List as an array (possibly null); when

View File

@@ -316,7 +316,12 @@ json_encode_item(garray_T *gap, typval_T *val, int copyID, int options)
if (isnan(val->vval.v_float)) if (isnan(val->vval.v_float))
ga_concat(gap, (char_u *)"NaN"); ga_concat(gap, (char_u *)"NaN");
else if (isinf(val->vval.v_float)) else if (isinf(val->vval.v_float))
{
if (val->vval.v_float < 0.0)
ga_concat(gap, (char_u *)"-Infinity");
else
ga_concat(gap, (char_u *)"Infinity"); ga_concat(gap, (char_u *)"Infinity");
}
else else
# endif # endif
{ {
@@ -736,7 +741,7 @@ json_decode_item(js_read_T *reader, typval_T *res, int options)
break; break;
default: default:
if (VIM_ISDIGIT(*p) || *p == '-') if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(p[1])))
{ {
#ifdef FEAT_FLOAT #ifdef FEAT_FLOAT
char_u *sp = p; char_u *sp = p;
@@ -834,6 +839,17 @@ json_decode_item(js_read_T *reader, typval_T *res, int options)
retval = OK; retval = OK;
break; break;
} }
if (STRNICMP((char *)p, "-Infinity", 9) == 0)
{
reader->js_used += 9;
if (cur_item != NULL)
{
cur_item->v_type = VAR_FLOAT;
cur_item->vval.v_float = -INFINITY;
}
retval = OK;
break;
}
if (STRNICMP((char *)p, "Infinity", 8) == 0) if (STRNICMP((char *)p, "Infinity", 8) == 0)
{ {
reader->js_used += 8; reader->js_used += 8;
@@ -851,6 +867,7 @@ json_decode_item(js_read_T *reader, typval_T *res, int options)
if ( if (
(len < 5 && STRNICMP((char *)p, "false", len) == 0) (len < 5 && STRNICMP((char *)p, "false", len) == 0)
#ifdef FEAT_FLOAT #ifdef FEAT_FLOAT
|| (len < 9 && STRNICMP((char *)p, "-Infinity", len) == 0)
|| (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0) || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
|| (len < 3 && STRNICMP((char *)p, "NaN", len) == 0) || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
#endif #endif
@@ -1072,7 +1089,7 @@ json_decode(js_read_T *reader, typval_T *res, int options)
* Return FAIL if the message has a decoding error. * Return FAIL if the message has a decoding error.
* Return MAYBE if the message is truncated, need to read more. * Return MAYBE if the message is truncated, need to read more.
* This only works reliable if the message contains an object, array or * This only works reliable if the message contains an object, array or
* string. A number might be trucated without knowing. * string. A number might be truncated without knowing.
* Does not advance the reader. * Does not advance the reader.
*/ */
int int

View File

@@ -29,8 +29,10 @@ let s:varnr = 1234
if has('float') if has('float')
let s:jsonfl = '12.34' let s:jsonfl = '12.34'
let s:varfl = 12.34 let s:varfl = 12.34
let s:jsoninf = 'Infinity' let s:jsonneginf = '-Infinity'
let s:varinf = 1.0 / 0.0 let s:jsonposinf = 'Infinity'
let s:varneginf = -1.0 / 0.0
let s:varposinf = 1.0 / 0.0
let s:jsonnan = 'NaN' let s:jsonnan = 'NaN'
let s:varnan = 0.0 / 0.0 let s:varnan = 0.0 / 0.0
endif endif
@@ -85,7 +87,8 @@ func Test_json_encode()
call assert_equal(s:jsonnr, json_encode(s:varnr)) call assert_equal(s:jsonnr, json_encode(s:varnr))
if has('float') if has('float')
call assert_equal(s:jsonfl, json_encode(s:varfl)) call assert_equal(s:jsonfl, json_encode(s:varfl))
call assert_equal(s:jsoninf, json_encode(s:varinf)) call assert_equal(s:jsonneginf, json_encode(s:varneginf))
call assert_equal(s:jsonposinf, json_encode(s:varposinf))
call assert_equal(s:jsonnan, json_encode(s:varnan)) call assert_equal(s:jsonnan, json_encode(s:varnan))
endif endif
@@ -202,7 +205,8 @@ func Test_js_encode()
call assert_equal(s:jsonnr, js_encode(s:varnr)) call assert_equal(s:jsonnr, js_encode(s:varnr))
if has('float') if has('float')
call assert_equal(s:jsonfl, js_encode(s:varfl)) call assert_equal(s:jsonfl, js_encode(s:varfl))
call assert_equal(s:jsoninf, js_encode(s:varinf)) call assert_equal(s:jsonneginf, js_encode(s:varneginf))
call assert_equal(s:jsonposinf, js_encode(s:varposinf))
call assert_equal(s:jsonnan, js_encode(s:varnan)) call assert_equal(s:jsonnan, js_encode(s:varnan))
endif endif
@@ -242,7 +246,8 @@ func Test_js_decode()
call assert_equal(s:varnr, js_decode(s:jsonnr)) call assert_equal(s:varnr, js_decode(s:jsonnr))
if has('float') if has('float')
call assert_equal(s:varfl, js_decode(s:jsonfl)) call assert_equal(s:varfl, js_decode(s:jsonfl))
call assert_equal(s:varinf, js_decode(s:jsoninf)) call assert_equal(s:varneginf, js_decode(s:jsonneginf))
call assert_equal(s:varposinf, js_decode(s:jsonposinf))
call assert_true(isnan(js_decode(s:jsonnan))) call assert_true(isnan(js_decode(s:jsonnan)))
endif endif

View File

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