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

Function cppCodeScan

src/extraction/languages/cpp-macro-methods.ts:475–572  ·  view source on GitHub ↗

* One pass over C/C++ source, skipping comments and string, character and raw * string literals: the preprocessor directives (each with its `\` * continuations), and, when `names` is given, the code's identifiers among * them.

(source: string, names: ReadonlySet<string> | null)

Source from the content-addressed store, hash-verified

473 * them.
474 */
475function cppCodeScan(source: string, names: ReadonlySet<string> | null): {
476 directives: Array<{ at: number; text: string }>;
477 words: Array<{ name: string; at: number; end: number }>;
478} {
479 const directives: Array<{ at: number; text: string }> = [];
480 const words: Array<{ name: string; at: number; end: number }> = [];
481 const n = source.length;
482 let lineStart = true;
483 let lastWordStart = -1;
484 let lastWordEnd = -1;
485 for (let i = 0; i < n; ) {
486 const c = source.charCodeAt(i);
487 if (c === 10) {
488 lineStart = true;
489 i++;
490 continue;
491 }
492 if (c === 32 || c === 9 || c === 13 || c === 11 || c === 12) {
493 i++;
494 continue;
495 }
496 if (c === 47 && source.charCodeAt(i + 1) === 47) {
497 // A `\` at the end continues a line comment.
498 let end = i;
499 for (;;) {
500 end = source.indexOf('\n', end);
501 if (end < 0) {
502 end = n;
503 break;
504 }
505 let k = end - 1;
506 if (source.charCodeAt(k) === 13) k--;
507 if (source.charCodeAt(k) !== 92) break;
508 end++;
509 }
510 i = end;
511 continue;
512 }
513 if (c === 47 && source.charCodeAt(i + 1) === 42) {
514 const end = source.indexOf('*/', i + 2);
515 i = end < 0 ? n : end + 2;
516 continue;
517 }
518 if (c === 35 && lineStart) {
519 let j = i + 1;
520 for (; j < n; j++) {
521 const d = source.charCodeAt(j);
522 if (d === 47 && source.charCodeAt(j + 1) === 42) {
523 const end = source.indexOf('*/', j + 2);
524 j = end < 0 ? n - 1 : end + 1;
525 } else if (d === 10) {
526 let k = j - 1;
527 if (source.charCodeAt(k) === 13) k--;
528 if (source.charCodeAt(k) !== 92) break;
529 }
530 }
531 directives.push({ at: i, text: source.slice(i, j) });
532 i = j;

Callers 2

cppMacroDirectivesFunction · 0.85
cppMacroInvocationsFunction · 0.85

Calls 5

pushMethod · 0.80
hasMethod · 0.80
quotedEndFunction · 0.70
isWordCharFunction · 0.70
isWordStartFunction · 0.70

Tested by

no test coverage detected