MCPcopy Create free account
hub / github.com/Tencent/TAD_Sim / print_string_ptr

Function print_string_ptr

common/map_sdk/transmission/src/cJSON.cpp:271–331  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

269
270/* Render the cstring provided to an escaped version that can be printed. */
271static char *print_string_ptr(const char *str) {
272 const char *ptr;
273 char *ptr2, *out;
274 int len = 0;
275 unsigned char token;
276
277 if (!str) return cJSON_strdup("");
278 ptr = str;
279 while ((token = *ptr) && ++len) {
280 if (strchr("\"\\\b\f\n\r\t", token)) {
281 len++;
282 } else if (token < 32) {
283 len += 5;
284 }
285 ptr++;
286 }
287
288 out = reinterpret_cast<char *>(cJSON_malloc(len + 3));
289 if (!out) return 0;
290
291 ptr2 = out;
292 ptr = str;
293 *ptr2++ = '\"';
294 while (*ptr) {
295 if ((unsigned char)*ptr > 31 && *ptr != '\"' && *ptr != '\\') {
296 *ptr2++ = *ptr++;
297 } else {
298 *ptr2++ = '\\';
299 switch (token = *ptr++) {
300 case '\\':
301 *ptr2++ = '\\';
302 break;
303 case '\"':
304 *ptr2++ = '\"';
305 break;
306 case '\b':
307 *ptr2++ = 'b';
308 break;
309 case '\f':
310 *ptr2++ = 'f';
311 break;
312 case '\n':
313 *ptr2++ = 'n';
314 break;
315 case '\r':
316 *ptr2++ = 'r';
317 break;
318 case '\t':
319 *ptr2++ = 't';
320 break;
321 default:
322 sprintf(ptr2, "u%04x", token);
323 ptr2 += 5;
324 break; /* escape and print */
325 }
326 }
327 }
328 *ptr2++ = '\"';

Callers 2

print_stringFunction · 0.85
print_objectFunction · 0.85

Calls 1

cJSON_strdupFunction · 0.85

Tested by

no test coverage detected