Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jv: Add some support for 64 bit ints in a very conservative way #1246

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/jv.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,27 @@ static void jvp_invalid_free(jv x) {
* Numbers
*/

jv jv_number(double x) {
jv j = {JV_KIND_NUMBER, 0, 0, 0, {.number = x}};
jv jv_number_full(double x, int64_t y) {
jv j = {JV_KIND_NUMBER, 0, 0, 0, {.number.dbl = x, .number.int64 = y}};
return j;
}

jv jv_number(double x) {
int64_t y = 0;
if(x == x && x < INT_MAX && x > INT_MIN && x == (int)x) {
y = x;
}
return jv_number_full(x, y);
}

double jv_number_value(jv j) {
assert(jv_get_kind(j) == JV_KIND_NUMBER);
return j.u.number;
return j.u.number.dbl;
}

int64_t jv_number_value_int64(jv j) {
assert(jv_get_kind(j) == JV_KIND_NUMBER);
return j.u.number.int64;
}

int jv_is_integer(jv j){
Expand Down
7 changes: 6 additions & 1 deletion src/jv.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ typedef struct {
int size;
union {
struct jv_refcnt* ptr;
double number;
struct {
double dbl;
int64_t int64;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not desirable, unfortunately, as it increases the sizeof(jv).

What I would recommend instead is a new jv kind flag that represents a signed 64-bit integer. Then add jv_int64() (creates a jv from an int64_t), jv_int64_value() (returns an int64_t given a numeric jv). For backwards compatibility I'd have jv_get_kind() return JV_KIND_NUMBER, and then add a jv_get_number_kind() that reports the numeric kind. jv_number_value() and jv_int64_value() should both accept numbers of any kind, and do the appropriate conversion.

This would allow us to also add uint64_t as well.

All arithmetic and math functions would continue to operate on doubles, though perhaps the arithmetic operations could eventually be made to operate on integers (falling back on doubles in the event of overflow/underflow).

} number;
} u;
} jv;

Expand Down Expand Up @@ -60,8 +63,10 @@ jv jv_true(void);
jv jv_false(void);
jv jv_bool(int);

jv jv_number_full(double, int64_t);
jv jv_number(double);
double jv_number_value(jv);
int64_t jv_number_value_int64(jv);
int jv_is_integer(jv);

jv jv_array(void);
Expand Down
8 changes: 7 additions & 1 deletion src/jv_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include "jv.h"
#include "jv_dtoa.h"
#include "jv_unicode.h"
Expand Down Expand Up @@ -490,7 +491,12 @@ static pfunc check_literal(struct jv_parser* p) {
double d = jvp_strtod(&p->dtoa, p->tokenbuf, &end);
if (end == 0 || *end != 0)
return "Invalid numeric literal";
TRY(value(p, jv_number(d)));
end = 0;
errno = 0;
int64_t i = strtoll(p->tokenbuf, &end, 10);
if (end == 0 || *end != 0 || errno != 0)
i = 0;
TRY(value(p, jv_number_full(d, i)));
}
p->tokenpos = 0;
return 0;
Expand Down
8 changes: 8 additions & 0 deletions src/jv_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdio.h>
#include <float.h>
#include <string.h>
#include <inttypes.h>

#ifdef WIN32
#include <windows.h>
Expand Down Expand Up @@ -176,6 +177,13 @@ static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent, FI
put_str("true", F, S, flags & JV_PRINT_ISATTY);
break;
case JV_KIND_NUMBER: {
int64_t i = jv_number_value_int64(x);
if (i) {
char buf[21];
snprintf(buf, sizeof(buf), "%" PRId64, i);
put_str(buf, F, S, flags & JV_PRINT_ISATTY);
break;
}
double d = jv_number_value(x);
if (d != d) {
// JSON doesn't have NaN, so we'll render it as "null"
Expand Down