| 485 | } |
| 486 | |
| 487 | QString ChangesManager::findBestMatchLineBased( |
| 488 | const QString &fileContent, |
| 489 | const QString &searchContent, |
| 490 | double threshold, |
| 491 | double *outSimilarity) const |
| 492 | { |
| 493 | QStringList fileLines = fileContent.split('\n'); |
| 494 | QStringList searchLines = searchContent.split('\n'); |
| 495 | |
| 496 | if (searchLines.isEmpty() || fileLines.isEmpty()) { |
| 497 | if (outSimilarity) *outSimilarity = 0.0; |
| 498 | return QString(); |
| 499 | } |
| 500 | |
| 501 | if (searchLines.size() > fileLines.size()) { |
| 502 | if (outSimilarity) *outSimilarity = 0.0; |
| 503 | return QString(); |
| 504 | } |
| 505 | |
| 506 | QString bestMatch; |
| 507 | double bestSimilarity = 0.0; |
| 508 | int searchLineCount = searchLines.size(); |
| 509 | |
| 510 | LOG_MESSAGE(QString("Line-based search: %1 search lines in %2 file lines") |
| 511 | .arg(searchLineCount).arg(fileLines.size())); |
| 512 | |
| 513 | for (int i = 0; i <= fileLines.size() - searchLineCount; ++i) { |
| 514 | int matchingLines = 0; |
| 515 | int totalLines = searchLineCount; |
| 516 | |
| 517 | for (int j = 0; j < searchLineCount; ++j) { |
| 518 | if (fileLines[i + j] == searchLines[j]) { |
| 519 | matchingLines++; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | double similarity = static_cast<double>(matchingLines) / totalLines; |
| 524 | |
| 525 | if (similarity > bestSimilarity) { |
| 526 | bestSimilarity = similarity; |
| 527 | if (similarity >= threshold) { |
| 528 | QStringList matchedLines; |
| 529 | for (int j = 0; j < searchLineCount; ++j) { |
| 530 | matchedLines.append(fileLines[i + j]); |
| 531 | } |
| 532 | bestMatch = matchedLines.join('\n'); |
| 533 | |
| 534 | if (similarity >= 0.99) { |
| 535 | if (outSimilarity) *outSimilarity = similarity; |
| 536 | LOG_MESSAGE(QString("Found exact line match at line %1").arg(i + 1)); |
| 537 | return bestMatch; |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | if (outSimilarity) { |
| 544 | *outSimilarity = bestSimilarity; |