Skip to content

Commit f2f64f1

Browse files
author
Thomas Hambach
committed
Added diffing for characters
1 parent f74b8e7 commit f2f64f1

21 files changed

+125
-58
lines changed

.vscode/settings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"omnisharp.organizeImportsOnFormat": true,
3+
"editor.codeActionsOnSave": {
4+
"source.fixAll": false,
5+
"source.organizeImports": true,
6+
"source.sortMembers": true
7+
}
8+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using CSharpDiff.Converters;
2+
using CSharpDiff.Diffs;
3+
using CSharpDiff.Diffs.Models;
4+
5+
namespace CSharpDiff.Tests
6+
{
7+
public class DiffCharacterTests
8+
{
9+
[Fact]
10+
public void ShouldDiffChars()
11+
{
12+
var diff = new Diff();
13+
var result = diff.diff("New Value.", "New ValueMoreData.");
14+
Assert.Equal("New Value<ins>MoreData</ins>.", DiffConvertXml.Convert(result));
15+
}
16+
17+
[Fact]
18+
public void ShouldConsiderCase()
19+
{
20+
var diff = new Diff(new DiffOptions
21+
{
22+
IgnoreCase = true
23+
});
24+
var result = diff.diff("New Value.", "New value.");
25+
Assert.Equal("New value.", DiffConvertXml.Convert(result));
26+
}
27+
28+
[Fact]
29+
public void ShouldConsiderCaseWithDifference()
30+
{
31+
var diff = new Diff(new DiffOptions
32+
{
33+
IgnoreCase = true
34+
});
35+
var result = diff.diff("New Values.", "New value.");
36+
Assert.Equal("New value<del>s</del>.", DiffConvertXml.Convert(result));
37+
}
38+
}
39+
}

CSharpDiff.Tests/DiffConvertXmlTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using CSharpDiff.Convert;
2-
using CSharpDiff.Diff.Models;
1+
using CSharpDiff.Converters;
2+
using CSharpDiff.Diffs.Models;
33

44
namespace CSharpDiff.Tests
55
{

CSharpDiff.Tests/DiffSentenceTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using CSharpDiff.Convert;
2-
using CSharpDiff.Diff;
1+
using CSharpDiff.Converters;
2+
using CSharpDiff.Diffs;
33

44
namespace CSharpDiff.Tests
55
{

CSharpDiff.Tests/DiffTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using CSharpDiff.Convert;
2-
using CSharpDiff.Diff;
1+
using CSharpDiff.Converters;
2+
using CSharpDiff.Diffs;
33

44
namespace CSharpDiff.Tests
55
{

CSharpDiff/CSharpDiff.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
<TargetFramework>net6.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<PackageId>CSharpDiff</PackageId>
7-
<Version>0.0.4</Version>
7+
<Version>0.0.5</Version>
88
<Authors>ThomasHambach</Authors>
99
<Description>
1010
Unified Diff support for .NET. Based on the popular library JSDIFF.
1111
</Description>
1212
<PackageTags>Diff;UniDiff;Line Diff;Unified Diff</PackageTags>
1313
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1414
<PackageReadmeFile>README.md</PackageReadmeFile>
15-
15+
<PackageProjectUrl>https://github.com/thomashambach/csharpdiff</PackageProjectUrl>
1616
</PropertyGroup>
1717
<ItemGroup>
1818
<None Include="..\README.md" Pack="true" PackagePath="\"/>

CSharpDiff/Convert/DiffConvertXml.cs renamed to CSharpDiff/Converters/DiffConvertXml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Web;
2-
using CSharpDiff.Diff.Models;
2+
using CSharpDiff.Diffs.Models;
33

4-
namespace CSharpDiff.Convert
4+
namespace CSharpDiff.Converters
55
{
66
public static class DiffConvertXml
77
{

CSharpDiff/Diff/Diff.cs renamed to CSharpDiff/Diffs/Diff.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
using System.Text.RegularExpressions;
2-
using CSharpDiff.Diff.Models;
2+
using CSharpDiff.Diffs.Models;
33

4-
namespace CSharpDiff.Diff
4+
namespace CSharpDiff.Diffs
55
{
66
public class Diff : IDiff
77
{
8-
public bool UseLongestToken { get; set; } = true;
9-
public bool IgnoreWhiteSpace { get; set; } = false;
8+
public Diff()
9+
{
10+
Options = new DiffOptions();
11+
}
12+
13+
public Diff(DiffOptions options)
14+
{
15+
Options = options;
16+
}
17+
18+
public DiffOptions Options { get; set; }
1019

1120
public IList<DiffResult> diff(string oldString, string newString)
1221
{
@@ -152,7 +161,7 @@ public List<DiffResult> buildValues(List<DiffResult> components, string[] newStr
152161
}
153162
else
154163
{
155-
component.value = join(newString[newPos..whereToTakeOld]);
164+
component.value = join(newString[newPos..whereToTake]);
156165
}
157166

158167
newPos += (int)component.count;
@@ -279,14 +288,9 @@ public int extractCommon(BestPath basePath, string[] newString, string[] oldStri
279288
return oldPos;
280289
}
281290

282-
public bool equals(char left, char right)
283-
{
284-
return left == right;
285-
}
286-
287291
public bool equals(string left, string right)
288292
{
289-
return left == right;
293+
return String.Compare(left, right, Options.IgnoreCase) == 0;
290294
}
291295
}
292296

CSharpDiff/Diff/DiffArray.cs renamed to CSharpDiff/Diffs/DiffArray.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using CSharpDiff.Diff.Models;
1+
using CSharpDiff.Diffs.Models;
22

3-
namespace CSharpDiff.Diff
3+
namespace CSharpDiff.Diffs
44
{
55
public class DiffArray : Diff
66
{

CSharpDiff/Diff/DiffLines.cs renamed to CSharpDiff/Diffs/DiffLines.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Text.RegularExpressions;
2-
using CSharpDiff.Diff.Models;
3-
namespace CSharpDiff.Diff
2+
using CSharpDiff.Diffs.Models;
3+
namespace CSharpDiff.Diffs
44
{
55
public class DiffLines : Diff
66
{
@@ -35,7 +35,7 @@ public class DiffLines : Diff
3535
}
3636
else
3737
{
38-
if (IgnoreWhiteSpace)
38+
if (Options.IgnoreWhiteSpace)
3939
{
4040
line = line.Trim();
4141
}

0 commit comments

Comments
 (0)