DiffCleanupEfficiency reduces the number of edits by eliminating operationally trivial equalities.
(diffs []Diff)
| 845 | |
| 846 | // DiffCleanupEfficiency reduces the number of edits by eliminating operationally trivial equalities. |
| 847 | func (dmp *DiffMatchPatch) DiffCleanupEfficiency(diffs []Diff) []Diff { |
| 848 | changes := false |
| 849 | // Stack of indices where equalities are found. |
| 850 | type equality struct { |
| 851 | data int |
| 852 | next *equality |
| 853 | } |
| 854 | var equalities *equality |
| 855 | // Always equal to equalities[equalitiesLength-1][1] |
| 856 | lastequality := "" |
| 857 | pointer := 0 // Index of current position. |
| 858 | // Is there an insertion operation before the last equality. |
| 859 | preIns := false |
| 860 | // Is there a deletion operation before the last equality. |
| 861 | preDel := false |
| 862 | // Is there an insertion operation after the last equality. |
| 863 | postIns := false |
| 864 | // Is there a deletion operation after the last equality. |
| 865 | postDel := false |
| 866 | for pointer < len(diffs) { |
| 867 | if diffs[pointer].Type == DiffEqual { // Equality found. |
| 868 | if len(diffs[pointer].Text) < dmp.DiffEditCost && |
| 869 | (postIns || postDel) { |
| 870 | // Candidate found. |
| 871 | equalities = &equality{ |
| 872 | data: pointer, |
| 873 | next: equalities, |
| 874 | } |
| 875 | preIns = postIns |
| 876 | preDel = postDel |
| 877 | lastequality = diffs[pointer].Text |
| 878 | } else { |
| 879 | // Not a candidate, and can never become one. |
| 880 | equalities = nil |
| 881 | lastequality = "" |
| 882 | } |
| 883 | postIns = false |
| 884 | postDel = false |
| 885 | } else { // An insertion or deletion. |
| 886 | if diffs[pointer].Type == DiffDelete { |
| 887 | postDel = true |
| 888 | } else { |
| 889 | postIns = true |
| 890 | } |
| 891 | |
| 892 | // Five types to be split: |
| 893 | // <ins>A</ins><del>B</del>XY<ins>C</ins><del>D</del> |
| 894 | // <ins>A</ins>X<ins>C</ins><del>D</del> |
| 895 | // <ins>A</ins><del>B</del>X<ins>C</ins> |
| 896 | // <ins>A</del>X<ins>C</ins><del>D</del> |
| 897 | // <ins>A</ins><del>B</del>X<del>C</del> |
| 898 | var sumPres int |
| 899 | if preIns { |
| 900 | sumPres++ |
| 901 | } |
| 902 | if preDel { |
| 903 | sumPres++ |
| 904 | } |