Debian Codegraph @

unified.go

raw symbols SCIP
1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package udiffscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/
6
7 import (
8 "fmtscip-go gomod github.com/golang/go/src go1.24 fmt/"
9 "logscip-go gomod github.com/golang/go/src go1.24 log/"
10 "regexpscip-go gomod github.com/golang/go/src go1.24 regexp/"
11 "strconvscip-go gomod github.com/golang/go/src go1.24 strconv/"
12 "stringsscip-go gomod github.com/golang/go/src go1.24 strings/"
13 )
14
15 // DefaultContextLines is the number of unchanged lines of surrounding
16 // context displayed by Unified. Use ToUnified to specify a different value.
17 const DefaultContextLinesscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/DefaultContextLines. = 3
18
19 // Unified returns a unified diff of the old and new strings.
20 // The old and new labels are the names of the old and new files.
21 // If the strings are equal, it returns the empty string.
22 func Unifiedscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/Unified().(oldLabel, newLabel, old, new string) string {
23 edits := Linesscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/Lines().(old, new)
24 unified, err := ToUnifiedscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/ToUnified().(oldLabel, newLabel, old, edits, DefaultContextLinesscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/DefaultContextLines.)
25 if err != nil {
26 // Can't happen: edits are consistent.
27 logscip-go gomod github.com/golang/go/src go1.24 log/.Fatalfscip-go gomod github.com/golang/go/src go1.24 log/Fatalf().("internal error in diff.Unified: %v", err)
28 }
29 return unified
30 }
31
32 // ToUnified applies the edits to content and returns a unified diff,
33 // with contextLines lines of (unchanged) context around each diff hunk.
34 // The old and new labels are the names of the content and result files.
35 // It returns an error if the edits are inconsistent; see ApplyEdits.
36 func ToUnifiedscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/ToUnified().(oldLabel, newLabel, content string, edits []Editscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/Edit#, contextLines int) (string, error) {
37 u, err := toUnifiedscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/toUnified().(oldLabel, newLabel, content, edits, contextLines)
38 if err != nil {
39 return "", err
40 }
41 return u.Stringscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/unified#String().(), nil
42 }
43
44 // unified represents a set of edits as a unified diff.
45 type unifiedscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/unified# struct {
46 // From is the name of the original file.
47 Fromscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/unified#From. string
48 // To is the name of the modified file.
49 Toscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/unified#To. string
50 // Hunks is the set of edit Hunks needed to transform the file content.
51 Hunksscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/unified#Hunks. []*hunkscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#
52 }
53
54 // Hunk represents a contiguous set of line edits to apply.
55 type hunkscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk# struct {
56 // The line in the original source where the hunk starts.
57 FromLinescip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#FromLine. int
58 // The line in the original source where the hunk finishes.
59 ToLinescip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#ToLine. int
60 // The set of line based edits to apply.
61 Linesscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#Lines. []linescip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/line#
62 }
63
64 // Line represents a single line operation to apply as part of a Hunk.
65 type linescip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/line# struct {
66 // Kind is the type of line this represents, deletion, insertion or copy.
67 Kindscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/line#Kind. OpKindscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/OpKind#
68 // Content is the Content of this line.
69 // For deletion it is the line being removed, for all others it is the line
70 // to put in the output.
71 Contentscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/line#Content. string
72 }
73
74 // OpKind is used to denote the type of operation a line represents.
75 type OpKindscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/OpKind# int
76
77 const (
78 // Delete is the operation kind for a line that is present in the input
79 // but not in the output.
80 Deletescip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/Delete. OpKindscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/OpKind# = iota
81 // Insert is the operation kind for a line that is new in the output.
82 Insertscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/Insert.
83 // Equal is the operation kind for a line that is the same in the input and
84 // output, often used to provide context around edited lines.
85 Equalscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/Equal.
86 )
87
88 // String returns a human readable representation of an OpKind. It is not
89 // intended for machine processing.
90 func (k OpKindscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/OpKind#) Stringscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/OpKind#String().() string {
91 switch k {
92 case Deletescip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/Delete.:
93 return "delete"
94 case Insertscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/Insert.:
95 return "insert"
96 case Equalscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/Equal.:
97 return "equal"
98 default:
99 panic("unknown operation kind")
100 }
101 }
102
103 // toUnified takes a file contents and a sequence of edits, and calculates
104 // a unified diff that represents those edits.
105 func toUnifiedscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/toUnified().(fromName, toName string, content string, edits []Editscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/Edit#, contextLines int) (unifiedscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/unified#, error) {
106 gap := contextLines * 2
107 u := unifiedscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/unified#{
108 Fromscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/unified#From.: fromName,
109 Toscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/unified#To.: toName,
110 }
111 if len(edits) == 0 {
112 return u, nil
113 }
114 var err error
115 edits, err = lineEditsscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/lineEdits().(content, edits) // expand to whole lines
116 if err != nil {
117 return u, err
118 }
119 lines, _ := splitLinesscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/splitLines().(content)
120 var h *hunkscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#
121 last := 0
122 toLine := 0
123 for _, edit := range edits {
124 // Compute the zero-based line numbers of the edit start and end.
125 // TODO(adonovan): opt: compute incrementally, avoid O(n^2).
126 start := stringsscip-go gomod github.com/golang/go/src go1.24 strings/.Countscip-go gomod github.com/golang/go/src go1.24 strings/Count().(content[:edit.Startscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/Edit#Start.], "\n")
127 end := stringsscip-go gomod github.com/golang/go/src go1.24 strings/.Countscip-go gomod github.com/golang/go/src go1.24 strings/Count().(content[:edit.Endscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/Edit#End.], "\n")
128 if edit.Endscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/Edit#End. == len(content) && len(content) > 0 && content[len(content)-1] != '\n' {
129 end++ // EOF counts as an implicit newline
130 }
131
132 switch {
133 case h != nil && start == last:
134 //direct extension
135 case h != nil && start <= last+gap:
136 //within range of previous lines, add the joiners
137 addEqualLinesscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/addEqualLines().(h, lines, last, start)
138 default:
139 //need to start a new hunk
140 if h != nil {
141 // add the edge to the previous hunk
142 addEqualLinesscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/addEqualLines().(h, lines, last, last+contextLines)
143 u.Hunksscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/unified#Hunks. = append(u.Hunksscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/unified#Hunks., h)
144 }
145 toLine += start - last
146 h = &hunkscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#{
147 FromLinescip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#FromLine.: start + 1,
148 ToLinescip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#ToLine.: toLine + 1,
149 }
150 // add the edge to the new hunk
151 delta := addEqualLinesscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/addEqualLines().(h, lines, start-contextLines, start)
152 h.FromLinescip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#FromLine. -= delta
153 h.ToLinescip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#ToLine. -= delta
154 }
155 last = start
156 for i := start; i < end; i++ {
157 h.Linesscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#Lines. = append(h.Linesscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#Lines., linescip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/line#{Kindscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/line#Kind.: Deletescip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/Delete., Contentscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/line#Content.: lines[i]})
158 last++
159 }
160 if edit.Newscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/Edit#New. != "" {
161 v, _ := splitLinesscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/splitLines().(edit.Newscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/Edit#New.)
162 for _, content := range v {
163 h.Linesscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#Lines. = append(h.Linesscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#Lines., linescip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/line#{Kindscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/line#Kind.: Insertscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/Insert., Contentscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/line#Content.: content})
164 toLine++
165 }
166 }
167 }
168 if h != nil {
169 // add the edge to the final hunk
170 addEqualLinesscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/addEqualLines().(h, lines, last, last+contextLines)
171 u.Hunksscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/unified#Hunks. = append(u.Hunksscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/unified#Hunks., h)
172 }
173 return u, nil
174 }
175
176 // split into lines removing a final empty line,
177 // and also return the offsets of the line beginnings.
178 func splitLinesscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/splitLines().(text string) ([]string, []int) {
179 var lines []string
180 offsets := []int{0}
181 start := 0
182 for i, r := range text {
183 if r == '\n' {
184 lines = append(lines, text[start:i+1])
185 start = i + 1
186 offsets = append(offsets, start)
187 }
188 }
189 if start < len(text) {
190 lines = append(lines, text[start:])
191 offsets = append(offsets, len(text))
192 }
193 return lines, offsets
194 }
195
196 func addEqualLinesscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/addEqualLines().(h *hunkscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#, lines []string, start, end int) int {
197 delta := 0
198 for i := start; i < end; i++ {
199 if i < 0 {
200 continue
201 }
202 if i >= len(lines) {
203 return delta
204 }
205 h.Linesscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#Lines. = append(h.Linesscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#Lines., linescip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/line#{Kindscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/line#Kind.: Equalscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/Equal., Contentscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/line#Content.: lines[i]})
206 delta++
207 }
208 return delta
209 }
210
211 // String converts a unified diff to the standard textual form for that diff.
212 // The output of this function can be passed to tools like patch.
213 func (u unifiedscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/unified#) Stringscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/unified#String().() string {
214 if len(u.Hunksscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/unified#Hunks.) == 0 {
215 return ""
216 }
217 b := new(stringsscip-go gomod github.com/golang/go/src go1.24 strings/.Builderscip-go gomod github.com/golang/go/src go1.24 strings/Builder#)
218 fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(b, "--- %s\n", u.Fromscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/unified#From.)
219 fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(b, "+++ %s\n", u.Toscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/unified#To.)
220 for _, hunk := range u.Hunksscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/unified#Hunks. {
221 fromCount, toCount := 0, 0
222 for _, l := range hunk.Linesscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#Lines. {
223 switch l.Kindscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/line#Kind. {
224 case Deletescip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/Delete.:
225 fromCount++
226 case Insertscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/Insert.:
227 toCount++
228 default:
229 fromCount++
230 toCount++
231 }
232 }
233 fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintscip-go gomod github.com/golang/go/src go1.24 fmt/Fprint().(b, "@@")
234 if fromCount > 1 {
235 fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(b, " -%d,%d", hunk.FromLinescip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#FromLine., fromCount)
236 } else if hunk.FromLinescip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#FromLine. == 1 && fromCount == 0 {
237 // Match odd GNU diff -u behavior adding to empty file.
238 fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(b, " -0,0")
239 } else {
240 fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(b, " -%d", hunk.FromLinescip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#FromLine.)
241 }
242 if toCount > 1 {
243 fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(b, " +%d,%d", hunk.ToLinescip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#ToLine., toCount)
244 } else if hunk.ToLinescip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#ToLine. == 1 && toCount == 0 {
245 // Match odd GNU diff -u behavior adding to empty file.
246 fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(b, " +0,0")
247 } else {
248 fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(b, " +%d", hunk.ToLinescip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#ToLine.)
249 }
250 fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintscip-go gomod github.com/golang/go/src go1.24 fmt/Fprint().(b, " @@\n")
251 for _, l := range hunk.Linesscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/hunk#Lines. {
252 switch l.Kindscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/line#Kind. {
253 case Deletescip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/Delete.:
254 fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(b, "-%s", l.Contentscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/line#Content.)
255 case Insertscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/Insert.:
256 fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(b, "+%s", l.Contentscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/line#Content.)
257 default:
258 fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(b, " %s", l.Contentscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/line#Content.)
259 }
260 if !stringsscip-go gomod github.com/golang/go/src go1.24 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.24 strings/HasSuffix().(l.Contentscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/line#Content., "\n") {
261 fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(b, "\n\\ No newline at end of file\n")
262 }
263 }
264 }
265 return b.Stringscip-go gomod github.com/golang/go/src go1.24 strings/Builder#String().()
266 }
267
268 // ApplyUnified applies the unified diffs.
269 func ApplyUnifiedscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/ApplyUnified().(udiffs, bef string) (string, error) {
270 before := stringsscip-go gomod github.com/golang/go/src go1.24 strings/.Splitscip-go gomod github.com/golang/go/src go1.24 strings/Split().(bef, "\n")
271 unif := stringsscip-go gomod github.com/golang/go/src go1.24 strings/.Splitscip-go gomod github.com/golang/go/src go1.24 strings/Split().(udiffs, "\n")
272 var got []string
273 left := 0
274 // parse and apply the unified diffs
275 for _, l := range unif {
276 if len(l) == 0 {
277 continue // probably the last line (from Split)
278 }
279 switch l[0] {
280 case '@': // The @@ line
281 m := atregexpscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/atregexp..FindStringSubmatchscip-go gomod github.com/golang/go/src go1.24 regexp/Regexp#FindStringSubmatch().(l)
282 fromLine, err := strconvscip-go gomod github.com/golang/go/src go1.24 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.24 strconv/Atoi().(m[1])
283 if err != nil {
284 return "", fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24 fmt/Errorf().("missing line number in %q", l)
285 }
286 // before is a slice, so0-based; fromLine is 1-based
287 for ; left < fromLine-1; left++ {
288 got = append(got, before[left])
289 }
290 case '+': // add this line
291 if stringsscip-go gomod github.com/golang/go/src go1.24 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.24 strings/HasPrefix().(l, "+++ ") {
292 continue
293 }
294 got = append(got, l[1:])
295 case '-': // delete this line
296 if stringsscip-go gomod github.com/golang/go/src go1.24 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.24 strings/HasPrefix().(l, "--- ") {
297 continue
298 }
299 left++
300 case ' ':
301 return "", fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24 fmt/Errorf().("unexpected line %q", l)
302 default:
303 return "", fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24 fmt/Errorf().("impossible unified %q", udiffs)
304 }
305 }
306 // copy any remaining lines
307 for ; left < len(before); left++ {
308 got = append(got, before[left])
309 }
310 return stringsscip-go gomod github.com/golang/go/src go1.24 strings/.Joinscip-go gomod github.com/golang/go/src go1.24 strings/Join().(got, "\n"), nil
311 }
312
313 // The first number in the @@ lines is the line number in the 'before' data
314 var atregexpscip-go gomod github.com/aymanbagabas/go-udiff . `github.com/aymanbagabas/go-udiff`/atregexp. = regexpscip-go gomod github.com/golang/go/src go1.24 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.24 regexp/MustCompile().(`@@ -(\d+).* @@`)