Skip to content

Commit 1ec5829

Browse files
Fix diff cleanup semantic (#9)
* copy over some doc comments * minor cleanups to improve readability only formatting and renaming, no semantic changes * ArrayListUnmanaged(Diff) => DiffList * std.testing => testing * use Diff.init() consistently * use DiffError consistently * break up some long lines * get test diffCleanupSemantic passing the only real change here is the last line in this diff. the rest is cleanups.
1 parent a8db970 commit 1ec5829

1 file changed

Lines changed: 20 additions & 20 deletions

File tree

DiffMatchPatch.zig

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,7 @@ fn diffCompute(
188188
const long_text = if (before.len > after.len) before else after;
189189
const short_text = if (before.len > after.len) after else before;
190190

191-
var short_text_in_long_text_index = std.mem.indexOf(u8, long_text, short_text);
192-
if (short_text_in_long_text_index) |index| {
191+
if (std.mem.indexOf(u8, long_text, short_text)) |index| {
193192
// Shorter text is inside the longer text (speedup).
194193
const op: Diff.Operation = if (before.len > after.len)
195194
.delete
@@ -210,8 +209,7 @@ fn diffCompute(
210209
}
211210

212211
// Check to see if the problem can be split in two.
213-
var maybe_half_match = try dmp.diffHalfMatch(allocator, before, after);
214-
if (maybe_half_match) |half_match| {
212+
if (try dmp.diffHalfMatch(allocator, before, after)) |half_match| {
215213
// A half-match was found, sort out the return data.
216214

217215
// Send both pairs off for separate processing.
@@ -230,7 +228,8 @@ fn diffCompute(
230228
deadline,
231229
);
232230
defer diffs_b.deinit(allocator);
233-
231+
var tmp_diffs = diffs;
232+
defer tmp_diffs.deinit(allocator);
234233
// Merge the results.
235234
diffs = diffs_a;
236235
try diffs.append(allocator, Diff.init(.equal, half_match.common_middle));
@@ -400,10 +399,10 @@ fn diffBisect(
400399
}
401400
v1.items[@intCast(usize, v_offset + 1)] = 0;
402401
v2.items[@intCast(usize, v_offset + 1)] = 0;
403-
var delta = before_length - after_length;
402+
const delta = before_length - after_length;
404403
// If the total number of characters is odd, then the front path will
405404
// collide with the reverse path.
406-
var front = (@mod(delta, 2) != 0);
405+
const front = delta & 1 == 1;
407406
// Offsets for start and end of k loop.
408407
// Prevents mapping of space beyond the grid.
409408
var k1start: isize = 0;
@@ -448,7 +447,7 @@ fn diffBisect(
448447
var k2_offset = v_offset + delta - k1;
449448
if (k2_offset >= 0 and k2_offset < v_length and v2.items[@intCast(usize, k2_offset)] != -1) {
450449
// Mirror x2 onto top-left coordinate system.
451-
var x2: isize = before_length - v2.items[@intCast(usize, k2_offset)];
450+
const x2 = before_length - v2.items[@intCast(usize, k2_offset)];
452451
if (x1 >= x2) {
453452
// Overlap detected.
454453
return dmp.diffBisectSplit(allocator, before, after, x1, y1, deadline);
@@ -458,38 +457,39 @@ fn diffBisect(
458457
}
459458

460459
// Walk the reverse path one step.
461-
var k2: isize = -@intCast(isize, d) + @intCast(isize, k2start);
460+
var k2: isize = -d + k2start;
462461
while (k2 <= d - k2end) : (k2 += 2) {
463-
var k2_offset: isize = @intCast(isize, v_offset) + k2;
462+
const k2_offset = v_offset + k2;
464463
var x2: isize = 0;
465-
if (k2 == -@intCast(isize, d) or k2 != d and
464+
if (k2 == -d or k2 != d and
466465
v2.items[@intCast(usize, k2_offset - 1)] < v2.items[@intCast(usize, k2_offset + 1)])
467466
{
468467
x2 = v2.items[@intCast(usize, k2_offset + 1)];
469468
} else {
470469
x2 = v2.items[@intCast(usize, k2_offset - 1)] + 1;
471470
}
472471
var y2: isize = x2 - k2;
473-
while (x2 < before.len and y2 < after.len and
474-
before[@intCast(usize, @intCast(isize, before.len) - x2 - 1)] == after[@intCast(usize, @intCast(isize, after.len) - y2 - 1)])
472+
while (x2 < before_length and y2 < after_length and
473+
before[@intCast(usize, before_length - x2 - 1)] ==
474+
after[@intCast(usize, after_length - y2 - 1)])
475475
{
476476
x2 += 1;
477477
y2 += 1;
478478
}
479479
v2.items[@intCast(usize, k2_offset)] = x2;
480-
if (x2 > before.len) {
480+
if (x2 > before_length) {
481481
// Ran off the left of the graph.
482482
k2end += 2;
483-
} else if (y2 > after.len) {
483+
} else if (y2 > after_length) {
484484
// Ran off the top of the graph.
485485
k2start += 2;
486486
} else if (!front) {
487-
var k1_offset: isize = @intCast(isize, v_offset) + delta - k2;
487+
const k1_offset = v_offset + delta - k2;
488488
if (k1_offset >= 0 and k1_offset < v_length and v1.items[@intCast(usize, k1_offset)] != -1) {
489-
var x1: isize = v1.items[@intCast(usize, k1_offset)];
490-
var y1: isize = @intCast(isize, v_offset) + x1 - k1_offset;
489+
const x1 = v1.items[@intCast(usize, k1_offset)];
490+
const y1 = v_offset + x1 - k1_offset;
491491
// Mirror x2 onto top-left coordinate system.
492-
x2 = @intCast(isize, before.len) - v2.items[@intCast(usize, k2_offset)];
492+
x2 = before_length - v2.items[@intCast(usize, k2_offset)];
493493
if (x1 >= x2) {
494494
// Overlap detected.
495495
return dmp.diffBisectSplit(allocator, before, after, x1, y1, deadline);
@@ -937,7 +937,7 @@ fn diffCleanupSemantic(allocator: std.mem.Allocator, diffs: *DiffList) DiffError
937937
length_deletions2 = 0;
938938
last_equality = diffs.items[@intCast(usize, pointer)].text;
939939
} else { // an insertion or deletion
940-
if (diffs.items[@intCast(usize, pointer)].operation == .equal) {
940+
if (diffs.items[@intCast(usize, pointer)].operation == .insert) {
941941
length_insertions2 += diffs.items[@intCast(usize, pointer)].text.len;
942942
} else {
943943
length_deletions2 += diffs.items[@intCast(usize, pointer)].text.len;

0 commit comments

Comments
 (0)