Skip to content

Commit

Permalink
Merge pull request #238 from sillsdev/ht-441-error-computing-string-d…
Browse files Browse the repository at this point in the history
…iffs

HT-441: Added code to detect error when computing string differences
  • Loading branch information
tombogle authored Jun 28, 2022
2 parents 94964c6 + 21f9f76 commit 665d749
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions src/HearThis/Utils/StringDifferences/StringDifferenceFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Linq;
using System.Text;
using SIL.Extensions;
using SIL.Reporting;
using static Icu.Character;
using static System.Char;

Expand Down Expand Up @@ -65,24 +66,36 @@ public StringDifferenceFinder(string origStr, string newStr)
throw new ArgumentException("String must not be null or empty", nameof(origStr));
if (string.IsNullOrEmpty(newStr))
throw new ArgumentException("String must not be null or empty", nameof(newStr));
foreach (var segment in ComputeDifferences(origStr, newStr))
try
{
switch (segment.Type)
foreach (var segment in ComputeDifferences(origStr, newStr))
{
case DifferenceType.Same:
OriginalStringDifferences.Add(segment);
NewStringDifferences.Add(segment);
break;
case DifferenceType.Addition:
NewStringDifferences.Add(segment);
break;
case DifferenceType.Deletion:
OriginalStringDifferences.Add(segment);
break;
default:
throw new ArgumentOutOfRangeException();
switch (segment.Type)
{
case DifferenceType.Same:
OriginalStringDifferences.Add(segment);
NewStringDifferences.Add(segment);
break;
case DifferenceType.Addition:
NewStringDifferences.Add(segment);
break;
case DifferenceType.Deletion:
OriginalStringDifferences.Add(segment);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
catch (Exception e)
{
Logger.WriteError(e);
ErrorReport.NotifyUserOfProblem(e, $"A problem occurred trying to compute differences between {origStr} and {newStr} (HT-441). Please report this.");
OriginalStringDifferences.Clear();
OriginalStringDifferences.Add(new StringDifferenceSegment(DifferenceType.Deletion, origStr));
NewStringDifferences.Clear();
OriginalStringDifferences.Add(new StringDifferenceSegment(DifferenceType.Addition, newStr));
}
}

private enum CharactersToKeepTogether
Expand Down Expand Up @@ -359,8 +372,8 @@ bool MustRemoveFollowingBase(CharactersToKeepTogether c) =>

if (common.Length > 1)
{
var iOrig = origStr.IndexOf(common);
var iNew = newStr.IndexOf(common);
var iOrig = origStr.IndexOf(common, StringComparison.Ordinal);
var iNew = newStr.IndexOf(common, StringComparison.Ordinal);
if (iNew < iOrig)
ExpandCommonSubstringIfNeeded(ref iNew, ref iOrig, ref common, newStr, origStr);
else
Expand Down

0 comments on commit 665d749

Please sign in to comment.