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

Function append_json_str_array

src/pipeline/pass_definitions.c:227–268  ·  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

225/* Append a JSON array of strings: ,"key":["a","b","c"]. Atomic like
226 * append_json_string: emitted only if the whole array fits. */
227static void append_json_str_array(char *buf, size_t bufsize, size_t *pos, const char *key,
228 const char **arr) {
229 if (!arr || !arr[0] || *pos >= bufsize - PD_JSON_MARGIN) {
230 return;
231 }
232 /* ,"key":[ + per item "<escaped>" + separating commas + ] */
233 size_t required = strlen(key) + PD_JSON_FIELD_OVERHEAD;
234 for (int i = 0; arr[i]; i++) {
235 required += def_json_escaped_len(arr[i]) + PD_ESC_SPACE + (i > 0 ? SKIP_ONE : 0);
236 }
237 if (*pos + required + PD_ESC_SPACE > bufsize) {
238 return; /* whole array would not fit — skip it atomically */
239 }
240 size_t p = *pos;
241 int n = snprintf(buf + p, bufsize - p, ",\"%s\":[", key);
242 if (n <= 0 || p + (size_t)n >= bufsize - PD_ESC_SPACE) {
243 return;
244 }
245 p += (size_t)n;
246 for (int i = 0; arr[i]; i++) {
247 if (i > 0 && p < bufsize - SKIP_ONE) {
248 buf[p++] = ',';
249 }
250 if (p < bufsize - SKIP_ONE) {
251 buf[p++] = '"';
252 }
253 /* Full escaping (not just quote/backslash): items like C param types
254 * sliced from multi-line declarations carry raw \n/\t bytes, which are
255 * invalid inside JSON strings. */
256 for (const char *s = arr[i]; *s && p < bufsize - PD_ESC_SPACE; s++) {
257 p += (size_t)def_json_escape_char(buf + p, bufsize - p - PD_ESC_SPACE, *s);
258 }
259 if (p < bufsize - SKIP_ONE) {
260 buf[p++] = '"';
261 }
262 }
263 if (p < bufsize - SKIP_ONE) {
264 buf[p++] = ']';
265 }
266 buf[p] = '\0';
267 *pos = p;
268}
269
270/* Build properties JSON for a definition node. */
271static void build_def_props(char *buf, size_t bufsize, const CBMDefinition *def) {

Callers 1

build_def_propsFunction · 0.70

Calls 2

def_json_escaped_lenFunction · 0.85
def_json_escape_charFunction · 0.85

Tested by

no test coverage detected