| 342 | // Misc |
| 343 | |
| 344 | export function indentify(text, indent) { |
| 345 | // Don't try to indentify huge strings - we may run out of memory |
| 346 | if (text.length > 1024 * 1024) return text; |
| 347 | |
| 348 | indent = ' '.repeat(indent); |
| 349 | |
| 350 | // Perform indentation in a smart fashion that does not leak indentation |
| 351 | // inside multiline strings enclosed in `` characters. |
| 352 | let out = ''; |
| 353 | for (let i = 0; i < text.length; ++i) { |
| 354 | // Output a C++ comment as-is, don't get confused by ` inside a C++ comment. |
| 355 | if (text[i] == '/' && text[i + 1] == '/') { |
| 356 | for (; i < text.length && text[i] != '\n'; ++i) { |
| 357 | out += text[i]; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | if (text[i] == '/' && text[i + 1] == '*') { |
| 362 | // Skip /* so that /*/ won't be mistaken as start& end of a /* */ comment. |
| 363 | out += text[i++]; |
| 364 | out += text[i++]; |
| 365 | for (; i < text.length && !(text[i - 1] == '*' && text[i] == '/'); ++i) { |
| 366 | out += text[i]; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | if (text[i] == '`') { |
| 371 | out += text[i++]; // Emit ` |
| 372 | for (; i < text.length && text[i] != '`'; ++i) { |
| 373 | out += text[i]; |
| 374 | } |
| 375 | } |
| 376 | out += text[i]; |
| 377 | if (text[i] == '\n') out += indent; |
| 378 | } |
| 379 | return out; |
| 380 | } |
| 381 | |
| 382 | // Correction tools |
| 383 | |