@@ -21,10 +21,10 @@ import (
21
21
// Patch represents one patch operation.
22
22
type Patch struct {
23
23
diffs []Diff
24
- start1 int
25
- start2 int
26
- length1 int
27
- length2 int
24
+ Start1 int
25
+ Start2 int
26
+ Length1 int
27
+ Length2 int
28
28
}
29
29
30
30
// String emulates GNU diff's format.
@@ -33,20 +33,20 @@ type Patch struct {
33
33
func (p * Patch ) String () string {
34
34
var coords1 , coords2 string
35
35
36
- if p .length1 == 0 {
37
- coords1 = strconv .Itoa (p .start1 ) + ",0"
38
- } else if p .length1 == 1 {
39
- coords1 = strconv .Itoa (p .start1 + 1 )
36
+ if p .Length1 == 0 {
37
+ coords1 = strconv .Itoa (p .Start1 ) + ",0"
38
+ } else if p .Length1 == 1 {
39
+ coords1 = strconv .Itoa (p .Start1 + 1 )
40
40
} else {
41
- coords1 = strconv .Itoa (p .start1 + 1 ) + "," + strconv .Itoa (p .length1 )
41
+ coords1 = strconv .Itoa (p .Start1 + 1 ) + "," + strconv .Itoa (p .Length1 )
42
42
}
43
43
44
- if p .length2 == 0 {
45
- coords2 = strconv .Itoa (p .start2 ) + ",0"
46
- } else if p .length2 == 1 {
47
- coords2 = strconv .Itoa (p .start2 + 1 )
44
+ if p .Length2 == 0 {
45
+ coords2 = strconv .Itoa (p .Start2 ) + ",0"
46
+ } else if p .Length2 == 1 {
47
+ coords2 = strconv .Itoa (p .Start2 + 1 )
48
48
} else {
49
- coords2 = strconv .Itoa (p .start2 + 1 ) + "," + strconv .Itoa (p .length2 )
49
+ coords2 = strconv .Itoa (p .Start2 + 1 ) + "," + strconv .Itoa (p .Length2 )
50
50
}
51
51
52
52
var text bytes.Buffer
@@ -76,37 +76,37 @@ func (dmp *DiffMatchPatch) PatchAddContext(patch Patch, text string) Patch {
76
76
return patch
77
77
}
78
78
79
- pattern := text [patch .start2 : patch .start2 + patch .length1 ]
79
+ pattern := text [patch .Start2 : patch .Start2 + patch .Length1 ]
80
80
padding := 0
81
81
82
82
// Look for the first and last matches of pattern in text. If two different matches are found, increase the pattern length.
83
83
for strings .Index (text , pattern ) != strings .LastIndex (text , pattern ) &&
84
84
len (pattern ) < dmp .MatchMaxBits - 2 * dmp .PatchMargin {
85
85
padding += dmp .PatchMargin
86
- maxStart := max (0 , patch .start2 - padding )
87
- minEnd := min (len (text ), patch .start2 + patch .length1 + padding )
86
+ maxStart := max (0 , patch .Start2 - padding )
87
+ minEnd := min (len (text ), patch .Start2 + patch .Length1 + padding )
88
88
pattern = text [maxStart :minEnd ]
89
89
}
90
90
// Add one chunk for good luck.
91
91
padding += dmp .PatchMargin
92
92
93
93
// Add the prefix.
94
- prefix := text [max (0 , patch .start2 - padding ):patch .start2 ]
94
+ prefix := text [max (0 , patch .Start2 - padding ):patch .Start2 ]
95
95
if len (prefix ) != 0 {
96
96
patch .diffs = append ([]Diff {Diff {DiffEqual , prefix }}, patch .diffs ... )
97
97
}
98
98
// Add the suffix.
99
- suffix := text [patch .start2 + patch .length1 : min (len (text ), patch .start2 + patch .length1 + padding )]
99
+ suffix := text [patch .Start2 + patch .Length1 : min (len (text ), patch .Start2 + patch .Length1 + padding )]
100
100
if len (suffix ) != 0 {
101
101
patch .diffs = append (patch .diffs , Diff {DiffEqual , suffix })
102
102
}
103
103
104
104
// Roll back the start points.
105
- patch .start1 -= len (prefix )
106
- patch .start2 -= len (prefix )
105
+ patch .Start1 -= len (prefix )
106
+ patch .Start2 -= len (prefix )
107
107
// Extend the lengths.
108
- patch .length1 += len (prefix ) + len (suffix )
109
- patch .length2 += len (prefix ) + len (suffix )
108
+ patch .Length1 += len (prefix ) + len (suffix )
109
+ patch .Length2 += len (prefix ) + len (suffix )
110
110
111
111
return patch
112
112
}
@@ -155,27 +155,27 @@ func (dmp *DiffMatchPatch) patchMake2(text1 string, diffs []Diff) []Patch {
155
155
for i , aDiff := range diffs {
156
156
if len (patch .diffs ) == 0 && aDiff .Type != DiffEqual {
157
157
// A new patch starts here.
158
- patch .start1 = charCount1
159
- patch .start2 = charCount2
158
+ patch .Start1 = charCount1
159
+ patch .Start2 = charCount2
160
160
}
161
161
162
162
switch aDiff .Type {
163
163
case DiffInsert :
164
164
patch .diffs = append (patch .diffs , aDiff )
165
- patch .length2 += len (aDiff .Text )
165
+ patch .Length2 += len (aDiff .Text )
166
166
postpatchText = postpatchText [:charCount2 ] +
167
167
aDiff .Text + postpatchText [charCount2 :]
168
168
case DiffDelete :
169
- patch .length1 += len (aDiff .Text )
169
+ patch .Length1 += len (aDiff .Text )
170
170
patch .diffs = append (patch .diffs , aDiff )
171
171
postpatchText = postpatchText [:charCount2 ] + postpatchText [charCount2 + len (aDiff .Text ):]
172
172
case DiffEqual :
173
173
if len (aDiff .Text ) <= 2 * dmp .PatchMargin &&
174
174
len (patch .diffs ) != 0 && i != len (diffs )- 1 {
175
175
// Small equality inside a patch.
176
176
patch .diffs = append (patch .diffs , aDiff )
177
- patch .length1 += len (aDiff .Text )
178
- patch .length2 += len (aDiff .Text )
177
+ patch .Length1 += len (aDiff .Text )
178
+ patch .Length2 += len (aDiff .Text )
179
179
}
180
180
if len (aDiff .Text ) >= 2 * dmp .PatchMargin {
181
181
// Time for a new patch.
@@ -219,10 +219,10 @@ func (dmp *DiffMatchPatch) PatchDeepCopy(patches []Patch) []Patch {
219
219
aDiff .Text ,
220
220
})
221
221
}
222
- patchCopy .start1 = aPatch .start1
223
- patchCopy .start2 = aPatch .start2
224
- patchCopy .length1 = aPatch .length1
225
- patchCopy .length2 = aPatch .length2
222
+ patchCopy .Start1 = aPatch .Start1
223
+ patchCopy .Start2 = aPatch .Start2
224
+ patchCopy .Length1 = aPatch .Length1
225
+ patchCopy .Length2 = aPatch .Length2
226
226
patchesCopy = append (patchesCopy , patchCopy )
227
227
}
228
228
return patchesCopy
@@ -246,7 +246,7 @@ func (dmp *DiffMatchPatch) PatchApply(patches []Patch, text string) (string, []b
246
246
delta := 0
247
247
results := make ([]bool , len (patches ))
248
248
for _ , aPatch := range patches {
249
- expectedLoc := aPatch .start2 + delta
249
+ expectedLoc := aPatch .Start2 + delta
250
250
text1 := dmp .DiffText1 (aPatch .diffs )
251
251
var startLoc int
252
252
endLoc := - 1
@@ -268,7 +268,7 @@ func (dmp *DiffMatchPatch) PatchApply(patches []Patch, text string) (string, []b
268
268
// No match found. :(
269
269
results [x ] = false
270
270
// Subtract the delta for this failed patch from subsequent patches.
271
- delta -= aPatch .length2 - aPatch .length1
271
+ delta -= aPatch .Length2 - aPatch .Length1
272
272
} else {
273
273
// Found a match. :)
274
274
results [x ] = true
@@ -329,42 +329,42 @@ func (dmp *DiffMatchPatch) PatchAddPadding(patches []Patch) string {
329
329
330
330
// Bump all the patches forward.
331
331
for i := range patches {
332
- patches [i ].start1 += paddingLength
333
- patches [i ].start2 += paddingLength
332
+ patches [i ].Start1 += paddingLength
333
+ patches [i ].Start2 += paddingLength
334
334
}
335
335
336
336
// Add some padding on start of first diff.
337
337
if len (patches [0 ].diffs ) == 0 || patches [0 ].diffs [0 ].Type != DiffEqual {
338
338
// Add nullPadding equality.
339
339
patches [0 ].diffs = append ([]Diff {Diff {DiffEqual , nullPadding }}, patches [0 ].diffs ... )
340
- patches [0 ].start1 -= paddingLength // Should be 0.
341
- patches [0 ].start2 -= paddingLength // Should be 0.
342
- patches [0 ].length1 += paddingLength
343
- patches [0 ].length2 += paddingLength
340
+ patches [0 ].Start1 -= paddingLength // Should be 0.
341
+ patches [0 ].Start2 -= paddingLength // Should be 0.
342
+ patches [0 ].Length1 += paddingLength
343
+ patches [0 ].Length2 += paddingLength
344
344
} else if paddingLength > len (patches [0 ].diffs [0 ].Text ) {
345
345
// Grow first equality.
346
346
extraLength := paddingLength - len (patches [0 ].diffs [0 ].Text )
347
347
patches [0 ].diffs [0 ].Text = nullPadding [len (patches [0 ].diffs [0 ].Text ):] + patches [0 ].diffs [0 ].Text
348
- patches [0 ].start1 -= extraLength
349
- patches [0 ].start2 -= extraLength
350
- patches [0 ].length1 += extraLength
351
- patches [0 ].length2 += extraLength
348
+ patches [0 ].Start1 -= extraLength
349
+ patches [0 ].Start2 -= extraLength
350
+ patches [0 ].Length1 += extraLength
351
+ patches [0 ].Length2 += extraLength
352
352
}
353
353
354
354
// Add some padding on end of last diff.
355
355
last := len (patches ) - 1
356
356
if len (patches [last ].diffs ) == 0 || patches [last ].diffs [len (patches [last ].diffs )- 1 ].Type != DiffEqual {
357
357
// Add nullPadding equality.
358
358
patches [last ].diffs = append (patches [last ].diffs , Diff {DiffEqual , nullPadding })
359
- patches [last ].length1 += paddingLength
360
- patches [last ].length2 += paddingLength
359
+ patches [last ].Length1 += paddingLength
360
+ patches [last ].Length2 += paddingLength
361
361
} else if paddingLength > len (patches [last ].diffs [len (patches [last ].diffs )- 1 ].Text ) {
362
362
// Grow last equality.
363
363
lastDiff := patches [last ].diffs [len (patches [last ].diffs )- 1 ]
364
364
extraLength := paddingLength - len (lastDiff .Text )
365
365
patches [last ].diffs [len (patches [last ].diffs )- 1 ].Text += nullPadding [:extraLength ]
366
- patches [last ].length1 += extraLength
367
- patches [last ].length2 += extraLength
366
+ patches [last ].Length1 += extraLength
367
+ patches [last ].Length2 += extraLength
368
368
}
369
369
370
370
return nullPadding
@@ -375,54 +375,54 @@ func (dmp *DiffMatchPatch) PatchAddPadding(patches []Patch) string {
375
375
func (dmp * DiffMatchPatch ) PatchSplitMax (patches []Patch ) []Patch {
376
376
patchSize := dmp .MatchMaxBits
377
377
for x := 0 ; x < len (patches ); x ++ {
378
- if patches [x ].length1 <= patchSize {
378
+ if patches [x ].Length1 <= patchSize {
379
379
continue
380
380
}
381
381
bigpatch := patches [x ]
382
382
// Remove the big old patch.
383
383
patches = append (patches [:x ], patches [x + 1 :]... )
384
384
x --
385
385
386
- start1 := bigpatch .start1
387
- start2 := bigpatch .start2
386
+ Start1 := bigpatch .Start1
387
+ Start2 := bigpatch .Start2
388
388
precontext := ""
389
389
for len (bigpatch .diffs ) != 0 {
390
390
// Create one of several smaller patches.
391
391
patch := Patch {}
392
392
empty := true
393
- patch .start1 = start1 - len (precontext )
394
- patch .start2 = start2 - len (precontext )
393
+ patch .Start1 = Start1 - len (precontext )
394
+ patch .Start2 = Start2 - len (precontext )
395
395
if len (precontext ) != 0 {
396
- patch .length1 = len (precontext )
397
- patch .length2 = len (precontext )
396
+ patch .Length1 = len (precontext )
397
+ patch .Length2 = len (precontext )
398
398
patch .diffs = append (patch .diffs , Diff {DiffEqual , precontext })
399
399
}
400
- for len (bigpatch .diffs ) != 0 && patch .length1 < patchSize - dmp .PatchMargin {
400
+ for len (bigpatch .diffs ) != 0 && patch .Length1 < patchSize - dmp .PatchMargin {
401
401
diffType := bigpatch .diffs [0 ].Type
402
402
diffText := bigpatch .diffs [0 ].Text
403
403
if diffType == DiffInsert {
404
404
// Insertions are harmless.
405
- patch .length2 += len (diffText )
406
- start2 += len (diffText )
405
+ patch .Length2 += len (diffText )
406
+ Start2 += len (diffText )
407
407
patch .diffs = append (patch .diffs , bigpatch .diffs [0 ])
408
408
bigpatch .diffs = bigpatch .diffs [1 :]
409
409
empty = false
410
410
} else if diffType == DiffDelete && len (patch .diffs ) == 1 && patch .diffs [0 ].Type == DiffEqual && len (diffText ) > 2 * patchSize {
411
411
// This is a large deletion. Let it pass in one chunk.
412
- patch .length1 += len (diffText )
413
- start1 += len (diffText )
412
+ patch .Length1 += len (diffText )
413
+ Start1 += len (diffText )
414
414
empty = false
415
415
patch .diffs = append (patch .diffs , Diff {diffType , diffText })
416
416
bigpatch .diffs = bigpatch .diffs [1 :]
417
417
} else {
418
418
// Deletion or equality. Only take as much as we can stomach.
419
- diffText = diffText [:min (len (diffText ), patchSize - patch .length1 - dmp .PatchMargin )]
419
+ diffText = diffText [:min (len (diffText ), patchSize - patch .Length1 - dmp .PatchMargin )]
420
420
421
- patch .length1 += len (diffText )
422
- start1 += len (diffText )
421
+ patch .Length1 += len (diffText )
422
+ Start1 += len (diffText )
423
423
if diffType == DiffEqual {
424
- patch .length2 += len (diffText )
425
- start2 += len (diffText )
424
+ patch .Length2 += len (diffText )
425
+ Start2 += len (diffText )
426
426
} else {
427
427
empty = false
428
428
}
@@ -448,8 +448,8 @@ func (dmp *DiffMatchPatch) PatchSplitMax(patches []Patch) []Patch {
448
448
}
449
449
450
450
if len (postcontext ) != 0 {
451
- patch .length1 += len (postcontext )
452
- patch .length2 += len (postcontext )
451
+ patch .Length1 += len (postcontext )
452
+ patch .Length2 += len (postcontext )
453
453
if len (patch .diffs ) != 0 && patch .diffs [len (patch .diffs )- 1 ].Type == DiffEqual {
454
454
patch .diffs [len (patch .diffs )- 1 ].Text += postcontext
455
455
} else {
@@ -496,27 +496,27 @@ func (dmp *DiffMatchPatch) PatchFromText(textline string) ([]Patch, error) {
496
496
patch = Patch {}
497
497
m := patchHeader .FindStringSubmatch (text [textPointer ])
498
498
499
- patch .start1 , _ = strconv .Atoi (m [1 ])
499
+ patch .Start1 , _ = strconv .Atoi (m [1 ])
500
500
if len (m [2 ]) == 0 {
501
- patch .start1 --
502
- patch .length1 = 1
501
+ patch .Start1 --
502
+ patch .Length1 = 1
503
503
} else if m [2 ] == "0" {
504
- patch .length1 = 0
504
+ patch .Length1 = 0
505
505
} else {
506
- patch .start1 --
507
- patch .length1 , _ = strconv .Atoi (m [2 ])
506
+ patch .Start1 --
507
+ patch .Length1 , _ = strconv .Atoi (m [2 ])
508
508
}
509
509
510
- patch .start2 , _ = strconv .Atoi (m [3 ])
510
+ patch .Start2 , _ = strconv .Atoi (m [3 ])
511
511
512
512
if len (m [4 ]) == 0 {
513
- patch .start2 --
514
- patch .length2 = 1
513
+ patch .Start2 --
514
+ patch .Length2 = 1
515
515
} else if m [4 ] == "0" {
516
- patch .length2 = 0
516
+ patch .Length2 = 0
517
517
} else {
518
- patch .start2 --
519
- patch .length2 , _ = strconv .Atoi (m [4 ])
518
+ patch .Start2 --
519
+ patch .Length2 , _ = strconv .Atoi (m [4 ])
520
520
}
521
521
textPointer ++
522
522
0 commit comments