| 428 | * @returns The processed output with backspaces handled |
| 429 | */ |
| 430 | export function processBackspaces(input: string): string { |
| 431 | let output = "" |
| 432 | let pos = 0 |
| 433 | let bsPos = input.indexOf("\b") |
| 434 | |
| 435 | while (bsPos !== -1) { |
| 436 | // Fast path: exclude char before backspace |
| 437 | output += input.substring(pos, bsPos - 1) |
| 438 | |
| 439 | // Move past backspace |
| 440 | pos = bsPos + 1 |
| 441 | |
| 442 | // Count consecutive backspaces |
| 443 | let count = 0 |
| 444 | while (input[pos] === "\b") { |
| 445 | count++ |
| 446 | pos++ |
| 447 | } |
| 448 | |
| 449 | // Trim output mathematically for consecutive backspaces |
| 450 | if (count > 0 && output.length > 0) { |
| 451 | output = output.substring(0, Math.max(0, output.length - count)) |
| 452 | } |
| 453 | |
| 454 | // Find next backspace |
| 455 | bsPos = input.indexOf("\b", pos) |
| 456 | } |
| 457 | |
| 458 | // Add remaining content |
| 459 | if (pos < input.length) { |
| 460 | output += input.substring(pos) |
| 461 | } |
| 462 | |
| 463 | return output |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Helper function to process a single line with carriage returns. |