Remove '+' and leading zeros from exponents in the encoder

Fixes GH-39.
This commit is contained in:
Petri Lehtinen 2011-11-01 20:49:15 +02:00
parent 92d9b89d59
commit 8484ea3fb2
4 changed files with 25 additions and 4 deletions

View File

@ -76,6 +76,7 @@ int jsonp_strtod(strbuffer_t *strbuffer, double *out)
int jsonp_dtostr(char *buffer, size_t size, double value)
{
int ret;
char *start, *end;
size_t length;
ret = snprintf(buffer, size, "%.17g", value);
@ -95,14 +96,34 @@ int jsonp_dtostr(char *buffer, size_t size, double value)
if(strchr(buffer, '.') == NULL &&
strchr(buffer, 'e') == NULL)
{
if(length + 2 >= size) {
if(length + 3 >= size) {
/* No space to append ".0" */
return -1;
}
buffer[length] = '.';
buffer[length + 1] = '0';
buffer[length + 2] = '\0';
length += 2;
}
/* Remove leading '+' from positive exponent. Also remove leading
zeros from exponents (added by some printf() implementations) */
start = strchr(buffer, 'e');
if(start) {
start++;
end = start + 1;
if(*start == '-')
start++;
while(*end == '0')
end++;
if(end != start) {
memmove(start, end, length - (size_t)(end - buffer));
length -= (size_t)(end - start);
}
}
return (int)length;
}

View File

@ -1 +1 @@
[1e+22]
[1e22]

View File

@ -1 +1 @@
[1.2299999999999999e+47]
[1.2299999999999999e47]

View File

@ -1 +1 @@
[1.23456e+80]
[1.23456e80]