MCPcopy Create free account
hub / github.com/sergi/go-diff / String

Method String

diffmatchpatch/patch.go:33–71  ·  view source on GitHub ↗

String emulates GNU diff's format. Header: @@ -382,8 +481,9 @@ Indices are printed as 1-based, not 0-based.

()

Source from the content-addressed store, hash-verified

31// Header: @@ -382,8 +481,9 @@
32// Indices are printed as 1-based, not 0-based.
33func (p *Patch) String() string {
34 var coords1, coords2 string
35
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)
40 } else {
41 coords1 = strconv.Itoa(p.Start1+1) + "," + strconv.Itoa(p.Length1)
42 }
43
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)
48 } else {
49 coords2 = strconv.Itoa(p.Start2+1) + "," + strconv.Itoa(p.Length2)
50 }
51
52 var text bytes.Buffer
53 _, _ = text.WriteString("@@ -" + coords1 + " +" + coords2 + " @@\n")
54
55 // Escape the body of the patch with %xx notation.
56 for _, aDiff := range p.diffs {
57 switch aDiff.Type {
58 case DiffInsert:
59 _, _ = text.WriteString("+")
60 case DiffDelete:
61 _, _ = text.WriteString("-")
62 case DiffEqual:
63 _, _ = text.WriteString(" ")
64 }
65
66 _, _ = text.WriteString(strings.Replace(url.QueryEscape(aDiff.Text), "+", " ", -1))
67 _, _ = text.WriteString("\n")
68 }
69
70 return unescaper.Replace(text.String())
71}
72
73// PatchAddContext increases the context until it is unique, but doesn't let the pattern expand beyond MatchMaxBits.
74func (dmp *DiffMatchPatch) PatchAddContext(patch Patch, text string) Patch {

Callers 10

TestPatchStringFunction · 0.45
TestPatchFromTextFunction · 0.45
TestPatchAddContextFunction · 0.45
PatchToTextMethod · 0.45
DiffPrettyHtmlMethod · 0.45
DiffPrettyTextMethod · 0.45
DiffText1Method · 0.45
DiffText2Method · 0.45
DiffToDeltaMethod · 0.45
prettyFunction · 0.45

Calls

no outgoing calls

Tested by 4

TestPatchStringFunction · 0.36
TestPatchFromTextFunction · 0.36
TestPatchAddContextFunction · 0.36
prettyFunction · 0.36