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

Function append_json_str_array

src/pipeline/pass_parallel.c:441–482  ·  view source on GitHub ↗

Append a JSON array of strings: ,"key":["a","b","c"]. Atomic like * append_json_string: emitted only if the whole array fits. */

Source from the content-addressed store, hash-verified

439/* Append a JSON array of strings: ,"key":["a","b","c"]. Atomic like
440 * append_json_string: emitted only if the whole array fits. */
441static void append_json_str_array(char *buf, size_t bufsize, size_t *pos, const char *key,
442 const char **arr) {
443 if (!arr || !arr[0] || *pos >= bufsize - PP_JSON_MARGIN) {
444 return;
445 }
446 /* ,"key":[ + per item "<escaped>" + separating commas + ] */
447 size_t required = strlen(key) + PP_JSON_FIELD_OVERHEAD;
448 for (int i = 0; arr[i]; i++) {
449 required += pp_json_escaped_len(arr[i]) + PP_ESC_SPACE + (i > 0 ? SKIP_ONE : 0);
450 }
451 if (*pos + required + PP_ESC_SPACE > bufsize) {
452 return; /* whole array would not fit — skip it atomically */
453 }
454 size_t p = *pos;
455 int n = snprintf(buf + p, bufsize - p, ",\"%s\":[", key);
456 if (n <= 0 || p + (size_t)n >= bufsize - PP_ESC_SPACE) {
457 return;
458 }
459 p += (size_t)n;
460 for (int i = 0; arr[i]; i++) {
461 if (i > 0 && p < bufsize - SKIP_ONE) {
462 buf[p++] = ',';
463 }
464 if (p < bufsize - SKIP_ONE) {
465 buf[p++] = '"';
466 }
467 /* Full escaping (not just quote/backslash): items like C param types
468 * sliced from multi-line declarations carry raw \n/\t bytes, which are
469 * invalid inside JSON strings. */
470 for (const char *s = arr[i]; *s && p < bufsize - PP_ESC_SPACE; s++) {
471 p += (size_t)json_escape_char(buf + p, bufsize - p - PP_ESC_SPACE, *s);
472 }
473 if (p < bufsize - SKIP_ONE) {
474 buf[p++] = '"';
475 }
476 }
477 if (p < bufsize - SKIP_ONE) {
478 buf[p++] = ']';
479 }
480 buf[p] = '\0';
481 *pos = p;
482}
483
484/* Append an already-serialized JSON value verbatim: ,"key":<json>. Atomic like
485 * append_json_string. For `variants`, which extraction builds as a JSON array

Callers 1

build_def_propsFunction · 0.70

Calls 2

pp_json_escaped_lenFunction · 0.85
json_escape_charFunction · 0.85

Tested by

no test coverage detected