| 1266 | } |
| 1267 | |
| 1268 | QList<Diff> Differ::merge(const QList<Diff> &diffList) |
| 1269 | { |
| 1270 | QString lastDelete; |
| 1271 | QString lastInsert; |
| 1272 | QList<Diff> newDiffList; |
| 1273 | for (int i = 0; i <= diffList.size(); i++) { |
| 1274 | Diff diff = i < diffList.size() |
| 1275 | ? diffList.at(i) |
| 1276 | : Diff(Diff::Equal); // dummy, ensure we process to the end |
| 1277 | // even when diffList doesn't end with equality |
| 1278 | if (diff.command == Diff::Delete) { |
| 1279 | lastDelete += diff.text; |
| 1280 | } else if (diff.command == Diff::Insert) { |
| 1281 | lastInsert += diff.text; |
| 1282 | } else { // Diff::Equal |
| 1283 | if (!(lastDelete.isEmpty() && lastInsert.isEmpty())) { |
| 1284 | |
| 1285 | // common prefix |
| 1286 | const int prefixCount = commonPrefix(lastDelete, lastInsert); |
| 1287 | if (prefixCount) { |
| 1288 | const QString prefix = lastDelete.left(prefixCount); |
| 1289 | lastDelete = lastDelete.mid(prefixCount); |
| 1290 | lastInsert = lastInsert.mid(prefixCount); |
| 1291 | |
| 1292 | if (!newDiffList.isEmpty() |
| 1293 | && newDiffList.last().command == Diff::Equal) { |
| 1294 | newDiffList.last().text += prefix; |
| 1295 | } else { |
| 1296 | newDiffList.append(Diff(Diff::Equal, prefix)); |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | // common suffix |
| 1301 | const int suffixCount = commonSuffix(lastDelete, lastInsert); |
| 1302 | if (suffixCount) { |
| 1303 | const QString suffix = lastDelete.right(suffixCount); |
| 1304 | lastDelete = lastDelete.left(lastDelete.size() - suffixCount); |
| 1305 | lastInsert = lastInsert.left(lastInsert.size() - suffixCount); |
| 1306 | |
| 1307 | diff.text.prepend(suffix); |
| 1308 | } |
| 1309 | |
| 1310 | // append delete / insert / equal |
| 1311 | if (!lastDelete.isEmpty()) |
| 1312 | newDiffList.append(Diff(Diff::Delete, lastDelete)); |
| 1313 | if (!lastInsert.isEmpty()) |
| 1314 | newDiffList.append(Diff(Diff::Insert, lastInsert)); |
| 1315 | if (!diff.text.isEmpty()) |
| 1316 | newDiffList.append(diff); |
| 1317 | lastDelete.clear(); |
| 1318 | lastInsert.clear(); |
| 1319 | } else { // join with last equal diff |
| 1320 | if (!newDiffList.isEmpty() |
| 1321 | && newDiffList.last().command == Diff::Equal) { |
| 1322 | newDiffList.last().text += diff.text; |
| 1323 | } else { |
| 1324 | if (!diff.text.isEmpty()) |
| 1325 | newDiffList.append(diff); |
nothing calls this directly
no test coverage detected