Skip to content

Commit 94d4893

Browse files
committed
fix: export diff slice in patch struct
closes sergi#94 contributor @ObviouslyGreen
1 parent 60dd882 commit 94d4893

File tree

2 files changed

+53
-53
lines changed

2 files changed

+53
-53
lines changed

Diff for: diffmatchpatch/patch.go

+51-51
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020

2121
// Patch represents one patch operation.
2222
type Patch struct {
23-
diffs []Diff
23+
Diffs []Diff
2424
Start1 int
2525
Start2 int
2626
Length1 int
@@ -53,7 +53,7 @@ func (p *Patch) String() string {
5353
_, _ = text.WriteString("@@ -" + coords1 + " +" + coords2 + " @@\n")
5454

5555
// Escape the body of the patch with %xx notation.
56-
for _, aDiff := range p.diffs {
56+
for _, aDiff := range p.Diffs {
5757
switch aDiff.Type {
5858
case DiffInsert:
5959
_, _ = text.WriteString("+")
@@ -94,12 +94,12 @@ func (dmp *DiffMatchPatch) PatchAddContext(patch Patch, text string) Patch {
9494
// Add the prefix.
9595
prefix := text[max(0, patch.Start2-padding):patch.Start2]
9696
if len(prefix) != 0 {
97-
patch.diffs = append([]Diff{{DiffEqual, prefix}}, patch.diffs...)
97+
patch.Diffs = append([]Diff{{DiffEqual, prefix}}, patch.Diffs...)
9898
}
9999
// Add the suffix.
100100
suffix := text[patch.Start2+patch.Length1 : min(len(text), patch.Start2+patch.Length1+padding)]
101101
if len(suffix) != 0 {
102-
patch.diffs = append(patch.diffs, Diff{DiffEqual, suffix})
102+
patch.Diffs = append(patch.Diffs, Diff{DiffEqual, suffix})
103103
}
104104

105105
// Roll back the start points.
@@ -155,33 +155,33 @@ func (dmp *DiffMatchPatch) patchMake2(text1 string, diffs []Diff) []Patch {
155155
postpatchText := text1
156156

157157
for i, aDiff := range diffs {
158-
if len(patch.diffs) == 0 && aDiff.Type != DiffEqual {
158+
if len(patch.Diffs) == 0 && aDiff.Type != DiffEqual {
159159
// A new patch starts here.
160160
patch.Start1 = charCount1
161161
patch.Start2 = charCount2
162162
}
163163

164164
switch aDiff.Type {
165165
case DiffInsert:
166-
patch.diffs = append(patch.diffs, aDiff)
166+
patch.Diffs = append(patch.Diffs, aDiff)
167167
patch.Length2 += len(aDiff.Text)
168168
postpatchText = postpatchText[:charCount2] +
169169
aDiff.Text + postpatchText[charCount2:]
170170
case DiffDelete:
171171
patch.Length1 += len(aDiff.Text)
172-
patch.diffs = append(patch.diffs, aDiff)
172+
patch.Diffs = append(patch.Diffs, aDiff)
173173
postpatchText = postpatchText[:charCount2] + postpatchText[charCount2+len(aDiff.Text):]
174174
case DiffEqual:
175175
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 {
177177
// Small equality inside a patch.
178-
patch.diffs = append(patch.diffs, aDiff)
178+
patch.Diffs = append(patch.Diffs, aDiff)
179179
patch.Length1 += len(aDiff.Text)
180180
patch.Length2 += len(aDiff.Text)
181181
}
182182
if len(aDiff.Text) >= 2*dmp.PatchMargin {
183183
// Time for a new patch.
184-
if len(patch.diffs) != 0 {
184+
if len(patch.Diffs) != 0 {
185185
patch = dmp.PatchAddContext(patch, prepatchText)
186186
patches = append(patches, patch)
187187
patch = Patch{}
@@ -204,7 +204,7 @@ func (dmp *DiffMatchPatch) patchMake2(text1 string, diffs []Diff) []Patch {
204204
}
205205

206206
// Pick up the leftover patch if not empty.
207-
if len(patch.diffs) != 0 {
207+
if len(patch.Diffs) != 0 {
208208
patch = dmp.PatchAddContext(patch, prepatchText)
209209
patches = append(patches, patch)
210210
}
@@ -217,8 +217,8 @@ func (dmp *DiffMatchPatch) PatchDeepCopy(patches []Patch) []Patch {
217217
patchesCopy := []Patch{}
218218
for _, aPatch := range patches {
219219
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{
222222
aDiff.Type,
223223
aDiff.Text,
224224
})
@@ -255,7 +255,7 @@ func (dmp *DiffMatchPatch) PatchApply(patches []Patch, text string) (string, []b
255255
results := make([]bool, len(patches))
256256
for _, aPatch := range patches {
257257
expectedLoc := aPatch.Start2 + delta
258-
text1 := dmp.DiffText1(aPatch.diffs)
258+
text1 := dmp.DiffText1(aPatch.Diffs)
259259
var startLoc int
260260
endLoc := -1
261261
if len(text1) > dmp.MatchMaxBits {
@@ -289,7 +289,7 @@ func (dmp *DiffMatchPatch) PatchApply(patches []Patch, text string) (string, []b
289289
}
290290
if text1 == text2 {
291291
// 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):]
293293
} else {
294294
// Imperfect match. Run a diff to get a framework of equivalent indices.
295295
diffs := dmp.DiffMain(text1, text2, false)
@@ -300,7 +300,7 @@ func (dmp *DiffMatchPatch) PatchApply(patches []Patch, text string) (string, []b
300300
} else {
301301
diffs = dmp.DiffCleanupSemanticLossless(diffs)
302302
index1 := 0
303-
for _, aDiff := range aPatch.diffs {
303+
for _, aDiff := range aPatch.Diffs {
304304
if aDiff.Type != DiffEqual {
305305
index2 := dmp.DiffXIndex(diffs, index1)
306306
if aDiff.Type == DiffInsert {
@@ -343,17 +343,17 @@ func (dmp *DiffMatchPatch) PatchAddPadding(patches []Patch) string {
343343
}
344344

345345
// 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 {
347347
// 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...)
349349
patches[0].Start1 -= paddingLength // Should be 0.
350350
patches[0].Start2 -= paddingLength // Should be 0.
351351
patches[0].Length1 += paddingLength
352352
patches[0].Length2 += paddingLength
353-
} else if paddingLength > len(patches[0].diffs[0].Text) {
353+
} else if paddingLength > len(patches[0].Diffs[0].Text) {
354354
// 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
357357
patches[0].Start1 -= extraLength
358358
patches[0].Start2 -= extraLength
359359
patches[0].Length1 += extraLength
@@ -362,17 +362,17 @@ func (dmp *DiffMatchPatch) PatchAddPadding(patches []Patch) string {
362362

363363
// Add some padding on end of last diff.
364364
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 {
367367
// 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})
369369
patches[last].Length1 += paddingLength
370370
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) {
372372
// Grow last equality.
373-
lastDiff := patches[last].diffs[len(patches[last].diffs)-1]
373+
lastDiff := patches[last].Diffs[len(patches[last].Diffs)-1]
374374
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]
376376
patches[last].Length1 += extraLength
377377
patches[last].Length2 += extraLength
378378
}
@@ -396,7 +396,7 @@ func (dmp *DiffMatchPatch) PatchSplitMax(patches []Patch) []Patch {
396396
Start1 := bigpatch.Start1
397397
Start2 := bigpatch.Start2
398398
precontext := ""
399-
for len(bigpatch.diffs) != 0 {
399+
for len(bigpatch.Diffs) != 0 {
400400
// Create one of several smaller patches.
401401
patch := Patch{}
402402
empty := true
@@ -405,25 +405,25 @@ func (dmp *DiffMatchPatch) PatchSplitMax(patches []Patch) []Patch {
405405
if len(precontext) != 0 {
406406
patch.Length1 = len(precontext)
407407
patch.Length2 = len(precontext)
408-
patch.diffs = append(patch.diffs, Diff{DiffEqual, precontext})
408+
patch.Diffs = append(patch.Diffs, Diff{DiffEqual, precontext})
409409
}
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
413413
if diffType == DiffInsert {
414414
// Insertions are harmless.
415415
patch.Length2 += len(diffText)
416416
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:]
419419
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 {
421421
// This is a large deletion. Let it pass in one chunk.
422422
patch.Length1 += len(diffText)
423423
Start1 += len(diffText)
424424
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:]
427427
} else {
428428
// Deletion or equality. Only take as much as we can stomach.
429429
diffText = diffText[:min(len(diffText), patchSize-patch.Length1-dmp.PatchMargin)]
@@ -436,33 +436,33 @@ func (dmp *DiffMatchPatch) PatchSplitMax(patches []Patch) []Patch {
436436
} else {
437437
empty = false
438438
}
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:]
442442
} else {
443-
bigpatch.diffs[0].Text = bigpatch.diffs[0].Text[len(diffText):]
443+
bigpatch.Diffs[0].Text = bigpatch.Diffs[0].Text[len(diffText):]
444444
}
445445
}
446446
}
447447
// Compute the head context for the next patch.
448-
precontext = dmp.DiffText2(patch.diffs)
448+
precontext = dmp.DiffText2(patch.Diffs)
449449
precontext = precontext[max(0, len(precontext)-dmp.PatchMargin):]
450450

451451
postcontext := ""
452452
// 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]
455455
} else {
456-
postcontext = dmp.DiffText1(bigpatch.diffs)
456+
postcontext = dmp.DiffText1(bigpatch.Diffs)
457457
}
458458

459459
if len(postcontext) != 0 {
460460
patch.Length1 += len(postcontext)
461461
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
464464
} else {
465-
patch.diffs = append(patch.diffs, Diff{DiffEqual, postcontext})
465+
patch.Diffs = append(patch.Diffs, Diff{DiffEqual, postcontext})
466466
}
467467
}
468468
if !empty {
@@ -542,13 +542,13 @@ func (dmp *DiffMatchPatch) PatchFromText(textline string) ([]Patch, error) {
542542
line, _ = url.QueryUnescape(line)
543543
if sign == '-' {
544544
// Deletion.
545-
patch.diffs = append(patch.diffs, Diff{DiffDelete, line})
545+
patch.Diffs = append(patch.Diffs, Diff{DiffDelete, line})
546546
} else if sign == '+' {
547547
// Insertion.
548-
patch.diffs = append(patch.diffs, Diff{DiffInsert, line})
548+
patch.Diffs = append(patch.Diffs, Diff{DiffInsert, line})
549549
} else if sign == ' ' {
550550
// Minor equality.
551-
patch.diffs = append(patch.diffs, Diff{DiffEqual, line})
551+
patch.Diffs = append(patch.Diffs, Diff{DiffEqual, line})
552552
} else if sign == '@' {
553553
// Start of next patch.
554554
break

Diff for: diffmatchpatch/patch_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestPatchString(t *testing.T) {
2929
Length1: 18,
3030
Length2: 17,
3131

32-
diffs: []Diff{
32+
Diffs: []Diff{
3333
{DiffEqual, "jump"},
3434
{DiffDelete, "s"},
3535
{DiffInsert, "ed"},
@@ -94,7 +94,7 @@ func TestPatchFromText(t *testing.T) {
9494
)
9595
assertEqual(t, 1, len(patches))
9696
assertEqual(t, diffs,
97-
patches[0].diffs,
97+
patches[0].Diffs,
9898
)
9999
assertEqual(t, nil, err)
100100
}

0 commit comments

Comments
 (0)