Skip to content

Commit

Permalink
Add configure guards around literal jv_numbers
Browse files Browse the repository at this point in the history
Allow building jq in a mode that doesn't use decnumber for benchmarking
purposes. decnumber support is enabled by default, and this option is
meant to be removed once we're happy with the performance.
  • Loading branch information
wtlangford committed Oct 22, 2019
1 parent 4860ed4 commit 74c44bc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
8 changes: 8 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ EOF
fi
])

dnl Disable decNumber support
AC_ARG_ENABLE([decnum],
AC_HELP_STRING([--disable-decnum], [disable decnum support]))

AS_IF([test "x$enable_decnum" != "xno"],[
AC_DEFINE([USE_DECNUM],1)
])

AM_CONDITIONAL([ENABLE_VALGRIND], [test "x$enable_valgrind" != xno])
AM_CONDITIONAL([ENABLE_ASAN], [test "x$enable_asan" = xyes])
AM_CONDITIONAL([ENABLE_UBSAN], [test "x$enable_ubsan" = xyes])
Expand Down
4 changes: 4 additions & 0 deletions src/jv.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ jv jv_number(double x) {

double jv_number_value(jv j) {
assert(JVP_HAS_KIND(j, JV_KIND_NUMBER));
#ifdef USE_DECNUM
if (JVP_HAS_FLAGS(j, JVP_FLAGS_NUMBER_LITERAL)) {
jvp_literal_number* n = jvp_literal_number_ptr(j);

Expand All @@ -422,8 +423,11 @@ double jv_number_value(jv j) {

return n->num_double;
} else {
#endif
return j.u.number;
#ifdef USE_DECNUM
}
#endif
}

int jv_is_integer(jv j){
Expand Down
9 changes: 9 additions & 0 deletions src/jv_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,11 +501,20 @@ static pfunc check_literal(struct jv_parser* p) {
} else {
// FIXME: better parser
p->tokenbuf[p->tokenpos] = 0;
#ifdef USE_DECNUM
jv number = jv_number_with_literal(p->tokenbuf);
if (jv_get_kind(number) == JV_KIND_INVALID) {
return "Invalid numeric literal";
}
TRY(value(p, number));
#else
char *end = 0;
double d = jvp_strtod(&p->dtoa, p->tokenbuf, &end);
if (end == 0 || *end != 0) {
return "Invalid numeric literal";
}
TRY(value(p, jv_number(d)));
#endif
}
p->tokenpos = 0;
return 0;
Expand Down
4 changes: 4 additions & 0 deletions src/jv_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,12 @@ static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent, FI
if (jvp_number_is_nan(x)) {
jv_dump_term(C, jv_null(), flags, indent, F, S);
} else {
#ifdef USE_DECNUM
const char * literal_data = jv_number_get_literal(x);
if (literal_data) {
put_str(literal_data, F, S, flags & JV_PRINT_ISATTY);
} else {
#endif
double d = jv_number_value(x);
if (d != d) {
// JSON doesn't have NaN, so we'll render it as "null"
Expand All @@ -249,7 +251,9 @@ static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent, FI
put_str(jvp_dtoa_fmt(C, buf, d), F, S, flags & JV_PRINT_ISATTY);
}
}
#ifdef USE_DECNUM
}
#endif
break;
}
case JV_KIND_STRING:
Expand Down

0 comments on commit 74c44bc

Please sign in to comment.