data/SieveBlockDiff.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <QByteArray> | ||
| 4 | #include <QList> | ||
| 5 | #include <QString> | ||
| 6 | #include <QStringList> | ||
| 7 | |||
| 8 | #include "data/LineDiff.h" | ||
| 9 | |||
| 10 | // Block-level comparison of two versions of a Sieve script (Sprint 95 | ||
| 11 | // S95.3.10, SIEVE-REVIEW): what an upload from the script editor or a | ||
| 12 | // restored version changes, in the words of the rule list — | ||
| 13 | // "Changed: Rule 12 · Added: Rule 73 · Removed: —" — with a line diff per | ||
| 14 | // block for the details. | ||
| 15 | // | ||
| 16 | // Blocks are matched by their "# rule:[Name]" marker; unnamed blocks by | ||
| 17 | // their exact bytes, then by position among the unmatched blocks of the | ||
| 18 | // same kind. The managed `require` line is reported separately. A version | ||
| 19 | // that does not parse is compared as one "whole script" block. | ||
| 20 | // | ||
| 21 | // Invariants: | ||
| 22 | // - diffScripts(a, a).identical() is true and lists no change. | ||
| 23 | // - Every block of `before` is unchanged, Changed, Moved or Removed; every | ||
| 24 | // block of `after` is unchanged, Changed, Moved or Added. | ||
| 25 | // - lineDiff() is LineDiff::diff of the two scripts' lines (exact LCS up | ||
| 26 | // to kExactLineLimit lines per side, Sprint 107 S107.2.2 moved it there). | ||
| 27 | // - Moved detection is O(n log n) in the number of blocks (S106-097). | ||
| 28 | namespace Sieve { | ||
| 29 | |||
| 30 | using DiffLine = LineDiff::Line; | ||
| 31 | |||
| 32 | struct BlockChange { | ||
| 33 | enum class Kind { Changed, Added, Removed, Moved }; | ||
| 34 | Kind kind = Kind::Changed; | ||
| 35 | QString name; // marker name, or the block's first line | ||
| 36 | bool named = false; | ||
| 37 | QList<DiffLine> lines; // Changed/Added/Removed; empty for Moved | ||
| 38 | }; | ||
| 39 | |||
| 40 | struct ScriptDiff { | ||
| 41 | QList<BlockChange> changes; | ||
| 42 | bool requireChanged = false; | ||
| 43 | bool wholeScript = false; // a side did not parse | ||
| 44 |
3/4✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 6 taken 10 times.
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 6 not taken.
|
11 | bool identical() const { return changes.isEmpty() && !requireChanged; } |
| 45 | }; | ||
| 46 | |||
| 47 | inline constexpr int kExactLineLimit = LineDiff::kExactLineLimit; | ||
| 48 | |||
| 49 | ScriptDiff diffScripts(const QByteArray &before, const QByteArray &after); | ||
| 50 | QList<DiffLine> lineDiff(const QByteArray &before, const QByteArray &after); | ||
| 51 | |||
| 52 | } // namespace Sieve | ||
| 53 |