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

Function ti_normalize_join

src/mcp/test_impact_source.c:538–586  ·  view source on GitHub ↗

os.path.normpath(os.path.join(dirname(from), inc)) for '/' paths. */

Source from the content-addressed store, hash-verified

536
537/* os.path.normpath(os.path.join(dirname(from), inc)) for '/' paths. */
538static char *ti_normalize_join(CBMArena *arena, const char *from, const char *inc, size_t inc_len) {
539 size_t from_len = strlen(from);
540 const char *slash = NULL;
541 for (size_t i = 0; i < from_len; i++) {
542 if (from[i] == '/') {
543 slash = from + i;
544 }
545 }
546 size_t dir_len = inc_len && inc[0] == '/' ? 0 : (slash ? (size_t)(slash - from) : 0);
547 size_t total = dir_len + 1 + inc_len;
548 char *joined = cbm_arena_alloc(arena, total + 1);
549 char *out = cbm_arena_alloc(arena, total + 2);
550 if (!joined || !out) {
551 return NULL;
552 }
553 memcpy(joined, from, dir_len);
554 joined[dir_len] = '/';
555 memcpy(joined + dir_len + 1, inc, inc_len);
556 joined[total] = '\0';
557 bool absolute = inc_len && inc[0] == '/';
558 size_t used = 0;
559 size_t depth = 0; /* segments that can be removed by a later ".." */
560 const char *p = dir_len ? joined : joined + 1;
561 while (*p) {
562 const char *e = strchr(p, '/');
563 size_t seg = e ? (size_t)(e - p) : strlen(p);
564 if (seg == 0 || (seg == 1 && p[0] == '.')) {
565 /* skip */
566 } else if (seg == 2 && p[0] == '.' && p[1] == '.' && depth > 0) {
567 while (used > 0 && out[used - 1] != '/') {
568 used--;
569 }
570 if (used > 0) {
571 used--;
572 }
573 depth--;
574 } else if (!(seg == 2 && p[0] == '.' && p[1] == '.' && absolute)) {
575 if (used) {
576 out[used++] = '/';
577 }
578 memcpy(out + used, p, seg);
579 used += seg;
580 depth += !(seg == 2 && p[0] == '.' && p[1] == '.');
581 }
582 p += seg + (e ? 1 : 0);
583 }
584 out[used] = '\0';
585 return out;
586}
587
588/* Every segment suffix of every path, for includes that do not resolve from
589 * the including file's directory. */

Callers 1

ti_resolve_includeFunction · 0.85

Calls 1

cbm_arena_allocFunction · 0.50

Tested by

no test coverage detected