| 63 | * comments cover, each with the columns they take. Empty for a file with none. |
| 64 | */ |
| 65 | export function cppNonCodeLines(file: string, context: ResolutionContext): Map<number, Ranges> { |
| 66 | let memo = RAW_STRING_LINES.get(context); |
| 67 | if (!memo) RAW_STRING_LINES.set(context, (memo = new Map())); |
| 68 | const hit = memo.get(file); |
| 69 | if (hit) return hit; |
| 70 | const lines = new Map<number, Ranges>(); |
| 71 | const text = context.readFile(file); |
| 72 | if (text && (text.includes('R"') || text.includes('/*'))) { |
| 73 | const n = text.length; |
| 74 | const hasCarriageReturns = text.includes('\r'); |
| 75 | // The line holding offset `i`, reached only when something on it is marked. |
| 76 | let line = 1; |
| 77 | let lineStart = 0; |
| 78 | const seek = (to: number): void => { |
| 79 | for (let nl = text.indexOf('\n', lineStart); nl >= 0 && nl < to; nl = text.indexOf('\n', lineStart)) { |
| 80 | line++; |
| 81 | lineStart = nl + 1; |
| 82 | } |
| 83 | }; |
| 84 | // Mark [from, to) of the text, which may span lines, as raw string contents. |
| 85 | const mark = (from: number, to: number): void => { |
| 86 | seek(from); |
| 87 | let l = line; |
| 88 | let start = from - lineStart - (hasCarriageReturns ? carriageReturnsIn(text, lineStart, from) : 0); |
| 89 | for (let k = from; ;) { |
| 90 | const nl = text.indexOf('\n', k); |
| 91 | const end = nl < 0 || nl >= to ? to : nl; |
| 92 | const width = end - k - (hasCarriageReturns ? carriageReturnsIn(text, k, end) : 0); |
| 93 | if (width > 0) (lines.get(l) ?? lines.set(l, []).get(l)!).push([start, start + width]); |
| 94 | if (end === to) break; |
| 95 | k = end + 1; |
| 96 | l++; |
| 97 | start = 0; |
| 98 | } |
| 99 | }; |
| 100 | let i = 0; |
| 101 | while (i < n) { |
| 102 | const c = text.charCodeAt(i); |
| 103 | if (c === SLASH) { |
| 104 | const next = text.charCodeAt(i + 1); |
| 105 | if (next === SLASH) { |
| 106 | const end = text.indexOf('\n', i); |
| 107 | i = end < 0 ? n : end; |
| 108 | } else if (next === STAR) { |
| 109 | const end = text.indexOf('*/', i + 2); |
| 110 | const to = end < 0 ? n : end + 2; |
| 111 | mark(i, to); |
| 112 | i = to; |
| 113 | } else i++; |
| 114 | } else if (c === QUOTE) { |
| 115 | // `R"`, `u8R"`, `uR"`, `UR"`, `LR"`, not part of a longer name. |
| 116 | const prefix = text.charCodeAt(i - 1) === CAPITAL_R ? /(?:u8|[uUL])?R$/.exec(text.slice(Math.max(0, i - 3), i)) : null; |
| 117 | const open = prefix && !isWordCode(text.charCodeAt(i - prefix[0].length - 1)) ? text.indexOf('(', i + 1) : -1; |
| 118 | const delimiter = open > i ? text.slice(i + 1, open) : null; |
| 119 | if (delimiter !== null && delimiter.length <= 16 && /^[^\s()\\]*$/.test(delimiter)) { |
| 120 | // From the opening quote through the closing one: `"cc( … )cc"`. |
| 121 | const close = text.indexOf(`)${delimiter}"`, open + 1); |
| 122 | const end = close < 0 ? n : close + delimiter.length + 2; |