diff src/json.c @ 15446:8ac454818352 v8.1.0731

patch 8.1.0731: JS encoding does not handle negative infinity commit https://github.com/vim/vim/commit/5f6b379ff3e34297d171635933f907ec80ed4f05 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Jan 12 14:24:27 2019 +0100 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)
author Bram Moolenaar <Bram@vim.org>
date Sat, 12 Jan 2019 14:30:06 +0100
parents de63593896b3
children 1d2b5c016f17
line wrap: on
line diff
--- a/src/json.c
+++ b/src/json.c
@@ -316,7 +316,12 @@ json_encode_item(garray_T *gap, typval_T
 	    if (isnan(val->vval.v_float))
 		ga_concat(gap, (char_u *)"NaN");
 	    else if (isinf(val->vval.v_float))
-		ga_concat(gap, (char_u *)"Infinity");
+	    {
+		if (val->vval.v_float < 0.0)
+		    ga_concat(gap, (char_u *)"-Infinity");
+		else
+		    ga_concat(gap, (char_u *)"Infinity");
+	    }
 	    else
 # endif
 	    {
@@ -736,7 +741,7 @@ json_decode_item(js_read_T *reader, typv
 		    break;
 
 		default:
-		    if (VIM_ISDIGIT(*p) || *p == '-')
+		    if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(p[1])))
 		    {
 #ifdef FEAT_FLOAT
 			char_u  *sp = p;
@@ -834,6 +839,17 @@ json_decode_item(js_read_T *reader, typv
 			retval = OK;
 			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)
 		    {
 			reader->js_used += 8;
@@ -851,6 +867,7 @@ json_decode_item(js_read_T *reader, typv
 		    if (
 			    (len < 5 && STRNICMP((char *)p, "false", len) == 0)
 #ifdef FEAT_FLOAT
+			    || (len < 9 && STRNICMP((char *)p, "-Infinity", len) == 0)
 			    || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
 			    || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
 #endif
@@ -1072,7 +1089,7 @@ json_decode(js_read_T *reader, typval_T 
  * Return FAIL if the message has a decoding error.
  * Return MAYBE if the message is truncated, need to read more.
  * 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.
  */
     int