Text: add checked patch apply and unified rendering (M-TEXT-06)
This commit is contained in:
1 parent
48e5e223af
commit
17a23bf88a
2 files changed
+232
No files matched your search
@@ -4,6 +4,7 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <format>
|
||||
#include <functional>
|
||||
#include <limits>
|
||||
#include <span>
|
||||
@@ -359,4 +360,118 @@ template <typename T>
|
||||
return out;
|
||||
}
|
||||
|
||||
// ── checked patch application & rendering ────────────────────────────────────
|
||||
|
||||
enum class PatchFailure { None, SourceMismatch, OutputTooLarge };
|
||||
|
||||
// Apply an edit script to `source`, checking that every Equal/Delete entry
|
||||
// matches the source element in order and that the whole source is consumed.
|
||||
// Returns PatchFailure::SourceMismatch when the script does not belong to the
|
||||
// source, and OutputTooLarge when the result exceeds `max_output` elements.
|
||||
template <typename T>
|
||||
[[nodiscard]] std::expected<std::vector<T>, PatchFailure>
|
||||
apply_checked(std::span<const T> source, const std::vector<Edit<T>>& edits,
|
||||
std::size_t max_output = std::numeric_limits<std::size_t>::max())
|
||||
{
|
||||
std::vector<T> out;
|
||||
out.reserve(std::min(source.size() + edits.size(), max_output));
|
||||
std::size_t cursor = 0;
|
||||
for (const auto& e : edits) {
|
||||
switch (e.op) {
|
||||
case Op::Equal:
|
||||
case Op::Delete:
|
||||
if (cursor >= source.size() || !(source[cursor] == e.value))
|
||||
return std::unexpected{PatchFailure::SourceMismatch};
|
||||
++cursor;
|
||||
if (e.op == Op::Equal) {
|
||||
if (out.size() >= max_output)
|
||||
return std::unexpected{PatchFailure::OutputTooLarge};
|
||||
out.push_back(e.value);
|
||||
}
|
||||
break;
|
||||
case Op::Insert:
|
||||
if (out.size() >= max_output)
|
||||
return std::unexpected{PatchFailure::OutputTooLarge};
|
||||
out.push_back(e.value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (cursor != source.size())
|
||||
return std::unexpected{PatchFailure::SourceMismatch};
|
||||
return out;
|
||||
}
|
||||
|
||||
// Render line edits as a unified diff with `context` unchanged lines around
|
||||
// each change and merged overlapping hunks.
|
||||
[[nodiscard]] inline std::string
|
||||
render_unified(const std::vector<LineDiff>& edits,
|
||||
std::string_view left_name = "a",
|
||||
std::string_view right_name = "b",
|
||||
std::size_t context = 3,
|
||||
std::size_t max_output_bytes =
|
||||
std::numeric_limits<std::size_t>::max())
|
||||
{
|
||||
std::string out;
|
||||
out += "--- ";
|
||||
out += left_name;
|
||||
out += '\n';
|
||||
out += "+++ ";
|
||||
out += right_name;
|
||||
out += '\n';
|
||||
|
||||
// Old/new line number (1-based) before each edit is consumed.
|
||||
struct Entry { const LineDiff* edit; std::size_t old_line; std::size_t new_line; };
|
||||
std::vector<Entry> entries;
|
||||
entries.reserve(edits.size());
|
||||
std::size_t old_line = 1, new_line = 1;
|
||||
for (const auto& e : edits) {
|
||||
entries.push_back({&e, old_line, new_line});
|
||||
if (e.op != Op::Insert) ++old_line;
|
||||
if (e.op != Op::Delete) ++new_line;
|
||||
}
|
||||
|
||||
std::vector<std::size_t> changes;
|
||||
for (std::size_t i = 0; i < entries.size(); ++i)
|
||||
if (entries[i].edit->op != Op::Equal) changes.push_back(i);
|
||||
if (changes.empty()) return out;
|
||||
|
||||
std::size_t index = 0;
|
||||
while (index < changes.size()) {
|
||||
const std::size_t first = changes[index];
|
||||
std::size_t last = first;
|
||||
++index;
|
||||
while (index < changes.size() &&
|
||||
changes[index] <= last + 2 * context + 1) {
|
||||
last = changes[index];
|
||||
++index;
|
||||
}
|
||||
const std::size_t begin = first > context ? first - context : 0;
|
||||
const std::size_t end = std::min(entries.size() - 1, last + context);
|
||||
|
||||
std::size_t old_count = 0, new_count = 0;
|
||||
for (std::size_t i = begin; i <= end; ++i) {
|
||||
if (entries[i].edit->op != Op::Insert) ++old_count;
|
||||
if (entries[i].edit->op != Op::Delete) ++new_count;
|
||||
}
|
||||
const std::size_t old_start = old_count == 0 ? entries[begin].old_line - 1
|
||||
: entries[begin].old_line;
|
||||
const std::size_t new_start = new_count == 0 ? entries[begin].new_line - 1
|
||||
: entries[begin].new_line;
|
||||
out += std::format("@@ -{},{} +{},{} @@\n", old_start, old_count,
|
||||
new_start, new_count);
|
||||
for (std::size_t i = begin; i <= end; ++i) {
|
||||
switch (entries[i].edit->op) {
|
||||
case Op::Equal: out += ' '; break;
|
||||
case Op::Insert: out += '+'; break;
|
||||
case Op::Delete: out += '-'; break;
|
||||
}
|
||||
out += entries[i].edit->value;
|
||||
out += '\n';
|
||||
if (out.size() > max_output_bytes)
|
||||
return out.substr(0, max_output_bytes);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace Void::Diff
|
||||
@@ -0,0 +1,117 @@
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
#include <Void/Text/Diff.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace Void::Diff;
|
||||
|
||||
std::vector<LineDiff> sample_edits()
|
||||
{
|
||||
return {
|
||||
LineDiff{Op::Equal, "a"},
|
||||
LineDiff{Op::Delete, "b"},
|
||||
LineDiff{Op::Insert, "B"},
|
||||
LineDiff{Op::Equal, "c"},
|
||||
};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test text_apply_checked_round_trips_and_rejects_mismatch()
|
||||
{
|
||||
// Round trip over generic sequences: applying a diff to its source
|
||||
// reconstructs the target.
|
||||
for (int i = 0; i < 40; ++i) {
|
||||
std::vector<int> left;
|
||||
std::vector<int> right;
|
||||
for (int k = 0; k < i % 7; ++k) {
|
||||
left.push_back((k * 3 + i) % 5);
|
||||
}
|
||||
for (int k = 0; k < i % 5; ++k) {
|
||||
right.push_back((k * 2 + i) % 4);
|
||||
}
|
||||
auto edits = diff_bounded(std::span<const int>{left}, std::span<const int>{right});
|
||||
Void::Testing::require(edits.has_value());
|
||||
auto applied = apply_checked(std::span<const int>{left}, *edits);
|
||||
Void::Testing::require(applied.has_value());
|
||||
Void::Testing::expect(*applied == right);
|
||||
|
||||
// A source that does not match the script is rejected.
|
||||
std::vector<int> wrong = left;
|
||||
wrong.push_back(99);
|
||||
auto rejected = apply_checked(std::span<const int>{wrong}, *edits);
|
||||
Void::Testing::expect(!rejected.has_value());
|
||||
Void::Testing::expect(rejected.error() == PatchFailure::SourceMismatch);
|
||||
|
||||
// The output bound is enforced.
|
||||
auto bounded = apply_checked(std::span<const int>{left}, *edits, 0);
|
||||
if (!right.empty()) {
|
||||
Void::Testing::expect(!bounded.has_value());
|
||||
Void::Testing::expect(bounded.error() == PatchFailure::OutputTooLarge);
|
||||
}
|
||||
}
|
||||
|
||||
// Line text round trip using the same split semantics as line_diff.
|
||||
const std::string a{"one\ntwo\nthree\n"};
|
||||
const std::string b{"one\n2\nthree\nfour\n"};
|
||||
auto line_edits = line_diff(a, b);
|
||||
std::vector<std::string> a_lines;
|
||||
std::size_t pos = 0;
|
||||
while (pos <= a.size()) {
|
||||
const auto nl = a.find('\n', pos);
|
||||
a_lines.push_back(a.substr(pos, nl == std::string::npos ? nl : nl - pos));
|
||||
if (nl == std::string::npos) break;
|
||||
pos = nl + 1;
|
||||
}
|
||||
auto applied = apply_checked(std::span<const std::string>{a_lines}, line_edits);
|
||||
Void::Testing::require(applied.has_value());
|
||||
std::string rebuilt;
|
||||
for (std::size_t i = 0; i < applied->size(); ++i) {
|
||||
if (i != 0) {
|
||||
rebuilt += '\n';
|
||||
}
|
||||
rebuilt += (*applied)[i];
|
||||
}
|
||||
Void::Testing::expect(rebuilt == b);
|
||||
}
|
||||
|
||||
Void::Testing::Test text_render_unified_hunks()
|
||||
{
|
||||
const auto edits = sample_edits();
|
||||
const std::string patch = render_unified(edits, "left", "right");
|
||||
|
||||
Void::Testing::expect(patch.starts_with("--- left\n+++ right\n"));
|
||||
Void::Testing::expect(patch.find("@@ -1,3 +1,3 @@") != std::string::npos);
|
||||
Void::Testing::expect(patch.find("\n a\n") != std::string::npos);
|
||||
Void::Testing::expect(patch.find("\n-b\n") != std::string::npos);
|
||||
Void::Testing::expect(patch.find("\n+B\n") != std::string::npos);
|
||||
Void::Testing::expect(patch.find("\n c\n") != std::string::npos);
|
||||
|
||||
// No changes: only the file headers are emitted.
|
||||
const std::vector<LineDiff> unchanged{LineDiff{Op::Equal, "x"}};
|
||||
Void::Testing::expect(render_unified(unchanged) == "--- a\n+++ b\n");
|
||||
|
||||
// Distant changes produce separate hunks; nearby ones merge.
|
||||
std::vector<LineDiff> distant;
|
||||
for (int i = 0; i < 30; ++i) {
|
||||
distant.push_back(LineDiff{Op::Equal, "line" + std::to_string(i)});
|
||||
if (i == 5 || i == 25) {
|
||||
distant.push_back(LineDiff{Op::Insert, "added" + std::to_string(i)});
|
||||
}
|
||||
}
|
||||
const std::string distant_patch = render_unified(distant, "a", "b", 1);
|
||||
std::size_t hunks = 0;
|
||||
for (std::size_t at = 0;
|
||||
(at = distant_patch.find("@@ -", at)) != std::string::npos; at += 4) {
|
||||
++hunks;
|
||||
}
|
||||
Void::Testing::expect_eq(hunks, std::size_t{2});
|
||||
|
||||
// The output bound truncates rather than growing without limit.
|
||||
const std::string bounded = render_unified(distant, "a", "b", 3, 40);
|
||||
Void::Testing::expect(bounded.size() <= 40);
|
||||
}
|
||||
Reference in new issue
Block a user