mirror of
https://github.com/json-c/json-c.git
synced 2026-04-10 07:49:07 +08:00
Reformat json_object_double_to_json_string_format() to have consistent spacing.
This commit is contained in:
@@ -742,20 +742,24 @@ static int json_object_double_to_json_string_format(struct json_object* jso,
|
|||||||
int flags,
|
int flags,
|
||||||
const char *format)
|
const char *format)
|
||||||
{
|
{
|
||||||
char buf[128], *p, *q;
|
char buf[128], *p, *q;
|
||||||
int size;
|
int size;
|
||||||
double dummy; /* needed for modf() */
|
double dummy; /* needed for modf() */
|
||||||
/* Although JSON RFC does not support
|
/* Although JSON RFC does not support
|
||||||
NaN or Infinity as numeric values
|
NaN or Infinity as numeric values
|
||||||
ECMA 262 section 9.8.1 defines
|
ECMA 262 section 9.8.1 defines
|
||||||
how to handle these cases as strings */
|
how to handle these cases as strings */
|
||||||
if(isnan(jso->o.c_double))
|
if (isnan(jso->o.c_double))
|
||||||
size = snprintf(buf, sizeof(buf), "NaN");
|
{
|
||||||
else if(isinf(jso->o.c_double))
|
size = snprintf(buf, sizeof(buf), "NaN");
|
||||||
if(jso->o.c_double > 0)
|
}
|
||||||
size = snprintf(buf, sizeof(buf), "Infinity");
|
else if (isinf(jso->o.c_double))
|
||||||
else
|
{
|
||||||
size = snprintf(buf, sizeof(buf), "-Infinity");
|
if(jso->o.c_double > 0)
|
||||||
|
size = snprintf(buf, sizeof(buf), "Infinity");
|
||||||
|
else
|
||||||
|
size = snprintf(buf, sizeof(buf), "-Infinity");
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
const char *std_format = "%.17g";
|
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)
|
if (size < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
p = strchr(buf, ',');
|
p = strchr(buf, ',');
|
||||||
if (p) {
|
if (p)
|
||||||
*p = '.';
|
*p = '.';
|
||||||
} else {
|
else
|
||||||
p = strchr(buf, '.');
|
p = strchr(buf, '.');
|
||||||
}
|
if (p && (flags & JSON_C_TO_STRING_NOZERO))
|
||||||
if (p && (flags & JSON_C_TO_STRING_NOZERO)) {
|
{
|
||||||
/* last useful digit, always keep 1 zero */
|
/* last useful digit, always keep 1 zero */
|
||||||
p++;
|
p++;
|
||||||
for (q=p ; *q ; q++) {
|
for (q=p ; *q ; q++) {
|
||||||
if (*q!='0') p=q;
|
if (*q!='0') p=q;
|
||||||
}
|
}
|
||||||
/* drop trailing zeroes */
|
/* drop trailing zeroes */
|
||||||
*(++p) = 0;
|
*(++p) = 0;
|
||||||
size = p-buf;
|
size = p-buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (size >= (int)sizeof(buf))
|
if (size >= (int)sizeof(buf))
|
||||||
// The standard formats are guaranteed not to overrun the buffer,
|
// The standard formats are guaranteed not to overrun the buffer,
|
||||||
|
|||||||
Reference in New Issue
Block a user