-
Notifications
You must be signed in to change notification settings - Fork 15
Open
Description
This samples show the basic power of this DiffLib and is provided as VB.NET:
Option Explicit On
Option Strict On
Imports DiffLib
Module Module1
Sub Main()
Const text1 As String = "This is a test of the diff implementation, with some text that is deleted."
Const text2 As String = "This is another test of the same implementation, with some more text."
Diffs.DumpDiffToConsole(text1, text2)
Console.WriteLine()
End Sub
End Module
Public Class Diffs
Public Shared Function DumpDiffAsHtml(text1 As String, text2 As String) As String
Dim sections As IEnumerable(Of DiffSection) = Diff.CalculateSections(Of Char)(text1.ToCharArray, text2.ToCharArray)
Return DumpDiffAsHtml(text1, text2, sections)
End Function
Private Shared Function DumpDiffAsHtml(text1 As String, text2 As String, sections As IEnumerable(Of DiffSection)) As String
Dim html = New System.Text.StringBuilder()
Dim i1 = 0
Dim i2 = 0
For Each section In sections
If section.IsMatch Then
html.Append(text1.Substring(i1, section.LengthInCollection1))
Else
html.Append("<span style='background-color: #ffcccc; text-decoration: line-through;'>" + text1.Substring(i1, section.LengthInCollection1) + "</span>")
html.Append("<span style='background-color: #ccffcc;'>" + text2.Substring(i2, section.LengthInCollection2) + "</span>")
End If
i1 += section.LengthInCollection1
i2 += section.LengthInCollection2
Next
Return html.ToString()
End Function
Public Shared Sub DumpDiffToConsole(text1 As String, text2 As String)
Dim sections As IEnumerable(Of DiffSection) = Diff.CalculateSections(Of Char)(text1.ToCharArray, text2.ToCharArray)
DumpDiffToConsole(text1, text2, sections)
End Sub
Private Shared Sub DumpDiffToConsole(text1 As String, text2 As String, sections As IEnumerable(Of DiffSection))
Dim DefaultColor As ConsoleColor = Console.ForegroundColor
Dim html = New System.Text.StringBuilder()
Dim i1 = 0
Dim i2 = 0
For Each section In sections
If section.IsMatch Then
Console.ForegroundColor = DefaultColor
Console.Write(text1.Substring(i1, section.LengthInCollection1))
Else
Console.ForegroundColor = ConsoleColor.Red
Console.Write(text1.Substring(i1, section.LengthInCollection1))
Console.ForegroundColor = ConsoleColor.Green
Console.Write(text2.Substring(i2, section.LengthInCollection2))
End If
i1 += section.LengthInCollection1
i2 += section.LengthInCollection2
Next
Console.ForegroundColor = DefaultColor
End Sub
End ClassAn alternative for HTML output is following method:
Const text1 As String = "This is a test of the diff implementation, with some text that is deleted."
Const text2 As String = "This is another test of the same implementation, with some more text."
Dim Html As String = Diffs.DumpDiffAsHtml(text1, text2)which generated following output
This is a<span style="background-color: #ffcccc; text-decoration: line-through;"></span><span style="background-color: #ccffcc;">nother</span> test of the <span style="background-color: #ffcccc; text-decoration: line-through;">diff</span><span style="background-color: #ccffcc;">same</span> implementation, with some <span style="background-color: #ffcccc; text-decoration: line-through;"></span><span style="background-color: #ccffcc;">more </span>text<span style="background-color: #ffcccc; text-decoration: line-through;"> that is deleted</span><span style="background-color: #ccffcc;"></span>.Metadata
Metadata
Assignees
Labels
No labels

