Skip to content

Commit c8106dc

Browse files
author
Thomas Hambach
committed
Added sentence support
1 parent 34def2c commit c8106dc

File tree

7 files changed

+149
-9
lines changed

7 files changed

+149
-9
lines changed

CSharpDiff.Tests/DiffSentenceTests.cs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using CSharpDiff.Convert;
2+
using CSharpDiff.Diff;
3+
4+
namespace CSharpDiff.Tests
5+
{
6+
7+
public class DiffSentenceTests
8+
{
9+
[Fact]
10+
public void ShouldDiffSentences()
11+
{
12+
var diff = new DiffSentence();
13+
var diffResult = diff.diff("New Value.", "New ValueMoreData.");
14+
var converted = DiffConvertXml.Convert(diffResult);
15+
Assert.Equal("<del>New Value.</del><ins>New ValueMoreData.</ins>", converted);
16+
}
17+
18+
[Fact]
19+
public void ShouldDiffLastSentence()
20+
{
21+
var diff = new DiffSentence();
22+
var diffResult = diff.diff("Here im. Rock you like old man.", "Here im. Rock you like hurricane.");
23+
var converted = DiffConvertXml.Convert(diffResult);
24+
Assert.Equal("Here im. <del>Rock you like old man.</del><ins>Rock you like hurricane.</ins>", converted);
25+
}
26+
}
27+
}

CSharpDiff/Diff/Diff.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace CSharpDiff.Diff
55
{
6-
public class Diff
6+
public class Diff : IDiff
77
{
88
public bool UseLongestToken { get; set; } = true;
99
public bool IgnoreWhiteSpace { get; set; } = false;
@@ -12,10 +12,10 @@ public IList<DiffResult> diff(string oldString, string newString)
1212
{
1313
var cleanOldString = removeEmpty(tokenize(oldString));
1414
var cleanNewString = removeEmpty(tokenize(newString));
15-
return determineDiff(oldString, newString, cleanOldString, cleanNewString);
15+
return determineDiff(cleanOldString, cleanNewString);
1616
}
1717

18-
public IList<DiffResult> determineDiff(string oldString, string newString, string[] cleanOldString, string[] cleanNewString)
18+
public IList<DiffResult> determineDiff(string[] cleanOldString, string[] cleanNewString)
1919
{
2020

2121
var diffs = new List<DiffResult>();

CSharpDiff/Diff/DiffArray.cs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using CSharpDiff.Diff.Models;
2+
3+
namespace CSharpDiff.Diff
4+
{
5+
public class DiffArray : Diff
6+
{
7+
public IList<DiffResult> diff(string[] oldArray, string[] newArray)
8+
{
9+
var cleanOldString = removeEmpty(oldArray);
10+
var cleanNewString = removeEmpty(newArray);
11+
return determineDiff(cleanOldString, cleanNewString);
12+
}
13+
14+
public new string join(string[] strings)
15+
{
16+
return "";
17+
}
18+
}
19+
}

CSharpDiff/Diff/DiffLines.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class DiffLines : Diff
88
{
99
var cleanOldString = removeEmpty(tokenize(oldString));
1010
var cleanNewString = removeEmpty(tokenize(newString));
11-
return determineDiff(oldString, newString, cleanOldString, cleanNewString);
11+
return determineDiff(cleanOldString, cleanNewString);
1212
}
1313

1414
public new string[] tokenize(string value)

CSharpDiff/Diff/DiffSentence.cs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Text.RegularExpressions;
2+
using CSharpDiff.Diff.Models;
3+
4+
namespace CSharpDiff.Diff
5+
{
6+
public class DiffSentence : Diff
7+
{
8+
public new IList<DiffResult> diff(string oldString, string newString)
9+
{
10+
var cleanOldString = removeEmpty(tokenize(oldString));
11+
var cleanNewString = removeEmpty(tokenize(newString));
12+
return determineDiff(cleanOldString, cleanNewString);
13+
}
14+
15+
public new string[] tokenize(string value)
16+
{
17+
var regex = new Regex(@"(\S.+?[.!?])(?=\s+|$)");
18+
return regex.Split(value);
19+
}
20+
}
21+
}

CSharpDiff/Diff/IDiff.cs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using CSharpDiff.Diff.Models;
2+
3+
namespace CSharpDiff.Diff
4+
{
5+
public interface IDiff
6+
{
7+
bool UseLongestToken { get; set; }
8+
9+
/// <summary>
10+
/// Defines whether we ignore white space.
11+
/// </summary>
12+
bool IgnoreWhiteSpace { get; set; }
13+
14+
List<DiffResult> buildValues(List<DiffResult> components, string[] newString, string[] oldString, bool useLongestToken);
15+
16+
BestPath clonePath(BestPath path);
17+
18+
IList<DiffResult> determineDiff(string[] cleanOldString, string[] cleanNewString);
19+
20+
IList<DiffResult> diff(string oldString, string newString);
21+
22+
/// <summary>
23+
/// Check whether left matches right.
24+
/// </summary>
25+
/// <param name="left"></param>
26+
/// <param name="right"></param>
27+
/// <returns></returns>
28+
bool equals(char left, char right);
29+
30+
/// <summary>
31+
/// Check whether left matches right.
32+
/// </summary>
33+
/// <param name="left"></param>
34+
/// <param name="right"></param>
35+
/// <returns></returns>
36+
bool equals(string left, string right);
37+
int extractCommon(BestPath basePath, string[] newString, string[] oldString, int diagonalPath);
38+
string join(string[] strings);
39+
List<DiffResult> pushComponent(List<DiffResult> components, bool? added, bool? removed);
40+
41+
/// <summary>
42+
/// Remove empty values from the array. Differs per diff type.
43+
/// </summary>
44+
/// <param name="array"></param>
45+
/// <returns></returns>
46+
string[] removeEmpty(string[] array);
47+
48+
/// <summary>
49+
/// Tokenize string, differs per diff type.
50+
/// </summary>
51+
/// <param name="value"></param>
52+
/// <returns></returns>
53+
string[] tokenize(string value);
54+
}
55+
}

README.md

+23-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,29 @@ C# Diff with Unified Diff Support, this codebase is a port from the popular JS l
1010

1111
## Usage Example
1212

13+
### Diff
14+
15+
#### Sentence
16+
17+
```c#
18+
using CSharpDiff.Diff;
19+
20+
var text1 = "Here im. Rock you like old man.";
21+
var text2 = "Here im. Rock you like hurricane.";
22+
23+
var diff = new DiffSentence();
24+
string patch = diff.diff(text1, text2);
25+
```
26+
27+
### Patch
28+
29+
#### Create
30+
1331
```c#
14-
using CSharpDiff;
32+
using CSharpDiff.Patch;
1533

16-
var text1 = "";
17-
var text2 = "";
34+
var text1 = "...";
35+
var text2 = "...";
1836

1937
var ps = new Patch();
2038
string patch = ps.create("filename1", "filename2", text1, text2, "header1", "header2", new PatchServiceOptions());
@@ -33,13 +51,13 @@ string patch = ps.create("filename1", "filename2", text1, text2, "header1", "hea
3351
- [ ] Merge
3452
- [ ] Parse
3553
- [ ] Diff
36-
- [ ] Array
54+
- [ ] Array (difficult, JS allows type mixing)
3755
- [x] Base
3856
- [ ] Character
3957
- [ ] CSS
4058
- [ ] JSON
4159
- [x] Line
42-
- [ ] Sentence
60+
- [x] Sentence
4361
- [ ] Word
4462
- [ ] Convert
4563
- [x] XML

0 commit comments

Comments
 (0)