MCPcopy Create free account
hub / github.com/acl-dev/acl / print_string_ptr

Function print_string_ptr

lib_acl/samples/json/json5/cJSON.cpp:252–307  ·  view source on GitHub ↗

Render the cstring provided to an escaped version that can be printed. */

Source from the content-addressed store, hash-verified

250
251/* Render the cstring provided to an escaped version that can be printed. */
252static char *print_string_ptr(const char *str,printbuffer *p)
253{
254 const char *ptr;char *ptr2,*out;int len=0,flag=0;unsigned char token;
255
256 for (ptr=str;*ptr;ptr++) flag|=((*ptr>0 && *ptr<32)||(*ptr=='\"')||(*ptr=='\\'))?1:0;
257 if (!flag)
258 {
259 len=ptr-str;
260 if (p) out=ensure(p,len+3);
261 else out=(char*)cJSON_malloc(len+3);
262 if (!out) return 0;
263 ptr2=out;*ptr2++='\"';
264 strcpy(ptr2,str);
265 ptr2[len]='\"';
266 ptr2[len+1]=0;
267 return out;
268 }
269
270 if (!str)
271 {
272 if (p) out=ensure(p,3);
273 else out=(char*)cJSON_malloc(3);
274 if (!out) return 0;
275 strcpy(out,"\"\"");
276 return out;
277 }
278 ptr=str;while ((token=*ptr) && ++len) {if (strchr("\"\\\b\f\n\r\t",token)) len++; else if (token<32) len+=5;ptr++;}
279
280 if (p) out=ensure(p,len+3);
281 else out=(char*)cJSON_malloc(len+3);
282 if (!out) return 0;
283
284 ptr2=out;ptr=str;
285 *ptr2++='\"';
286 while (*ptr)
287 {
288 if ((unsigned char)*ptr>31 && *ptr!='\"' && *ptr!='\\') *ptr2++=*ptr++;
289 else
290 {
291 *ptr2++='\\';
292 switch (token=*ptr++)
293 {
294 case '\\': *ptr2++='\\'; break;
295 case '\"': *ptr2++='\"'; break;
296 case '\b': *ptr2++='b'; break;
297 case '\f': *ptr2++='f'; break;
298 case '\n': *ptr2++='n'; break;
299 case '\r': *ptr2++='r'; break;
300 case '\t': *ptr2++='t'; break;
301 default: sprintf(ptr2,"u%04x",token);ptr2+=5; break; /* escape and print */
302 }
303 }
304 }
305 *ptr2++='\"';*ptr2++=0;
306 return out;
307}
308/* Invote print_string_ptr (which is useful) on an item. */
309static char *print_string(cJSON *item,printbuffer *p) {return print_string_ptr(item->valuestring,p);}

Callers 2

print_stringFunction · 0.85
print_objectFunction · 0.85

Calls 1

ensureFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…