@@ -20,7 +20,7 @@ import (
20
20
21
21
// Patch represents one patch operation.
22
22
type Patch struct {
23
- diffs []Diff
23
+ Diffs []Diff
24
24
Start1 int
25
25
Start2 int
26
26
Length1 int
@@ -53,7 +53,7 @@ func (p *Patch) String() string {
53
53
_ , _ = text .WriteString ("@@ -" + coords1 + " +" + coords2 + " @@\n " )
54
54
55
55
// Escape the body of the patch with %xx notation.
56
- for _ , aDiff := range p .diffs {
56
+ for _ , aDiff := range p .Diffs {
57
57
switch aDiff .Type {
58
58
case DiffInsert :
59
59
_ , _ = text .WriteString ("+" )
@@ -94,12 +94,12 @@ func (dmp *DiffMatchPatch) PatchAddContext(patch Patch, text string) Patch {
94
94
// Add the prefix.
95
95
prefix := text [max (0 , patch .Start2 - padding ):patch .Start2 ]
96
96
if len (prefix ) != 0 {
97
- patch .diffs = append ([]Diff {{DiffEqual , prefix }}, patch .diffs ... )
97
+ patch .Diffs = append ([]Diff {{DiffEqual , prefix }}, patch .Diffs ... )
98
98
}
99
99
// Add the suffix.
100
100
suffix := text [patch .Start2 + patch .Length1 : min (len (text ), patch .Start2 + patch .Length1 + padding )]
101
101
if len (suffix ) != 0 {
102
- patch .diffs = append (patch .diffs , Diff {DiffEqual , suffix })
102
+ patch .Diffs = append (patch .Diffs , Diff {DiffEqual , suffix })
103
103
}
104
104
105
105
// Roll back the start points.
@@ -155,33 +155,33 @@ func (dmp *DiffMatchPatch) patchMake2(text1 string, diffs []Diff) []Patch {
155
155
postpatchText := text1
156
156
157
157
for i , aDiff := range diffs {
158
- if len (patch .diffs ) == 0 && aDiff .Type != DiffEqual {
158
+ if len (patch .Diffs ) == 0 && aDiff .Type != DiffEqual {
159
159
// A new patch starts here.
160
160
patch .Start1 = charCount1
161
161
patch .Start2 = charCount2
162
162
}
163
163
164
164
switch aDiff .Type {
165
165
case DiffInsert :
166
- patch .diffs = append (patch .diffs , aDiff )
166
+ patch .Diffs = append (patch .Diffs , aDiff )
167
167
patch .Length2 += len (aDiff .Text )
168
168
postpatchText = postpatchText [:charCount2 ] +
169
169
aDiff .Text + postpatchText [charCount2 :]
170
170
case DiffDelete :
171
171
patch .Length1 += len (aDiff .Text )
172
- patch .diffs = append (patch .diffs , aDiff )
172
+ patch .Diffs = append (patch .Diffs , aDiff )
173
173
postpatchText = postpatchText [:charCount2 ] + postpatchText [charCount2 + len (aDiff .Text ):]
174
174
case DiffEqual :
175
175
if len (aDiff .Text ) <= 2 * dmp .PatchMargin &&
176
- len (patch .diffs ) != 0 && i != len (diffs )- 1 {
176
+ len (patch .Diffs ) != 0 && i != len (diffs )- 1 {
177
177
// Small equality inside a patch.
178
- patch .diffs = append (patch .diffs , aDiff )
178
+ patch .Diffs = append (patch .Diffs , aDiff )
179
179
patch .Length1 += len (aDiff .Text )
180
180
patch .Length2 += len (aDiff .Text )
181
181
}
182
182
if len (aDiff .Text ) >= 2 * dmp .PatchMargin {
183
183
// Time for a new patch.
184
- if len (patch .diffs ) != 0 {
184
+ if len (patch .Diffs ) != 0 {
185
185
patch = dmp .PatchAddContext (patch , prepatchText )
186
186
patches = append (patches , patch )
187
187
patch = Patch {}
@@ -204,7 +204,7 @@ func (dmp *DiffMatchPatch) patchMake2(text1 string, diffs []Diff) []Patch {
204
204
}
205
205
206
206
// Pick up the leftover patch if not empty.
207
- if len (patch .diffs ) != 0 {
207
+ if len (patch .Diffs ) != 0 {
208
208
patch = dmp .PatchAddContext (patch , prepatchText )
209
209
patches = append (patches , patch )
210
210
}
@@ -217,8 +217,8 @@ func (dmp *DiffMatchPatch) PatchDeepCopy(patches []Patch) []Patch {
217
217
patchesCopy := []Patch {}
218
218
for _ , aPatch := range patches {
219
219
patchCopy := Patch {}
220
- for _ , aDiff := range aPatch .diffs {
221
- patchCopy .diffs = append (patchCopy .diffs , Diff {
220
+ for _ , aDiff := range aPatch .Diffs {
221
+ patchCopy .Diffs = append (patchCopy .Diffs , Diff {
222
222
aDiff .Type ,
223
223
aDiff .Text ,
224
224
})
@@ -255,7 +255,7 @@ func (dmp *DiffMatchPatch) PatchApply(patches []Patch, text string) (string, []b
255
255
results := make ([]bool , len (patches ))
256
256
for _ , aPatch := range patches {
257
257
expectedLoc := aPatch .Start2 + delta
258
- text1 := dmp .DiffText1 (aPatch .diffs )
258
+ text1 := dmp .DiffText1 (aPatch .Diffs )
259
259
var startLoc int
260
260
endLoc := - 1
261
261
if len (text1 ) > dmp .MatchMaxBits {
@@ -289,7 +289,7 @@ func (dmp *DiffMatchPatch) PatchApply(patches []Patch, text string) (string, []b
289
289
}
290
290
if text1 == text2 {
291
291
// Perfect match, just shove the Replacement text in.
292
- text = text [:startLoc ] + dmp .DiffText2 (aPatch .diffs ) + text [startLoc + len (text1 ):]
292
+ text = text [:startLoc ] + dmp .DiffText2 (aPatch .Diffs ) + text [startLoc + len (text1 ):]
293
293
} else {
294
294
// Imperfect match. Run a diff to get a framework of equivalent indices.
295
295
diffs := dmp .DiffMain (text1 , text2 , false )
@@ -300,7 +300,7 @@ func (dmp *DiffMatchPatch) PatchApply(patches []Patch, text string) (string, []b
300
300
} else {
301
301
diffs = dmp .DiffCleanupSemanticLossless (diffs )
302
302
index1 := 0
303
- for _ , aDiff := range aPatch .diffs {
303
+ for _ , aDiff := range aPatch .Diffs {
304
304
if aDiff .Type != DiffEqual {
305
305
index2 := dmp .DiffXIndex (diffs , index1 )
306
306
if aDiff .Type == DiffInsert {
@@ -343,17 +343,17 @@ func (dmp *DiffMatchPatch) PatchAddPadding(patches []Patch) string {
343
343
}
344
344
345
345
// Add some padding on start of first diff.
346
- if len (patches [0 ].diffs ) == 0 || patches [0 ].diffs [0 ].Type != DiffEqual {
346
+ if len (patches [0 ].Diffs ) == 0 || patches [0 ].Diffs [0 ].Type != DiffEqual {
347
347
// Add nullPadding equality.
348
- patches [0 ].diffs = append ([]Diff {{DiffEqual , nullPadding }}, patches [0 ].diffs ... )
348
+ patches [0 ].Diffs = append ([]Diff {{DiffEqual , nullPadding }}, patches [0 ].Diffs ... )
349
349
patches [0 ].Start1 -= paddingLength // Should be 0.
350
350
patches [0 ].Start2 -= paddingLength // Should be 0.
351
351
patches [0 ].Length1 += paddingLength
352
352
patches [0 ].Length2 += paddingLength
353
- } else if paddingLength > len (patches [0 ].diffs [0 ].Text ) {
353
+ } else if paddingLength > len (patches [0 ].Diffs [0 ].Text ) {
354
354
// Grow first equality.
355
- extraLength := paddingLength - len (patches [0 ].diffs [0 ].Text )
356
- patches [0 ].diffs [0 ].Text = nullPadding [len (patches [0 ].diffs [0 ].Text ):] + patches [0 ].diffs [0 ].Text
355
+ extraLength := paddingLength - len (patches [0 ].Diffs [0 ].Text )
356
+ patches [0 ].Diffs [0 ].Text = nullPadding [len (patches [0 ].Diffs [0 ].Text ):] + patches [0 ].Diffs [0 ].Text
357
357
patches [0 ].Start1 -= extraLength
358
358
patches [0 ].Start2 -= extraLength
359
359
patches [0 ].Length1 += extraLength
@@ -362,17 +362,17 @@ func (dmp *DiffMatchPatch) PatchAddPadding(patches []Patch) string {
362
362
363
363
// Add some padding on end of last diff.
364
364
last := len (patches ) - 1
365
- if len (patches [last ].diffs ) == 0 ||
366
- patches [last ].diffs [len (patches [last ].diffs )- 1 ].Type != DiffEqual {
365
+ if len (patches [last ].Diffs ) == 0 ||
366
+ patches [last ].Diffs [len (patches [last ].Diffs )- 1 ].Type != DiffEqual {
367
367
// Add nullPadding equality.
368
- patches [last ].diffs = append (patches [last ].diffs , Diff {DiffEqual , nullPadding })
368
+ patches [last ].Diffs = append (patches [last ].Diffs , Diff {DiffEqual , nullPadding })
369
369
patches [last ].Length1 += paddingLength
370
370
patches [last ].Length2 += paddingLength
371
- } else if paddingLength > len (patches [last ].diffs [len (patches [last ].diffs )- 1 ].Text ) {
371
+ } else if paddingLength > len (patches [last ].Diffs [len (patches [last ].Diffs )- 1 ].Text ) {
372
372
// Grow last equality.
373
- lastDiff := patches [last ].diffs [len (patches [last ].diffs )- 1 ]
373
+ lastDiff := patches [last ].Diffs [len (patches [last ].Diffs )- 1 ]
374
374
extraLength := paddingLength - len (lastDiff .Text )
375
- patches [last ].diffs [len (patches [last ].diffs )- 1 ].Text += nullPadding [:extraLength ]
375
+ patches [last ].Diffs [len (patches [last ].Diffs )- 1 ].Text += nullPadding [:extraLength ]
376
376
patches [last ].Length1 += extraLength
377
377
patches [last ].Length2 += extraLength
378
378
}
@@ -396,7 +396,7 @@ func (dmp *DiffMatchPatch) PatchSplitMax(patches []Patch) []Patch {
396
396
Start1 := bigpatch .Start1
397
397
Start2 := bigpatch .Start2
398
398
precontext := ""
399
- for len (bigpatch .diffs ) != 0 {
399
+ for len (bigpatch .Diffs ) != 0 {
400
400
// Create one of several smaller patches.
401
401
patch := Patch {}
402
402
empty := true
@@ -405,25 +405,25 @@ func (dmp *DiffMatchPatch) PatchSplitMax(patches []Patch) []Patch {
405
405
if len (precontext ) != 0 {
406
406
patch .Length1 = len (precontext )
407
407
patch .Length2 = len (precontext )
408
- patch .diffs = append (patch .diffs , Diff {DiffEqual , precontext })
408
+ patch .Diffs = append (patch .Diffs , Diff {DiffEqual , precontext })
409
409
}
410
- for len (bigpatch .diffs ) != 0 && patch .Length1 < patchSize - dmp .PatchMargin {
411
- diffType := bigpatch .diffs [0 ].Type
412
- diffText := bigpatch .diffs [0 ].Text
410
+ for len (bigpatch .Diffs ) != 0 && patch .Length1 < patchSize - dmp .PatchMargin {
411
+ diffType := bigpatch .Diffs [0 ].Type
412
+ diffText := bigpatch .Diffs [0 ].Text
413
413
if diffType == DiffInsert {
414
414
// Insertions are harmless.
415
415
patch .Length2 += len (diffText )
416
416
Start2 += len (diffText )
417
- patch .diffs = append (patch .diffs , bigpatch .diffs [0 ])
418
- bigpatch .diffs = bigpatch .diffs [1 :]
417
+ patch .Diffs = append (patch .Diffs , bigpatch .Diffs [0 ])
418
+ bigpatch .Diffs = bigpatch .Diffs [1 :]
419
419
empty = false
420
- } else if diffType == DiffDelete && len (patch .diffs ) == 1 && patch .diffs [0 ].Type == DiffEqual && len (diffText ) > 2 * patchSize {
420
+ } else if diffType == DiffDelete && len (patch .Diffs ) == 1 && patch .Diffs [0 ].Type == DiffEqual && len (diffText ) > 2 * patchSize {
421
421
// This is a large deletion. Let it pass in one chunk.
422
422
patch .Length1 += len (diffText )
423
423
Start1 += len (diffText )
424
424
empty = false
425
- patch .diffs = append (patch .diffs , Diff {diffType , diffText })
426
- bigpatch .diffs = bigpatch .diffs [1 :]
425
+ patch .Diffs = append (patch .Diffs , Diff {diffType , diffText })
426
+ bigpatch .Diffs = bigpatch .Diffs [1 :]
427
427
} else {
428
428
// Deletion or equality. Only take as much as we can stomach.
429
429
diffText = diffText [:min (len (diffText ), patchSize - patch .Length1 - dmp .PatchMargin )]
@@ -436,33 +436,33 @@ func (dmp *DiffMatchPatch) PatchSplitMax(patches []Patch) []Patch {
436
436
} else {
437
437
empty = false
438
438
}
439
- patch .diffs = append (patch .diffs , Diff {diffType , diffText })
440
- if diffText == bigpatch .diffs [0 ].Text {
441
- bigpatch .diffs = bigpatch .diffs [1 :]
439
+ patch .Diffs = append (patch .Diffs , Diff {diffType , diffText })
440
+ if diffText == bigpatch .Diffs [0 ].Text {
441
+ bigpatch .Diffs = bigpatch .Diffs [1 :]
442
442
} else {
443
- bigpatch .diffs [0 ].Text = bigpatch .diffs [0 ].Text [len (diffText ):]
443
+ bigpatch .Diffs [0 ].Text = bigpatch .Diffs [0 ].Text [len (diffText ):]
444
444
}
445
445
}
446
446
}
447
447
// Compute the head context for the next patch.
448
- precontext = dmp .DiffText2 (patch .diffs )
448
+ precontext = dmp .DiffText2 (patch .Diffs )
449
449
precontext = precontext [max (0 , len (precontext )- dmp .PatchMargin ):]
450
450
451
451
postcontext := ""
452
452
// Append the end context for this patch.
453
- if len (dmp .DiffText1 (bigpatch .diffs )) > dmp .PatchMargin {
454
- postcontext = dmp .DiffText1 (bigpatch .diffs )[:dmp .PatchMargin ]
453
+ if len (dmp .DiffText1 (bigpatch .Diffs )) > dmp .PatchMargin {
454
+ postcontext = dmp .DiffText1 (bigpatch .Diffs )[:dmp .PatchMargin ]
455
455
} else {
456
- postcontext = dmp .DiffText1 (bigpatch .diffs )
456
+ postcontext = dmp .DiffText1 (bigpatch .Diffs )
457
457
}
458
458
459
459
if len (postcontext ) != 0 {
460
460
patch .Length1 += len (postcontext )
461
461
patch .Length2 += len (postcontext )
462
- if len (patch .diffs ) != 0 && patch .diffs [len (patch .diffs )- 1 ].Type == DiffEqual {
463
- patch .diffs [len (patch .diffs )- 1 ].Text += postcontext
462
+ if len (patch .Diffs ) != 0 && patch .Diffs [len (patch .Diffs )- 1 ].Type == DiffEqual {
463
+ patch .Diffs [len (patch .Diffs )- 1 ].Text += postcontext
464
464
} else {
465
- patch .diffs = append (patch .diffs , Diff {DiffEqual , postcontext })
465
+ patch .Diffs = append (patch .Diffs , Diff {DiffEqual , postcontext })
466
466
}
467
467
}
468
468
if ! empty {
@@ -542,13 +542,13 @@ func (dmp *DiffMatchPatch) PatchFromText(textline string) ([]Patch, error) {
542
542
line , _ = url .QueryUnescape (line )
543
543
if sign == '-' {
544
544
// Deletion.
545
- patch .diffs = append (patch .diffs , Diff {DiffDelete , line })
545
+ patch .Diffs = append (patch .Diffs , Diff {DiffDelete , line })
546
546
} else if sign == '+' {
547
547
// Insertion.
548
- patch .diffs = append (patch .diffs , Diff {DiffInsert , line })
548
+ patch .Diffs = append (patch .Diffs , Diff {DiffInsert , line })
549
549
} else if sign == ' ' {
550
550
// Minor equality.
551
- patch .diffs = append (patch .diffs , Diff {DiffEqual , line })
551
+ patch .Diffs = append (patch .Diffs , Diff {DiffEqual , line })
552
552
} else if sign == '@' {
553
553
// Start of next patch.
554
554
break
0 commit comments