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