Source File
patch.go
Belonging Package
github.com/sergi/go-diff/diffmatchpatch
package diffmatchpatch
import (
)
func ( *Patch) () string {
var , string
if .Length1 == 0 {
= strconv.Itoa(.Start1) + ",0"
} else if .Length1 == 1 {
= strconv.Itoa(.Start1 + 1)
} else {
= strconv.Itoa(.Start1+1) + "," + strconv.Itoa(.Length1)
}
if .Length2 == 0 {
= strconv.Itoa(.Start2) + ",0"
} else if .Length2 == 1 {
= strconv.Itoa(.Start2 + 1)
} else {
= strconv.Itoa(.Start2+1) + "," + strconv.Itoa(.Length2)
}
var bytes.Buffer
_, _ = .WriteString("@@ -" + + " +" + + " @@\n")
for , := range .diffs {
switch .Type {
case DiffInsert:
_, _ = .WriteString("+")
case DiffDelete:
_, _ = .WriteString("-")
case DiffEqual:
_, _ = .WriteString(" ")
}
_, _ = .WriteString(strings.Replace(url.QueryEscape(.Text), "+", " ", -1))
_, _ = .WriteString("\n")
}
return unescaper.Replace(.String())
}
for strings.Index(, ) != strings.LastIndex(, ) &&
len() < .MatchMaxBits-2*.PatchMargin {
+= .PatchMargin
:= max(0, .Start2-)
:= min(len(), .Start2+.Length1+)
= [:]
+= .PatchMargin
func ( *DiffMatchPatch) ( ...interface{}) []Patch {
if len() == 1 {
, := [0].([]Diff)
:= .DiffText1()
return .(, )
} else if len() == 2 {
:= [0].(string)
switch t := [1].(type) {
case string:
:= .DiffMain(, , true)
if len() > 2 {
= .DiffCleanupSemantic()
= .DiffCleanupEfficiency()
}
return .(, )
case []Diff:
return .patchMake2(, )
}
} else if len() == 3 {
return .([0], [2])
}
return []Patch{}
}
:=
:=
for , := range {
if len(.diffs) != 0 {
= .PatchAddContext(, )
= append(, )
=
=
}
}
}
if .Type != DiffInsert {
+= len(.Text)
}
if .Type != DiffDelete {
+= len(.Text)
}
}
if len(.diffs) != 0 {
= .PatchAddContext(, )
= append(, )
}
return
}
= .PatchDeepCopy()
:= .PatchAddPadding()
= + +
= .PatchSplitMax()
= .MatchMain(, [:.MatchMaxBits], )
if != -1 {
= .MatchMain(,
[len()-.MatchMaxBits:], +len()-.MatchMaxBits)
= -1
}
}
} else {
= .MatchMain(, , )
}
[] = false
} else {
= .DiffCleanupSemanticLossless()
:= 0
for , := range .diffs {
if .Type != DiffEqual {
:= .DiffXIndex(, )
= [:+] + .Text + [+:]
:= +
= [:] +
[+.DiffXIndex(, +len(.Text))-:]
}
}
if .Type != DiffDelete {
+= len(.Text)
}
}
}
}
}
++
func ( *DiffMatchPatch) ( []Patch) string {
:= .PatchMargin
:= ""
for := 1; <= ; ++ {
+= string()
}
:= len() - 1
func ( *DiffMatchPatch) ( []Patch) []Patch {
:= .MatchMaxBits
for := 0; < len(); ++ {
if [].Length1 <= {
continue
}
= .DiffText2(.diffs)
= [max(0, len()-.PatchMargin):]
if len(.DiffText1(.diffs)) > .PatchMargin {
= .DiffText1(.diffs)[:.PatchMargin]
} else {
= .DiffText1(.diffs)
}
if len() != 0 {
.Length1 += len()
.Length2 += len()
if len(.diffs) != 0 && .diffs[len(.diffs)-1].Type == DiffEqual {
.diffs[len(.diffs)-1].Text +=
} else {
.diffs = append(.diffs, Diff{DiffEqual, })
}
}
if ! {
++
= append([:], append([]Patch{}, [:]...)...)
}
}
}
return
}
func ( *DiffMatchPatch) ( []Patch) string {
var bytes.Buffer
for , := range {
_, _ = .WriteString(.String())
}
return .String()
}
func ( *DiffMatchPatch) ( string) ([]Patch, error) {
:= []Patch{}
if len() == 0 {
return , nil
}
:= strings.Split(, "\n")
:= 0
:= regexp.MustCompile("^@@ -(\\d+),?(\\d*) \\+(\\d+),?(\\d*) @@$")
var Patch
var uint8
var string
for < len() {
if !.MatchString([]) {
return , errors.New("Invalid patch string: " + [])
}
= Patch{}
:= .FindStringSubmatch([])
.Start1, _ = strconv.Atoi([1])
if len([2]) == 0 {
.Start1--
.Length1 = 1
} else if [2] == "0" {
.Length1 = 0
} else {
.Start1--
.Length1, _ = strconv.Atoi([2])
}
.Start2, _ = strconv.Atoi([3])
if len([4]) == 0 {
.Start2--
.Length2 = 1
} else if [4] == "0" {
.Length2 = 0
} else {
.Start2--
.Length2, _ = strconv.Atoi([4])
}
++
for < len() {
if len([]) > 0 {
= [][0]
} else {
++
continue
}
= [][1:]
= strings.Replace(, "+", "%2b", -1)
, _ = url.QueryUnescape()
.diffs = append(.diffs, Diff{DiffDelete, })
.diffs = append(.diffs, Diff{DiffInsert, })
break
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |