String emulates GNU diff's format. Header: @@ -382,8 +481,9 @@ Indices are printed as 1-based, not 0-based.
()
| 66 | // Header: @@ -382,8 +481,9 @@ |
| 67 | // Indices are printed as 1-based, not 0-based. |
| 68 | func (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. |
| 109 | func (dmp *DiffMatchPatch) PatchAddContext(patch Patch, text string) Patch { |
no outgoing calls