Reorder and merge like edit sections in place. Merge equalities. Any edit section can move as long as it doesn't cross an equality. Return the merged diffs sequence. Args: diffs: Array of diff tuples.
(diffs)
| 645 | |
| 646 | |
| 647 | def merge(diffs): |
| 648 | """ |
| 649 | Reorder and merge like edit sections in place. Merge equalities. |
| 650 | Any edit section can move as long as it doesn't cross an equality. |
| 651 | Return the merged diffs sequence. |
| 652 | Args: |
| 653 | diffs: Array of diff tuples. |
| 654 | """ |
| 655 | diffs.append((DIFF_EQUAL, '')) # Add a dummy entry at the end. |
| 656 | pointer = 0 |
| 657 | count_delete = 0 |
| 658 | count_insert = 0 |
| 659 | text_delete = '' |
| 660 | text_insert = '' |
| 661 | |
| 662 | while pointer < len(diffs): |
| 663 | |
| 664 | if diffs[pointer][0] == DIFF_INSERT: |
| 665 | count_insert += 1 |
| 666 | text_insert += diffs[pointer][1] |
| 667 | pointer += 1 |
| 668 | |
| 669 | elif diffs[pointer][0] == DIFF_DELETE: |
| 670 | count_delete += 1 |
| 671 | text_delete += diffs[pointer][1] |
| 672 | pointer += 1 |
| 673 | |
| 674 | elif diffs[pointer][0] == DIFF_EQUAL: |
| 675 | |
| 676 | # Upon reaching an equality, check for prior redundancies. |
| 677 | if count_delete + count_insert > 1: |
| 678 | if count_delete != 0 and count_insert != 0: |
| 679 | |
| 680 | # Factor out any common prefixies. |
| 681 | commonlength = common_prefix(text_insert, text_delete) |
| 682 | if commonlength != 0: |
| 683 | |
| 684 | x = pointer - count_delete - count_insert - 1 |
| 685 | if x >= 0 and diffs[x][0] == DIFF_EQUAL: |
| 686 | diffs[x] = ( |
| 687 | diffs[x][0], |
| 688 | diffs[x][1] + text_insert[:commonlength]) |
| 689 | else: |
| 690 | diffs.insert(0, (DIFF_EQUAL, text_insert[:commonlength])) |
| 691 | pointer += 1 |
| 692 | |
| 693 | text_insert = text_insert[commonlength:] |
| 694 | text_delete = text_delete[commonlength:] |
| 695 | |
| 696 | # Factor out any common suffixies. |
| 697 | commonlength = common_suffix(text_insert, text_delete) |
| 698 | if commonlength != 0: |
| 699 | diffs[pointer] = ( |
| 700 | diffs[pointer][0], |
| 701 | text_insert[-commonlength:] + diffs[pointer][1]) |
| 702 | |
| 703 | text_insert = text_insert[:-commonlength] |
| 704 | text_delete = text_delete[:-commonlength] |
no test coverage detected