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

Function rs_lines_resync

internal/cbm/doclink_rs.c:762–800  ·  view source on GitHub ↗

Give the lines of a file's own doc their real source lines. * * The inner docs of a file are every `//!` line and bang block before its * first item, joined: plain comments, blank lines and `#![..]` attributes * between them are not part of the text, so counting from the first line * drifts. Each doc line is looked up in the source instead, forward from the * line before it (a doc line equal

Source from the content-addressed store, hash-verified

760 * line before it (a doc line equals its source line up to indentation).
761 * Nothing changes unless every line is found. */
762static void rs_lines_resync(CBMExtractCtx *ctx, rs_line_t *lines, int n, const char *doc,
763 uint32_t doc_line) {
764 const char *src = ctx->source;
765 size_t src_len = ctx->source_len > 0 ? (size_t)ctx->source_len : 0;
766 uint32_t *found = (uint32_t *)cbm_arena_alloc(rs_scratch(ctx), (size_t)n * sizeof(uint32_t));
767 if (!src || !found || doc_line == 0) {
768 return;
769 }
770 size_t pos = 0; /* start of source line `at` */
771 uint32_t at = SKIP_ONE;
772 while (at < doc_line && pos < src_len) {
773 const char *nl = (const char *)memchr(src + pos, '\n', src_len - pos);
774 pos = nl ? (size_t)(nl - src) + SKIP_ONE : src_len;
775 at++;
776 }
777 const char *d = doc;
778 for (int i = 0; i < n; i++) {
779 const char *dnl = strchr(d, '\n');
780 size_t dlen = dnl ? (size_t)(dnl - d) : strlen(d);
781 const char *want = rs_trimmed(d, &dlen);
782 bool matched = false;
783 while (!matched && pos < src_len) {
784 const char *nl = (const char *)memchr(src + pos, '\n', src_len - pos);
785 size_t slen = nl ? (size_t)(nl - (src + pos)) : src_len - pos;
786 const char *have = rs_trimmed(src + pos, &slen);
787 matched = slen == dlen && memcmp(have, want, dlen) == 0;
788 found[i] = at;
789 pos = nl ? (size_t)(nl - src) + SKIP_ONE : src_len;
790 at++;
791 }
792 if (!matched) {
793 return;
794 }
795 d = dnl ? dnl + SKIP_ONE : d + strlen(d);
796 }
797 for (int i = 0; i < n; i++) {
798 lines[i].line = found[i];
799 }
800}
801
802/* ── Markdown blocks ─────────────────────────────────────────────── */
803

Callers 1

rs_parse_textFunction · 0.85

Calls 3

rs_scratchFunction · 0.85
rs_trimmedFunction · 0.85
cbm_arena_allocFunction · 0.70

Tested by

no test coverage detected