Browse Source

Optimize json_parse_int64 when sscanf work as expected

pull/55/head
Remi Collet 12 years ago
parent
commit
952a07d5a0
2 changed files with 24 additions and 0 deletions
  1. +18
    -0
      configure.in
  2. +6
    -0
      json_util.c

+ 18
- 0
configure.in View File

@@ -57,6 +57,24 @@ AC_LANG_POP([C])

AM_PROG_LIBTOOL

AC_MSG_CHECKING([for working sscanf])
AC_TRY_RUN([
#include <stdio.h>
#include <stdint.h>
#include <errno.h>
int main(int argc, char *argv[]){
long i;
errno=0;
return (sscanf("1234567890123456789012345","%ld",&i)==1 && errno==ERANGE && i==INT64_MAX ? 0 : 1);
}],[
AC_MSG_RESULT([yes])
AC_DEFINE(HAS_SSCANF_ERANGE, 1, [Whether correctly return ERANGE])
],[
AC_MSG_RESULT([no, not working])
],[
AC_MSG_RESULT([no, not found])
])

AC_CONFIG_FILES([
Makefile
json.pc


+ 6
- 0
json_util.c View File

@@ -145,15 +145,20 @@ int json_object_to_file(char *filename, struct json_object *obj)
int json_parse_int64(const char *buf, int64_t *retval)
{
int64_t num64;
#ifndef HAS_SSCANF_ERANGE
const char *buf_skip_space;
int orig_has_neg;
int _errno;

errno = 0; // sscanf won't always set errno, so initialize
#endif

if (sscanf(buf, "%" SCNd64, &num64) != 1)
{
MC_DEBUG("Failed to parse, sscanf != 1\n");
return 1;
}
#ifndef HAS_SSCANF_ERANGE
_errno = errno;
buf_skip_space = buf;
orig_has_neg = 0;
@@ -209,6 +214,7 @@ int json_parse_int64(const char *buf, int64_t *retval)
else
num64 = INT64_MAX;
}
#endif
*retval = num64;
return 0;
}


Loading…
Cancel
Save