MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / cbm_json_escape

Function cbm_json_escape

src/foundation/str_util.c:358–412  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

356}
357
358int cbm_json_escape(char *buf, int bufsize, const char *src) {
359 if (!buf || bufsize <= 0) {
360 return 0;
361 }
362 if (!src) {
363 buf[0] = '\0';
364 return 0;
365 }
366 int pos = 0;
367 int i = 0;
368 for (; src[i] && pos < bufsize - JSON_NUL_RESERVE; i++) {
369 unsigned char c = (unsigned char)src[i];
370 if (c == '"' || c == '\\') {
371 if (pos + JSON_ESC_LEN > bufsize - JSON_NUL_RESERVE) {
372 break;
373 }
374 buf[pos++] = '\\';
375 buf[pos++] = (char)c;
376 } else if (c == '\n') {
377 if (pos + JSON_ESC_LEN > bufsize - JSON_NUL_RESERVE) {
378 break;
379 }
380 buf[pos++] = '\\';
381 buf[pos++] = 'n';
382 } else if (c == '\r') {
383 if (pos + JSON_ESC_LEN > bufsize - JSON_NUL_RESERVE) {
384 break;
385 }
386 buf[pos++] = '\\';
387 buf[pos++] = 'r';
388 } else if (c == '\t') {
389 if (pos + JSON_ESC_LEN > bufsize - JSON_NUL_RESERVE) {
390 break;
391 }
392 buf[pos++] = '\\';
393 buf[pos++] = 't';
394 } else if (c < JSON_CTRL_LIMIT) {
395 /* Other control chars: escape as \u00XX */
396 if (pos + 6 > bufsize - JSON_NUL_RESERVE) {
397 break;
398 }
399 pos += snprintf(buf + pos, 7, "\\u%04x", c);
400 } else {
401 buf[pos++] = (char)c;
402 }
403 }
404 buf[pos] = '\0';
405 /* A truncated output must never end inside a multibyte UTF-8 sequence: a
406 * callee cut mid-character persisted bytes SQLite cannot decode as text
407 * (2026-09-16 probe: nine CALLS rows across rust, java and typescript). */
408 if (src[i] != '\0') {
409 pos = cbm_utf8_trim_partial(buf);
410 }
411 return pos;
412}

Callers 15

cbm_variant_assignFunction · 0.85
json_append_stringFunction · 0.85
handle_repo_infoFunction · 0.85
ui_log_add_strFunction · 0.85
handle_ui_log_postFunction · 0.85
handle_browseFunction · 0.85
handle_index_startFunction · 0.85
http_append_json_escapedFunction · 0.85
gc_edgeFunction · 0.85
an_deployFunction · 0.85
an_taskFunction · 0.85

Calls 1

cbm_utf8_trim_partialFunction · 0.85

Tested by

no test coverage detected