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