Skip to content

Commit 90353d4

Browse files
authored
Remove desitination type param from cast builtins (#15)
1 parent df02d43 commit 90353d4

File tree

1 file changed

+70
-70
lines changed

1 file changed

+70
-70
lines changed

DiffMatchPatch.zig

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -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.milliTimestamp()) + dmp.diff_timeout;
86+
@as(u64, @intCast(std.time.milliTimestamp())) + dmp.diff_timeout;
8787
return dmp.diffInternal(allocator, before, after, check_lines, deadline);
8888
}
8989

@@ -343,20 +343,20 @@ fn diffHalfMatchInternal(
343343
var best_short_text_b: []const u8 = "";
344344

345345
while (j < short_text.len and b: {
346-
j = @intCast(isize, std.mem.indexOf(u8, short_text[@intCast(usize, j + 1)..], seed) orelse break :b false) + j + 1;
346+
j = @as(isize, @intCast(std.mem.indexOf(u8, short_text[@as(usize, @intCast(j + 1))..], seed) orelse break :b false)) + j + 1;
347347
break :b true;
348348
}) {
349-
var prefix_length = diffCommonPrefix(long_text[i..], short_text[@intCast(usize, j)..]);
350-
var suffix_length = diffCommonSuffix(long_text[0..i], short_text[0..@intCast(usize, j)]);
349+
var prefix_length = diffCommonPrefix(long_text[i..], short_text[@as(usize, @intCast(j))..]);
350+
var suffix_length = diffCommonSuffix(long_text[0..i], short_text[0..@as(usize, @intCast(j))]);
351351
if (best_common.items.len < suffix_length + prefix_length) {
352352
best_common.items.len = 0;
353-
try best_common.appendSlice(allocator, short_text[@intCast(usize, j - @intCast(isize, suffix_length)) .. @intCast(usize, j - @intCast(isize, suffix_length)) + suffix_length]);
354-
try best_common.appendSlice(allocator, short_text[@intCast(usize, j) .. @intCast(usize, j) + prefix_length]);
353+
try best_common.appendSlice(allocator, short_text[@as(usize, @intCast(j - @as(isize, @intCast(suffix_length)))) .. @as(usize, @intCast(j - @as(isize, @intCast(suffix_length)))) + suffix_length]);
354+
try best_common.appendSlice(allocator, short_text[@as(usize, @intCast(j)) .. @as(usize, @intCast(j)) + prefix_length]);
355355

356356
best_long_text_a = long_text[0 .. i - suffix_length];
357357
best_long_text_b = long_text[i + prefix_length ..];
358-
best_short_text_a = short_text[0..@intCast(usize, j - @intCast(isize, suffix_length))];
359-
best_short_text_b = short_text[@intCast(usize, j + @intCast(isize, prefix_length))..];
358+
best_short_text_a = short_text[0..@as(usize, @intCast(j - @as(isize, @intCast(suffix_length))))];
359+
best_short_text_b = short_text[@as(usize, @intCast(j + @as(isize, @intCast(prefix_length))))..];
360360
}
361361
}
362362
if (best_common.items.len * 2 >= long_text.len) {
@@ -386,24 +386,24 @@ fn diffBisect(
386386
after: []const u8,
387387
deadline: u64,
388388
) DiffError!DiffList {
389-
const before_length = @intCast(isize, before.len);
390-
const after_length = @intCast(isize, after.len);
391-
const max_d = @intCast(isize, (before.len + after.len + 1) / 2);
389+
const before_length: isize = @intCast(before.len);
390+
const after_length: isize = @intCast(after.len);
391+
const max_d: isize = @intCast((before.len + after.len + 1) / 2);
392392
const v_offset = max_d;
393393
const v_length = 2 * max_d;
394394

395-
var v1 = try ArrayListUnmanaged(isize).initCapacity(allocator, @intCast(usize, v_length));
396-
v1.items.len = @intCast(usize, v_length);
397-
var v2 = try ArrayListUnmanaged(isize).initCapacity(allocator, @intCast(usize, v_length));
398-
v2.items.len = @intCast(usize, v_length);
395+
var v1 = try ArrayListUnmanaged(isize).initCapacity(allocator, @as(usize, @intCast(v_length)));
396+
v1.items.len = @intCast(v_length);
397+
var v2 = try ArrayListUnmanaged(isize).initCapacity(allocator, @as(usize, @intCast(v_length)));
398+
v2.items.len = @intCast(v_length);
399399

400400
var x: usize = 0;
401401
while (x < v_length) : (x += 1) {
402402
v1.items[x] = -1;
403403
v2.items[x] = -1;
404404
}
405-
v1.items[@intCast(usize, v_offset + 1)] = 0;
406-
v2.items[@intCast(usize, v_offset + 1)] = 0;
405+
v1.items[@intCast(v_offset + 1)] = 0;
406+
v2.items[@intCast(v_offset + 1)] = 0;
407407
const delta = before_length - after_length;
408408
// If the total number of characters is odd, then the front path will
409409
// collide with the reverse path.
@@ -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.milliTimestamp()) > deadline) {
421+
if (@as(u64, @intCast(std.time.milliTimestamp())) > deadline) {
422422
break;
423423
}
424424

@@ -428,20 +428,20 @@ fn diffBisect(
428428
var k1_offset = v_offset + k1;
429429
var x1: isize = 0;
430430
if (k1 == -d or (k1 != d and
431-
v1.items[@intCast(usize, k1_offset - 1)] < v1.items[@intCast(usize, k1_offset + 1)]))
431+
v1.items[@intCast(k1_offset - 1)] < v1.items[@intCast(k1_offset + 1)]))
432432
{
433-
x1 = v1.items[@intCast(usize, k1_offset + 1)];
433+
x1 = v1.items[@intCast(k1_offset + 1)];
434434
} else {
435-
x1 = v1.items[@intCast(usize, k1_offset - 1)] + 1;
435+
x1 = v1.items[@intCast(k1_offset - 1)] + 1;
436436
}
437437
var y1 = x1 - k1;
438438
while (x1 < before_length and
439-
y1 < after_length and before[@intCast(usize, x1)] == after[@intCast(usize, y1)])
439+
y1 < after_length and before[@intCast(x1)] == after[@intCast(y1)])
440440
{
441441
x1 += 1;
442442
y1 += 1;
443443
}
444-
v1.items[@intCast(usize, k1_offset)] = x1;
444+
v1.items[@intCast(k1_offset)] = x1;
445445
if (x1 > before_length) {
446446
// Ran off the right of the graph.
447447
k1end += 2;
@@ -450,9 +450,9 @@ fn diffBisect(
450450
k1start += 2;
451451
} else if (front) {
452452
var k2_offset = v_offset + delta - k1;
453-
if (k2_offset >= 0 and k2_offset < v_length and v2.items[@intCast(usize, k2_offset)] != -1) {
453+
if (k2_offset >= 0 and k2_offset < v_length and v2.items[@intCast(k2_offset)] != -1) {
454454
// Mirror x2 onto top-left coordinate system.
455-
const x2 = before_length - v2.items[@intCast(usize, k2_offset)];
455+
const x2 = before_length - v2.items[@intCast(k2_offset)];
456456
if (x1 >= x2) {
457457
// Overlap detected.
458458
return dmp.diffBisectSplit(allocator, before, after, x1, y1, deadline);
@@ -467,21 +467,21 @@ fn diffBisect(
467467
const k2_offset = v_offset + k2;
468468
var x2: isize = 0;
469469
if (k2 == -d or (k2 != d and
470-
v2.items[@intCast(usize, k2_offset - 1)] < v2.items[@intCast(usize, k2_offset + 1)]))
470+
v2.items[@intCast(k2_offset - 1)] < v2.items[@intCast(k2_offset + 1)]))
471471
{
472-
x2 = v2.items[@intCast(usize, k2_offset + 1)];
472+
x2 = v2.items[@intCast(k2_offset + 1)];
473473
} else {
474-
x2 = v2.items[@intCast(usize, k2_offset - 1)] + 1;
474+
x2 = v2.items[@intCast(k2_offset - 1)] + 1;
475475
}
476476
var y2: isize = x2 - k2;
477477
while (x2 < before_length and y2 < after_length and
478-
before[@intCast(usize, before_length - x2 - 1)] ==
479-
after[@intCast(usize, after_length - y2 - 1)])
478+
before[@intCast(before_length - x2 - 1)] ==
479+
after[@intCast(after_length - y2 - 1)])
480480
{
481481
x2 += 1;
482482
y2 += 1;
483483
}
484-
v2.items[@intCast(usize, k2_offset)] = x2;
484+
v2.items[@intCast(k2_offset)] = x2;
485485
if (x2 > before_length) {
486486
// Ran off the left of the graph.
487487
k2end += 2;
@@ -490,11 +490,11 @@ fn diffBisect(
490490
k2start += 2;
491491
} else if (!front) {
492492
const k1_offset = v_offset + delta - k2;
493-
if (k1_offset >= 0 and k1_offset < v_length and v1.items[@intCast(usize, k1_offset)] != -1) {
494-
const x1 = v1.items[@intCast(usize, k1_offset)];
493+
if (k1_offset >= 0 and k1_offset < v_length and v1.items[@intCast(k1_offset)] != -1) {
494+
const x1 = v1.items[@intCast(k1_offset)];
495495
const y1 = v_offset + x1 - k1_offset;
496496
// Mirror x2 onto top-left coordinate system.
497-
x2 = before_length - v2.items[@intCast(usize, k2_offset)];
497+
x2 = before_length - v2.items[@intCast(k2_offset)];
498498
if (x1 >= x2) {
499499
// Overlap detected.
500500
return dmp.diffBisectSplit(allocator, before, after, x1, y1, deadline);
@@ -528,10 +528,10 @@ fn diffBisectSplit(
528528
y: isize,
529529
deadline: u64,
530530
) DiffError!DiffList {
531-
const text1a = text1[0..@intCast(usize, x)];
532-
const text2a = text2[0..@intCast(usize, y)];
533-
const text1b = text1[@intCast(usize, x)..];
534-
const text2b = text2[@intCast(usize, y)..];
531+
const text1a = text1[0..@intCast(x)];
532+
const text2a = text2[0..@intCast(y)];
533+
const text1b = text1[@intCast(x)..];
534+
const text2b = text2[@intCast(y)..];
535535

536536
// Compute both diffs serially.
537537
var diffs = try dmp.diffInternal(allocator, text1a, text2a, false, deadline);
@@ -680,24 +680,24 @@ fn diffLinesToCharsMunge(
680680
// Walk the text, pulling out a Substring for each line.
681681
// text.split('\n') would would temporarily double our memory footprint.
682682
// Modifying text would create many large strings to garbage collect.
683-
while (line_end < @intCast(isize, text.len) - 1) {
683+
while (line_end < @as(isize, @intCast(text.len)) - 1) {
684684
line_end = b: {
685-
break :b @intCast(isize, std.mem.indexOf(u8, text[@intCast(usize, line_start)..], "\n") orelse
686-
break :b @intCast(isize, text.len - 1)) + line_start;
685+
break :b @as(isize, @intCast(std.mem.indexOf(u8, text[@intCast(line_start)..], "\n") orelse
686+
break :b @intCast(text.len - 1))) + line_start;
687687
};
688-
line = text[@intCast(usize, line_start) .. @intCast(usize, line_start) + @intCast(usize, line_end + 1 - line_start)];
688+
line = text[@intCast(line_start) .. @as(usize, @intCast(line_start)) + @as(usize, @intCast(line_end + 1 - line_start))];
689689

690690
if (line_hash.get(line)) |value| {
691-
try chars.append(allocator, @intCast(u8, value));
691+
try chars.append(allocator, @intCast(value));
692692
} else {
693693
if (line_array.items.len == max_lines) {
694694
// Bail out at 255 because char 256 == char 0.
695-
line = text[@intCast(usize, line_start)..];
696-
line_end = @intCast(isize, text.len);
695+
line = text[@intCast(line_start)..];
696+
line_end = @intCast(text.len);
697697
}
698698
try line_array.append(allocator, line);
699699
try line_hash.put(allocator, line, line_array.items.len - 1);
700-
try chars.append(allocator, @intCast(u8, line_array.items.len - 1));
700+
try chars.append(allocator, @intCast(line_array.items.len - 1));
701701
}
702702
line_start = line_end + 1;
703703
}
@@ -934,18 +934,18 @@ fn diffCleanupSemantic(allocator: std.mem.Allocator, diffs: *DiffList) DiffError
934934
var length_insertions2: usize = 0;
935935
var length_deletions2: usize = 0;
936936
while (pointer < diffs.items.len) {
937-
if (diffs.items[@intCast(usize, pointer)].operation == .equal) { // Equality found.
937+
if (diffs.items[@intCast(pointer)].operation == .equal) { // Equality found.
938938
try equalities.append(allocator, pointer);
939939
length_insertions1 = length_insertions2;
940940
length_deletions1 = length_deletions2;
941941
length_insertions2 = 0;
942942
length_deletions2 = 0;
943-
last_equality = diffs.items[@intCast(usize, pointer)].text;
943+
last_equality = diffs.items[@intCast(pointer)].text;
944944
} else { // an insertion or deletion
945-
if (diffs.items[@intCast(usize, pointer)].operation == .insert) {
946-
length_insertions2 += diffs.items[@intCast(usize, pointer)].text.len;
945+
if (diffs.items[@intCast(pointer)].operation == .insert) {
946+
length_insertions2 += diffs.items[@intCast(pointer)].text.len;
947947
} else {
948-
length_deletions2 += diffs.items[@intCast(usize, pointer)].text.len;
948+
length_deletions2 += diffs.items[@intCast(pointer)].text.len;
949949
}
950950
// Eliminate an equality that is smaller or equal to the edits on both
951951
// sides of it.
@@ -956,11 +956,11 @@ fn diffCleanupSemantic(allocator: std.mem.Allocator, diffs: *DiffList) DiffError
956956
// Duplicate record.
957957
try diffs.insert(
958958
allocator,
959-
@intCast(usize, equalities.items[equalities.items.len - 1]),
959+
@intCast(equalities.items[equalities.items.len - 1]),
960960
Diff.init(.delete, try allocator.dupe(u8, last_equality.?)),
961961
);
962962
// Change second copy to insert.
963-
diffs.items[@intCast(usize, equalities.items[equalities.items.len - 1] + 1)].operation = .insert;
963+
diffs.items[@intCast(equalities.items[equalities.items.len - 1] + 1)].operation = .insert;
964964
// Throw away the equality we just deleted.
965965
_ = equalities.pop();
966966
if (equalities.items.len > 0) {
@@ -992,46 +992,46 @@ fn diffCleanupSemantic(allocator: std.mem.Allocator, diffs: *DiffList) DiffError
992992
// Only extract an overlap if it is as big as the edit ahead or behind it.
993993
pointer = 1;
994994
while (pointer < diffs.items.len) {
995-
if (diffs.items[@intCast(usize, pointer - 1)].operation == .delete and
996-
diffs.items[@intCast(usize, pointer)].operation == .insert)
995+
if (diffs.items[@intCast(pointer - 1)].operation == .delete and
996+
diffs.items[@intCast(pointer)].operation == .insert)
997997
{
998-
const deletion = diffs.items[@intCast(usize, pointer - 1)].text;
999-
const insertion = diffs.items[@intCast(usize, pointer)].text;
998+
const deletion = diffs.items[@intCast(pointer - 1)].text;
999+
const insertion = diffs.items[@intCast(pointer)].text;
10001000
var overlap_length1: usize = diffCommonOverlap(deletion, insertion);
10011001
var overlap_length2: usize = diffCommonOverlap(insertion, deletion);
10021002
if (overlap_length1 >= overlap_length2) {
1003-
if (@floatFromInt(f32, overlap_length1) >= @floatFromInt(f32, deletion.len) / 2.0 or
1004-
@floatFromInt(f32, overlap_length1) >= @floatFromInt(f32, insertion.len) / 2.0)
1003+
if (@as(f32, @floatFromInt(overlap_length1)) >= @as(f32, @floatFromInt(deletion.len)) / 2.0 or
1004+
@as(f32, @floatFromInt(overlap_length1)) >= @as(f32, @floatFromInt(insertion.len)) / 2.0)
10051005
{
10061006
// Overlap found.
10071007
// Insert an equality and trim the surrounding edits.
10081008
try diffs.insert(
10091009
allocator,
1010-
@intCast(usize, pointer),
1010+
@intCast(pointer),
10111011
Diff.init(.equal, try allocator.dupe(u8, insertion[0..overlap_length1])),
10121012
);
1013-
diffs.items[@intCast(usize, pointer - 1)].text =
1013+
diffs.items[@intCast(pointer - 1)].text =
10141014
try allocator.dupe(u8, deletion[0 .. deletion.len - overlap_length1]);
1015-
diffs.items[@intCast(usize, pointer + 1)].text =
1015+
diffs.items[@intCast(pointer + 1)].text =
10161016
try allocator.dupe(u8, insertion[overlap_length1..]);
10171017
pointer += 1;
10181018
}
10191019
} else {
1020-
if (@floatFromInt(f32, overlap_length2) >= @floatFromInt(f32, deletion.len) / 2.0 or
1021-
@floatFromInt(f32, overlap_length2) >= @floatFromInt(f32, insertion.len) / 2.0)
1020+
if (@as(f32, @floatFromInt(overlap_length2)) >= @as(f32, @floatFromInt(deletion.len)) / 2.0 or
1021+
@as(f32, @floatFromInt(overlap_length2)) >= @as(f32, @floatFromInt(insertion.len)) / 2.0)
10221022
{
10231023
// Reverse overlap found.
10241024
// Insert an equality and swap and trim the surrounding edits.
10251025
try diffs.insert(
10261026
allocator,
1027-
@intCast(usize, pointer),
1027+
@intCast(pointer),
10281028
Diff.init(.equal, try allocator.dupe(u8, deletion[0..overlap_length2])),
10291029
);
1030-
diffs.items[@intCast(usize, pointer - 1)].operation = .insert;
1031-
diffs.items[@intCast(usize, pointer - 1)].text =
1030+
diffs.items[@intCast(pointer - 1)].operation = .insert;
1031+
diffs.items[@intCast(pointer - 1)].text =
10321032
try allocator.dupe(u8, insertion[0 .. insertion.len - overlap_length2]);
1033-
diffs.items[@intCast(usize, pointer + 1)].operation = .delete;
1034-
diffs.items[@intCast(usize, pointer + 1)].text =
1033+
diffs.items[@intCast(pointer + 1)].operation = .delete;
1034+
diffs.items[@intCast(pointer + 1)].text =
10351035
try allocator.dupe(u8, deletion[overlap_length2..]);
10361036
pointer += 1;
10371037
}
@@ -1051,7 +1051,7 @@ pub fn diffCleanupSemanticLossless(
10511051
) DiffError!void {
10521052
var pointer: usize = 1;
10531053
// Intentionally ignore the first and last element (don't need checking).
1054-
while (pointer < @intCast(isize, diffs.items.len) - 1) {
1054+
while (pointer < @as(isize, @intCast(diffs.items.len)) - 1) {
10551055
if (diffs.items[pointer - 1].operation == .equal and
10561056
diffs.items[pointer + 1].operation == .equal)
10571057
{

0 commit comments

Comments
 (0)