forked from fantaisie-software/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffAlgorithm.pb
375 lines (315 loc) · 10.5 KB
/
DiffAlgorithm.pb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
; --------------------------------------------------------------------------------------------
; Copyright (c) Fantaisie Software. All rights reserved.
; Dual licensed under the GPL and Fantaisie Software licenses.
; See LICENSE and LICENSE-FANTAISIE in the project root for license information.
; --------------------------------------------------------------------------------------------
; Implementation of the Myers Diff algorithm with the linear space refinement specified in
; "An O(ND) Difference Algorithm and Its Variations" -- Algorithmica 1, 2 (1986), 251-266.
; See here: http://www.xmailserver.org/diff2.pdf
;
; A useful tutorial with code examples:
; https://blog.jcoglan.com/2017/02/12/the-myers-diff-algorithm-part-1/
; https://blog.jcoglan.com/2017/02/15/the-myers-diff-algorithm-part-2/
; https://blog.jcoglan.com/2017/02/17/the-myers-diff-algorithm-part-3/
; https://blog.jcoglan.com/2017/03/22/myers-diff-in-linear-space-theory/
; https://blog.jcoglan.com/2017/04/25/myers-diff-in-linear-space-implementation/
;
; A very useful tutorial and graphical tool to visualize the algorithm:
; http://simplygenius.net/Article/DiffTutorial1
; http://simplygenius.net/Article/DiffTutorial2
;
; Compute checksum of a line depending on the given Flags
;
Procedure Diff_ComputeChecksum(*Start, Length, Flags)
If Length = 0
ProcedureReturn 0
ElseIf Flags = 0
ProcedureReturn CRC32Fingerprint(*Start, Length)
Else
; need to get a string to apply further options first
;
Protected Line$ = PeekS(*Start, Length, #PB_Ascii) ; ok for ascii and utf8, as we just need a checksum and no content
If Flags & #DIFF_IgnoreCase
Line$ = LCase(Line$)
EndIf
If Flags & #DIFF_IgnoreSpaceAll
Line$ = Trim(ReplaceString(Line$, Chr(9), " ")) ; convert tab, remove space on sides
; collapse any multiple space to single ones, so we have only significant spaces left
While FindString(Line$, " ", 1)
Line$ = ReplaceString(Line$, " ", " ")
Wend
Else
If Flags & #DIFF_IgnoreSpaceLeft
While Left(Line$, 1) = " " Or Left(Line$, 1) = Chr(9) ; simple trim is not enough here
Line$ = Right(Line$, Len(Line$)-1)
Wend
EndIf
If Flags & #DIFF_IgnoreSpaceRight
While Right(Line$, 1) = " " Or Right(Line$, 1) = Chr(9)
Line$ = Left(Line$, Len(Line$)-1)
Wend
EndIf
EndIf
If Len(Line$) = 0
ProcedureReturn 0
Else
; No need to convert back to Ascii. Just compute the hash of the unicode line
ProcedureReturn CRC32Fingerprint(@Line$, StringByteLength(Line$))
EndIf
EndIf
EndProcedure
; Scan a file and fill the lines array
;
Procedure Diff_PreProcess(*Pointer.Byte, Size, Array Lines.DiffLine(1), Flags)
Protected *BufferEnd = *Pointer + Size
Protected Lines = 0
Protected Space = ArraySize(Lines()) ; don't do +1 to have the extra space for the last line
Protected *LineStart = *Pointer
While *Pointer < *BufferEnd
; detect next newline
If *Pointer\b = 13
*Pointer + 1
If *Pointer < *BufferEnd And *Pointer\b = 10
*Pointer + 1
EndIf
ElseIf *Pointer\b = 10
*Pointer + 1
Else
; no newline
*Pointer + 1
Continue
EndIf
; newline found
If Space = 0
ReDim Lines(Lines * 2)
Space = Lines
EndIf
Lines(Lines)\Checksum = Diff_ComputeChecksum(*LineStart, *Pointer-*LineStart, Flags)
Lines(Lines)\Start = *LineStart
Lines(Lines)\Length = *Pointer - *LineStart
Lines + 1
Space - 1
*LineStart = *Pointer
Wend
; add the last line
If *Pointer > *LineStart
Lines(Lines)\Checksum = Diff_ComputeChecksum(*LineStart, *Pointer-*LineStart, Flags)
Lines(Lines)\Start = *LineStart
Lines(Lines)\Length = *Pointer - *LineStart
Lines + 1
EndIf
ProcedureReturn Lines
EndProcedure
; Add a (possible empty) edit to the edit script
; Multiple edits with the same Op are combined into one
;
Procedure Diff_AddEdit(*Ctx.DiffContext, Op, Array Lines.DiffLine(1), StartLine, Lines)
Protected Line
If Lines > 0
If LastElement(*Ctx\Edits()) = 0 Or *Ctx\Edits()\Op <> Op
; start a new edit
AddElement(*Ctx\Edits())
*Ctx\Edits()\Op = Op
*Ctx\Edits()\StartLine = StartLine
*Ctx\Edits()\Start = Lines(StartLine)\Start
EndIf
; append lines to existing edit
*Ctx\Edits()\Lines + Lines
For Line = StartLine To StartLine + Lines - 1
*Ctx\Edits()\Length + Lines(Line)\Length
Next Line
EndIf
EndProcedure
; Find middle snake
; Returns edit distance D
;
Procedure Diff_FindMiddleSnake(*Ctx.DiffContext, AOffset, N, BOffset, M, *x.Integer, *y.Integer, *u.Integer, *v.Integer)
Protected DELTA = N - M
Protected OFFSET = (N + M) * 2
Protected odd, mid, D, k, c, x, y
odd = Bool(DELTA % 2 <> 0)
If odd
mid = (N + M) / 2 + 1
Else
mid = (N + M) / 2
EndIf
*Ctx\FV(1+OFFSET) = 0
*Ctx\RV(DELTA-1+OFFSET) = N
For D = 0 To mid
; forward search
For k = -D To D Step 2
If k = -D Or (k <> D And *Ctx\FV(k-1+OFFSET) < *Ctx\FV(k+1+OFFSET))
x = *Ctx\FV(k+1+OFFSET)
Else
x = *Ctx\FV(k-1+OFFSET) + 1
EndIf
y = x - k
*x\i = x
*y\i = y
While x < N And y < M And *Ctx\A(AOffset + x)\Checksum = *Ctx\B(BOffset + y)\Checksum
x + 1
y + 1
Wend
*Ctx\FV(k+OFFSET) = x
If odd And k >= (DELTA - (D - 1)) And k <= (DELTA + (D - 1))
If x >= *Ctx\RV(k+OFFSET)
*u\i = x
*v\i = y
ProcedureReturn 2 * D - 1
EndIf
EndIf
Next k
; reverse search
For k = -D To D Step 2
c = k + DELTA
If k = D Or (k <> -D And *Ctx\RV(c-1+OFFSET) < *Ctx\RV(c+1+OFFSET))
x = *Ctx\RV(c-1+OFFSET)
Else
x = *Ctx\RV(c+1+OFFSET) - 1
EndIf
y = x - c
*u\i = x
*v\i = y
While x > 0 And y > 0 And *Ctx\A(AOffset + x-1)\Checksum = *Ctx\B(BOffset + y-1)\Checksum
x - 1
y - 1
Wend
*Ctx\RV(c+OFFSET) = x
If odd = 0 And c >= -D And c <= D
If x <= *Ctx\FV(c+OFFSET)
*x\i = x
*y\i = y
ProcedureReturn 2 * D
EndIf
EndIf
Next k
Next D
EndProcedure
; Recursively compute Diff
;
Procedure Diff_Recursive(*Ctx.DiffContext, AOffset, N, BOffset, M)
Protected D, x, y, u, v
If N > 0 And M = 0
; second sequence is empty: so only deletions
Diff_AddEdit(*Ctx, #DIFF_DELETE, *Ctx\A(), AOffset, N)
ElseIf N = 0 And M > 0
; first sequence is empty: so only additions
Diff_AddEdit(*Ctx, #DIFF_INSERT, *Ctx\B(), BOffset, M)
ElseIf N > 0 And M > 0
D = Diff_FindMiddleSnake(*Ctx, AOffset, N, BOffset, M, @x, @y, @u, @v)
;Debug "D = " + D + " x=" + x + " y=" + y + " -> u=" + u + " v=" + v
If D = 0
; both sequences are equal
Diff_AddEdit(*Ctx, #DIFF_MATCH, *Ctx\A(), AOffset, N)
ElseIf D = 1
; exactly one edit and 0 or more matches
If M > N
If x = u
; \
; \
; |
Diff_AddEdit(*Ctx, #DIFF_MATCH, *Ctx\A(), AOffset, N)
Diff_AddEdit(*Ctx, #DIFF_INSERT, *Ctx\B(), BOffset + M-1, 1)
Else
; |
; \
; \
Diff_AddEdit(*Ctx, #DIFF_INSERT, *Ctx\B(), BOffset, 1)
Diff_AddEdit(*Ctx, #DIFF_MATCH, *Ctx\A(), AOffset, N)
EndIf
Else
If x = u
; \
; \
; -
Diff_AddEdit(*Ctx, #DIFF_MATCH, *Ctx\A(), AOffset, M)
Diff_AddEdit(*Ctx, #DIFF_DELETE, *Ctx\A(), AOffset + N-1, 1)
Else
; -
; \
; \
Diff_AddEdit(*Ctx, #DIFF_DELETE, *Ctx\A(), AOffset, 1)
Diff_AddEdit(*Ctx, #DIFF_MATCH, *Ctx\A(), AOffset + 1, M)
EndIf
EndIf
Else
; general case: recurse around the middle snake
Diff_Recursive(*Ctx, AOffset, x, BOffset, y)
Diff_AddEdit(*Ctx, #DIFF_MATCH, *Ctx\A(), AOffset + x, u-x)
Diff_Recursive(*Ctx, AOffset + u, N-u, BOffset + v, M-v)
EndIf
EndIf
EndProcedure
; Main Diff function
;
Procedure Diff(*Ctx.DiffContext, *BufferA, SizeA, *BufferB, SizeB, Flags = 0)
Protected N, M, MAX, Offset
N = Diff_PreProcess(*BufferA, SizeA, *Ctx\A(), Flags)
M = Diff_PreProcess(*BufferB, SizeB, *Ctx\B(), Flags)
*Ctx\LineCountA = N
*Ctx\LineCountB = M
Dim *Ctx\FV((N + M)*4+1)
Dim *Ctx\RV((N + M)*4+1)
ClearList(*Ctx\Edits())
; Note:
; The handling of the D=1 Case in FindMiddleSnake() assumes that we always start
; with an inset Or delete. So strip off common lines at the start to ensure this
Offset = 0
While Offset < N And Offset < M And *Ctx\A(Offset)\Checksum = *Ctx\B(Offset)\Checksum
Offset + 1
Wend
Diff_AddEdit(*Ctx, #DIFF_Match, *Ctx\A(), 0, Offset)
Diff_Recursive(*Ctx, Offset, N-Offset, Offset, M-Offset)
FreeArray(*Ctx\FV())
FreeArray(*Ctx\RV())
EndProcedure
; Some debugging utilities
;
CompilerIf #PB_Compiler_Debugger
Procedure DebugEdit(Prefix$, List Edits.DiffEdit(), Array Lines.DiffLine(1))
Protected Line, Length, Line$
For Line = Edits()\StartLine To Edits()\StartLine + Edits()\Lines - 1
Line$ = PeekS(Lines(Line)\Start, Lines(Line)\Length, #PB_Ascii)
Line$ = RTrim(RTrim(Line$, Chr(10)), Chr(13)) ; trim newline
Debug Prefix$ + " " + Line$
Next Line
EndProcedure
Procedure DebugDiff(*Ctx.DiffContext)
ForEach *Ctx\Edits()
Select *Ctx\Edits()\Op
Case #DIFF_Match
DebugEdit(" ", *Ctx\Edits(), *Ctx\A())
Case #DIFF_Delete
DebugEdit("-", *Ctx\Edits(), *Ctx\A())
Case #DIFF_Insert
DebugEdit("+", *Ctx\Edits(), *Ctx\B())
EndSelect
Next *Ctx\Edits()
EndProcedure
Procedure DebugFile(FileA$, FileB$)
Protected *BufferA, SizeA, *BufferB, SizeB, Context.DiffContext
If ReadFile(0, FileA$)
SizeA = Lof(0)
*BufferA = AllocateMemory(SizeA)
ReadData(0, *BufferA, SizeA)
CloseFile(0)
Else
Debug "Cannot load: " + FileA$
End
EndIf
If ReadFile(0, FileB$)
SizeB = Lof(0)
*BufferB = AllocateMemory(SizeB)
ReadData(0, *BufferB, SizeB)
CloseFile(0)
Else
Debug "Cannot load: " + FileB$
End
EndIf
Diff(@Context, *BufferA, SizeA, *BufferB, SizeB)
DebugDiff(@Context)
FreeMemory(*BufferA)
FreeMemory(*BufferB)
EndProcedure
;DebugFile("C:\PureBasic\Test\diff\Preferences\0081.pb", "C:\PureBasic\Test\diff\Preferences\0082.pb")
CompilerEndIf