mirror of
https://github.com/DaveGamble/cJSON
synced 2025-04-08 18:27:30 +00:00
optimises for number/string print
git-svn-id: svn://svn.code.sf.net/p/cjson/code@59 e3330c51-1366-4df0-8b21-3ccf24e3d50e
This commit is contained in:
parent
28691956a6
commit
575724a15f
36
cJSON.c
36
cJSON.c
@ -122,7 +122,16 @@ static char *print_number(cJSON *item)
|
|||||||
if (fabs(((double)item->valueint)-d)<=DBL_EPSILON && d<=INT_MAX && d>=INT_MIN)
|
if (fabs(((double)item->valueint)-d)<=DBL_EPSILON && d<=INT_MAX && d>=INT_MIN)
|
||||||
{
|
{
|
||||||
str=(char*)cJSON_malloc(21); /* 2^64+1 can be represented in 21 chars. */
|
str=(char*)cJSON_malloc(21); /* 2^64+1 can be represented in 21 chars. */
|
||||||
if (str) sprintf(str,"%d",item->valueint);
|
if (str)
|
||||||
|
{
|
||||||
|
int i,j,t;
|
||||||
|
|
||||||
|
if (d<0) t=-d; else t=d;
|
||||||
|
for (i=0;t>0;t/=10) str[i++]='0'+(t%10);
|
||||||
|
if (d<0) str[i++]='-';
|
||||||
|
str[i--]=0;
|
||||||
|
for (j=0;j<i;i--,j++) {t=str[j];str[j]=str[i];str[i]=t;} // reverse the string.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -211,10 +220,33 @@ static const char *parse_string(cJSON *item,const char *str)
|
|||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int escapable[256]={ 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1,
|
||||||
|
0,0,1,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
|
||||||
|
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 1,0,0,0,
|
||||||
|
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
|
||||||
|
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
|
||||||
|
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
|
||||||
|
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
|
||||||
|
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0};
|
||||||
|
|
||||||
/* Render the cstring provided to an escaped version that can be printed. */
|
/* Render the cstring provided to an escaped version that can be printed. */
|
||||||
static char *print_string_ptr(const char *str)
|
static char *print_string_ptr(const char *str)
|
||||||
{
|
{
|
||||||
const char *ptr;char *ptr2,*out;int len=0;unsigned char token;
|
const char *ptr;char *ptr2,*out;int len=0,flag=0;unsigned char token;
|
||||||
|
|
||||||
|
ptr=str;while (*ptr) flag|=escapable[*ptr++];
|
||||||
|
if (!flag)
|
||||||
|
{
|
||||||
|
len=ptr-str;
|
||||||
|
out=(char*)cJSON_malloc(len+3);
|
||||||
|
if (!out) return 0;
|
||||||
|
ptr2=out;*ptr2++='\"';
|
||||||
|
strcpy(ptr2,str);
|
||||||
|
ptr2[len]='\"';
|
||||||
|
ptr2[len+1]=0;
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
if (!str) return cJSON_strdup("");
|
if (!str) return cJSON_strdup("");
|
||||||
ptr=str;while ((token=*ptr) && ++len) {if (strchr("\"\\\b\f\n\r\t",token)) len++; else if (token<32) len+=5;ptr++;}
|
ptr=str;while ((token=*ptr) && ++len) {if (strchr("\"\\\b\f\n\r\t",token)) len++; else if (token<32) len+=5;ptr++;}
|
||||||
|
Loading…
Reference in New Issue
Block a user