(source: string)
| 1225 | * use this helper so they are safe when called directly. |
| 1226 | */ |
| 1227 | export function maskCppRawStrings(source: string): { source: string; restore: (blanked: string) => string } { |
| 1228 | const unchanged = { source, restore: (blanked: string): string => blanked }; |
| 1229 | if (source.indexOf('R"') === -1) return unchanged; |
| 1230 | // Skip comments and ordinary literals before looking for a raw opener. The |
| 1231 | // char-literal boundary leaves numeric digit separators (1'000) alone. |
| 1232 | const re = /\/\/[^\r\n]*|\/\*[\s\S]*?(?:\*\/|$)|\b(?:u8|[LuU])?R"([^ \t\v\f\r\n()\\]{0,16})\(|"(?:\\[\s\S]|[^"\\])*(?:"|$)|(?<!\w)(?:u8|[LuU])?'(?:\\[\s\S]|[^'\\])*(?:'|$)/g; |
| 1233 | const spans: Array<{ start: number; end: number }> = []; |
| 1234 | const parts: string[] = []; |
| 1235 | let last = 0; |
| 1236 | let m: RegExpExecArray | null; |
| 1237 | while ((m = re.exec(source)) !== null) { |
| 1238 | if (m[1] === undefined) continue; |
| 1239 | const closer = `)${m[1]}"`; |
| 1240 | const close = source.indexOf(closer, re.lastIndex); |
| 1241 | // An unterminated raw literal owns the rest of the file too. |
| 1242 | const end = close < 0 ? source.length : close + closer.length; |
| 1243 | spans.push({ start: m.index, end }); |
| 1244 | parts.push(source.slice(last, m.index), source.slice(m.index, end).replace(/[^\r\n]/g, '\0')); |
| 1245 | last = re.lastIndex = end; |
| 1246 | } |
| 1247 | if (spans.length === 0) return unchanged; |
| 1248 | parts.push(source.slice(last)); |
| 1249 | const masked = parts.join(''); |
| 1250 | return { |
| 1251 | source: masked, |
| 1252 | restore(blanked): string { |
| 1253 | // A length-changing rewrite cannot be restored at the original offsets. |
| 1254 | if (blanked === masked || blanked.length !== source.length) return source; |
| 1255 | const chars = blanked.split(''); |
| 1256 | for (const { start, end } of spans) { |
| 1257 | for (let i = start; i < end; i++) { |
| 1258 | if (chars[i] === '\0') chars[i] = source[i] as string; |
| 1259 | } |
| 1260 | } |
| 1261 | return chars.join(''); |
| 1262 | }, |
| 1263 | }; |
| 1264 | } |
| 1265 | |
| 1266 | /** |
| 1267 | * Blank annotation-style macro invocations that decorate a declaration but carry |
no test coverage detected