如何使用 google-diff-match-patch C# 库?

发布于 2024-11-08 23:16:43 字数 602 浏览 5 评论 0原文

我正在查看 http://code.google.com/p/google-diff-match-patch / 并已下载该文件。当我看到它是 2 个文件时

DiffMatchPatch.cs
DiffMatchPatchTest.cs

,当我尝试创建 DiffMatchPatch.cs 的新对象时,我必须传入一些操作和字符串文本。

http://neil.fraser.name/software/diff_match_patch/svn/trunk/demos/ demo_diff.html

在演示中,他们划掉了不同的单词,这就是我想要实现的目标。

我正在尝试比较服务器端的 2 个文本块,找出差异,并向用户发送一封电子邮件,其中包含文件文本块,就像最终结果在我上面发布的演示中一样。

那么有人有关于如何使用 C# 版本的教程吗?

I am looking at http://code.google.com/p/google-diff-match-patch/ and have downloaded the file. When I look at it is 2 files

DiffMatchPatch.cs
DiffMatchPatchTest.cs

When I try to make a new object of DiffMatchPatch.cs I have to pass in some operation and string text.

http://neil.fraser.name/software/diff_match_patch/svn/trunk/demos/demo_diff.html

In the demo they cross out the words that are different and that is what I am trying to achieve.

I am trying to compare 2 blocks of text on the server side finds the differences and send a email to the user with the file block of text to them like the end result is in the demo that I posted above.

So does anyone have a tutorial on how to use the C# version?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぇ气 2024-11-15 23:16:43

作为参考,这非常简单:

var dmp = new diff_match_patch();
var diffs = dmp.diff_main(text1, text2);
var html = dmp.diff_prettyHtml(diffs);

For reference, this is really easy:

var dmp = new diff_match_patch();
var diffs = dmp.diff_main(text1, text2);
var html = dmp.diff_prettyHtml(diffs);
方觉久 2024-11-15 23:16:43

当前版本(2.1.0)的实现如下所示

var dmp = DiffMatchPatchModule.Default;
var diffs = dmp.DiffMain(text1, text2);
var html = dmp.DiffPrettyHtml(diffs);

Implementation with current version(2.1.0) would look like this

var dmp = DiffMatchPatchModule.Default;
var diffs = dmp.DiffMain(text1, text2);
var html = dmp.DiffPrettyHtml(diffs);
窝囊感情。 2024-11-15 23:16:43

对于任何因标题而遇到此线程并期望通过 https://github.com/pocketberserker/Diff.Match.Patch 在 NuGet 上找到的库,用于创建 diff 字符串,以便他可以将更改发送到某个地方(例如通过 websocket)并且根据旧值和差异字符串在目的地恢复它,其工作方式如下:

var oldValue = "Test old text.";
var newValue = "Test new text.";

// create diff string
var dmp = DiffMatchPatch.DiffMatchPatchModule.Default;
var diffs = dmp.DiffMain(oldValue, newValue);
var srcDelta = dmp.DiffToDelta(diffs);
// restore from diff
var dmp = DiffMatchPatch.DiffMatchPatchModule.Default;
var dstDelta = dmp.DiffFromDelta(oldValue, srcDelta);
var restoredNewValue = dmp.DiffText2(dstDelta);

For anyone who came across this thread because of the title and expected an explanation on how to use the Google Diff-Match-Patch algorithm via the https://github.com/pocketberserker/Diff.Match.Patch library found on NuGet, to create a diff string, so he can send the change somewhere (e.g. via websocket) and restore it at the destination based on the old value and the diff string, that would work like this:

var oldValue = "Test old text.";
var newValue = "Test new text.";

// create diff string
var dmp = DiffMatchPatch.DiffMatchPatchModule.Default;
var diffs = dmp.DiffMain(oldValue, newValue);
var srcDelta = dmp.DiffToDelta(diffs);
// restore from diff
var dmp = DiffMatchPatch.DiffMatchPatchModule.Default;
var dstDelta = dmp.DiffFromDelta(oldValue, srcDelta);
var restoredNewValue = dmp.DiffText2(dstDelta);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文