Skip to content

Commit 2fc9cd3

Browse files
authored
Merge pull request sergi#82 from engineone/master
exported patch fields
2 parents 832b506 + 9500d9a commit 2fc9cd3

File tree

2 files changed

+84
-84
lines changed

2 files changed

+84
-84
lines changed

diffmatchpatch/patch.go

Lines changed: 80 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ import (
2121
// Patch represents one patch operation.
2222
type Patch struct {
2323
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
2828
}
2929

3030
// String emulates GNU diff's format.
@@ -33,20 +33,20 @@ type Patch struct {
3333
func (p *Patch) String() string {
3434
var coords1, coords2 string
3535

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)
4040
} else {
41-
coords1 = strconv.Itoa(p.start1+1) + "," + strconv.Itoa(p.length1)
41+
coords1 = strconv.Itoa(p.Start1+1) + "," + strconv.Itoa(p.Length1)
4242
}
4343

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)
4848
} else {
49-
coords2 = strconv.Itoa(p.start2+1) + "," + strconv.Itoa(p.length2)
49+
coords2 = strconv.Itoa(p.Start2+1) + "," + strconv.Itoa(p.Length2)
5050
}
5151

5252
var text bytes.Buffer
@@ -76,37 +76,37 @@ func (dmp *DiffMatchPatch) PatchAddContext(patch Patch, text string) Patch {
7676
return patch
7777
}
7878

79-
pattern := text[patch.start2 : patch.start2+patch.length1]
79+
pattern := text[patch.Start2 : patch.Start2+patch.Length1]
8080
padding := 0
8181

8282
// Look for the first and last matches of pattern in text. If two different matches are found, increase the pattern length.
8383
for strings.Index(text, pattern) != strings.LastIndex(text, pattern) &&
8484
len(pattern) < dmp.MatchMaxBits-2*dmp.PatchMargin {
8585
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)
8888
pattern = text[maxStart:minEnd]
8989
}
9090
// Add one chunk for good luck.
9191
padding += dmp.PatchMargin
9292

9393
// Add the prefix.
94-
prefix := text[max(0, patch.start2-padding):patch.start2]
94+
prefix := text[max(0, patch.Start2-padding):patch.Start2]
9595
if len(prefix) != 0 {
9696
patch.diffs = append([]Diff{Diff{DiffEqual, prefix}}, patch.diffs...)
9797
}
9898
// 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)]
100100
if len(suffix) != 0 {
101101
patch.diffs = append(patch.diffs, Diff{DiffEqual, suffix})
102102
}
103103

104104
// 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)
107107
// 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)
110110

