diffCompute finds the differences between two rune slices. Assumes that the texts do not have any common prefix or suffix.
(text1, text2 []rune, checklines bool, deadline time.Time)
| 131 | |
| 132 | // diffCompute finds the differences between two rune slices. Assumes that the texts do not have any common prefix or suffix. |
| 133 | func (dmp *DiffMatchPatch) diffCompute(text1, text2 []rune, checklines bool, deadline time.Time) []Diff { |
| 134 | diffs := []Diff{} |
| 135 | if len(text1) == 0 { |
| 136 | // Just add some text (speedup). |
| 137 | return append(diffs, Diff{DiffInsert, string(text2)}) |
| 138 | } else if len(text2) == 0 { |
| 139 | // Just delete some text (speedup). |
| 140 | return append(diffs, Diff{DiffDelete, string(text1)}) |
| 141 | } |
| 142 | |
| 143 | var longtext, shorttext []rune |
| 144 | if len(text1) > len(text2) { |
| 145 | longtext = text1 |
| 146 | shorttext = text2 |
| 147 | } else { |
| 148 | longtext = text2 |
| 149 | shorttext = text1 |
| 150 | } |
| 151 | |
| 152 | if i := runesIndex(longtext, shorttext); i != -1 { |
| 153 | op := DiffInsert |
| 154 | // Swap insertions for deletions if diff is reversed. |
| 155 | if len(text1) > len(text2) { |
| 156 | op = DiffDelete |
| 157 | } |
| 158 | // Shorter text is inside the longer text (speedup). |
| 159 | return []Diff{ |
| 160 | Diff{op, string(longtext[:i])}, |
| 161 | Diff{DiffEqual, string(shorttext)}, |
| 162 | Diff{op, string(longtext[i+len(shorttext):])}, |
| 163 | } |
| 164 | } else if len(shorttext) == 1 { |
| 165 | // Single character string. |
| 166 | // After the previous speedup, the character can't be an equality. |
| 167 | return []Diff{ |
| 168 | {DiffDelete, string(text1)}, |
| 169 | {DiffInsert, string(text2)}, |
| 170 | } |
| 171 | // Check to see if the problem can be split in two. |
| 172 | } else if hm := dmp.diffHalfMatch(text1, text2); hm != nil { |
| 173 | // A half-match was found, sort out the return data. |
| 174 | text1A := hm[0] |
| 175 | text1B := hm[1] |
| 176 | text2A := hm[2] |
| 177 | text2B := hm[3] |
| 178 | midCommon := hm[4] |
| 179 | // Send both pairs off for separate processing. |
| 180 | diffsA := dmp.diffMainRunes(text1A, text2A, checklines, deadline) |
| 181 | diffsB := dmp.diffMainRunes(text1B, text2B, checklines, deadline) |
| 182 | // Merge the results. |
| 183 | diffs := diffsA |
| 184 | diffs = append(diffs, Diff{DiffEqual, string(midCommon)}) |
| 185 | diffs = append(diffs, diffsB...) |
| 186 | return diffs |
| 187 | } else if checklines && len(text1) > 100 && len(text2) > 100 { |
| 188 | return dmp.diffLineMode(text1, text2, deadline) |
| 189 | } |
| 190 | return dmp.diffBisect(text1, text2, deadline) |
no test coverage detected