@@ -606,7 +606,7 @@ fn diffCharsToLines(allocator: std.mem.Allocator, diffs: []Diff, line_array: []c
606606// Any edit section can move as long as it doesn't cross an equality.
607607// @param diffs List of Diff objects.
608608//
609- fn diffCleanupMerge (allocator : std.mem.Allocator , diffs : * std .ArrayListUnmanaged (Diff )) ! void {
609+ fn diffCleanupMerge (allocator : std.mem.Allocator , diffs : * std .ArrayListUnmanaged (Diff )) error { OutOfMemory } ! void {
610610 // Add a dummy entry at the end.
611611 try diffs .append (allocator , Diff { .operation = .equal , .text = "" });
612612 var pointer : usize = 0 ;
@@ -819,10 +819,10 @@ fn diffCleanupSemantic(allocator: std.mem.Allocator, diffs: *ArrayListUnmanaged(
819819 // Reverse overlap found.
820820 // Insert an equality and swap and trim the surrounding edits.
821821 diffs .Insert (pointer , Diff { .operation = .equal , .text = deletion .Substring (0 , overlap_length2 ) });
822- diffs .items [pointer - 1 ].operation = Operation . INSERT ;
822+ diffs .items [pointer - 1 ].operation = .insert ;
823823 diffs .items [pointer - 1 ].text =
824824 insertion .Substring (0 , insertion .len - overlap_length2 );
825- diffs .items [pointer + 1 ].operation = Operation . DELETE ;
825+ diffs .items [pointer + 1 ].operation = .delete ;
826826 diffs .items [pointer + 1 ].text = deletion .Substring (overlap_length2 );
827827 pointer += 1 ;
828828 }
@@ -947,7 +947,7 @@ pub fn diffCleanupSemanticLossless(
947947// @param two Second string.
948948// @return The score.
949949//
950- fn cleanupSemanticScore (one : []const u8 , two : []const u8 ) usize {
950+ fn diffCleanupSemanticScore (one : []const u8 , two : []const u8 ) usize {
951951 if (one .len == 0 or two .len == 0 ) {
952952 // Edges are the best.
953953 return 6 ;
@@ -968,10 +968,10 @@ fn cleanupSemanticScore(one: []const u8, two: []const u8) usize {
968968 const lineBreak2 = whitespace2 and std .ascii .isControl (char2 );
969969 const blankLine1 = lineBreak1 and
970970 // BLANKLINEEND.IsMatch(one);
971- (mem .endsWith (u8 , "\n " ) or mem .endsWith (u8 , "\r \n " ));
971+ (std . mem .endsWith (u8 , "\n " ) or std . mem .endsWith (u8 , "\r \n " ));
972972 const blankLine2 = lineBreak2 and
973973 // BLANKLINESTART.IsMatch(two);
974- (mem .startsWith (u8 , "\n " ) or mem .startsWith (u8 , "\r \n " ));
974+ (std . mem .startsWith (u8 , "\n " ) or std . mem .startsWith (u8 , "\r \n " ));
975975
976976 if (blankLine1 or blankLine2 ) {
977977 // Five points for blank lines.
@@ -998,13 +998,17 @@ fn cleanupSemanticScore(one: []const u8, two: []const u8) usize {
998998
999999/// Reduce the number of edits by eliminating operationally trivial
10001000/// equalities.
1001- pub fn diffCleanupEfficiency (allocator : std.mem.Allocator , diffs : * ArrayListUnmanaged (Diff )) error {OutOfMemory }! void {
1001+ pub fn diffCleanupEfficiency (
1002+ dmp : DiffMatchPatch ,
1003+ allocator : std.mem.Allocator ,
1004+ diffs : * ArrayListUnmanaged (Diff ),
1005+ ) error {OutOfMemory }! void {
10021006 var changes = false ;
10031007 // Stack of indices where equalities are found.
10041008 var equalities = ArrayListUnmanaged (Diff ){};
10051009 // Always equal to equalities[equalitiesLength-1][1]
10061010 var last_equality = "" ;
1007- var pointer : usize = 0 ; // Index of current position.
1011+ var pointer : isize = 0 ; // Index of current position.
10081012 // Is there an insertion operation before the last equality.
10091013 var pre_ins = false ;
10101014 // Is there a deletion operation before the last equality.
@@ -1014,22 +1018,22 @@ pub fn diffCleanupEfficiency(allocator: std.mem.Allocator, diffs: *ArrayListUnma
10141018 // Is there a deletion operation after the last equality.
10151019 var post_del = false ;
10161020 while (pointer < diffs .Count ) {
1017- if (diffs [pointer ].operation == Operation . EQUAL ) { // Equality found.
1018- if (diffs [pointer ].text .Length < this . Diff_EditCost and (post_ins or post_del )) {
1021+ if (diffs . items [pointer ].operation == .equal ) { // Equality found.
1022+ if (diffs [pointer ].text .len < dmp . diff_edit_cost and (post_ins or post_del )) {
10191023 // Candidate found.
10201024 equalities .Push (pointer );
10211025 pre_ins = post_ins ;
10221026 pre_del = post_del ;
10231027 last_equality = diffs [pointer ].text ;
10241028 } else {
10251029 // Not a candidate, and can never become one.
1026- equalities .Clear () ;
1027- last_equality = string . Empty ;
1030+ equalities .items . len = 0 ;
1031+ last_equality = "" ;
10281032 }
10291033 post_ins = false ;
10301034 post_del = false ;
10311035 } else { // An insertion or deletion.
1032- if (diffs [pointer ].operation == Operation . DELETE ) {
1036+ if (diffs . items [pointer ].operation == .delete ) {
10331037 post_del = true ;
10341038 } else {
10351039 post_ins = true ;
@@ -1040,24 +1044,28 @@ pub fn diffCleanupEfficiency(allocator: std.mem.Allocator, diffs: *ArrayListUnma
10401044 // <ins>A</ins><del>B</del>X<ins>C</ins>
10411045 // <ins>A</del>X<ins>C</ins><del>D</del>
10421046 // <ins>A</ins><del>B</del>X<del>C</del>
1043- if ((last_equality .Length != 0 ) and ((pre_ins and pre_del and post_ins and post_del ) or ((last_equality .Length < this . Diff_EditCost / 2 ) and ((if (pre_ins ) 1 else 0 ) + (if (pre_del ) 1 else 0 ) + (if (post_ins ) 1 else 0 ) + (if (post_del ) 1 else 0 )) == 3 ))) {
1047+ if ((last_equality .Length != 0 ) and ((pre_ins and pre_del and post_ins and post_del ) or ((last_equality .Length < dmp . diff_edit_cost / 2 ) and ((if (pre_ins ) 1 else 0 ) + (if (pre_del ) 1 else 0 ) + (if (post_ins ) 1 else 0 ) + (if (post_del ) 1 else 0 )) == 3 ))) {
10441048 // Duplicate record.
1045- diffs .Insert (equalities .Peek (), Diff (Operation .DELETE , last_equality ));
1049+ try diffs .insert (
1050+ allocator ,
1051+ equalities .items [equalities .items .len - 1 ],
1052+ Diff { .operation = .delete , .text = try allocator .dupe (u8 , last_equality ) },
1053+ );
10461054 // Change second copy to insert.
1047- diffs [equalities .Peek () + 1 ].operation = Operation . INSERT ;
1048- equalities .Pop (); // Throw away the equality we just deleted.
1049- last_equality = string . Empty ;
1055+ diffs [equalities .items [ equalities . items . len - 1 ] + 1 ].operation = .insert ;
1056+ _ = equalities .pop (); // Throw away the equality we just deleted.
1057+ last_equality = "" ;
10501058 if (pre_ins and pre_del ) {
10511059 // No changes made which could affect previous entry, keep going.
10521060 post_ins = true ;
10531061 post_del = true ;
1054- equalities .Clear () ;
1062+ equalities .items . len = 0 ;
10551063 } else {
1056- if (equalities .Count > 0 ) {
1057- equalities .Pop ();
1064+ if (equalities .items . len > 0 ) {
1065+ _ = equalities .pop ();
10581066 }
10591067
1060- pointer = if (equalities .Count > 0 ) equalities .Peek () else -1 ;
1068+ pointer = if (equalities .items . len > 0 ) equalities .items [ equalities . items . len - 1 ] else -1 ;
10611069 post_ins = false ;
10621070 post_del = false ;
10631071 }
@@ -1068,6 +1076,6 @@ pub fn diffCleanupEfficiency(allocator: std.mem.Allocator, diffs: *ArrayListUnma
10681076 }
10691077
10701078 if (changes ) {
1071- diffCleanupMerge (allocator , diffs );
1079+ try diffCleanupMerge (allocator , diffs );
10721080 }
10731081}
0 commit comments