mmanela / diffplex

DiffPlex is Netstandard 1.0+ C# library to generate textual diffs.
Apache License 2.0
996 stars 183 forks source link

Not expected result #73

Closed boukenka closed 3 years ago

boukenka commented 3 years ago

Hello,

I am doing the following :

string oldText ="<revision>1<revision>";
string newText = "<revision>2</revision>";
SideBySideDiffBuilder sideBySideDiffBuilder = new SideBySideDiffBuilder();
SideBySideDiffModel sideBySideDiffModel = sideBySideDiffBuilder.BuildDiffModel(oldText, newText);

The result should be that only character '1' has been changed to '2'. Or it is not the result. The line of the old text is defined as deleted in the subPieces and the line of the new text is defined as inserted in the subPieces. Could you help please? Is it by design? Is there a workaround?

PS: If ignoreWhiteSpace is set to True or False, the result remains the same.

mmanela commented 3 years ago

The default word separator does not consider < or > to break words (this may be a mistake but has been this way for a long time). To get around this you need to pass your own word breaker. The below code shows you how:

string oldText = "<revision>1<revision>";
string newText = "<revision>2</revision>";
char[] wordSeparaters = { ' ', '\t', '.', '(', ')', '{', '}', ',', '!', '?', ';', '<','>' };
var delimiterChunker = new DelimiterChunker(wordSeparaters);
SideBySideDiffBuilder sideBySideDiffBuilder = new SideBySideDiffBuilder(Differ.Instance, new LineChunker(), delimiterChunker);
SideBySideDiffModel sideBySideDiffModel = sideBySideDiffBuilder.BuildDiffModel(oldText, newText);
boukenka commented 3 years ago

Thank you @mmanela. I will give it a go. 😄

boukenka commented 3 years ago

That solved my problem. 😄

May I suggest the following two points, please.

Thanks again @mmanela.

mmanela commented 3 years ago

@boukenka Both are reasonable suggestion. Would you be interested in contributing :) PRs are gladly appreciated