MCPcopy Create free account
hub / github.com/Bwar/CJsonObject / print_string_ptr

Function print_string_ptr

cJSON.c:339–405  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

337
338/* Render the cstring provided to an escaped version that can be printed. */
339static char *print_string_ptr(const char *str)
340{
341 const char *ptr;
342 char *ptr2, *out;
343 int len = 0;
344 unsigned char token;
345
346 if (!str)
347 return cJSON_strdup("");
348 ptr = str;
349 while ((token = *ptr) && ++len)
350 {
351 if (strchr("\"\\\b\f\n\r\t", token))
352 len++;
353 else if (token < 32)
354 len += 5;
355 ptr++;
356 }
357
358 out = (char*) cJSON_malloc(len + 3);
359 if (!out)
360 return 0;
361
362 ptr2 = out;
363 ptr = str;
364 *ptr2++ = '\"';
365 while (*ptr)
366 {
367 if ((unsigned char) *ptr > 31 && *ptr != '\"' && *ptr != '\\')
368 *ptr2++ = *ptr++;
369 else
370 {
371 *ptr2++ = '\\';
372 switch (token = *ptr++)
373 {
374 case '\\':
375 *ptr2++ = '\\';
376 break;
377 case '\"':
378 *ptr2++ = '\"';
379 break;
380 case '\b':
381 *ptr2++ = 'b';
382 break;
383 case '\f':
384 *ptr2++ = 'f';
385 break;
386 case '\n':
387 *ptr2++ = 'n';
388 break;
389 case '\r':
390 *ptr2++ = 'r';
391 break;
392 case '\t':
393 *ptr2++ = 't';
394 break;
395 default:
396 sprintf(ptr2, "u%04x", token);

Callers 2

print_stringFunction · 0.85
print_objectFunction · 0.85

Calls 1

cJSON_strdupFunction · 0.85

Tested by

no test coverage detected