| 1091 | |
| 1092 | |
| 1093 | void diff_match_patch::diff_cleanupMerge(QList<Diff> &diffs) { |
| 1094 | diffs.append(Diff(EQUAL, "")); // Add a dummy entry at the end. |
| 1095 | QMutableListIterator<Diff> pointer(diffs); |
| 1096 | int count_delete = 0; |
| 1097 | int count_insert = 0; |
| 1098 | QString text_delete = ""; |
| 1099 | QString text_insert = ""; |
| 1100 | Diff *thisDiff = pointer.hasNext() ? &pointer.next() : NULL; |
| 1101 | Diff *prevEqual = NULL; |
| 1102 | int commonlength; |
| 1103 | while (thisDiff != NULL) { |
| 1104 | switch (thisDiff->operation) { |
| 1105 | case INSERT: |
| 1106 | count_insert++; |
| 1107 | text_insert += thisDiff->text; |
| 1108 | prevEqual = NULL; |
| 1109 | break; |
| 1110 | case DELETE: |
| 1111 | count_delete++; |
| 1112 | text_delete += thisDiff->text; |
| 1113 | prevEqual = NULL; |
| 1114 | break; |
| 1115 | case EQUAL: |
| 1116 | if (count_delete + count_insert > 1) { |
| 1117 | bool both_types = count_delete != 0 && count_insert != 0; |
| 1118 | // Delete the offending records. |
| 1119 | pointer.previous(); // Reverse direction. |
| 1120 | while (count_delete-- > 0) { |
| 1121 | pointer.previous(); |
| 1122 | pointer.remove(); |
| 1123 | } |
| 1124 | while (count_insert-- > 0) { |
| 1125 | pointer.previous(); |
| 1126 | pointer.remove(); |
| 1127 | } |
| 1128 | if (both_types) { |
| 1129 | // Factor out any common prefixies. |
| 1130 | commonlength = diff_commonPrefix(text_insert, text_delete); |
| 1131 | if (commonlength != 0) { |
| 1132 | if (pointer.hasPrevious()) { |
| 1133 | thisDiff = &pointer.previous(); |
| 1134 | if (thisDiff->operation != EQUAL) { |
| 1135 | throw "Previous diff should have been an equality."; |
| 1136 | } |
| 1137 | thisDiff->text += text_insert.left(commonlength); |
| 1138 | pointer.next(); |
| 1139 | } else { |
| 1140 | pointer.insert(Diff(EQUAL, text_insert.left(commonlength))); |
| 1141 | } |
| 1142 | text_insert = safeMid(text_insert, commonlength); |
| 1143 | text_delete = safeMid(text_delete, commonlength); |
| 1144 | } |
| 1145 | // Factor out any common suffixies. |
| 1146 | commonlength = diff_commonSuffix(text_insert, text_delete); |
| 1147 | if (commonlength != 0) { |
| 1148 | thisDiff = &pointer.next(); |
| 1149 | thisDiff->text = safeMid(text_insert, text_insert.length() |
| 1150 | - commonlength) + thisDiff->text; |