* @brief Scans one text file for a needle, appending bounded line-level hits. */
| 460 | * @brief Scans one text file for a needle, appending bounded line-level hits. |
| 461 | */ |
| 462 | static void searchFile(const QString& path, |
| 463 | const QString& root, |
| 464 | const QRegularExpression& needle, |
| 465 | QJsonArray& hits, |
| 466 | bool& truncated) |
| 467 | { |
| 468 | QFile file(path); |
| 469 | if (!file.open(QIODevice::ReadOnly)) |
| 470 | return; |
| 471 | |
| 472 | if (looksBinary(file.peek(AI::FileSandbox::kBinarySniffBytes))) |
| 473 | return; |
| 474 | |
| 475 | QTextStream stream(&file); |
| 476 | int lineNo = 0; |
| 477 | while (!stream.atEnd()) { |
| 478 | const auto line = stream.readLine(); |
| 479 | ++lineNo; |
| 480 | if (!needle.match(line).hasMatch()) |
| 481 | continue; |
| 482 | |
| 483 | if (hits.size() >= AI::FileSandbox::kMaxSearchHits) { |
| 484 | truncated = true; |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | QJsonObject row; |
| 489 | row[QStringLiteral("file")] = displayPath(path, root); |
| 490 | row[QStringLiteral("line")] = lineNo; |
| 491 | row[QStringLiteral("text")] = line.left(400); |
| 492 | hits.append(row); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * @brief Walks the read roots, collecting files to search within the scan cap. |
no test coverage detected