@@ -29,9 +29,6 @@ pub const Diff = struct {
2929 });
3030 }
3131
32- pub fn init (operation : Operation , text : []const u8 ) Diff {
33- return .{ .operation = operation , .text = text };
34- }
3532 pub fn eql (a : Diff , b : Diff ) bool {
3633 return a .operation == b .operation and std .mem .eql (u8 , a .text , b .text );
3734 }
@@ -137,10 +134,10 @@ fn diffInternal(
137134 errdefer deinitDiffList (allocator , & diffs );
138135 if (before .len != 0 ) {
139136 try diffs .ensureUnusedCapacity (allocator , 1 );
140- diffs .appendAssumeCapacity (Diff . init (
141- .equal ,
142- try allocator .dupe (u8 , before ),
143- ) );
137+ diffs .appendAssumeCapacity (.{
138+ .operation = . equal ,
139+ . text = try allocator .dupe (u8 , before ),
140+ } );
144141 }
145142 return diffs ;
146143 }
@@ -165,17 +162,17 @@ fn diffInternal(
165162
166163 if (common_prefix .len != 0 ) {
167164 try diffs .ensureUnusedCapacity (allocator , 1 );
168- diffs .insertAssumeCapacity (0 , Diff . init (
169- .equal ,
170- try allocator .dupe (u8 , common_prefix ),
171- ) );
165+ diffs .insertAssumeCapacity (0 , .{
166+ .operation = . equal ,
167+ . text = try allocator .dupe (u8 , common_prefix ),
168+ } );
172169 }
173170 if (common_suffix .len != 0 ) {
174171 try diffs .ensureUnusedCapacity (allocator , 1 );
175- diffs .appendAssumeCapacity (Diff . init (
176- .equal ,
177- try allocator .dupe (u8 , common_suffix ),
178- ) );
172+ diffs .appendAssumeCapacity (.{
173+ .operation = . equal ,
174+ . text = try allocator .dupe (u8 , common_suffix ),
175+ } );
179176 }
180177
181178 try diffCleanupMerge (allocator , & diffs );
@@ -230,10 +227,10 @@ fn diffCompute(
230227 var diffs = DiffList {};
231228 errdefer deinitDiffList (allocator , & diffs );
232229 try diffs .ensureUnusedCapacity (allocator , 1 );
233- diffs .appendAssumeCapacity (Diff . init (
234- .insert ,
235- try allocator .dupe (u8 , after ),
236- ) );
230+ diffs .appendAssumeCapacity (.{
231+ .operation = . insert ,
232+ . text = try allocator .dupe (u8 , after ),
233+ } );
237234 return diffs ;
238235 }
239236
@@ -242,10 +239,10 @@ fn diffCompute(
242239 var diffs = DiffList {};
243240 errdefer deinitDiffList (allocator , & diffs );
244241 try diffs .ensureUnusedCapacity (allocator , 1 );
245- diffs .appendAssumeCapacity (Diff . init (
246- .delete ,
247- try allocator .dupe (u8 , before ),
248- ) );
242+ diffs .appendAssumeCapacity (.{
243+ .operation = . delete ,
244+ . text = try allocator .dupe (u8 , before ),
245+ } );
249246 return diffs ;
250247 }
251248
@@ -261,18 +258,18 @@ fn diffCompute(
261258 else
262259 .insert ;
263260 try diffs .ensureUnusedCapacity (allocator , 3 );
264- diffs .appendAssumeCapacity (Diff . init (
265- op ,
266- try allocator .dupe (u8 , long_text [0.. index ]),
267- ) );
268- diffs .appendAssumeCapacity (Diff . init (
269- .equal ,
270- try allocator .dupe (u8 , short_text ),
271- ) );
272- diffs .appendAssumeCapacity (Diff . init (
273- op ,
274- try allocator .dupe (u8 , long_text [index + short_text .len .. ]),
275- ) );
261+ diffs .appendAssumeCapacity (.{
262+ . operation = op ,
263+ . text = try allocator .dupe (u8 , long_text [0.. index ]),
264+ } );
265+ diffs .appendAssumeCapacity (.{
266+ .operation = . equal ,
267+ . text = try allocator .dupe (u8 , short_text ),
268+ } );
269+ diffs .appendAssumeCapacity (.{
270+ . operation = op ,
271+ . text = try allocator .dupe (u8 , long_text [index + short_text .len .. ]),
272+ } );
276273 return diffs ;
277274 }
278275
@@ -282,14 +279,14 @@ fn diffCompute(
282279 var diffs = DiffList {};
283280 errdefer deinitDiffList (allocator , & diffs );
284281 try diffs .ensureUnusedCapacity (allocator , 2 );
285- diffs .appendAssumeCapacity (Diff . init (
286- .delete ,
287- try allocator .dupe (u8 , before ),
288- ) );
289- diffs .appendAssumeCapacity (Diff . init (
290- .insert ,
291- try allocator .dupe (u8 , after ),
292- ) );
282+ diffs .appendAssumeCapacity (.{
283+ .operation = . delete ,
284+ . text = try allocator .dupe (u8 , before ),
285+ } );
286+ diffs .appendAssumeCapacity (.{
287+ .operation = . insert ,
288+ . text = try allocator .dupe (u8 , after ),
289+ } );
293290 return diffs ;
294291 }
295292
@@ -324,12 +321,10 @@ fn diffCompute(
324321
325322 // Merge the results.
326323 try diffs .ensureUnusedCapacity (allocator , 1 );
327- diffs .appendAssumeCapacity (
328- Diff .init (.equal , try allocator .dupe (
329- u8 ,
330- half_match .common_middle ,
331- )),
332- );
324+ diffs .appendAssumeCapacity (.{
325+ .operation = .equal ,
326+ .text = try allocator .dupe (u8 , half_match .common_middle ),
327+ });
333328 try diffs .appendSlice (allocator , diffs_b .items );
334329 return diffs ;
335330 }
@@ -634,14 +629,14 @@ fn diffBisect(
634629 var diffs = DiffList {};
635630 errdefer deinitDiffList (allocator , & diffs );
636631 try diffs .ensureUnusedCapacity (allocator , 2 );
637- diffs .appendAssumeCapacity (Diff . init (
638- .delete ,
639- try allocator .dupe (u8 , before ),
640- ) );
641- diffs .appendAssumeCapacity (Diff . init (
642- .insert ,
643- try allocator .dupe (u8 , after ),
644- ) );
632+ diffs .appendAssumeCapacity (.{
633+ .operation = . delete ,
634+ . text = try allocator .dupe (u8 , before ),
635+ } );
636+ diffs .appendAssumeCapacity (.{
637+ .operation = . insert ,
638+ . text = try allocator .dupe (u8 , after ),
639+ } );
645640 return diffs ;
646641}
647642
@@ -715,7 +710,7 @@ fn diffLineMode(
715710
716711 // Rediff any replacement blocks, this time character-by-character.
717712 // Add a dummy entry at the end.
718- try diffs .append (allocator , Diff . init ( . equal , "" ) );
713+ try diffs .append (allocator , .{ . operation = . equal , . text = "" } );
719714
720715 var pointer : usize = 0 ;
721716 var count_delete : usize = 0 ;
@@ -882,7 +877,10 @@ fn diffCharsToLines(
882877 while (j < d .text .len ) : (j += 1 ) {
883878 try text .appendSlice (allocator , line_array [d .text [j ]]);
884879 }
885- diffs .appendAssumeCapacity (Diff .init (d .operation , try text .toOwnedSlice (allocator )));
880+ diffs .appendAssumeCapacity (.{
881+ .operation = d .operation ,
882+ .text = try text .toOwnedSlice (allocator ),
883+ });
886884 }
887885 return diffs ;
888886}
@@ -892,7 +890,7 @@ fn diffCharsToLines(
892890/// @param diffs List of Diff objects.
893891fn diffCleanupMerge (allocator : std.mem.Allocator , diffs : * DiffList ) DiffError ! void {
894892 // Add a dummy entry at the end.
895- try diffs .append (allocator , Diff . init ( . equal , "" ) );
893+ try diffs .append (allocator , .{ . operation = . equal , . text = "" } );
896894 var pointer : usize = 0 ;
897895 var count_delete : usize = 0 ;
898896 var count_insert : usize = 0 ;
@@ -936,7 +934,7 @@ fn diffCleanupMerge(allocator: std.mem.Allocator, diffs: *DiffList) DiffError!vo
936934 } else {
937935 try diffs .ensureUnusedCapacity (allocator , 1 );
938936 const text = try allocator .dupe (u8 , text_insert .items [0.. common_length ]);
939- diffs .insertAssumeCapacity (0 , Diff . init ( . equal , text ) );
937+ diffs .insertAssumeCapacity (0 , .{ . operation = . equal , . text = text } );
940938 pointer += 1 ;
941939 }
942940 text_insert .replaceRangeAssumeCapacity (0 , common_length , &.{});
@@ -965,18 +963,18 @@ fn diffCleanupMerge(allocator: std.mem.Allocator, diffs: *DiffList) DiffError!vo
965963
966964 if (text_delete .items .len != 0 ) {
967965 try diffs .ensureUnusedCapacity (allocator , 1 );
968- diffs .insertAssumeCapacity (pointer , Diff . init (
969- .delete ,
970- try allocator .dupe (u8 , text_delete .items ),
971- ) );
966+ diffs .insertAssumeCapacity (pointer , .{
967+ .operation = . delete ,
968+ . text = try allocator .dupe (u8 , text_delete .items ),
969+ } );
972970 pointer += 1 ;
973971 }
974972 if (text_insert .items .len != 0 ) {
975973 try diffs .ensureUnusedCapacity (allocator , 1 );
976- diffs .insertAssumeCapacity (pointer , Diff . init (
977- .insert ,
978- try allocator .dupe (u8 , text_insert .items ),
979- ) );
974+ diffs .insertAssumeCapacity (pointer , .{
975+ .operation = . insert ,
976+ . text = try allocator .dupe (u8 , text_insert .items ),
977+ } );
980978 pointer += 1 ;
981979 }
982980 pointer += 1 ;
@@ -1105,10 +1103,10 @@ pub fn diffCleanupSemantic(allocator: std.mem.Allocator, diffs: *DiffList) DiffE
11051103 try diffs .ensureUnusedCapacity (allocator , 1 );
11061104 diffs .insertAssumeCapacity (
11071105 @intCast (equalities .items [equalities .items .len - 1 ]),
1108- Diff . init (
1109- .delete ,
1110- try allocator .dupe (u8 , last_equality .? ),
1111- ) ,
1106+ .{
1107+ .operation = . delete ,
1108+ . text = try allocator .dupe (u8 , last_equality .? ),
1109+ } ,
11121110 );
11131111 // Change second copy to insert.
11141112 diffs .items [@intCast (equalities .items [equalities .items .len - 1 ] + 1 )].operation = .insert ;
@@ -1157,13 +1155,10 @@ pub fn diffCleanupSemantic(allocator: std.mem.Allocator, diffs: *DiffList) DiffE
11571155 // Overlap found.
11581156 // Insert an equality and trim the surrounding edits.
11591157 try diffs .ensureUnusedCapacity (allocator , 1 );
1160- diffs .insertAssumeCapacity (
1161- @intCast (pointer ),
1162- Diff .init (
1163- .equal ,
1164- try allocator .dupe (u8 , insertion [0.. overlap_length1 ]),
1165- ),
1166- );
1158+ diffs .insertAssumeCapacity (@intCast (pointer ), .{
1159+ .operation = .equal ,
1160+ .text = try allocator .dupe (u8 , insertion [0.. overlap_length1 ]),
1161+ });
11671162 diffs .items [@intCast (pointer - 1 )].text =
11681163 try allocator .dupe (u8 , deletion [0 .. deletion .len - overlap_length1 ]);
11691164 allocator .free (deletion );
@@ -1179,13 +1174,10 @@ pub fn diffCleanupSemantic(allocator: std.mem.Allocator, diffs: *DiffList) DiffE
11791174 // Reverse overlap found.
11801175 // Insert an equality and swap and trim the surrounding edits.
11811176 try diffs .ensureUnusedCapacity (allocator , 1 );
1182- diffs .insertAssumeCapacity (
1183- @intCast (pointer ),
1184- Diff .init (
1185- .equal ,
1186- try allocator .dupe (u8 , deletion [0.. overlap_length2 ]),
1187- ),
1188- );
1177+ diffs .insertAssumeCapacity (@intCast (pointer ), .{
1178+ .operation = .equal ,
1179+ .text = try allocator .dupe (u8 , deletion [0.. overlap_length2 ]),
1180+ });
11891181 const new_minus = try allocator .dupe (u8 , insertion [0 .. insertion .len - overlap_length2 ]);
11901182 errdefer allocator .free (new_minus ); // necessary due to swap
11911183 const new_plus = try allocator .dupe (u8 , deletion [overlap_length2 .. ]);
@@ -1435,10 +1427,10 @@ pub fn diffCleanupEfficiency(
14351427 try diffs .ensureUnusedCapacity (allocator , 1 );
14361428 diffs .insertAssumeCapacity (
14371429 equalities .items [equalities .items .len - 1 ],
1438- Diff . init (
1439- .delete ,
1440- try allocator .dupe (u8 , last_equality ),
1441- ) ,
1430+ .{
1431+ .operation = . delete ,
1432+ . text = try allocator .dupe (u8 , last_equality ),
1433+ } ,
14421434 );
14431435 // Change second copy to insert.
14441436 diffs .items [equalities .items [equalities .items .len - 1 ] + 1 ].operation = .insert ;
@@ -1802,8 +1794,8 @@ test diffCharsToLines {
18021794 defer deinitDiffList (testing .allocator , & diff_list );
18031795 try diff_list .ensureTotalCapacity (testing .allocator , 2 );
18041796 diff_list .appendSliceAssumeCapacity (&.{
1805- Diff . init ( . equal , try testing .allocator .dupe (u8 , "\u{0001}\u{0002}\u{0001} " )) ,
1806- Diff . init ( . insert , try testing .allocator .dupe (u8 , "\u{0002}\u{0001}\u{0002} " )) ,
1797+ .{ . operation = . equal , . text = try testing .allocator .dupe (u8 , "\u{0001}\u{0002}\u{0001} " ) } ,
1798+ .{ . operation = . insert , . text = try testing .allocator .dupe (u8 , "\u{0002}\u{0001}\u{0002} " ) } ,
18071799 });
18081800 try checkAllAllocationFailures (testing .allocator , testDiffCharsToLines , .{.{
18091801 .diffs = diff_list .items ,
@@ -2046,10 +2038,10 @@ fn sliceToDiffList(allocator: Allocator, diff_slice: []const Diff) !DiffList {
20462038 errdefer deinitDiffList (allocator , & diff_list );
20472039 try diff_list .ensureTotalCapacity (allocator , diff_slice .len );
20482040 for (diff_slice ) | d | {
2049- diff_list .appendAssumeCapacity (Diff . init (
2050- d .operation ,
2051- try allocator .dupe (u8 , d .text ),
2052- ) );
2041+ diff_list .appendAssumeCapacity (.{
2042+ . operation = d .operation ,
2043+ . text = try allocator .dupe (u8 , d .text ),
2044+ } );
20532045 }
20542046 return diff_list ;
20552047}
0 commit comments