I've previously written a (not very robust) text comparison procedure and would to make it a lot better. I'm doing the usual net search for algorithms and source code. I also looked on the forum but did not find any PowerBASIC code along those lines.
Basically, I'd like to put text in two RichEdit controls and use colors to highlight the differences - a WinDiff kind of comparison. But I don't want a separate utility. I already use my limited procedure in gbSnippets to compare snippets from different libraries, and would like to improve the procedure.
The code below is my starting point - it just basically does a look ahead for a fixed number of lines. It reads the left side, looks for a match within the next few lines (10 in this case) on the right side. When it discovers differences it offsets the differing lines, using "********" to fill in where an offset was displayed. Speed of the comparison shouldn't be an issue, given the relatively small snippet sizes I'm working with.
My procedure generally works for small differences, but has limitations. It does not address RichEdit coloration of the differences.
If anyone has something to share, I'd appreciate hearing about it.
Basically, I'd like to put text in two RichEdit controls and use colors to highlight the differences - a WinDiff kind of comparison. But I don't want a separate utility. I already use my limited procedure in gbSnippets to compare snippets from different libraries, and would like to improve the procedure.
The code below is my starting point - it just basically does a look ahead for a fixed number of lines. It reads the left side, looks for a match within the next few lines (10 in this case) on the right side. When it discovers differences it offsets the differing lines, using "********" to fill in where an offset was displayed. Speed of the comparison shouldn't be an issue, given the relatively small snippet sizes I'm working with.
My procedure generally works for small differences, but has limitations. It does not address RichEdit coloration of the differences.
If anyone has something to share, I'd appreciate hearing about it.
Code:
'Compilable Example: #Compile Exe #Dim All #Include "win32api.inc" #Include "richedit.inc" #Include "commctrl.inc" %ID_RELeft = 200 : %ID_RERight = 300 %ID_Button1 = 400 : %ID_Button2 = 500 Global hDlg As Dword, hRELeft As Dword, hRERight As Dword Function PBMain () As Long Local Style& style& = %WS_Child Or %WS_Visible Or %ES_MultiLine Or %WS_VScroll Or %ES_AutoHScroll _ Or %WS_HScroll Or %ES_AutoVScroll Or %ES_WantReturn Or %ES_NoHideSel Or %WS_TabStop Dialog New Pixels, 0, "Compare Text Example",400,400,400,200, %WS_OverlappedWindow To hDlg Control Add Button, hDlg, %ID_Button1, "Compare", 30,10,100,20 Control Add Button, hDlg, %ID_Button2, "Reset", 150,10,100,20 LoadLibrary("riched32.dll") : InitCommonControls Control Add "RichEdit", hDlg, %ID_RELeft, "",0,0,50,50, style&, %WS_Ex_ClientEdge Control Add "RichEdit", hDlg, %ID_RERight, "",0,0,50,50, style&, %WS_Ex_ClientEdge Control Handle hDlg, %ID_RELeft To hRELeft Control Handle hDlg, %ID_RERight To hRERight LoadText Dialog Show Modal hDlg Call DlgProc End Function CallBack Function DlgProc() As Long Select Case Cb.Msg Case %WM_Command If Cb.Ctl = %ID_Button1 Then ShowDifference If Cb.Ctl = %ID_Button2 Then LoadText Case %WM_Size 'resizes controls when form is resized Dim w As Long, h As Long Dialog Get Client Cb.Hndl To w,h Control Set Loc Cb.Hndl, %ID_RELeft, 5,35 Control Set Size Cb.Hndl, %ID_RELeft, (w-20)/2, h-40 Control Set Loc Cb.Hndl, %ID_RERight, w/2+5, 35 Control Set Size Cb.Hndl, %ID_RERight, (w-20)/2, h-40 End Select End Function Sub LoadText Local temp$ temp$ = "These are lines" + $CrLf + "of text to compare" + $CrLf + "to the right side." Control Set Text hDlg, %ID_RELeft, temp$ temp$ = "These are lines" + $CrLf + "of text to compare" + $CrLf + "to the left side." Control Set Text hDlg, %ID_RERight, temp$ End Sub Sub ShowDifference Local Found As Long, iStep As Long, A As String, B As String, SColor As String Local i As Long, j As Long, n As Long, Done As Long, r As Long Local A1() As String, B1() As String, S() As String, LText As String, RText As String 'get raw text data Control Get Text hDlg, %ID_RELeft To A Control Get Text hDlg, %ID_RERight To B ReDim A1(ParseCount(A,$CrLf)-1), B1(ParseCount(B,$CrLf)-1) Parse A, A1(), $CrLf : Parse B, B1(), $CrLf LText = "" : RText = "" 'start by comparing lines A1(0) and B1(0) Do While Not Done If i > UBound(A1) Then 'no more A1() entries, so load all remaining B1() entries For n = j To UBound(B1) LText = LText + $CrLf + "" RText = RText + $CrLf + B1(n) SColor = SColor & ":" & Str$(n) Next n Done = %True Exit Do End If If j > UBound(B1) Then 'no more B1() entries, so load all remaining A1() entries For n = i To UBound(A1) LText = LText + $CrLf + A1(n) RText = RText + $CrLf + "" SColor = SColor & ":" & Str$(n) Next n Done = %True Exit Do End If If A1(i) = B1(j) Then 'they are equal so display them LText = LText + $CrLf + A1(i) RText = RText + $CrLf + B1(j) 'go to next pair of lines i = i + 1 j = j + 1 Else 'they are not equal, so check to see if B1(j) is 'found within the next 10 lines of A1() Found = %False iStep = 20 If i + iStep > UBound(A1) Then iStep = UBound(A1) - i For r = 1 To iStep If A1(i + r) = B1(j) Then Found = %True Exit For End If Next r If Found = %True Then 'B1(j) was found within 10 lines of A() 'print all of A1(i)-n/a up to the point it is found 'then print A1(i)-B1(j) 'If r > 1 Then For n = 0 To r - 1 LText = LText + $CrLf + A1(i + n) RText = RText + $CrLf + String$(200, "*") SColor = SColor & ":" & Str$(i + n) Next n 'End If LText = LText + $CrLf + A1(i + r) RText = RText + $CrLf + B1(j) i = i + r + 1 j = j + 1 Else 'B1(j) was not found within 10 lines of A() 'print n/a-B1(j) LText = LText + $CrLf + String$(200, "*") RText = RText + $CrLf + B1(j) SColor = SColor & ":" & Str$(j) j = j + 1 End If End If Loop 'get rid of leading $crlf If Len(LText) > 0 Then LText = Right$(LText, Len(LText) - 2) If Len(RText) > 0 Then RText = Right$(RText, Len(RText) - 2) 'show differences Control Set Text hDlg, %ID_RELeft, LText Control Set Text hDlg, %ID_RERight, RText End Sub
Comment