Skip to content

Commit c36d881

Browse files
committed
Done! (excluding memory model)
1 parent b8810ec commit c36d881

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

DiffMatchPatch.zig

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ pub const Diff = struct {
3737
}
3838
};
3939

40-
/// Number of microseconds to map a diff before giving up (0 for infinity).
41-
diff_timeout: u64 = 1 * std.time.us_per_s,
40+
/// Number of milliseconds to map a diff before giving up (0 for infinity).
41+
diff_timeout: u64 = 1000,
4242
/// Cost of an empty edit operation in terms of edit characters.
4343
diff_edit_cost: u16 = 4,
4444

@@ -83,7 +83,7 @@ pub fn diff(
8383
const deadline = if (dmp.diff_timeout == 0)
8484
std.math.maxInt(u64)
8585
else
86-
@intCast(u64, std.time.microTimestamp()) + dmp.diff_timeout;
86+
@intCast(u64, std.time.milliTimestamp()) + dmp.diff_timeout;
8787
return dmp.diffInternal(allocator, before, after, check_lines, deadline);
8888
}
8989

@@ -99,7 +99,7 @@ fn diffInternal(
9999
var diffs = DiffList{};
100100
if (std.mem.eql(u8, before, after)) {
101101
if (before.len != 0) {
102-
try diffs.append(allocator, Diff.init(.equal, before));
102+
try diffs.append(allocator, Diff.init(.equal, try allocator.dupe(u8, before)));
103103
}
104104
return diffs;
105105
}
@@ -121,10 +121,10 @@ fn diffInternal(
121121

122122
// Restore the prefix and suffix.
123123
if (common_prefix.len != 0) {
124-
try diffs.insert(allocator, 0, Diff.init(.equal, common_prefix));
124+
try diffs.insert(allocator, 0, Diff.init(.equal, try allocator.dupe(u8, common_prefix)));
125125
}
126126
if (common_suffix.len != 0) {
127-
try diffs.append(allocator, Diff.init(.equal, common_suffix));
127+
try diffs.append(allocator, Diff.init(.equal, try allocator.dupe(u8, common_suffix)));
128128
}
129129

130130
try diffCleanupMerge(allocator, &diffs);
@@ -178,13 +178,13 @@ fn diffCompute(
178178

179179
if (before.len == 0) {
180180
// Just add some text (speedup).
181-
try diffs.append(allocator, Diff.init(.insert, after));
181+
try diffs.append(allocator, Diff.init(.insert, try allocator.dupe(u8, after)));
182182
return diffs;
183183
}
184184

185185
if (after.len == 0) {
186186
// Just delete some text (speedup).
187-
try diffs.append(allocator, Diff.init(.delete, before));
187+
try diffs.append(allocator, Diff.init(.delete, try allocator.dupe(u8, before)));
188188
return diffs;
189189
}
190190

@@ -197,9 +197,9 @@ fn diffCompute(
197197
.delete
198198
else
199199
.insert;
200-
try diffs.append(allocator, Diff.init(op, long_text[0..index]));
201-
try diffs.append(allocator, Diff.init(.equal, short_text));
202-
try diffs.append(allocator, Diff.init(op, long_text[index + short_text.len ..]));
200+
try diffs.append(allocator, Diff.init(op, try allocator.dupe(u8, long_text[0..index])));
201+
try diffs.append(allocator, Diff.init(.equal, try allocator.dupe(u8, short_text)));
202+
try diffs.append(allocator, Diff.init(op, try allocator.dupe(u8, long_text[index + short_text.len ..])));
203203
return diffs;
204204
}
205205

@@ -418,7 +418,7 @@ fn diffBisect(
418418
var d: isize = 0;
419419
while (d < max_d) : (d += 1) {
420420
// Bail out if deadline is reached.
421-
if (@intCast(u64, std.time.microTimestamp()) > deadline) {
421+
if (@intCast(u64, std.time.milliTimestamp()) > deadline) {
422422
break;
423423
}
424424

@@ -506,8 +506,8 @@ fn diffBisect(
506506
// Diff took too long and hit the deadline or
507507
// number of diffs equals number of characters, no commonality at all.
508508
var diffs = DiffList{};
509-
try diffs.append(allocator, Diff.init(.delete, before));
510-
try diffs.append(allocator, Diff.init(.insert, after));
509+
try diffs.append(allocator, Diff.init(.delete, try allocator.dupe(u8, before)));
510+
try diffs.append(allocator, Diff.init(.insert, try allocator.dupe(u8, after)));
511511
return diffs;
512512
}
513513

@@ -957,7 +957,7 @@ fn diffCleanupSemantic(allocator: std.mem.Allocator, diffs: *DiffList) DiffError
957957
try diffs.insert(
958958
allocator,
959959
@intCast(usize, equalities.items[equalities.items.len - 1]),
960-
Diff.init(.delete, last_equality.?),
960+
Diff.init(.delete, try allocator.dupe(u8, last_equality.?)),
961961
);
962962
// Change second copy to insert.
963963
diffs.items[@intCast(usize, equalities.items[equalities.items.len - 1] + 1)].operation = .insert;
@@ -2001,20 +2001,20 @@ test diff {
20012001
try diffs.appendSlice(arena.allocator(), &.{ Diff.init(.insert, " "), Diff.init(.equal, "a"), Diff.init(.insert, "nd"), Diff.init(.equal, " [[Pennsylvania]]"), Diff.init(.delete, " and [[New") });
20022002
try testing.expectEqualDeep(diffs.items, (try this.diff(arena.allocator(), "a [[Pennsylvania]] and [[New", " and [[Pennsylvania]]", false)).items); // diff: Large equality.
20032003

2004-
this.diff_timeout = 100 * std.time.ns_per_ms; // 100ms
2004+
this.diff_timeout = 100; // 100ms
20052005
// Increase the text lengths by 1024 times to ensure a timeout.
20062006
{
2007-
const a = "`Twas brillig, and the slithy toves\nDid gyre and gimble in the wabe:\nAll mimsy were the borogoves,\nAnd the mome raths outgrabe.\n" ** 10;
2008-
const b = "I am the very model of a modern major general,\nI've information vegetable, animal, and mineral,\nI know the kings of England, and I quote the fights historical,\nFrom Marathon to Waterloo, in order categorical.\n" ** 10;
2007+
const a = "`Twas brillig, and the slithy toves\nDid gyre and gimble in the wabe:\nAll mimsy were the borogoves,\nAnd the mome raths outgrabe.\n" ** 1024;
2008+
const b = "I am the very model of a modern major general,\nI've information vegetable, animal, and mineral,\nI know the kings of England, and I quote the fights historical,\nFrom Marathon to Waterloo, in order categorical.\n" ** 1024;
20092009
const start_time = std.time.milliTimestamp();
20102010
_ = try this.diff(arena.allocator(), a, b, false); // Travis - TODO not sure what the third arg should be
20112011
const end_time = std.time.milliTimestamp();
20122012
// Test that we took at least the timeout period.
2013-
try testing.expect((this.diff_timeout * 1000) * 10000 <= end_time - start_time); // diff: Timeout min.
2013+
try testing.expect(this.diff_timeout <= end_time - start_time); // diff: Timeout min.
20142014
// Test that we didn't take forever (be forgiving).
20152015
// Theoretically this test could fail very occasionally if the
20162016
// OS task swaps or locks up for a second at the wrong moment.
2017-
try testing.expect((this.diff_timeout * 1000) * 10000 * 2 > end_time - start_time); // diff: Timeout max.
2017+
try testing.expect((this.diff_timeout) * 10000 * 2 > end_time - start_time); // diff: Timeout max.
20182018
this.diff_timeout = 0;
20192019
}
20202020
{

0 commit comments

Comments
 (0)