* The pre-optimization split('')-based stripCStyle, kept verbatim as the * ORACLE: the rewritten segment-builder must be byte-identical on every * input (the C fn-pointer synthesizer's regexes run over this text, and any * divergence would silently change synthesized edges).
(src: string, allowSingleQuoteStrings: boolean)
| 9 | * divergence would silently change synthesized edges). |
| 10 | */ |
| 11 | function referenceStripCStyle(src: string, allowSingleQuoteStrings: boolean): string { |
| 12 | const out = src.split(''); |
| 13 | let i = 0; |
| 14 | const n = src.length; |
| 15 | const blankRange = (buf: string[], start: number, end: number): void => { |
| 16 | for (let k = start; k < end; k++) { |
| 17 | buf[k] = src[k] === '\n' ? '\n' : ' '; |
| 18 | } |
| 19 | }; |
| 20 | while (i < n) { |
| 21 | const c = src[i]!; |
| 22 | const c2 = src[i + 1] ?? ''; |
| 23 | if (c === '/' && c2 === '*') { |
| 24 | const start = i; |
| 25 | i += 2; |
| 26 | while (i < n && !(src[i] === '*' && src[i + 1] === '/')) i++; |
| 27 | if (i < n) i += 2; |
| 28 | blankRange(out, start, i); |
| 29 | continue; |
| 30 | } |
| 31 | if (c === '/' && c2 === '/') { |
| 32 | const start = i; |
| 33 | while (i < n && src[i] !== '\n') i++; |
| 34 | blankRange(out, start, i); |
| 35 | continue; |
| 36 | } |
| 37 | if (c === '"' || (allowSingleQuoteStrings && c === "'") || c === '`') { |
| 38 | const quote = c; |
| 39 | i++; |
| 40 | while (i < n && src[i] !== quote) { |
| 41 | if (src[i] === '\\' && i + 1 < n) { |
| 42 | i += 2; |
| 43 | continue; |
| 44 | } |
| 45 | if (quote !== '`' && src[i] === '\n') break; |
| 46 | i++; |
| 47 | } |
| 48 | if (i < n && src[i] === quote) i++; |
| 49 | continue; |
| 50 | } |
| 51 | i++; |
| 52 | } |
| 53 | return out.join(''); |
| 54 | } |
| 55 | |
| 56 | const FIXTURES: Array<[string, string]> = [ |
| 57 | ['plain code, no comments', 'int main(void) {\n\treturn a / b;\n}\n'], |
no test coverage detected