111111
return patch
112112
}
@@ -155,27 +155,27 @@ func (dmp *DiffMatchPatch) patchMake2(text1 string, diffs []Diff) []Patch {
155155
for i, aDiff := range diffs {
156156
if len(patch.diffs) == 0 && aDiff.Type != DiffEqual {
157157
// A new patch starts here.
158-
patch.start1 = charCount1
159-
patch.start2 = charCount2
158+
patch.Start1 = charCount1
159+
patch.Start2 = charCount2
160160
}
161161

162162
switch aDiff.Type {
163163
case DiffInsert:
164164
patch.diffs = append(patch.diffs, aDiff)
165-
patch.length2 += len(aDiff.Text)
165+
patch.Length2 += len(aDiff.Text)
166166
postpatchText = postpatchText[:charCount2] +
167167
aDiff.Text + postpatchText[charCount2:]
168168
case DiffDelete:
169-
patch.length1 += len(aDiff.Text)
169+
patch.Length1 += len(aDiff.Text)
170170
patch.diffs = append(patch.diffs, aDiff)
171171
postpatchText = postpatchText[:charCount2] + postpatchText[charCount2+len(aDiff.Text):]
172172
case DiffEqual:
173173
if len(aDiff.Text) <= 2*dmp.PatchMargin &&
174174
len(patch.diffs) != 0 && i != len(diffs)-1 {
175175
// Small equality inside a patch.
176176
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)
179179
}
180180
if len(aDiff.Text) >= 2*dmp.PatchMargin {
181181
// Time for a new patch.
@@ -219,10 +219,10 @@ func (dmp *DiffMatchPatch) PatchDeepCopy(patches []Patch) []Patch {
219219
aDiff.Text,
220220
})
221221
}
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
226226
patchesCopy = append(patchesCopy, patchCopy)
227227
}
228228
return patchesCopy
@@ -246,7 +246,7 @@ func (dmp *DiffMatchPatch) PatchApply(patches []Patch, text string) (string, []b
246246
delta := 0
247247
results := make([]bool, len(patches))
248248
for _, aPatch := range patches {
249-
expectedLoc := aPatch.start2 + delta
249+
expectedLoc := aPatch.Start2 + delta
250250
text1 := dmp.DiffText1(aPatch.diffs)
251251
var startLoc int
252252
endLoc := -1
@@ -268,7 +268,7 @@ func (dmp *DiffMatchPatch) PatchApply(patches []Patch, text string) (string, []b
268268
// No match found. :(
269269
results[x] = false
270270
// Subtract the delta for this failed patch from subsequent patches.
271-
delta -= aPatch.length2 - aPatch.length1
271+
delta -= aPatch.Length2 - aPatch.Length1
272272
} else {
273273
// Found a match. :)
274274
results[x] = true
@@ -329,42 +329,42 @@ func (dmp *DiffMatchPatch) PatchAddPadding(patches []Patch) string {
329329

330330
// Bump all the patches forward.
331331
for i := range patches {
332-
patches[i].start1 += paddingLength
333-
patches[i].start2 += paddingLength
332+
patches[i].Start1 += paddingLength
333+
patches[i].Start2 += paddingLength
334334
}
335335

336336
// Add some padding on start of first diff.
337337
if len(patches[0].diffs) == 0 || patches[0].diffs[0].Type != DiffEqual {
338338
// Add nullPadding equality.
339339
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
344344
} else if paddingLength > len(patches[0].diffs[0].Text) {
345345
// Grow first equality.
346346
extraLength := paddingLength - len(patches[0].diffs[0].Text)
347347
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
352352
}
353353

354354
// Add some padding on end of last diff.
355355
last := len(patches) - 1
356356
if len(patches[last].diffs) == 0 || patches[last].diffs[len(patches[last].diffs)-1].Type != DiffEqual {
357357
// Add nullPadding equality.
358358
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
361361
} else if paddingLength > len(patches[last].diffs[len(patches[last].diffs)-1].Text) {
362362
// Grow last equality.
363363
lastDiff := patches[last].diffs[len(patches[last].diffs)-1]
364364
extraLength := paddingLength - len(lastDiff.Text)
365365
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
368368
}
369369

370370
return nullPadding
@@ -375,54 +375,54 @@ func (dmp *DiffMatchPatch) PatchAddPadding(patches []Patch) string {
375375
func (dmp *DiffMatchPatch) PatchSplitMax(patches []Patch) []Patch {
376376
patchSize := dmp.MatchMaxBits
377377
for x := 0; x < len(patches); x++ {
378-
if patches[x].length1 <= patchSize {
378+
if patches[x].Length1 <= patchSize {
379379
continue
380380
}
381381
bigpatch := patches[x]
382382
// Remove the big old patch.
383383
patches = append(patches[:x], patches[x+1:]...)
384384
x--
385385

386-
start1 := bigpatch.start1
387-
start2 := bigpatch.start2
386+
Start1 := bigpatch.Start1
387+
Start2 := bigpatch.Start2
388388
precontext := ""
389389
for len(bigpatch.diffs) != 0 {
390390
// Create one of several smaller patches.
391391
patch := Patch{}
392392
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)
395395
if len(precontext) != 0 {
396-
patch.length1 = len(precontext)
397-
patch.length2 = len(precontext)
396+
patch.Length1 = len(precontext)
397+
patch.Length2 = len(precontext)
398398
patch.diffs = append(patch.diffs, Diff{DiffEqual, precontext})
399399
}
400-
for len(bigpatch.diffs) != 0 && patch.length1 < patchSize-dmp.PatchMargin {
400+
for len(bigpatch.diffs) != 0 && patch.Length1 < patchSize-dmp.PatchMargin {
401401
diffType := bigpatch.diffs[0].Type
402402
diffText := bigpatch.diffs[0].Text
403403
if diffType == DiffInsert {
404404
// Insertions are harmless.
405-
patch.length2 += len(diffText)
406-
start2 += len(diffText)
405+
patch.Length2 += len(diffText)
406+
Start2 += len(diffText)
407407
patch.diffs = append(patch.diffs, bigpatch.diffs[0])
408408
bigpatch.diffs = bigpatch.diffs[1:]
409409
empty = false
410410
} else if diffType == DiffDelete && len(patch.diffs) == 1 && patch.diffs[0].Type == DiffEqual && len(diffText) > 2*patchSize {
411411
// 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)
414414
empty = false
415415
patch.diffs = append(patch.diffs, Diff{diffType, diffText})
416416
bigpatch.diffs = bigpatch.diffs[1:]
417417
} else {
418418
// 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)]
420420

421-
patch.length1 += len(diffText)
422-
start1 += len(diffText)
421+
patch.Length1 += len(diffText)
422+
Start1 += len(diffText)
423423
if diffType == DiffEqual {
424-
patch.length2 += len(diffText)
425-
start2 += len(diffText)
424+
patch.Length2 += len(diffText)
425+
Start2 += len(diffText)
426426
} else {
427427
empty = false
428428
}
@@ -448,8 +448,8 @@ func (dmp *DiffMatchPatch) PatchSplitMax(patches []Patch) []Patch {
448448
}
449449

450450
if len(postcontext) != 0 {
451-
patch.length1 += len(postcontext)
452-
patch.length2 += len(postcontext)
451+
patch.Length1 += len(postcontext)
452+
patch.Length2 += len(postcontext)
453453
if len(patch.diffs) != 0 && patch.diffs[len(patch.diffs)-1].Type == DiffEqual {
454454
patch.diffs[len(patch.diffs)-1].Text += postcontext
455455
} else {
@@ -496,27 +496,27 @@ func (dmp *DiffMatchPatch) PatchFromText(textline string) ([]Patch, error) {
496496
patch = Patch{}
497497
m := patchHeader.FindStringSubmatch(text[textPointer])
498498

499-
patch.start1, _ = strconv.Atoi(m[1])
499+
patch.Start1, _ = strconv.Atoi(m[1])
500500
if len(m[2]) == 0 {
501-
patch.start1--
502-
patch.length1 = 1
501+
patch.Start1--
502+
patch.Length1 = 1
503503
} else if m[2] == "0" {
504-
patch.length1 = 0
504+
patch.Length1 = 0
505505
} else {
506-
patch.start1--
507-
patch.length1, _ = strconv.Atoi(m[2])
506+
patch.Start1--
507+
patch.Length1, _ = strconv.Atoi(m[2])
508508
}
509509

510-
patch.start2, _ = strconv.Atoi(m[3])
510+
patch.Start2, _ = strconv.Atoi(m[3])
511511

512512
if len(m[4]) == 0 {
513-
patch.start2--
514-
patch.length2 = 1
513+
patch.Start2--
514+
patch.Length2 = 1
515515
} else if m[4] == "0" {
516-
patch.length2 = 0
516+
patch.Length2 = 0
517517
} else {
518-
patch.start2--
519-
patch.length2, _ = strconv.Atoi(m[4])
518+
patch.Start2--
519+
patch.Length2, _ = strconv.Atoi(m[4])
520520
}
521521
textPointer++
522522

diffmatchpatch/patch_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ func TestPatchString(t *testing.T) {
2626
for i, tc := range []TestCase{
2727
{
2828
Patch: Patch{
29-
start1: 20,
30-
start2: 21,
31-
length1: 18,
32-
length2: 17,
29+
Start1: 20,
30+
Start2: 21,
31+
Length1: 18,
32+
Length2: 17,
3333

3434
diffs: []Diff{
3535
{DiffEqual, "jump"},

0 commit comments

Comments
 (0)