Find the differences between two texts. Assumes that the texts do not have any common prefix or suffix. Args: text1: Old string to be diffed. text2: New string to be diffed. deadline: Time when the diff should be complete by. Returns:
(self, text1, text2, deadline)
| 184 | return diffs |
| 185 | |
| 186 | def compute(self, text1, text2, deadline): |
| 187 | """ |
| 188 | Find the differences between two texts. Assumes that the texts do not |
| 189 | have any common prefix or suffix. |
| 190 | |
| 191 | Args: |
| 192 | text1: Old string to be diffed. |
| 193 | text2: New string to be diffed. |
| 194 | deadline: Time when the diff should be complete by. |
| 195 | |
| 196 | Returns: |
| 197 | Array of changes. |
| 198 | """ |
| 199 | if not text1: |
| 200 | # Just add some text (speedup). |
| 201 | return [(DIFF_INSERT, text2)] |
| 202 | |
| 203 | if not text2: |
| 204 | # Just delete some text (speedup). |
| 205 | return [(DIFF_DELETE, text1)] |
| 206 | |
| 207 | len_text1 = len(text1) |
| 208 | len_text2 = len(text2) |
| 209 | |
| 210 | reversed_diff = len_text1 > len_text2 |
| 211 | |
| 212 | if reversed_diff: |
| 213 | longtext, shorttext = text1, text2 |
| 214 | len_shorttext = len_text2 |
| 215 | else: |
| 216 | shorttext, longtext = text1, text2 |
| 217 | len_shorttext = len_text1 |
| 218 | |
| 219 | i = longtext.find(shorttext) |
| 220 | |
| 221 | if i != -1: |
| 222 | # Shorter text is inside the longer text (speedup). |
| 223 | diffs = [(DIFF_INSERT, longtext[:i]), |
| 224 | (DIFF_EQUAL, shorttext), |
| 225 | (DIFF_INSERT, longtext[i + len_shorttext:])] |
| 226 | # Swap insertions for deletions if diff is reversed. |
| 227 | if reversed_diff: |
| 228 | diffs[0] = (DIFF_DELETE, diffs[0][1]) |
| 229 | diffs[2] = (DIFF_DELETE, diffs[2][1]) |
| 230 | return diffs |
| 231 | |
| 232 | if len_shorttext == 1: |
| 233 | # Single character string. |
| 234 | # After the previous speedup, the character can't be an equality. |
| 235 | return [(DIFF_DELETE, text1), (DIFF_INSERT, text2)] |
| 236 | |
| 237 | # Check to see if the problem can be split in two. |
| 238 | hm = half_match(text1, text2, len_text1, len_text2) |
| 239 | if hm: |
| 240 | # A half-match was found, sort out the return data. |
| 241 | (text1_a, text1_b, text2_a, text2_b, mid_common) = hm |
| 242 | # Send both pairs off for separate processing. |
| 243 | diffs_a = self.difference(text1_a, text2_a, deadline) |
no test coverage detected