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