|
16 | 16 | #include "vim.h" |
17 | 17 |
|
18 | 18 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 19 | + |
| 20 | +#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H) |
| 21 | + /* for isnan() and isinf() */ |
| 22 | +# include <math.h> |
| 23 | +#endif |
| 24 | + |
19 | 25 | static int json_encode_item(garray_T *gap, typval_T *val, int copyID, int options); |
20 | 26 | static int json_decode_item(js_read_T *reader, typval_T *res, int options); |
21 | 27 |
|
@@ -267,8 +273,20 @@ json_encode_item(garray_T *gap, typval_T *val, int copyID, int options) |
267 | 273 |
|
268 | 274 | case VAR_FLOAT: |
269 | 275 | #ifdef FEAT_FLOAT |
270 | | - vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", val->vval.v_float); |
271 | | - ga_concat(gap, numbuf); |
| 276 | +# if defined(HAVE_MATH_H) |
| 277 | + if ((options & JSON_JS) && isnan(val->vval.v_float)) |
| 278 | + ga_concat(gap, (char_u *)"NaN"); |
| 279 | + else if ((options & JSON_JS) && isinf(val->vval.v_float)) |
| 280 | + ga_concat(gap, (char_u *)"Infinity"); |
| 281 | + else if (isnan(val->vval.v_float) || isinf(val->vval.v_float)) |
| 282 | + ga_concat(gap, (char_u *)"null"); |
| 283 | + else |
| 284 | +# endif |
| 285 | + { |
| 286 | + vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", |
| 287 | + val->vval.v_float); |
| 288 | + ga_concat(gap, numbuf); |
| 289 | + } |
272 | 290 | break; |
273 | 291 | #endif |
274 | 292 | case VAR_UNKNOWN: |
@@ -720,9 +738,36 @@ json_decode_item(js_read_T *reader, typval_T *res, int options) |
720 | 738 | } |
721 | 739 | return OK; |
722 | 740 | } |
| 741 | +#ifdef FEAT_FLOAT |
| 742 | + if (STRNICMP((char *)p, "NaN", 3) == 0) |
| 743 | + { |
| 744 | + reader->js_used += 3; |
| 745 | + if (res != NULL) |
| 746 | + { |
| 747 | + res->v_type = VAR_FLOAT; |
| 748 | + res->vval.v_float = 0.0 / 0.0; |
| 749 | + } |
| 750 | + return OK; |
| 751 | + } |
| 752 | + if (STRNICMP((char *)p, "Infinity", 8) == 0) |
| 753 | + { |
| 754 | + reader->js_used += 8; |
| 755 | + if (res != NULL) |
| 756 | + { |
| 757 | + res->v_type = VAR_FLOAT; |
| 758 | + res->vval.v_float = 1.0 / 0.0; |
| 759 | + } |
| 760 | + return OK; |
| 761 | + } |
| 762 | +#endif |
723 | 763 | /* check for truncated name */ |
724 | 764 | len = (int)(reader->js_end - (reader->js_buf + reader->js_used)); |
725 | | - if ((len < 5 && STRNICMP((char *)p, "false", len) == 0) |
| 765 | + if ( |
| 766 | + (len < 5 && STRNICMP((char *)p, "false", len) == 0) |
| 767 | +#ifdef FEAT_FLOAT |
| 768 | + || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0) |
| 769 | + || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0) |
| 770 | +#endif |
726 | 771 | || (len < 4 && (STRNICMP((char *)p, "true", len) == 0 |
727 | 772 | || STRNICMP((char *)p, "null", len) == 0))) |
728 | 773 | return MAYBE; |
|
0 commit comments