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

Method DiffToDelta

diffmatchpatch/diff.go:1230–1258  ·  view source on GitHub ↗

DiffToDelta crushes the diff into an encoded string which describes the operations required to transform text1 into text2. E.g. =3\t-2\t+ing -> Keep 3 chars, delete 2 chars, insert 'ing'. Operations are tab-separated. Inserted text is escaped using %xx notation.

(diffs []Diff)

Source from the content-addressed store, hash-verified

1228// DiffToDelta crushes the diff into an encoded string which describes the operations required to transform text1 into text2.
1229// E.g. =3\t-2\t+ing -> Keep 3 chars, delete 2 chars, insert 'ing'. Operations are tab-separated. Inserted text is escaped using %xx notation.
1230func (dmp *DiffMatchPatch) DiffToDelta(diffs []Diff) string {
1231 var text bytes.Buffer
1232 for _, aDiff := range diffs {
1233 switch aDiff.Type {
1234 case DiffInsert:
1235 _, _ = text.WriteString("+")
1236 _, _ = text.WriteString(strings.Replace(url.QueryEscape(aDiff.Text), "+", " ", -1))
1237 _, _ = text.WriteString("\t")
1238 break
1239 case DiffDelete:
1240 _, _ = text.WriteString("-")
1241 _, _ = text.WriteString(strconv.Itoa(utf8.RuneCountInString(aDiff.Text)))
1242 _, _ = text.WriteString("\t")
1243 break
1244 case DiffEqual:
1245 _, _ = text.WriteString("=")
1246 _, _ = text.WriteString(strconv.Itoa(utf8.RuneCountInString(aDiff.Text)))
1247 _, _ = text.WriteString("\t")
1248 break
1249 }
1250 }
1251 delta := text.String()
1252 if len(delta) != 0 {
1253 // Strip off trailing tab character.
1254 delta = delta[0 : utf8.RuneCountInString(delta)-1]
1255 delta = unescaper.Replace(delta)
1256 }
1257 return delta
1258}
1259
1260// DiffFromDelta given the original text1, and an encoded string which describes the operations required to transform text1 into text2, comAdde the full diff.
1261func (dmp *DiffMatchPatch) DiffFromDelta(text1 string, delta string) (diffs []Diff, err error) {

Callers 1

TestDiffDeltaFunction · 0.80

Calls 1

StringMethod · 0.45

Tested by 1

TestDiffDeltaFunction · 0.64