| 235 | |
| 236 | |
| 237 | QList<Diff> diff_match_patch::diff_compute(QString text1, QString text2, |
| 238 | bool checklines, clock_t deadline) { |
| 239 | QList<Diff> diffs; |
| 240 | |
| 241 | if (text1.isEmpty()) { |
| 242 | // Just add some text (speedup). |
| 243 | diffs.append(Diff(INSERT, text2)); |
| 244 | return diffs; |
| 245 | } |
| 246 | |
| 247 | if (text2.isEmpty()) { |
| 248 | // Just delete some text (speedup). |
| 249 | diffs.append(Diff(DELETE, text1)); |
| 250 | return diffs; |
| 251 | } |
| 252 | |
| 253 | { |
| 254 | const QString longtext = text1.length() > text2.length() ? text1 : text2; |
| 255 | const QString shorttext = text1.length() > text2.length() ? text2 : text1; |
| 256 | const int i = longtext.indexOf(shorttext); |
| 257 | if (i != -1) { |
| 258 | // Shorter text is inside the longer text (speedup). |
| 259 | const Operation op = (text1.length() > text2.length()) ? DELETE : INSERT; |
| 260 | diffs.append(Diff(op, longtext.left(i))); |
| 261 | diffs.append(Diff(EQUAL, shorttext)); |
| 262 | diffs.append(Diff(op, safeMid(longtext, i + shorttext.length()))); |
| 263 | return diffs; |
| 264 | } |
| 265 | |
| 266 | if (shorttext.length() == 1) { |
| 267 | // Single character string. |
| 268 | // After the previous speedup, the character can't be an equality. |
| 269 | diffs.append(Diff(DELETE, text1)); |
| 270 | diffs.append(Diff(INSERT, text2)); |
| 271 | return diffs; |
| 272 | } |
| 273 | // Garbage collect longtext and shorttext by scoping out. |
| 274 | } |
| 275 | |
| 276 | // Check to see if the problem can be split in two. |
| 277 | const QStringList hm = diff_halfMatch(text1, text2); |
| 278 | if (hm.count() > 0) { |
| 279 | // A half-match was found, sort out the return data. |
| 280 | const QString text1_a = hm[0]; |
| 281 | const QString text1_b = hm[1]; |
| 282 | const QString text2_a = hm[2]; |
| 283 | const QString text2_b = hm[3]; |
| 284 | const QString mid_common = hm[4]; |
| 285 | // Send both pairs off for separate processing. |
| 286 | const QList<Diff> diffs_a = diff_main(text1_a, text2_a, |
| 287 | checklines, deadline); |
| 288 | const QList<Diff> diffs_b = diff_main(text1_b, text2_b, |
| 289 | checklines, deadline); |
| 290 | // Merge the results. |
| 291 | diffs = diffs_a; |
| 292 | diffs.append(Diff(EQUAL, mid_common)); |
| 293 | diffs += diffs_b; |
| 294 | return diffs; |