ToPatch creates a Patch list from given Diffs output from DiffLines and the b strings from which the needed lines of source are copied. ApplyPatch with this on the a strings will result in the b strings. The resulting Patch is independent of bstr slice.
(bstr []string)
| 124 | // ApplyPatch with this on the a strings will result in the b strings. |
| 125 | // The resulting Patch is independent of bstr slice. |
| 126 | func (dif Diffs) ToPatch(bstr []string) Patch { |
| 127 | pt := make(Patch, len(dif)) |
| 128 | for pi, op := range dif { |
| 129 | pr := &PatchRec{Op: op} |
| 130 | if op.Tag == 'r' || op.Tag == 'i' { |
| 131 | nl := (op.J2 - op.J1) |
| 132 | pr.Blines = make([]string, nl) |
| 133 | for i := 0; i < nl; i++ { |
| 134 | pr.Blines[i] = bstr[op.J1+i] |
| 135 | } |
| 136 | } |
| 137 | pt[pi] = pr |
| 138 | } |
| 139 | return pt |
| 140 | } |
| 141 | |
| 142 | // Apply applies given Patch to given file as list of strings |
| 143 | // this does no checking except range checking so it won't crash |