-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdiff_utils.cpp
More file actions
332 lines (294 loc) · 9.88 KB
/
Copy pathdiff_utils.cpp
File metadata and controls
332 lines (294 loc) · 9.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "diff_utils.hpp"
#include <algorithm>
#include <cstddef>
#include <sstream>
#include <vector>
namespace acecode {
namespace {
constexpr size_t kDiffContextLines = 3;
// 中间段超过这个乘积就退回"整段替换",避免 O(n*m) 在超大文件上爆内存。
constexpr size_t kMaxLcsCells = 1500000;
// 按行切开,丢掉行尾 CR,这样 CRLF / LF / 混用不会把每一行都标成改动。
static std::vector<std::string> split_lines(const std::string& s) {
std::vector<std::string> lines;
std::istringstream iss(s);
std::string line;
while (std::getline(iss, line)) {
if (!line.empty() && line.back() == '\r') line.pop_back();
lines.push_back(std::move(line));
}
return lines;
}
enum class LineOpKind {
Keep,
Del,
Add,
};
struct LineOp {
LineOpKind kind = LineOpKind::Keep;
size_t old_index = 0;
size_t new_index = 0;
};
struct ChangeSpan {
size_t old_start = 0;
size_t old_end = 0;
size_t new_start = 0;
size_t new_end = 0;
};
static std::vector<LineOp> lcs_edit_script(
const std::vector<std::string>& a,
const std::vector<std::string>& b
) {
const size_t n = a.size();
const size_t m = b.size();
std::vector<LineOp> ops;
if (n == 0 && m == 0) return ops;
// 超大中间段:不做 LCS,整段删再整段加(旧行为),保证工具路径可预测。
if (n > 0 && m > 0 && n > kMaxLcsCells / m) {
ops.reserve(n + m);
for (size_t i = 0; i < n; ++i) ops.push_back({LineOpKind::Del, i, 0});
for (size_t j = 0; j < m; ++j) ops.push_back({LineOpKind::Add, 0, j});
return ops;
}
std::vector<std::vector<int>> dp(n + 1, std::vector<int>(m + 1, 0));
for (size_t i = 1; i <= n; ++i) {
for (size_t j = 1; j <= m; ++j) {
if (a[i - 1] == b[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = (dp[i - 1][j] >= dp[i][j - 1]) ? dp[i - 1][j] : dp[i][j - 1];
}
}
}
std::vector<LineOp> rev;
size_t i = n;
size_t j = m;
while (i > 0 && j > 0) {
if (a[i - 1] == b[j - 1]) {
rev.push_back({LineOpKind::Keep, i - 1, j - 1});
--i;
--j;
} else if (dp[i - 1][j] >= dp[i][j - 1]) {
rev.push_back({LineOpKind::Del, i - 1, j});
--i;
} else {
rev.push_back({LineOpKind::Add, i, j - 1});
--j;
}
}
while (i > 0) {
rev.push_back({LineOpKind::Del, i - 1, j});
--i;
}
while (j > 0) {
rev.push_back({LineOpKind::Add, i, j - 1});
--j;
}
std::reverse(rev.begin(), rev.end());
return rev;
}
static std::vector<ChangeSpan> collect_change_spans(const std::vector<LineOp>& ops) {
std::vector<ChangeSpan> spans;
ChangeSpan current;
bool open = false;
size_t old_i = 0;
size_t new_i = 0;
auto flush = [&]() {
if (!open) return;
spans.push_back(current);
open = false;
};
for (const auto& op : ops) {
if (op.kind == LineOpKind::Keep) {
flush();
++old_i;
++new_i;
continue;
}
if (!open) {
current = ChangeSpan{old_i, old_i, new_i, new_i};
open = true;
}
if (op.kind == LineOpKind::Del) {
current.old_end = ++old_i;
} else {
current.new_end = ++new_i;
}
}
flush();
return spans;
}
static std::vector<ChangeSpan> merge_change_spans(std::vector<ChangeSpan> spans) {
if (spans.empty()) return spans;
std::vector<ChangeSpan> merged;
merged.push_back(spans.front());
for (size_t i = 1; i < spans.size(); ++i) {
auto& prev = merged.back();
const auto& next = spans[i];
const bool close_old = next.old_start <= prev.old_end + 2 * kDiffContextLines;
const bool close_new = next.new_start <= prev.new_end + 2 * kDiffContextLines;
if (close_old && close_new) {
prev.old_end = next.old_end;
prev.new_end = next.new_end;
} else {
merged.push_back(next);
}
}
return merged;
}
static DiffHunk make_hunk(
const std::vector<std::string>& old_lines,
const std::vector<std::string>& new_lines,
const ChangeSpan& change
) {
const size_t pre = std::min({kDiffContextLines, change.old_start, change.new_start});
const size_t post = std::min({
kDiffContextLines,
old_lines.size() - change.old_end,
new_lines.size() - change.new_end
});
const size_t hunk_old_begin = change.old_start - pre;
const size_t hunk_old_end = change.old_end + post;
const size_t hunk_new_begin = change.new_start - pre;
const size_t hunk_new_end = change.new_end + post;
DiffHunk hunk;
hunk.old_start = static_cast<int>(hunk_old_begin + 1);
hunk.new_start = static_cast<int>(hunk_new_begin + 1);
hunk.old_count = static_cast<int>(hunk_old_end - hunk_old_begin);
hunk.new_count = static_cast<int>(hunk_new_end - hunk_new_begin);
int next_old_no = hunk.old_start;
int next_new_no = hunk.new_start;
for (size_t i = hunk_old_begin; i < change.old_start; ++i) {
DiffLine line;
line.kind = DiffLineKind::Context;
line.text = old_lines[i];
line.old_line_no = next_old_no++;
line.new_line_no = next_new_no++;
hunk.lines.push_back(std::move(line));
}
for (size_t i = change.old_start; i < change.old_end; ++i) {
DiffLine line;
line.kind = DiffLineKind::Removed;
line.text = old_lines[i];
line.old_line_no = next_old_no++;
hunk.lines.push_back(std::move(line));
}
for (size_t i = change.new_start; i < change.new_end; ++i) {
DiffLine line;
line.kind = DiffLineKind::Added;
line.text = new_lines[i];
line.new_line_no = next_new_no++;
hunk.lines.push_back(std::move(line));
}
for (size_t i = change.old_end; i < hunk_old_end; ++i) {
DiffLine line;
line.kind = DiffLineKind::Context;
line.text = old_lines[i];
line.old_line_no = next_old_no++;
line.new_line_no = next_new_no++;
hunk.lines.push_back(std::move(line));
}
return hunk;
}
} // namespace
std::vector<DiffHunk> generate_structured_diff(
const std::string& old_content,
const std::string& new_content,
const std::string& /*file_path*/
) {
const auto old_lines = split_lines(old_content);
const auto new_lines = split_lines(new_content);
size_t prefix = 0;
while (prefix < old_lines.size() && prefix < new_lines.size() &&
old_lines[prefix] == new_lines[prefix]) {
++prefix;
}
size_t suffix = 0;
while (suffix < (old_lines.size() - prefix) &&
suffix < (new_lines.size() - prefix) &&
old_lines[old_lines.size() - 1 - suffix] ==
new_lines[new_lines.size() - 1 - suffix]) {
++suffix;
}
if (prefix + suffix == old_lines.size() && prefix + suffix == new_lines.size()) {
return {};
}
const std::vector<std::string> old_mid(
old_lines.begin() + static_cast<std::ptrdiff_t>(prefix),
old_lines.end() - static_cast<std::ptrdiff_t>(suffix));
const std::vector<std::string> new_mid(
new_lines.begin() + static_cast<std::ptrdiff_t>(prefix),
new_lines.end() - static_cast<std::ptrdiff_t>(suffix));
auto ops = lcs_edit_script(old_mid, new_mid);
for (auto& op : ops) {
op.old_index += prefix;
op.new_index += prefix;
}
// 公共前缀/后缀是 Keep,这样 hunk 能带上两侧 context。
std::vector<LineOp> full_ops;
full_ops.reserve(prefix + ops.size() + suffix);
for (size_t i = 0; i < prefix; ++i) {
full_ops.push_back({LineOpKind::Keep, i, i});
}
full_ops.insert(full_ops.end(), ops.begin(), ops.end());
for (size_t i = 0; i < suffix; ++i) {
const size_t old_i = old_lines.size() - suffix + i;
const size_t new_i = new_lines.size() - suffix + i;
full_ops.push_back({LineOpKind::Keep, old_i, new_i});
}
const auto spans = merge_change_spans(collect_change_spans(full_ops));
std::vector<DiffHunk> hunks;
hunks.reserve(spans.size());
for (const auto& span : spans) {
hunks.push_back(make_hunk(old_lines, new_lines, span));
}
return hunks;
}
std::string generate_unified_diff(
const std::string& old_content,
const std::string& new_content,
const std::string& file_path,
DiffStats& stats
) {
stats.additions = 0;
stats.deletions = 0;
auto hunks = generate_structured_diff(old_content, new_content, file_path);
std::ostringstream out;
out << "--- " << file_path << "\n";
out << "+++ " << file_path << "\n";
if (hunks.empty()) {
// 保持和旧实现的输出格式兼容:虽然没有 hunk,但标头仍输出。
return out.str();
}
for (const auto& hunk : hunks) {
out << "@@ -" << hunk.old_start << "," << hunk.old_count
<< " +" << hunk.new_start << "," << hunk.new_count << " @@\n";
for (const auto& line : hunk.lines) {
char marker = ' ';
switch (line.kind) {
case DiffLineKind::Added:
marker = '+';
++stats.additions;
break;
case DiffLineKind::Removed:
marker = '-';
++stats.deletions;
break;
case DiffLineKind::Context:
marker = ' ';
break;
}
out << marker << line.text << "\n";
}
}
return out.str();
}
std::string generate_unified_diff(
const std::string& old_content,
const std::string& new_content,
const std::string& file_path
) {
DiffStats discard;
return generate_unified_diff(old_content, new_content, file_path, discard);
}
} // namespace acecode