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

Function json_tree_block_string

src/mcp/compact_out.c:958–998  ·  view source on GitHub ↗

Source and Markdown lose substantial model utility when every newline is * escaped into one long quoted scalar. Use the familiar YAML block markers: * `|-` means no trailing newline, `|` means one, and `|+` preserves several. * The content remains readable/copyable and valid UTF-8. */

Source from the content-addressed store, hash-verified

956 * `|-` means no trailing newline, `|` means one, and `|+` preserves several.
957 * The content remains readable/copyable and valid UTF-8. */
958static void json_tree_block_string(cbm_sb_t *sb, const char *text, size_t len, int depth) {
959 size_t trailing_newlines = 0;
960 while (trailing_newlines < len && text[len - trailing_newlines - 1] == '\n') {
961 trailing_newlines++;
962 }
963 cbm_sb_append(sb, trailing_newlines == 0 ? "|-\n" : trailing_newlines == 1 ? "|\n" : "|+\n");
964
965 size_t line_offset = 0;
966 for (;;) {
967 const char *line = text + line_offset;
968 const char *newline = memchr(line, '\n', len - line_offset);
969 const char *end = newline ? newline : text + len;
970 json_tree_indent(sb, depth + 1);
971 for (const char *p = line; p < end; p++) {
972 unsigned char c = (unsigned char)*p;
973 if (c == '\t' || (c >= 0x20 && c != 0x7f && c < 0x80)) {
974 cbm_sb_append_n(sb, p, 1);
975 } else if (c >= 0x80) {
976 size_t sequence = utf8_sequence_length((const unsigned char *)p, (size_t)(end - p));
977 if (sequence && p + sequence <= end) {
978 cbm_sb_append_n(sb, p, sequence);
979 p += sequence - 1;
980 } else {
981 cbm_sb_append_n(sb, "\xEF\xBF\xBD", 3);
982 }
983 } else {
984 char escaped[8];
985 snprintf(escaped, sizeof(escaped), "\\u%04x", (unsigned)c);
986 cbm_sb_append(sb, escaped);
987 }
988 }
989 cbm_sb_append_n(sb, "\n", 1);
990 if (!newline) {
991 break;
992 }
993 line_offset = (size_t)(newline - text) + 1;
994 if (line_offset == len) {
995 break;
996 }
997 }
998}
999
1000static void json_tree_scalar(cbm_sb_t *sb, yyjson_val *value) {
1001 if (yyjson_is_str(value)) {

Callers 1

json_tree_valueFunction · 0.85

Calls 4

cbm_sb_appendFunction · 0.85
json_tree_indentFunction · 0.85
cbm_sb_append_nFunction · 0.85
utf8_sequence_lengthFunction · 0.85

Tested by

no test coverage detected