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

Function cppCodeScan

src/extraction/languages/cpp-macro-methods.ts:474–571  ·  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

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