Text: add checked patch apply and unified rendering (M-TEXT-06)

This commit is contained in:
faumaray committed 2026-12-04 05:38:16 +03:00
1 parent 48e5e223af
commit 17a23bf88a
2 files changed
+232

No files matched your search

+115
View File
@@ -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