Skip to content

Commit

Permalink
Define JSON_INT_MAX
Browse files Browse the repository at this point in the history
Either it has been defined explictly, or we attempt to calculate it
using the method reverted by fcfa748.

See discussion in json-parser#84 and json-parser#102.
  • Loading branch information
DimitriPapadopoulos committed Aug 29, 2021
1 parent 31a2c76 commit 72a3353
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
26 changes: 17 additions & 9 deletions json.c
Expand Up @@ -33,7 +33,6 @@
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdint.h>
#endif

const struct _json_value json_value_none;
Expand All @@ -46,14 +45,23 @@ const struct _json_value json_value_none;

typedef unsigned int json_uchar;

/* There has to be a better way to do this */
static const json_int_t JSON_INT_MAX = sizeof(json_int_t) == 1
? INT8_MAX
: (sizeof(json_int_t) == 2
? INT16_MAX
: (sizeof(json_int_t) == 4
? INT32_MAX
: INT64_MAX));
#ifndef JSON__USER_DEFINED_JSON_INT_T
#if defined(_MSC_VER)
/* https://docs.microsoft.com/en-us/cpp/cpp/data-type-ranges */
#define JSON_INT_MAX 9223372036854775807LL
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
/* C99 */
#define JSON_INT_MAX INT_FAST64_MAX
#else
/* C89 */
#include <limits.h>
#define JSON_INT_MAX LONG_MAX
#endif
#endif

#ifndef JSON_INT_MAX
#define JSON_INT_MAX (json_int_t)(((unsigned json_int_t)(-1)) / (unsigned json_int_t)2);
#endif

static unsigned char hex_value (json_char c)
{
Expand Down
3 changes: 3 additions & 0 deletions json.h
Expand Up @@ -36,6 +36,7 @@
#endif

#ifndef json_int_t
#undef JSON__USER_DEFINED_JSON_INT_T
#if defined(_MSC_VER)
#define json_int_t __int64
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
Expand All @@ -46,6 +47,8 @@
/* C89 */
#define json_int_t long
#endif
#else
#define JSON__USER_DEFINED_JSON_INT_T 1
#endif

#include <stddef.h>
Expand Down

0 comments on commit 72a3353

Please sign in to comment.