Skip to content

Commit 17d5fee

Browse files
committed
refactor: use RLS and decl literals
1 parent 0fcad90 commit 17d5fee

File tree

1 file changed

+48
-49
lines changed

1 file changed

+48
-49
lines changed

DiffMatchPatch.zig

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ const DiffMatchPatch = @This();
33
const std = @import("std");
44
const testing = std.testing;
55
const Allocator = std.mem.Allocator;
6-
const ArrayListUnmanaged = std.ArrayListUnmanaged;
76

87
/// DMP with default configuration options
9-
pub const default = DiffMatchPatch{};
8+
pub const default: DiffMatchPatch = .{};
109

1110
pub const Diff = struct {
1211
pub const Operation = enum {
@@ -97,9 +96,9 @@ pub fn diff(
9796
return dmp.diffInternal(allocator, before, after, check_lines, deadline);
9897
}
9998

100-
const DiffList = ArrayListUnmanaged(Diff);
99+
const DiffList = std.ArrayListUnmanaged(Diff);
101100

102-
/// Deinit an `ArrayListUnmanaged(Diff)` and the allocated slices of
101+
/// Deinit an `std.ArrayListUnmanaged(Diff)` and the allocated slices of
103102
/// text in each `Diff`.
104103
pub fn deinitDiffList(allocator: Allocator, diffs: *DiffList) void {
105104
defer diffs.deinit(allocator);
@@ -130,7 +129,7 @@ fn diffInternal(
130129
) DiffError!DiffList {
131130
// Check for equality (speedup).
132131
if (std.mem.eql(u8, before, after)) {
133-
var diffs = DiffList{};
132+
var diffs: DiffList = .empty;
134133
errdefer deinitDiffList(allocator, &diffs);
135134
if (before.len != 0) {
136135
try diffs.ensureUnusedCapacity(allocator, 1);
@@ -224,7 +223,7 @@ fn diffCompute(
224223
) DiffError!DiffList {
225224
if (before.len == 0) {
226225
// Just add some text (speedup).
227-
var diffs = DiffList{};
226+
var diffs: DiffList = .empty;
228227
errdefer deinitDiffList(allocator, &diffs);
229228
try diffs.ensureUnusedCapacity(allocator, 1);
230229
diffs.appendAssumeCapacity(.{
@@ -236,7 +235,7 @@ fn diffCompute(
236235

237236
if (after.len == 0) {
238237
// Just delete some text (speedup).
239-
var diffs = DiffList{};
238+
var diffs: DiffList = .empty;
240239
errdefer deinitDiffList(allocator, &diffs);
241240
try diffs.ensureUnusedCapacity(allocator, 1);
242241
diffs.appendAssumeCapacity(.{
@@ -250,7 +249,7 @@ fn diffCompute(
250249
const short_text = if (before.len > after.len) after else before;
251250

252251
if (std.mem.indexOf(u8, long_text, short_text)) |index| {
253-
var diffs = DiffList{};
252+
var diffs: DiffList = .empty;
254253
errdefer deinitDiffList(allocator, &diffs);
255254
// Shorter text is inside the longer text (speedup).
256255
const op: Diff.Operation = if (before.len > after.len)
@@ -276,7 +275,7 @@ fn diffCompute(
276275
if (short_text.len == 1) {
277276
// Single character string.
278277
// After the previous speedup, the character can't be an equality.
279-
var diffs = DiffList{};
278+
var diffs: DiffList = .empty;
280279
errdefer deinitDiffList(allocator, &diffs);
281280
try diffs.ensureUnusedCapacity(allocator, 2);
282281
diffs.appendAssumeCapacity(.{
@@ -442,30 +441,30 @@ fn diffHalfMatchInternal(
442441
const seed = long_text[i .. i + long_text.len / 4];
443442
var j: isize = -1;
444443

445-
var best_common = std.ArrayListUnmanaged(u8){};
444+
var best_common: std.ArrayListUnmanaged(u8) = .empty;
446445
defer best_common.deinit(allocator);
447446
var best_long_text_a: []const u8 = "";
448447
var best_long_text_b: []const u8 = "";
449448
var best_short_text_a: []const u8 = "";
450449
var best_short_text_b: []const u8 = "";
451450

452451
while (j < short_text.len and b: {
453-
j = @as(isize, @intCast(std.mem.indexOf(u8, short_text[@as(usize, @intCast(j + 1))..], seed) orelse break :b false)) + j + 1;
452+
j = @as(isize, @intCast(std.mem.indexOf(u8, short_text[@intCast(j + 1)..], seed) orelse break :b false)) + j + 1;
454453
break :b true;
455454
}) {
456-
const prefix_length = diffCommonPrefix(long_text[i..], short_text[@as(usize, @intCast(j))..]);
457-
const suffix_length = diffCommonSuffix(long_text[0..i], short_text[0..@as(usize, @intCast(j))]);
455+
const prefix_length = diffCommonPrefix(long_text[i..], short_text[@intCast(j)..]);
456+
const suffix_length = diffCommonSuffix(long_text[0..i], short_text[0..@intCast(j)]);
458457
if (best_common.items.len < suffix_length + prefix_length) {
459458
best_common.clearRetainingCapacity();
460-
const a = short_text[@as(usize, @intCast(j - @as(isize, @intCast(suffix_length)))) .. @as(usize, @intCast(j - @as(isize, @intCast(suffix_length)))) + suffix_length];
459+
const a = short_text[@intCast(j - @as(isize, @intCast(suffix_length))) .. @as(usize, @intCast(j - @as(isize, @intCast(suffix_length)))) + suffix_length];
461460
try best_common.appendSlice(allocator, a);
462-
const b = short_text[@as(usize, @intCast(j)) .. @as(usize, @intCast(j)) + prefix_length];
461+
const b = short_text[@intCast(j) .. @as(usize, @intCast(j)) + prefix_length];
463462
try best_common.appendSlice(allocator, b);
464463

465464
best_long_text_a = long_text[0 .. i - suffix_length];
466465
best_long_text_b = long_text[i + prefix_length ..];
467-
best_short_text_a = short_text[0..@as(usize, @intCast(j - @as(isize, @intCast(suffix_length))))];
468-
best_short_text_b = short_text[@as(usize, @intCast(j + @as(isize, @intCast(prefix_length))))..];
466+
best_short_text_a = short_text[0..@intCast(j - @as(isize, @intCast(suffix_length)))];
467+
best_short_text_b = short_text[@intCast(j + @as(isize, @intCast(prefix_length)))..];
469468
}
470469
}
471470
if (best_common.items.len * 2 >= long_text.len) {
@@ -511,10 +510,10 @@ fn diffBisect(
511510
const v_offset = max_d;
512511
const v_length = 2 * max_d;
513512

514-
var v1 = try ArrayListUnmanaged(isize).initCapacity(allocator, @as(usize, @intCast(v_length)));
513+
var v1: std.ArrayListUnmanaged(isize) = try .initCapacity(allocator, @intCast(v_length));
515514
defer v1.deinit(allocator);
516515
v1.items.len = @intCast(v_length);
517-
var v2 = try ArrayListUnmanaged(isize).initCapacity(allocator, @as(usize, @intCast(v_length)));
516+
var v2: std.ArrayListUnmanaged(isize) = try .initCapacity(allocator, @intCast(v_length));
518517
defer v2.deinit(allocator);
519518
v2.items.len = @intCast(v_length);
520519

@@ -626,7 +625,7 @@ fn diffBisect(
626625
}
627626
// Diff took too long and hit the deadline or
628627
// number of diffs equals number of characters, no commonality at all.
629-
var diffs = DiffList{};
628+
var diffs: DiffList = .empty;
630629
errdefer deinitDiffList(allocator, &diffs);
631630
try diffs.ensureUnusedCapacity(allocator, 2);
632631
diffs.appendAssumeCapacity(.{
@@ -715,8 +714,8 @@ fn diffLineMode(
715714
var pointer: usize = 0;
716715
var count_delete: usize = 0;
717716
var count_insert: usize = 0;
718-
var text_delete = ArrayListUnmanaged(u8){};
719-
var text_insert = ArrayListUnmanaged(u8){};
717+
var text_delete: std.ArrayListUnmanaged(u8) = .empty;
718+
var text_insert: std.ArrayListUnmanaged(u8) = .empty;
720719
defer {
721720
text_delete.deinit(allocator);
722721
text_insert.deinit(allocator);
@@ -774,7 +773,7 @@ fn diffLineMode(
774773
const LinesToCharsResult = struct {
775774
chars_1: []const u8,
776775
chars_2: []const u8,
777-
line_array: ArrayListUnmanaged([]const u8),
776+
line_array: std.ArrayListUnmanaged([]const u8),
778777

779778
pub fn deinit(self: *LinesToCharsResult, allocator: Allocator) void {
780779
allocator.free(self.chars_1);
@@ -795,9 +794,9 @@ fn diffLinesToChars(
795794
text1: []const u8,
796795
text2: []const u8,
797796
) DiffError!LinesToCharsResult {
798-
var line_array = ArrayListUnmanaged([]const u8){};
797+
var line_array: std.ArrayListUnmanaged([]const u8) = .empty;
799798
errdefer line_array.deinit(allocator);
800-
var line_hash = std.StringHashMapUnmanaged(usize){};
799+
var line_hash: std.StringHashMapUnmanaged(usize) = .empty;
801800
defer line_hash.deinit(allocator);
802801
// e.g. line_array[4] == "Hello\n"
803802
// e.g. line_hash.get("Hello\n") == 4
@@ -823,13 +822,13 @@ fn diffLinesToChars(
823822
fn diffLinesToCharsMunge(
824823
allocator: std.mem.Allocator,
825824
text: []const u8,
826-
line_array: *ArrayListUnmanaged([]const u8),
825+
line_array: *std.ArrayListUnmanaged([]const u8),
827826
line_hash: *std.StringHashMapUnmanaged(usize),
828827
max_lines: usize,
829828
) DiffError![]const u8 {
830829
var line_start: isize = 0;
831830
var line_end: isize = -1;
832-
var chars = ArrayListUnmanaged(u8){};
831+
var chars: std.ArrayListUnmanaged(u8) = .empty;
833832
defer chars.deinit(allocator);
834833
// Walk the text, pulling out a Substring for each line.
835834
// TODO this can be handled with a Reader, avoiding all the manual splitting
@@ -866,10 +865,10 @@ fn diffCharsToLines(
866865
char_diffs: *DiffList,
867866
line_array: []const []const u8,
868867
) DiffError!DiffList {
869-
var diffs = DiffList{};
868+
var diffs: DiffList = .empty;
870869
errdefer deinitDiffList(allocator, &diffs);
871870
try diffs.ensureTotalCapacity(allocator, char_diffs.items.len);
872-
var text = ArrayListUnmanaged(u8){};
871+
var text: std.ArrayListUnmanaged(u8) = .empty;
873872
defer text.deinit(allocator);
874873

875874
for (char_diffs.items) |*d| {
@@ -895,10 +894,10 @@ fn diffCleanupMerge(allocator: std.mem.Allocator, diffs: *DiffList) DiffError!vo
895894
var count_delete: usize = 0;
896895
var count_insert: usize = 0;
897896

898-
var text_delete = ArrayListUnmanaged(u8){};
897+
var text_delete: std.ArrayListUnmanaged(u8) = .empty;
899898
defer text_delete.deinit(allocator);
900899

901-
var text_insert = ArrayListUnmanaged(u8){};
900+
var text_insert: std.ArrayListUnmanaged(u8) = .empty;
902901
defer text_insert.deinit(allocator);
903902

904903
var common_length: usize = undefined;
@@ -1068,7 +1067,7 @@ fn diffCleanupMerge(allocator: std.mem.Allocator, diffs: *DiffList) DiffError!vo
10681067
pub fn diffCleanupSemantic(allocator: std.mem.Allocator, diffs: *DiffList) DiffError!void {
10691068
var changes = false;
10701069
// Stack of indices where equalities are found.
1071-
var equalities = ArrayListUnmanaged(isize){};
1070+
var equalities: std.ArrayListUnmanaged(isize) = .empty;
10721071
defer equalities.deinit(allocator);
10731072
// Always equal to equalities[equalitiesLength-1][1]
10741073
var last_equality: ?[]const u8 = null;
@@ -1210,15 +1209,15 @@ pub fn diffCleanupSemanticLossless(
12101209
diffs.items[pointer + 1].operation == .equal)
12111210
{
12121211
// This is a single edit surrounded by equalities.
1213-
var equality_1 = std.ArrayListUnmanaged(u8){};
1212+
var equality_1: std.ArrayListUnmanaged(u8) = .empty;
12141213
defer equality_1.deinit(allocator);
12151214
try equality_1.appendSlice(allocator, diffs.items[pointer - 1].text);
12161215

1217-
var edit = std.ArrayListUnmanaged(u8){};
1216+
var edit: std.ArrayListUnmanaged(u8) = .empty;
12181217
defer edit.deinit(allocator);
12191218
try edit.appendSlice(allocator, diffs.items[pointer].text);
12201219

1221-
var equality_2 = std.ArrayListUnmanaged(u8){};
1220+
var equality_2: std.ArrayListUnmanaged(u8) = .empty;
12221221
defer equality_2.deinit(allocator);
12231222
try equality_2.appendSlice(allocator, diffs.items[pointer + 1].text);
12241223

@@ -1244,15 +1243,15 @@ pub fn diffCleanupSemanticLossless(
12441243

12451244
// Second, step character by character right,
12461245
// looking for the best fit.
1247-
var best_equality_1 = ArrayListUnmanaged(u8){};
1246+
var best_equality_1: std.ArrayListUnmanaged(u8) = .empty;
12481247
defer best_equality_1.deinit(allocator);
12491248
try best_equality_1.appendSlice(allocator, equality_1.items);
12501249

1251-
var best_edit = ArrayListUnmanaged(u8){};
1250+
var best_edit: std.ArrayListUnmanaged(u8) = .empty;
12521251
defer best_edit.deinit(allocator);
12531252
try best_edit.appendSlice(allocator, edit.items);
12541253

1255-
var best_equality_2 = ArrayListUnmanaged(u8){};
1254+
var best_equality_2: std.ArrayListUnmanaged(u8) = .empty;
12561255
defer best_equality_2.deinit(allocator);
12571256
try best_equality_2.appendSlice(allocator, equality_2.items);
12581257

@@ -1775,7 +1774,7 @@ fn testDiffCharsToLines(
17751774
expected: []const Diff,
17761775
},
17771776
) !void {
1778-
var char_diffs = try DiffList.initCapacity(allocator, params.diffs.len);
1777+
var char_diffs: DiffList = try .initCapacity(allocator, params.diffs.len);
17791778
defer deinitDiffList(allocator, &char_diffs);
17801779

17811780
for (params.diffs) |item| {
@@ -1790,7 +1789,7 @@ fn testDiffCharsToLines(
17901789

17911790
test diffCharsToLines {
17921791
// Convert chars up to lines.
1793-
var diff_list = DiffList{};
1792+
var diff_list: DiffList = .empty;
17941793
defer deinitDiffList(testing.allocator, &diff_list);
17951794
try diff_list.ensureTotalCapacity(testing.allocator, 2);
17961795
diff_list.appendSliceAssumeCapacity(&.{
@@ -1817,7 +1816,7 @@ fn testDiffCleanupMerge(allocator: std.mem.Allocator, params: struct {
18171816
input: []const Diff,
18181817
expected: []const Diff,
18191818
}) !void {
1820-
var diffs = try DiffList.initCapacity(allocator, params.input.len);
1819+
var diffs: DiffList = try .initCapacity(allocator, params.input.len);
18211820
defer deinitDiffList(allocator, &diffs);
18221821

18231822
for (params.input) |item| {
@@ -2021,7 +2020,7 @@ fn testDiffCleanupSemanticLossless(
20212020
expected: []const Diff,
20222021
},
20232022
) !void {
2024-
var diffs = try DiffList.initCapacity(allocator, params.input.len);
2023+
var diffs: DiffList = try .initCapacity(allocator, params.input.len);
20252024
defer deinitDiffList(allocator, &diffs);
20262025

20272026
for (params.input) |item| {
@@ -2034,7 +2033,7 @@ fn testDiffCleanupSemanticLossless(
20342033
}
20352034

20362035
fn sliceToDiffList(allocator: Allocator, diff_slice: []const Diff) !DiffList {
2037-
var diff_list = DiffList{};
2036+
var diff_list: DiffList = .empty;
20382037
errdefer deinitDiffList(allocator, &diff_list);
20392038
try diff_list.ensureTotalCapacity(allocator, diff_slice.len);
20402039
for (diff_slice) |d| {
@@ -2278,7 +2277,7 @@ test diffBisect {
22782277
}
22792278

22802279
fn diffHalfMatchLeak(allocator: Allocator) !void {
2281-
const dmp = DiffMatchPatch{};
2280+
const dmp: DiffMatchPatch = .default;
22822281
const text1 = "The quick brown fox jumps over the lazy dog.";
22832282
const text2 = "That quick brown fox jumped over a lazy dog.";
22842283
var diffs = try dmp.diff(allocator, text2, text1, true);
@@ -2609,7 +2608,7 @@ fn testDiffCleanupSemantic(
26092608
expected: []const Diff,
26102609
},
26112610
) !void {
2612-
var diffs = try DiffList.initCapacity(allocator, params.input.len);
2611+
var diffs: DiffList = try .initCapacity(allocator, params.input.len);
26132612
defer deinitDiffList(allocator, &diffs);
26142613

26152614
for (params.input) |item| {
@@ -2788,7 +2787,7 @@ fn testDiffCleanupEfficiency(
27882787
expected: []const Diff,
27892788
},
27902789
) !void {
2791-
var diffs = try DiffList.initCapacity(allocator, params.input.len);
2790+
var diffs: DiffList = try .initCapacity(allocator, params.input.len);
27922791
defer deinitDiffList(allocator, &diffs);
27932792
for (params.input) |item| {
27942793
diffs.appendAssumeCapacity(.{ .operation = item.operation, .text = try allocator.dupe(u8, item.text) });
@@ -2800,12 +2799,12 @@ fn testDiffCleanupEfficiency(
28002799

28012800
test "diffCleanupEfficiency" {
28022801
const allocator = testing.allocator;
2803-
var dmp = DiffMatchPatch{};
2802+
var dmp: DiffMatchPatch = .default;
28042803
dmp.diff_edit_cost = 4;
28052804
{ // Null case.
2806-
var diffs = DiffList{};
2805+
var diffs: DiffList = .empty;
28072806
try dmp.diffCleanupEfficiency(allocator, &diffs);
2808-
try testing.expectEqualDeep(DiffList{}, diffs);
2807+
try testing.expectEqualDeep(DiffList.empty, diffs);
28092808
}
28102809
{ // No elimination.
28112810
const dslice: []const Diff = &.{

0 commit comments

Comments
 (0)