MCPcopy Index your code
hub / github.com/armink/struct2json / print_number

Function print_number

struct2json/src/cJSON.c:147–176  ·  view source on GitHub ↗

Render the number nicely from the given item into a string. */

Source from the content-addressed store, hash-verified

145
146/* Render the number nicely from the given item into a string. */
147static char *print_number(cJSON *item,printbuffer *p)
148{
149 char *str=0;
150 double d=item->valuedouble;
151 if (d==0)
152 {
153 if (p) str=ensure(p,2);
154 else str=(char*)cJSON_malloc(2); /* special case for 0. */
155 if (str) strcpy(str,"0");
156 }
157 else if (fabs(((double)item->valueint)-d)<=DBL_EPSILON && d<=INT_MAX && d>=INT_MIN)
158 {
159 if (p) str=ensure(p,21);
160 else str=(char*)cJSON_malloc(21); /* 2^64+1 can be represented in 21 chars. */
161 if (str) sprintf(str,"%d",item->valueint);
162 }
163 else
164 {
165 if (p) str=ensure(p,64);
166 else str=(char*)cJSON_malloc(64); /* This is a nice tradeoff. */
167 if (str)
168 {
169 if (fpclassify(d) != FP_ZERO && !isnormal(d)) sprintf(str,"null");
170 else if (fabs(floor(d)-d)<=DBL_EPSILON && fabs(d)<1.0e60) sprintf(str,"%.0f",d);
171 else if (fabs(d)<1.0e-6 || fabs(d)>1.0e9) sprintf(str,"%e",d);
172 else sprintf(str,"%f",d);
173 }
174 }
175 return str;
176}
177
178static unsigned parse_hex4(const char *str)
179{

Callers 1

print_valueFunction · 0.85

Calls 1

ensureFunction · 0.85

Tested by

no test coverage detected