My own diff-invention: Builds DiffList for scratch. Automaticly clears all previous data in list. */
| 1233 | Builds DiffList for scratch. Automaticly clears all previous data in list. |
| 1234 | */ |
| 1235 | void DiffList::calcDiff(const QString& line1, const QString& line2, const qint32 maxSearchRange) |
| 1236 | { |
| 1237 | clear(); |
| 1238 | |
| 1239 | const QChar* p1end = line1.constData() + line1.size(); |
| 1240 | const QChar* p2end = line2.constData() + line2.size(); |
| 1241 | |
| 1242 | QString::const_iterator p1 = line1.cbegin(), p2 = line2.cbegin(); |
| 1243 | |
| 1244 | /* |
| 1245 | This loop should never reach the exit condition specified here. However it must have a hard wired |
| 1246 | stopping point to prevent runaway allocation if something unexpected happens. |
| 1247 | diffList is therefor hard capped at aprox 50 MB in size. |
| 1248 | */ |
| 1249 | for(; size() * sizeof(Diff) + sizeof(DiffList) < (50 << 20);) |
| 1250 | { |
| 1251 | qint32 nofEquals = 0; |
| 1252 | while(p1 != line1.cend() && p2 != line2.cend() && *p1 == *p2) |
| 1253 | { |
| 1254 | ++p1; |
| 1255 | ++p2; |
| 1256 | ++nofEquals; |
| 1257 | } |
| 1258 | |
| 1259 | bool bBestValid = false; |
| 1260 | qint32 bestI1 = 0; |
| 1261 | qint32 bestI2 = 0; |
| 1262 | qint32 i1 = 0; |
| 1263 | qint32 i2 = 0; |
| 1264 | |
| 1265 | for(i1 = 0;; ++i1) |
| 1266 | { |
| 1267 | if(&p1[i1] == p1end || (bBestValid && i1 >= bestI1 + bestI2)) |
| 1268 | { |
| 1269 | break; |
| 1270 | } |
| 1271 | for(i2 = 0; i2 < maxSearchRange; ++i2) |
| 1272 | { |
| 1273 | if (&p2[i2] == p2end || (bBestValid && i1 + i2 >= bestI1 + bestI2)) |
| 1274 | { |
| 1275 | break; |
| 1276 | } |
| 1277 | else if(p2[i2] == p1[i1] && |
| 1278 | (abs(i1 - i2) < 3 || (&p2[i2 + 1] == p2end && &p1[i1 + 1] == p1end) || |
| 1279 | (&p2[i2 + 1] != p2end && &p1[i1 + 1] != p1end && p2[i2 + 1] == p1[i1 + 1]))) |
| 1280 | { |
| 1281 | if(i1 + i2 < bestI1 + bestI2 || !bBestValid) |
| 1282 | { |
| 1283 | bestI1 = i1; |
| 1284 | bestI2 = i2; |
| 1285 | bBestValid = true; |
| 1286 | break; |
| 1287 | } |
| 1288 | } |
| 1289 | } |
| 1290 | /* |
| 1291 | Bail this should never happen. |
| 1292 | Acts back stop against harder to detect overfollow issues. |
no test coverage detected