| 1419 | } |
| 1420 | |
| 1421 | QList<Diff> Differ::cleanupSemanticsLossless(const QList<Diff> &diffList) |
| 1422 | { |
| 1423 | if (diffList.size() < 3) // we need at least 3 items |
| 1424 | return diffList; |
| 1425 | |
| 1426 | QList<Diff> newDiffList; |
| 1427 | Diff prevDiff = diffList.at(0); |
| 1428 | Diff thisDiff = diffList.at(1); |
| 1429 | Diff nextDiff = diffList.at(2); |
| 1430 | int i = 2; |
| 1431 | while (i < diffList.size()) { |
| 1432 | if (prevDiff.command == Diff::Equal |
| 1433 | && nextDiff.command == Diff::Equal) { |
| 1434 | |
| 1435 | // Single edit surrounded by equalities |
| 1436 | QString equality1 = prevDiff.text; |
| 1437 | QString edit = thisDiff.text; |
| 1438 | QString equality2 = nextDiff.text; |
| 1439 | |
| 1440 | // Shift the edit as far left as possible |
| 1441 | const int suffixCount = commonSuffix(equality1, edit); |
| 1442 | if (suffixCount) { |
| 1443 | const QString commonString = edit.mid(edit.size() - suffixCount); |
| 1444 | equality1 = equality1.left(equality1.size() - suffixCount); |
| 1445 | edit = commonString + edit.left(edit.size() - suffixCount); |
| 1446 | equality2 = commonString + equality2; |
| 1447 | } |
| 1448 | |
| 1449 | // Step char by char right, looking for the best score |
| 1450 | QString bestEquality1 = equality1; |
| 1451 | QString bestEdit = edit; |
| 1452 | QString bestEquality2 = equality2; |
| 1453 | int bestScore = cleanupSemanticsScore(equality1, edit) |
| 1454 | + cleanupSemanticsScore(edit, equality2); |
| 1455 | |
| 1456 | while (!edit.isEmpty() && !equality2.isEmpty() |
| 1457 | && edit.at(0) == equality2.at(0)) { |
| 1458 | equality1 += edit.at(0); |
| 1459 | edit = edit.mid(1) + equality2.at(0); |
| 1460 | equality2 = equality2.mid(1); |
| 1461 | const int score = cleanupSemanticsScore(equality1, edit) |
| 1462 | + cleanupSemanticsScore(edit, equality2); |
| 1463 | if (score >= bestScore) { |
| 1464 | bestEquality1 = equality1; |
| 1465 | bestEdit = edit; |
| 1466 | bestEquality2 = equality2; |
| 1467 | bestScore = score; |
| 1468 | } |
| 1469 | } |
| 1470 | prevDiff.text = bestEquality1; |
| 1471 | thisDiff.text = bestEdit; |
| 1472 | nextDiff.text = bestEquality2; |
| 1473 | |
| 1474 | if (!bestEquality1.isEmpty()) |
| 1475 | newDiffList.append(prevDiff); // append modified equality1 |
| 1476 | if (bestEquality2.isEmpty()) { |
| 1477 | i++; |
| 1478 | if (i < diffList.size()) |
nothing calls this directly
no test coverage detected