(src: string, allowSingleQuoteStrings: boolean)
| 218 | // ---------- C-style (JS/TS/Java/C#/Swift) ---------- |
| 219 | |
| 220 | function stripCStyle(src: string, allowSingleQuoteStrings: boolean): string { |
| 221 | const out = src.split(''); |
| 222 | let i = 0; |
| 223 | const n = src.length; |
| 224 | |
| 225 | while (i < n) { |
| 226 | const c = src[i]!; |
| 227 | const c2 = src[i + 1] ?? ''; |
| 228 | |
| 229 | // Block comment |
| 230 | if (c === '/' && c2 === '*') { |
| 231 | const start = i; |
| 232 | i += 2; |
| 233 | while (i < n && !(src[i] === '*' && src[i + 1] === '/')) i++; |
| 234 | if (i < n) i += 2; |
| 235 | blankRange(out, start, i, src); |
| 236 | continue; |
| 237 | } |
| 238 | |
| 239 | // Line comment |
| 240 | if (c === '/' && c2 === '/') { |
| 241 | const start = i; |
| 242 | while (i < n && src[i] !== '\n') i++; |
| 243 | blankRange(out, start, i, src); |
| 244 | continue; |
| 245 | } |
| 246 | |
| 247 | // String literals |
| 248 | if (c === '"' || (allowSingleQuoteStrings && c === "'") || c === '`') { |
| 249 | const quote = c; |
| 250 | i++; |
| 251 | while (i < n && src[i] !== quote) { |
| 252 | if (src[i] === '\\' && i + 1 < n) { |
| 253 | i += 2; |
| 254 | continue; |
| 255 | } |
| 256 | // Template literal can span lines; regular strings break on newline (treat as unterminated) |
| 257 | if (quote !== '`' && src[i] === '\n') break; |
| 258 | i++; |
| 259 | } |
| 260 | if (i < n && src[i] === quote) i++; |
| 261 | continue; |
| 262 | } |
| 263 | |
| 264 | i++; |
| 265 | } |
| 266 | |
| 267 | return out.join(''); |
| 268 | } |
| 269 | |
| 270 | // ---------- PHP ---------- |
| 271 |
no test coverage detected