Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export diff slice in patch struct #94

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 51 additions & 51 deletions diffmatchpatch/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

// Patch represents one patch operation.
type Patch struct {
diffs []Diff
Diffs []Diff
Start1 int
Start2 int
Length1 int
Expand Down Expand Up @@ -53,7 +53,7 @@ func (p *Patch) String() string {
_, _ = text.WriteString("@@ -" + coords1 + " +" + coords2 + " @@\n")

// Escape the body of the patch with %xx notation.
for _, aDiff := range p.diffs {
for _, aDiff := range p.Diffs {
switch aDiff.Type {
case DiffInsert:
_, _ = text.WriteString("+")
Expand Down Expand Up @@ -93,12 +93,12 @@ func (dmp *DiffMatchPatch) PatchAddContext(patch Patch, text string) Patch {
// Add the prefix.
prefix := text[max(0, patch.Start2-padding):patch.Start2]
if len(prefix) != 0 {
patch.diffs = append([]Diff{Diff{DiffEqual, prefix}}, patch.diffs...)
patch.Diffs = append([]Diff{Diff{DiffEqual, prefix}}, patch.Diffs...)
}
// Add the suffix.
suffix := text[patch.Start2+patch.Length1 : min(len(text), patch.Start2+patch.Length1+padding)]
if len(suffix) != 0 {
patch.diffs = append(patch.diffs, Diff{DiffEqual, suffix})
patch.Diffs = append(patch.Diffs, Diff{DiffEqual, suffix})
}

// Roll back the start points.
Expand Down Expand Up @@ -153,33 +153,33 @@ func (dmp *DiffMatchPatch) patchMake2(text1 string, diffs []Diff) []Patch {
postpatchText := text1

for i, aDiff := range diffs {
if len(patch.diffs) == 0 && aDiff.Type != DiffEqual {
if len(patch.Diffs) == 0 && aDiff.Type != DiffEqual {
// A new patch starts here.
patch.Start1 = charCount1
patch.Start2 = charCount2
}

switch aDiff.Type {
case DiffInsert:
patch.diffs = append(patch.diffs, aDiff)
patch.Diffs = append(patch.Diffs, aDiff)
patch.Length2 += len(aDiff.Text)
postpatchText = postpatchText[:charCount2] +
aDiff.Text + postpatchText[charCount2:]
case DiffDelete:
patch.Length1 += len(aDiff.Text)
patch.diffs = append(patch.diffs, aDiff)
patch.Diffs = append(patch.Diffs, aDiff)
postpatchText = postpatchText[:charCount2] + postpatchText[charCount2+len(aDiff.Text):]
case DiffEqual:
if len(aDiff.Text) <= 2*dmp.PatchMargin &&
len(patch.diffs) != 0 && i != len(diffs)-1 {
len(patch.Diffs) != 0 && i != len(diffs)-1 {
// Small equality inside a patch.
patch.diffs = append(patch.diffs, aDiff)
patch.Diffs = append(patch.Diffs, aDiff)
patch.Length1 += len(aDiff.Text)
patch.Length2 += len(aDiff.Text)
}
if len(aDiff.Text) >= 2*dmp.PatchMargin {
// Time for a new patch.
if len(patch.diffs) != 0 {
if len(patch.Diffs) != 0 {
patch = dmp.PatchAddContext(patch, prepatchText)
patches = append(patches, patch)
patch = Patch{}
Expand All @@ -200,7 +200,7 @@ func (dmp *DiffMatchPatch) patchMake2(text1 string, diffs []Diff) []Patch {
}

// Pick up the leftover patch if not empty.
if len(patch.diffs) != 0 {
if len(patch.Diffs) != 0 {
patch = dmp.PatchAddContext(patch, prepatchText)
patches = append(patches, patch)
}
Expand All @@ -213,8 +213,8 @@ func (dmp *DiffMatchPatch) PatchDeepCopy(patches []Patch) []Patch {
patchesCopy := []Patch{}
for _, aPatch := range patches {
patchCopy := Patch{}
for _, aDiff := range aPatch.diffs {
patchCopy.diffs = append(patchCopy.diffs, Diff{
for _, aDiff := range aPatch.Diffs {
patchCopy.Diffs = append(patchCopy.Diffs, Diff{
aDiff.Type,
aDiff.Text,
})
Expand Down Expand Up @@ -247,7 +247,7 @@ func (dmp *DiffMatchPatch) PatchApply(patches []Patch, text string) (string, []b
results := make([]bool, len(patches))
for _, aPatch := range patches {
expectedLoc := aPatch.Start2 + delta
text1 := dmp.DiffText1(aPatch.diffs)
text1 := dmp.DiffText1(aPatch.Diffs)
var startLoc int
endLoc := -1
if len(text1) > dmp.MatchMaxBits {
Expand Down Expand Up @@ -281,7 +281,7 @@ func (dmp *DiffMatchPatch) PatchApply(patches []Patch, text string) (string, []b
}
if text1 == text2 {
// Perfect match, just shove the Replacement text in.
text = text[:startLoc] + dmp.DiffText2(aPatch.diffs) + text[startLoc+len(text1):]
text = text[:startLoc] + dmp.DiffText2(aPatch.Diffs) + text[startLoc+len(text1):]
} else {
// Imperfect match. Run a diff to get a framework of equivalent indices.
diffs := dmp.DiffMain(text1, text2, false)
Expand All @@ -291,7 +291,7 @@ func (dmp *DiffMatchPatch) PatchApply(patches []Patch, text string) (string, []b
} else {
diffs = dmp.DiffCleanupSemanticLossless(diffs)
index1 := 0
for _, aDiff := range aPatch.diffs {
for _, aDiff := range aPatch.Diffs {
if aDiff.Type != DiffEqual {
index2 := dmp.DiffXIndex(diffs, index1)
if aDiff.Type == DiffInsert {
Expand Down Expand Up @@ -334,17 +334,17 @@ func (dmp *DiffMatchPatch) PatchAddPadding(patches []Patch) string {
}

// Add some padding on start of first diff.
if len(patches[0].diffs) == 0 || patches[0].diffs[0].Type != DiffEqual {
if len(patches[0].Diffs) == 0 || patches[0].Diffs[0].Type != DiffEqual {
// Add nullPadding equality.
patches[0].diffs = append([]Diff{Diff{DiffEqual, nullPadding}}, patches[0].diffs...)
patches[0].Diffs = append([]Diff{Diff{DiffEqual, nullPadding}}, patches[0].Diffs...)
patches[0].Start1 -= paddingLength // Should be 0.
patches[0].Start2 -= paddingLength // Should be 0.
patches[0].Length1 += paddingLength
patches[0].Length2 += paddingLength
} else if paddingLength > len(patches[0].diffs[0].Text) {
} else if paddingLength > len(patches[0].Diffs[0].Text) {
// Grow first equality.
extraLength := paddingLength - len(patches[0].diffs[0].Text)
patches[0].diffs[0].Text = nullPadding[len(patches[0].diffs[0].Text):] + patches[0].diffs[0].Text
extraLength := paddingLength - len(patches[0].Diffs[0].Text)
patches[0].Diffs[0].Text = nullPadding[len(patches[0].Diffs[0].Text):] + patches[0].Diffs[0].Text
patches[0].Start1 -= extraLength
patches[0].Start2 -= extraLength
patches[0].Length1 += extraLength
Expand All @@ -353,16 +353,16 @@ func (dmp *DiffMatchPatch) PatchAddPadding(patches []Patch) string {

// Add some padding on end of last diff.
last := len(patches) - 1
if len(patches[last].diffs) == 0 || patches[last].diffs[len(patches[last].diffs)-1].Type != DiffEqual {
if len(patches[last].Diffs) == 0 || patches[last].Diffs[len(patches[last].Diffs)-1].Type != DiffEqual {
// Add nullPadding equality.
patches[last].diffs = append(patches[last].diffs, Diff{DiffEqual, nullPadding})
patches[last].Diffs = append(patches[last].Diffs, Diff{DiffEqual, nullPadding})
patches[last].Length1 += paddingLength
patches[last].Length2 += paddingLength
} else if paddingLength > len(patches[last].diffs[len(patches[last].diffs)-1].Text) {
} else if paddingLength > len(patches[last].Diffs[len(patches[last].Diffs)-1].Text) {
// Grow last equality.
lastDiff := patches[last].diffs[len(patches[last].diffs)-1]
lastDiff := patches[last].Diffs[len(patches[last].Diffs)-1]
extraLength := paddingLength - len(lastDiff.Text)
patches[last].diffs[len(patches[last].diffs)-1].Text += nullPadding[:extraLength]
patches[last].Diffs[len(patches[last].Diffs)-1].Text += nullPadding[:extraLength]
patches[last].Length1 += extraLength
patches[last].Length2 += extraLength
}
Expand All @@ -386,7 +386,7 @@ func (dmp *DiffMatchPatch) PatchSplitMax(patches []Patch) []Patch {
Start1 := bigpatch.Start1
Start2 := bigpatch.Start2
precontext := ""
for len(bigpatch.diffs) != 0 {
for len(bigpatch.Diffs) != 0 {
// Create one of several smaller patches.
patch := Patch{}
empty := true
Expand All @@ -395,25 +395,25 @@ func (dmp *DiffMatchPatch) PatchSplitMax(patches []Patch) []Patch {
if len(precontext) != 0 {
patch.Length1 = len(precontext)
patch.Length2 = len(precontext)
patch.diffs = append(patch.diffs, Diff{DiffEqual, precontext})
patch.Diffs = append(patch.Diffs, Diff{DiffEqual, precontext})
}
for len(bigpatch.diffs) != 0 && patch.Length1 < patchSize-dmp.PatchMargin {
diffType := bigpatch.diffs[0].Type
diffText := bigpatch.diffs[0].Text
for len(bigpatch.Diffs) != 0 && patch.Length1 < patchSize-dmp.PatchMargin {
diffType := bigpatch.Diffs[0].Type
diffText := bigpatch.Diffs[0].Text
if diffType == DiffInsert {
// Insertions are harmless.
patch.Length2 += len(diffText)
Start2 += len(diffText)
patch.diffs = append(patch.diffs, bigpatch.diffs[0])
bigpatch.diffs = bigpatch.diffs[1:]
patch.Diffs = append(patch.Diffs, bigpatch.Diffs[0])
bigpatch.Diffs = bigpatch.Diffs[1:]
empty = false
} else if diffType == DiffDelete && len(patch.diffs) == 1 && patch.diffs[0].Type == DiffEqual && len(diffText) > 2*patchSize {
} else if diffType == DiffDelete && len(patch.Diffs) == 1 && patch.Diffs[0].Type == DiffEqual && len(diffText) > 2*patchSize {
// This is a large deletion. Let it pass in one chunk.
patch.Length1 += len(diffText)
Start1 += len(diffText)
empty = false
patch.diffs = append(patch.diffs, Diff{diffType, diffText})
bigpatch.diffs = bigpatch.diffs[1:]
patch.Diffs = append(patch.Diffs, Diff{diffType, diffText})
bigpatch.Diffs = bigpatch.Diffs[1:]
} else {
// Deletion or equality. Only take as much as we can stomach.
diffText = diffText[:min(len(diffText), patchSize-patch.Length1-dmp.PatchMargin)]
Expand All @@ -426,34 +426,34 @@ func (dmp *DiffMatchPatch) PatchSplitMax(patches []Patch) []Patch {
} else {
empty = false
}
patch.diffs = append(patch.diffs, Diff{diffType, diffText})
if diffText == bigpatch.diffs[0].Text {
bigpatch.diffs = bigpatch.diffs[1:]
patch.Diffs = append(patch.Diffs, Diff{diffType, diffText})
if diffText == bigpatch.Diffs[0].Text {
bigpatch.Diffs = bigpatch.Diffs[1:]
} else {
bigpatch.diffs[0].Text =
bigpatch.diffs[0].Text[len(diffText):]
bigpatch.Diffs[0].Text =
bigpatch.Diffs[0].Text[len(diffText):]
}
}
}
// Compute the head context for the next patch.
precontext = dmp.DiffText2(patch.diffs)
precontext = dmp.DiffText2(patch.Diffs)
precontext = precontext[max(0, len(precontext)-dmp.PatchMargin):]

postcontext := ""
// Append the end context for this patch.
if len(dmp.DiffText1(bigpatch.diffs)) > dmp.PatchMargin {
postcontext = dmp.DiffText1(bigpatch.diffs)[:dmp.PatchMargin]
if len(dmp.DiffText1(bigpatch.Diffs)) > dmp.PatchMargin {
postcontext = dmp.DiffText1(bigpatch.Diffs)[:dmp.PatchMargin]
} else {
postcontext = dmp.DiffText1(bigpatch.diffs)
postcontext = dmp.DiffText1(bigpatch.Diffs)
}

if len(postcontext) != 0 {
patch.Length1 += len(postcontext)
patch.Length2 += len(postcontext)
if len(patch.diffs) != 0 && patch.diffs[len(patch.diffs)-1].Type == DiffEqual {
patch.diffs[len(patch.diffs)-1].Text += postcontext
if len(patch.Diffs) != 0 && patch.Diffs[len(patch.Diffs)-1].Type == DiffEqual {
patch.Diffs[len(patch.Diffs)-1].Text += postcontext
} else {
patch.diffs = append(patch.diffs, Diff{DiffEqual, postcontext})
patch.Diffs = append(patch.Diffs, Diff{DiffEqual, postcontext})
}
}
if !empty {
Expand Down Expand Up @@ -533,13 +533,13 @@ func (dmp *DiffMatchPatch) PatchFromText(textline string) ([]Patch, error) {
line, _ = url.QueryUnescape(line)
if sign == '-' {
// Deletion.
patch.diffs = append(patch.diffs, Diff{DiffDelete, line})
patch.Diffs = append(patch.Diffs, Diff{DiffDelete, line})
} else if sign == '+' {
// Insertion.
patch.diffs = append(patch.diffs, Diff{DiffInsert, line})
patch.Diffs = append(patch.Diffs, Diff{DiffInsert, line})
} else if sign == ' ' {
// Minor equality.
patch.diffs = append(patch.diffs, Diff{DiffEqual, line})
patch.Diffs = append(patch.Diffs, Diff{DiffEqual, line})
} else if sign == '@' {
// Start of next patch.
break
Expand Down
4 changes: 2 additions & 2 deletions diffmatchpatch/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestPatchString(t *testing.T) {
Length1: 18,
Length2: 17,

diffs: []Diff{
Diffs: []Diff{
{DiffEqual, "jump"},
{DiffDelete, "s"},
{DiffInsert, "ed"},
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestPatchFromText(t *testing.T) {
patches, err := dmp.PatchFromText("@@ -1,21 +1,21 @@\n-%601234567890-=%5B%5D%5C;',./\n+~!@#$%25%5E&*()_+%7B%7D%7C:%22%3C%3E?\n")
assert.Len(t, patches, 1)
assert.Equal(t, diffs,
patches[0].diffs,
patches[0].Diffs,
)
assert.Nil(t, err)
}
Expand Down