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

Method String

diffmatchpatch/patch.go:68–106  ·  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

66// Header: @@ -382,8 +481,9 @@
67// Indices are printed as 1-based, not 0-based.
68func (p *Patch) String() string {
69 var coords1, coords2 string
70
71 if p.Length1 == 0 {
72 coords1 = strconv.Itoa(p.Start1) + ",0"
73 } else if p.Length1 == 1 {
74 coords1 = strconv.Itoa(p.Start1 + 1)
75 } else {
76 coords1 = strconv.Itoa(p.Start1+1) + "," + strconv.Itoa(p.Length1)
77 }
78
79 if p.Length2 == 0 {
80 coords2 = strconv.Itoa(p.Start2) + ",0"
81 } else if p.Length2 == 1 {
82 coords2 = strconv.Itoa(p.Start2 + 1)
83 } else {
84 coords2 = strconv.Itoa(p.Start2+1) + "," + strconv.Itoa(p.Length2)
85 }
86
87 var text bytes.Buffer
88 _, _ = text.WriteString("@@ -" + coords1 + " +" + coords2 + " @@\n")
89
90 // Escape the body of the patch with %xx notation.
91 for _, aDiff := range p.diffs {
92 switch aDiff.Type {
93 case DiffInsert:
94 _, _ = text.WriteString("+")
95 case DiffDelete:
96 _, _ = text.WriteString("-")
97 case DiffEqual:
98 _, _ = text.WriteString(" ")
99 }
100
101 _, _ = text.WriteString(strings.Replace(url.QueryEscape(aDiff.Text), "+", " ", -1))
102 _, _ = text.WriteString("\n")
103 }
104
105 return unescaper.Replace(text.String())
106}
107
108// PatchAddContext increases the context until it is unique, but doesn't let the pattern expand beyond MatchMaxBits.
109func (dmp *DiffMatchPatch) PatchAddContext(patch Patch, text string) Patch {

Callers 11

TestPatchStringFunction · 0.45
TestPatchFromTextFunction · 0.45
TestPatchAddContextFunction · 0.45
diffText1PrefixFunction · 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