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

Method patchMake2

diffmatchpatch/patch.go:184–262  ·  view source on GitHub ↗

patchMake2 computes a list of patches to turn text1 into text2. text2 is not provided, diffs are the delta between text1 and text2.

(text1 string, diffs []Diff)

Source from the content-addressed store, hash-verified

182// patchMake2 computes a list of patches to turn text1 into text2.
183// text2 is not provided, diffs are the delta between text1 and text2.
184func (dmp *DiffMatchPatch) patchMake2(text1 string, diffs []Diff) []Patch {
185 // Check for null inputs not needed since null can't be passed in C#.
186 patches := []Patch{}
187 if len(diffs) == 0 {
188 return patches // Get rid of the null case.
189 }
190
191 // DiffMain replaces invalid UTF-8 with the Unicode replacement character.
192 // Use that normalized source when it is exactly what the diffs describe,
193 // while preserving raw bytes in caller-supplied byte-level diffs.
194 if !utf8.ValidString(text1) {
195 diffText1 := dmp.DiffText1(diffs)
196 if string([]rune(text1)) == diffText1 {
197 text1 = diffText1
198 }
199 }
200
201 patch := Patch{}
202 charCount1 := 0 // Number of characters into the text1 string.
203 charCount2 := 0 // Number of characters into the text2 string.
204 // Start with text1 (prepatchText) and apply the diffs until we arrive at text2 (postpatchText). We recreate the patches one by one to determine context info.
205 prepatchText := text1
206 postpatchText := text1
207
208 for i, aDiff := range diffs {
209 if len(patch.diffs) == 0 && aDiff.Type != DiffEqual {
210 // A new patch starts here.
211 patch.Start1 = charCount1
212 patch.Start2 = charCount2
213 }
214
215 switch aDiff.Type {
216 case DiffInsert:
217 patch.diffs = append(patch.diffs, aDiff)
218 patch.Length2 += len(aDiff.Text)
219 postpatchText = postpatchText[:charCount2] +
220 aDiff.Text + postpatchText[charCount2:]
221 case DiffDelete:
222 patch.Length1 += len(aDiff.Text)
223 patch.diffs = append(patch.diffs, aDiff)
224 postpatchText = postpatchText[:charCount2] + postpatchText[charCount2+len(aDiff.Text):]
225 case DiffEqual:
226 if len(aDiff.Text) <= 2*dmp.PatchMargin &&
227 len(patch.diffs) != 0 && i != len(diffs)-1 {
228 // Small equality inside a patch.
229 patch.diffs = append(patch.diffs, aDiff)
230 patch.Length1 += len(aDiff.Text)
231 patch.Length2 += len(aDiff.Text)
232 }
233 if len(aDiff.Text) >= 2*dmp.PatchMargin {
234 // Time for a new patch.
235 if len(patch.diffs) != 0 {
236 patch = dmp.PatchAddContext(patch, prepatchText)
237 patches = append(patches, patch)
238 patch = Patch{}
239 // Unlike Unidiff, our patch lists have a rolling context. http://code.google.com/p/google-diff-match-patch/wiki/Unidiff Update prepatch text & pos to reflect the application of the just completed patch.
240 prepatchText = postpatchText
241 charCount1 = charCount2

Callers 1

PatchMakeMethod · 0.95

Calls 2

DiffText1Method · 0.95
PatchAddContextMethod · 0.95

Tested by

no test coverage detected