Formatting.

This commit is contained in:
NathanSweet 2014-08-30 20:43:53 +02:00
parent 6dcac8f958
commit bdd90b6854
2 changed files with 57 additions and 65 deletions

View File

@ -1,5 +1,6 @@
/* /*
Copyright (c) 2009 Dave Gamble Copyright (c) 2009, Dave Gamble
Copyright (c) 2013, Esoteric Software
Permission is hereby granted, dispose of charge, to any person obtaining a copy Permission is hereby granted, dispose of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@ -55,19 +56,19 @@ static int Json_strcasecmp (const char* s1, const char* s2) {
/* TODO we may be able to elide these NULL checks if we can prove /* TODO we may be able to elide these NULL checks if we can prove
* the graph and input (only callsite is Json_getItem) should not have NULLs * the graph and input (only callsite is Json_getItem) should not have NULLs
*/ */
if ( s1 && s2 ) if (s1 && s2) {
{
#if defined(_WIN32) #if defined(_WIN32)
return _stricmp( s1, s2 ); return _stricmp(s1, s2);
#else #else
return strcasecmp( s1, s2 ); return strcasecmp( s1, s2 );
#endif #endif
} } else {
else if (s1 < s2)
{ return -1; /* s1 is null, s2 is not */
if ( s1 < s2 ) return -1; /* s1 is null, s2 is not */ else if (s1 == s2)
else if ( s1 == s2 ) return 0; /* both are null */ return 0; /* both are null */
else return 1; /* s2 is nul s1 is not */ else
return 1; /* s2 is nul s1 is not */
} }
} }
@ -101,29 +102,24 @@ static const char* parse_number (Json *item, const char* num) {
* We also already know that this starts with [-0-9] from parse_value. * We also already know that this starts with [-0-9] from parse_value.
*/ */
#if __STDC_VERSION__ >= 199901L #if __STDC_VERSION__ >= 199901L
n = strtof( num, &endptr ); n = strtof(num, &endptr);
#else #else
n = (float)strtod( num, &endptr ); n = (float)strtod( num, &endptr );
#endif #endif
/* ignore errno's ERANGE, which returns +/-HUGE_VAL */ /* ignore errno's ERANGE, which returns +/-HUGE_VAL */
/* n is 0 on any other error */ /* n is 0 on any other error */
if (endptr != num) {
if ( endptr != num )
{
/* Parse success, number found. */ /* Parse success, number found. */
item->valueFloat = n; item->valueFloat = n;
item->valueInt = (int)n; item->valueInt = (int)n;
item->type = Json_Number; item->type = Json_Number;
return endptr; return endptr;
} } else {
else
{
/* Parse failure, ep is set. */ /* Parse failure, ep is set. */
ep = num; ep = num;
return 0; return 0;
} }
} }
/* Parse the input text into an unescaped cstring, and populate item. */ /* Parse the input text into an unescaped cstring, and populate item. */
@ -231,7 +227,7 @@ static const char* parse_object (Json *item, const char* value);
/* Utility to jump whitespace and cr/lf */ /* Utility to jump whitespace and cr/lf */
static const char* skip (const char* in) { static const char* skip (const char* in) {
if ( !in ) return 0; /* must propagate NULL since it's often called in skip(f(...)) form */ if (!in) return 0; /* must propagate NULL since it's often called in skip(f(...)) form */
while (*in && (unsigned char)*in <= 32) while (*in && (unsigned char)*in <= 32)
in++; in++;
return in; return in;
@ -262,54 +258,50 @@ static const char* parse_value (Json *item, const char* value) {
if (!value) return 0; /* Fail on null. */ if (!value) return 0; /* Fail on null. */
#endif #endif
switch ( *value ) switch (*value) {
{ case 'n': {
case 'n': if (!strncmp(value + 1, "ull", 3)) {
{ item->type = Json_NULL;
if (!strncmp(value+1, "ull", 3)) { return value + 4;
item->type = Json_NULL;
return value + 4;
}
break;
} }
case 'f': break;
{ }
if (!strncmp(value+1, "alse", 4)) { case 'f': {
item->type = Json_False; if (!strncmp(value + 1, "alse", 4)) {
/* calloc prevents us needing item->type = Json_False or valueInt = 0 here */ item->type = Json_False;
return value + 5; /* calloc prevents us needing item->type = Json_False or valueInt = 0 here */
} return value + 5;
break;
} }
case 't': break;
{ }
if (!strncmp(value+1, "rue", 3)) { case 't': {
item->type = Json_True; if (!strncmp(value + 1, "rue", 3)) {
item->valueInt = 1; item->type = Json_True;
return value + 4; item->valueInt = 1;
} return value + 4;
break;
} }
case '\"': break;
return parse_string(item, value); }
case '[': case '\"':
return parse_array(item, value); return parse_string(item, value);
case '{': case '[':
return parse_object(item, value); return parse_array(item, value);
case '-': /* fallthrough */ case '{':
case '0': /* fallthrough */ return parse_object(item, value);
case '1': /* fallthrough */ case '-': /* fallthrough */
case '2': /* fallthrough */ case '0': /* fallthrough */
case '3': /* fallthrough */ case '1': /* fallthrough */
case '4': /* fallthrough */ case '2': /* fallthrough */
case '5': /* fallthrough */ case '3': /* fallthrough */
case '6': /* fallthrough */ case '4': /* fallthrough */
case '7': /* fallthrough */ case '5': /* fallthrough */
case '8': /* fallthrough */ case '6': /* fallthrough */
case '9': case '7': /* fallthrough */
return parse_number(item, value); case '8': /* fallthrough */
default: case '9':
break; return parse_number(item, value);
default:
break;
} }
ep = value; ep = value;