From ade45732f2ff6b0110a20614f9ebb92a4d38b8c8 Mon Sep 17 00:00:00 2001 From: leo Date: Fri, 8 Oct 2021 16:01:46 +0800 Subject: [PATCH] optimize: combine two modified parts those have one same unchanged character --- src/Models/TextCompare.cs | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Models/TextCompare.cs b/src/Models/TextCompare.cs index efc8e6ef..e59e3424 100644 --- a/src/Models/TextCompare.cs +++ b/src/Models/TextCompare.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; namespace SourceGit.Models { @@ -84,6 +84,7 @@ namespace SourceGit.Models { var ret = new List(); var posOld = 0; var posNew = 0; + var last = null as Different; do { while (posOld < sizeOld && posNew < sizeNew && !chunksOld[posOld].Modified && !chunksNew[posNew].Modified) { posOld++; @@ -97,13 +98,25 @@ namespace SourceGit.Models { for (; posOld < sizeOld && chunksOld[posOld].Modified; posOld++) countOld += chunksOld[posOld].Size; for (; posNew < sizeNew && chunksNew[posNew].Modified; posNew++) countNew += chunksNew[posNew].Size; - if (countOld + countNew > 0) { - ret.Add(new Different( - countOld > 0 ? chunksOld[beginOld].Start : 0, - countOld, - countNew > 0 ? chunksNew[beginNew].Start : 0, - countNew)); + if (countOld + countNew == 0) continue; + + var diff = new Different( + countOld > 0 ? chunksOld[beginOld].Start : 0, + countOld, + countNew > 0 ? chunksNew[beginNew].Start : 0, + countNew); + if (last != null) { + var midSizeOld = diff.DeletedStart - last.DeletedStart - last.DeletedCount; + var midSizeNew = diff.AddedStart - last.AddedStart - last.AddedCount; + if (midSizeOld == 1 && midSizeNew == 1) { + last.DeletedCount += (1 + countOld); + last.AddedCount += (1 + countNew); + continue; + } } + + last = diff; + ret.Add(diff); } while (posOld < sizeOld && posNew < sizeNew); return ret;