Browse Source

Reformat json_object_double_to_json_string_format() to have consistent spacing.

tags/json-c-0.13-20171207
Eric Haszlakiewicz 8 years ago
parent
commit
5a99e527ff
1 changed files with 34 additions and 30 deletions
  1. +34
    -30
      json_object.c

+ 34
- 30
json_object.c View File

@@ -742,20 +742,24 @@ static int json_object_double_to_json_string_format(struct json_object* jso,
int flags,
const char *format)
{
char buf[128], *p, *q;
int size;
double dummy; /* needed for modf() */
/* Although JSON RFC does not support
NaN or Infinity as numeric values
ECMA 262 section 9.8.1 defines
how to handle these cases as strings */
if(isnan(jso->o.c_double))
size = snprintf(buf, sizeof(buf), "NaN");
else if(isinf(jso->o.c_double))
if(jso->o.c_double > 0)
size = snprintf(buf, sizeof(buf), "Infinity");
else
size = snprintf(buf, sizeof(buf), "-Infinity");
char buf[128], *p, *q;
int size;
double dummy; /* needed for modf() */
/* Although JSON RFC does not support
NaN or Infinity as numeric values
ECMA 262 section 9.8.1 defines
how to handle these cases as strings */
if (isnan(jso->o.c_double))
{
size = snprintf(buf, sizeof(buf), "NaN");
}
else if (isinf(jso->o.c_double))
{
if(jso->o.c_double > 0)
size = snprintf(buf, sizeof(buf), "Infinity");
else
size = snprintf(buf, sizeof(buf), "-Infinity");
}
else
{
const char *std_format = "%.17g";
@@ -782,22 +786,22 @@ static int json_object_double_to_json_string_format(struct json_object* jso,
if (size < 0)
return -1;

p = strchr(buf, ',');
if (p) {
*p = '.';
} else {
p = strchr(buf, '.');
}
if (p && (flags & JSON_C_TO_STRING_NOZERO)) {
/* last useful digit, always keep 1 zero */
p++;
for (q=p ; *q ; q++) {
if (*q!='0') p=q;
}
/* drop trailing zeroes */
*(++p) = 0;
size = p-buf;
}
p = strchr(buf, ',');
if (p)
*p = '.';
else
p = strchr(buf, '.');
if (p && (flags & JSON_C_TO_STRING_NOZERO))
{
/* last useful digit, always keep 1 zero */
p++;
for (q=p ; *q ; q++) {
if (*q!='0') p=q;
}
/* drop trailing zeroes */
*(++p) = 0;
size = p-buf;
}

if (size >= (int)sizeof(buf))
// The standard formats are guaranteed not to overrun the buffer,


Loading…
Cancel
Save