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

Function yaml_doc_init

src/cli/config_yaml_edit.c:1229–1274  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1227}
1228
1229static int yaml_doc_init(yaml_doc_t *doc, const char *data, size_t len) {
1230 memset(doc, 0, sizeof(*doc));
1231 doc->data = data;
1232 doc->len = len;
1233 if (yaml_build_lines(doc) != 0) {
1234 return YAML_ERROR;
1235 }
1236 /* A UTF-8 BOM is a prologue, not content (#1656): Windows-authored
1237 * configs routinely carry one (PowerShell 5.1 Set-Content -Encoding UTF8
1238 * writes it), and treating its bytes as the first key made every edit op
1239 * fail content-independently. Skip it for structure; edits splice ranges
1240 * at line offsets, so the BOM survives every write byte-for-byte. */
1241 if (doc->line_count > 0U && doc->len >= YAML_DOC_MARKER_LEN &&
1242 (unsigned char)data[0] == 0xEFU && (unsigned char)data[1] == 0xBBU &&
1243 (unsigned char)data[2] == 0xBFU) {
1244 yaml_line_t *first = &doc->lines[0];
1245 if (first->start == 0U && !first->blank) {
1246 first->start = YAML_DOC_MARKER_LEN;
1247 if (first->start + first->indent >= first->text_end) {
1248 first->blank = true;
1249 }
1250 }
1251 }
1252 if (yaml_mark_dquote_continuations(doc) != 0) {
1253 yaml_doc_free(doc);
1254 return YAML_ERROR;
1255 }
1256 for (size_t i = 0; i < doc->line_count; i++) {
1257 const yaml_line_t *line = &doc->lines[i];
1258 if (line->indent == 0U && !line->blank && !line->comment && !line->dquote_cont) {
1259 size_t len_no_eol = line->text_end - line->start;
1260 if ((len_no_eol == YAML_DOC_MARKER_LEN &&
1261 memcmp(doc->data + line->start, "---", YAML_DOC_MARKER_LEN) == 0) ||
1262 (len_no_eol == YAML_DOC_MARKER_LEN &&
1263 memcmp(doc->data + line->start, "...", YAML_DOC_MARKER_LEN) == 0)) {
1264 yaml_doc_free(doc);
1265 return YAML_ERROR;
1266 }
1267 }
1268 }
1269 if (yaml_validate_root_mapping(doc) != 0) {
1270 yaml_doc_free(doc);
1271 return YAML_ERROR;
1272 }
1273 return 0;
1274}
1275
1276static size_t yaml_skip_spaces(const char *data, size_t pos, size_t end) {
1277 while (pos < end && data[pos] == ' ') {

Calls 4

yaml_build_linesFunction · 0.85
yaml_doc_freeFunction · 0.85

Tested by

no test coverage detected