Skip to content

Commit

Permalink
BsDiff.Search is no longer recursive (possible StackOverflowException)
Browse files Browse the repository at this point in the history
  • Loading branch information
jzebedee committed Oct 21, 2014
1 parent 81bffcd commit 24bc904
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions deltaq/BsDiff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,25 +225,32 @@ private static int MatchLength(IList<byte> oldData, IList<byte> newData)

private static int Search(IList<int> I, byte[] oldData, IList<byte> newData, int start, int end, out int pos)
{
if (end - start < 2)
while (true)
{
var startLength = MatchLength(oldData.Slice(I[start]), newData);
var endLength = MatchLength(oldData.Slice(I[end]), newData);
if (end - start < 2)
{
var startLength = MatchLength(oldData.Slice(I[start]), newData);
var endLength = MatchLength(oldData.Slice(I[end]), newData);

if (startLength > endLength)
{
pos = I[start];
return startLength;
}

if (startLength > endLength)
pos = I[end];
return endLength;
}

var midPoint = start + (end - start) / 2;
if (CompareBytes(oldData.Slice(I[midPoint]), newData) < 0)
{
pos = I[start];
return startLength;
start = midPoint;
continue;
}

pos = I[end];
return endLength;
end = midPoint;
}

var midPoint = start + (end - start) / 2;
return CompareBytes(oldData.Slice(I[midPoint]), newData) < 0 ?
Search(I, oldData, newData, midPoint, end, out pos) :
Search(I, oldData, newData, start, midPoint, out pos);
}
}
}

0 comments on commit 24bc904

Please sign in to comment.