| 566 | |
| 567 | // the hunks of two short runs, by their longest common subsequence |
| 568 | const compareEach = ( |
| 569 | base: string[], |
| 570 | other: string[], |
| 571 | b0: number, |
| 572 | b1: number, |
| 573 | o0: number, |
| 574 | o1: number, |
| 575 | hunks: Hunk[] |
| 576 | ): void => { |
| 577 | const [rows, columns] = [b1 - b0 + 1, o1 - o0 + 1] |
| 578 | // the length of what the runs from each pair on have in common |
| 579 | const common = new Uint32Array(rows * columns) |
| 580 | for (let i = rows - 2; i >= 0; i--) { |
| 581 | for (let j = columns - 2; j >= 0; j--) { |
| 582 | common[i * columns + j] = |
| 583 | base[b0 + i] === other[o0 + j] ? |
| 584 | common[(i + 1) * columns + j + 1] + 1 |
| 585 | : Math.max(common[(i + 1) * columns + j], common[i * columns + j + 1]) |
| 586 | } |
| 587 | } |
| 588 | let [i, j] = [0, 0] |
| 589 | let hunk: Hunk | undefined |
| 590 | while (i < rows - 1 || j < columns - 1) { |
| 591 | if (i < rows - 1 && j < columns - 1 && base[b0 + i] === other[o0 + j]) { |
| 592 | i++ |
| 593 | j++ |
| 594 | hunk = undefined |
| 595 | continue |
| 596 | } |
| 597 | if (!hunk) hunks.push((hunk = [b0 + i, b0 + i, o0 + j, o0 + j])) |
| 598 | if ( |
| 599 | j === columns - 1 || |
| 600 | (i < rows - 1 && |
| 601 | common[(i + 1) * columns + j] >= common[i * columns + j + 1]) |
| 602 | ) |
| 603 | hunk[1] = b0 + ++i |
| 604 | else hunk[3] = o0 + ++j |
| 605 | } |
| 606 | } |