(text: string, line: number, rawLines: Map<number, Ranges>)
| 138 | * and character literals blanked, raw strings included, columns kept. |
| 139 | */ |
| 140 | export function cppLiteralsBlanked(text: string, line: number, rawLines: Map<number, Ranges>): string { |
| 141 | const raw = rawLines.get(line); |
| 142 | const hasQuotes = text.includes('"') || text.includes('\''); |
| 143 | if (!raw && !hasQuotes) return text; |
| 144 | const blanks: Ranges = raw ? raw.slice() : []; |
| 145 | if (hasQuotes) { |
| 146 | const inRaw = (k: number): boolean => !!raw?.some(([start, end]) => k >= start && k < end); |
| 147 | for (let k = 0; k < text.length; k++) { |
| 148 | const c = text.charCodeAt(k); |
| 149 | if (c !== SLASH && c !== QUOTE && c !== APOSTROPHE) continue; |
| 150 | if (inRaw(k)) continue; |
| 151 | if (c === SLASH) { |
| 152 | const next = text.charCodeAt(k + 1); |
| 153 | if (next === SLASH) break; |
| 154 | if (next !== STAR) continue; |
| 155 | // A comment's quotes are no string's (cppCodeOf blanks the comment). |
| 156 | const close = text.indexOf('*/', k + 2); |
| 157 | if (close < 0) break; |
| 158 | k = close + 1; |
| 159 | continue; |
| 160 | } |
| 161 | if (c === APOSTROPHE && isWordCode(text.charCodeAt(k - 1))) continue; |
| 162 | let end = k + 1; |
| 163 | while (end < text.length && text.charCodeAt(end) !== c) end += text.charCodeAt(end) === BACKSLASH ? 2 : 1; |
| 164 | blanks.push([k + 1, Math.min(end, text.length)]); |
| 165 | k = end; |
| 166 | } |
| 167 | } |
| 168 | return blankRanges(text, blanks); |
| 169 | } |
| 170 | |
| 171 | /** `text` with spaces over each of `ranges`, which may overlap and may run past its end. */ |
| 172 | function blankRanges(text: string, ranges: Ranges): string { |
no test coverage detected