MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / blankCTypeKeywordArgs

Function blankCTypeKeywordArgs

src/extraction/languages/c-cpp.ts:2476–2548  ·  view source on GitHub ↗
(source: string)

Source from the content-addressed store, hash-verified

2474const C_TYPE_ARG_OPENER_RE = /^(struct|union|enum)([ \t\r\n]+)([A-Za-z_]\w*)([ \t\r\n]*\*+)?(?=[ \t\r\n]*[,)])/;
2475const C_TYPE_ARG_SCAN_CAP = 600;
2476export function blankCTypeKeywordArgs(source: string): string {
2477 if (!/\b(?:struct|union|enum)[ \t\r\n]/.test(source)) return source;
2478 const rawStrings = maskCppRawStrings(source);
2479 source = rawStrings.source;
2480 let chars: string[] | null = null;
2481 C_TYPE_ARG_HEAD_RE.lastIndex = 0;
2482 let m: RegExpExecArray | null;
2483 while ((m = C_TYPE_ARG_HEAD_RE.exec(source)) !== null) {
2484 const head = m[1] as string;
2485 if (C_TYPE_ARG_HEAD_EXCLUSIONS.has(head)) continue;
2486 // Reject declaration shapes: an identifier or `*` (a return/field type)
2487 // immediately before the head — unless that word is `return`.
2488 let j = m.index - 1;
2489 while (j >= 0 && (source[j] === ' ' || source[j] === '\t')) j--;
2490 const prev = j >= 0 ? (source[j] as string) : '';
2491 if (/[\w*]/.test(prev)) {
2492 let w = j;
2493 while (w >= 0 && /\w/.test(source[w] as string)) w--;
2494 if (source.slice(w + 1, j + 1) !== 'return') continue;
2495 }
2496 const open = m.index + (m[0] as string).length - 1;
2497 let depth = 1;
2498 let atArgStart = true;
2499 for (let k = open + 1; k < source.length && k - open <= C_TYPE_ARG_SCAN_CAP; k++) {
2500 const ch = source[k] as string;
2501 if (ch === '"' || ch === "'") {
2502 const quote = ch;
2503 k++;
2504 while (k < source.length && source[k] !== quote) {
2505 if (source[k] === '\\') k++;
2506 k++;
2507 }
2508 atArgStart = false;
2509 continue;
2510 }
2511 if (ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r') continue;
2512 if (ch === '(') {
2513 depth++;
2514 atArgStart = false;
2515 continue;
2516 }
2517 if (ch === ')') {
2518 depth--;
2519 if (depth === 0) break;
2520 atArgStart = false;
2521 continue;
2522 }
2523 if (ch === ';' || ch === '{' || ch === '}') break; // not an argument list
2524 if (ch === ',') {
2525 if (depth === 1) atArgStart = true;
2526 continue;
2527 }
2528 if (depth === 1 && atArgStart) {
2529 const opener = C_TYPE_ARG_OPENER_RE.exec(source.slice(k, k + 160));
2530 if (opener) {
2531 chars ??= [...source];
2532 const kw = opener[1] as string;
2533 for (let b = k; b < k + kw.length; b++) chars[b] = ' ';

Callers 3

preParseCSourceFunction · 0.85
extraction.test.tsFile · 0.85

Calls 5

maskCppRawStringsFunction · 0.85
hasMethod · 0.80
restoreMethod · 0.80
execMethod · 0.65
joinMethod · 0.45

Tested by

no test coverage detected