* 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)
| 472 | * them. |
| 473 | */ |
| 474 | function 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; |
no test coverage detected